blob: e1d052845bb053c735d488af8f315bd3936b0ccb [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
39import java.util.List;
40
41/**
42 * Customized Preference to enable / disable mobile data.
43 * Basically copy of with com.android.settings.CellDataPreference.
44 */
45public class MobileDataPreference extends DialogPreference {
46
47 private static final boolean DBG = false;
48 private static final String TAG = "MobileDataPreference";
49
50 public int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
51 public boolean mChecked;
52 public boolean mMultiSimDialog;
53 private TelephonyManager mTelephonyManager;
54 private SubscriptionManager mSubscriptionManager;
55
56 public MobileDataPreference(Context context, AttributeSet attrs) {
57 super(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
58 }
59
60 @Override
61 protected void onRestoreInstanceState(Parcelable s) {
62 CellDataState state = (CellDataState) s;
63 super.onRestoreInstanceState(state.getSuperState());
64 mTelephonyManager = TelephonyManager.from(getContext());
65 mChecked = state.mChecked;
66 mMultiSimDialog = state.mMultiSimDialog;
67 if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
68 mSubId = state.mSubId;
69 setKey(getKey() + mSubId);
70 }
71 notifyChanged();
72 }
73
74 @Override
75 protected Parcelable onSaveInstanceState() {
76 CellDataState state = new CellDataState(super.onSaveInstanceState());
77 state.mChecked = mChecked;
78 state.mMultiSimDialog = mMultiSimDialog;
79 state.mSubId = mSubId;
80 return state;
81 }
82
83 @Override
84 protected void onAttachedToActivity() {
85 super.onAttachedToActivity();
86 mListener.setListener(true, mSubId, getContext());
87 }
88
89 @Override
90 protected void onPrepareForRemoval() {
91 mListener.setListener(false, mSubId, getContext());
92 super.onPrepareForRemoval();
93 }
94
95 /**
96 * Initialize this preference with subId.
97 */
98 public void initialize(int subId) {
99 if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
100 throw new IllegalArgumentException("MobileDataPreference needs a SubscriptionInfo");
101 }
102 mSubscriptionManager = SubscriptionManager.from(getContext());
103 mTelephonyManager = TelephonyManager.from(getContext());
104 if (mSubId != subId) {
105 mSubId = subId;
106 setKey(getKey() + subId);
107 }
108 updateChecked();
109 }
110
111 private void updateChecked() {
112 setChecked(mTelephonyManager.getDataEnabled(mSubId));
113 }
114
115 @Override
116 public void performClick(PreferenceScreen preferenceScreen) {
117 final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(
118 mSubId);
119 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
120 boolean isMultiSim = (mTelephonyManager.getSimCount() > 1);
121 if (mChecked) {
122 // If the device is single SIM or is enabling data on the active data SIM then forgo
123 // the pop-up.
124 if (isMultiSim || (nextSir != null && currentSir != null
125 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
126 setMobileDataEnabled(false);
127 if (nextSir != null && currentSir != null
128 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
129 disableDataForOtherSubscriptions(mSubId);
130 }
131 return;
132 }
133 // disabling data; show confirmation dialog which eventually
134 // calls setMobileDataEnabled() once user confirms.
135 mMultiSimDialog = false;
136 super.performClick(preferenceScreen);
137 } else {
138 // If we are showing the Sim Card tile then we are a Multi-Sim device.
139 if (isMultiSim) {
140 mMultiSimDialog = true;
141 if (nextSir != null && currentSir != null
142 && currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
143 setMobileDataEnabled(true);
144 disableDataForOtherSubscriptions(mSubId);
145 return;
146 }
147 super.performClick(preferenceScreen);
148 } else {
149 setMobileDataEnabled(true);
150 }
151 }
152 }
153
154 private void setMobileDataEnabled(boolean enabled) {
155 if (DBG) Log.d(TAG, "setMobileDataEnabled(" + enabled + "," + mSubId + ")");
156 mTelephonyManager.setDataEnabled(mSubId, enabled);
157 setChecked(enabled);
158 }
159
160 private void setChecked(boolean checked) {
161 if (mChecked == checked) return;
162 mChecked = checked;
163 notifyChanged();
164 }
165
166 @Override
167 protected void onBindView(View view) {
168 super.onBindView(view);
169 View checkableView = view.findViewById(com.android.internal.R.id.switch_widget);
170 checkableView.setClickable(false);
171 ((Checkable) checkableView).setChecked(mChecked);
172 }
173
174 @Override
175 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
176 if (mMultiSimDialog) {
177 showMultiSimDialog(builder);
178 } else {
179 showDisableDialog(builder);
180 }
181 }
182
183 private void showDisableDialog(AlertDialog.Builder builder) {
184 builder.setTitle(null)
185 .setMessage(R.string.data_usage_disable_mobile)
186 .setPositiveButton(android.R.string.ok, this)
187 .setNegativeButton(android.R.string.cancel, null);
188 }
189
190 private void showMultiSimDialog(AlertDialog.Builder builder) {
191 final SubscriptionInfo currentSir = mSubscriptionManager.getActiveSubscriptionInfo(mSubId);
192 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
193
194 final String previousName = (nextSir == null)
195 ? getContext().getResources().getString(R.string.sim_selection_required_pref)
196 : nextSir.getDisplayName().toString();
197
198 builder.setTitle(R.string.sim_change_data_title);
199 builder.setMessage(getContext().getString(R.string.sim_change_data_message,
200 String.valueOf(currentSir != null ? currentSir.getDisplayName() : null),
201 previousName));
202
203 builder.setPositiveButton(R.string.ok, this);
204 builder.setNegativeButton(R.string.cancel, null);
205 }
206
207 private void disableDataForOtherSubscriptions(int subId) {
208 List<SubscriptionInfo> subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
209 if (subInfoList != null) {
210 for (SubscriptionInfo subInfo : subInfoList) {
211 if (subInfo.getSubscriptionId() != subId) {
212 mTelephonyManager.setDataEnabled(subInfo.getSubscriptionId(), false);
213 }
214 }
215 }
216 }
217
218 @Override
219 public void onClick(DialogInterface dialog, int which) {
220 if (which != DialogInterface.BUTTON_POSITIVE) {
221 return;
222 }
223 if (mMultiSimDialog) {
224 mSubscriptionManager.setDefaultDataSubId(mSubId);
225 setMobileDataEnabled(true);
226 disableDataForOtherSubscriptions(mSubId);
227 } else {
228 // TODO: extend to modify policy enabled flag.
229 setMobileDataEnabled(false);
230 }
231 }
232
233 private final DataStateListener mListener = new DataStateListener() {
234 @Override
235 public void onChange(boolean selfChange) {
236 updateChecked();
237 }
238 };
239
240 /**
241 * Listener that listens mobile data state change.
242 */
243 public abstract static class DataStateListener extends ContentObserver {
244 public DataStateListener() {
245 super(new Handler(Looper.getMainLooper()));
246 }
247
248 /**
249 * Set / Unset data state listening, specifying subId.
250 */
251 public void setListener(boolean listening, int subId, Context context) {
252 if (listening) {
253 Uri uri = Global.getUriFor(Global.MOBILE_DATA);
254 if (TelephonyManager.getDefault().getSimCount() != 1) {
255 uri = Global.getUriFor(Global.MOBILE_DATA + subId);
256 }
257 context.getContentResolver().registerContentObserver(uri, false, this);
258 } else {
259 context.getContentResolver().unregisterContentObserver(this);
260 }
261 }
262 }
263
264 /**
265 * Class that represents state of mobile data state.
266 * Used by onSaveInstanceState and onRestoreInstanceState.
267 */
268 public static class CellDataState extends BaseSavedState {
269 public int mSubId;
270 public boolean mChecked;
271 public boolean mMultiSimDialog;
272
273 public CellDataState(Parcelable base) {
274 super(base);
275 }
276
277 public CellDataState(Parcel source) {
278 super(source);
279 mChecked = source.readByte() != 0;
280 mMultiSimDialog = source.readByte() != 0;
281 mSubId = source.readInt();
282 }
283
284 @Override
285 public void writeToParcel(Parcel dest, int flags) {
286 super.writeToParcel(dest, flags);
287 dest.writeByte((byte) (mChecked ? 1 : 0));
288 dest.writeByte((byte) (mMultiSimDialog ? 1 : 0));
289 dest.writeInt(mSubId);
290 }
291
292 public static final Creator<CellDataState> CREATOR = new Creator<CellDataState>() {
293 @Override
294 public CellDataState createFromParcel(Parcel source) {
295 return new CellDataState(source);
296 }
297
298 @Override
299 public CellDataState[] newArray(int size) {
300 return new CellDataState[size];
301 }
302 };
303 }
304}