blob: 34c017c4bcb754c86d91539e8e7443c83c365373 [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 static com.android.phone.TimeConsumingPreferenceActivity.RESPONSE_ERROR;
20
21import android.app.AlertDialog;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.res.TypedArray;
25import android.os.AsyncResult;
26import android.os.Bundle;
27import android.os.Handler;
28import android.os.Message;
Kikkawa Shoheie47388b2020-08-07 18:17:14 +090029import android.os.PersistableBundle;
30import android.telephony.CarrierConfigManager;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080031import android.text.method.DigitsKeyListener;
32import android.text.method.PasswordTransformationMethod;
33import android.util.AttributeSet;
34import android.util.Log;
35import android.view.View;
36import android.widget.EditText;
37import android.widget.TextView;
38import android.widget.Toast;
39
40import com.android.internal.telephony.CommandException;
41import com.android.internal.telephony.Phone;
42import com.android.internal.telephony.PhoneFactory;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080043import com.android.phone.settings.fdn.EditPinPreference;
44
45import java.lang.ref.WeakReference;
46
47/**
48 * This preference represents the status of call barring options, enabling/disabling
49 * the call barring option will prompt the user for the current password.
50 */
51public class CallBarringEditPreference extends EditPinPreference {
52 private static final String LOG_TAG = "CallBarringEditPreference";
53 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
54
55 private String mFacility;
56 boolean mIsActivated = false;
57 private CharSequence mEnableText;
58 private CharSequence mDisableText;
59 private CharSequence mSummaryOn;
60 private CharSequence mSummaryOff;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080061 private int mButtonClicked;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080062 private final MyHandler mHandler = new MyHandler(this);
63 private Phone mPhone;
64 private TimeConsumingPreferenceListener mTcpListener;
65
66 private static final int PW_LENGTH = 4;
67
68 /**
69 * CallBarringEditPreference constructor.
70 *
71 * @param context The context of view.
72 * @param attrs The attributes of the XML tag that is inflating EditTextPreference.
73 */
74 public CallBarringEditPreference(Context context, AttributeSet attrs) {
75 super(context, attrs);
76 // Get the summary settings, use CheckBoxPreference as the standard.
77 TypedArray typedArray = context.obtainStyledAttributes(attrs,
Meng Wang92ea32d2020-05-06 18:51:51 +000078 android.R.styleable.CheckBoxPreference, 0, 0);
79 mSummaryOn = typedArray.getString(android.R.styleable.CheckBoxPreference_summaryOn);
80 mSummaryOff = typedArray.getString(android.R.styleable.CheckBoxPreference_summaryOff);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080081 mDisableText = context.getText(R.string.disable);
82 mEnableText = context.getText(R.string.enable);
83 typedArray.recycle();
84
85 // Get default phone
86 mPhone = PhoneFactory.getDefaultPhone();
87
88 typedArray = context.obtainStyledAttributes(attrs,
89 R.styleable.CallBarringEditPreference, 0, R.style.EditPhoneNumberPreference);
90 mFacility = typedArray.getString(R.styleable.CallBarringEditPreference_facility);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +080091 typedArray.recycle();
92 }
93
94 /**
95 * CallBarringEditPreference constructor.
96 *
97 * @param context The context of view.
98 */
99 public CallBarringEditPreference(Context context) {
100 this(context, null);
101 }
102
103 void init(TimeConsumingPreferenceListener listener, boolean skipReading, Phone phone) {
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700104 Log.d(LOG_TAG, "init: phone id = " + phone.getPhoneId());
105
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800106 mPhone = phone;
107
108 mTcpListener = listener;
109 if (!skipReading) {
110 // Query call barring status
111 mPhone.getCallBarring(mFacility, "", mHandler.obtainMessage(
Kikkawa Shoheie47388b2020-08-07 18:17:14 +0900112 MyHandler.MESSAGE_GET_CALL_BARRING), getServiceClassForCallBarring(mPhone));
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800113 if (mTcpListener != null) {
114 mTcpListener.onStarted(this, true);
115 }
116 }
117 }
118
119 @Override
120 public void onClick(DialogInterface dialog, int which) {
121 super.onClick(dialog, which);
122 mButtonClicked = which;
123 }
124
125 @Override
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800126 protected void showDialog(Bundle state) {
Suresh Koletib74bac72019-12-16 18:11:40 +0530127 setDialogMessage(getContext().getString(R.string.messageCallBarring));
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800128 super.showDialog(state);
129 }
130
131 @Override
132 protected void onBindView(View view) {
133 super.onBindView(view);
134
135 // Sync the summary view
136 TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
137 if (summaryView != null) {
138 CharSequence sum;
139 int vis;
140
141 // Set summary depending upon mode
142 if (mIsActivated) {
143 sum = (mSummaryOn == null) ? getSummary() : mSummaryOn;
144 } else {
145 sum = (mSummaryOff == null) ? getSummary() : mSummaryOff;
146 }
147
148 if (sum != null) {
149 summaryView.setText(sum);
150 vis = View.VISIBLE;
151 } else {
152 vis = View.GONE;
153 }
154
155 if (vis != summaryView.getVisibility()) {
156 summaryView.setVisibility(vis);
157 }
158 }
159 }
160
161 @Override
162 protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
163 builder.setPositiveButton(null, null);
164 builder.setNeutralButton(mIsActivated ? mDisableText : mEnableText, this);
165 }
166
167 @Override
168 protected void onBindDialogView(View view) {
169 super.onBindDialogView(view);
170 // Default the button clicked to be the cancel button.
171 mButtonClicked = DialogInterface.BUTTON_NEGATIVE;
172
173 final EditText editText = (EditText) view.findViewById(android.R.id.edit);
174 if (editText != null) {
175 editText.setSingleLine(true);
176 editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
177 editText.setKeyListener(DigitsKeyListener.getInstance());
178
Suresh Koletib74bac72019-12-16 18:11:40 +0530179 editText.setVisibility(View.VISIBLE);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800180 }
181 }
182
183 @Override
184 protected void onDialogClosed(boolean positiveResult) {
185 super.onDialogClosed(positiveResult);
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700186 Log.d(LOG_TAG, "onDialogClosed: mButtonClicked=" + mButtonClicked + ", positiveResult="
187 + positiveResult);
188
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800189 if (mButtonClicked != DialogInterface.BUTTON_NEGATIVE) {
Suresh Koletib74bac72019-12-16 18:11:40 +0530190 String password = getEditText().getText().toString();
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800191
Suresh Koletib74bac72019-12-16 18:11:40 +0530192 // Check if the password is valid.
193 if (password == null || password.length() != PW_LENGTH) {
194 Toast.makeText(getContext(),
195 getContext().getString(R.string.call_barring_right_pwd_number),
196 Toast.LENGTH_SHORT).show();
197 return;
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800198 }
199
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700200 Log.d(LOG_TAG, "onDialogClosed");
201
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800202 // Send set call barring message to RIL layer.
203 mPhone.setCallBarring(mFacility, !mIsActivated, password,
Pooja Jain93124702019-11-19 18:00:46 +0530204 mHandler.obtainMessage(MyHandler.MESSAGE_SET_CALL_BARRING),
Kikkawa Shoheie47388b2020-08-07 18:17:14 +0900205 getServiceClassForCallBarring(mPhone));
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800206 if (mTcpListener != null) {
207 mTcpListener.onStarted(this, false);
208 }
209 }
210 }
211
212 void handleCallBarringResult(boolean status) {
213 mIsActivated = status;
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700214 Log.i(LOG_TAG, "handleCallBarringResult: mIsActivated=" + mIsActivated);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800215 }
216
Kikkawa Shoheie47388b2020-08-07 18:17:14 +0900217 private static int getServiceClassForCallBarring(Phone phone) {
218 int serviceClass = CarrierConfigManager.SERVICE_CLASS_VOICE;
219 PersistableBundle carrierConfig = PhoneGlobals.getInstance()
220 .getCarrierConfigForSubId(phone.getSubId());
221 if (carrierConfig != null) {
222 serviceClass = carrierConfig.getInt(
223 CarrierConfigManager.KEY_CALL_BARRING_DEFAULT_SERVICE_CLASS_INT,
224 CarrierConfigManager.SERVICE_CLASS_VOICE);
225 }
226 return serviceClass;
227 }
228
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800229 void updateSummaryText() {
230 notifyChanged();
231 notifyDependencyChange(shouldDisableDependents());
232 }
233
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800234 @Override
235 public boolean shouldDisableDependents() {
236 return mIsActivated;
237 }
238
239 // Message protocol:
240 // what: get vs. set
241 // arg1: action -- register vs. disable
242 // arg2: get vs. set for the preceding request
243 private static class MyHandler extends Handler {
244 private static final int MESSAGE_GET_CALL_BARRING = 0;
245 private static final int MESSAGE_SET_CALL_BARRING = 1;
246
247 private final WeakReference<CallBarringEditPreference> mCallBarringEditPreference;
248
249 private MyHandler(CallBarringEditPreference callBarringEditPreference) {
250 mCallBarringEditPreference =
251 new WeakReference<CallBarringEditPreference>(callBarringEditPreference);
252 }
253
254 @Override
255 public void handleMessage(Message msg) {
256 switch (msg.what) {
257 case MESSAGE_GET_CALL_BARRING:
258 handleGetCallBarringResponse(msg);
259 break;
260 case MESSAGE_SET_CALL_BARRING:
261 handleSetCallBarringResponse(msg);
262 break;
263 default:
264 break;
265 }
266 }
267
268 // Handle the response message for query CB status.
269 private void handleGetCallBarringResponse(Message msg) {
270 final CallBarringEditPreference pref = mCallBarringEditPreference.get();
271 if (pref == null) {
272 return;
273 }
274
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700275 Log.i(LOG_TAG, "handleGetCallBarringResponse: done");
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800276
277 AsyncResult ar = (AsyncResult) msg.obj;
278
279 if (msg.arg2 == MESSAGE_SET_CALL_BARRING) {
280 pref.mTcpListener.onFinished(pref, false);
281 } else {
282 pref.mTcpListener.onFinished(pref, true);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800283 }
284
285 // Unsuccessful query for call barring.
286 if (ar.exception != null) {
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700287 Log.i(LOG_TAG, "handleGetCallBarringResponse: ar.exception=" + ar.exception);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800288 pref.mTcpListener.onException(pref, (CommandException) ar.exception);
289 } else {
290 if (ar.userObj instanceof Throwable) {
291 pref.mTcpListener.onError(pref, RESPONSE_ERROR);
292 }
293 int[] ints = (int[]) ar.result;
294 if (ints.length == 0) {
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700295 Log.i(LOG_TAG, "handleGetCallBarringResponse: ar.result.length==0");
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800296 pref.setEnabled(false);
297 pref.mTcpListener.onError(pref, RESPONSE_ERROR);
298 } else {
299 pref.handleCallBarringResult(ints[0] != 0);
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700300 Log.i(LOG_TAG,
301 "handleGetCallBarringResponse: CB state successfully queried: "
302 + ints[0]);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800303 }
304 }
305 // Update call barring status.
306 pref.updateSummaryText();
307 }
308
309 // Handle the response message for CB settings.
310 private void handleSetCallBarringResponse(Message msg) {
311 final CallBarringEditPreference pref = mCallBarringEditPreference.get();
312 if (pref == null) {
313 return;
314 }
315
316 AsyncResult ar = (AsyncResult) msg.obj;
317
318 if (ar.exception != null || ar.userObj instanceof Throwable) {
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700319 Log.i(LOG_TAG, "handleSetCallBarringResponse: ar.exception=" + ar.exception);
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800320 }
Tyler Gunn4dd7c5c2022-05-09 14:30:41 -0700321 Log.i(LOG_TAG, "handleSetCallBarringResponse: re-get call barring option");
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800322 pref.mPhone.getCallBarring(
323 pref.mFacility,
324 "",
325 obtainMessage(MESSAGE_GET_CALL_BARRING, 0, MESSAGE_SET_CALL_BARRING,
Kikkawa Shoheie47388b2020-08-07 18:17:14 +0900326 ar.exception), getServiceClassForCallBarring(pref.mPhone));
Aida Takeshi7c3b4a32016-08-11 13:42:24 +0800327 }
328 }
329}