blob: b91f927224ed37c1c4b39d87225a34ed5ffd7201 [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 Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-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 Chi17ec2282018-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 Chi17ec2282018-06-15 19:00:15 +080032import android.database.ContentObserver;
33import android.database.Cursor;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080034import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070035import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070036import android.media.AudioManager;
37import android.media.ToneGenerator;
38import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Billy Chi17ec2282018-06-15 19:00:15 +080041import android.os.Handler;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070042import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070044import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070045import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080048import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070051import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070053import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080054import android.text.Spannable;
55import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.text.TextUtils;
57import android.text.TextWatcher;
58import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080059import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070061import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070062import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070064import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070065import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.View;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080067import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080069import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070
Lucas Dupinaf9e9912017-06-22 12:39:39 -070071import com.android.internal.colorextraction.ColorExtractor;
72import com.android.internal.colorextraction.ColorExtractor.GradientColors;
73import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070074import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070075import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070076import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang92cfe512018-06-07 16:25:27 +080078import java.util.ArrayList;
79import java.util.List;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081/**
82 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
83 *
84 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
85 * activity from apps/Contacts) that:
86 * 1. Allows ONLY emergency calls to be dialed
87 * 2. Disallows voicemail functionality
88 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
89 * activity to stay in front of the keyguard.
90 *
91 * TODO: Even though this is an ultra-simplified version of the normal
92 * dialer, there's still lots of code duplication between this class and
93 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
94 * moved into a shared base class that would live in the framework?
95 * Or could we figure out some way to move *this* class into apps/Contacts
96 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080097 *
98 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +080099 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
100 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800101 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800102 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 */
104public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700105 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800106 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
107 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 // Keys used with onSaveInstanceState().
109 private static final String LAST_NUMBER = "lastNumber";
110
111 // Intent action for this activity.
112 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
113
114 // List of dialer button IDs.
115 private static final int[] DIALER_KEYS = new int[] {
116 R.id.one, R.id.two, R.id.three,
117 R.id.four, R.id.five, R.id.six,
118 R.id.seven, R.id.eight, R.id.nine,
119 R.id.star, R.id.zero, R.id.pound };
120
121 // Debug constants.
122 private static final boolean DBG = false;
123 private static final String LOG_TAG = "EmergencyDialer";
124
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 /** The length of DTMF tones in milliseconds */
126 private static final int TONE_LENGTH_MS = 150;
127
128 /** The DTMF tone volume relative to other sounds in the stream */
129 private static final int TONE_RELATIVE_VOLUME = 80;
130
131 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
132 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
133
134 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
135
Lucas Dupineb9c5702017-05-10 16:57:09 -0700136 /** 90% opacity, different from other gradients **/
137 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
138
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800139 /** 85% opacity for black background **/
140 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
141
Tyler Gunnc5428972018-03-28 14:15:34 -0700142 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 private View mDialButton;
144 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800145 private View mEmergencyShortcutView;
146 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800148 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
149
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150 private ToneGenerator mToneGenerator;
151 private Object mToneGeneratorLock = new Object();
152
153 // determines if we want to playback local DTMF tones.
154 private boolean mDTMFToneEnabled;
155
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700156 private EmergencyActionGroup mEmergencyActionGroup;
157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 // close activity when screen turns off
159 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
160 @Override
161 public void onReceive(Context context, Intent intent) {
162 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700163 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700164 }
165 }
166 };
167
168 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
169
Lucas Dupineb9c5702017-05-10 16:57:09 -0700170 // Background gradient
171 private ColorExtractor mColorExtractor;
172 private GradientDrawable mBackgroundGradient;
173 private boolean mSupportsDarkText;
174
Tyler Gunnc5428972018-03-28 14:15:34 -0700175 private boolean mIsWfcEmergencyCallingWarningEnabled;
176 private float mDefaultDigitsTextSize;
177
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800178 private boolean mAreEmergencyDialerShortcutsEnabled;
179
Billy Chi17ec2282018-06-15 19:00:15 +0800180 /** Key of emergency information user name */
181 private static final String KEY_EMERGENCY_INFO_NAME = "name";
182
183 /** Authority of emergency information */
184 private static final String AUTHORITY = "com.android.emergency.info.name";
185
186 /** Content path of emergency information name */
187 private static final String CONTENT_PATH = "name";
188
189 /** Content URI of emergency information */
190 private static final Uri CONTENT_URI = new Uri.Builder()
191 .scheme(ContentResolver.SCHEME_CONTENT)
192 .authority(AUTHORITY)
193 .path(CONTENT_PATH)
194 .build();
195
196 /** ContentObserver for monitoring emergency info name changes */
197 private ContentObserver mEmergencyInfoNameObserver;
198
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199 @Override
200 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
201 // Do nothing
202 }
203
204 @Override
205 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700206 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700207 }
208
209 @Override
210 public void afterTextChanged(Editable input) {
211 // Check for special sequences, in particular the "**04" or "**05"
212 // sequences that allow you to enter PIN or PUK-related codes.
213 //
214 // But note we *don't* allow most other special sequences here,
215 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
216 // since those shouldn't be available if the device is locked.
217 //
218 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
219 // here, not the regular handleChars() method.
220 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
221 // A special sequence was entered, clear the digits
222 mDigits.getText().clear();
223 }
224
225 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800226 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 }
228
229 @Override
230 protected void onCreate(Bundle icicle) {
231 super.onCreate(icicle);
232
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800234 setShowWhenLocked(true);
235 // Allow turning screen on
236 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800238 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
239 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
240
Lucas Dupineb9c5702017-05-10 16:57:09 -0700241 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800242
243 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
244 // And the background color is black with 85% opacity.
245 if (mAreEmergencyDialerShortcutsEnabled) {
246 updateTheme(false);
247 } else {
248 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
249 ColorExtractor.TYPE_EXTRA_DARK);
250 updateTheme(lockScreenColors.supportsDarkText());
251 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700252
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 setContentView(R.layout.emergency_dialer);
254
Tyler Gunnc5428972018-03-28 14:15:34 -0700255 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700256 mDigits.setKeyListener(DialerKeyListener.getInstance());
257 mDigits.setOnClickListener(this);
258 mDigits.setOnKeyListener(this);
259 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700260 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700261 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 maybeAddNumberFormatting();
263
Lucas Dupineb9c5702017-05-10 16:57:09 -0700264 mBackgroundGradient = new GradientDrawable(this);
265 Point displaySize = new Point();
266 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
267 .getDefaultDisplay().getSize(displaySize);
268 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800269 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
270 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700271 getWindow().setBackgroundDrawable(mBackgroundGradient);
272
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 // Check for the presence of the keypad
274 View view = findViewById(R.id.one);
275 if (view != null) {
276 setupKeypad();
277 }
278
279 mDelete = findViewById(R.id.deleteButton);
280 mDelete.setOnClickListener(this);
281 mDelete.setOnLongClickListener(this);
282
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700283 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284
285 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700286 // Read carrier config through the public API because PhoneGlobals is not available when we
287 // run as a secondary user.
288 CarrierConfigManager configMgr =
289 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
290 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800291 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700292
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700293 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 mDialButton.setOnClickListener(this);
295 } else {
296 mDialButton.setVisibility(View.GONE);
297 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700298 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
299 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
300 maybeShowWfcEmergencyCallingWarning();
301
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700302 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303
304 if (icicle != null) {
305 super.onRestoreInstanceState(icicle);
306 }
307
308 // Extract phone number from intent
309 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700310 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
312 if (number != null) {
313 mDigits.setText(number);
314 }
315 }
316
317 // if the mToneGenerator creation fails, just continue without it. It is
318 // a local audio signal, and is not as important as the dtmf tone itself.
319 synchronized (mToneGeneratorLock) {
320 if (mToneGenerator == null) {
321 try {
322 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
323 } catch (RuntimeException e) {
324 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
325 mToneGenerator = null;
326 }
327 }
328 }
329
330 final IntentFilter intentFilter = new IntentFilter();
331 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
332 registerReceiver(mBroadcastReceiver, intentFilter);
333
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700334 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800335
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800336 if (mAreEmergencyDialerShortcutsEnabled) {
337 setupEmergencyShortcutsView();
338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 }
340
341 @Override
342 protected void onDestroy() {
343 super.onDestroy();
344 synchronized (mToneGeneratorLock) {
345 if (mToneGenerator != null) {
346 mToneGenerator.release();
347 mToneGenerator = null;
348 }
349 }
350 unregisterReceiver(mBroadcastReceiver);
Billy Chi17ec2282018-06-15 19:00:15 +0800351 if (mEmergencyInfoNameObserver != null) {
352 getContentResolver().unregisterContentObserver(mEmergencyInfoNameObserver);
353 mEmergencyInfoNameObserver = null;
354 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 }
356
357 @Override
358 protected void onRestoreInstanceState(Bundle icicle) {
359 mLastNumber = icicle.getString(LAST_NUMBER);
360 }
361
362 @Override
363 protected void onSaveInstanceState(Bundle outState) {
364 super.onSaveInstanceState(outState);
365 outState.putString(LAST_NUMBER, mLastNumber);
366 }
367
368 /**
369 * Explicitly turn off number formatting, since it gets in the way of the emergency
370 * number detector
371 */
372 protected void maybeAddNumberFormatting() {
373 // Do nothing.
374 }
375
376 @Override
377 protected void onPostCreate(Bundle savedInstanceState) {
378 super.onPostCreate(savedInstanceState);
379
380 // This can't be done in onCreate(), since the auto-restoring of the digits
381 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
382 // is called. This method will be called every time the activity is created, and
383 // will always happen after onRestoreSavedInstanceState().
384 mDigits.addTextChangedListener(this);
385 }
386
387 private void setupKeypad() {
388 // Setup the listeners for the buttons
389 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700390 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
391 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700392 }
393
394 View view = findViewById(R.id.zero);
395 view.setOnLongClickListener(this);
396 }
397
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800398 @Override
399 public void onBackPressed() {
400 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800401 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800402 // Otherwise, it would finish the activity.
403 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
404 && mDialpadView.getVisibility() == View.VISIBLE) {
405 switchView(mEmergencyShortcutView, mDialpadView, true);
406 return;
407 }
408 super.onBackPressed();
409 }
410
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700411 /**
412 * handle key events
413 */
414 @Override
415 public boolean onKeyDown(int keyCode, KeyEvent event) {
416 switch (keyCode) {
417 // Happen when there's a "Call" hard button.
418 case KeyEvent.KEYCODE_CALL: {
419 if (TextUtils.isEmpty(mDigits.getText().toString())) {
420 // if we are adding a call from the InCallScreen and the phone
421 // number entered is empty, we just close the dialer to expose
422 // the InCallScreen under it.
423 finish();
424 } else {
425 // otherwise, we place the call.
426 placeCall();
427 }
428 return true;
429 }
430 }
431 return super.onKeyDown(keyCode, event);
432 }
433
434 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700435 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700436 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
437 mDigits.onKeyDown(keyCode, event);
438 }
439
440 @Override
441 public boolean onKey(View view, int keyCode, KeyEvent event) {
442 switch (view.getId()) {
443 case R.id.digits:
444 // Happen when "Done" button of the IME is pressed. This can happen when this
445 // Activity is forced into landscape mode due to a desk dock.
446 if (keyCode == KeyEvent.KEYCODE_ENTER
447 && event.getAction() == KeyEvent.ACTION_UP) {
448 placeCall();
449 return true;
450 }
451 break;
452 }
453 return false;
454 }
455
456 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700457 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800458 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700459 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800460 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700461 return handled;
462 }
463
464 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800465 public void onConfirmClick(EmergencyShortcutButton button) {
466 if (button == null) return;
467
468 String phoneNumber = button.getPhoneNumber();
469
470 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800471 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800472 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
473 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
474 } else {
475 Log.d(LOG_TAG, "emergency number is empty");
476 }
477 }
478
479 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700480 public void onClick(View view) {
481 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700482 case R.id.deleteButton: {
483 keyPressed(KeyEvent.KEYCODE_DEL);
484 return;
485 }
486 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700487 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700488 placeCall();
489 return;
490 }
491 case R.id.digits: {
492 if (mDigits.length() != 0) {
493 mDigits.setCursorVisible(true);
494 }
495 return;
496 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800497 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800498 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800499 switchView(mDialpadView, mEmergencyShortcutView, true);
500 return;
501 }
502 case R.id.emergency_info_button: {
503 Intent intent = (Intent) view.getTag(R.id.tag_intent);
504 if (intent != null) {
505 startActivity(intent);
506 }
507 return;
508 }
Yorke Lee23a70732014-08-14 17:12:01 -0700509 }
510 }
511
512 @Override
513 public void onPressed(View view, boolean pressed) {
514 if (!pressed) {
515 return;
516 }
517 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700518 case R.id.one: {
519 playTone(ToneGenerator.TONE_DTMF_1);
520 keyPressed(KeyEvent.KEYCODE_1);
521 return;
522 }
523 case R.id.two: {
524 playTone(ToneGenerator.TONE_DTMF_2);
525 keyPressed(KeyEvent.KEYCODE_2);
526 return;
527 }
528 case R.id.three: {
529 playTone(ToneGenerator.TONE_DTMF_3);
530 keyPressed(KeyEvent.KEYCODE_3);
531 return;
532 }
533 case R.id.four: {
534 playTone(ToneGenerator.TONE_DTMF_4);
535 keyPressed(KeyEvent.KEYCODE_4);
536 return;
537 }
538 case R.id.five: {
539 playTone(ToneGenerator.TONE_DTMF_5);
540 keyPressed(KeyEvent.KEYCODE_5);
541 return;
542 }
543 case R.id.six: {
544 playTone(ToneGenerator.TONE_DTMF_6);
545 keyPressed(KeyEvent.KEYCODE_6);
546 return;
547 }
548 case R.id.seven: {
549 playTone(ToneGenerator.TONE_DTMF_7);
550 keyPressed(KeyEvent.KEYCODE_7);
551 return;
552 }
553 case R.id.eight: {
554 playTone(ToneGenerator.TONE_DTMF_8);
555 keyPressed(KeyEvent.KEYCODE_8);
556 return;
557 }
558 case R.id.nine: {
559 playTone(ToneGenerator.TONE_DTMF_9);
560 keyPressed(KeyEvent.KEYCODE_9);
561 return;
562 }
563 case R.id.zero: {
564 playTone(ToneGenerator.TONE_DTMF_0);
565 keyPressed(KeyEvent.KEYCODE_0);
566 return;
567 }
568 case R.id.pound: {
569 playTone(ToneGenerator.TONE_DTMF_P);
570 keyPressed(KeyEvent.KEYCODE_POUND);
571 return;
572 }
573 case R.id.star: {
574 playTone(ToneGenerator.TONE_DTMF_S);
575 keyPressed(KeyEvent.KEYCODE_STAR);
576 return;
577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700578 }
579 }
580
581 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 * called for long touch events
583 */
584 @Override
585 public boolean onLongClick(View view) {
586 int id = view.getId();
587 switch (id) {
588 case R.id.deleteButton: {
589 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700590 return true;
591 }
592 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700593 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700594 keyPressed(KeyEvent.KEYCODE_PLUS);
595 return true;
596 }
597 }
598 return false;
599 }
600
601 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700602 protected void onStart() {
603 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800604 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
605 // And set background color to black.
606 if (mAreEmergencyDialerShortcutsEnabled) {
607 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
608 updateTheme(false);
609 } else {
610 mColorExtractor.addOnColorsChangedListener(this);
611 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
612 ColorExtractor.TYPE_EXTRA_DARK);
613 // Do not animate when view isn't visible yet, just set an initial state.
614 mBackgroundGradient.setColors(lockScreenColors, false);
615 updateTheme(lockScreenColors.supportsDarkText());
616 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700617 }
618
619 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 protected void onResume() {
621 super.onResume();
622
623 // retrieve the DTMF tone play back setting.
624 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
625 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
626
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700627 // if the mToneGenerator creation fails, just continue without it. It is
628 // a local audio signal, and is not as important as the dtmf tone itself.
629 synchronized (mToneGeneratorLock) {
630 if (mToneGenerator == null) {
631 try {
632 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
633 TONE_RELATIVE_VOLUME);
634 } catch (RuntimeException e) {
635 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
636 mToneGenerator = null;
637 }
638 }
639 }
640
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700641 updateDialAndDeleteButtonStateEnabledAttr();
642 }
643
644 @Override
645 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 }
648
Lucas Dupineb9c5702017-05-10 16:57:09 -0700649 @Override
650 protected void onStop() {
651 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700652 mColorExtractor.removeOnColorsChangedListener(this);
653 }
654
655 /**
656 * Sets theme based on gradient colors
657 * @param supportsDarkText true if gradient supports dark text
658 */
659 private void updateTheme(boolean supportsDarkText) {
660 if (mSupportsDarkText == supportsDarkText) {
661 return;
662 }
663 mSupportsDarkText = supportsDarkText;
664
665 // We can't change themes after inflation, in this case we'll have to recreate
666 // the whole activity.
667 if (mBackgroundGradient != null) {
668 recreate();
669 return;
670 }
671
672 int vis = getWindow().getDecorView().getSystemUiVisibility();
673 if (supportsDarkText) {
674 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
675 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
676 setTheme(R.style.EmergencyDialerThemeDark);
677 } else {
678 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
679 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
680 setTheme(R.style.EmergencyDialerTheme);
681 }
682 getWindow().getDecorView().setSystemUiVisibility(vis);
683 }
684
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 /**
686 * place the call, but check to make sure it is a viable number.
687 */
688 private void placeCall() {
689 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900690
691 // Convert into emergency number according to emergency conversion map.
692 // If conversion map is not defined (this is default), this method does
693 // nothing and just returns input number.
694 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
695
Yorke Lee36bb2542014-06-05 08:09:52 -0700696 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700697 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
698
699 // place the call if it is a valid number
700 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
701 // There is no number entered.
702 playTone(ToneGenerator.TONE_PROP_NACK);
703 return;
704 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700705 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
706 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700707 } else {
708 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
709
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700710 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
711 }
Yorke Lee9b341512014-10-17 11:36:41 -0700712 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 }
714
715 /**
716 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
717 *
718 * The tone is played locally, using the audio stream for phone calls.
719 * Tones are played only if the "Audible touch tones" user preference
720 * is checked, and are NOT played if the device is in silent mode.
721 *
722 * @param tone a tone code from {@link ToneGenerator}
723 */
724 void playTone(int tone) {
725 // if local tone playback is disabled, just return.
726 if (!mDTMFToneEnabled) {
727 return;
728 }
729
730 // Also do nothing if the phone is in silent mode.
731 // We need to re-check the ringer mode for *every* playTone()
732 // call, rather than keeping a local flag that's updated in
733 // onResume(), since it's possible to toggle silent mode without
734 // leaving the current activity (via the ENDCALL-longpress menu.)
735 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
736 int ringerMode = audioManager.getRingerMode();
737 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
738 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
739 return;
740 }
741
742 synchronized (mToneGeneratorLock) {
743 if (mToneGenerator == null) {
744 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
745 return;
746 }
747
748 // Start the new tone (will stop any playing tone)
749 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
750 }
751 }
752
753 private CharSequence createErrorMessage(String number) {
754 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800755 String errorString = getString(R.string.dial_emergency_error, number);
756 int startingPosition = errorString.indexOf(number);
757 int endingPosition = startingPosition + number.length();
758 Spannable result = new SpannableString(errorString);
759 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
760 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 } else {
762 return getText(R.string.dial_emergency_empty_error).toString();
763 }
764 }
765
766 @Override
767 protected Dialog onCreateDialog(int id) {
768 AlertDialog dialog = null;
769 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
770 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700771 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700772 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
773 .setMessage(createErrorMessage(mLastNumber))
774 .setPositiveButton(R.string.ok, null)
775 .setCancelable(true).create();
776
777 // blur stuff behind the dialog
778 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700779 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700780 }
781 return dialog;
782 }
783
784 @Override
785 protected void onPrepareDialog(int id, Dialog dialog) {
786 super.onPrepareDialog(id, dialog);
787 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
788 AlertDialog alert = (AlertDialog) dialog;
789 alert.setMessage(createErrorMessage(mLastNumber));
790 }
791 }
792
Andrew Leed5631e82014-10-08 16:03:58 -0700793 @Override
794 public boolean onOptionsItemSelected(MenuItem item) {
795 final int itemId = item.getItemId();
796 if (itemId == android.R.id.home) {
797 onBackPressed();
798 return true;
799 }
800 return super.onOptionsItemSelected(item);
801 }
802
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 /**
804 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
805 */
806 private void updateDialAndDeleteButtonStateEnabledAttr() {
807 final boolean notEmpty = mDigits.length() != 0;
808
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700809 mDelete.setEnabled(notEmpty);
810 }
Yorke Lee91311662014-10-24 14:50:45 -0700811
812 /**
813 * Remove the digit just before the current position. Used by various long pressed callbacks
814 * to remove the digit that was populated as a result of the short click.
815 */
816 private void removePreviousDigitIfPossible() {
817 final int currentPosition = mDigits.getSelectionStart();
818 if (currentPosition > 0) {
819 mDigits.setSelection(currentPosition);
820 mDigits.getText().delete(currentPosition - 1, currentPosition);
821 }
822 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800823
824 /**
825 * Update the text-to-speech annotations in the edit field.
826 */
827 private void updateTtsSpans() {
828 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
829 mDigits.getText().removeSpan(o);
830 }
831 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
832 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700833
834 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700835 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700836 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700837 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
838 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700839 mBackgroundGradient.setColors(colors);
840 updateTheme(colors.supportsDarkText());
841 }
842 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700843
844 /**
845 * Where a carrier requires a warning that emergency calling is not available while on WFC,
846 * add hint text above the dial pad which warns the user of this case.
847 */
848 private void maybeShowWfcEmergencyCallingWarning() {
849 if (!mIsWfcEmergencyCallingWarningEnabled) {
850 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
851 return;
852 }
853
854 // Use an async task rather than calling into Telephony on UI thread.
855 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
856 @Override
857 protected Boolean doInBackground(Void... voids) {
858 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
859 boolean isWfcAvailable = tm.isWifiCallingAvailable();
860 ServiceState ss = tm.getServiceState();
861 boolean isCellAvailable =
862 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
863 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
864 + " isCellAvailable=" + isCellAvailable
865 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
866 return isWfcAvailable && !isCellAvailable;
867 }
868
869 @Override
870 protected void onPostExecute(Boolean result) {
871 if (result.booleanValue()) {
872 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
873 mDigits.setHint(R.string.dial_emergency_calling_not_available);
874 } else {
875 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
876 mDigits.setHint("");
877 }
878 maybeChangeHintSize();
879 }
880 };
881 showWfcWarningTask.execute((Void) null);
882 }
883
884 /**
885 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
886 * edit view and set the font size to a smaller size appropriate for the emergency calling
887 * warning.
888 */
889 private void maybeChangeHintSize() {
890 if (TextUtils.isEmpty(mDigits.getHint())
891 || !TextUtils.isEmpty(mDigits.getText().toString())) {
892 // No hint or there are dialed digits, so use default size.
893 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
894 // By default, the digits view auto-resizes to fit the text it contains, so
895 // enable that now.
896 mDigits.setResizeEnabled(true);
897 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
898 } else {
899 // Hint present and no dialed digits, set custom font size appropriate for the warning.
900 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
901 R.dimen.emergency_call_warning_size));
902 // Since we're populating this with a static text string, disable auto-resize.
903 mDigits.setResizeEnabled(false);
904 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
905 }
906 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800907
908 private void setupEmergencyShortcutsView() {
909 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
910 mDialpadView = findViewById(R.id.emergency_dialer);
911
912 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
913 dialpadButton.setOnClickListener(this);
914
915 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
916 emergencyInfoButton.setOnClickListener(this);
917
918 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
919 mEmergencyActionGroup.setVisibility(View.GONE);
920
921 // Setup dialpad title.
922 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
923 emergencyDialpadTitle.setVisibility(View.VISIBLE);
924
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800925 // TODO: Integrating emergency phone number table will get location information.
926 // Using null to present no location status.
927 setLocationInfo(null);
928
929 mEmergencyShortcutButtonList = new ArrayList<>();
930 setupEmergencyCallShortcutButton();
931
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800932 switchView(mEmergencyShortcutView, mDialpadView, false);
Billy Chi17ec2282018-06-15 19:00:15 +0800933
934 mEmergencyInfoNameObserver = new ContentObserver(new Handler()) {
935 @Override
936 public void onChange(boolean selfChange) {
937 super.onChange(selfChange);
938 getEmergencyInfoNameAsync();
939 }
940 };
941 // Register ContentProvider for monitoring emergency info name changes.
942 getContentResolver().registerContentObserver(CONTENT_URI, false,
943 mEmergencyInfoNameObserver);
944 // Query emergency info name.
945 getEmergencyInfoNameAsync();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800946 }
947
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800948 private void setLocationInfo(String country) {
949 final View locationInfo = findViewById(R.id.location_info);
950
951 if (TextUtils.isEmpty(country)) {
952 locationInfo.setVisibility(View.INVISIBLE);
953 } else {
954 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
955 location.setText(country);
956 locationInfo.setVisibility(View.VISIBLE);
957 }
958 }
959
960 // TODO: Integrate emergency phone number table.
961 // Using default layout(no location, phone number is 112, description is Emergency,
962 // and icon is cross shape) until integrating emergency phone number table.
963 private void setupEmergencyCallShortcutButton() {
964 final ViewGroup shortcutButtonContainer = findViewById(
965 R.id.emergency_shortcut_buttons_container);
966 shortcutButtonContainer.setClipToOutline(true);
967
968 final EmergencyShortcutButton button =
969 (EmergencyShortcutButton) getLayoutInflater().inflate(
970 R.layout.emergency_shortcut_button,
971 shortcutButtonContainer, false);
972
973 button.setPhoneNumber("112");
974 button.setPhoneDescription("Emergency");
975 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
976 button.setOnConfirmClickListener(this);
977
978 shortcutButtonContainer.addView(button);
979 mEmergencyShortcutButtonList.add(button);
980
981 //Set emergency number title for numerous buttons.
982 if (shortcutButtonContainer.getChildCount() > 1) {
983 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
984 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
985 }
986 }
987
988 /**
989 * Called by the activity before a touch event is dispatched to the view hierarchy.
990 */
991 private void onPreTouchEvent(MotionEvent event) {
992 mEmergencyActionGroup.onPreTouchEvent(event);
993
994 if (mEmergencyShortcutButtonList != null) {
995 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
996 button.onPreTouchEvent(event);
997 }
998 }
999 }
1000
1001 /**
1002 * Called by the activity after a touch event is dispatched to the view hierarchy.
1003 */
1004 private void onPostTouchEvent(MotionEvent event) {
1005 mEmergencyActionGroup.onPostTouchEvent(event);
1006
1007 if (mEmergencyShortcutButtonList != null) {
1008 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1009 button.onPostTouchEvent(event);
1010 }
1011 }
1012 }
1013
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001014 /**
1015 * Switch two view.
1016 *
1017 * @param displayView the view would be displayed.
1018 * @param hideView the view would be hidden.
1019 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1020 */
1021 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1022 if (displayView == null || hideView == null) {
1023 return;
1024 }
1025
1026 if (displayView.getVisibility() == View.VISIBLE) {
1027 return;
1028 }
1029
1030 if (hasAnimation) {
1031 crossfade(hideView, displayView);
1032 } else {
1033 hideView.setVisibility(View.GONE);
1034 displayView.setVisibility(View.VISIBLE);
1035 }
1036 }
1037
1038 /**
1039 * Fade out and fade in animation between two view transition.
1040 */
1041 private void crossfade(View fadeOutView, View fadeInView) {
1042 if (fadeOutView == null || fadeInView == null) {
1043 return;
1044 }
1045 final int shortAnimationDuration = getResources().getInteger(
1046 android.R.integer.config_shortAnimTime);
1047
1048 fadeInView.setAlpha(0f);
1049 fadeInView.setVisibility(View.VISIBLE);
1050
1051 fadeInView.animate()
1052 .alpha(1f)
1053 .setDuration(shortAnimationDuration)
1054 .setListener(null);
1055
1056 fadeOutView.animate()
1057 .alpha(0f)
1058 .setDuration(shortAnimationDuration)
1059 .setListener(new AnimatorListenerAdapter() {
1060 @Override
1061 public void onAnimationEnd(Animator animation) {
1062 fadeOutView.setVisibility(View.GONE);
1063 }
1064 });
1065 }
Billy Chi17ec2282018-06-15 19:00:15 +08001066
1067 /**
1068 * Get emergency info name from EmergencyInfo and then update EmergencyInfoGroup.
1069 */
1070 private void getEmergencyInfoNameAsync() {
1071 new AsyncTask<Void, Void, String>() {
1072 @Override
1073 protected String doInBackground(Void... params) {
1074 String name = "";
1075 try (Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,
1076 null)) {
1077 if (cursor != null && cursor.moveToFirst()) {
1078 int index = cursor.getColumnIndex(KEY_EMERGENCY_INFO_NAME);
1079 name = index > -1 ? cursor.getString(index) : "";
1080 }
1081 } catch (IllegalArgumentException ex) {
1082 Log.w(LOG_TAG, "getEmergencyInfoNameAsync failed", ex);
1083 }
1084 return name;
1085 }
1086
1087 @Override
1088 protected void onPostExecute(String result) {
1089 super.onPostExecute(result);
1090 if (!isFinishing() && !isDestroyed()) {
1091 // Update emergency info with emergency info name
Chihhang Chuangf8d33002018-07-02 11:08:50 +08001092 EmergencyInfoGroup group = findViewById(R.id.emergency_info_button);
Billy Chi17ec2282018-06-15 19:00:15 +08001093 if (group != null) {
1094 group.updateEmergencyInfo(result);
1095 }
1096 }
1097 }
1098 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
1099 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100}