blob: 5cafa8a3a637b4661b6db2d52ed5cc26f51c57a6 [file] [log] [blame]
Aida Takeshi7c3b4a32016-08-11 13:42:24 +08001/*
2 * Copyright (C) 2018 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.ActionBar;
20import android.app.Dialog;
Brad Ebingeraae14a72018-08-29 16:15:00 -070021import android.content.Context;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080022import android.os.AsyncResult;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.Message;
Brad Ebingeraae14a72018-08-29 16:15:00 -070026import android.os.PersistableBundle;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080027import android.preference.Preference;
28import android.preference.PreferenceScreen;
Brad Ebingeraae14a72018-08-29 16:15:00 -070029import android.telephony.CarrierConfigManager;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080030import android.telephony.SubscriptionManager;
31import android.telephony.TelephonyManager;
32import android.util.Log;
33import android.view.MenuItem;
34import android.widget.Toast;
35
36import com.android.internal.telephony.CommandException;
37import com.android.internal.telephony.CommandsInterface;
38import com.android.internal.telephony.GsmCdmaPhone;
39import com.android.internal.telephony.Phone;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080040import com.android.phone.settings.fdn.EditPinPreference;
41
42import java.util.ArrayList;
43
44/**
45 * Implements the preference to enable/disable calling barring options and
46 * the dialogs to change the passward.
47 */
48public class GsmUmtsCallBarringOptions extends TimeConsumingPreferenceActivity
49 implements EditPinPreference.OnPinEnteredListener {
50 private static final String LOG_TAG = "GsmUmtsCallBarringOptions";
51 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
52
53 // String keys for preference lookup
54 // Preference is handled solely in xml.
55 // Block all outgoing calls
56 private static final String BUTTON_BAOC_KEY = "button_baoc_key";
57 // Block all outgoing international calls
58 private static final String BUTTON_BAOIC_KEY = "button_baoic_key";
59 // Block all outgoing international roaming calls
60 private static final String BUTTON_BAOICxH_KEY = "button_baoicxh_key";
61 // Block all incoming calls
62 private static final String BUTTON_BAIC_KEY = "button_baic_key";
63 // Block all incoming international roaming calls
64 private static final String BUTTON_BAICr_KEY = "button_baicr_key";
65 // Disable all barring
66 private static final String BUTTON_BA_ALL_KEY = "button_ba_all_key";
67 // Change passward
68 private static final String BUTTON_BA_CHANGE_PW_KEY = "button_change_pw_key";
69
70 private static final String PW_CHANGE_STATE_KEY = "pin_change_state_key";
71 private static final String OLD_PW_KEY = "old_pw_key";
72 private static final String NEW_PW_KEY = "new_pw_key";
73 private static final String DIALOG_MESSAGE_KEY = "dialog_message_key";
74 private static final String DIALOG_PW_ENTRY_KEY = "dialog_pw_enter_key";
75 private static final String KEY_STATUS = "toggle";
76 private static final String PREFERENCE_ENABLED_KEY = "PREFERENCE_ENABLED";
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080077 private static final String SAVED_BEFORE_LOAD_COMPLETED_KEY = "PROGRESS_SHOWING";
78
79 private CallBarringEditPreference mButtonBAOC;
80 private CallBarringEditPreference mButtonBAOIC;
81 private CallBarringEditPreference mButtonBAOICxH;
82 private CallBarringEditPreference mButtonBAIC;
83 private CallBarringEditPreference mButtonBAICr;
84 private CallBarringDeselectAllPreference mButtonDisableAll;
85 private EditPinPreference mButtonChangePW;
86
87 // State variables
88 private int mPwChangeState;
89 private String mOldPassword;
90 private String mNewPassword;
91 private int mPwChangeDialogStrId;
92
93 private static final int PW_CHANGE_OLD = 0;
94 private static final int PW_CHANGE_NEW = 1;
95 private static final int PW_CHANGE_REENTER = 2;
96
97 private static final int BUSY_READING_DIALOG = 100;
98 private static final int BUSY_SAVING_DIALOG = 200;
99
100 // Password change complete event
101 private static final int EVENT_PW_CHANGE_COMPLETE = 100;
102 // Disable all complete event
103 private static final int EVENT_DISABLE_ALL_COMPLETE = 200;
104
105 private static final int PW_LENGTH = 4;
106
107 private Phone mPhone;
108 private ArrayList<CallBarringEditPreference> mPreferences =
109 new ArrayList<CallBarringEditPreference>();
110 private int mInitIndex = 0;
111 private boolean mFirstResume;
112 private Bundle mIcicle;
113
114 private SubscriptionInfoHelper mSubscriptionInfoHelper;
115 private Dialog mProgressDialog;
116
117 @Override
118 public void onPinEntered(EditPinPreference preference, boolean positiveResult) {
119 if (preference == mButtonChangePW) {
120 updatePWChangeState(positiveResult);
121 } else if (preference == mButtonDisableAll) {
122 disableAllBarring(positiveResult);
123 }
124 }
125
126 /**
127 * Display a toast for message.
128 */
129 private void displayMessage(int strId) {
130 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
131 }
132
133 /**
134 * Attempt to disable all for call barring settings.
135 */
136 private void disableAllBarring(boolean positiveResult) {
137 if (!positiveResult) {
138 // Return on cancel
139 return;
140 }
141
Suresh Koletib74bac72019-12-16 18:11:40 +0530142 String password = mButtonDisableAll.getText();
143 // Validate the length of password first, before submitting it to the
144 // RIL for CB disable.
145 if (!validatePassword(password)) {
146 mButtonDisableAll.setText("");
147 displayMessage(R.string.call_barring_right_pwd_number);
148 return;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800149 }
150
151 // Submit the disable all request
152 mButtonDisableAll.setText("");
153 Message onComplete = mHandler.obtainMessage(EVENT_DISABLE_ALL_COMPLETE);
154 mPhone.setCallBarring(CommandsInterface.CB_FACILITY_BA_ALL, false, password, onComplete, 0);
155 this.onStarted(mButtonDisableAll, false);
156 }
157
158 /**
159 * Attempt to change the password for call barring settings.
160 */
161 private void updatePWChangeState(boolean positiveResult) {
162 if (!positiveResult) {
163 // Reset the state on cancel
164 resetPwChangeState();
165 return;
166 }
167
168 // Progress through the dialog states, generally in this order:
169 // 1. Enter old password
170 // 2. Enter new password
171 // 3. Re-Enter new password
172 // In general, if any invalid entries are made, the dialog re-
173 // appears with text to indicate what the issue is.
174 switch (mPwChangeState) {
175 case PW_CHANGE_OLD:
176 mOldPassword = mButtonChangePW.getText();
177 mButtonChangePW.setText("");
178 if (validatePassword(mOldPassword)) {
179 mPwChangeState = PW_CHANGE_NEW;
180 displayPwChangeDialog();
181 } else {
182 displayPwChangeDialog(R.string.call_barring_right_pwd_number, true);
183 }
184 break;
185 case PW_CHANGE_NEW:
186 mNewPassword = mButtonChangePW.getText();
187 mButtonChangePW.setText("");
188 if (validatePassword(mNewPassword)) {
189 mPwChangeState = PW_CHANGE_REENTER;
190 displayPwChangeDialog();
191 } else {
192 displayPwChangeDialog(R.string.call_barring_right_pwd_number, true);
193 }
194 break;
195 case PW_CHANGE_REENTER:
196 // If the re-entered password is not valid, display a message
197 // and reset the state.
198 if (!mNewPassword.equals(mButtonChangePW.getText())) {
199 mPwChangeState = PW_CHANGE_NEW;
200 mButtonChangePW.setText("");
201 displayPwChangeDialog(R.string.call_barring_pwd_not_match, true);
202 } else {
203 // If the password is valid, then submit the change password request
204 mButtonChangePW.setText("");
205 Message onComplete = mHandler.obtainMessage(EVENT_PW_CHANGE_COMPLETE);
206 ((GsmCdmaPhone) mPhone).changeCallBarringPassword(
207 CommandsInterface.CB_FACILITY_BA_ALL,
208 mOldPassword, mNewPassword, onComplete);
209 this.onStarted(mButtonChangePW, false);
210 }
211 break;
212 default:
213 if (DBG) {
214 Log.d(LOG_TAG, "updatePWChangeState: Unknown password change state: "
215 + mPwChangeState);
216 }
217 break;
218 }
219 }
220
221 /**
222 * Handler for asynchronous replies from the framework layer.
223 */
224 private Handler mHandler = new Handler() {
225 @Override
226 public void handleMessage(Message msg) {
227 AsyncResult ar = (AsyncResult) msg.obj;
228 switch (msg.what) {
229 // Handle the response message for password change from the framework layer.
230 case EVENT_PW_CHANGE_COMPLETE: {
231 onFinished(mButtonChangePW, false);
232 // Unsuccessful change, display a toast to user with failure reason.
233 if (ar.exception != null) {
234 if (DBG) {
235 Log.d(LOG_TAG,
236 "change password for call barring failed with exception: "
237 + ar.exception);
238 }
Aishwarya Mallampati1c436d72022-04-11 20:21:23 +0000239 CommandException commandException = (CommandException) ar.exception;
240 onException(mButtonChangePW, commandException);
241 if (commandException.getCommandError()
242 != CommandException.Error.FDN_CHECK_FAILURE) {
243 // Not a FDN_CHECK_FAILURE, enable mButtonChangePW
244 mButtonChangePW.setEnabled(true);
245 }
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800246 } else if (ar.userObj instanceof Throwable) {
247 onError(mButtonChangePW, RESPONSE_ERROR);
248 } else {
249 // Successful change.
250 displayMessage(R.string.call_barring_change_pwd_success);
251 }
252 resetPwChangeState();
253 break;
254 }
255 // When disabling all call barring, either fail and display a toast,
256 // or just update the UI.
257 case EVENT_DISABLE_ALL_COMPLETE: {
258 onFinished(mButtonDisableAll, false);
259 if (ar.exception != null) {
260 if (DBG) {
261 Log.d(LOG_TAG, "can not disable all call barring with exception: "
262 + ar.exception);
263 }
264 onException(mButtonDisableAll, (CommandException) ar.exception);
265 mButtonDisableAll.setEnabled(true);
266 } else if (ar.userObj instanceof Throwable) {
267 onError(mButtonDisableAll, RESPONSE_ERROR);
268 } else {
269 // Reset to normal behaviour on successful change.
270 displayMessage(R.string.call_barring_deactivate_success);
271 resetCallBarringPrefState(false);
272 }
273 break;
274 }
275 default: {
276 if (DBG) {
277 Log.d(LOG_TAG, "Unknown message id: " + msg.what);
278 }
279 break;
280 }
281 }
282 }
283 };
284
285 /**
286 * The next two functions are for updating the message field on the dialog.
287 */
288 private void displayPwChangeDialog() {
289 displayPwChangeDialog(0, true);
290 }
291
292 private void displayPwChangeDialog(int strId, boolean shouldDisplay) {
293 int msgId = 0;
294 switch (mPwChangeState) {
295 case PW_CHANGE_OLD:
296 msgId = R.string.call_barring_old_pwd;
297 break;
298 case PW_CHANGE_NEW:
299 msgId = R.string.call_barring_new_pwd;
300 break;
301 case PW_CHANGE_REENTER:
302 msgId = R.string.call_barring_confirm_pwd;
303 break;
304 default:
305 break;
306 }
307
308 // Append the note/additional message, if needed.
309 if (strId != 0) {
310 mButtonChangePW.setDialogMessage(getText(msgId) + "\n" + getText(strId));
311 } else {
312 mButtonChangePW.setDialogMessage(msgId);
313 }
314
315 // Only display if requested.
316 if (shouldDisplay) {
317 mButtonChangePW.showPinDialog();
318 }
319 mPwChangeDialogStrId = strId;
320 }
321
322 /**
323 * Reset the state of the password change dialog.
324 */
325 private void resetPwChangeState() {
326 mPwChangeState = PW_CHANGE_OLD;
327 displayPwChangeDialog(0, false);
328 mOldPassword = "";
329 mNewPassword = "";
330 }
331
332 /**
333 * Reset the state of the all call barring setting to disable.
334 */
335 private void resetCallBarringPrefState(boolean enable) {
336 for (CallBarringEditPreference pref : mPreferences) {
337 pref.mIsActivated = enable;
338 pref.updateSummaryText();
339 }
340 }
341
342 /**
343 * Validate the password entry.
344 *
345 * @param password This is the password to validate
346 */
347 private boolean validatePassword(String password) {
348 return password != null && password.length() == PW_LENGTH;
349 }
350
351 @Override
352 protected void onCreate(Bundle icicle) {
353 super.onCreate(icicle);
Tyler Gunn662cb392025-01-17 23:34:46 +0000354
355 getWindow().addSystemFlags(
356 android.view.WindowManager.LayoutParams
357 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
358
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800359 if (DBG) {
360 Log.d(LOG_TAG, "onCreate, reading callbarring_options.xml file");
361 }
362 addPreferencesFromResource(R.xml.callbarring_options);
363
364 mSubscriptionInfoHelper = new SubscriptionInfoHelper(this, getIntent());
365 mPhone = mSubscriptionInfoHelper.getPhone();
366 if (DBG) {
367 Log.d(LOG_TAG, "onCreate, reading callbarring_options.xml file finished!");
368 }
369
Brad Ebingeraae14a72018-08-29 16:15:00 -0700370 CarrierConfigManager configManager = (CarrierConfigManager)
371 mPhone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
372 PersistableBundle carrierConfig;
373 if (mSubscriptionInfoHelper.hasSubId()) {
374 carrierConfig = configManager.getConfigForSubId(mSubscriptionInfoHelper.getSubId());
375 } else {
376 carrierConfig = configManager.getConfig();
377 }
378 boolean isPwChangeButtonVisible = true;
379 boolean isDisableAllButtonVisible = true;
380 if (carrierConfig != null) {
381 isPwChangeButtonVisible = carrierConfig.getBoolean(
382 CarrierConfigManager.KEY_CALL_BARRING_SUPPORTS_PASSWORD_CHANGE_BOOL, true);
383 isDisableAllButtonVisible = carrierConfig.getBoolean(
384 CarrierConfigManager.KEY_CALL_BARRING_SUPPORTS_DEACTIVATE_ALL_BOOL, true);
385 } else {
386 Log.w(LOG_TAG, "Couldn't access CarrierConfig bundle");
387 }
388
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800389 // Get UI object references
390 PreferenceScreen prefSet = getPreferenceScreen();
391 mButtonBAOC = (CallBarringEditPreference) prefSet.findPreference(BUTTON_BAOC_KEY);
392 mButtonBAOIC = (CallBarringEditPreference) prefSet.findPreference(BUTTON_BAOIC_KEY);
393 mButtonBAOICxH = (CallBarringEditPreference) prefSet.findPreference(BUTTON_BAOICxH_KEY);
394 mButtonBAIC = (CallBarringEditPreference) prefSet.findPreference(BUTTON_BAIC_KEY);
395 mButtonBAICr = (CallBarringEditPreference) prefSet.findPreference(BUTTON_BAICr_KEY);
396 mButtonDisableAll = (CallBarringDeselectAllPreference)
397 prefSet.findPreference(BUTTON_BA_ALL_KEY);
398 mButtonChangePW = (EditPinPreference) prefSet.findPreference(BUTTON_BA_CHANGE_PW_KEY);
399
Brad Ebingeraae14a72018-08-29 16:15:00 -0700400 // Some carriers do not use PW change and disable all buttons. Hide them if this is the
401 // case.
402 if (!isDisableAllButtonVisible) {
403 prefSet.removePreference(mButtonDisableAll);
404 }
405 if (!isPwChangeButtonVisible) {
406 prefSet.removePreference(mButtonChangePW);
407 }
408
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800409 // Assign click listener and update state
410 mButtonBAOC.setOnPinEnteredListener(this);
411 mButtonBAOIC.setOnPinEnteredListener(this);
412 mButtonBAOICxH.setOnPinEnteredListener(this);
413 mButtonBAIC.setOnPinEnteredListener(this);
414 mButtonBAICr.setOnPinEnteredListener(this);
415 mButtonDisableAll.setOnPinEnteredListener(this);
416 mButtonChangePW.setOnPinEnteredListener(this);
417
418 // Store CallBarringEditPreferencence objects in array list.
419 mPreferences.add(mButtonBAOC);
420 mPreferences.add(mButtonBAOIC);
421 mPreferences.add(mButtonBAOICxH);
422 mPreferences.add(mButtonBAIC);
423 mPreferences.add(mButtonBAICr);
424
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800425 // Find out if the sim card is ready.
426 boolean isSimReady = TelephonyManager.from(this).getSimState(
427 SubscriptionManager.getSlotIndex(mPhone.getSubId()))
428 == TelephonyManager.SIM_STATE_READY;
429
Suresh Koletib74bac72019-12-16 18:11:40 +0530430 // Deactivate all option and Change password option are unavailable
431 // when sim card is not ready.
432 if (isSimReady) {
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800433 mButtonDisableAll.setEnabled(true);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800434 mButtonChangePW.setEnabled(true);
435 } else {
Suresh Koletib74bac72019-12-16 18:11:40 +0530436 mButtonDisableAll.setEnabled(false);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800437 mButtonChangePW.setEnabled(false);
438 mButtonChangePW.setSummary(R.string.call_barring_change_pwd_description_disabled);
439 }
440
441 // Wait to do the initialization until onResume so that the TimeConsumingPreferenceActivity
442 // dialog can display as it relies on onResume / onPause to maintain its foreground state.
443 mFirstResume = true;
444 mIcicle = icicle;
445
446 ActionBar actionBar = getActionBar();
447 if (actionBar != null) {
448 // android.R.id.home will be triggered in onOptionsItemSelected()
449 actionBar.setDisplayHomeAsUpEnabled(true);
450 }
451
452 if (mIcicle != null && !mIcicle.getBoolean(SAVED_BEFORE_LOAD_COMPLETED_KEY)) {
453 if (DBG) {
454 Log.d(LOG_TAG, "restore stored states");
455 }
456 mInitIndex = mPreferences.size();
457
458 for (CallBarringEditPreference pref : mPreferences) {
459 Bundle bundle = mIcicle.getParcelable(pref.getKey());
460 if (bundle != null) {
461 pref.handleCallBarringResult(bundle.getBoolean(KEY_STATUS));
462 pref.init(this, true, mPhone);
463 pref.setEnabled(bundle.getBoolean(PREFERENCE_ENABLED_KEY, pref.isEnabled()));
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800464 }
465 }
466 mPwChangeState = mIcicle.getInt(PW_CHANGE_STATE_KEY);
467 mOldPassword = mIcicle.getString(OLD_PW_KEY);
468 mNewPassword = mIcicle.getString(NEW_PW_KEY);
469 displayPwChangeDialog(mIcicle.getInt(DIALOG_MESSAGE_KEY, mPwChangeDialogStrId), false);
470 mButtonChangePW.setText(mIcicle.getString(DIALOG_PW_ENTRY_KEY));
471 }
472 }
473
474 @Override
475 public void onResume() {
476 super.onResume();
477
478 if (mFirstResume) {
479 if (mIcicle == null || mIcicle.getBoolean(SAVED_BEFORE_LOAD_COMPLETED_KEY)) {
480 if (DBG) {
481 Log.d(LOG_TAG, "onResume: start to init ");
482 }
483 resetPwChangeState();
484 mPreferences.get(mInitIndex).init(this, false, mPhone);
485
486 // Request removing BUSY_SAVING_DIALOG because reading is restarted.
487 // (If it doesn't exist, nothing happen.)
488 removeDialog(BUSY_SAVING_DIALOG);
489 }
490 mFirstResume = false;
491 mIcicle = null;
492 }
493 }
494
495 @Override
496 protected void onSaveInstanceState(Bundle outState) {
497 super.onSaveInstanceState(outState);
498
499 for (CallBarringEditPreference pref : mPreferences) {
500 Bundle bundle = new Bundle();
501 bundle.putBoolean(KEY_STATUS, pref.mIsActivated);
502 bundle.putBoolean(PREFERENCE_ENABLED_KEY, pref.isEnabled());
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800503 outState.putParcelable(pref.getKey(), bundle);
504 }
505 outState.putInt(PW_CHANGE_STATE_KEY, mPwChangeState);
506 outState.putString(OLD_PW_KEY, mOldPassword);
507 outState.putString(NEW_PW_KEY, mNewPassword);
508 outState.putInt(DIALOG_MESSAGE_KEY, mPwChangeDialogStrId);
509 outState.putString(DIALOG_PW_ENTRY_KEY, mButtonChangePW.getText());
510
511 outState.putBoolean(SAVED_BEFORE_LOAD_COMPLETED_KEY,
512 mProgressDialog != null && mProgressDialog.isShowing());
513 }
514
515 /**
516 * Finish initialization of this preference and start next.
517 *
518 * @param preference The preference.
519 * @param reading If true to dismiss the busy reading dialog,
520 * false to dismiss the busy saving dialog.
521 */
522 public void onFinished(Preference preference, boolean reading) {
523 if (mInitIndex < mPreferences.size() - 1 && !isFinishing()) {
524 mInitIndex++;
525 mPreferences.get(mInitIndex).init(this, false, mPhone);
526 }
527 super.onFinished(preference, reading);
528 }
529
530 @Override
531 public boolean onOptionsItemSelected(MenuItem item) {
532 final int itemId = item.getItemId();
533 if (itemId == android.R.id.home) {
534 CallFeaturesSetting.goUpToTopLevelSetting(this, mSubscriptionInfoHelper);
535 return true;
536 }
537 return super.onOptionsItemSelected(item);
538 }
539
540 @Override
541 protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
542 super.onPrepareDialog(id, dialog, args);
543 if (id == BUSY_READING_DIALOG || id == BUSY_SAVING_DIALOG) {
544 // For onSaveInstanceState, treat the SAVING dialog as the same as the READING. As
545 // the result, if the activity is recreated while waiting for SAVING, it starts reading
546 // all the newest data.
547 mProgressDialog = dialog;
548 }
549 }
550}