blob: 9d43d60b2b647ecb5d6093ced868b5aae5ce4308 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2009 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.Activity;
Brad Ebinger6d4ef742018-02-07 10:59:33 -080020import android.app.AlertDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.Dialog;
22import android.app.ProgressDialog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.content.BroadcastReceiver;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.DialogInterface;
Li Wei65667ea2017-08-02 16:06:59 +080027import android.content.DialogInterface.OnCancelListener;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070028import android.content.Intent;
29import android.content.IntentFilter;
30import android.content.ServiceConnection;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.os.AsyncResult;
32import android.os.Bundle;
33import android.os.CountDownTimer;
34import android.os.Handler;
35import android.os.IBinder;
36import android.os.Looper;
37import android.os.Message;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.util.Log;
39
40import com.android.internal.telephony.Phone;
41import com.android.internal.telephony.TelephonyIntents;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042
43/**
44 * Displays dialog that enables users to exit Emergency Callback Mode
45 *
46 * @see EmergencyCallbackModeService
47 */
Li Wei65667ea2017-08-02 16:06:59 +080048public class EmergencyCallbackModeExitDialog extends Activity implements OnCancelListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049
Yorke Lee34a72cb2014-10-12 13:17:04 -070050 private static final String TAG = "EmergencyCallbackMode";
51
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052 /** Intent to trigger the Emergency Callback Mode exit dialog */
53 static final String ACTION_SHOW_ECM_EXIT_DIALOG =
54 "com.android.phone.action.ACTION_SHOW_ECM_EXIT_DIALOG";
55 /** Used to get the users choice from the return Intent's extra */
56 public static final String EXTRA_EXIT_ECM_RESULT = "exit_ecm_result";
57
58 public static final int EXIT_ECM_BLOCK_OTHERS = 1;
59 public static final int EXIT_ECM_DIALOG = 2;
60 public static final int EXIT_ECM_PROGRESS_DIALOG = 3;
61 public static final int EXIT_ECM_IN_EMERGENCY_CALL_DIALOG = 4;
62
63 AlertDialog mAlertDialog = null;
64 ProgressDialog mProgressDialog = null;
65 CountDownTimer mTimer = null;
66 EmergencyCallbackModeService mService = null;
67 Handler mHandler = null;
68 int mDialogType = 0;
69 long mEcmTimeout = 0;
70 private boolean mInEmergencyCall = false;
71 private static final int ECM_TIMER_RESET = 1;
72 private Phone mPhone = null;
73
74 @Override
75 public void onCreate(Bundle savedInstanceState) {
76 super.onCreate(savedInstanceState);
77
Sandeep Kuntade73a6a2014-10-15 18:45:56 +053078 mPhone = PhoneGlobals.getInstance().getPhoneInEcm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079 // Check if phone is in Emergency Callback Mode. If not, exit.
Brad Ebinger6d4ef742018-02-07 10:59:33 -080080 if (mPhone == null || !mPhone.isInEcm()) {
81 Log.i(TAG, "ECMModeExitDialog launched - isInEcm: false" + " phone:" + mPhone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082 finish();
Yorke Lee34a72cb2014-10-12 13:17:04 -070083 return;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084 }
Brad Ebinger6d4ef742018-02-07 10:59:33 -080085 Log.i(TAG, "ECMModeExitDialog launched - isInEcm: true" + " phone:" + mPhone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
87 mHandler = new Handler();
88
89 // Start thread that will wait for the connection completion so that it can get
90 // timeout value from the service
91 Thread waitForConnectionCompleteThread = new Thread(null, mTask,
92 "EcmExitDialogWaitThread");
93 waitForConnectionCompleteThread.start();
94
95 // Register ECM timer reset notfication
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096 mPhone.registerForEcmTimerReset(mTimerResetHandler, ECM_TIMER_RESET, null);
97
98 // Register receiver for intent closing the dialog
99 IntentFilter filter = new IntentFilter();
100 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
101 registerReceiver(mEcmExitReceiver, filter);
102 }
103
104 @Override
105 public void onDestroy() {
106 super.onDestroy();
Yorke Lee34a72cb2014-10-12 13:17:04 -0700107 try {
108 unregisterReceiver(mEcmExitReceiver);
109 } catch (IllegalArgumentException e) {
110 // Receiver was never registered - silently ignore.
111 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 // Unregister ECM timer reset notification
Yorke Lee34a72cb2014-10-12 13:17:04 -0700113 if (mPhone != null) {
114 mPhone.unregisterForEcmTimerReset(mHandler);
115 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 }
117
118 @Override
119 protected void onRestoreInstanceState(Bundle savedInstanceState) {
120 super.onRestoreInstanceState(savedInstanceState);
121 mDialogType = savedInstanceState.getInt("DIALOG_TYPE");
122 }
123
124 @Override
125 protected void onSaveInstanceState(Bundle outState) {
126 super.onSaveInstanceState(outState);
127 outState.putInt("DIALOG_TYPE", mDialogType);
128 }
129
130 /**
131 * Waits until bind to the service completes
132 */
133 private Runnable mTask = new Runnable() {
134 public void run() {
135 Looper.prepare();
136
137 // Bind to the remote service
138 bindService(new Intent(EmergencyCallbackModeExitDialog.this,
139 EmergencyCallbackModeService.class), mConnection, Context.BIND_AUTO_CREATE);
140
141 // Wait for bind to finish
142 synchronized (EmergencyCallbackModeExitDialog.this) {
143 try {
144 if (mService == null) {
145 EmergencyCallbackModeExitDialog.this.wait();
146 }
147 } catch (InterruptedException e) {
148 Log.d("ECM", "EmergencyCallbackModeExitDialog InterruptedException: "
149 + e.getMessage());
150 e.printStackTrace();
151 }
152 }
153
154 // Get timeout value and call state from the service
155 if (mService != null) {
156 mEcmTimeout = mService.getEmergencyCallbackModeTimeout();
157 mInEmergencyCall = mService.getEmergencyCallbackModeCallState();
Yorke Lee34a72cb2014-10-12 13:17:04 -0700158 try {
159 // Unbind from remote service
160 unbindService(mConnection);
161 } catch (IllegalArgumentException e) {
162 // Failed to unbind from service. Don't crash as this brings down the entire
163 // radio.
164 Log.w(TAG, "Failed to unbind from EmergencyCallbackModeService");
165 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 }
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 // Show dialog
169 mHandler.post(new Runnable() {
170 public void run() {
171 showEmergencyCallbackModeExitDialog();
172 }
173 });
174 }
175 };
176
177 /**
178 * Shows Emergency Callback Mode dialog and starts countdown timer
179 */
180 private void showEmergencyCallbackModeExitDialog() {
Yorke Lee34a72cb2014-10-12 13:17:04 -0700181 if (!this.isResumed()) {
182 Log.w(TAG, "Tried to show dialog, but activity was already finished");
183 return;
184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185 if(mInEmergencyCall) {
186 mDialogType = EXIT_ECM_IN_EMERGENCY_CALL_DIALOG;
187 showDialog(EXIT_ECM_IN_EMERGENCY_CALL_DIALOG);
188 } else {
189 if (getIntent().getAction().equals(
190 TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS)) {
191 mDialogType = EXIT_ECM_BLOCK_OTHERS;
192 showDialog(EXIT_ECM_BLOCK_OTHERS);
193 } else if (getIntent().getAction().equals(ACTION_SHOW_ECM_EXIT_DIALOG)) {
194 mDialogType = EXIT_ECM_DIALOG;
195 showDialog(EXIT_ECM_DIALOG);
196 }
197
198 mTimer = new CountDownTimer(mEcmTimeout, 1000) {
199 @Override
200 public void onTick(long millisUntilFinished) {
201 CharSequence text = getDialogText(millisUntilFinished);
202 mAlertDialog.setMessage(text);
203 }
204
205 @Override
206 public void onFinish() {
207 //Do nothing
208 }
209 }.start();
210 }
211 }
212
213 /**
214 * Creates dialog that enables users to exit Emergency Callback Mode
215 */
216 @Override
217 protected Dialog onCreateDialog(int id) {
218 switch (id) {
219 case EXIT_ECM_BLOCK_OTHERS:
220 case EXIT_ECM_DIALOG:
221 CharSequence text = getDialogText(mEcmTimeout);
222 mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this)
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700223 .setIcon(R.drawable.ic_emergency_callback_mode)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 .setTitle(R.string.phone_in_ecm_notification_title)
225 .setMessage(text)
226 .setPositiveButton(R.string.alert_dialog_yes,
227 new DialogInterface.OnClickListener() {
228 public void onClick(DialogInterface dialog,int whichButton) {
229 // User clicked Yes. Exit Emergency Callback Mode.
230 mPhone.exitEmergencyCallbackMode();
231
232 // Show progress dialog
233 showDialog(EXIT_ECM_PROGRESS_DIALOG);
234 mTimer.cancel();
235 }
236 })
237 .setNegativeButton(R.string.alert_dialog_no,
238 new DialogInterface.OnClickListener() {
239 public void onClick(DialogInterface dialog, int whichButton) {
240 // User clicked No
241 setResult(RESULT_OK, (new Intent()).putExtra(
242 EXTRA_EXIT_ECM_RESULT, false));
243 finish();
244 }
245 }).create();
Li Wei65667ea2017-08-02 16:06:59 +0800246 mAlertDialog.setOnCancelListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 return mAlertDialog;
248
249 case EXIT_ECM_IN_EMERGENCY_CALL_DIALOG:
250 mAlertDialog = new AlertDialog.Builder(EmergencyCallbackModeExitDialog.this)
Tyler Gunn625eb0b2014-08-27 20:37:32 -0700251 .setIcon(R.drawable.ic_emergency_callback_mode)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 .setTitle(R.string.phone_in_ecm_notification_title)
253 .setMessage(R.string.alert_dialog_in_ecm_call)
254 .setNeutralButton(R.string.alert_dialog_dismiss,
255 new DialogInterface.OnClickListener() {
256 public void onClick(DialogInterface dialog, int whichButton) {
257 // User clicked Dismiss
258 setResult(RESULT_OK, (new Intent()).putExtra(
259 EXTRA_EXIT_ECM_RESULT, false));
260 finish();
261 }
262 }).create();
Li Wei65667ea2017-08-02 16:06:59 +0800263 mAlertDialog.setOnCancelListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 return mAlertDialog;
265
266 case EXIT_ECM_PROGRESS_DIALOG:
267 mProgressDialog = new ProgressDialog(EmergencyCallbackModeExitDialog.this);
268 mProgressDialog.setMessage(getText(R.string.progress_dialog_exiting_ecm));
269 mProgressDialog.setIndeterminate(true);
270 mProgressDialog.setCancelable(false);
271 return mProgressDialog;
272
273 default:
274 return null;
275 }
276 }
277
278 /**
279 * Returns dialog box text with updated timeout value
280 */
281 private CharSequence getDialogText(long millisUntilFinished) {
282 // Format time
283 int minutes = (int)(millisUntilFinished / 60000);
284 String time = String.format("%d:%02d", minutes,
285 (millisUntilFinished % 60000) / 1000);
286
287 switch (mDialogType) {
288 case EXIT_ECM_BLOCK_OTHERS:
289 return String.format(getResources().getQuantityText(
290 R.plurals.alert_dialog_not_avaialble_in_ecm, minutes).toString(), time);
291 case EXIT_ECM_DIALOG:
292 return String.format(getResources().getQuantityText(R.plurals.alert_dialog_exit_ecm,
293 minutes).toString(), time);
294 }
295 return null;
296 }
297
298 /**
Li Wei65667ea2017-08-02 16:06:59 +0800299 * Closes activity when dialog is canceled
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 */
Yorke Lee34a72cb2014-10-12 13:17:04 -0700301 @Override
Li Wei65667ea2017-08-02 16:06:59 +0800302 public void onCancel(DialogInterface dialog) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 EmergencyCallbackModeExitDialog.this.setResult(RESULT_OK, (new Intent())
304 .putExtra(EXTRA_EXIT_ECM_RESULT, false));
305 finish();
306 }
307
308 /**
309 * Listens for Emergency Callback Mode state change intents
310 */
311 private BroadcastReceiver mEcmExitReceiver = new BroadcastReceiver() {
312 @Override
313 public void onReceive(Context context, Intent intent) {
314 // Received exit Emergency Callback Mode notification close all dialogs
315 if (intent.getAction().equals(
316 TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED)) {
317 if (intent.getBooleanExtra("phoneinECMState", false) == false) {
318 if (mAlertDialog != null)
319 mAlertDialog.dismiss();
320 if (mProgressDialog != null)
321 mProgressDialog.dismiss();
322 EmergencyCallbackModeExitDialog.this.setResult(RESULT_OK, (new Intent())
323 .putExtra(EXTRA_EXIT_ECM_RESULT, true));
324 finish();
325 }
326 }
327 }
328 };
329
330 /**
331 * Class for interacting with the interface of the service
332 */
333 private ServiceConnection mConnection = new ServiceConnection() {
334 public void onServiceConnected(ComponentName className, IBinder service) {
335 mService = ((EmergencyCallbackModeService.LocalBinder)service).getService();
336 // Notify thread that connection is ready
337 synchronized (EmergencyCallbackModeExitDialog.this) {
338 EmergencyCallbackModeExitDialog.this.notify();
339 }
340 }
341
342 public void onServiceDisconnected(ComponentName className) {
343 mService = null;
344 }
345 };
346
347 /**
348 * Class for receiving framework timer reset notifications
349 */
350 private Handler mTimerResetHandler = new Handler () {
351 public void handleMessage(Message msg) {
352 switch (msg.what) {
353 case ECM_TIMER_RESET:
354 if(!((Boolean)((AsyncResult) msg.obj).result).booleanValue()) {
355 EmergencyCallbackModeExitDialog.this.setResult(RESULT_OK, (new Intent())
356 .putExtra(EXTRA_EXIT_ECM_RESULT, false));
357 finish();
358 }
359 break;
360 }
361 }
362 };
363}