[Settings] Apply proxy design to data usage

Enable proxy to subscription manager in data usage UI.

Bug: 141833767
Test: manual
make RunSettingsRoboTests -j ROBOTEST_FILTER=BillingCyclePreferenceTest
make RunSettingsRoboTests -j ROBOTEST_FILTER=CellDataPreferenceTest
make RunSettingsRoboTests -j ROBOTEST_FILTER=DataUsageListTest
make RunSettingsRoboTests -j ROBOTEST_FILTER=DataUsageSummaryTest
make RunSettingsRoboTests -j ROBOTEST_FILTER=DataUsageUtilsTest
make RunSettingsRoboTests -j ROBOTEST_FILTER=MobileDataEnabledListenerTest

Change-Id: Id119738dc16ece8767c088b9a0794997e4b0334f
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 436acef..65d0c82 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -156,19 +156,19 @@
     public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
             PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
 
-        Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
+        final Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
         if (preference == null) {
             return false;
         }
 
-        Intent intent = preference.getIntent();
+        final Intent intent = preference.getIntent();
         if (intent != null) {
             // Find the activity that is in the system image
-            PackageManager pm = context.getPackageManager();
-            List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
-            int listSize = list.size();
+            final PackageManager pm = context.getPackageManager();
+            final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
+            final int listSize = list.size();
             for (int i = 0; i < listSize; i++) {
-                ResolveInfo resolveInfo = list.get(i);
+                final ResolveInfo resolveInfo = list.get(i);
                 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
                         != 0) {
 
@@ -199,7 +199,7 @@
      * @throws IllegalStateException if no UserManager could be retrieved.
      */
     public static UserManager getUserManager(Context context) {
-        UserManager um = UserManager.get(context);
+        final UserManager um = UserManager.get(context);
         if (um == null) {
             throw new IllegalStateException("Unable to load UserManager");
         }
@@ -217,7 +217,7 @@
      * Returns whether the device is voice-capable (meaning, it is also a phone).
      */
     public static boolean isVoiceCapable(Context context) {
-        TelephonyManager telephony =
+        final TelephonyManager telephony =
                 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
         return telephony != null && telephony.isVoiceCapable();
     }
@@ -228,12 +228,12 @@
      * @return the formatted and newline-separated IP addresses, or null if none.
      */
     public static String getWifiIpAddresses(Context context) {
-        WifiManager wifiManager = context.getSystemService(WifiManager.class);
-        Network currentNetwork = wifiManager.getCurrentNetwork();
+        final WifiManager wifiManager = context.getSystemService(WifiManager.class);
+        final Network currentNetwork = wifiManager.getCurrentNetwork();
         if (currentNetwork != null) {
-            ConnectivityManager cm = (ConnectivityManager)
+            final ConnectivityManager cm = (ConnectivityManager)
                 context.getSystemService(Context.CONNECTIVITY_SERVICE);
-            LinkProperties prop = cm.getLinkProperties(currentNetwork);
+            final LinkProperties prop = cm.getLinkProperties(currentNetwork);
             return formatIpAddresses(prop);
         }
         return null;
@@ -241,7 +241,7 @@
 
     private static String formatIpAddresses(LinkProperties prop) {
         if (prop == null) return null;
-        Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
+        final Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
         // If there are no entries, return null
         if (!iter.hasNext()) return null;
         // Concatenate all available addresses, comma separated
@@ -262,7 +262,7 @@
         // And : new Locale("en_US").toString() => "en_us"
         if (null == localeStr)
             return Locale.getDefault();
-        String[] brokenDownLocale = localeStr.split("_", 3);
+        final String[] brokenDownLocale = localeStr.split("_", 3);
         // split may not return a 0-length array.
         if (1 == brokenDownLocale.length) {
             return new Locale(brokenDownLocale[0]);
@@ -396,7 +396,7 @@
      * exists but it is disabled.
      */
     public static UserHandle getManagedProfile(UserManager userManager) {
-        List<UserHandle> userProfiles = userManager.getUserProfiles();
+        final List<UserHandle> userProfiles = userManager.getUserProfiles();
         for (UserHandle profile : userProfiles) {
             if (profile.getIdentifier() == userManager.getUserHandle()) {
                 continue;
@@ -420,7 +420,7 @@
         // we need to use UserManager.getProfiles that is available on API 23 (the one currently
         // used for Settings Robolectric tests).
         final int myUserId = UserHandle.myUserId();
-        List<UserInfo> profiles = userManager.getProfiles(myUserId);
+        final List<UserInfo> profiles = userManager.getProfiles(myUserId);
         final int count = profiles.size();
         for (int i = 0; i < count; i++) {
             final UserInfo profile = profiles.get(i);
@@ -438,7 +438,7 @@
      * @return the managed profile id or UserHandle.USER_NULL if there is none.
      */
     public static int getManagedProfileId(UserManager um, int parentUserId) {
-        int[] profileIds = um.getProfileIdsWithDisabled(parentUserId);
+        final int[] profileIds = um.getProfileIdsWithDisabled(parentUserId);
         for (int profileId : profileIds) {
             if (profileId != parentUserId) {
                 return profileId;
@@ -464,13 +464,14 @@
      */
     public static UserHandle getSecureTargetUser(IBinder activityToken,
             UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) {
-        UserHandle currentUser = new UserHandle(UserHandle.myUserId());
-        IActivityManager am = ActivityManager.getService();
+        final UserHandle currentUser = new UserHandle(UserHandle.myUserId());
+        final IActivityManager am = ActivityManager.getService();
         try {
-            String launchedFromPackage = am.getLaunchedFromPackage(activityToken);
-            boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage);
+            final String launchedFromPackage = am.getLaunchedFromPackage(activityToken);
+            final boolean launchedFromSettingsApp =
+                    SETTINGS_PACKAGE_NAME.equals(launchedFromPackage);
 
-            UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId(
+            final UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId(
                     am.getLaunchedFromUid(activityToken)));
             if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) {
                 // Check it's secure
@@ -478,14 +479,14 @@
                     return launchedFromUser;
                 }
             }
-            UserHandle extrasUser = getUserHandleFromBundle(intentExtras);
+            final UserHandle extrasUser = getUserHandleFromBundle(intentExtras);
             if (extrasUser != null && !extrasUser.equals(currentUser)) {
                 // Check it's secure
                 if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) {
                     return extrasUser;
                 }
             }
-            UserHandle argumentsUser = getUserHandleFromBundle(arguments);
+            final UserHandle argumentsUser = getUserHandleFromBundle(arguments);
             if (argumentsUser != null && !argumentsUser.equals(currentUser)) {
                 // Check it's secure
                 if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) {
@@ -555,10 +556,11 @@
     }
 
     public static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) {
-        List<IntentFilterVerificationInfo> iviList = pm.getIntentFilterVerifications(packageName);
-        List<IntentFilter> filters = pm.getAllIntentFilters(packageName);
+        final List<IntentFilterVerificationInfo> iviList =
+                pm.getIntentFilterVerifications(packageName);
+        final List<IntentFilter> filters = pm.getAllIntentFilters(packageName);
 
-        ArraySet<String> result = new ArraySet<>();
+        final ArraySet<String> result = new ArraySet<>();
         if (iviList != null && iviList.size() > 0) {
             for (IntentFilterVerificationInfo ivi : iviList) {
                 for (String host : ivi.getDomains()) {
@@ -582,16 +584,16 @@
      * Returns the application info of the currently installed MDM package.
      */
     public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) {
-        DevicePolicyManager dpm =
+        final DevicePolicyManager dpm =
                 (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
-        ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId);
+        final ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId);
         if (mdmPackage == null) {
             return null;
         }
-        String mdmPackageName = mdmPackage.getPackageName();
+        final String mdmPackageName = mdmPackage.getPackageName();
         try {
-            IPackageManager ipm = AppGlobals.getPackageManager();
-            ApplicationInfo mdmApplicationInfo =
+            final IPackageManager ipm = AppGlobals.getPackageManager();
+            final ApplicationInfo mdmApplicationInfo =
                     ipm.getApplicationInfo(mdmPackageName, 0, profileId);
             return mdmApplicationInfo;
         } catch (RemoteException e) {
@@ -618,7 +620,7 @@
      */
     public static SpannableString createAccessibleSequence(CharSequence displayText,
             String accessibileText) {
-        SpannableString str = new SpannableString(displayText);
+        final SpannableString str = new SpannableString(displayText);
         str.setSpan(new TtsSpan.TextBuilder(accessibileText).build(), 0,
                 displayText.length(),
                 Spannable.SPAN_INCLUSIVE_INCLUSIVE);
@@ -652,7 +654,7 @@
         }
         final boolean allowAnyUser = isInternal
                 && bundle.getBoolean(ChooseLockSettingsHelper.EXTRA_ALLOW_ANY_USER, false);
-        int userId = bundle.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId());
+        final int userId = bundle.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId());
         if (userId == LockPatternUtils.USER_FRP) {
             return allowAnyUser ? userId : enforceSystemUser(context, userId);
         } else {
@@ -699,7 +701,7 @@
      * Returns the user id of the credential owner of the given user id.
      */
     public static int getCredentialOwnerUserId(Context context, int userId) {
-        UserManager um = getUserManager(context);
+        final UserManager um = getUserManager(context);
         return um.getCredentialOwnerProfile(userId);
     }
 
@@ -799,7 +801,7 @@
     }
 
     public static boolean hasFingerprintHardware(Context context) {
-        FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context);
+        final FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context);
         return fingerprintManager != null && fingerprintManager.isHardwareDetected();
     }
 
@@ -812,7 +814,7 @@
     }
 
     public static boolean hasFaceHardware(Context context) {
-        FaceManager faceManager = getFaceManagerOrNull(context);
+        final FaceManager faceManager = getFaceManagerOrNull(context);
         return faceManager != null && faceManager.isHardwareDetected();
     }
 
@@ -865,7 +867,7 @@
     public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) {
         final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID,
                 VolumeInfo.ID_PRIVATE_INTERNAL);
-        VolumeInfo volume = sm.findVolumeById(volumeId);
+        final VolumeInfo volume = sm.findVolumeById(volumeId);
         return isVolumeValid(volume) ? volume : null;
     }
 
@@ -878,12 +880,13 @@
      */
     public static boolean isProfileOrDeviceOwner(UserManager userManager,
             DevicePolicyManager devicePolicyManager, String packageName) {
-        List<UserInfo> userInfos = userManager.getUsers();
+        final List<UserInfo> userInfos = userManager.getUsers();
         if (devicePolicyManager.isDeviceOwnerAppOnAnyUser(packageName)) {
             return true;
         }
         for (int i = 0, size = userInfos.size(); i < size; i++) {
-            ComponentName cn = devicePolicyManager.getProfileOwnerAsUser(userInfos.get(i).id);
+            final ComponentName cn = devicePolicyManager
+                    .getProfileOwnerAsUser(userInfos.get(i).id);
             if (cn != null && cn.getPackageName().equals(packageName)) {
                 return true;
             }
@@ -938,9 +941,9 @@
             return original;
         }
 
-        float scaleWidth = ((float) maxWidth) / actualWidth;
-        float scaleHeight = ((float) maxHeight) / actualHeight;
-        float scale = Math.min(scaleWidth, scaleHeight);
+        final float scaleWidth = ((float) maxWidth) / actualWidth;
+        final float scaleHeight = ((float) maxHeight) / actualHeight;
+        final float scale = Math.min(scaleWidth, scaleHeight);
         final int width = (int) (actualWidth * scale);
         final int height = (int) (actualHeight * scale);
 
diff --git a/src/com/android/settings/datausage/BillingCyclePreference.java b/src/com/android/settings/datausage/BillingCyclePreference.java
index 23c577c..8dea7de 100644
--- a/src/com/android/settings/datausage/BillingCyclePreference.java
+++ b/src/com/android/settings/datausage/BillingCyclePreference.java
@@ -26,27 +26,39 @@
 
 import com.android.settings.R;
 import com.android.settings.core.SubSettingLauncher;
-import com.android.settings.datausage.CellDataPreference.DataStateListener;
+import com.android.settings.network.MobileDataEnabledListener;
 
-public class BillingCyclePreference extends Preference implements TemplatePreference {
+/**
+ * Preference which displays billing cycle of subscription
+ */
+public class BillingCyclePreference extends Preference
+        implements TemplatePreference, MobileDataEnabledListener.Client {
 
     private NetworkTemplate mTemplate;
     private NetworkServices mServices;
     private int mSubId;
+    private MobileDataEnabledListener mListener;
 
+    /**
+     * Preference constructor
+     *
+     * @param context Context of preference
+     * @param arrts The attributes of the XML tag that is inflating the preference
+     */
     public BillingCyclePreference(Context context, AttributeSet attrs) {
         super(context, attrs);
+        mListener = new MobileDataEnabledListener(context, this);
     }
 
     @Override
     public void onAttached() {
         super.onAttached();
-        mListener.setListener(true, mSubId, getContext());
+        mListener.start(mSubId);
     }
 
     @Override
     public void onDetached() {
-        mListener.setListener(false, mSubId, getContext());
+        mListener.stop();
         super.onDetached();
     }
 
@@ -73,7 +85,7 @@
 
     @Override
     public Intent getIntent() {
-        Bundle args = new Bundle();
+        final Bundle args = new Bundle();
         args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate);
         return new SubSettingLauncher(getContext())
                 .setDestination(BillingCycleSettings.class.getName())
@@ -83,10 +95,10 @@
                 .toIntent();
     }
 
-    private final DataStateListener mListener = new DataStateListener() {
-        @Override
-        public void onChange(boolean selfChange) {
-            updateEnabled();
-        }
-    };
+    /**
+     * Implementation of MobileDataEnabledListener.Client
+     */
+    public void onMobileDataEnabledChange() {
+        updateEnabled();
+    }
 }
diff --git a/src/com/android/settings/datausage/CellDataPreference.java b/src/com/android/settings/datausage/CellDataPreference.java
index 5bfe967..f4f2be7 100644
--- a/src/com/android/settings/datausage/CellDataPreference.java
+++ b/src/com/android/settings/datausage/CellDataPreference.java
@@ -17,14 +17,9 @@
 import android.app.settings.SettingsEnums;
 import android.content.Context;
 import android.content.DialogInterface;
-import android.database.ContentObserver;
 import android.net.NetworkTemplate;
-import android.net.Uri;
-import android.os.Handler;
-import android.os.Looper;
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.provider.Settings.Global;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
@@ -39,33 +34,38 @@
 import androidx.preference.PreferenceViewHolder;
 
 import com.android.settings.R;
+import com.android.settings.network.MobileDataEnabledListener;
+import com.android.settings.network.ProxySubscriptionManager;
 import com.android.settings.overlay.FeatureFactory;
 import com.android.settingslib.CustomDialogPreferenceCompat;
 
-import java.util.List;
-
-public class CellDataPreference extends CustomDialogPreferenceCompat implements TemplatePreference {
+/**
+ * Preference of cellular data control within Data Usage
+ */
+public class CellDataPreference extends CustomDialogPreferenceCompat
+        implements TemplatePreference, MobileDataEnabledListener.Client {
 
     private static final String TAG = "CellDataPreference";
 
     public int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     public boolean mChecked;
     public boolean mMultiSimDialog;
-    private TelephonyManager mTelephonyManager;
     @VisibleForTesting
-    SubscriptionManager mSubscriptionManager;
+    ProxySubscriptionManager mProxySubscriptionMgr;
+    private MobileDataEnabledListener mDataStateListener;
 
     public CellDataPreference(Context context, AttributeSet attrs) {
         super(context, attrs, TypedArrayUtils.getAttr(context,
                 androidx.preference.R.attr.switchPreferenceStyle,
                 android.R.attr.switchPreferenceStyle));
+        mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
+        mDataStateListener = new MobileDataEnabledListener(context, this);
     }
 
     @Override
     protected void onRestoreInstanceState(Parcelable s) {
-        CellDataState state = (CellDataState) s;
+        final CellDataState state = (CellDataState) s;
         super.onRestoreInstanceState(state.getSuperState());
-        mTelephonyManager = TelephonyManager.from(getContext());
         mChecked = state.mChecked;
         mMultiSimDialog = state.mMultiSimDialog;
         if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
@@ -77,7 +77,7 @@
 
     @Override
     protected Parcelable onSaveInstanceState() {
-        CellDataState state = new CellDataState(super.onSaveInstanceState());
+        final CellDataState state = new CellDataState(super.onSaveInstanceState());
         state.mChecked = mChecked;
         state.mMultiSimDialog = mMultiSimDialog;
         state.mSubId = mSubId;
@@ -87,19 +87,14 @@
     @Override
     public void onAttached() {
         super.onAttached();
-        mDataStateListener.setListener(true, mSubId, getContext());
-        if (mSubscriptionManager!= null) {
-            mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
-        }
+        mDataStateListener.start(mSubId);
+        mProxySubscriptionMgr.addActiveSubscriptionsListener(mOnSubscriptionsChangeListener);
     }
 
     @Override
     public void onDetached() {
-        mDataStateListener.setListener(false, mSubId, getContext());
-        if (mSubscriptionManager!= null) {
-            mSubscriptionManager.removeOnSubscriptionsChangedListener(
-                    mOnSubscriptionsChangeListener);
-        }
+        mDataStateListener.stop();
+        mProxySubscriptionMgr.removeActiveSubscriptionsListener(mOnSubscriptionsChangeListener);
         super.onDetached();
     }
 
@@ -108,10 +103,9 @@
         if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             throw new IllegalArgumentException("CellDataPreference needs a SubscriptionInfo");
         }
-        mSubscriptionManager = SubscriptionManager.from(getContext());
-        mTelephonyManager = TelephonyManager.from(getContext());
 
-        mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
+        mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(getContext());
+        mProxySubscriptionMgr.addActiveSubscriptionsListener(mOnSubscriptionsChangeListener);
 
         if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
             mSubId = subId;
@@ -122,13 +116,13 @@
     }
 
     private void updateChecked() {
-        setChecked(mTelephonyManager.getDataEnabled(mSubId));
+        setChecked(getContext().getSystemService(TelephonyManager.class).getDataEnabled(mSubId));
     }
 
     private void updateEnabled() {
         // If this subscription is not active, for example, SIM card is taken out, we disable
         // the button.
-        setEnabled(mSubscriptionManager.getActiveSubscriptionInfo(mSubId) != null);
+        setEnabled(mProxySubscriptionMgr.getActiveSubscriptionInfo(mSubId) != null);
     }
 
     @Override
@@ -136,9 +130,10 @@
         final Context context = getContext();
         FeatureFactory.getFactory(context).getMetricsFeatureProvider()
                 .action(context, SettingsEnums.ACTION_CELL_DATA_TOGGLE, !mChecked);
-        final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(
+        final SubscriptionInfo currentSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
                 mSubId);
-        final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
+        final SubscriptionInfo nextSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
+                SubscriptionManager.getDefaultDataSubscriptionId());
         if (mChecked) {
             setMobileDataEnabled(false);
             if (nextSir != null && currentSir != null
@@ -153,7 +148,7 @@
     private void setMobileDataEnabled(boolean enabled) {
         if (DataUsageSummary.LOGD) Log.d(TAG, "setMobileDataEnabled(" + enabled + ","
                 + mSubId + ")");
-        mTelephonyManager.setDataEnabled(mSubId, enabled);
+        getContext().getSystemService(TelephonyManager.class).setDataEnabled(mSubId, enabled);
         setChecked(enabled);
     }
 
@@ -166,7 +161,7 @@
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
-        View switchView = holder.findViewById(android.R.id.switch_widget);
+        final View switchView = holder.findViewById(android.R.id.switch_widget);
         switchView.setClickable(false);
         ((Checkable) switchView).setChecked(mChecked);
     }
@@ -191,8 +186,10 @@
 
     private void showMultiSimDialog(Builder builder,
             DialogInterface.OnClickListener listener) {
-        final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
-        final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
+        final SubscriptionInfo currentSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
+                mSubId);
+        final SubscriptionInfo nextSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
+                SubscriptionManager.getDefaultDataSubscriptionId());
 
         final String previousName = (nextSir == null)
             ? getContext().getResources().getString(R.string.sim_selection_required_pref)
@@ -208,14 +205,10 @@
     }
 
     private void disableDataForOtherSubscriptions(int subId) {
-        List<SubscriptionInfo> subInfoList = mSubscriptionManager
-                .getActiveSubscriptionInfoList(true);
-        if (subInfoList != null) {
-            for (SubscriptionInfo subInfo : subInfoList) {
-                if (subInfo.getSubscriptionId() != subId) {
-                    mTelephonyManager.setDataEnabled(subInfo.getSubscriptionId(), false);
-                }
-            }
+        final SubscriptionInfo subInfo = mProxySubscriptionMgr.getActiveSubscriptionInfo(
+                subId);
+        if (subInfo != null) {
+            getContext().getSystemService(TelephonyManager.class).setDataEnabled(subId, false);
         }
     }
 
@@ -225,7 +218,7 @@
             return;
         }
         if (mMultiSimDialog) {
-            mSubscriptionManager.setDefaultDataSubId(mSubId);
+            mProxySubscriptionMgr.get().setDefaultDataSubId(mSubId);
             setMobileDataEnabled(true);
             disableDataForOtherSubscriptions(mSubId);
         } else {
@@ -235,40 +228,23 @@
     }
 
     @VisibleForTesting
-    final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener
-            = new SubscriptionManager.OnSubscriptionsChangedListener() {
-        @Override
-        public void onSubscriptionsChanged() {
-            if (DataUsageSummary.LOGD) {
-                Log.d(TAG, "onSubscriptionsChanged");
-            }
-            updateEnabled();
-        }
-    };
-
-    private final DataStateListener mDataStateListener = new DataStateListener() {
-        @Override
-        public void onChange(boolean selfChange) {
-            updateChecked();
-        }
-    };
-
-    public abstract static class DataStateListener extends ContentObserver {
-        public DataStateListener() {
-            super(new Handler(Looper.getMainLooper()));
-        }
-
-        public void setListener(boolean listening, int subId, Context context) {
-            if (listening) {
-                Uri uri = Global.getUriFor(Global.MOBILE_DATA);
-                if (TelephonyManager.getDefault().getSimCount() != 1) {
-                    uri = Global.getUriFor(Global.MOBILE_DATA + subId);
+    final ProxySubscriptionManager.OnActiveSubscriptionChangedListener
+            mOnSubscriptionsChangeListener =
+            new ProxySubscriptionManager.OnActiveSubscriptionChangedListener() {
+                public void onChanged() {
+                    if (DataUsageSummary.LOGD) {
+                        Log.d(TAG, "onSubscriptionsChanged");
+                    }
+                    updateEnabled();
                 }
-                context.getContentResolver().registerContentObserver(uri, false, this);
-            } else {
-                context.getContentResolver().unregisterContentObserver(this);
-            }
-        }
+            };
+
+    /**
+     * Implementation of MobileDataEnabledListener.Client
+    */
+    @VisibleForTesting
+    public void onMobileDataEnabledChange() {
+        updateChecked();
     }
 
     public static class CellDataState extends BaseSavedState {
diff --git a/src/com/android/settings/datausage/DataUsageList.java b/src/com/android/settings/datausage/DataUsageList.java
index f477ba3..f747951 100644
--- a/src/com/android/settings/datausage/DataUsageList.java
+++ b/src/com/android/settings/datausage/DataUsageList.java
@@ -39,7 +39,6 @@
 import android.provider.Settings;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
-import android.telephony.TelephonyManager;
 import android.util.Log;
 import android.util.SparseArray;
 import android.view.View;
@@ -57,6 +56,8 @@
 import com.android.settings.R;
 import com.android.settings.core.SubSettingLauncher;
 import com.android.settings.datausage.CycleAdapter.SpinnerInterface;
+import com.android.settings.network.MobileDataEnabledListener;
+import com.android.settings.network.ProxySubscriptionManager;
 import com.android.settings.widget.LoadingViewController;
 import com.android.settingslib.AppItem;
 import com.android.settingslib.net.NetworkCycleChartData;
@@ -72,7 +73,8 @@
  * Panel showing data usage history across various networks, including options
  * to inspect based on usage cycle and control through {@link NetworkPolicy}.
  */
-public class DataUsageList extends DataUsageBaseFragment {
+public class DataUsageList extends DataUsageBaseFragment
+        implements MobileDataEnabledListener.Client {
 
     static final String EXTRA_SUB_ID = "sub_id";
     static final String EXTRA_NETWORK_TEMPLATE = "network_template";
@@ -91,13 +93,8 @@
     private static final int LOADER_CHART_DATA = 2;
     private static final int LOADER_SUMMARY = 3;
 
-    private final CellDataPreference.DataStateListener mDataStateListener =
-            new CellDataPreference.DataStateListener() {
-                @Override
-                public void onChange(boolean selfChange) {
-                    updatePolicy();
-                }
-            };
+    @VisibleForTesting
+    MobileDataEnabledListener mDataStateListener;
 
     @VisibleForTesting
     NetworkTemplate mTemplate;
@@ -111,7 +108,6 @@
     LoadingViewController mLoadingViewController;
 
     private ChartDataUsagePreference mChart;
-    private TelephonyManager mTelephonyManager;
     private List<NetworkCycleChartData> mCycleData;
     private ArrayList<Long> mCycles;
     private UidDetailProvider mUidDetailProvider;
@@ -133,14 +129,15 @@
         if (!isBandwidthControlEnabled()) {
             Log.w(TAG, "No bandwidth control; leaving");
             activity.finish();
+            return;
         }
 
         mUidDetailProvider = new UidDetailProvider(activity);
-        mTelephonyManager = activity.getSystemService(TelephonyManager.class);
         mUsageAmount = findPreference(KEY_USAGE_AMOUNT);
         mChart = findPreference(KEY_CHART_DATA);
         mApps = findPreference(KEY_APPS_GROUP);
         processArgument();
+        mDataStateListener = new MobileDataEnabledListener(activity, this);
     }
 
     @Override
@@ -190,20 +187,21 @@
     @Override
     public void onResume() {
         super.onResume();
-        mDataStateListener.setListener(true, mSubId, getContext());
+        mDataStateListener.start(mSubId);
         updateBody();
     }
 
     @Override
     public void onPause() {
         super.onPause();
-        mDataStateListener.setListener(false, mSubId, getContext());
+        mDataStateListener.stop();
     }
 
     @Override
     public void onDestroy() {
         mUidDetailProvider.clearCache();
         mUidDetailProvider = null;
+        mDataStateListener.stop();
 
         super.onDestroy();
     }
@@ -234,6 +232,13 @@
     }
 
     /**
+     * Implementation of MobileDataEnabledListener.Client
+     */
+    public void onMobileDataEnabledChange() {
+        updatePolicy();
+    }
+
+    /**
      * Update body content based on current tab. Loads network cycle data from system, and
      * binds them to visible controls.
      */
@@ -253,7 +258,7 @@
 
         int seriesColor = context.getColor(R.color.sim_noitification);
         if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
-            final SubscriptionInfo sir = services.mSubscriptionManager
+            final SubscriptionInfo sir = ProxySubscriptionManager.getInstance(context)
                     .getActiveSubscriptionInfo(mSubId);
 
             if (sir != null) {
@@ -400,7 +405,7 @@
         Collections.sort(items);
         for (int i = 0; i < items.size(); i++) {
             final int percentTotal = largest != 0 ? (int) (items.get(i).total * 100 / largest) : 0;
-            AppDataUsagePreference preference = new AppDataUsagePreference(getContext(),
+            final AppDataUsagePreference preference = new AppDataUsagePreference(getContext(),
                     items.get(i), percentTotal, mUidDetailProvider);
             preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                 @Override
diff --git a/src/com/android/settings/datausage/DataUsageSummary.java b/src/com/android/settings/datausage/DataUsageSummary.java
index d26fc30..0796e5a 100644
--- a/src/com/android/settings/datausage/DataUsageSummary.java
+++ b/src/com/android/settings/datausage/DataUsageSummary.java
@@ -33,6 +33,7 @@
 import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
+import com.android.settings.network.ProxySubscriptionManager;
 import com.android.settingslib.NetworkPolicyEditor;
 import com.android.settingslib.core.AbstractPreferenceController;
 
@@ -65,6 +66,7 @@
     private DataUsageSummaryPreference mSummaryPreference;
     private DataUsageSummaryPreferenceController mSummaryController;
     private NetworkTemplate mDefaultTemplate;
+    private ProxySubscriptionManager mProxySubscriptionMgr;
 
     @Override
     public int getHelpResource() {
@@ -76,6 +78,11 @@
         super.onCreate(icicle);
         Context context = getContext();
 
+        // enable ProxySubscriptionMgr with Lifecycle support for all controllers
+        // live within this fragment
+        mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
+        mProxySubscriptionMgr.setLifecycle(getLifecycle());
+
         boolean hasMobileData = DataUsageUtils.hasMobileData(context);
 
         final int defaultSubId = SubscriptionManager.getDefaultDataSubscriptionId();
diff --git a/src/com/android/settings/network/ActiveSubsciptionsListener.java b/src/com/android/settings/network/ActiveSubsciptionsListener.java
index 1ed8078..d7a1def 100644
--- a/src/com/android/settings/network/ActiveSubsciptionsListener.java
+++ b/src/com/android/settings/network/ActiveSubsciptionsListener.java
@@ -85,6 +85,7 @@
     @VisibleForTesting
     BroadcastReceiver mSubscriptionChangeReceiver;
 
+    private Integer mMaxActiveSubscriptionInfos;
     private List<SubscriptionInfo> mCachedActiveSubscriptionInfo;
 
     /**
@@ -126,6 +127,24 @@
     }
 
     /**
+     * Get current max. number active subscription info(s) been setup within device
+     *
+     * @return max. number of active subscription info(s)
+     */
+    public int getActiveSubscriptionInfoCountMax() {
+        int count = 0;
+        if (mMaxActiveSubscriptionInfos == null) {
+            count = getSubscriptionManager().getActiveSubscriptionInfoCountMax();
+            if (mIsMonitoringDataChange) {
+                mMaxActiveSubscriptionInfos = count;
+            }
+        } else {
+            count = mMaxActiveSubscriptionInfos.intValue();
+        }
+        return count;
+    }
+
+    /**
      * Get a list of active subscription info
      *
      * @return A list of active subscription info
@@ -134,7 +153,7 @@
         if (mIsCachedDataAvailable) {
             return mCachedActiveSubscriptionInfo;
         }
-        mIsCachedDataAvailable = true;
+        mIsCachedDataAvailable = mIsMonitoringDataChange;
         mCachedActiveSubscriptionInfo = getSubscriptionManager().getActiveSubscriptionInfoList();
         return mCachedActiveSubscriptionInfo;
     }
@@ -163,6 +182,7 @@
      */
     public void clearCache() {
         mIsCachedDataAvailable = false;
+        mMaxActiveSubscriptionInfos = null;
         mCachedActiveSubscriptionInfo = null;
     }
 
diff --git a/src/com/android/settings/network/GlobalSettingsChangeListener.java b/src/com/android/settings/network/GlobalSettingsChangeListener.java
index 03103d7..89d374b 100644
--- a/src/com/android/settings/network/GlobalSettingsChangeListener.java
+++ b/src/com/android/settings/network/GlobalSettingsChangeListener.java
@@ -33,7 +33,8 @@
 /**
  * A listener for Settings.Global configuration change, with support of Lifecycle
  */
-abstract class GlobalSettingsChangeListener extends ContentObserver implements LifecycleObserver {
+public abstract class GlobalSettingsChangeListener extends ContentObserver
+        implements LifecycleObserver, AutoCloseable {
 
     /**
      * Constructor
@@ -41,7 +42,7 @@
      * @param context of this listener
      * @param field field of Global Settings
      */
-    GlobalSettingsChangeListener(Context context, String field) {
+    public GlobalSettingsChangeListener(Context context, String field) {
         super(new Handler());
         mContext = context;
         mField = field;
@@ -92,6 +93,13 @@
 
     @OnLifecycleEvent(ON_DESTROY)
     void onDestroy() {
+        close();
+    }
+
+    /**
+     * Implementation of AutoCloseable
+     */
+    public void close() {
         monitorUri(false);
         notifyChangeBasedOn(null);
     }
diff --git a/src/com/android/settings/network/MobileDataEnabledListener.java b/src/com/android/settings/network/MobileDataEnabledListener.java
index 8344f88..4c04282 100644
--- a/src/com/android/settings/network/MobileDataEnabledListener.java
+++ b/src/com/android/settings/network/MobileDataEnabledListener.java
@@ -17,53 +17,98 @@
 package com.android.settings.network;
 
 import android.content.Context;
-import android.database.ContentObserver;
-import android.net.Uri;
-import android.os.Handler;
 import android.provider.Settings;
 import android.telephony.SubscriptionManager;
 
 /** Helper class to listen for changes in the enabled state of mobile data. */
-public class MobileDataEnabledListener extends ContentObserver {
+public class MobileDataEnabledListener {
     private Context mContext;
     private Client mClient;
     private int mSubId;
+    /**
+     * There're 2 listeners both activated at the same time.
+     * For project that access MOBILE_DATA, only first listener is functional.
+     * For project that access "MOBILE_DATA + subId", first listener will be stopped when receiving
+     * any onChange from second listener.
+     */
+    private GlobalSettingsChangeListener mListener;
+    private GlobalSettingsChangeListener mListenerForSubId;
 
     public interface Client {
         void onMobileDataEnabledChange();
     }
 
+    /**
+     * Constructor
+     *
+     * @param context of this listener
+     * @param client callback when configuration changed
+     */
     public MobileDataEnabledListener(Context context, Client client) {
-        super(new Handler());
         mContext = context;
         mClient = client;
         mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
     }
 
-    /** Starts listening to changes in the enabled state for data on the given subscription id. */
+    /**
+     * Starts listening to changes in the enabled state for data on the given subscription id.
+     *
+     * @param subId subscription id for enabled state of data subscription
+     */
     public void start(int subId) {
         mSubId = subId;
-        Uri uri;
-        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
-            uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA);
-        }  else {
-            uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + mSubId);
+
+        if (mListener == null) {
+            mListener = new GlobalSettingsChangeListener(mContext,
+                    Settings.Global.MOBILE_DATA) {
+                public void onChanged(String field) {
+                    mClient.onMobileDataEnabledChange();
+                }
+            };
         }
-        mContext.getContentResolver().registerContentObserver(uri, true /*notifyForDescendants*/,
-                this);
+        stopMonitorSubIdSpecific();
+
+        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+            return;
+        }
+
+        mListenerForSubId = new GlobalSettingsChangeListener(mContext,
+                Settings.Global.MOBILE_DATA + mSubId) {
+            public void onChanged(String field) {
+                stopMonitor();
+                mClient.onMobileDataEnabledChange();
+            }
+        };
     }
 
+    /**
+     * Get latest subscription id configured for listening
+     *
+     * @return subscription id
+     */
     public int getSubId() {
         return mSubId;
     }
 
-    public MobileDataEnabledListener stop() {
-        mContext.getContentResolver().unregisterContentObserver(this);
-        return this;
+    /**
+     * Stop listening to changes in the enabled state for data.
+     */
+    public void stop() {
+        stopMonitor();
+        stopMonitorSubIdSpecific();
     }
 
-    @Override
-    public void onChange(boolean selfChange) {
-        mClient.onMobileDataEnabledChange();
+    private void stopMonitor() {
+        if (mListener != null) {
+            mListener.close();
+            mListener = null;
+        }
+    }
+
+    private void stopMonitorSubIdSpecific() {
+        if (mListenerForSubId != null) {
+            mListenerForSubId.close();
+            mListenerForSubId = null;
+        }
     }
 }
diff --git a/src/com/android/settings/network/ProxySubscriptionManager.java b/src/com/android/settings/network/ProxySubscriptionManager.java
index ec4fcbe..4e3e717 100644
--- a/src/com/android/settings/network/ProxySubscriptionManager.java
+++ b/src/com/android/settings/network/ProxySubscriptionManager.java
@@ -82,7 +82,6 @@
             }
         };
 
-        mKeepCacheWhenOnStart = true;
         mSubsciptionsMonitor.start();
     }
 
@@ -90,7 +89,6 @@
     private Context mContext;
     private ActiveSubsciptionsListener mSubsciptionsMonitor;
     private GlobalSettingsChangeListener mAirplaneModeMonitor;
-    private boolean mKeepCacheWhenOnStart;
 
     private List<OnActiveSubscriptionChangedListener> mActiveSubscriptionsListeners;
 
@@ -100,6 +98,12 @@
         }
     }
 
+    @Override
+    public void onSubscriptionsChanged() {
+        clearCache();
+        notifyAllListeners();
+    }
+
     /**
      * Lifecycle for data within proxy
      *
@@ -118,15 +122,11 @@
 
     @OnLifecycleEvent(ON_START)
     void onStart() {
-        if (!mKeepCacheWhenOnStart) {
-            mSubsciptionsMonitor.clearCache();
-        }
         mSubsciptionsMonitor.start();
     }
 
     @OnLifecycleEvent(ON_STOP)
     void onStop() {
-        mKeepCacheWhenOnStart = false;
         mSubsciptionsMonitor.stop();
     }
 
@@ -152,6 +152,15 @@
     }
 
     /**
+     * Get current max. number active subscription info(s) been setup within device
+     *
+     * @return max. number of active subscription info(s)
+     */
+    public int getActiveSubscriptionInfoCountMax() {
+        return mSubsciptionsMonitor.getActiveSubscriptionInfoCountMax();
+    }
+
+    /**
      * Get a list of active subscription info
      *
      * @return A list of active subscription info
@@ -183,6 +192,9 @@
      * @param listener listener to active subscriptions change
      */
     public void addActiveSubscriptionsListener(OnActiveSubscriptionChangedListener listener) {
+        if (mActiveSubscriptionsListeners.contains(listener)) {
+            return;
+        }
         mActiveSubscriptionsListeners.add(listener);
     }