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 "
113                     + RegistrationManager.registrationStateToString(mRegistrationState)
114                     + " to " + RegistrationManager.registrationStateToString(newState));
115             mRegistrationState = newState;
116         }
117     }
118 
getImsRegistrationState()119     public int getImsRegistrationState() {
120         synchronized (mLock) {
121             return mRegistrationState;
122         }
123     }
124 
isImsRegistered()125     public boolean isImsRegistered() {
126         return getImsRegistrationState() == RegistrationManager.REGISTRATION_STATE_REGISTERED;
127     }
128 
getCallback()129     public RegistrationManager.RegistrationCallback getCallback() {
130         return mImsRegistrationCallback;
131     }
132 
getCallbackBinder()133     public IImsRegistrationCallback getCallbackBinder() {
134         return mImsRegistrationCallback.getBinder();
135     }
136 }
137