blob: 61061d9bb4494c57ce1499bfc12b230d2dfa3284 [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;
Chihhang Chuanga26f07b2018-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 Gunn1acf54a2018-03-28 14:15:34 -070039import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.Bundle;
Billy Chi9614de82018-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 Gunnca7dfef2018-03-27 18:50:05 +000045import 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 Chi9614de82018-06-15 19:00:15 +080048import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070049import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070050import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-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 Gunn1acf54a2018-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 Chuang4f2b7812018-06-07 16:25:27 +080067import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.view.WindowManager;
Chihhang Chuang4f2b7812018-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 Gunn1acf54a2018-03-28 14:15:34 -070076import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang4f2b7812018-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 Chuangd301d9a2018-06-05 15:29:06 +080097 *
98 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-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 Chuangd301d9a2018-06-05 15:29:06 +0800101 * TODO item:
Chihhang Chuang4f2b7812018-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 Chuang4f2b7812018-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 Chuanga26f07b2018-07-02 11:08:50 +0800139 /** 85% opacity for black background **/
140 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
141
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700142 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143 private View mDialButton;
144 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800145 private View mEmergencyShortcutView;
146 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147
Chihhang Chuang4f2b7812018-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 Gunn1acf54a2018-03-28 14:15:34 -0700175 private boolean mIsWfcEmergencyCallingWarningEnabled;
176 private float mDefaultDigitsTextSize;
177
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800178 private boolean mAreEmergencyDialerShortcutsEnabled;
179
Billy Chi9614de82018-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 Gunn1acf54a2018-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.
234 WindowManager.LayoutParams lp = getWindow().getAttributes();
235 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700236
237 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700238 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700239 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
240
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700241 getWindow().setAttributes(lp);
242
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800243 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
244 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
245
Lucas Dupineb9c5702017-05-10 16:57:09 -0700246 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800247
248 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
249 // And the background color is black with 85% opacity.
250 if (mAreEmergencyDialerShortcutsEnabled) {
251 updateTheme(false);
252 } else {
253 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
254 ColorExtractor.TYPE_EXTRA_DARK);
255 updateTheme(lockScreenColors.supportsDarkText());
256 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 setContentView(R.layout.emergency_dialer);
259
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700260 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 mDigits.setKeyListener(DialerKeyListener.getInstance());
262 mDigits.setOnClickListener(this);
263 mDigits.setOnKeyListener(this);
264 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700265 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700266 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 maybeAddNumberFormatting();
268
Lucas Dupineb9c5702017-05-10 16:57:09 -0700269 mBackgroundGradient = new GradientDrawable(this);
270 Point displaySize = new Point();
271 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
272 .getDefaultDisplay().getSize(displaySize);
273 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800274 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
275 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700276 getWindow().setBackgroundDrawable(mBackgroundGradient);
277
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278 // Check for the presence of the keypad
279 View view = findViewById(R.id.one);
280 if (view != null) {
281 setupKeypad();
282 }
283
284 mDelete = findViewById(R.id.deleteButton);
285 mDelete.setOnClickListener(this);
286 mDelete.setOnLongClickListener(this);
287
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700288 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289
290 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700291 // Read carrier config through the public API because PhoneGlobals is not available when we
292 // run as a secondary user.
293 CarrierConfigManager configMgr =
294 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
295 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800296 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700297
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700298 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 mDialButton.setOnClickListener(this);
300 } else {
301 mDialButton.setVisibility(View.GONE);
302 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700303 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
304 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
305 maybeShowWfcEmergencyCallingWarning();
306
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700307 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308
309 if (icicle != null) {
310 super.onRestoreInstanceState(icicle);
311 }
312
313 // Extract phone number from intent
314 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700315 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
317 if (number != null) {
318 mDigits.setText(number);
319 }
320 }
321
322 // if the mToneGenerator creation fails, just continue without it. It is
323 // a local audio signal, and is not as important as the dtmf tone itself.
324 synchronized (mToneGeneratorLock) {
325 if (mToneGenerator == null) {
326 try {
327 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
328 } catch (RuntimeException e) {
329 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
330 mToneGenerator = null;
331 }
332 }
333 }
334
335 final IntentFilter intentFilter = new IntentFilter();
336 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
337 registerReceiver(mBroadcastReceiver, intentFilter);
338
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700339 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800340
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800341 if (mAreEmergencyDialerShortcutsEnabled) {
342 setupEmergencyShortcutsView();
343 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 }
345
346 @Override
347 protected void onDestroy() {
348 super.onDestroy();
349 synchronized (mToneGeneratorLock) {
350 if (mToneGenerator != null) {
351 mToneGenerator.release();
352 mToneGenerator = null;
353 }
354 }
355 unregisterReceiver(mBroadcastReceiver);
Billy Chi9614de82018-06-15 19:00:15 +0800356 if (mEmergencyInfoNameObserver != null) {
357 getContentResolver().unregisterContentObserver(mEmergencyInfoNameObserver);
358 mEmergencyInfoNameObserver = null;
359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 }
361
362 @Override
363 protected void onRestoreInstanceState(Bundle icicle) {
364 mLastNumber = icicle.getString(LAST_NUMBER);
365 }
366
367 @Override
368 protected void onSaveInstanceState(Bundle outState) {
369 super.onSaveInstanceState(outState);
370 outState.putString(LAST_NUMBER, mLastNumber);
371 }
372
373 /**
374 * Explicitly turn off number formatting, since it gets in the way of the emergency
375 * number detector
376 */
377 protected void maybeAddNumberFormatting() {
378 // Do nothing.
379 }
380
381 @Override
382 protected void onPostCreate(Bundle savedInstanceState) {
383 super.onPostCreate(savedInstanceState);
384
385 // This can't be done in onCreate(), since the auto-restoring of the digits
386 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
387 // is called. This method will be called every time the activity is created, and
388 // will always happen after onRestoreSavedInstanceState().
389 mDigits.addTextChangedListener(this);
390 }
391
392 private void setupKeypad() {
393 // Setup the listeners for the buttons
394 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700395 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
396 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700397 }
398
399 View view = findViewById(R.id.zero);
400 view.setOnLongClickListener(this);
401 }
402
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800403 @Override
404 public void onBackPressed() {
405 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800406 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800407 // Otherwise, it would finish the activity.
408 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
409 && mDialpadView.getVisibility() == View.VISIBLE) {
410 switchView(mEmergencyShortcutView, mDialpadView, true);
411 return;
412 }
413 super.onBackPressed();
414 }
415
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700416 /**
417 * handle key events
418 */
419 @Override
420 public boolean onKeyDown(int keyCode, KeyEvent event) {
421 switch (keyCode) {
422 // Happen when there's a "Call" hard button.
423 case KeyEvent.KEYCODE_CALL: {
424 if (TextUtils.isEmpty(mDigits.getText().toString())) {
425 // if we are adding a call from the InCallScreen and the phone
426 // number entered is empty, we just close the dialer to expose
427 // the InCallScreen under it.
428 finish();
429 } else {
430 // otherwise, we place the call.
431 placeCall();
432 }
433 return true;
434 }
435 }
436 return super.onKeyDown(keyCode, event);
437 }
438
439 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700440 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700441 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
442 mDigits.onKeyDown(keyCode, event);
443 }
444
445 @Override
446 public boolean onKey(View view, int keyCode, KeyEvent event) {
447 switch (view.getId()) {
448 case R.id.digits:
449 // Happen when "Done" button of the IME is pressed. This can happen when this
450 // Activity is forced into landscape mode due to a desk dock.
451 if (keyCode == KeyEvent.KEYCODE_ENTER
452 && event.getAction() == KeyEvent.ACTION_UP) {
453 placeCall();
454 return true;
455 }
456 break;
457 }
458 return false;
459 }
460
461 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700462 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800463 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700464 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800465 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700466 return handled;
467 }
468
469 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800470 public void onConfirmClick(EmergencyShortcutButton button) {
471 if (button == null) return;
472
473 String phoneNumber = button.getPhoneNumber();
474
475 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800476 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800477 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
478 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
479 } else {
480 Log.d(LOG_TAG, "emergency number is empty");
481 }
482 }
483
484 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700485 public void onClick(View view) {
486 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700487 case R.id.deleteButton: {
488 keyPressed(KeyEvent.KEYCODE_DEL);
489 return;
490 }
491 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700492 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700493 placeCall();
494 return;
495 }
496 case R.id.digits: {
497 if (mDigits.length() != 0) {
498 mDigits.setCursorVisible(true);
499 }
500 return;
501 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800502 case R.id.floating_action_button_dialpad: {
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800503 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800504 switchView(mDialpadView, mEmergencyShortcutView, true);
505 return;
506 }
507 case R.id.emergency_info_button: {
508 Intent intent = (Intent) view.getTag(R.id.tag_intent);
509 if (intent != null) {
510 startActivity(intent);
511 }
512 return;
513 }
Yorke Lee23a70732014-08-14 17:12:01 -0700514 }
515 }
516
517 @Override
518 public void onPressed(View view, boolean pressed) {
519 if (!pressed) {
520 return;
521 }
522 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700523 case R.id.one: {
524 playTone(ToneGenerator.TONE_DTMF_1);
525 keyPressed(KeyEvent.KEYCODE_1);
526 return;
527 }
528 case R.id.two: {
529 playTone(ToneGenerator.TONE_DTMF_2);
530 keyPressed(KeyEvent.KEYCODE_2);
531 return;
532 }
533 case R.id.three: {
534 playTone(ToneGenerator.TONE_DTMF_3);
535 keyPressed(KeyEvent.KEYCODE_3);
536 return;
537 }
538 case R.id.four: {
539 playTone(ToneGenerator.TONE_DTMF_4);
540 keyPressed(KeyEvent.KEYCODE_4);
541 return;
542 }
543 case R.id.five: {
544 playTone(ToneGenerator.TONE_DTMF_5);
545 keyPressed(KeyEvent.KEYCODE_5);
546 return;
547 }
548 case R.id.six: {
549 playTone(ToneGenerator.TONE_DTMF_6);
550 keyPressed(KeyEvent.KEYCODE_6);
551 return;
552 }
553 case R.id.seven: {
554 playTone(ToneGenerator.TONE_DTMF_7);
555 keyPressed(KeyEvent.KEYCODE_7);
556 return;
557 }
558 case R.id.eight: {
559 playTone(ToneGenerator.TONE_DTMF_8);
560 keyPressed(KeyEvent.KEYCODE_8);
561 return;
562 }
563 case R.id.nine: {
564 playTone(ToneGenerator.TONE_DTMF_9);
565 keyPressed(KeyEvent.KEYCODE_9);
566 return;
567 }
568 case R.id.zero: {
569 playTone(ToneGenerator.TONE_DTMF_0);
570 keyPressed(KeyEvent.KEYCODE_0);
571 return;
572 }
573 case R.id.pound: {
574 playTone(ToneGenerator.TONE_DTMF_P);
575 keyPressed(KeyEvent.KEYCODE_POUND);
576 return;
577 }
578 case R.id.star: {
579 playTone(ToneGenerator.TONE_DTMF_S);
580 keyPressed(KeyEvent.KEYCODE_STAR);
581 return;
582 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 }
584 }
585
586 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 * called for long touch events
588 */
589 @Override
590 public boolean onLongClick(View view) {
591 int id = view.getId();
592 switch (id) {
593 case R.id.deleteButton: {
594 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 return true;
596 }
597 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700598 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700599 keyPressed(KeyEvent.KEYCODE_PLUS);
600 return true;
601 }
602 }
603 return false;
604 }
605
606 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700607 protected void onStart() {
608 super.onStart();
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800609 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
610 // And set background color to black.
611 if (mAreEmergencyDialerShortcutsEnabled) {
612 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
613 updateTheme(false);
614 } else {
615 mColorExtractor.addOnColorsChangedListener(this);
616 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
617 ColorExtractor.TYPE_EXTRA_DARK);
618 // Do not animate when view isn't visible yet, just set an initial state.
619 mBackgroundGradient.setColors(lockScreenColors, false);
620 updateTheme(lockScreenColors.supportsDarkText());
621 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700622 }
623
624 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700625 protected void onResume() {
626 super.onResume();
627
628 // retrieve the DTMF tone play back setting.
629 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
630 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
631
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700632 // if the mToneGenerator creation fails, just continue without it. It is
633 // a local audio signal, and is not as important as the dtmf tone itself.
634 synchronized (mToneGeneratorLock) {
635 if (mToneGenerator == null) {
636 try {
637 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
638 TONE_RELATIVE_VOLUME);
639 } catch (RuntimeException e) {
640 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
641 mToneGenerator = null;
642 }
643 }
644 }
645
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700646 updateDialAndDeleteButtonStateEnabledAttr();
647 }
648
649 @Override
650 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700651 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700652 }
653
Lucas Dupineb9c5702017-05-10 16:57:09 -0700654 @Override
655 protected void onStop() {
656 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700657 mColorExtractor.removeOnColorsChangedListener(this);
658 }
659
660 /**
661 * Sets theme based on gradient colors
662 * @param supportsDarkText true if gradient supports dark text
663 */
664 private void updateTheme(boolean supportsDarkText) {
665 if (mSupportsDarkText == supportsDarkText) {
666 return;
667 }
668 mSupportsDarkText = supportsDarkText;
669
670 // We can't change themes after inflation, in this case we'll have to recreate
671 // the whole activity.
672 if (mBackgroundGradient != null) {
673 recreate();
674 return;
675 }
676
677 int vis = getWindow().getDecorView().getSystemUiVisibility();
678 if (supportsDarkText) {
679 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
680 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
681 setTheme(R.style.EmergencyDialerThemeDark);
682 } else {
683 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
684 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
685 setTheme(R.style.EmergencyDialerTheme);
686 }
687 getWindow().getDecorView().setSystemUiVisibility(vis);
688 }
689
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700690 /**
691 * place the call, but check to make sure it is a viable number.
692 */
693 private void placeCall() {
694 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900695
696 // Convert into emergency number according to emergency conversion map.
697 // If conversion map is not defined (this is default), this method does
698 // nothing and just returns input number.
699 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
700
Yorke Lee36bb2542014-06-05 08:09:52 -0700701 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700702 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
703
704 // place the call if it is a valid number
705 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
706 // There is no number entered.
707 playTone(ToneGenerator.TONE_PROP_NACK);
708 return;
709 }
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000710 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
711 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 } else {
713 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
714
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
716 }
Yorke Lee9b341512014-10-17 11:36:41 -0700717 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 }
719
720 /**
721 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
722 *
723 * The tone is played locally, using the audio stream for phone calls.
724 * Tones are played only if the "Audible touch tones" user preference
725 * is checked, and are NOT played if the device is in silent mode.
726 *
727 * @param tone a tone code from {@link ToneGenerator}
728 */
729 void playTone(int tone) {
730 // if local tone playback is disabled, just return.
731 if (!mDTMFToneEnabled) {
732 return;
733 }
734
735 // Also do nothing if the phone is in silent mode.
736 // We need to re-check the ringer mode for *every* playTone()
737 // call, rather than keeping a local flag that's updated in
738 // onResume(), since it's possible to toggle silent mode without
739 // leaving the current activity (via the ENDCALL-longpress menu.)
740 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
741 int ringerMode = audioManager.getRingerMode();
742 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
743 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
744 return;
745 }
746
747 synchronized (mToneGeneratorLock) {
748 if (mToneGenerator == null) {
749 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
750 return;
751 }
752
753 // Start the new tone (will stop any playing tone)
754 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
755 }
756 }
757
758 private CharSequence createErrorMessage(String number) {
759 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800760 String errorString = getString(R.string.dial_emergency_error, number);
761 int startingPosition = errorString.indexOf(number);
762 int endingPosition = startingPosition + number.length();
763 Spannable result = new SpannableString(errorString);
764 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
765 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700766 } else {
767 return getText(R.string.dial_emergency_empty_error).toString();
768 }
769 }
770
771 @Override
772 protected Dialog onCreateDialog(int id) {
773 AlertDialog dialog = null;
774 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
775 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700776 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
778 .setMessage(createErrorMessage(mLastNumber))
779 .setPositiveButton(R.string.ok, null)
780 .setCancelable(true).create();
781
782 // blur stuff behind the dialog
783 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700784 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700785 }
786 return dialog;
787 }
788
789 @Override
790 protected void onPrepareDialog(int id, Dialog dialog) {
791 super.onPrepareDialog(id, dialog);
792 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
793 AlertDialog alert = (AlertDialog) dialog;
794 alert.setMessage(createErrorMessage(mLastNumber));
795 }
796 }
797
Andrew Leed5631e82014-10-08 16:03:58 -0700798 @Override
799 public boolean onOptionsItemSelected(MenuItem item) {
800 final int itemId = item.getItemId();
801 if (itemId == android.R.id.home) {
802 onBackPressed();
803 return true;
804 }
805 return super.onOptionsItemSelected(item);
806 }
807
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700808 /**
809 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
810 */
811 private void updateDialAndDeleteButtonStateEnabledAttr() {
812 final boolean notEmpty = mDigits.length() != 0;
813
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700814 mDelete.setEnabled(notEmpty);
815 }
Yorke Lee91311662014-10-24 14:50:45 -0700816
817 /**
818 * Remove the digit just before the current position. Used by various long pressed callbacks
819 * to remove the digit that was populated as a result of the short click.
820 */
821 private void removePreviousDigitIfPossible() {
822 final int currentPosition = mDigits.getSelectionStart();
823 if (currentPosition > 0) {
824 mDigits.setSelection(currentPosition);
825 mDigits.getText().delete(currentPosition - 1, currentPosition);
826 }
827 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800828
829 /**
830 * Update the text-to-speech annotations in the edit field.
831 */
832 private void updateTtsSpans() {
833 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
834 mDigits.getText().removeSpan(o);
835 }
836 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
837 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700838
839 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700840 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700841 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700842 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
843 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700844 mBackgroundGradient.setColors(colors);
845 updateTheme(colors.supportsDarkText());
846 }
847 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700848
849 /**
850 * Where a carrier requires a warning that emergency calling is not available while on WFC,
851 * add hint text above the dial pad which warns the user of this case.
852 */
853 private void maybeShowWfcEmergencyCallingWarning() {
854 if (!mIsWfcEmergencyCallingWarningEnabled) {
855 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
856 return;
857 }
858
859 // Use an async task rather than calling into Telephony on UI thread.
860 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
861 @Override
862 protected Boolean doInBackground(Void... voids) {
863 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
864 boolean isWfcAvailable = tm.isWifiCallingAvailable();
865 ServiceState ss = tm.getServiceState();
866 boolean isCellAvailable =
867 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
868 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
869 + " isCellAvailable=" + isCellAvailable
870 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
871 return isWfcAvailable && !isCellAvailable;
872 }
873
874 @Override
875 protected void onPostExecute(Boolean result) {
876 if (result.booleanValue()) {
877 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
878 mDigits.setHint(R.string.dial_emergency_calling_not_available);
879 } else {
880 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
881 mDigits.setHint("");
882 }
883 maybeChangeHintSize();
884 }
885 };
886 showWfcWarningTask.execute((Void) null);
887 }
888
889 /**
890 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
891 * edit view and set the font size to a smaller size appropriate for the emergency calling
892 * warning.
893 */
894 private void maybeChangeHintSize() {
895 if (TextUtils.isEmpty(mDigits.getHint())
896 || !TextUtils.isEmpty(mDigits.getText().toString())) {
897 // No hint or there are dialed digits, so use default size.
898 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
899 // By default, the digits view auto-resizes to fit the text it contains, so
900 // enable that now.
901 mDigits.setResizeEnabled(true);
902 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
903 } else {
904 // Hint present and no dialed digits, set custom font size appropriate for the warning.
905 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
906 R.dimen.emergency_call_warning_size));
907 // Since we're populating this with a static text string, disable auto-resize.
908 mDigits.setResizeEnabled(false);
909 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
910 }
911 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800912
913 private void setupEmergencyShortcutsView() {
914 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
915 mDialpadView = findViewById(R.id.emergency_dialer);
916
917 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
918 dialpadButton.setOnClickListener(this);
919
920 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
921 emergencyInfoButton.setOnClickListener(this);
922
923 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
924 mEmergencyActionGroup.setVisibility(View.GONE);
925
926 // Setup dialpad title.
927 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
928 emergencyDialpadTitle.setVisibility(View.VISIBLE);
929
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800930 // TODO: Integrating emergency phone number table will get location information.
931 // Using null to present no location status.
932 setLocationInfo(null);
933
934 mEmergencyShortcutButtonList = new ArrayList<>();
935 setupEmergencyCallShortcutButton();
936
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800937 switchView(mEmergencyShortcutView, mDialpadView, false);
Billy Chi9614de82018-06-15 19:00:15 +0800938
939 mEmergencyInfoNameObserver = new ContentObserver(new Handler()) {
940 @Override
941 public void onChange(boolean selfChange) {
942 super.onChange(selfChange);
943 getEmergencyInfoNameAsync();
944 }
945 };
946 // Register ContentProvider for monitoring emergency info name changes.
947 getContentResolver().registerContentObserver(CONTENT_URI, false,
948 mEmergencyInfoNameObserver);
949 // Query emergency info name.
950 getEmergencyInfoNameAsync();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800951 }
952
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800953 private void setLocationInfo(String country) {
954 final View locationInfo = findViewById(R.id.location_info);
955
956 if (TextUtils.isEmpty(country)) {
957 locationInfo.setVisibility(View.INVISIBLE);
958 } else {
959 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
960 location.setText(country);
961 locationInfo.setVisibility(View.VISIBLE);
962 }
963 }
964
965 // TODO: Integrate emergency phone number table.
966 // Using default layout(no location, phone number is 112, description is Emergency,
967 // and icon is cross shape) until integrating emergency phone number table.
968 private void setupEmergencyCallShortcutButton() {
969 final ViewGroup shortcutButtonContainer = findViewById(
970 R.id.emergency_shortcut_buttons_container);
971 shortcutButtonContainer.setClipToOutline(true);
972
973 final EmergencyShortcutButton button =
974 (EmergencyShortcutButton) getLayoutInflater().inflate(
975 R.layout.emergency_shortcut_button,
976 shortcutButtonContainer, false);
977
978 button.setPhoneNumber("112");
979 button.setPhoneDescription("Emergency");
980 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
981 button.setOnConfirmClickListener(this);
982
983 shortcutButtonContainer.addView(button);
984 mEmergencyShortcutButtonList.add(button);
985
986 //Set emergency number title for numerous buttons.
987 if (shortcutButtonContainer.getChildCount() > 1) {
988 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
989 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
990 }
991 }
992
993 /**
994 * Called by the activity before a touch event is dispatched to the view hierarchy.
995 */
996 private void onPreTouchEvent(MotionEvent event) {
997 mEmergencyActionGroup.onPreTouchEvent(event);
998
999 if (mEmergencyShortcutButtonList != null) {
1000 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1001 button.onPreTouchEvent(event);
1002 }
1003 }
1004 }
1005
1006 /**
1007 * Called by the activity after a touch event is dispatched to the view hierarchy.
1008 */
1009 private void onPostTouchEvent(MotionEvent event) {
1010 mEmergencyActionGroup.onPostTouchEvent(event);
1011
1012 if (mEmergencyShortcutButtonList != null) {
1013 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1014 button.onPostTouchEvent(event);
1015 }
1016 }
1017 }
1018
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001019 /**
1020 * Switch two view.
1021 *
1022 * @param displayView the view would be displayed.
1023 * @param hideView the view would be hidden.
1024 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1025 */
1026 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1027 if (displayView == null || hideView == null) {
1028 return;
1029 }
1030
1031 if (displayView.getVisibility() == View.VISIBLE) {
1032 return;
1033 }
1034
1035 if (hasAnimation) {
1036 crossfade(hideView, displayView);
1037 } else {
1038 hideView.setVisibility(View.GONE);
1039 displayView.setVisibility(View.VISIBLE);
1040 }
1041 }
1042
1043 /**
1044 * Fade out and fade in animation between two view transition.
1045 */
1046 private void crossfade(View fadeOutView, View fadeInView) {
1047 if (fadeOutView == null || fadeInView == null) {
1048 return;
1049 }
1050 final int shortAnimationDuration = getResources().getInteger(
1051 android.R.integer.config_shortAnimTime);
1052
1053 fadeInView.setAlpha(0f);
1054 fadeInView.setVisibility(View.VISIBLE);
1055
1056 fadeInView.animate()
1057 .alpha(1f)
1058 .setDuration(shortAnimationDuration)
1059 .setListener(null);
1060
1061 fadeOutView.animate()
1062 .alpha(0f)
1063 .setDuration(shortAnimationDuration)
1064 .setListener(new AnimatorListenerAdapter() {
1065 @Override
1066 public void onAnimationEnd(Animator animation) {
1067 fadeOutView.setVisibility(View.GONE);
1068 }
1069 });
1070 }
Billy Chi9614de82018-06-15 19:00:15 +08001071
1072 /**
1073 * Get emergency info name from EmergencyInfo and then update EmergencyInfoGroup.
1074 */
1075 private void getEmergencyInfoNameAsync() {
1076 new AsyncTask<Void, Void, String>() {
1077 @Override
1078 protected String doInBackground(Void... params) {
1079 String name = "";
1080 try (Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,
1081 null)) {
1082 if (cursor != null && cursor.moveToFirst()) {
1083 int index = cursor.getColumnIndex(KEY_EMERGENCY_INFO_NAME);
1084 name = index > -1 ? cursor.getString(index) : "";
1085 }
1086 } catch (IllegalArgumentException ex) {
1087 Log.w(LOG_TAG, "getEmergencyInfoNameAsync failed", ex);
1088 }
1089 return name;
1090 }
1091
1092 @Override
1093 protected void onPostExecute(String result) {
1094 super.onPostExecute(result);
1095 if (!isFinishing() && !isDestroyed()) {
1096 // Update emergency info with emergency info name
Chihhang Chuanga26f07b2018-07-02 11:08:50 +08001097 EmergencyInfoGroup group = findViewById(R.id.emergency_info_button);
Billy Chi9614de82018-06-15 19:00:15 +08001098 if (group != null) {
1099 group.updateEmergencyInfo(result);
1100 }
1101 }
1102 }
1103 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
1104 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105}