Fix data usage multi-sim stuff

Bug: 26866568
Change-Id: I5d1a6547124c1744af7e30c5a02b46ca07cc81e8
This commit is contained in:
Jason Monk
2016-02-22 15:04:57 -05:00
parent 60df76b169
commit ac933ab5d7
2 changed files with 55 additions and 47 deletions

View File

@@ -14,10 +14,8 @@
package com.android.settings.datausage; package com.android.settings.datausage;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkPolicy; import android.net.NetworkPolicy;
import android.net.NetworkTemplate; import android.net.NetworkTemplate;
import android.os.Bundle; import android.os.Bundle;
@@ -26,6 +24,7 @@ import android.support.v7.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.datausage.CellDataPreference.DataStateListener;
public class BillingCyclePreference extends Preference implements TemplatePreference { public class BillingCyclePreference extends Preference implements TemplatePreference {
@@ -41,13 +40,12 @@ public class BillingCyclePreference extends Preference implements TemplatePrefer
@Override @Override
public void onAttached() { public void onAttached() {
super.onAttached(); super.onAttached();
getContext().registerReceiver(mReceiver, new IntentFilter( mListener.setListener(true, mSubId, getContext());
CellDataPreference.ACTION_DATA_ENABLED_CHANGED));
} }
@Override @Override
public void onDetached() { public void onDetached() {
getContext().unregisterReceiver(mReceiver); mListener.setListener(false, mSubId, getContext());
super.onDetached(); super.onDetached();
} }
@@ -81,9 +79,9 @@ public class BillingCyclePreference extends Preference implements TemplatePrefer
args, null, 0, getTitle(), false); args, null, 0, getTitle(), false);
} }
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final DataStateListener mListener = new DataStateListener() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onChange(boolean selfChange) {
updateEnabled(); updateEnabled();
} }
}; };

View File

@@ -15,14 +15,16 @@
package com.android.settings.datausage; package com.android.settings.datausage;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.database.ContentObserver;
import android.content.IntentFilter;
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.support.v7.preference.PreferenceViewHolder; import android.support.v7.preference.PreferenceViewHolder;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
@@ -31,7 +33,6 @@ import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.Checkable; import android.widget.Checkable;
import com.android.internal.logging.MetricsLogger; import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.MetricsProto.MetricsEvent; import com.android.internal.logging.MetricsProto.MetricsEvent;
import com.android.settings.CustomDialogPreference; import com.android.settings.CustomDialogPreference;
@@ -42,10 +43,6 @@ import java.util.List;
public class CellDataPreference extends CustomDialogPreference implements TemplatePreference { public class CellDataPreference extends CustomDialogPreference implements TemplatePreference {
// TODO: Get Telephony to broadcast when this changes, and remove this.
static final String ACTION_DATA_ENABLED_CHANGED =
"com.android.settings.action.DATA_ENABLED_CHANGED";
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;
@@ -84,12 +81,12 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
@Override @Override
public void onAttached() { public void onAttached() {
super.onAttached(); super.onAttached();
getContext().registerReceiver(mReceiver, new IntentFilter(ACTION_DATA_ENABLED_CHANGED)); mListener.setListener(true, mSubId, getContext());
} }
@Override @Override
public void onDetached() { public void onDetached() {
getContext().unregisterReceiver(mReceiver); mListener.setListener(false, mSubId, getContext());
super.onDetached(); super.onDetached();
} }
@@ -98,6 +95,7 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
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()); mTelephonyManager = TelephonyManager.from(getContext());
if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
mSubId = subId; mSubId = subId;
@@ -114,6 +112,22 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
protected void performClick(View view) { protected void performClick(View view) {
MetricsLogger.action(getContext(), MetricsEvent.ACTION_CELL_DATA_TOGGLE, !mChecked); MetricsLogger.action(getContext(), MetricsEvent.ACTION_CELL_DATA_TOGGLE, !mChecked);
if (mChecked) { if (mChecked) {
final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(
mSubId);
final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
// If the device is single SIM or is enabling data on the active data SIM then forgo
// the pop-up.
if (!Utils.showSimCardTile(getContext()) ||
(nextSir != null && currentSir != null &&
currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
setMobileDataEnabled(true);
if (nextSir != null && currentSir != null &&
currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
disableDataForOtherSubscriptions(mSubId);
}
return;
}
// disabling data; show confirmation dialog which eventually // disabling data; show confirmation dialog which eventually
// calls setMobileDataEnabled() once user confirms. // calls setMobileDataEnabled() once user confirms.
mMultiSimDialog = false; mMultiSimDialog = false;
@@ -130,10 +144,10 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
} }
private void setMobileDataEnabled(boolean enabled) { private void setMobileDataEnabled(boolean enabled) {
if (DataUsageSummary.LOGD) Log.d(TAG, "setMobileDataEnabled()"); if (DataUsageSummary.LOGD) Log.d(TAG, "setMobileDataEnabled(" + enabled + ","
+ mSubId + ")");
mTelephonyManager.setDataEnabled(mSubId, enabled); mTelephonyManager.setDataEnabled(mSubId, enabled);
setChecked(enabled); setChecked(enabled);
getContext().sendBroadcast(new Intent(ACTION_DATA_ENABLED_CHANGED));
} }
private void setChecked(boolean checked) { private void setChecked(boolean checked) {
@@ -170,24 +184,9 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
private void showMultiSimDialog(AlertDialog.Builder builder, private void showMultiSimDialog(AlertDialog.Builder builder,
DialogInterface.OnClickListener listener) { DialogInterface.OnClickListener listener) {
mSubscriptionManager = SubscriptionManager.from(getContext());
final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId); final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo(); final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
// If the device is single SIM or is enabling data on the active data SIM then forgo
// the pop-up.
if (!Utils.showSimCardTile(getContext()) ||
(nextSir != null && currentSir != null &&
currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
setMobileDataEnabled(true);
if (nextSir != null && currentSir != null &&
currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
disableDataForOtherSubscriptions(mSubId);
}
return;
}
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)
: nextSir.getDisplayName().toString(); : nextSir.getDisplayName().toString();
@@ -197,17 +196,8 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
String.valueOf(currentSir != null ? currentSir.getDisplayName() : null), String.valueOf(currentSir != null ? currentSir.getDisplayName() : null),
previousName)); previousName));
builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.okay, listener);
@Override
public void onClick(DialogInterface dialog, int id) {
mSubscriptionManager.setDefaultDataSubId(mSubId);
setMobileDataEnabled(true);
disableDataForOtherSubscriptions(mSubId);
}
});
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.create().show();
} }
private void disableDataForOtherSubscriptions(int subId) { private void disableDataForOtherSubscriptions(int subId) {
@@ -219,7 +209,6 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
} }
} }
} }
getContext().sendBroadcast(new Intent(ACTION_DATA_ENABLED_CHANGED));
} }
@Override @Override
@@ -228,19 +217,40 @@ public class CellDataPreference extends CustomDialogPreference implements Templa
return; return;
} }
if (mMultiSimDialog) { if (mMultiSimDialog) {
mSubscriptionManager.setDefaultDataSubId(mSubId);
setMobileDataEnabled(true);
disableDataForOtherSubscriptions(mSubId);
} else { } else {
// TODO: extend to modify policy enabled flag. // TODO: extend to modify policy enabled flag.
setMobileDataEnabled(false); setMobileDataEnabled(false);
} }
} }
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final DataStateListener mListener = new DataStateListener() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onChange(boolean selfChange) {
updateChecked(); 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);
}
}
}
public static class CellDataState extends BaseSavedState { public static class CellDataState extends BaseSavedState {
public int mSubId; public int mSubId;
public boolean mChecked; public boolean mChecked;