diff options
| author | 2024-09-27 05:50:01 +0000 | |
|---|---|---|
| committer | 2024-09-27 05:50:01 +0000 | |
| commit | fc24bfa3304ff2d206f279f521f59c5c3e9e15b5 (patch) | |
| tree | 971335b52d86718826ff26a7103b7bd512b3b0f9 | |
| parent | 7e21ef8b265daae22dad9d0fc0a6f727cabf6789 (diff) | |
| parent | a7927cc559c1c41b8d62036edc3ce534f9e2dd3d (diff) | |
Merge "Fix crash from exceeding the number of permissible registered listeners" into main
2 files changed, 50 insertions, 10 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java index 5ea9e6ae0a70..301ab2bcdd65 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java @@ -311,10 +311,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi mConfig = MobileMappings.Config.readConfig(mContext); mTelephonyManager = mTelephonyManager.createForSubscriptionId(mDefaultDataSubId); mSubIdTelephonyManagerMap.put(mDefaultDataSubId, mTelephonyManager); - InternetTelephonyCallback telephonyCallback = - new InternetTelephonyCallback(mDefaultDataSubId); - mSubIdTelephonyCallbackMap.put(mDefaultDataSubId, telephonyCallback); - mTelephonyManager.registerTelephonyCallback(mExecutor, telephonyCallback); + registerInternetTelephonyCallback(mTelephonyManager, mDefaultDataSubId); // Listen the connectivity changes mConnectivityManager.registerDefaultNetworkCallback(mConnectivityManagerNetworkCallback); mCanConfigWifi = canConfigWifi; @@ -346,6 +343,23 @@ public class InternetDialogController implements AccessPointController.AccessPoi mCallback = null; } + /** + * This is to generate and register the new callback to Telephony for uncached subscription id, + * then cache it. Telephony also cached this callback into + * {@link com.android.server.TelephonyRegistry}, so if subscription id and callback were cached + * already, it shall do nothing to avoid registering redundant callback to Telephony. + */ + private void registerInternetTelephonyCallback( + TelephonyManager telephonyManager, int subId) { + if (mSubIdTelephonyCallbackMap.containsKey(subId)) { + // Avoid to generate and register unnecessary callback to Telephony. + return; + } + InternetTelephonyCallback telephonyCallback = new InternetTelephonyCallback(subId); + mSubIdTelephonyCallbackMap.put(subId, telephonyCallback); + telephonyManager.registerTelephonyCallback(mExecutor, telephonyCallback); + } + boolean isAirplaneModeEnabled() { return mGlobalSettings.getInt(Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } @@ -673,9 +687,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi int subId = subInfo.getSubscriptionId(); if (mSubIdTelephonyManagerMap.get(subId) == null) { TelephonyManager secondaryTm = mTelephonyManager.createForSubscriptionId(subId); - InternetTelephonyCallback telephonyCallback = new InternetTelephonyCallback(subId); - secondaryTm.registerTelephonyCallback(mExecutor, telephonyCallback); - mSubIdTelephonyCallbackMap.put(subId, telephonyCallback); + registerInternetTelephonyCallback(secondaryTm, subId); mSubIdTelephonyManagerMap.put(subId, secondaryTm); } return subId; @@ -1351,6 +1363,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi if (DEBUG) { Log.d(TAG, "DDS: defaultDataSubId:" + defaultDataSubId); } + if (SubscriptionManager.isUsableSubscriptionId(defaultDataSubId)) { // clean up old defaultDataSubId TelephonyCallback oldCallback = mSubIdTelephonyCallbackMap.get(mDefaultDataSubId); @@ -1366,9 +1379,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi // create for new defaultDataSubId mTelephonyManager = mTelephonyManager.createForSubscriptionId(defaultDataSubId); mSubIdTelephonyManagerMap.put(defaultDataSubId, mTelephonyManager); - InternetTelephonyCallback newCallback = new InternetTelephonyCallback(defaultDataSubId); - mSubIdTelephonyCallbackMap.put(defaultDataSubId, newCallback); - mTelephonyManager.registerTelephonyCallback(mHandler::post, newCallback); + registerInternetTelephonyCallback(mTelephonyManager, defaultDataSubId); mCallback.onSubscriptionsChanged(defaultDataSubId); } mDefaultDataSubId = defaultDataSubId; diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java index eea02eec7099..2f8f45cb0197 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -887,6 +888,34 @@ public class InternetDialogDelegateControllerTest extends SysuiTestCase { } @Test + public void getActiveAutoSwitchNonDdsSubId_registerCallbackForExistedSubId_notRegister() { + mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true); + + // Adds non DDS subId + SubscriptionInfo info = mock(SubscriptionInfo.class); + doReturn(SUB_ID2).when(info).getSubscriptionId(); + doReturn(false).when(info).isOpportunistic(); + when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info); + + mInternetDialogController.getActiveAutoSwitchNonDdsSubId(); + + // 1st time is onStart(), 2nd time is getActiveAutoSwitchNonDdsSubId() + verify(mTelephonyManager, times(2)).registerTelephonyCallback(any(), any()); + assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.size() == 2); + + // Adds non DDS subId again + doReturn(SUB_ID2).when(info).getSubscriptionId(); + doReturn(false).when(info).isOpportunistic(); + when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info); + + mInternetDialogController.getActiveAutoSwitchNonDdsSubId(); + + // Does not add due to cached subInfo in mSubIdTelephonyCallbackMap. + verify(mTelephonyManager, times(2)).registerTelephonyCallback(any(), any()); + assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.size() == 2); + } + + @Test public void getMobileNetworkSummary() { mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true); Resources res1 = mock(Resources.class); |