blob: a7caab09cb4694bfc132939fdfcfed08a3284278 [file] [log] [blame]
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +00001/*
2 * Copyright (C) 2021 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
17package com.android.phone;
18
19import static android.telephony.ims.ImsStateCallback.REASON_IMS_SERVICE_DISCONNECTED;
20import static android.telephony.ims.ImsStateCallback.REASON_IMS_SERVICE_NOT_READY;
21import static android.telephony.ims.ImsStateCallback.REASON_NO_IMS_SERVICE_CONFIGURED;
22import static android.telephony.ims.ImsStateCallback.REASON_SUBSCRIPTION_INACTIVE;
23import static android.telephony.ims.ImsStateCallback.REASON_UNKNOWN_PERMANENT_ERROR;
24import static android.telephony.ims.ImsStateCallback.REASON_UNKNOWN_TEMPORARY_ERROR;
25import static android.telephony.ims.feature.ImsFeature.FEATURE_MMTEL;
26import static android.telephony.ims.feature.ImsFeature.FEATURE_RCS;
27import static android.telephony.ims.feature.ImsFeature.STATE_READY;
28import static android.telephony.ims.feature.ImsFeature.STATE_UNAVAILABLE;
29
30import static com.android.ims.FeatureConnector.UNAVAILABLE_REASON_DISCONNECTED;
31import static com.android.ims.FeatureConnector.UNAVAILABLE_REASON_IMS_UNSUPPORTED;
32import static com.android.ims.FeatureConnector.UNAVAILABLE_REASON_NOT_READY;
33import static com.android.ims.FeatureConnector.UNAVAILABLE_REASON_SERVER_UNAVAILABLE;
34
35import android.content.BroadcastReceiver;
36import android.content.Context;
37import android.content.Intent;
38import android.content.IntentFilter;
39import android.os.AsyncResult;
40import android.os.Bundle;
41import android.os.Handler;
42import android.os.HandlerThread;
43import android.os.IBinder;
44import android.os.Looper;
45import android.os.Message;
46import android.telephony.CarrierConfigManager;
47import android.telephony.SubscriptionManager;
48import android.telephony.TelephonyRegistryManager;
49import android.telephony.ims.feature.ImsFeature;
50import android.util.LocalLog;
51import android.util.Log;
52import android.util.SparseArray;
53
54import com.android.ims.FeatureConnector;
55import com.android.ims.ImsManager;
56import com.android.ims.RcsFeatureManager;
57import com.android.internal.annotations.VisibleForTesting;
58import com.android.internal.telephony.IImsStateCallback;
59import com.android.internal.telephony.Phone;
60import com.android.internal.telephony.PhoneConfigurationManager;
61import com.android.internal.telephony.PhoneFactory;
62import com.android.internal.telephony.ims.ImsResolver;
63import com.android.internal.telephony.util.HandlerExecutor;
64import com.android.internal.util.IndentingPrintWriter;
65import com.android.services.telephony.rcs.RcsFeatureController;
66import com.android.telephony.Rlog;
67
68import java.util.ArrayList;
69import java.util.Arrays;
70import java.util.HashMap;
71import java.util.concurrent.Executor;
72
73/**
74 * Implementation of the controller managing {@link ImsStateCallback}s
75 */
76public class ImsStateCallbackController {
77 private static final String TAG = "ImsStateCallbackController";
78 private static final boolean VDBG = false;
79 private static final int LOG_SIZE = 50;
80
81 /**
82 * Create a FeatureConnector for this class to use to connect to an ImsManager.
83 */
84 @VisibleForTesting
85 public interface MmTelFeatureConnectorFactory {
86 /**
87 * Create a FeatureConnector for this class to use to connect to an ImsManager.
88 * @param listener will receive ImsManager instance.
89 * @param executor that the Listener callbacks will be called on.
90 * @return A FeatureConnector
91 */
92 FeatureConnector<ImsManager> create(Context context, int slotId,
93 String logPrefix, FeatureConnector.Listener<ImsManager> listener,
94 Executor executor);
95 }
96
97 /**
98 * Create a FeatureConnector for this class to use to connect to an RcsFeatureManager.
99 */
100 @VisibleForTesting
101 public interface RcsFeatureConnectorFactory {
102 /**
103 * Create a FeatureConnector for this class to use to connect to an RcsFeatureManager.
104 * @param listener will receive RcsFeatureManager instance.
105 * @param executor that the Listener callbacks will be called on.
106 * @return A FeatureConnector
107 */
108 FeatureConnector<RcsFeatureManager> create(Context context, int slotId,
109 FeatureConnector.Listener<RcsFeatureManager> listener,
110 Executor executor, String logPrefix);
111 }
112
113 /** Indicates that the state is not valid, used in ExternalRcsFeatureState only */
114 private static final int STATE_UNKNOWN = -1;
115
116 /** The unavailable reason of ImsFeature is not initialized */
117 private static final int NOT_INITIALIZED = -1;
118 /** The ImsFeature is available. */
119 private static final int AVAILABLE = 0;
120
121 private static final int EVENT_SUB_CHANGED = 1;
122 private static final int EVENT_REGISTER_CALLBACK = 2;
123 private static final int EVENT_UNREGISTER_CALLBACK = 3;
124 private static final int EVENT_CARRIER_CONFIG_CHANGED = 4;
125 private static final int EVENT_EXTERNAL_RCS_STATE_CHANGED = 5;
126 private static final int EVENT_MSIM_CONFIGURATION_CHANGE = 6;
127
128 private static ImsStateCallbackController sInstance;
129 private static final LocalLog sLocalLog = new LocalLog(LOG_SIZE);
130
131 /**
132 * get the instance
133 */
134 public static ImsStateCallbackController getInstance() {
135 synchronized (ImsStateCallbackController.class) {
136 return sInstance;
137 }
138 }
139
140 private final PhoneGlobals mApp;
141 private final Handler mHandler;
142 private final ImsResolver mImsResolver;
143 private final SparseArray<MmTelFeatureListener> mMmTelFeatureListeners = new SparseArray<>();
144 private final SparseArray<RcsFeatureListener> mRcsFeatureListeners = new SparseArray<>();
145
146 private final SubscriptionManager mSubscriptionManager;
147 private final TelephonyRegistryManager mTelephonyRegistryManager;
148 private MmTelFeatureConnectorFactory mMmTelFeatureFactory;
149 private RcsFeatureConnectorFactory mRcsFeatureFactory;
150
151 private HashMap<IBinder, CallbackWrapper> mWrappers = new HashMap<>();
152
153 private final Object mDumpLock = new Object();
154
155 private int mNumSlots;
156
157 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
158 @Override
159 public void onReceive(Context context, Intent intent) {
160 if (intent == null) {
161 return;
162 }
163 if (CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED.equals(intent.getAction())) {
164 Bundle bundle = intent.getExtras();
165 if (bundle == null) {
166 return;
167 }
168 int slotId = bundle.getInt(CarrierConfigManager.EXTRA_SLOT_INDEX,
169 SubscriptionManager.INVALID_PHONE_INDEX);
170 int subId = bundle.getInt(CarrierConfigManager.EXTRA_SUBSCRIPTION_INDEX,
171 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
172
173 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
174 loge("onReceive ACTION_CARRIER_CONFIG_CHANGED invalid slotId");
175 return;
176 }
177
178 if (subId <= SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
179 loge("onReceive ACTION_CARRIER_CONFIG_CHANGED invalid subId");
180 //subscription changed will be notified by mSubChangedListener
181 return;
182 }
183
184 notifyCarrierConfigChanged(slotId);
185 }
186 }
187 };
188
189 private final SubscriptionManager.OnSubscriptionsChangedListener mSubChangedListener =
190 new SubscriptionManager.OnSubscriptionsChangedListener() {
191 @Override
192 public void onSubscriptionsChanged() {
193 if (!mHandler.hasMessages(EVENT_SUB_CHANGED)) {
194 mHandler.sendEmptyMessage(EVENT_SUB_CHANGED);
195 }
196 }
197 };
198
199 private final class MyHandler extends Handler {
200 MyHandler(Looper looper) {
201 super(looper);
202 }
203
204 @Override
205 public void handleMessage(Message msg) {
206 if (VDBG) logv("handleMessage: " + msg);
207 synchronized (mDumpLock) {
208 switch (msg.what) {
209 case EVENT_SUB_CHANGED:
210 onSubChanged();
211 break;
212
213 case EVENT_REGISTER_CALLBACK:
214 onRegisterCallback((ImsStateCallbackController.CallbackWrapper) msg.obj);
215 break;
216
217 case EVENT_UNREGISTER_CALLBACK:
218 onUnregisterCallback((IImsStateCallback) msg.obj);
219 break;
220
221 case EVENT_CARRIER_CONFIG_CHANGED:
222 onCarrierConfigChanged(msg.arg1);
223 break;
224
225 case EVENT_EXTERNAL_RCS_STATE_CHANGED:
226 if (msg.obj == null) break;
227 onExternalRcsStateChanged((ExternalRcsFeatureState) msg.obj);
228 break;
229
230 case EVENT_MSIM_CONFIGURATION_CHANGE:
231 AsyncResult result = (AsyncResult) msg.obj;
232 Integer numSlots = (Integer) result.result;
233 if (numSlots == null) {
234 Log.w(TAG, "msim config change with null num slots");
235 break;
236 }
237 updateFeatureControllerSize(numSlots);
238 break;
239
240 default:
241 loge("Unhandled event " + msg.what);
242 }
243 }
244 }
245 }
246
247 private final class MmTelFeatureListener implements FeatureConnector.Listener<ImsManager> {
248 private FeatureConnector<ImsManager> mConnector;
249 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
250 private int mState = STATE_UNAVAILABLE;
251 private int mReason = REASON_IMS_SERVICE_DISCONNECTED;
252
253 /*
254 * Remember the last return of verifyImsMmTelConfigured().
255 * true means ImsResolver found an IMS package for FEATURE_MMTEL.
256 *
257 * mReason is updated through connectionUnavailable triggered by ImsResolver.
258 * mHasConfig is update through notifyConfigChanged triggered by mReceiver.
259 * mHasConfig can be a redundancy of (mReason == REASON_NO_IMS_SERVICE_CONFIGURED).
260 * However, when a carrier config changes, we are not sure the order
261 * of execution of connectionUnavailable and notifyConfigChanged.
262 * So, it's safe to use a separated state to retain it.
263 * We assume mHasConfig is true, until it's determined explicitly.
264 */
265 private boolean mHasConfig = true;
266
267 private int mSlotId = -1;
268 private String mLogPrefix = "";
269
270 MmTelFeatureListener(int slotId) {
271 mSlotId = slotId;
272 mLogPrefix = "[" + slotId + ", MMTEL] ";
273 if (VDBG) logv(mLogPrefix + "created");
274
275 mConnector = mMmTelFeatureFactory.create(
276 mApp, slotId, TAG, this, new HandlerExecutor(mHandler));
277 mConnector.connect();
278 }
279
280 void setSubId(int subId) {
281 if (VDBG) logv(mLogPrefix + "setSubId mSubId=" + mSubId + ", subId=" + subId);
282 if (mSubId == subId) return;
283 logd(mLogPrefix + "setSubId changed subId=" + subId);
284
285 mSubId = subId;
286 }
287
288 void destroy() {
289 if (VDBG) logv(mLogPrefix + "destroy");
290 mConnector.disconnect();
291 mConnector = null;
292 }
293
294 @Override
Jonggeon Kim0da9a7b2021-12-23 08:56:50 +0000295 public void connectionReady(ImsManager manager, int subId) {
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000296 logd(mLogPrefix + "connectionReady " + subId);
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000297
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000298 mSubId = subId;
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000299 mState = STATE_READY;
300 mReason = AVAILABLE;
301 mHasConfig = true;
302 onFeatureStateChange(mSubId, FEATURE_MMTEL, mState, mReason);
303 }
304
305 @Override
306 public void connectionUnavailable(int reason) {
307 logd(mLogPrefix + "connectionUnavailable reason=" + connectorReasonToString(reason));
308
309 reason = convertReasonType(reason);
310 if (mReason == reason) return;
311
312 connectionUnavailableInternal(reason);
313 }
314
315 private void connectionUnavailableInternal(int reason) {
316 mState = STATE_UNAVAILABLE;
317 mReason = reason;
318
319 /* If having no IMS package for MMTEL,
320 * dicard the reason except REASON_NO_IMS_SERVICE_CONFIGURED. */
321 if (!mHasConfig && reason != REASON_NO_IMS_SERVICE_CONFIGURED) return;
322
323 onFeatureStateChange(mSubId, FEATURE_MMTEL, mState, mReason);
324 }
325
326 void notifyConfigChanged(boolean hasConfig) {
327 if (mHasConfig == hasConfig) return;
328
329 logd(mLogPrefix + "notifyConfigChanged " + hasConfig);
330
331 mHasConfig = hasConfig;
332 if (hasConfig) {
333 // REASON_NO_IMS_SERVICE_CONFIGURED is already reported to the clients,
334 // since there is no configuration of IMS package for MMTEL.
335 // Now, a carrier configuration change is notified and
336 // the response from ImsResolver is changed from false to true.
337 if (mState != STATE_READY) {
338 if (mReason == REASON_NO_IMS_SERVICE_CONFIGURED) {
339 // In this case, notify clients the reason, REASON_DISCONNCTED,
340 // to update the state.
341 connectionUnavailable(UNAVAILABLE_REASON_DISCONNECTED);
342 } else {
343 // ImsResolver and ImsStateCallbackController run with different Looper.
344 // In this case, FeatureConnectorListener is updated ahead of this.
345 // But, connectionUnavailable didn't notify clients since mHasConfig is
346 // false. So, notify clients here.
347 connectionUnavailableInternal(mReason);
348 }
349 }
350 } else {
351 // FeatureConnector doesn't report UNAVAILABLE_REASON_IMS_UNSUPPORTED,
352 // so report the reason here.
353 connectionUnavailable(UNAVAILABLE_REASON_IMS_UNSUPPORTED);
354 }
355 }
356
357 // called from onRegisterCallback
358 boolean notifyState(CallbackWrapper wrapper) {
359 if (VDBG) logv(mLogPrefix + "notifyState subId=" + wrapper.mSubId);
360
361 return wrapper.notifyState(mSubId, FEATURE_MMTEL, mState, mReason);
362 }
363
364 void dump(IndentingPrintWriter pw) {
365 pw.println("Listener={slotId=" + mSlotId
366 + ", subId=" + mSubId
367 + ", state=" + ImsFeature.STATE_LOG_MAP.get(mState)
368 + ", reason=" + imsStateReasonToString(mReason)
369 + ", hasConfig=" + mHasConfig
370 + "}");
371 }
372 }
373
374 private final class RcsFeatureListener implements FeatureConnector.Listener<RcsFeatureManager> {
375 private FeatureConnector<RcsFeatureManager> mConnector;
376 private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
377 private int mState = STATE_UNAVAILABLE;
378 private int mReason = REASON_IMS_SERVICE_DISCONNECTED;
379
380 /*
381 * Remember the last return of verifyImsMmTelConfigured().
382 * true means ImsResolver found an IMS package for FEATURE_RCS.
383 *
384 * mReason is updated through connectionUnavailable triggered by ImsResolver.
385 * mHasConfig is update through notifyConfigChanged triggered by mReceiver,
386 * and notifyExternalRcsState which triggered by TelephonyRcsService refers it.
387 * mHasConfig can be a redundancy of (mReason == REASON_NO_IMS_SERVICE_CONFIGURED).
388 * However, when a carrier config changes, we are not sure the order
389 * of execution of connectionUnavailable, notifyConfigChanged and notifyExternalRcsState.
390 * So, it's safe to use a separated state to retain it.
391 * We assume mHasConfig is true, until it's determined explicitly.
392 */
393 private boolean mHasConfig = true;
394
395 /*
396 * TelephonyRcsService doesn’t try to connect to RcsFeature if there is no active feature
397 * for a given subscription. The active features are declared by carrier configs and
398 * configuration resources. The APIs of ImsRcsManager and SipDelegateManager are available
399 * only when the RcsFeatureController has a STATE_READY state connection.
400 * This configuration is different from the configuration of IMS package for RCS.
401 * ImsStateCallbackController's FeatureConnectorListener can be STATE_READY state,
402 * even in case there is no active RCS feature. But Manager's APIs throws exception.
403 *
404 * For RCS, in addition to mHasConfig, the sate of TelephonyRcsService and
405 * RcsFeatureConnector will be traced to determine the state to be notified to clients.
406 */
407 private ExternalRcsFeatureState mExternalState = null;
408
409 private int mSlotId = -1;
410 private String mLogPrefix = "";
411
412 RcsFeatureListener(int slotId) {
413 mSlotId = slotId;
414 mLogPrefix = "[" + slotId + ", RCS] ";
415 if (VDBG) logv(mLogPrefix + "created");
416
417 mConnector = mRcsFeatureFactory.create(
418 mApp, slotId, this, new HandlerExecutor(mHandler), TAG);
419 mConnector.connect();
420 }
421
422 void setSubId(int subId) {
423 if (VDBG) logv(mLogPrefix + "setSubId mSubId=" + mSubId + ", subId=" + subId);
424 if (mSubId == subId) return;
425 logd(mLogPrefix + "setSubId changed subId=" + subId);
426
427 mSubId = subId;
428 }
429
430 void destroy() {
431 if (VDBG) logv(mLogPrefix + "destroy");
432
433 mConnector.disconnect();
434 mConnector = null;
435 }
436
437 @Override
Jonggeon Kim0da9a7b2021-12-23 08:56:50 +0000438 public void connectionReady(RcsFeatureManager manager, int subId) {
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000439 logd(mLogPrefix + "connectionReady " + subId);
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000440
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000441 mSubId = subId;
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000442 mState = STATE_READY;
443 mReason = AVAILABLE;
444 mHasConfig = true;
445
446 if (mExternalState != null && mExternalState.isReady()) {
447 onFeatureStateChange(mSubId, FEATURE_RCS, mState, mReason);
448 }
449 }
450
451 @Override
452 public void connectionUnavailable(int reason) {
453 logd(mLogPrefix + "connectionUnavailable reason=" + connectorReasonToString(reason));
454
455 reason = convertReasonType(reason);
456 if (mReason == reason) return;
457
458 connectionUnavailableInternal(reason);
459 }
460
461 private void connectionUnavailableInternal(int reason) {
462 mState = STATE_UNAVAILABLE;
463 mReason = reason;
464
465 /* If having no IMS package for RCS,
466 * dicard the reason except REASON_NO_IMS_SERVICE_CONFIGURED. */
467 if (!mHasConfig && reason != REASON_NO_IMS_SERVICE_CONFIGURED) return;
468
469 if (mExternalState == null && reason != REASON_NO_IMS_SERVICE_CONFIGURED) {
470 // Wait until TelephonyRcsService notifies its state.
471 return;
472 }
473
474 if (mExternalState != null && !mExternalState.hasActiveFeatures()) {
475 // notifyExternalRcsState has notified REASON_NO_IMS_SERVICE_CONFIGURED already
476 // ignore it
477 return;
478 }
479
480 if ((mExternalState != null && mExternalState.hasActiveFeatures())
481 || mReason == REASON_NO_IMS_SERVICE_CONFIGURED) {
482 onFeatureStateChange(mSubId, FEATURE_RCS, mState, mReason);
483 }
484 }
485
486 void notifyConfigChanged(boolean hasConfig) {
487 if (mHasConfig == hasConfig) return;
488
489 logd(mLogPrefix + "notifyConfigChanged " + hasConfig);
490
491 mHasConfig = hasConfig;
492 if (hasConfig) {
493 // REASON_NO_IMS_SERVICE_CONFIGURED is already reported to the clients,
494 // since there is no configuration of IMS package for RCS.
495 // Now, a carrier configuration change is notified and
496 // the response from ImsResolver is changed from false to true.
497 if (mState != STATE_READY) {
498 if (mReason == REASON_NO_IMS_SERVICE_CONFIGURED) {
499 // In this case, notify clients the reason, REASON_DISCONNCTED,
500 // to update the state.
501 connectionUnavailable(UNAVAILABLE_REASON_DISCONNECTED);
502 } else {
503 // ImsResolver and ImsStateCallbackController run with different Looper.
504 // In this case, FeatureConnectorListener is updated ahead of this.
505 // But, connectionUnavailable didn't notify clients since mHasConfig is
506 // false. So, notify clients here.
507 connectionUnavailableInternal(mReason);
508 }
509 }
510 } else {
511 // FeatureConnector doesn't report UNAVAILABLE_REASON_IMS_UNSUPPORTED,
512 // so report the reason here.
513 connectionUnavailable(UNAVAILABLE_REASON_IMS_UNSUPPORTED);
514 }
515 }
516
517 void notifyExternalRcsState(ExternalRcsFeatureState fs) {
518 if (VDBG) {
519 logv(mLogPrefix + "notifyExternalRcsState"
520 + " state=" + (fs.mState == STATE_UNKNOWN
521 ? "" : ImsFeature.STATE_LOG_MAP.get(fs.mState))
522 + ", reason=" + imsStateReasonToString(fs.mReason));
523 }
524
525 ExternalRcsFeatureState oldFs = mExternalState;
526 // External state is from TelephonyRcsService while a feature is added or removed.
527 if (fs.mState == STATE_UNKNOWN) {
528 if (oldFs != null) fs.mState = oldFs.mState;
529 else fs.mState = STATE_UNAVAILABLE;
530 }
531
532 mExternalState = fs;
533
534 // No IMS package found.
535 // REASON_NO_IMS_SERVICE_CONFIGURED is notified to clients already.
536 if (!mHasConfig) return;
537
538 if (fs.hasActiveFeatures()) {
539 if (mState == STATE_READY) {
540 if ((oldFs == null || !oldFs.isReady()) && fs.isReady()) {
541 // it is waiting RcsFeatureConnector's notification.
542 // notify clients here.
543 onFeatureStateChange(mSubId, FEATURE_RCS, mState, mReason);
544 } else if (!fs.isReady()) {
545 // Wait RcsFeatureConnector's notification
546 } else {
547 // ignore duplicated notification
548 }
549 }
550 } else {
551 // notify only once
552 if (oldFs == null || oldFs.hasActiveFeatures()) {
553 if (mReason != REASON_NO_IMS_SERVICE_CONFIGURED) {
554 onFeatureStateChange(
555 mSubId, FEATURE_RCS, STATE_UNAVAILABLE,
556 REASON_NO_IMS_SERVICE_CONFIGURED);
557 }
558 } else {
559 // ignore duplicated notification
560 }
561 }
562 }
563
564 // called from onRegisterCallback
565 boolean notifyState(CallbackWrapper wrapper) {
566 if (VDBG) logv(mLogPrefix + "notifyState subId=" + wrapper.mSubId);
567
568 if (mHasConfig) {
569 if (mExternalState == null) {
570 // Wait until TelephonyRcsService notifies its state.
571 return wrapper.notifyState(mSubId, FEATURE_RCS, STATE_UNAVAILABLE,
572 REASON_IMS_SERVICE_DISCONNECTED);
573 } else if (!mExternalState.hasActiveFeatures()) {
574 return wrapper.notifyState(mSubId, FEATURE_RCS, STATE_UNAVAILABLE,
575 REASON_NO_IMS_SERVICE_CONFIGURED);
576 }
577 }
578
579 return wrapper.notifyState(mSubId, FEATURE_RCS, mState, mReason);
580 }
581
582 void dump(IndentingPrintWriter pw) {
583 pw.println("Listener={slotId=" + mSlotId
584 + ", subId=" + mSubId
585 + ", state=" + ImsFeature.STATE_LOG_MAP.get(mState)
586 + ", reason=" + imsStateReasonToString(mReason)
587 + ", hasConfig=" + mHasConfig
588 + ", isReady=" + (mExternalState == null ? false : mExternalState.isReady())
589 + ", hasFeatures=" + (mExternalState == null ? false
590 : mExternalState.hasActiveFeatures())
591 + "}");
592 }
593 }
594
595 /**
596 * A wrapper class for the callback registered
597 */
598 private static class CallbackWrapper {
599 private final int mSubId;
600 private final int mRequiredFeature;
601 private final IImsStateCallback mCallback;
602 private final IBinder mBinder;
603 private final String mCallingPackage;
604 private int mLastReason = NOT_INITIALIZED;
605
606 CallbackWrapper(int subId, int feature, IImsStateCallback callback,
607 String callingPackage) {
608 mSubId = subId;
609 mRequiredFeature = feature;
610 mCallback = callback;
611 mBinder = callback.asBinder();
612 mCallingPackage = callingPackage;
613 }
614
615 /**
616 * @return false when accessing callback binder throws an Exception.
617 * That means the callback binder is not valid any longer.
618 * The death of remote process can cause this.
619 * This instance shall be removed from the list.
620 */
621 boolean notifyState(int subId, int feature, int state, int reason) {
622 if (VDBG) {
623 logv("CallbackWrapper notifyState subId=" + subId
624 + ", feature=" + ImsFeature.FEATURE_LOG_MAP.get(feature)
625 + ", state=" + ImsFeature.STATE_LOG_MAP.get(state)
626 + ", reason=" + imsStateReasonToString(reason));
627 }
628
629 try {
630 if (state == STATE_READY) {
631 mCallback.onAvailable();
632 } else {
633 mCallback.onUnavailable(reason);
634 }
635 mLastReason = reason;
636 } catch (Exception e) {
637 loge("CallbackWrapper notifyState e=" + e);
638 return false;
639 }
640
641 return true;
642 }
643
644 void notifyInactive() {
645 if (VDBG) logv("CallbackWrapper notifyInactive subId=" + mSubId);
646
647 try {
648 mCallback.onUnavailable(REASON_SUBSCRIPTION_INACTIVE);
649 } catch (Exception e) {
650 // ignored
651 }
652 }
653
654 void dump(IndentingPrintWriter pw) {
655 pw.println("CallbackWrapper={subId=" + mSubId
656 + ", feature=" + ImsFeature.FEATURE_LOG_MAP.get(mRequiredFeature)
657 + ", reason=" + imsStateReasonToString(mLastReason)
658 + ", pkg=" + mCallingPackage
659 + "}");
660 }
661 }
662
663 private static class ExternalRcsFeatureState {
664 private int mSlotId;
665 private int mState = STATE_UNAVAILABLE;
666 private int mReason = NOT_INITIALIZED;
667
668 ExternalRcsFeatureState(int slotId, int state, int reason) {
669 mSlotId = slotId;
670 mState = state;
671 mReason = reason;
672 }
673
674 boolean hasActiveFeatures() {
675 return mReason != REASON_NO_IMS_SERVICE_CONFIGURED;
676 }
677
678 boolean isReady() {
679 return mState == STATE_READY;
680 }
681 }
682
683 /**
684 * create an instance
685 */
686 public static ImsStateCallbackController make(PhoneGlobals app, int numSlots) {
687 synchronized (ImsStateCallbackController.class) {
688 if (sInstance == null) {
689 logd("ImsStateCallbackController created");
690
691 HandlerThread handlerThread = new HandlerThread(TAG);
692 handlerThread.start();
693 sInstance = new ImsStateCallbackController(app, handlerThread.getLooper(), numSlots,
694 ImsManager::getConnector, RcsFeatureManager::getConnector,
695 ImsResolver.getInstance());
696 }
697 }
698 return sInstance;
699 }
700
701 @VisibleForTesting
702 public ImsStateCallbackController(PhoneGlobals app, Looper looper, int numSlots,
703 MmTelFeatureConnectorFactory mmTelFactory, RcsFeatureConnectorFactory rcsFactory,
704 ImsResolver imsResolver) {
705 mApp = app;
706 mHandler = new MyHandler(looper);
707 mImsResolver = imsResolver;
708 mSubscriptionManager = mApp.getSystemService(SubscriptionManager.class);
709 mTelephonyRegistryManager = mApp.getSystemService(TelephonyRegistryManager.class);
710 mMmTelFeatureFactory = mmTelFactory;
711 mRcsFeatureFactory = rcsFactory;
712
713 updateFeatureControllerSize(numSlots);
714
715 mTelephonyRegistryManager.addOnSubscriptionsChangedListener(
716 mSubChangedListener, mSubChangedListener.getHandlerExecutor());
717
718 PhoneConfigurationManager.registerForMultiSimConfigChange(mHandler,
719 EVENT_MSIM_CONFIGURATION_CHANGE, null);
720
721 mApp.registerReceiver(mReceiver, new IntentFilter(
722 CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED));
723
724 onSubChanged();
725 }
726
727 /**
728 * Update the number of {@link RcsFeatureController}s that are created based on the number of
729 * active slots on the device.
730 */
731 @VisibleForTesting
732 public void updateFeatureControllerSize(int newNumSlots) {
733 if (mNumSlots != newNumSlots) {
734 logd("updateFeatures: oldSlots=" + mNumSlots
735 + ", newNumSlots=" + newNumSlots);
736 if (mNumSlots < newNumSlots) {
737 for (int i = mNumSlots; i < newNumSlots; i++) {
738 MmTelFeatureListener m = new MmTelFeatureListener(i);
739 mMmTelFeatureListeners.put(i, m);
740 RcsFeatureListener r = new RcsFeatureListener(i);
741 mRcsFeatureListeners.put(i, r);
742 }
743 } else {
744 for (int i = (mNumSlots - 1); i > (newNumSlots - 1); i--) {
745 MmTelFeatureListener m = mMmTelFeatureListeners.get(i);
746 if (m != null) {
747 mMmTelFeatureListeners.remove(i);
748 m.destroy();
749 }
750 RcsFeatureListener r = mRcsFeatureListeners.get(i);
751 if (r != null) {
752 mRcsFeatureListeners.remove(i);
753 r.destroy();
754 }
755 }
756 }
757 }
758 mNumSlots = newNumSlots;
759 }
760
761 /**
762 * Dependencies for testing.
763 */
764 @VisibleForTesting
765 public void onSubChanged() {
766 for (int i = 0; i < mMmTelFeatureListeners.size(); i++) {
767 MmTelFeatureListener l = mMmTelFeatureListeners.valueAt(i);
768 l.setSubId(getSubId(i));
769 }
770
771 for (int i = 0; i < mRcsFeatureListeners.size(); i++) {
772 RcsFeatureListener l = mRcsFeatureListeners.valueAt(i);
773 l.setSubId(getSubId(i));
774 }
775
776 if (mWrappers.size() == 0) return;
777
778 ArrayList<IBinder> inactiveCallbacks = new ArrayList<>();
779 final int[] activeSubs = mSubscriptionManager.getActiveSubscriptionIdList();
780
781 if (VDBG) logv("onSubChanged activeSubs=" + Arrays.toString(activeSubs));
782
783 // Remove callbacks for inactive subscriptions
784 for (IBinder binder : mWrappers.keySet()) {
785 CallbackWrapper wrapper = mWrappers.get(binder);
786 if (wrapper != null) {
787 if (!isActive(activeSubs, wrapper.mSubId)) {
788 // inactive subscription
789 inactiveCallbacks.add(binder);
790 }
791 } else {
792 // unexpected, remove it
793 inactiveCallbacks.add(binder);
794 }
795 }
796 removeInactiveCallbacks(inactiveCallbacks, "onSubChanged");
797 }
798
799 private void onFeatureStateChange(int subId, int feature, int state, int reason) {
800 if (VDBG) {
801 logv("onFeatureStateChange subId=" + subId
802 + ", feature=" + ImsFeature.FEATURE_LOG_MAP.get(feature)
803 + ", state=" + ImsFeature.STATE_LOG_MAP.get(state)
804 + ", reason=" + imsStateReasonToString(reason));
805 }
806
807 ArrayList<IBinder> inactiveCallbacks = new ArrayList<>();
808 mWrappers.values().forEach(wrapper -> {
809 if (subId == wrapper.mSubId
810 && feature == wrapper.mRequiredFeature
811 && !wrapper.notifyState(subId, feature, state, reason)) {
812 // callback has exception, remove it
813 inactiveCallbacks.add(wrapper.mBinder);
814 }
815 });
816 removeInactiveCallbacks(inactiveCallbacks, "onFeatureStateChange");
817 }
818
819 private void onRegisterCallback(CallbackWrapper wrapper) {
820 if (wrapper == null) return;
821
822 if (VDBG) logv("onRegisterCallback before size=" + mWrappers.size());
823 if (VDBG) {
824 logv("onRegisterCallback subId=" + wrapper.mSubId
825 + ", feature=" + wrapper.mRequiredFeature);
826 }
827
828 // Not sure the following case can happen or not:
829 // step1) Subscription changed
830 // step2) ImsStateCallbackController not processed onSubChanged yet
831 // step3) Client registers with a strange subId
832 // The validity of the subId is checked PhoneInterfaceManager#registerImsStateCallback.
833 // So, register the wrapper here before trying to notifyState.
834 // TODO: implement the recovery for this case, notifying the current reson, in onSubChanged
835 mWrappers.put(wrapper.mBinder, wrapper);
836
837 if (wrapper.mRequiredFeature == FEATURE_MMTEL) {
838 for (int i = 0; i < mMmTelFeatureListeners.size(); i++) {
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000839 if (wrapper.mSubId == getSubId(i)) {
840 MmTelFeatureListener l = mMmTelFeatureListeners.valueAt(i);
841 if (!l.notifyState(wrapper)) {
842 mWrappers.remove(wrapper.mBinder);
843 }
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000844 break;
845 }
846 }
847 } else if (wrapper.mRequiredFeature == FEATURE_RCS) {
848 for (int i = 0; i < mRcsFeatureListeners.size(); i++) {
Hunsuk Choif34a6b22022-01-11 03:41:34 +0000849 if (wrapper.mSubId == getSubId(i)) {
850 RcsFeatureListener l = mRcsFeatureListeners.valueAt(i);
851 if (!l.notifyState(wrapper)) {
852 mWrappers.remove(wrapper.mBinder);
853 }
Hunsuk Choic7ebc0f2021-11-15 23:46:41 +0000854 break;
855 }
856 }
857 }
858
859 if (VDBG) logv("onRegisterCallback after size=" + mWrappers.size());
860 }
861
862 private void onUnregisterCallback(IImsStateCallback cb) {
863 if (cb == null) return;
864 mWrappers.remove(cb.asBinder());
865 }
866
867 private void onCarrierConfigChanged(int slotId) {
868 if (slotId >= mNumSlots) {
869 logd("onCarrierConfigChanged invalid slotId "
870 + slotId + ", mNumSlots=" + mNumSlots);
871 return;
872 }
873
874 logv("onCarrierConfigChanged slotId=" + slotId);
875
876 boolean hasConfig = verifyImsMmTelConfigured(slotId);
877 if (slotId < mMmTelFeatureListeners.size()) {
878 MmTelFeatureListener listener = mMmTelFeatureListeners.valueAt(slotId);
879 listener.notifyConfigChanged(hasConfig);
880 }
881
882 hasConfig = verifyImsRcsConfigured(slotId);
883 if (slotId < mRcsFeatureListeners.size()) {
884 RcsFeatureListener listener = mRcsFeatureListeners.valueAt(slotId);
885 listener.notifyConfigChanged(hasConfig);
886 }
887 }
888
889 private void onExternalRcsStateChanged(ExternalRcsFeatureState fs) {
890 logv("onExternalRcsStateChanged slotId=" + fs.mSlotId
891 + ", state=" + (fs.mState == STATE_UNKNOWN
892 ? "" : ImsFeature.STATE_LOG_MAP.get(fs.mState))
893 + ", reason=" + imsStateReasonToString(fs.mReason));
894
895 RcsFeatureListener listener = mRcsFeatureListeners.get(fs.mSlotId);
896 if (listener != null) {
897 listener.notifyExternalRcsState(fs);
898 } else {
899 // unexpected state
900 loge("onExternalRcsStateChanged slotId=" + fs.mSlotId + ", no listener.");
901 }
902 }
903
904 /**
905 * Interface to be notified from TelephonyRcsSerice and RcsFeatureController
906 *
907 * @param ready true if feature's state is STATE_READY. Valid only when it is true.
908 * @param hasActiveFeatures true if the RcsFeatureController has active features.
909 */
910 public void notifyExternalRcsStateChanged(
911 int slotId, boolean ready, boolean hasActiveFeatures) {
912 int state = STATE_UNKNOWN;
913 int reason = REASON_IMS_SERVICE_DISCONNECTED;
914
915 if (ready) {
916 // From RcsFeatureController
917 state = STATE_READY;
918 reason = AVAILABLE;
919 } else if (!hasActiveFeatures) {
920 // From TelephonyRcsService
921 reason = REASON_NO_IMS_SERVICE_CONFIGURED;
922 state = STATE_UNAVAILABLE;
923 } else {
924 // From TelephonyRcsService
925 // TelephonyRcsService doesn't know the exact state of FeatureConnection.
926 // Only when there is no feature, we can assume the state.
927 }
928
929 if (VDBG) {
930 logv("notifyExternalRcsStateChanged slotId=" + slotId
931 + ", ready=" + ready
932 + ", hasActiveFeatures=" + hasActiveFeatures);
933 }
934
935 ExternalRcsFeatureState fs = new ExternalRcsFeatureState(slotId, state, reason);
936 mHandler.sendMessage(mHandler.obtainMessage(EVENT_EXTERNAL_RCS_STATE_CHANGED, fs));
937 }
938
939 /**
940 * Notifies carrier configuration has changed.
941 */
942 @VisibleForTesting
943 public void notifyCarrierConfigChanged(int slotId) {
944 if (VDBG) logv("notifyCarrierConfigChanged slotId=" + slotId);
945 mHandler.sendMessage(mHandler.obtainMessage(EVENT_CARRIER_CONFIG_CHANGED, slotId, 0));
946 }
947 /**
948 * Register IImsStateCallback
949 *
950 * @param feature for which state is changed, ImsFeature.FEATURE_*
951 */
952 public void registerImsStateCallback(int subId, int feature, IImsStateCallback cb,
953 String callingPackage) {
954 if (VDBG) {
955 logv("registerImsStateCallback subId=" + subId
956 + ", feature=" + feature + ", pkg=" + callingPackage);
957 }
958
959 CallbackWrapper wrapper = new CallbackWrapper(subId, feature, cb, callingPackage);
960 mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_CALLBACK, wrapper));
961 }
962
963 /**
964 * Unegister previously registered callback
965 */
966 public void unregisterImsStateCallback(IImsStateCallback cb) {
967 if (VDBG) logv("unregisterImsStateCallback");
968
969 mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_CALLBACK, cb));
970 }
971
972 private void removeInactiveCallbacks(
973 ArrayList<IBinder> inactiveCallbacks, String message) {
974 if (inactiveCallbacks == null || inactiveCallbacks.size() == 0) return;
975
976 if (VDBG) {
977 logv("removeInactiveCallbacks size="
978 + inactiveCallbacks.size() + " from " + message);
979 }
980
981 for (IBinder binder : inactiveCallbacks) {
982 CallbackWrapper wrapper = mWrappers.get(binder);
983 if (wrapper != null) {
984 // Send the reason REASON_SUBSCRIPTION_INACTIVE to the client
985 wrapper.notifyInactive();
986 mWrappers.remove(binder);
987 }
988 }
989 inactiveCallbacks.clear();
990 }
991
992 private int getSubId(int slotId) {
993 Phone phone = mPhoneFactoryProxy.getPhone(slotId);
994 if (phone != null) return phone.getSubId();
995 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
996 }
997
998 private static boolean isActive(final int[] activeSubs, int subId) {
999 for (int i : activeSubs) {
1000 if (i == subId) return true;
1001 }
1002 return false;
1003 }
1004
1005 private static int convertReasonType(int reason) {
1006 switch(reason) {
1007 case UNAVAILABLE_REASON_NOT_READY:
1008 return REASON_IMS_SERVICE_NOT_READY;
1009 case UNAVAILABLE_REASON_IMS_UNSUPPORTED:
1010 return REASON_NO_IMS_SERVICE_CONFIGURED;
1011 default:
1012 break;
1013 }
1014
1015 return REASON_IMS_SERVICE_DISCONNECTED;
1016 }
1017
1018 private boolean verifyImsMmTelConfigured(int slotId) {
1019 boolean ret = false;
1020 if (mImsResolver == null) {
1021 loge("verifyImsMmTelConfigured mImsResolver is null");
1022 } else {
1023 ret = mImsResolver.isImsServiceConfiguredForFeature(slotId, FEATURE_MMTEL);
1024 }
1025 if (VDBG) logv("verifyImsMmTelConfigured slotId=" + slotId + ", ret=" + ret);
1026 return ret;
1027 }
1028
1029 private boolean verifyImsRcsConfigured(int slotId) {
1030 boolean ret = false;
1031 if (mImsResolver == null) {
1032 loge("verifyImsRcsConfigured mImsResolver is null");
1033 } else {
1034 ret = mImsResolver.isImsServiceConfiguredForFeature(slotId, FEATURE_RCS);
1035 }
1036 if (VDBG) logv("verifyImsRcsConfigured slotId=" + slotId + ", ret=" + ret);
1037 return ret;
1038 }
1039
1040 private static String connectorReasonToString(int reason) {
1041 switch(reason) {
1042 case UNAVAILABLE_REASON_DISCONNECTED:
1043 return "DISCONNECTED";
1044 case UNAVAILABLE_REASON_NOT_READY:
1045 return "NOT_READY";
1046 case UNAVAILABLE_REASON_IMS_UNSUPPORTED:
1047 return "IMS_UNSUPPORTED";
1048 case UNAVAILABLE_REASON_SERVER_UNAVAILABLE:
1049 return "SERVER_UNAVAILABLE";
1050 default:
1051 break;
1052 }
1053 return "";
1054 }
1055
1056 private static String imsStateReasonToString(int reason) {
1057 switch(reason) {
1058 case AVAILABLE:
1059 return "READY";
1060 case REASON_UNKNOWN_TEMPORARY_ERROR:
1061 return "UNKNOWN_TEMPORARY_ERROR";
1062 case REASON_UNKNOWN_PERMANENT_ERROR:
1063 return "UNKNOWN_PERMANENT_ERROR";
1064 case REASON_IMS_SERVICE_DISCONNECTED:
1065 return "IMS_SERVICE_DISCONNECTED";
1066 case REASON_NO_IMS_SERVICE_CONFIGURED:
1067 return "NO_IMS_SERVICE_CONFIGURED";
1068 case REASON_SUBSCRIPTION_INACTIVE:
1069 return "SUBSCRIPTION_INACTIVE";
1070 case REASON_IMS_SERVICE_NOT_READY:
1071 return "IMS_SERVICE_NOT_READY";
1072 default:
1073 break;
1074 }
1075 return "";
1076 }
1077
1078 /**
1079 * PhoneFactory Dependencies for testing.
1080 */
1081 @VisibleForTesting
1082 public interface PhoneFactoryProxy {
1083 /**
1084 * Override getPhone for testing.
1085 */
1086 Phone getPhone(int index);
1087 }
1088
1089 private PhoneFactoryProxy mPhoneFactoryProxy = new PhoneFactoryProxy() {
1090 @Override
1091 public Phone getPhone(int index) {
1092 return PhoneFactory.getPhone(index);
1093 }
1094 };
1095
1096 private void release() {
1097 if (VDBG) logv("release");
1098
1099 mTelephonyRegistryManager.removeOnSubscriptionsChangedListener(mSubChangedListener);
1100 mApp.unregisterReceiver(mReceiver);
1101
1102 for (int i = 0; i < mMmTelFeatureListeners.size(); i++) {
1103 mMmTelFeatureListeners.valueAt(i).destroy();
1104 }
1105 mMmTelFeatureListeners.clear();
1106
1107 for (int i = 0; i < mRcsFeatureListeners.size(); i++) {
1108 mRcsFeatureListeners.valueAt(i).destroy();
1109 }
1110 mRcsFeatureListeners.clear();
1111 }
1112
1113 /**
1114 * destroy the instance
1115 */
1116 @VisibleForTesting
1117 public void destroy() {
1118 if (VDBG) logv("destroy it");
1119
1120 release();
1121 mHandler.getLooper().quit();
1122 }
1123
1124 /**
1125 * get the handler
1126 */
1127 @VisibleForTesting
1128 public Handler getHandler() {
1129 return mHandler;
1130 }
1131
1132 /**
1133 * Determine whether the callback is registered or not
1134 */
1135 @VisibleForTesting
1136 public boolean isRegistered(IImsStateCallback cb) {
1137 if (cb == null) return false;
1138 return mWrappers.containsKey(cb.asBinder());
1139 }
1140
1141 /**
1142 * Dump this instance into a readable format for dumpsys usage.
1143 */
1144 public void dump(IndentingPrintWriter pw) {
1145 pw.increaseIndent();
1146 synchronized (mDumpLock) {
1147 pw.println("CallbackWrappers:");
1148 pw.increaseIndent();
1149 mWrappers.values().forEach(wrapper -> wrapper.dump(pw));
1150 pw.decreaseIndent();
1151 pw.println("MmTelFeatureListeners:");
1152 pw.increaseIndent();
1153 for (int i = 0; i < mNumSlots; i++) {
1154 MmTelFeatureListener l = mMmTelFeatureListeners.get(i);
1155 if (l == null) continue;
1156 l.dump(pw);
1157 }
1158 pw.decreaseIndent();
1159 pw.println("RcsFeatureListeners:");
1160 pw.increaseIndent();
1161 for (int i = 0; i < mNumSlots; i++) {
1162 RcsFeatureListener l = mRcsFeatureListeners.get(i);
1163 if (l == null) continue;
1164 l.dump(pw);
1165 }
1166 pw.decreaseIndent();
1167 pw.println("Most recent logs:");
1168 pw.increaseIndent();
1169 sLocalLog.dump(pw);
1170 pw.decreaseIndent();
1171 }
1172 pw.decreaseIndent();
1173 }
1174
1175 private static void logv(String msg) {
1176 Rlog.d(TAG, msg);
1177 }
1178
1179 private static void logd(String msg) {
1180 Rlog.d(TAG, msg);
1181 sLocalLog.log(msg);
1182 }
1183
1184 private static void loge(String msg) {
1185 Rlog.e(TAG, msg);
1186 sLocalLog.log(msg);
1187 }
1188}