1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.internal.telephony.imsphone;
18 
19 import android.annotation.AnyThread;
20 import android.annotation.NonNull;
21 import android.net.Uri;
22 import android.telephony.ims.ImsReasonInfo;
23 import android.telephony.ims.RegistrationManager;
24 import android.telephony.ims.aidl.IImsRegistrationCallback;
25 import android.util.Log;
26 
27 import java.util.concurrent.Executor;
28 
29 /**
30  * A helper class to manager the ImsRegistrationCallback can notify the state changed to listener.
31  */
32 @AnyThread
33 public class ImsRegistrationCallbackHelper {
34     private static final String TAG = "ImsRegCallbackHelper";
35 
36     /**
37      * The interface to receive IMS registration updated.
38      */
39     public interface ImsRegistrationUpdate {
40         /**
41          * Handle the callback when IMS is registered.
42          */
handleImsRegistered(int imsRadioTech)43         void handleImsRegistered(int imsRadioTech);
44 
45         /**
46          * Handle the callback when IMS is registering.
47          */
handleImsRegistering(int imsRadioTech)48         void handleImsRegistering(int imsRadioTech);
49 
50         /**
51          * Handle the callback when IMS is unregistered.
52          */
handleImsUnregistered(ImsReasonInfo imsReasonInfo)53         void handleImsUnregistered(ImsReasonInfo imsReasonInfo);
54 
55         /**
56          * Handle the callback when the list of subscriber {@link Uri}s associated with this IMS
57          * subscription changed.
58          */
handleImsSubscriberAssociatedUriChanged(Uri[] uris)59         void handleImsSubscriberAssociatedUriChanged(Uri[] uris);
60     }
61 
62     private ImsRegistrationUpdate mImsRegistrationUpdate;
63     private int mRegistrationState = RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED;
64     private final Object mLock = new Object();
65 
66     private final RegistrationManager.RegistrationCallback mImsRegistrationCallback =
67             new RegistrationManager.RegistrationCallback() {
68                 @Override
69                 public void onRegistered(int imsRadioTech) {
70                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_REGISTERED);
71                     mImsRegistrationUpdate.handleImsRegistered(imsRadioTech);
72                 }
73 
74                 @Override
75                 public void onRegistering(int imsRadioTech) {
76                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_REGISTERING);
77                     mImsRegistrationUpdate.handleImsRegistering(imsRadioTech);
78                 }
79 
80                 @Override
81                 public void onUnregistered(ImsReasonInfo imsReasonInfo) {
82                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED);
83                     mImsRegistrationUpdate.handleImsUnregistered(imsReasonInfo);
84                 }
85 
86                 @Override
87                 public void onSubscriberAssociatedUriChanged(Uri[] uris) {
88                     mImsRegistrationUpdate.handleImsSubscriberAssociatedUriChanged(uris);
89                 }
90             };
91 
ImsRegistrationCallbackHelper(@onNull ImsRegistrationUpdate registrationUpdate, Executor executor)92     public ImsRegistrationCallbackHelper(@NonNull ImsRegistrationUpdate registrationUpdate,
93             Executor executor) {
94         mImsRegistrationCallback.setExecutor(executor);
95         mImsRegistrationUpdate = registrationUpdate;
96     }
97 
98     /**
99      * Reset the IMS registration state.
100      */
reset()101     public void reset() {
102         Log.d(TAG, "reset");
103         updateRegistrationState(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED);
104     }
105 
106     /**
107      * Update the latest IMS registration state.
108      */
updateRegistrationState( @egistrationManager.ImsRegistrationState int newState)109     public synchronized void updateRegistrationState(
110             @RegistrationManager.ImsRegistrationState int newState) {
111         synchronized (mLock) {
112             Log.d(TAG, "updateRegistrationState: registration state from " + mRegistrationState
113                     + " to " + newState);
114             mRegistrationState = newState;
115         }
116     }
117 
getImsRegistrationState()118     public int getImsRegistrationState() {
119         synchronized (mLock) {
120             return mRegistrationState;
121         }
122     }
123 
isImsRegistered()124     public boolean isImsRegistered() {
125         return getImsRegistrationState() == RegistrationManager.REGISTRATION_STATE_REGISTERED;
126     }
127 
getCallback()128     public RegistrationManager.RegistrationCallback getCallback() {
129         return mImsRegistrationCallback;
130     }
131 
getCallbackBinder()132     public IImsRegistrationCallback getCallbackBinder() {
133         return mImsRegistrationCallback.getBinder();
134     }
135 }
136