[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
This commit is contained in:
Bonian Chen
2019-11-08 07:40:35 +08:00
parent adfdb0ddf1
commit 5e65da0c2f
12 changed files with 285 additions and 191 deletions

View File

@@ -156,19 +156,19 @@ public final class Utils extends com.android.settingslib.Utils {
public static boolean updatePreferenceToSpecificActivityOrRemove(Context context, public static boolean updatePreferenceToSpecificActivityOrRemove(Context context,
PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) { PreferenceGroup parentPreferenceGroup, String preferenceKey, int flags) {
Preference preference = parentPreferenceGroup.findPreference(preferenceKey); final Preference preference = parentPreferenceGroup.findPreference(preferenceKey);
if (preference == null) { if (preference == null) {
return false; return false;
} }
Intent intent = preference.getIntent(); final Intent intent = preference.getIntent();
if (intent != null) { if (intent != null) {
// Find the activity that is in the system image // Find the activity that is in the system image
PackageManager pm = context.getPackageManager(); final PackageManager pm = context.getPackageManager();
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); final List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
int listSize = list.size(); final int listSize = list.size();
for (int i = 0; i < listSize; i++) { 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) if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
!= 0) { != 0) {
@@ -199,7 +199,7 @@ public final class Utils extends com.android.settingslib.Utils {
* @throws IllegalStateException if no UserManager could be retrieved. * @throws IllegalStateException if no UserManager could be retrieved.
*/ */
public static UserManager getUserManager(Context context) { public static UserManager getUserManager(Context context) {
UserManager um = UserManager.get(context); final UserManager um = UserManager.get(context);
if (um == null) { if (um == null) {
throw new IllegalStateException("Unable to load UserManager"); throw new IllegalStateException("Unable to load UserManager");
} }
@@ -217,7 +217,7 @@ public final class Utils extends com.android.settingslib.Utils {
* Returns whether the device is voice-capable (meaning, it is also a phone). * Returns whether the device is voice-capable (meaning, it is also a phone).
*/ */
public static boolean isVoiceCapable(Context context) { public static boolean isVoiceCapable(Context context) {
TelephonyManager telephony = final TelephonyManager telephony =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephony != null && telephony.isVoiceCapable(); return telephony != null && telephony.isVoiceCapable();
} }
@@ -228,12 +228,12 @@ public final class Utils extends com.android.settingslib.Utils {
* @return the formatted and newline-separated IP addresses, or null if none. * @return the formatted and newline-separated IP addresses, or null if none.
*/ */
public static String getWifiIpAddresses(Context context) { public static String getWifiIpAddresses(Context context) {
WifiManager wifiManager = context.getSystemService(WifiManager.class); final WifiManager wifiManager = context.getSystemService(WifiManager.class);
Network currentNetwork = wifiManager.getCurrentNetwork(); final Network currentNetwork = wifiManager.getCurrentNetwork();
if (currentNetwork != null) { if (currentNetwork != null) {
ConnectivityManager cm = (ConnectivityManager) final ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE); context.getSystemService(Context.CONNECTIVITY_SERVICE);
LinkProperties prop = cm.getLinkProperties(currentNetwork); final LinkProperties prop = cm.getLinkProperties(currentNetwork);
return formatIpAddresses(prop); return formatIpAddresses(prop);
} }
return null; return null;
@@ -241,7 +241,7 @@ public final class Utils extends com.android.settingslib.Utils {
private static String formatIpAddresses(LinkProperties prop) { private static String formatIpAddresses(LinkProperties prop) {
if (prop == null) return null; 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 there are no entries, return null
if (!iter.hasNext()) return null; if (!iter.hasNext()) return null;
// Concatenate all available addresses, comma separated // Concatenate all available addresses, comma separated
@@ -262,7 +262,7 @@ public final class Utils extends com.android.settingslib.Utils {
// And : new Locale("en_US").toString() => "en_us" // And : new Locale("en_US").toString() => "en_us"
if (null == localeStr) if (null == localeStr)
return Locale.getDefault(); return Locale.getDefault();
String[] brokenDownLocale = localeStr.split("_", 3); final String[] brokenDownLocale = localeStr.split("_", 3);
// split may not return a 0-length array. // split may not return a 0-length array.
if (1 == brokenDownLocale.length) { if (1 == brokenDownLocale.length) {
return new Locale(brokenDownLocale[0]); return new Locale(brokenDownLocale[0]);
@@ -396,7 +396,7 @@ public final class Utils extends com.android.settingslib.Utils {
* exists but it is disabled. * exists but it is disabled.
*/ */
public static UserHandle getManagedProfile(UserManager userManager) { public static UserHandle getManagedProfile(UserManager userManager) {
List<UserHandle> userProfiles = userManager.getUserProfiles(); final List<UserHandle> userProfiles = userManager.getUserProfiles();
for (UserHandle profile : userProfiles) { for (UserHandle profile : userProfiles) {
if (profile.getIdentifier() == userManager.getUserHandle()) { if (profile.getIdentifier() == userManager.getUserHandle()) {
continue; continue;
@@ -420,7 +420,7 @@ public final class Utils extends com.android.settingslib.Utils {
// we need to use UserManager.getProfiles that is available on API 23 (the one currently // we need to use UserManager.getProfiles that is available on API 23 (the one currently
// used for Settings Robolectric tests). // used for Settings Robolectric tests).
final int myUserId = UserHandle.myUserId(); final int myUserId = UserHandle.myUserId();
List<UserInfo> profiles = userManager.getProfiles(myUserId); final List<UserInfo> profiles = userManager.getProfiles(myUserId);
final int count = profiles.size(); final int count = profiles.size();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
final UserInfo profile = profiles.get(i); final UserInfo profile = profiles.get(i);
@@ -438,7 +438,7 @@ public final class Utils extends com.android.settingslib.Utils {
* @return the managed profile id or UserHandle.USER_NULL if there is none. * @return the managed profile id or UserHandle.USER_NULL if there is none.
*/ */
public static int getManagedProfileId(UserManager um, int parentUserId) { public static int getManagedProfileId(UserManager um, int parentUserId) {
int[] profileIds = um.getProfileIdsWithDisabled(parentUserId); final int[] profileIds = um.getProfileIdsWithDisabled(parentUserId);
for (int profileId : profileIds) { for (int profileId : profileIds) {
if (profileId != parentUserId) { if (profileId != parentUserId) {
return profileId; return profileId;
@@ -464,13 +464,14 @@ public final class Utils extends com.android.settingslib.Utils {
*/ */
public static UserHandle getSecureTargetUser(IBinder activityToken, public static UserHandle getSecureTargetUser(IBinder activityToken,
UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) { UserManager um, @Nullable Bundle arguments, @Nullable Bundle intentExtras) {
UserHandle currentUser = new UserHandle(UserHandle.myUserId()); final UserHandle currentUser = new UserHandle(UserHandle.myUserId());
IActivityManager am = ActivityManager.getService(); final IActivityManager am = ActivityManager.getService();
try { try {
String launchedFromPackage = am.getLaunchedFromPackage(activityToken); final String launchedFromPackage = am.getLaunchedFromPackage(activityToken);
boolean launchedFromSettingsApp = SETTINGS_PACKAGE_NAME.equals(launchedFromPackage); final boolean launchedFromSettingsApp =
SETTINGS_PACKAGE_NAME.equals(launchedFromPackage);
UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId( final UserHandle launchedFromUser = new UserHandle(UserHandle.getUserId(
am.getLaunchedFromUid(activityToken))); am.getLaunchedFromUid(activityToken)));
if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) { if (launchedFromUser != null && !launchedFromUser.equals(currentUser)) {
// Check it's secure // Check it's secure
@@ -478,14 +479,14 @@ public final class Utils extends com.android.settingslib.Utils {
return launchedFromUser; return launchedFromUser;
} }
} }
UserHandle extrasUser = getUserHandleFromBundle(intentExtras); final UserHandle extrasUser = getUserHandleFromBundle(intentExtras);
if (extrasUser != null && !extrasUser.equals(currentUser)) { if (extrasUser != null && !extrasUser.equals(currentUser)) {
// Check it's secure // Check it's secure
if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) { if (launchedFromSettingsApp && isProfileOf(um, extrasUser)) {
return extrasUser; return extrasUser;
} }
} }
UserHandle argumentsUser = getUserHandleFromBundle(arguments); final UserHandle argumentsUser = getUserHandleFromBundle(arguments);
if (argumentsUser != null && !argumentsUser.equals(currentUser)) { if (argumentsUser != null && !argumentsUser.equals(currentUser)) {
// Check it's secure // Check it's secure
if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) { if (launchedFromSettingsApp && isProfileOf(um, argumentsUser)) {
@@ -555,10 +556,11 @@ public final class Utils extends com.android.settingslib.Utils {
} }
public static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) { public static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) {
List<IntentFilterVerificationInfo> iviList = pm.getIntentFilterVerifications(packageName); final List<IntentFilterVerificationInfo> iviList =
List<IntentFilter> filters = pm.getAllIntentFilters(packageName); 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) { if (iviList != null && iviList.size() > 0) {
for (IntentFilterVerificationInfo ivi : iviList) { for (IntentFilterVerificationInfo ivi : iviList) {
for (String host : ivi.getDomains()) { for (String host : ivi.getDomains()) {
@@ -582,16 +584,16 @@ public final class Utils extends com.android.settingslib.Utils {
* Returns the application info of the currently installed MDM package. * Returns the application info of the currently installed MDM package.
*/ */
public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) { public static ApplicationInfo getAdminApplicationInfo(Context context, int profileId) {
DevicePolicyManager dpm = final DevicePolicyManager dpm =
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId); final ComponentName mdmPackage = dpm.getProfileOwnerAsUser(profileId);
if (mdmPackage == null) { if (mdmPackage == null) {
return null; return null;
} }
String mdmPackageName = mdmPackage.getPackageName(); final String mdmPackageName = mdmPackage.getPackageName();
try { try {
IPackageManager ipm = AppGlobals.getPackageManager(); final IPackageManager ipm = AppGlobals.getPackageManager();
ApplicationInfo mdmApplicationInfo = final ApplicationInfo mdmApplicationInfo =
ipm.getApplicationInfo(mdmPackageName, 0, profileId); ipm.getApplicationInfo(mdmPackageName, 0, profileId);
return mdmApplicationInfo; return mdmApplicationInfo;
} catch (RemoteException e) { } catch (RemoteException e) {
@@ -618,7 +620,7 @@ public final class Utils extends com.android.settingslib.Utils {
*/ */
public static SpannableString createAccessibleSequence(CharSequence displayText, public static SpannableString createAccessibleSequence(CharSequence displayText,
String accessibileText) { String accessibileText) {
SpannableString str = new SpannableString(displayText); final SpannableString str = new SpannableString(displayText);
str.setSpan(new TtsSpan.TextBuilder(accessibileText).build(), 0, str.setSpan(new TtsSpan.TextBuilder(accessibileText).build(), 0,
displayText.length(), displayText.length(),
Spannable.SPAN_INCLUSIVE_INCLUSIVE); Spannable.SPAN_INCLUSIVE_INCLUSIVE);
@@ -652,7 +654,7 @@ public final class Utils extends com.android.settingslib.Utils {
} }
final boolean allowAnyUser = isInternal final boolean allowAnyUser = isInternal
&& bundle.getBoolean(ChooseLockSettingsHelper.EXTRA_ALLOW_ANY_USER, false); && 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) { if (userId == LockPatternUtils.USER_FRP) {
return allowAnyUser ? userId : enforceSystemUser(context, userId); return allowAnyUser ? userId : enforceSystemUser(context, userId);
} else { } else {
@@ -699,7 +701,7 @@ public final class Utils extends com.android.settingslib.Utils {
* Returns the user id of the credential owner of the given user id. * Returns the user id of the credential owner of the given user id.
*/ */
public static int getCredentialOwnerUserId(Context context, int userId) { public static int getCredentialOwnerUserId(Context context, int userId) {
UserManager um = getUserManager(context); final UserManager um = getUserManager(context);
return um.getCredentialOwnerProfile(userId); return um.getCredentialOwnerProfile(userId);
} }
@@ -799,7 +801,7 @@ public final class Utils extends com.android.settingslib.Utils {
} }
public static boolean hasFingerprintHardware(Context context) { public static boolean hasFingerprintHardware(Context context) {
FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context); final FingerprintManager fingerprintManager = getFingerprintManagerOrNull(context);
return fingerprintManager != null && fingerprintManager.isHardwareDetected(); return fingerprintManager != null && fingerprintManager.isHardwareDetected();
} }
@@ -812,7 +814,7 @@ public final class Utils extends com.android.settingslib.Utils {
} }
public static boolean hasFaceHardware(Context context) { public static boolean hasFaceHardware(Context context) {
FaceManager faceManager = getFaceManagerOrNull(context); final FaceManager faceManager = getFaceManagerOrNull(context);
return faceManager != null && faceManager.isHardwareDetected(); return faceManager != null && faceManager.isHardwareDetected();
} }
@@ -865,7 +867,7 @@ public final class Utils extends com.android.settingslib.Utils {
public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) { public static VolumeInfo maybeInitializeVolume(StorageManager sm, Bundle bundle) {
final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID, final String volumeId = bundle.getString(VolumeInfo.EXTRA_VOLUME_ID,
VolumeInfo.ID_PRIVATE_INTERNAL); VolumeInfo.ID_PRIVATE_INTERNAL);
VolumeInfo volume = sm.findVolumeById(volumeId); final VolumeInfo volume = sm.findVolumeById(volumeId);
return isVolumeValid(volume) ? volume : null; return isVolumeValid(volume) ? volume : null;
} }
@@ -878,12 +880,13 @@ public final class Utils extends com.android.settingslib.Utils {
*/ */
public static boolean isProfileOrDeviceOwner(UserManager userManager, public static boolean isProfileOrDeviceOwner(UserManager userManager,
DevicePolicyManager devicePolicyManager, String packageName) { DevicePolicyManager devicePolicyManager, String packageName) {
List<UserInfo> userInfos = userManager.getUsers(); final List<UserInfo> userInfos = userManager.getUsers();
if (devicePolicyManager.isDeviceOwnerAppOnAnyUser(packageName)) { if (devicePolicyManager.isDeviceOwnerAppOnAnyUser(packageName)) {
return true; return true;
} }
for (int i = 0, size = userInfos.size(); i < size; i++) { 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)) { if (cn != null && cn.getPackageName().equals(packageName)) {
return true; return true;
} }
@@ -938,9 +941,9 @@ public final class Utils extends com.android.settingslib.Utils {
return original; return original;
} }
float scaleWidth = ((float) maxWidth) / actualWidth; final float scaleWidth = ((float) maxWidth) / actualWidth;
float scaleHeight = ((float) maxHeight) / actualHeight; final float scaleHeight = ((float) maxHeight) / actualHeight;
float scale = Math.min(scaleWidth, scaleHeight); final float scale = Math.min(scaleWidth, scaleHeight);
final int width = (int) (actualWidth * scale); final int width = (int) (actualWidth * scale);
final int height = (int) (actualHeight * scale); final int height = (int) (actualHeight * scale);

View File

@@ -26,27 +26,39 @@ import androidx.preference.Preference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher; 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 NetworkTemplate mTemplate;
private NetworkServices mServices; private NetworkServices mServices;
private int mSubId; 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) { public BillingCyclePreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
mListener = new MobileDataEnabledListener(context, this);
} }
@Override @Override
public void onAttached() { public void onAttached() {
super.onAttached(); super.onAttached();
mListener.setListener(true, mSubId, getContext()); mListener.start(mSubId);
} }
@Override @Override
public void onDetached() { public void onDetached() {
mListener.setListener(false, mSubId, getContext()); mListener.stop();
super.onDetached(); super.onDetached();
} }
@@ -73,7 +85,7 @@ public class BillingCyclePreference extends Preference implements TemplatePrefer
@Override @Override
public Intent getIntent() { public Intent getIntent() {
Bundle args = new Bundle(); final Bundle args = new Bundle();
args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate); args.putParcelable(DataUsageList.EXTRA_NETWORK_TEMPLATE, mTemplate);
return new SubSettingLauncher(getContext()) return new SubSettingLauncher(getContext())
.setDestination(BillingCycleSettings.class.getName()) .setDestination(BillingCycleSettings.class.getName())
@@ -83,10 +95,10 @@ public class BillingCyclePreference extends Preference implements TemplatePrefer
.toIntent(); .toIntent();
} }
private final DataStateListener mListener = new DataStateListener() { /**
@Override * Implementation of MobileDataEnabledListener.Client
public void onChange(boolean selfChange) { */
updateEnabled(); public void onMobileDataEnabledChange() {
} updateEnabled();
}; }
} }

View File

@@ -17,14 +17,9 @@ package com.android.settings.datausage;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.database.ContentObserver;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.Settings.Global;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
@@ -39,33 +34,38 @@ import androidx.core.content.res.TypedArrayUtils;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import com.android.settings.R; 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.settings.overlay.FeatureFactory;
import com.android.settingslib.CustomDialogPreferenceCompat; import com.android.settingslib.CustomDialogPreferenceCompat;
import java.util.List; /**
* Preference of cellular data control within Data Usage
public class CellDataPreference extends CustomDialogPreferenceCompat implements TemplatePreference { */
public class CellDataPreference extends CustomDialogPreferenceCompat
implements TemplatePreference, MobileDataEnabledListener.Client {
private static final String TAG = "CellDataPreference"; private static final String TAG = "CellDataPreference";
public int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; public int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
public boolean mChecked; public boolean mChecked;
public boolean mMultiSimDialog; public boolean mMultiSimDialog;
private TelephonyManager mTelephonyManager;
@VisibleForTesting @VisibleForTesting
SubscriptionManager mSubscriptionManager; ProxySubscriptionManager mProxySubscriptionMgr;
private MobileDataEnabledListener mDataStateListener;
public CellDataPreference(Context context, AttributeSet attrs) { public CellDataPreference(Context context, AttributeSet attrs) {
super(context, attrs, TypedArrayUtils.getAttr(context, super(context, attrs, TypedArrayUtils.getAttr(context,
androidx.preference.R.attr.switchPreferenceStyle, androidx.preference.R.attr.switchPreferenceStyle,
android.R.attr.switchPreferenceStyle)); android.R.attr.switchPreferenceStyle));
mProxySubscriptionMgr = ProxySubscriptionManager.getInstance(context);
mDataStateListener = new MobileDataEnabledListener(context, this);
} }
@Override @Override
protected void onRestoreInstanceState(Parcelable s) { protected void onRestoreInstanceState(Parcelable s) {
CellDataState state = (CellDataState) s; final CellDataState state = (CellDataState) s;
super.onRestoreInstanceState(state.getSuperState()); super.onRestoreInstanceState(state.getSuperState());
mTelephonyManager = TelephonyManager.from(getContext());
mChecked = state.mChecked; mChecked = state.mChecked;
mMultiSimDialog = state.mMultiSimDialog; mMultiSimDialog = state.mMultiSimDialog;
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
@@ -77,7 +77,7 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
@Override @Override
protected Parcelable onSaveInstanceState() { protected Parcelable onSaveInstanceState() {
CellDataState state = new CellDataState(super.onSaveInstanceState()); final CellDataState state = new CellDataState(super.onSaveInstanceState());
state.mChecked = mChecked; state.mChecked = mChecked;
state.mMultiSimDialog = mMultiSimDialog; state.mMultiSimDialog = mMultiSimDialog;
state.mSubId = mSubId; state.mSubId = mSubId;
@@ -87,19 +87,14 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
@Override @Override
public void onAttached() { public void onAttached() {
super.onAttached(); super.onAttached();
mDataStateListener.setListener(true, mSubId, getContext()); mDataStateListener.start(mSubId);
if (mSubscriptionManager!= null) { mProxySubscriptionMgr.addActiveSubscriptionsListener(mOnSubscriptionsChangeListener);
mSubscriptionManager.addOnSubscriptionsChangedListener(mOnSubscriptionsChangeListener);
}
} }
@Override @Override
public void onDetached() { public void onDetached() {
mDataStateListener.setListener(false, mSubId, getContext()); mDataStateListener.stop();
if (mSubscriptionManager!= null) { mProxySubscriptionMgr.removeActiveSubscriptionsListener(mOnSubscriptionsChangeListener);
mSubscriptionManager.removeOnSubscriptionsChangedListener(
mOnSubscriptionsChangeListener);
}
super.onDetached(); super.onDetached();
} }
@@ -108,10 +103,9 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
throw new IllegalArgumentException("CellDataPreference needs a SubscriptionInfo"); 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) { if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mSubId = subId; mSubId = subId;
@@ -122,13 +116,13 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
} }
private void updateChecked() { private void updateChecked() {
setChecked(mTelephonyManager.getDataEnabled(mSubId)); setChecked(getContext().getSystemService(TelephonyManager.class).getDataEnabled(mSubId));
} }
private void updateEnabled() { private void updateEnabled() {
// If this subscription is not active, for example, SIM card is taken out, we disable // If this subscription is not active, for example, SIM card is taken out, we disable
// the button. // the button.
setEnabled(mSubscriptionManager.getActiveSubscriptionInfo(mSubId) != null); setEnabled(mProxySubscriptionMgr.getActiveSubscriptionInfo(mSubId) != null);
} }
@Override @Override
@@ -136,9 +130,10 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
final Context context = getContext(); final Context context = getContext();
FeatureFactory.getFactory(context).getMetricsFeatureProvider() FeatureFactory.getFactory(context).getMetricsFeatureProvider()
.action(context, SettingsEnums.ACTION_CELL_DATA_TOGGLE, !mChecked); .action(context, SettingsEnums.ACTION_CELL_DATA_TOGGLE, !mChecked);
final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo( final SubscriptionInfo currentSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
mSubId); mSubId);
final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo(); final SubscriptionInfo nextSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
SubscriptionManager.getDefaultDataSubscriptionId());
if (mChecked) { if (mChecked) {
setMobileDataEnabled(false); setMobileDataEnabled(false);
if (nextSir != null && currentSir != null if (nextSir != null && currentSir != null
@@ -153,7 +148,7 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
private void setMobileDataEnabled(boolean enabled) { private void setMobileDataEnabled(boolean enabled) {
if (DataUsageSummary.LOGD) Log.d(TAG, "setMobileDataEnabled(" + enabled + "," if (DataUsageSummary.LOGD) Log.d(TAG, "setMobileDataEnabled(" + enabled + ","
+ mSubId + ")"); + mSubId + ")");
mTelephonyManager.setDataEnabled(mSubId, enabled); getContext().getSystemService(TelephonyManager.class).setDataEnabled(mSubId, enabled);
setChecked(enabled); setChecked(enabled);
} }
@@ -166,7 +161,7 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
@Override @Override
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(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); switchView.setClickable(false);
((Checkable) switchView).setChecked(mChecked); ((Checkable) switchView).setChecked(mChecked);
} }
@@ -191,8 +186,10 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
private void showMultiSimDialog(Builder builder, private void showMultiSimDialog(Builder builder,
DialogInterface.OnClickListener listener) { DialogInterface.OnClickListener listener) {
final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId); final SubscriptionInfo currentSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo(); mSubId);
final SubscriptionInfo nextSir = mProxySubscriptionMgr.getActiveSubscriptionInfo(
SubscriptionManager.getDefaultDataSubscriptionId());
final String previousName = (nextSir == null) final String previousName = (nextSir == null)
? getContext().getResources().getString(R.string.sim_selection_required_pref) ? getContext().getResources().getString(R.string.sim_selection_required_pref)
@@ -208,14 +205,10 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
} }
private void disableDataForOtherSubscriptions(int subId) { private void disableDataForOtherSubscriptions(int subId) {
List<SubscriptionInfo> subInfoList = mSubscriptionManager final SubscriptionInfo subInfo = mProxySubscriptionMgr.getActiveSubscriptionInfo(
.getActiveSubscriptionInfoList(true); subId);
if (subInfoList != null) { if (subInfo != null) {
for (SubscriptionInfo subInfo : subInfoList) { getContext().getSystemService(TelephonyManager.class).setDataEnabled(subId, false);
if (subInfo.getSubscriptionId() != subId) {
mTelephonyManager.setDataEnabled(subInfo.getSubscriptionId(), false);
}
}
} }
} }
@@ -225,7 +218,7 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
return; return;
} }
if (mMultiSimDialog) { if (mMultiSimDialog) {
mSubscriptionManager.setDefaultDataSubId(mSubId); mProxySubscriptionMgr.get().setDefaultDataSubId(mSubId);
setMobileDataEnabled(true); setMobileDataEnabled(true);
disableDataForOtherSubscriptions(mSubId); disableDataForOtherSubscriptions(mSubId);
} else { } else {
@@ -235,40 +228,23 @@ public class CellDataPreference extends CustomDialogPreferenceCompat implements
} }
@VisibleForTesting @VisibleForTesting
final SubscriptionManager.OnSubscriptionsChangedListener mOnSubscriptionsChangeListener final ProxySubscriptionManager.OnActiveSubscriptionChangedListener
= new SubscriptionManager.OnSubscriptionsChangedListener() { mOnSubscriptionsChangeListener =
@Override new ProxySubscriptionManager.OnActiveSubscriptionChangedListener() {
public void onSubscriptionsChanged() { public void onChanged() {
if (DataUsageSummary.LOGD) { if (DataUsageSummary.LOGD) {
Log.d(TAG, "onSubscriptionsChanged"); Log.d(TAG, "onSubscriptionsChanged");
} }
updateEnabled(); 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);
} }
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 { public static class CellDataState extends BaseSavedState {

View File

@@ -39,7 +39,6 @@ import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.View; import android.view.View;
@@ -57,6 +56,8 @@ import androidx.preference.PreferenceGroup;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settings.datausage.CycleAdapter.SpinnerInterface; 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.settings.widget.LoadingViewController;
import com.android.settingslib.AppItem; import com.android.settingslib.AppItem;
import com.android.settingslib.net.NetworkCycleChartData; import com.android.settingslib.net.NetworkCycleChartData;
@@ -72,7 +73,8 @@ import java.util.List;
* Panel showing data usage history across various networks, including options * Panel showing data usage history across various networks, including options
* to inspect based on usage cycle and control through {@link NetworkPolicy}. * 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_SUB_ID = "sub_id";
static final String EXTRA_NETWORK_TEMPLATE = "network_template"; static final String EXTRA_NETWORK_TEMPLATE = "network_template";
@@ -91,13 +93,8 @@ public class DataUsageList extends DataUsageBaseFragment {
private static final int LOADER_CHART_DATA = 2; private static final int LOADER_CHART_DATA = 2;
private static final int LOADER_SUMMARY = 3; private static final int LOADER_SUMMARY = 3;
private final CellDataPreference.DataStateListener mDataStateListener = @VisibleForTesting
new CellDataPreference.DataStateListener() { MobileDataEnabledListener mDataStateListener;
@Override
public void onChange(boolean selfChange) {
updatePolicy();
}
};
@VisibleForTesting @VisibleForTesting
NetworkTemplate mTemplate; NetworkTemplate mTemplate;
@@ -111,7 +108,6 @@ public class DataUsageList extends DataUsageBaseFragment {
LoadingViewController mLoadingViewController; LoadingViewController mLoadingViewController;
private ChartDataUsagePreference mChart; private ChartDataUsagePreference mChart;
private TelephonyManager mTelephonyManager;
private List<NetworkCycleChartData> mCycleData; private List<NetworkCycleChartData> mCycleData;
private ArrayList<Long> mCycles; private ArrayList<Long> mCycles;
private UidDetailProvider mUidDetailProvider; private UidDetailProvider mUidDetailProvider;
@@ -133,14 +129,15 @@ public class DataUsageList extends DataUsageBaseFragment {
if (!isBandwidthControlEnabled()) { if (!isBandwidthControlEnabled()) {
Log.w(TAG, "No bandwidth control; leaving"); Log.w(TAG, "No bandwidth control; leaving");
activity.finish(); activity.finish();
return;
} }
mUidDetailProvider = new UidDetailProvider(activity); mUidDetailProvider = new UidDetailProvider(activity);
mTelephonyManager = activity.getSystemService(TelephonyManager.class);
mUsageAmount = findPreference(KEY_USAGE_AMOUNT); mUsageAmount = findPreference(KEY_USAGE_AMOUNT);
mChart = findPreference(KEY_CHART_DATA); mChart = findPreference(KEY_CHART_DATA);
mApps = findPreference(KEY_APPS_GROUP); mApps = findPreference(KEY_APPS_GROUP);
processArgument(); processArgument();
mDataStateListener = new MobileDataEnabledListener(activity, this);
} }
@Override @Override
@@ -190,20 +187,21 @@ public class DataUsageList extends DataUsageBaseFragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mDataStateListener.setListener(true, mSubId, getContext()); mDataStateListener.start(mSubId);
updateBody(); updateBody();
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
mDataStateListener.setListener(false, mSubId, getContext()); mDataStateListener.stop();
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
mUidDetailProvider.clearCache(); mUidDetailProvider.clearCache();
mUidDetailProvider = null; mUidDetailProvider = null;
mDataStateListener.stop();
super.onDestroy(); super.onDestroy();
} }
@@ -233,6 +231,13 @@ public class DataUsageList extends DataUsageBaseFragment {
} }
} }
/**
* Implementation of MobileDataEnabledListener.Client
*/
public void onMobileDataEnabledChange() {
updatePolicy();
}
/** /**
* Update body content based on current tab. Loads network cycle data from system, and * Update body content based on current tab. Loads network cycle data from system, and
* binds them to visible controls. * binds them to visible controls.
@@ -253,7 +258,7 @@ public class DataUsageList extends DataUsageBaseFragment {
int seriesColor = context.getColor(R.color.sim_noitification); int seriesColor = context.getColor(R.color.sim_noitification);
if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mSubId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
final SubscriptionInfo sir = services.mSubscriptionManager final SubscriptionInfo sir = ProxySubscriptionManager.getInstance(context)
.getActiveSubscriptionInfo(mSubId); .getActiveSubscriptionInfo(mSubId);
if (sir != null) { if (sir != null) {
@@ -400,7 +405,7 @@ public class DataUsageList extends DataUsageBaseFragment {
Collections.sort(items); Collections.sort(items);
for (int i = 0; i < items.size(); i++) { for (int i = 0; i < items.size(); i++) {
final int percentTotal = largest != 0 ? (int) (items.get(i).total * 100 / largest) : 0; 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); items.get(i), percentTotal, mUidDetailProvider);
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override

View File

@@ -33,6 +33,7 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.network.ProxySubscriptionManager;
import com.android.settingslib.NetworkPolicyEditor; import com.android.settingslib.NetworkPolicyEditor;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -65,6 +66,7 @@ public class DataUsageSummary extends DataUsageBaseFragment implements DataUsage
private DataUsageSummaryPreference mSummaryPreference; private DataUsageSummaryPreference mSummaryPreference;
private DataUsageSummaryPreferenceController mSummaryController; private DataUsageSummaryPreferenceController mSummaryController;
private NetworkTemplate mDefaultTemplate; private NetworkTemplate mDefaultTemplate;
private ProxySubscriptionManager mProxySubscriptionMgr;
@Override @Override
public int getHelpResource() { public int getHelpResource() {
@@ -76,6 +78,11 @@ public class DataUsageSummary extends DataUsageBaseFragment implements DataUsage
super.onCreate(icicle); super.onCreate(icicle);
Context context = getContext(); 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); boolean hasMobileData = DataUsageUtils.hasMobileData(context);
final int defaultSubId = SubscriptionManager.getDefaultDataSubscriptionId(); final int defaultSubId = SubscriptionManager.getDefaultDataSubscriptionId();

View File

@@ -85,6 +85,7 @@ public abstract class ActiveSubsciptionsListener
@VisibleForTesting @VisibleForTesting
BroadcastReceiver mSubscriptionChangeReceiver; BroadcastReceiver mSubscriptionChangeReceiver;
private Integer mMaxActiveSubscriptionInfos;
private List<SubscriptionInfo> mCachedActiveSubscriptionInfo; private List<SubscriptionInfo> mCachedActiveSubscriptionInfo;
/** /**
@@ -125,6 +126,24 @@ public abstract class ActiveSubsciptionsListener
return mSubscriptionManager; return mSubscriptionManager;
} }
/**
* 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 * Get a list of active subscription info
* *
@@ -134,7 +153,7 @@ public abstract class ActiveSubsciptionsListener
if (mIsCachedDataAvailable) { if (mIsCachedDataAvailable) {
return mCachedActiveSubscriptionInfo; return mCachedActiveSubscriptionInfo;
} }
mIsCachedDataAvailable = true; mIsCachedDataAvailable = mIsMonitoringDataChange;
mCachedActiveSubscriptionInfo = getSubscriptionManager().getActiveSubscriptionInfoList(); mCachedActiveSubscriptionInfo = getSubscriptionManager().getActiveSubscriptionInfoList();
return mCachedActiveSubscriptionInfo; return mCachedActiveSubscriptionInfo;
} }
@@ -163,6 +182,7 @@ public abstract class ActiveSubsciptionsListener
*/ */
public void clearCache() { public void clearCache() {
mIsCachedDataAvailable = false; mIsCachedDataAvailable = false;
mMaxActiveSubscriptionInfos = null;
mCachedActiveSubscriptionInfo = null; mCachedActiveSubscriptionInfo = null;
} }

View File

@@ -33,7 +33,8 @@ import androidx.lifecycle.OnLifecycleEvent;
/** /**
* A listener for Settings.Global configuration change, with support of Lifecycle * 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 * Constructor
@@ -41,7 +42,7 @@ abstract class GlobalSettingsChangeListener extends ContentObserver implements L
* @param context of this listener * @param context of this listener
* @param field field of Global Settings * @param field field of Global Settings
*/ */
GlobalSettingsChangeListener(Context context, String field) { public GlobalSettingsChangeListener(Context context, String field) {
super(new Handler()); super(new Handler());
mContext = context; mContext = context;
mField = field; mField = field;
@@ -92,6 +93,13 @@ abstract class GlobalSettingsChangeListener extends ContentObserver implements L
@OnLifecycleEvent(ON_DESTROY) @OnLifecycleEvent(ON_DESTROY)
void onDestroy() { void onDestroy() {
close();
}
/**
* Implementation of AutoCloseable
*/
public void close() {
monitorUri(false); monitorUri(false);
notifyChangeBasedOn(null); notifyChangeBasedOn(null);
} }

View File

@@ -17,53 +17,98 @@
package com.android.settings.network; package com.android.settings.network;
import android.content.Context; import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
/** Helper class to listen for changes in the enabled state of mobile data. */ /** 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 Context mContext;
private Client mClient; private Client mClient;
private int mSubId; 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 { public interface Client {
void onMobileDataEnabledChange(); void onMobileDataEnabledChange();
} }
/**
* Constructor
*
* @param context of this listener
* @param client callback when configuration changed
*/
public MobileDataEnabledListener(Context context, Client client) { public MobileDataEnabledListener(Context context, Client client) {
super(new Handler());
mContext = context; mContext = context;
mClient = client; mClient = client;
mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 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) { public void start(int subId) {
mSubId = subId; mSubId = subId;
Uri uri;
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mListener == null) {
uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA); mListener = new GlobalSettingsChangeListener(mContext,
} else { Settings.Global.MOBILE_DATA) {
uri = Settings.Global.getUriFor(Settings.Global.MOBILE_DATA + mSubId); public void onChanged(String field) {
mClient.onMobileDataEnabledChange();
}
};
} }
mContext.getContentResolver().registerContentObserver(uri, true /*notifyForDescendants*/, stopMonitorSubIdSpecific();
this);
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() { public int getSubId() {
return mSubId; return mSubId;
} }
public MobileDataEnabledListener stop() { /**
mContext.getContentResolver().unregisterContentObserver(this); * Stop listening to changes in the enabled state for data.
return this; */
public void stop() {
stopMonitor();
stopMonitorSubIdSpecific();
} }
@Override private void stopMonitor() {
public void onChange(boolean selfChange) { if (mListener != null) {
mClient.onMobileDataEnabledChange(); mListener.close();
mListener = null;
}
}
private void stopMonitorSubIdSpecific() {
if (mListenerForSubId != null) {
mListenerForSubId.close();
mListenerForSubId = null;
}
} }
} }

View File

@@ -82,7 +82,6 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
} }
}; };
mKeepCacheWhenOnStart = true;
mSubsciptionsMonitor.start(); mSubsciptionsMonitor.start();
} }
@@ -90,7 +89,6 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
private Context mContext; private Context mContext;
private ActiveSubsciptionsListener mSubsciptionsMonitor; private ActiveSubsciptionsListener mSubsciptionsMonitor;
private GlobalSettingsChangeListener mAirplaneModeMonitor; private GlobalSettingsChangeListener mAirplaneModeMonitor;
private boolean mKeepCacheWhenOnStart;
private List<OnActiveSubscriptionChangedListener> mActiveSubscriptionsListeners; private List<OnActiveSubscriptionChangedListener> mActiveSubscriptionsListeners;
@@ -100,6 +98,12 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
} }
} }
@Override
public void onSubscriptionsChanged() {
clearCache();
notifyAllListeners();
}
/** /**
* Lifecycle for data within proxy * Lifecycle for data within proxy
* *
@@ -118,15 +122,11 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
@OnLifecycleEvent(ON_START) @OnLifecycleEvent(ON_START)
void onStart() { void onStart() {
if (!mKeepCacheWhenOnStart) {
mSubsciptionsMonitor.clearCache();
}
mSubsciptionsMonitor.start(); mSubsciptionsMonitor.start();
} }
@OnLifecycleEvent(ON_STOP) @OnLifecycleEvent(ON_STOP)
void onStop() { void onStop() {
mKeepCacheWhenOnStart = false;
mSubsciptionsMonitor.stop(); mSubsciptionsMonitor.stop();
} }
@@ -151,6 +151,15 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
return mSubsciptionsMonitor.getSubscriptionManager(); return mSubsciptionsMonitor.getSubscriptionManager();
} }
/**
* 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 * Get a list of active subscription info
* *
@@ -183,6 +192,9 @@ public class ProxySubscriptionManager extends SubscriptionManager.OnSubscription
* @param listener listener to active subscriptions change * @param listener listener to active subscriptions change
*/ */
public void addActiveSubscriptionsListener(OnActiveSubscriptionChangedListener listener) { public void addActiveSubscriptionsListener(OnActiveSubscriptionChangedListener listener) {
if (mActiveSubscriptionsListeners.contains(listener)) {
return;
}
mActiveSubscriptionsListeners.add(listener); mActiveSubscriptionsListeners.add(listener);
} }

View File

@@ -18,17 +18,18 @@ package com.android.settings.datausage;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.doReturn;
import android.content.Context; import android.content.Context;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import androidx.preference.PreferenceViewHolder; import androidx.preference.PreferenceViewHolder;
import com.android.settings.network.ProxySubscriptionManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -41,14 +42,13 @@ import org.robolectric.RuntimeEnvironment;
public class CellDataPreferenceTest { public class CellDataPreferenceTest {
@Mock @Mock
private SubscriptionManager mSubscriptionManager; private ProxySubscriptionManager mProxySubscriptionMgr;
@Mock @Mock
private SubscriptionInfo mSubInfo; private SubscriptionInfo mSubInfo;
private Context mContext; private Context mContext;
private PreferenceViewHolder mHolder; private PreferenceViewHolder mHolder;
private CellDataPreference mPreference; private CellDataPreference mPreference;
private SubscriptionManager.OnSubscriptionsChangedListener mListener;
@Before @Before
public void setUp() { public void setUp() {
@@ -56,9 +56,9 @@ public class CellDataPreferenceTest {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mPreference = new CellDataPreference(mContext, null); mPreference = new CellDataPreference(mContext, null);
mListener = mPreference.mOnSubscriptionsChangeListener; mPreference.mProxySubscriptionMgr = mProxySubscriptionMgr;
LayoutInflater inflater = LayoutInflater.from(mContext); final LayoutInflater inflater = LayoutInflater.from(mContext);
final View view = inflater.inflate(mPreference.getLayoutResource(), final View view = inflater.inflate(mPreference.getLayoutResource(),
new LinearLayout(mContext), false); new LinearLayout(mContext), false);
@@ -67,12 +67,15 @@ public class CellDataPreferenceTest {
@Test @Test
public void noActiveSub_shouldDisable() { public void noActiveSub_shouldDisable() {
mPreference.mSubscriptionManager = mSubscriptionManager; doReturn(null).when(mProxySubscriptionMgr).getActiveSubscriptionInfo(anyInt());
mListener.onSubscriptionsChanged(); mPreference.mOnSubscriptionsChangeListener.onChanged();
assertThat(mPreference.isEnabled()).isFalse(); assertThat(mPreference.isEnabled()).isFalse();
}
when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(mSubInfo); @Test
mListener.onSubscriptionsChanged(); public void hasActiveSub_shouldEnable() {
doReturn(mSubInfo).when(mProxySubscriptionMgr).getActiveSubscriptionInfo(anyInt());
mPreference.mOnSubscriptionsChangeListener.onChanged();
assertThat(mPreference.isEnabled()).isTrue(); assertThat(mPreference.isEnabled()).isTrue();
} }
} }

View File

@@ -42,6 +42,7 @@ import androidx.preference.PreferenceManager;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.network.MobileDataEnabledListener;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.LoadingViewController; import com.android.settings.widget.LoadingViewController;
import com.android.settingslib.AppItem; import com.android.settingslib.AppItem;
@@ -67,7 +68,7 @@ import java.util.List;
public class DataUsageListTest { public class DataUsageListTest {
@Mock @Mock
private CellDataPreference.DataStateListener mListener; private MobileDataEnabledListener mMobileDataEnabledListener;
@Mock @Mock
private TemplatePreference.NetworkServices mNetworkServices; private TemplatePreference.NetworkServices mNetworkServices;
@@ -83,9 +84,11 @@ public class DataUsageListTest {
mActivity = spy(mActivityController.get()); mActivity = spy(mActivityController.get());
mNetworkServices.mPolicyEditor = mock(NetworkPolicyEditor.class); mNetworkServices.mPolicyEditor = mock(NetworkPolicyEditor.class);
mDataUsageList = spy(DataUsageList.class); mDataUsageList = spy(DataUsageList.class);
mDataUsageList.mDataStateListener = mMobileDataEnabledListener;
doReturn(mActivity).when(mDataUsageList).getContext(); doReturn(mActivity).when(mDataUsageList).getContext();
ReflectionHelpers.setField(mDataUsageList, "mDataStateListener", mListener); ReflectionHelpers.setField(mDataUsageList, "mDataStateListener",
mMobileDataEnabledListener);
ReflectionHelpers.setField(mDataUsageList, "services", mNetworkServices); ReflectionHelpers.setField(mDataUsageList, "services", mNetworkServices);
} }
@@ -98,11 +101,11 @@ public class DataUsageListTest {
mDataUsageList.onResume(); mDataUsageList.onResume();
verify(mListener).setListener(true, mDataUsageList.mSubId, mActivity); verify(mMobileDataEnabledListener).start(anyInt());
mDataUsageList.onPause(); mDataUsageList.onPause();
verify(mListener).setListener(false, mDataUsageList.mSubId, mActivity); verify(mMobileDataEnabledListener).stop();
} }
@Test @Test

View File

@@ -54,7 +54,7 @@ public final class DataUsageUtilsTest {
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
ShadowApplication shadowContext = ShadowApplication.getInstance(); final ShadowApplication shadowContext = ShadowApplication.getInstance();
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager); shadowContext.setSystemService(Context.CONNECTIVITY_SERVICE, mManager);
shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager); shadowContext.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
@@ -64,28 +64,28 @@ public final class DataUsageUtilsTest {
@Test @Test
public void mobileDataStatus_whenNetworkIsSupported() { public void mobileDataStatus_whenNetworkIsSupported() {
when(mManager.isNetworkSupported(anyInt())).thenReturn(true); when(mManager.isNetworkSupported(anyInt())).thenReturn(true);
boolean hasMobileData = DataUsageUtils.hasMobileData(mContext); final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
assertThat(hasMobileData).isTrue(); assertThat(hasMobileData).isTrue();
} }
@Test @Test
public void mobileDataStatus_whenNetworkIsNotSupported() { public void mobileDataStatus_whenNetworkIsNotSupported() {
when(mManager.isNetworkSupported(anyInt())).thenReturn(false); when(mManager.isNetworkSupported(anyInt())).thenReturn(false);
boolean hasMobileData = DataUsageUtils.hasMobileData(mContext); final boolean hasMobileData = DataUsageUtils.hasMobileData(mContext);
assertThat(hasMobileData).isFalse(); assertThat(hasMobileData).isFalse();
} }
@Test @Test
public void hasSim_simStateReady() { public void hasSim_simStateReady() {
when(mTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY); when(mTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_READY);
boolean hasSim = DataUsageUtils.hasSim(mContext); final boolean hasSim = DataUsageUtils.hasSim(mContext);
assertThat(hasSim).isTrue(); assertThat(hasSim).isTrue();
} }
@Test @Test
public void hasSim_simStateMissing() { public void hasSim_simStateMissing() {
when(mTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_ABSENT); when(mTelephonyManager.getSimState()).thenReturn(TelephonyManager.SIM_STATE_ABSENT);
boolean hasSim = DataUsageUtils.hasSim(mContext); final boolean hasSim = DataUsageUtils.hasSim(mContext);
assertThat(hasSim).isFalse(); assertThat(hasSim).isFalse();
} }