blob: f5c892f70c2f5382c92057d2c05c7b50fd1349cd [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 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
Tyler Gunn1acf54a2018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
Billy Chi9614de82018-06-15 19:00:15 +080028import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.content.Context;
30import android.content.Intent;
31import android.content.IntentFilter;
Billy Chi9614de82018-06-15 19:00:15 +080032import android.database.ContentObserver;
33import android.database.Cursor;
Lucas Dupineb9c5702017-05-10 16:57:09 -070034import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070035import android.media.AudioManager;
36import android.media.ToneGenerator;
37import android.net.Uri;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070038import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.os.Bundle;
Billy Chi9614de82018-06-15 19:00:15 +080040import android.os.Handler;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070041import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070043import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000044import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070045import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.PhoneNumberUtils;
Billy Chi9614de82018-06-15 19:00:15 +080047import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070048import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070049import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070050import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070052import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080053import android.text.Spannable;
54import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.text.TextUtils;
56import android.text.TextWatcher;
57import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080058import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070060import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070061import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070063import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070064import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.View;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080066import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080068import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Lucas Dupinaf9e9912017-06-22 12:39:39 -070070import com.android.internal.colorextraction.ColorExtractor;
71import com.android.internal.colorextraction.ColorExtractor.GradientColors;
72import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070073import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070074import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070075import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080077import java.util.ArrayList;
78import java.util.List;
79
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080/**
81 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
82 *
83 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
84 * activity from apps/Contacts) that:
85 * 1. Allows ONLY emergency calls to be dialed
86 * 2. Disallows voicemail functionality
87 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
88 * activity to stay in front of the keyguard.
89 *
90 * TODO: Even though this is an ultra-simplified version of the normal
91 * dialer, there's still lots of code duplication between this class and
92 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
93 * moved into a shared base class that would live in the framework?
94 * Or could we figure out some way to move *this* class into apps/Contacts
95 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080096 *
97 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080098 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
99 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800100 * TODO item:
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800101 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102 */
103public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700104 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800105 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
106 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107 // Keys used with onSaveInstanceState().
108 private static final String LAST_NUMBER = "lastNumber";
109
110 // Intent action for this activity.
111 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
112
113 // List of dialer button IDs.
114 private static final int[] DIALER_KEYS = new int[] {
115 R.id.one, R.id.two, R.id.three,
116 R.id.four, R.id.five, R.id.six,
117 R.id.seven, R.id.eight, R.id.nine,
118 R.id.star, R.id.zero, R.id.pound };
119
120 // Debug constants.
121 private static final boolean DBG = false;
122 private static final String LOG_TAG = "EmergencyDialer";
123
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124 /** The length of DTMF tones in milliseconds */
125 private static final int TONE_LENGTH_MS = 150;
126
127 /** The DTMF tone volume relative to other sounds in the stream */
128 private static final int TONE_RELATIVE_VOLUME = 80;
129
130 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
131 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
132
133 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
134
Lucas Dupineb9c5702017-05-10 16:57:09 -0700135 /** 90% opacity, different from other gradients **/
136 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
137
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700138 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 private View mDialButton;
140 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800141 private View mEmergencyShortcutView;
142 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800144 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
145
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 private ToneGenerator mToneGenerator;
147 private Object mToneGeneratorLock = new Object();
148
149 // determines if we want to playback local DTMF tones.
150 private boolean mDTMFToneEnabled;
151
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700152 private EmergencyActionGroup mEmergencyActionGroup;
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 // close activity when screen turns off
155 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
156 @Override
157 public void onReceive(Context context, Intent intent) {
158 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700159 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 }
161 }
162 };
163
164 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
165
Lucas Dupineb9c5702017-05-10 16:57:09 -0700166 // Background gradient
167 private ColorExtractor mColorExtractor;
168 private GradientDrawable mBackgroundGradient;
169 private boolean mSupportsDarkText;
170
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700171 private boolean mIsWfcEmergencyCallingWarningEnabled;
172 private float mDefaultDigitsTextSize;
173
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800174 private boolean mAreEmergencyDialerShortcutsEnabled;
175
Billy Chi9614de82018-06-15 19:00:15 +0800176 /** Key of emergency information user name */
177 private static final String KEY_EMERGENCY_INFO_NAME = "name";
178
179 /** Authority of emergency information */
180 private static final String AUTHORITY = "com.android.emergency.info.name";
181
182 /** Content path of emergency information name */
183 private static final String CONTENT_PATH = "name";
184
185 /** Content URI of emergency information */
186 private static final Uri CONTENT_URI = new Uri.Builder()
187 .scheme(ContentResolver.SCHEME_CONTENT)
188 .authority(AUTHORITY)
189 .path(CONTENT_PATH)
190 .build();
191
192 /** ContentObserver for monitoring emergency info name changes */
193 private ContentObserver mEmergencyInfoNameObserver;
194
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 @Override
196 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
197 // Do nothing
198 }
199
200 @Override
201 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700202 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700203 }
204
205 @Override
206 public void afterTextChanged(Editable input) {
207 // Check for special sequences, in particular the "**04" or "**05"
208 // sequences that allow you to enter PIN or PUK-related codes.
209 //
210 // But note we *don't* allow most other special sequences here,
211 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
212 // since those shouldn't be available if the device is locked.
213 //
214 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
215 // here, not the regular handleChars() method.
216 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
217 // A special sequence was entered, clear the digits
218 mDigits.getText().clear();
219 }
220
221 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800222 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 }
224
225 @Override
226 protected void onCreate(Bundle icicle) {
227 super.onCreate(icicle);
228
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 // Allow this activity to be displayed in front of the keyguard / lockscreen.
230 WindowManager.LayoutParams lp = getWindow().getAttributes();
231 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700232
233 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700234 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700235 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
236
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 getWindow().setAttributes(lp);
238
Lucas Dupineb9c5702017-05-10 16:57:09 -0700239 mColorExtractor = new ColorExtractor(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700240 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
241 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700242 updateTheme(lockScreenColors.supportsDarkText());
243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 setContentView(R.layout.emergency_dialer);
245
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700246 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 mDigits.setKeyListener(DialerKeyListener.getInstance());
248 mDigits.setOnClickListener(this);
249 mDigits.setOnKeyListener(this);
250 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700251 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700252 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 maybeAddNumberFormatting();
254
Lucas Dupineb9c5702017-05-10 16:57:09 -0700255 mBackgroundGradient = new GradientDrawable(this);
256 Point displaySize = new Point();
257 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
258 .getDefaultDisplay().getSize(displaySize);
259 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
260 mBackgroundGradient.setAlpha(BACKGROUND_GRADIENT_ALPHA);
261 getWindow().setBackgroundDrawable(mBackgroundGradient);
262
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 // Check for the presence of the keypad
264 View view = findViewById(R.id.one);
265 if (view != null) {
266 setupKeypad();
267 }
268
269 mDelete = findViewById(R.id.deleteButton);
270 mDelete.setOnClickListener(this);
271 mDelete.setOnLongClickListener(this);
272
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700273 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274
275 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700276 // Read carrier config through the public API because PhoneGlobals is not available when we
277 // run as a secondary user.
278 CarrierConfigManager configMgr =
279 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
280 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800281 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700282
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700283 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 mDialButton.setOnClickListener(this);
285 } else {
286 mDialButton.setVisibility(View.GONE);
287 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700288 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
289 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
290 maybeShowWfcEmergencyCallingWarning();
291
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700292 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293
294 if (icicle != null) {
295 super.onRestoreInstanceState(icicle);
296 }
297
298 // Extract phone number from intent
299 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700300 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
302 if (number != null) {
303 mDigits.setText(number);
304 }
305 }
306
307 // if the mToneGenerator creation fails, just continue without it. It is
308 // a local audio signal, and is not as important as the dtmf tone itself.
309 synchronized (mToneGeneratorLock) {
310 if (mToneGenerator == null) {
311 try {
312 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
313 } catch (RuntimeException e) {
314 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
315 mToneGenerator = null;
316 }
317 }
318 }
319
320 final IntentFilter intentFilter = new IntentFilter();
321 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
322 registerReceiver(mBroadcastReceiver, intentFilter);
323
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700324 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800325
326 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
327 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
328
329 if (mAreEmergencyDialerShortcutsEnabled) {
330 setupEmergencyShortcutsView();
331 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 }
333
334 @Override
335 protected void onDestroy() {
336 super.onDestroy();
337 synchronized (mToneGeneratorLock) {
338 if (mToneGenerator != null) {
339 mToneGenerator.release();
340 mToneGenerator = null;
341 }
342 }
343 unregisterReceiver(mBroadcastReceiver);
Billy Chi9614de82018-06-15 19:00:15 +0800344 if (mEmergencyInfoNameObserver != null) {
345 getContentResolver().unregisterContentObserver(mEmergencyInfoNameObserver);
346 mEmergencyInfoNameObserver = null;
347 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 }
349
350 @Override
351 protected void onRestoreInstanceState(Bundle icicle) {
352 mLastNumber = icicle.getString(LAST_NUMBER);
353 }
354
355 @Override
356 protected void onSaveInstanceState(Bundle outState) {
357 super.onSaveInstanceState(outState);
358 outState.putString(LAST_NUMBER, mLastNumber);
359 }
360
361 /**
362 * Explicitly turn off number formatting, since it gets in the way of the emergency
363 * number detector
364 */
365 protected void maybeAddNumberFormatting() {
366 // Do nothing.
367 }
368
369 @Override
370 protected void onPostCreate(Bundle savedInstanceState) {
371 super.onPostCreate(savedInstanceState);
372
373 // This can't be done in onCreate(), since the auto-restoring of the digits
374 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
375 // is called. This method will be called every time the activity is created, and
376 // will always happen after onRestoreSavedInstanceState().
377 mDigits.addTextChangedListener(this);
378 }
379
380 private void setupKeypad() {
381 // Setup the listeners for the buttons
382 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700383 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
384 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700385 }
386
387 View view = findViewById(R.id.zero);
388 view.setOnLongClickListener(this);
389 }
390
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800391 @Override
392 public void onBackPressed() {
393 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800394 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800395 // Otherwise, it would finish the activity.
396 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
397 && mDialpadView.getVisibility() == View.VISIBLE) {
398 switchView(mEmergencyShortcutView, mDialpadView, true);
399 return;
400 }
401 super.onBackPressed();
402 }
403
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700404 /**
405 * handle key events
406 */
407 @Override
408 public boolean onKeyDown(int keyCode, KeyEvent event) {
409 switch (keyCode) {
410 // Happen when there's a "Call" hard button.
411 case KeyEvent.KEYCODE_CALL: {
412 if (TextUtils.isEmpty(mDigits.getText().toString())) {
413 // if we are adding a call from the InCallScreen and the phone
414 // number entered is empty, we just close the dialer to expose
415 // the InCallScreen under it.
416 finish();
417 } else {
418 // otherwise, we place the call.
419 placeCall();
420 }
421 return true;
422 }
423 }
424 return super.onKeyDown(keyCode, event);
425 }
426
427 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700428 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
430 mDigits.onKeyDown(keyCode, event);
431 }
432
433 @Override
434 public boolean onKey(View view, int keyCode, KeyEvent event) {
435 switch (view.getId()) {
436 case R.id.digits:
437 // Happen when "Done" button of the IME is pressed. This can happen when this
438 // Activity is forced into landscape mode due to a desk dock.
439 if (keyCode == KeyEvent.KEYCODE_ENTER
440 && event.getAction() == KeyEvent.ACTION_UP) {
441 placeCall();
442 return true;
443 }
444 break;
445 }
446 return false;
447 }
448
449 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700450 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800451 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700452 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800453 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700454 return handled;
455 }
456
457 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800458 public void onConfirmClick(EmergencyShortcutButton button) {
459 if (button == null) return;
460
461 String phoneNumber = button.getPhoneNumber();
462
463 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800464 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800465 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
466 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
467 } else {
468 Log.d(LOG_TAG, "emergency number is empty");
469 }
470 }
471
472 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700473 public void onClick(View view) {
474 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700475 case R.id.deleteButton: {
476 keyPressed(KeyEvent.KEYCODE_DEL);
477 return;
478 }
479 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700480 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700481 placeCall();
482 return;
483 }
484 case R.id.digits: {
485 if (mDigits.length() != 0) {
486 mDigits.setCursorVisible(true);
487 }
488 return;
489 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800490 case R.id.floating_action_button_dialpad: {
491 switchView(mDialpadView, mEmergencyShortcutView, true);
492 return;
493 }
494 case R.id.emergency_info_button: {
495 Intent intent = (Intent) view.getTag(R.id.tag_intent);
496 if (intent != null) {
497 startActivity(intent);
498 }
499 return;
500 }
Yorke Lee23a70732014-08-14 17:12:01 -0700501 }
502 }
503
504 @Override
505 public void onPressed(View view, boolean pressed) {
506 if (!pressed) {
507 return;
508 }
509 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510 case R.id.one: {
511 playTone(ToneGenerator.TONE_DTMF_1);
512 keyPressed(KeyEvent.KEYCODE_1);
513 return;
514 }
515 case R.id.two: {
516 playTone(ToneGenerator.TONE_DTMF_2);
517 keyPressed(KeyEvent.KEYCODE_2);
518 return;
519 }
520 case R.id.three: {
521 playTone(ToneGenerator.TONE_DTMF_3);
522 keyPressed(KeyEvent.KEYCODE_3);
523 return;
524 }
525 case R.id.four: {
526 playTone(ToneGenerator.TONE_DTMF_4);
527 keyPressed(KeyEvent.KEYCODE_4);
528 return;
529 }
530 case R.id.five: {
531 playTone(ToneGenerator.TONE_DTMF_5);
532 keyPressed(KeyEvent.KEYCODE_5);
533 return;
534 }
535 case R.id.six: {
536 playTone(ToneGenerator.TONE_DTMF_6);
537 keyPressed(KeyEvent.KEYCODE_6);
538 return;
539 }
540 case R.id.seven: {
541 playTone(ToneGenerator.TONE_DTMF_7);
542 keyPressed(KeyEvent.KEYCODE_7);
543 return;
544 }
545 case R.id.eight: {
546 playTone(ToneGenerator.TONE_DTMF_8);
547 keyPressed(KeyEvent.KEYCODE_8);
548 return;
549 }
550 case R.id.nine: {
551 playTone(ToneGenerator.TONE_DTMF_9);
552 keyPressed(KeyEvent.KEYCODE_9);
553 return;
554 }
555 case R.id.zero: {
556 playTone(ToneGenerator.TONE_DTMF_0);
557 keyPressed(KeyEvent.KEYCODE_0);
558 return;
559 }
560 case R.id.pound: {
561 playTone(ToneGenerator.TONE_DTMF_P);
562 keyPressed(KeyEvent.KEYCODE_POUND);
563 return;
564 }
565 case R.id.star: {
566 playTone(ToneGenerator.TONE_DTMF_S);
567 keyPressed(KeyEvent.KEYCODE_STAR);
568 return;
569 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 }
571 }
572
573 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700574 * called for long touch events
575 */
576 @Override
577 public boolean onLongClick(View view) {
578 int id = view.getId();
579 switch (id) {
580 case R.id.deleteButton: {
581 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 return true;
583 }
584 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700585 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700586 keyPressed(KeyEvent.KEYCODE_PLUS);
587 return true;
588 }
589 }
590 return false;
591 }
592
593 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700594 protected void onStart() {
595 super.onStart();
596
597 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin94b566f2017-05-28 11:45:52 -0700598 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
599 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700600 // Do not animate when view isn't visible yet, just set an initial state.
601 mBackgroundGradient.setColors(lockScreenColors, false);
602 updateTheme(lockScreenColors.supportsDarkText());
603 }
604
605 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700606 protected void onResume() {
607 super.onResume();
608
609 // retrieve the DTMF tone play back setting.
610 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
611 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
612
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700613 // if the mToneGenerator creation fails, just continue without it. It is
614 // a local audio signal, and is not as important as the dtmf tone itself.
615 synchronized (mToneGeneratorLock) {
616 if (mToneGenerator == null) {
617 try {
618 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
619 TONE_RELATIVE_VOLUME);
620 } catch (RuntimeException e) {
621 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
622 mToneGenerator = null;
623 }
624 }
625 }
626
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 updateDialAndDeleteButtonStateEnabledAttr();
628 }
629
630 @Override
631 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700633 }
634
Lucas Dupineb9c5702017-05-10 16:57:09 -0700635 @Override
636 protected void onStop() {
637 super.onStop();
638
639 mColorExtractor.removeOnColorsChangedListener(this);
640 }
641
642 /**
643 * Sets theme based on gradient colors
644 * @param supportsDarkText true if gradient supports dark text
645 */
646 private void updateTheme(boolean supportsDarkText) {
647 if (mSupportsDarkText == supportsDarkText) {
648 return;
649 }
650 mSupportsDarkText = supportsDarkText;
651
652 // We can't change themes after inflation, in this case we'll have to recreate
653 // the whole activity.
654 if (mBackgroundGradient != null) {
655 recreate();
656 return;
657 }
658
659 int vis = getWindow().getDecorView().getSystemUiVisibility();
660 if (supportsDarkText) {
661 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
662 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
663 setTheme(R.style.EmergencyDialerThemeDark);
664 } else {
665 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
666 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
667 setTheme(R.style.EmergencyDialerTheme);
668 }
669 getWindow().getDecorView().setSystemUiVisibility(vis);
670 }
671
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700672 /**
673 * place the call, but check to make sure it is a viable number.
674 */
675 private void placeCall() {
676 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900677
678 // Convert into emergency number according to emergency conversion map.
679 // If conversion map is not defined (this is default), this method does
680 // nothing and just returns input number.
681 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
682
Yorke Lee36bb2542014-06-05 08:09:52 -0700683 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700684 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
685
686 // place the call if it is a valid number
687 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
688 // There is no number entered.
689 playTone(ToneGenerator.TONE_PROP_NACK);
690 return;
691 }
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000692 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
693 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700694 } else {
695 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
696
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
698 }
Yorke Lee9b341512014-10-17 11:36:41 -0700699 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700700 }
701
702 /**
703 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
704 *
705 * The tone is played locally, using the audio stream for phone calls.
706 * Tones are played only if the "Audible touch tones" user preference
707 * is checked, and are NOT played if the device is in silent mode.
708 *
709 * @param tone a tone code from {@link ToneGenerator}
710 */
711 void playTone(int tone) {
712 // if local tone playback is disabled, just return.
713 if (!mDTMFToneEnabled) {
714 return;
715 }
716
717 // Also do nothing if the phone is in silent mode.
718 // We need to re-check the ringer mode for *every* playTone()
719 // call, rather than keeping a local flag that's updated in
720 // onResume(), since it's possible to toggle silent mode without
721 // leaving the current activity (via the ENDCALL-longpress menu.)
722 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
723 int ringerMode = audioManager.getRingerMode();
724 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
725 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
726 return;
727 }
728
729 synchronized (mToneGeneratorLock) {
730 if (mToneGenerator == null) {
731 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
732 return;
733 }
734
735 // Start the new tone (will stop any playing tone)
736 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
737 }
738 }
739
740 private CharSequence createErrorMessage(String number) {
741 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800742 String errorString = getString(R.string.dial_emergency_error, number);
743 int startingPosition = errorString.indexOf(number);
744 int endingPosition = startingPosition + number.length();
745 Spannable result = new SpannableString(errorString);
746 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
747 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700748 } else {
749 return getText(R.string.dial_emergency_empty_error).toString();
750 }
751 }
752
753 @Override
754 protected Dialog onCreateDialog(int id) {
755 AlertDialog dialog = null;
756 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
757 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700758 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700759 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
760 .setMessage(createErrorMessage(mLastNumber))
761 .setPositiveButton(R.string.ok, null)
762 .setCancelable(true).create();
763
764 // blur stuff behind the dialog
765 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700766 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 }
768 return dialog;
769 }
770
771 @Override
772 protected void onPrepareDialog(int id, Dialog dialog) {
773 super.onPrepareDialog(id, dialog);
774 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
775 AlertDialog alert = (AlertDialog) dialog;
776 alert.setMessage(createErrorMessage(mLastNumber));
777 }
778 }
779
Andrew Leed5631e82014-10-08 16:03:58 -0700780 @Override
781 public boolean onOptionsItemSelected(MenuItem item) {
782 final int itemId = item.getItemId();
783 if (itemId == android.R.id.home) {
784 onBackPressed();
785 return true;
786 }
787 return super.onOptionsItemSelected(item);
788 }
789
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 /**
791 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
792 */
793 private void updateDialAndDeleteButtonStateEnabledAttr() {
794 final boolean notEmpty = mDigits.length() != 0;
795
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 mDelete.setEnabled(notEmpty);
797 }
Yorke Lee91311662014-10-24 14:50:45 -0700798
799 /**
800 * Remove the digit just before the current position. Used by various long pressed callbacks
801 * to remove the digit that was populated as a result of the short click.
802 */
803 private void removePreviousDigitIfPossible() {
804 final int currentPosition = mDigits.getSelectionStart();
805 if (currentPosition > 0) {
806 mDigits.setSelection(currentPosition);
807 mDigits.getText().delete(currentPosition - 1, currentPosition);
808 }
809 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800810
811 /**
812 * Update the text-to-speech annotations in the edit field.
813 */
814 private void updateTtsSpans() {
815 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
816 mDigits.getText().removeSpan(o);
817 }
818 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
819 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700820
821 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700822 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700823 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700824 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
825 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700826 mBackgroundGradient.setColors(colors);
827 updateTheme(colors.supportsDarkText());
828 }
829 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700830
831 /**
832 * Where a carrier requires a warning that emergency calling is not available while on WFC,
833 * add hint text above the dial pad which warns the user of this case.
834 */
835 private void maybeShowWfcEmergencyCallingWarning() {
836 if (!mIsWfcEmergencyCallingWarningEnabled) {
837 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
838 return;
839 }
840
841 // Use an async task rather than calling into Telephony on UI thread.
842 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
843 @Override
844 protected Boolean doInBackground(Void... voids) {
845 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
846 boolean isWfcAvailable = tm.isWifiCallingAvailable();
847 ServiceState ss = tm.getServiceState();
848 boolean isCellAvailable =
849 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
850 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
851 + " isCellAvailable=" + isCellAvailable
852 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
853 return isWfcAvailable && !isCellAvailable;
854 }
855
856 @Override
857 protected void onPostExecute(Boolean result) {
858 if (result.booleanValue()) {
859 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
860 mDigits.setHint(R.string.dial_emergency_calling_not_available);
861 } else {
862 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
863 mDigits.setHint("");
864 }
865 maybeChangeHintSize();
866 }
867 };
868 showWfcWarningTask.execute((Void) null);
869 }
870
871 /**
872 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
873 * edit view and set the font size to a smaller size appropriate for the emergency calling
874 * warning.
875 */
876 private void maybeChangeHintSize() {
877 if (TextUtils.isEmpty(mDigits.getHint())
878 || !TextUtils.isEmpty(mDigits.getText().toString())) {
879 // No hint or there are dialed digits, so use default size.
880 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
881 // By default, the digits view auto-resizes to fit the text it contains, so
882 // enable that now.
883 mDigits.setResizeEnabled(true);
884 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
885 } else {
886 // Hint present and no dialed digits, set custom font size appropriate for the warning.
887 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
888 R.dimen.emergency_call_warning_size));
889 // Since we're populating this with a static text string, disable auto-resize.
890 mDigits.setResizeEnabled(false);
891 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
892 }
893 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800894
895 private void setupEmergencyShortcutsView() {
896 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
897 mDialpadView = findViewById(R.id.emergency_dialer);
898
899 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
900 dialpadButton.setOnClickListener(this);
901
902 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
903 emergencyInfoButton.setOnClickListener(this);
904
905 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
906 mEmergencyActionGroup.setVisibility(View.GONE);
907
908 // Setup dialpad title.
909 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
910 emergencyDialpadTitle.setVisibility(View.VISIBLE);
911
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800912 // TODO: Integrating emergency phone number table will get location information.
913 // Using null to present no location status.
914 setLocationInfo(null);
915
916 mEmergencyShortcutButtonList = new ArrayList<>();
917 setupEmergencyCallShortcutButton();
918
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800919 switchView(mEmergencyShortcutView, mDialpadView, false);
Billy Chi9614de82018-06-15 19:00:15 +0800920
921 mEmergencyInfoNameObserver = new ContentObserver(new Handler()) {
922 @Override
923 public void onChange(boolean selfChange) {
924 super.onChange(selfChange);
925 getEmergencyInfoNameAsync();
926 }
927 };
928 // Register ContentProvider for monitoring emergency info name changes.
929 getContentResolver().registerContentObserver(CONTENT_URI, false,
930 mEmergencyInfoNameObserver);
931 // Query emergency info name.
932 getEmergencyInfoNameAsync();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800933 }
934
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800935 private void setLocationInfo(String country) {
936 final View locationInfo = findViewById(R.id.location_info);
937
938 if (TextUtils.isEmpty(country)) {
939 locationInfo.setVisibility(View.INVISIBLE);
940 } else {
941 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
942 location.setText(country);
943 locationInfo.setVisibility(View.VISIBLE);
944 }
945 }
946
947 // TODO: Integrate emergency phone number table.
948 // Using default layout(no location, phone number is 112, description is Emergency,
949 // and icon is cross shape) until integrating emergency phone number table.
950 private void setupEmergencyCallShortcutButton() {
951 final ViewGroup shortcutButtonContainer = findViewById(
952 R.id.emergency_shortcut_buttons_container);
953 shortcutButtonContainer.setClipToOutline(true);
954
955 final EmergencyShortcutButton button =
956 (EmergencyShortcutButton) getLayoutInflater().inflate(
957 R.layout.emergency_shortcut_button,
958 shortcutButtonContainer, false);
959
960 button.setPhoneNumber("112");
961 button.setPhoneDescription("Emergency");
962 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
963 button.setOnConfirmClickListener(this);
964
965 shortcutButtonContainer.addView(button);
966 mEmergencyShortcutButtonList.add(button);
967
968 //Set emergency number title for numerous buttons.
969 if (shortcutButtonContainer.getChildCount() > 1) {
970 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
971 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
972 }
973 }
974
975 /**
976 * Called by the activity before a touch event is dispatched to the view hierarchy.
977 */
978 private void onPreTouchEvent(MotionEvent event) {
979 mEmergencyActionGroup.onPreTouchEvent(event);
980
981 if (mEmergencyShortcutButtonList != null) {
982 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
983 button.onPreTouchEvent(event);
984 }
985 }
986 }
987
988 /**
989 * Called by the activity after a touch event is dispatched to the view hierarchy.
990 */
991 private void onPostTouchEvent(MotionEvent event) {
992 mEmergencyActionGroup.onPostTouchEvent(event);
993
994 if (mEmergencyShortcutButtonList != null) {
995 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
996 button.onPostTouchEvent(event);
997 }
998 }
999 }
1000
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001001 /**
1002 * Switch two view.
1003 *
1004 * @param displayView the view would be displayed.
1005 * @param hideView the view would be hidden.
1006 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1007 */
1008 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1009 if (displayView == null || hideView == null) {
1010 return;
1011 }
1012
1013 if (displayView.getVisibility() == View.VISIBLE) {
1014 return;
1015 }
1016
1017 if (hasAnimation) {
1018 crossfade(hideView, displayView);
1019 } else {
1020 hideView.setVisibility(View.GONE);
1021 displayView.setVisibility(View.VISIBLE);
1022 }
1023 }
1024
1025 /**
1026 * Fade out and fade in animation between two view transition.
1027 */
1028 private void crossfade(View fadeOutView, View fadeInView) {
1029 if (fadeOutView == null || fadeInView == null) {
1030 return;
1031 }
1032 final int shortAnimationDuration = getResources().getInteger(
1033 android.R.integer.config_shortAnimTime);
1034
1035 fadeInView.setAlpha(0f);
1036 fadeInView.setVisibility(View.VISIBLE);
1037
1038 fadeInView.animate()
1039 .alpha(1f)
1040 .setDuration(shortAnimationDuration)
1041 .setListener(null);
1042
1043 fadeOutView.animate()
1044 .alpha(0f)
1045 .setDuration(shortAnimationDuration)
1046 .setListener(new AnimatorListenerAdapter() {
1047 @Override
1048 public void onAnimationEnd(Animator animation) {
1049 fadeOutView.setVisibility(View.GONE);
1050 }
1051 });
1052 }
Billy Chi9614de82018-06-15 19:00:15 +08001053
1054 /**
1055 * Get emergency info name from EmergencyInfo and then update EmergencyInfoGroup.
1056 */
1057 private void getEmergencyInfoNameAsync() {
1058 new AsyncTask<Void, Void, String>() {
1059 @Override
1060 protected String doInBackground(Void... params) {
1061 String name = "";
1062 try (Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,
1063 null)) {
1064 if (cursor != null && cursor.moveToFirst()) {
1065 int index = cursor.getColumnIndex(KEY_EMERGENCY_INFO_NAME);
1066 name = index > -1 ? cursor.getString(index) : "";
1067 }
1068 } catch (IllegalArgumentException ex) {
1069 Log.w(LOG_TAG, "getEmergencyInfoNameAsync failed", ex);
1070 }
1071 return name;
1072 }
1073
1074 @Override
1075 protected void onPostExecute(String result) {
1076 super.onPostExecute(result);
1077 if (!isFinishing() && !isDestroyed()) {
1078 // Update emergency info with emergency info name
1079 EmergencyInfoGroup group = findViewById(R.id.emergency_info_group);
1080 if (group != null) {
1081 group.updateEmergencyInfo(result);
1082 }
1083 }
1084 }
1085 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
1086 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087}