From 120adb234997ca9585cc9128361e3c8f9fc9f6a8 Mon Sep 17 00:00:00 2001 From: Mengjun Leng Date: Thu, 25 Apr 2024 12:53:47 +0530 Subject: [PATCH] Refacotr mobile data observer for repository In current design, the mobile data observer might be removed due to SIM absence after registered so that mobile data network info can't be updated to clients. To fix it, migrate it to use API TelephonyCallback.onUserMobileDataStateChanged Bug: 327696232 Change-Id: I26c8f946823abb7505f0227c8dd8ab0700d0c5f2 --- .../network/MobileNetworkRepository.java | 48 ++++++++----------- .../DefaultSubscriptionController.java | 3 -- 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/src/com/android/settings/network/MobileNetworkRepository.java b/src/com/android/settings/network/MobileNetworkRepository.java index 6820f10cebf..8ee5389bfca 100644 --- a/src/com/android/settings/network/MobileNetworkRepository.java +++ b/src/com/android/settings/network/MobileNetworkRepository.java @@ -91,7 +91,6 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions private AirplaneModeObserver mAirplaneModeObserver; private DataRoamingObserver mDataRoamingObserver; private MetricsFeatureProvider mMetricsFeatureProvider; - private Map mDataContentObserverMap = new HashMap<>(); private int mPhysicalSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX; private int mLogicalSlotIndex = SubscriptionManager.INVALID_SIM_SLOT_INDEX; private int mCardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT; @@ -210,6 +209,9 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions */ public void addRegister(LifecycleOwner lifecycleOwner, MobileNetworkCallback mobileNetworkCallback, int subId) { + if (DEBUG) { + Log.d(TAG, "addRegister by SUB ID " + subId); + } if (sCallbacks.isEmpty()) { mSubscriptionManager.addOnSubscriptionsChangedListener(mContext.getMainExecutor(), this); @@ -221,7 +223,6 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions observeAllUiccInfo(lifecycleOwner); observeAllMobileNetworkInfo(lifecycleOwner); if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { - addRegisterBySubId(subId); createTelephonyManagerBySubId(subId); mDataRoamingObserver.register(mContext, subId); } @@ -230,21 +231,6 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions sendAvailableSubInfoCache(mobileNetworkCallback); } - public void addRegisterBySubId(int subId) { - Log.d(TAG, "MobileDataContentObserver addRegisterBySubId: " + subId); - MobileDataContentObserver dataContentObserver = new MobileDataContentObserver( - new Handler(Looper.getMainLooper())); - dataContentObserver.setOnMobileDataChangedListener(() -> { - sExecutor.execute(() -> { - Log.d(TAG, "MobileDataContentObserver changed"); - insertMobileNetworkInfo(mContext, subId, - getTelephonyManagerBySubId(mContext, subId)); - }); - }); - dataContentObserver.register(mContext, subId); - mDataContentObserverMap.put(subId, dataContentObserver); - } - private void createTelephonyManagerBySubId(int subId) { if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID || mTelephonyCallbackMap.containsKey(subId)) { @@ -254,7 +240,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions return; } PhoneCallStateTelephonyCallback - telephonyCallback = new PhoneCallStateTelephonyCallback(); + telephonyCallback = new PhoneCallStateTelephonyCallback(subId); TelephonyManager telephonyManager = mContext.getSystemService( TelephonyManager.class).createForSubscriptionId(subId); telephonyManager.registerTelephonyCallback(mContext.getMainExecutor(), @@ -293,10 +279,6 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions } } } - if (mDataContentObserverMap.containsKey(subId)) { - mDataContentObserverMap.get(subId).unRegister(mContext); - mDataContentObserverMap.remove(subId); - } } public void removeRegister(MobileNetworkCallback mobileNetworkCallback) { @@ -307,10 +289,6 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions mSubscriptionManager.removeOnSubscriptionsChangedListener(this); mAirplaneModeObserver.unRegister(mContext); mDataRoamingObserver.unRegister(mContext); - mDataContentObserverMap.forEach((id, observer) -> { - observer.unRegister(mContext); - }); - mDataContentObserverMap.clear(); mTelephonyManagerMap.forEach((id, manager) -> { TelephonyCallback callback = mTelephonyCallbackMap.get(id); @@ -763,7 +741,14 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions } private class PhoneCallStateTelephonyCallback extends TelephonyCallback implements - TelephonyCallback.CallStateListener { + TelephonyCallback.CallStateListener, + TelephonyCallback.UserMobileDataStateListener { + + private int mSubId; + + public PhoneCallStateTelephonyCallback(int subId) { + mSubId = subId; + } @Override public void onCallStateChanged(int state) { @@ -771,6 +756,15 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions callback.onCallStateChanged(state); } } + + @Override + public void onUserMobileDataStateChanged(boolean enabled) { + Log.d(TAG, "onUserMobileDataStateChanged enabled " + enabled + " on SUB " + mSubId); + sExecutor.execute(() -> { + insertMobileNetworkInfo(mContext, mSubId, + getTelephonyManagerBySubId(mContext, mSubId)); + }); + } } /** diff --git a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java index 024fb834968..fa8760c2205 100644 --- a/src/com/android/settings/network/telephony/DefaultSubscriptionController.java +++ b/src/com/android/settings/network/telephony/DefaultSubscriptionController.java @@ -100,9 +100,6 @@ public abstract class DefaultSubscriptionController extends TelephonyBasePrefere mMobileNetworkRepository.addRegister(mLifecycleOwner, this, SubscriptionManager.INVALID_SUBSCRIPTION_ID); mMobileNetworkRepository.updateEntity(); - // Can not get default subId from database until get the callback, add register by subId - // later. - mMobileNetworkRepository.addRegisterBySubId(getDefaultSubscriptionId()); mDataSubscriptionChangedReceiver.registerReceiver(); }