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.ImsRegistrationAttributes;
24 import android.telephony.ims.RegistrationManager;
25 import android.telephony.ims.aidl.IImsRegistrationCallback;
26 import android.telephony.ims.stub.ImsRegistrationImplBase;
27 import android.util.Log;
28 
29 import java.util.concurrent.Executor;
30 
31 /**
32  * A helper class to manager the ImsRegistrationCallback can notify the state changed to listener.
33  */
34 @AnyThread
35 public class ImsRegistrationCallbackHelper {
36     private static final String TAG = "ImsRegCallbackHelper";
37 
38     /**
39      * The interface to receive IMS registration updated.
40      */
41     public interface ImsRegistrationUpdate {
42         /**
43          * Handle the callback when IMS is registered.
44          */
handleImsRegistered(@onNull ImsRegistrationAttributes attributes)45         void handleImsRegistered(@NonNull ImsRegistrationAttributes attributes);
46 
47         /**
48          * Handle the callback when IMS is registering.
49          */
handleImsRegistering(int imsRadioTech)50         void handleImsRegistering(int imsRadioTech);
51 
52         /**
53          * Handle the callback when IMS is unregistered.
54          */
handleImsUnregistered(ImsReasonInfo imsReasonInfo, @RegistrationManager.SuggestedAction int suggestedAction, @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech)55         void handleImsUnregistered(ImsReasonInfo imsReasonInfo,
56                 @RegistrationManager.SuggestedAction int suggestedAction,
57                 @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech);
58 
59         /**
60          * Handle the callback when the list of subscriber {@link Uri}s associated with this IMS
61          * subscription changed.
62          */
handleImsSubscriberAssociatedUriChanged(Uri[] uris)63         void handleImsSubscriberAssociatedUriChanged(Uri[] uris);
64     }
65 
66     private ImsRegistrationUpdate mImsRegistrationUpdate;
67     private int mRegistrationState = RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED;
68     private final Object mLock = new Object();
69 
70     private final RegistrationManager.RegistrationCallback mImsRegistrationCallback =
71             new RegistrationManager.RegistrationCallback() {
72                 @Override
73                 public void onRegistered(@NonNull ImsRegistrationAttributes attributes) {
74                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_REGISTERED);
75                     mImsRegistrationUpdate.handleImsRegistered(attributes);
76                 }
77 
78                 @Override
79                 public void onRegistering(int imsRadioTech) {
80                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_REGISTERING);
81                     mImsRegistrationUpdate.handleImsRegistering(imsRadioTech);
82                 }
83 
84                 @Override
85                 public void onUnregistered(ImsReasonInfo imsReasonInfo) {
86                     onUnregistered(imsReasonInfo, RegistrationManager.SUGGESTED_ACTION_NONE,
87                             ImsRegistrationImplBase.REGISTRATION_TECH_NONE);
88                 }
89 
90                 @Override
91                 public void onUnregistered(ImsReasonInfo imsReasonInfo,
92                         @RegistrationManager.SuggestedAction int suggestedAction,
93                         @ImsRegistrationImplBase.ImsRegistrationTech int imsRadioTech) {
94                     updateRegistrationState(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED);
95                     mImsRegistrationUpdate.handleImsUnregistered(imsReasonInfo, suggestedAction,
96                             imsRadioTech);
97                 }
98 
99                 @Override
100                 public void onSubscriberAssociatedUriChanged(Uri[] uris) {
101                     mImsRegistrationUpdate.handleImsSubscriberAssociatedUriChanged(uris);
102                 }
103             };
104 
ImsRegistrationCallbackHelper(@onNull ImsRegistrationUpdate registrationUpdate, Executor executor)105     public ImsRegistrationCallbackHelper(@NonNull ImsRegistrationUpdate registrationUpdate,
106             Executor executor) {
107         mImsRegistrationCallback.setExecutor(executor);
108         mImsRegistrationUpdate = registrationUpdate;
109     }
110 
111     /**
112      * Reset the IMS registration state.
113      */
reset()114     public void reset() {
115         Log.d(TAG, "reset");
116         updateRegistrationState(RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED);
117     }
118 
119     /**
120      * Update the latest IMS registration state.
121      */
updateRegistrationState( @egistrationManager.ImsRegistrationState int newState)122     public synchronized void updateRegistrationState(
123             @RegistrationManager.ImsRegistrationState int newState) {
124         synchronized (mLock) {
125             Log.d(TAG, "updateRegistrationState: registration state from "
126                     + RegistrationManager.registrationStateToString(mRegistrationState)
127                     + " to " + RegistrationManager.registrationStateToString(newState));
128             mRegistrationState = newState;
129         }
130     }
131 
getImsRegistrationState()132     public int getImsRegistrationState() {
133         synchronized (mLock) {
134             return mRegistrationState;
135         }
136     }
137 
isImsRegistered()138     public boolean isImsRegistered() {
139         return getImsRegistrationState() == RegistrationManager.REGISTRATION_STATE_REGISTERED;
140     }
141 
getCallback()142     public RegistrationManager.RegistrationCallback getCallback() {
143         return mImsRegistrationCallback;
144     }
145 
getCallbackBinder()146     public IImsRegistrationCallback getCallbackBinder() {
147         return mImsRegistrationCallback.getBinder();
148     }
149 }
150