blob: d92686eddf903784a2ec7455d123d37199d22603 [file] [log] [blame]
Malcolm Chen9ef63c62017-06-20 16:53:01 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.Handler;
25import android.os.Looper;
26import android.os.Parcel;
27import android.os.Parcelable;
28import android.preference.DialogPreference;
29import android.preference.PreferenceScreen;
30import android.provider.Settings.Global;
31import android.telephony.SubscriptionInfo;
32import android.telephony.SubscriptionManager;
33import android.telephony.TelephonyManager;
34import android.util.AttributeSet;
35import android.util.Log;
36import android.view.View;
37import android.widget.Checkable;
38
Malcolm Chen66c618e2017-07-24 16:44:32 -070039import com.android.internal.logging.MetricsLogger;
40import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
41
Malcolm Chen9ef63c62017-06-20 16:53:01 -070042import java.util.List;
43
44/**
45 * Customized Preference to enable / disable mobile data.
46 * Basically copy of with com.android.settings.CellDataPreference.
47 */
48public class MobileDataPreference extends DialogPreference {
49
50 private static final boolean DBG = false;
51 private static final String TAG = "MobileDataPreference";
52
53 public int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
54 public boolean mChecked;
55 public boolean mMultiSimDialog;
56 private TelephonyManager mTelephonyManager;
57 private SubscriptionManager mSubscriptionManager;
58
59 public MobileDataPreference(Context context, AttributeSet attrs) {
60 super(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
61 }
62
63 @Override
64 protected void onRestoreInstanceState(Parcelable s) {
65 CellDataState state = (CellDataState) s;
66 super.onRestoreInstanceState(state.getSuperState());
67 mTelephonyManager = TelephonyManager.from(getContext());
68 mChecked = state.mChecked;
69 mMultiSimDialog = state.mMultiSimDialog;
70 if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
71 mSubId = state.mSubId;
72 setKey(getKey() + mSubId);
73 }
74 notifyChanged();
75 }
76
77 @Override
78 protected Parcelable onSaveInstanceState() {
79 CellDataState state = new CellDataState(super.onSaveInstanceState());
80 state.mChecked = mChecked;
81 state.mMultiSimDialog = mMultiSimDialog;
82 state.mSubId = mSubId;
83 return state;
84 }
85
86 @Override
87 protected void onAttachedToActivity() {
88 super.onAttachedToActivity();
89 mListener.setListener(true, mSubId, getContext());
90 }
91
92 @Override
93 protected void onPrepareForRemoval() {
94 mListener.setListener(false, mSubId, getContext());
95 super.onPrepareForRemoval();
96 }
97
98 /**
99 * Initialize this preference with subId.
100 */
101 public void initialize(int subId) {
102 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
103 throw new IllegalArgumentException("MobileDataPreference needs a SubscriptionInfo");
104 }
105 mSubscriptionManager = SubscriptionManager.from(getContext());
106 mTelephonyManager = TelephonyManager.from(getContext());
107 if (mSubId != subId) {
108 mSubId = subId;
109 setKey(getKey() + subId);
110 }
111 updateChecked();
112 }
113
114 private void updateChecked() {
115 setChecked(mTelephonyManager.getDataEnabled(mSubId));
116 }
117
118 @Override
119 public void performClick(PreferenceScreen preferenceScreen) {
120 final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(
121 mSubId);
122 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
123 boolean isMultiSim = (mTelephonyManager.getSimCount() > 1);
124 if (mChecked) {
125 // If the device is single SIM or is enabling data on the active data SIM then forgo
126 // the pop-up.
127 if (isMultiSim || (nextSir != null && currentSir != null
128 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
129 setMobileDataEnabled(false);
130 if (nextSir != null && currentSir != null
131 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
132 disableDataForOtherSubscriptions(mSubId);
133 }
134 return;
135 }
136 // disabling data; show confirmation dialog which eventually
137 // calls setMobileDataEnabled() once user confirms.
138 mMultiSimDialog = false;
139 super.performClick(preferenceScreen);
140 } else {
141 // If we are showing the Sim Card tile then we are a Multi-Sim device.
142 if (isMultiSim) {
143 mMultiSimDialog = true;
144 if (nextSir != null && currentSir != null
145 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
146 setMobileDataEnabled(true);
147 disableDataForOtherSubscriptions(mSubId);
148 return;
149 }
150 super.performClick(preferenceScreen);
151 } else {
152 setMobileDataEnabled(true);
153 }
154 }
155 }
156
157 private void setMobileDataEnabled(boolean enabled) {
158 if (DBG) Log.d(TAG, "setMobileDataEnabled(" + enabled + "," + mSubId + ")");
Malcolm Chen66c618e2017-07-24 16:44:32 -0700159
160 MetricsLogger.action(getContext(), MetricsEvent.ACTION_MOBILE_NETWORK_MOBILE_DATA_TOGGLE,
161 enabled);
162
Malcolm Chen9ef63c62017-06-20 16:53:01 -0700163 mTelephonyManager.setDataEnabled(mSubId, enabled);
164 setChecked(enabled);
165 }
166
167 private void setChecked(boolean checked) {
168 if (mChecked == checked) return;
169 mChecked = checked;
170 notifyChanged();
171 }
172
173 @Override
174 protected void onBindView(View view) {
175 super.onBindView(view);
176 View checkableView = view.findViewById(com.android.internal.R.id.switch_widget);
177 checkableView.setClickable(false);
178 ((Checkable) checkableView).setChecked(mChecked);
179 }
180
181 @Override
182 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
183 if (mMultiSimDialog) {
184 showMultiSimDialog(builder);
185 } else {
186 showDisableDialog(builder);
187 }
188 }
189
190 private void showDisableDialog(AlertDialog.Builder builder) {
191 builder.setTitle(null)
192 .setMessage(R.string.data_usage_disable_mobile)
193 .setPositiveButton(android.R.string.ok, this)
194 .setNegativeButton(android.R.string.cancel, null);
195 }
196
197 private void showMultiSimDialog(AlertDialog.Builder builder) {
198 final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
199 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
200
201 final String previousName = (nextSir == null)
202 ? getContext().getResources().getString(R.string.sim_selection_required_pref)
203 : nextSir.getDisplayName().toString();
204
205 builder.setTitle(R.string.sim_change_data_title);
206 builder.setMessage(getContext().getString(R.string.sim_change_data_message,
207 String.valueOf(currentSir != null ? currentSir.getDisplayName() : null),
208 previousName));
209
210 builder.setPositiveButton(R.string.ok, this);
211 builder.setNegativeButton(R.string.cancel, null);
212 }
213
214 private void disableDataForOtherSubscriptions(int subId) {
215 List<SubscriptionInfo> subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
216 if (subInfoList != null) {
217 for (SubscriptionInfo subInfo : subInfoList) {
218 if (subInfo.getSubscriptionId() != subId) {
219 mTelephonyManager.setDataEnabled(subInfo.getSubscriptionId(), false);
220 }
221 }
222 }
223 }
224
225 @Override
226 public void onClick(DialogInterface dialog, int which) {
227 if (which != DialogInterface.BUTTON_POSITIVE) {
228 return;
229 }
230 if (mMultiSimDialog) {
231 mSubscriptionManager.setDefaultDataSubId(mSubId);
232 setMobileDataEnabled(true);
233 disableDataForOtherSubscriptions(mSubId);
234 } else {
235 // TODO: extend to modify policy enabled flag.
236 setMobileDataEnabled(false);
237 }
238 }
239
240 private final DataStateListener mListener = new DataStateListener() {
241 @Override
242 public void onChange(boolean selfChange) {
243 updateChecked();
244 }
245 };
246
247 /**
248 * Listener that listens mobile data state change.
249 */
250 public abstract static class DataStateListener extends ContentObserver {
251 public DataStateListener() {
252 super(new Handler(Looper.getMainLooper()));
253 }
254
255 /**
256 * Set / Unset data state listening, specifying subId.
257 */
258 public void setListener(boolean listening, int subId, Context context) {
259 if (listening) {
260 Uri uri = Global.getUriFor(Global.MOBILE_DATA);
261 if (TelephonyManager.getDefault().getSimCount() != 1) {
262 uri = Global.getUriFor(Global.MOBILE_DATA + subId);
263 }
264 context.getContentResolver().registerContentObserver(uri, false, this);
265 } else {
266 context.getContentResolver().unregisterContentObserver(this);
267 }
268 }
269 }
270
271 /**
272 * Class that represents state of mobile data state.
273 * Used by onSaveInstanceState and onRestoreInstanceState.
274 */
275 public static class CellDataState extends BaseSavedState {
276 public int mSubId;
277 public boolean mChecked;
278 public boolean mMultiSimDialog;
279
280 public CellDataState(Parcelable base) {
281 super(base);
282 }
283
284 public CellDataState(Parcel source) {
285 super(source);
286 mChecked = source.readByte() != 0;
287 mMultiSimDialog = source.readByte() != 0;
288 mSubId = source.readInt();
289 }
290
291 @Override
292 public void writeToParcel(Parcel dest, int flags) {
293 super.writeToParcel(dest, flags);
294 dest.writeByte((byte) (mChecked ? 1 : 0));
295 dest.writeByte((byte) (mMultiSimDialog ? 1 : 0));
296 dest.writeInt(mSubId);
297 }
298
299 public static final Creator<CellDataState> CREATOR = new Creator<CellDataState>() {
300 @Override
301 public CellDataState createFromParcel(Parcel source) {
302 return new CellDataState(source);
303 }
304
305 @Override
306 public CellDataState[] newArray(int size) {
307 return new CellDataState[size];
308 }
309 };
310 }
311}