• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 
17 package com.android.phone;
18 
19 import android.app.ProgressDialog;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Handler;
23 import android.os.Message;
24 import android.preference.Preference;
25 import android.preference.PreferenceCategory;
26 import android.preference.TwoStatePreference;
27 import android.telephony.ServiceState;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.TelephonyManager;
30 import android.util.AttributeSet;
31 import android.util.Log;
32 import android.widget.Toast;
33 
34 import com.android.internal.logging.MetricsLogger;
35 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
36 import com.android.settingslib.utils.ThreadUtils;
37 
38 /**
39  * "Networks" settings UI for the Phone app.
40  */
41 public class NetworkOperators extends PreferenceCategory
42         implements Preference.OnPreferenceChangeListener {
43 
44     private static final String LOG_TAG = "NetworkOperators";
45     private static final boolean DBG = true;
46 
47     private static final int EVENT_AUTO_SELECT_DONE = 100;
48     private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 200;
49 
50     //String keys for preference lookup
51     public static final String BUTTON_NETWORK_SELECT_KEY = "button_network_select_key";
52     public static final String BUTTON_AUTO_SELECT_KEY = "button_auto_select_key";
53     public static final String BUTTON_CHOOSE_NETWORK_KEY = "button_choose_network_key";
54     public static final String CATEGORY_NETWORK_OPERATORS_KEY = "network_operators_category_key";
55 
56     //preference objects
57     private NetworkSelectListPreference mNetworkSelect;
58     private TwoStatePreference mAutoSelect;
59     private Preference mChooseNetwork;
60     private ProgressDialog mProgressDialog;
61 
62     private int mSubId;
63     private TelephonyManager mTelephonyManager;
64 
65     // There's two sets of Auto-Select UI in this class.
66     // If {@code com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI} set as true
67     // {@link mChooseNetwork} will be used, otherwise {@link mNetworkSelect} will be used.
68     boolean mEnableNewManualSelectNetworkUI;
69 
NetworkOperators(Context context, AttributeSet attrs)70     public NetworkOperators(Context context, AttributeSet attrs) {
71         super(context, attrs);
72     }
73 
NetworkOperators(Context context)74     public NetworkOperators(Context context) {
75         super(context);
76     }
77 
78     /**
79      * Initialize NetworkOperators instance.
80      */
initialize()81     public void initialize() {
82         mEnableNewManualSelectNetworkUI = getContext().getResources().getBoolean(
83                 com.android.internal.R.bool.config_enableNewAutoSelectNetworkUI);
84         mAutoSelect = (TwoStatePreference) findPreference(BUTTON_AUTO_SELECT_KEY);
85         mChooseNetwork = findPreference(BUTTON_CHOOSE_NETWORK_KEY);
86         mNetworkSelect = (NetworkSelectListPreference) findPreference(BUTTON_NETWORK_SELECT_KEY);
87         if (mEnableNewManualSelectNetworkUI) {
88             removePreference(mNetworkSelect);
89         } else {
90             removePreference(mChooseNetwork);
91         }
92         mProgressDialog = new ProgressDialog(getContext());
93         mTelephonyManager = TelephonyManager.from(getContext());
94     }
95 
96     /**
97      * Update NetworkOperators instance if like subId is updated.
98      *
99      * @param subId Corresponding subscription ID of this network.
100      */
update(final int subId)101     protected void update(final int subId) {
102         mSubId = subId;
103         mTelephonyManager = TelephonyManager.from(getContext()).createForSubscriptionId(mSubId);
104 
105         if (mAutoSelect != null) {
106             mAutoSelect.setOnPreferenceChangeListener(this);
107         }
108 
109         if (mEnableNewManualSelectNetworkUI) {
110             if (mChooseNetwork != null) {
111                 ServiceState ss = mTelephonyManager.getServiceState();
112                 if (ss != null && ss.getState() == ServiceState.STATE_IN_SERVICE) {
113                     mChooseNetwork.setSummary(mTelephonyManager.getNetworkOperatorName());
114                 } else {
115                     mChooseNetwork.setSummary(R.string.network_disconnected);
116                 }
117             }
118         } else {
119             if (mNetworkSelect != null) {
120                 mNetworkSelect.initialize(mSubId, this, mProgressDialog);
121             }
122         }
123         getNetworkSelectionMode();
124     }
125 
126     /**
127      * Implemented to support onPreferenceChangeListener to look for preference
128      * changes specifically on auto select button.
129      *
130      * @param preference is the preference to be changed, should be auto select button.
131      * @param newValue   should be the value of whether autoSelect is checked.
132      */
133     @Override
onPreferenceChange(Preference preference, Object newValue)134     public boolean onPreferenceChange(Preference preference, Object newValue) {
135         if (preference == mAutoSelect) {
136             boolean autoSelect = (Boolean) newValue;
137             if (DBG) logd("onPreferenceChange autoSelect: " + String.valueOf(autoSelect));
138             selectNetworkAutomatic(autoSelect);
139             MetricsLogger.action(getContext(),
140                     MetricsEvent.ACTION_MOBILE_NETWORK_AUTO_SELECT_NETWORK_TOGGLE, autoSelect);
141             return true;
142         }
143         return false;
144     }
145 
146     private final Handler mHandler = new Handler() {
147         @Override
148         public void handleMessage(Message msg) {
149             switch (msg.what) {
150                 case EVENT_AUTO_SELECT_DONE:
151                     mAutoSelect.setEnabled(true);
152                     dismissProgressBar();
153 
154                     boolean isSuccessed = (boolean) msg.obj;
155 
156                     if (isSuccessed) {
157                         if (DBG) logd("automatic network selection: succeeded!");
158                         displayNetworkSelectionSucceeded();
159                     } else {
160                         if (DBG) logd("automatic network selection: failed!");
161                         displayNetworkSelectionFailed();
162                     }
163 
164                     break;
165                 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
166                     int networkSelectionMode = msg.arg1;
167                     if (networkSelectionMode == TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN) {
168                         if (DBG) logd("get network selection mode: failed!");
169                     } else {
170                         boolean autoSelect = networkSelectionMode
171                                 == TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
172                         if (DBG) {
173                             logd("get network selection mode: "
174                                     + (autoSelect ? "auto" : "manual") + " selection");
175                         }
176                         if (mAutoSelect != null) {
177                             mAutoSelect.setChecked(autoSelect);
178                         }
179                         if (mEnableNewManualSelectNetworkUI) {
180                             if (mChooseNetwork != null) {
181                                 mChooseNetwork.setEnabled(!autoSelect);
182                             }
183                         } else {
184                             if (mNetworkSelect != null) {
185                                 mNetworkSelect.setEnabled(!autoSelect);
186                             }
187                         }
188                     }
189             }
190             return;
191         }
192     };
193 
194     // Used by both mAutoSelect and mNetworkSelect buttons.
displayNetworkSelectionFailed()195     protected void displayNetworkSelectionFailed() {
196         Toast.makeText(getContext(), R.string.connect_later, Toast.LENGTH_LONG).show();
197     }
198 
199     // Used by both mAutoSelect and mNetworkSelect buttons.
displayNetworkSelectionSucceeded()200     protected void displayNetworkSelectionSucceeded() {
201         Toast.makeText(getContext(), R.string.registration_done, Toast.LENGTH_LONG).show();
202     }
203 
selectNetworkAutomatic(boolean autoSelect)204     private void selectNetworkAutomatic(boolean autoSelect) {
205         if (DBG) logd("selectNetworkAutomatic: " + String.valueOf(autoSelect));
206 
207         if (autoSelect) {
208             if (mEnableNewManualSelectNetworkUI) {
209                 if (mChooseNetwork != null) {
210                     mChooseNetwork.setEnabled(!autoSelect);
211                 }
212             } else {
213                 if (mNetworkSelect != null) {
214                     mNetworkSelect.setEnabled(!autoSelect);
215                 }
216             }
217             if (DBG) logd("select network automatically...");
218             showAutoSelectProgressBar();
219             mAutoSelect.setEnabled(false);
220             if (SubscriptionManager.isValidSubscriptionId(mSubId)) {
221                 ThreadUtils.postOnBackgroundThread(() -> {
222                     mTelephonyManager.setNetworkSelectionModeAutomatic();
223                     // Because TelephonyManager#setNetworkSelectionModeAutomatic doesn't have a
224                     // return value, we query the current network selection mode to tell if the
225                     // TelephonyManager#setNetworkSelectionModeAutomatic is successed.
226                     int networkSelectionMode = mTelephonyManager.getNetworkSelectionMode();
227                     Message msg = mHandler.obtainMessage(EVENT_AUTO_SELECT_DONE);
228                     msg.obj = networkSelectionMode == TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
229                     msg.sendToTarget();
230                 });
231             }
232         } else {
233             if (mEnableNewManualSelectNetworkUI) {
234                 if (mChooseNetwork != null) {
235                     // Open the choose Network page automatically when user turn off the auto-select
236                     openChooseNetworkPage();
237                 }
238             } else {
239                 if (mNetworkSelect != null) {
240                     mNetworkSelect.onClick();
241                 }
242             }
243         }
244     }
245 
getNetworkSelectionMode()246     protected void getNetworkSelectionMode() {
247         if (DBG) logd("getting network selection mode...");
248         ThreadUtils.postOnBackgroundThread(() -> {
249             int networkSelectionMode = mTelephonyManager.getNetworkSelectionMode();
250             Message msg = mHandler.obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE);
251             msg.arg1 = networkSelectionMode;
252             msg.sendToTarget();
253         });
254     }
255 
dismissProgressBar()256     private void dismissProgressBar() {
257         if (mProgressDialog != null && mProgressDialog.isShowing()) {
258             mProgressDialog.dismiss();
259         }
260     }
261 
showAutoSelectProgressBar()262     private void showAutoSelectProgressBar() {
263         mProgressDialog.setMessage(
264                 getContext().getResources().getString(R.string.register_automatically));
265         mProgressDialog.setCanceledOnTouchOutside(false);
266         mProgressDialog.setCancelable(false);
267         mProgressDialog.setIndeterminate(true);
268         mProgressDialog.show();
269     }
270 
271     /**
272      * Open the Choose network page via {@alink NetworkSelectSettingActivity}
273      */
openChooseNetworkPage()274     public void openChooseNetworkPage() {
275         Intent intent = NetworkSelectSettingActivity.getIntent(getContext(), mSubId);
276         getContext().startActivity(intent);
277     }
278 
preferenceTreeClick(Preference preference)279     protected boolean preferenceTreeClick(Preference preference) {
280         if (mEnableNewManualSelectNetworkUI) {
281             if (DBG) logd("enable New AutoSelectNetwork UI");
282             if (preference == mChooseNetwork) {
283                 openChooseNetworkPage();
284             }
285             return (preference == mAutoSelect || preference == mChooseNetwork);
286         } else {
287             return (preference == mAutoSelect || preference == mNetworkSelect);
288         }
289     }
290 
logd(String msg)291     private void logd(String msg) {
292         Log.d(LOG_TAG, "[NetworksList] " + msg);
293     }
294 
loge(String msg)295     private void loge(String msg) {
296         Log.e(LOG_TAG, "[NetworksList] " + msg);
297     }
298 }
299