blob: 79928a6367f62985fdf995ebda93362363f4a568 [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;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
CY Cheng42873672018-06-15 21:20:10 +080031import android.database.DataSetObserver;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +080032import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070033import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
35import android.media.ToneGenerator;
36import android.net.Uri;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070037import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.provider.Settings;
Shaotang Lif27c74d2018-07-19 17:32:42 +080041import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000043import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070044import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.PhoneNumberUtils;
Billy Chi9614de82018-06-15 19:00:15 +080046import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070047import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070048import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070049import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070051import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080052import android.text.Spannable;
53import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.text.TextUtils;
55import android.text.TextWatcher;
56import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080057import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070059import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070060import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070062import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070063import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.view.View;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080065import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080067import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Lucas Dupinaf9e9912017-06-22 12:39:39 -070069import com.android.internal.colorextraction.ColorExtractor;
70import com.android.internal.colorextraction.ColorExtractor.GradientColors;
71import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070072import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070073import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070074import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080075import com.android.phone.ecc.CountryEccInfo;
76import com.android.phone.ecc.EccInfoHelper;
Leo Hsu8f1c6712018-07-10 11:37:52 +080077import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080079import java.util.ArrayList;
80import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080081import java.util.Locale;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080082
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083/**
84 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
85 *
86 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
87 * activity from apps/Contacts) that:
88 * 1. Allows ONLY emergency calls to be dialed
89 * 2. Disallows voicemail functionality
90 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
91 * activity to stay in front of the keyguard.
92 *
93 * TODO: Even though this is an ultra-simplified version of the normal
94 * dialer, there's still lots of code duplication between this class and
95 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
96 * moved into a shared base class that would live in the framework?
97 * Or could we figure out some way to move *this* class into apps/Contacts
98 * also?
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080099 *
100 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800101 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
102 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800103 * TODO item:
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800104 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 */
106public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700107 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800108 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
109 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110 // Keys used with onSaveInstanceState().
111 private static final String LAST_NUMBER = "lastNumber";
112
113 // Intent action for this activity.
114 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
115
116 // List of dialer button IDs.
117 private static final int[] DIALER_KEYS = new int[] {
118 R.id.one, R.id.two, R.id.three,
119 R.id.four, R.id.five, R.id.six,
120 R.id.seven, R.id.eight, R.id.nine,
121 R.id.star, R.id.zero, R.id.pound };
122
123 // Debug constants.
124 private static final boolean DBG = false;
125 private static final String LOG_TAG = "EmergencyDialer";
126
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700127 /** The length of DTMF tones in milliseconds */
128 private static final int TONE_LENGTH_MS = 150;
129
130 /** The DTMF tone volume relative to other sounds in the stream */
131 private static final int TONE_RELATIVE_VOLUME = 80;
132
133 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
134 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
135
136 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
137
Lucas Dupineb9c5702017-05-10 16:57:09 -0700138 /** 90% opacity, different from other gradients **/
139 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
140
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800141 /** 85% opacity for black background **/
142 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
143
CY Cheng42873672018-06-15 21:20:10 +0800144 /** Size limit of emergency shortcut buttons container. **/
145 private static final int SHORTCUT_SIZE_LIMIT = 3;
146
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700147 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700148 private View mDialButton;
149 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800150 private View mEmergencyShortcutView;
151 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
CY Cheng42873672018-06-15 21:20:10 +0800153 private EccInfoHelper mEccInfoHelper;
154
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800155 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800156 private EccShortcutAdapter mShortcutAdapter;
157 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800158
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159 private ToneGenerator mToneGenerator;
160 private Object mToneGeneratorLock = new Object();
161
162 // determines if we want to playback local DTMF tones.
163 private boolean mDTMFToneEnabled;
164
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700165 private EmergencyActionGroup mEmergencyActionGroup;
166
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167 // close activity when screen turns off
168 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
169 @Override
170 public void onReceive(Context context, Intent intent) {
171 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700172 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173 }
174 }
175 };
176
177 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
178
Lucas Dupineb9c5702017-05-10 16:57:09 -0700179 // Background gradient
180 private ColorExtractor mColorExtractor;
181 private GradientDrawable mBackgroundGradient;
182 private boolean mSupportsDarkText;
183
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700184 private boolean mIsWfcEmergencyCallingWarningEnabled;
185 private float mDefaultDigitsTextSize;
186
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800187 private boolean mAreEmergencyDialerShortcutsEnabled;
188
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 @Override
190 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
191 // Do nothing
192 }
193
194 @Override
195 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700196 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700197 }
198
199 @Override
200 public void afterTextChanged(Editable input) {
201 // Check for special sequences, in particular the "**04" or "**05"
202 // sequences that allow you to enter PIN or PUK-related codes.
203 //
204 // But note we *don't* allow most other special sequences here,
205 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
206 // since those shouldn't be available if the device is locked.
207 //
208 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
209 // here, not the regular handleChars() method.
210 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
211 // A special sequence was entered, clear the digits
212 mDigits.getText().clear();
213 }
214
215 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800216 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 }
218
219 @Override
220 protected void onCreate(Bundle icicle) {
221 super.onCreate(icicle);
222
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 // Allow this activity to be displayed in front of the keyguard / lockscreen.
224 WindowManager.LayoutParams lp = getWindow().getAttributes();
225 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700226
227 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700228 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700229 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
230
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231 getWindow().setAttributes(lp);
232
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800233 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
234 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng42873672018-06-15 21:20:10 +0800235 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
236 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800237
Lucas Dupineb9c5702017-05-10 16:57:09 -0700238 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800239
240 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
241 // And the background color is black with 85% opacity.
242 if (mAreEmergencyDialerShortcutsEnabled) {
243 updateTheme(false);
244 } else {
245 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
246 ColorExtractor.TYPE_EXTRA_DARK);
247 updateTheme(lockScreenColors.supportsDarkText());
248 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700249
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 setContentView(R.layout.emergency_dialer);
251
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700252 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 mDigits.setKeyListener(DialerKeyListener.getInstance());
254 mDigits.setOnClickListener(this);
255 mDigits.setOnKeyListener(this);
256 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700257 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700258 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 maybeAddNumberFormatting();
260
Lucas Dupineb9c5702017-05-10 16:57:09 -0700261 mBackgroundGradient = new GradientDrawable(this);
262 Point displaySize = new Point();
263 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
264 .getDefaultDisplay().getSize(displaySize);
265 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800266 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
267 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700268 getWindow().setBackgroundDrawable(mBackgroundGradient);
269
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 // Check for the presence of the keypad
271 View view = findViewById(R.id.one);
272 if (view != null) {
273 setupKeypad();
274 }
275
276 mDelete = findViewById(R.id.deleteButton);
277 mDelete.setOnClickListener(this);
278 mDelete.setOnLongClickListener(this);
279
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700280 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281
282 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700283 // Read carrier config through the public API because PhoneGlobals is not available when we
284 // run as a secondary user.
285 CarrierConfigManager configMgr =
286 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
287 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800288 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700289
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700290 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700291 mDialButton.setOnClickListener(this);
292 } else {
293 mDialButton.setVisibility(View.GONE);
294 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700295 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
296 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
297 maybeShowWfcEmergencyCallingWarning();
298
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700299 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300
301 if (icicle != null) {
302 super.onRestoreInstanceState(icicle);
303 }
304
305 // Extract phone number from intent
306 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700307 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
309 if (number != null) {
310 mDigits.setText(number);
311 }
312 }
313
314 // if the mToneGenerator creation fails, just continue without it. It is
315 // a local audio signal, and is not as important as the dtmf tone itself.
316 synchronized (mToneGeneratorLock) {
317 if (mToneGenerator == null) {
318 try {
319 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
320 } catch (RuntimeException e) {
321 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
322 mToneGenerator = null;
323 }
324 }
325 }
326
327 final IntentFilter intentFilter = new IntentFilter();
328 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
329 registerReceiver(mBroadcastReceiver, intentFilter);
330
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700331 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800332
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800333 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu8f1c6712018-07-10 11:37:52 +0800334 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800335 setupEmergencyShortcutsView();
336 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 }
338
339 @Override
340 protected void onDestroy() {
341 super.onDestroy();
342 synchronized (mToneGeneratorLock) {
343 if (mToneGenerator != null) {
344 mToneGenerator.release();
345 mToneGenerator = null;
346 }
347 }
348 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800349 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
350 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
351 mShortcutDataSetObserver = null;
352 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700353 }
354
355 @Override
356 protected void onRestoreInstanceState(Bundle icicle) {
357 mLastNumber = icicle.getString(LAST_NUMBER);
358 }
359
360 @Override
361 protected void onSaveInstanceState(Bundle outState) {
362 super.onSaveInstanceState(outState);
363 outState.putString(LAST_NUMBER, mLastNumber);
364 }
365
366 /**
367 * Explicitly turn off number formatting, since it gets in the way of the emergency
368 * number detector
369 */
370 protected void maybeAddNumberFormatting() {
371 // Do nothing.
372 }
373
374 @Override
375 protected void onPostCreate(Bundle savedInstanceState) {
376 super.onPostCreate(savedInstanceState);
377
378 // This can't be done in onCreate(), since the auto-restoring of the digits
379 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
380 // is called. This method will be called every time the activity is created, and
381 // will always happen after onRestoreSavedInstanceState().
382 mDigits.addTextChangedListener(this);
383 }
384
385 private void setupKeypad() {
386 // Setup the listeners for the buttons
387 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700388 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
389 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 }
391
392 View view = findViewById(R.id.zero);
393 view.setOnLongClickListener(this);
394 }
395
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800396 @Override
397 public void onBackPressed() {
398 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800399 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800400 // Otherwise, it would finish the activity.
401 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
402 && mDialpadView.getVisibility() == View.VISIBLE) {
403 switchView(mEmergencyShortcutView, mDialpadView, true);
404 return;
405 }
406 super.onBackPressed();
407 }
408
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 /**
410 * handle key events
411 */
412 @Override
413 public boolean onKeyDown(int keyCode, KeyEvent event) {
414 switch (keyCode) {
415 // Happen when there's a "Call" hard button.
416 case KeyEvent.KEYCODE_CALL: {
417 if (TextUtils.isEmpty(mDigits.getText().toString())) {
418 // if we are adding a call from the InCallScreen and the phone
419 // number entered is empty, we just close the dialer to expose
420 // the InCallScreen under it.
421 finish();
422 } else {
423 // otherwise, we place the call.
424 placeCall();
425 }
426 return true;
427 }
428 }
429 return super.onKeyDown(keyCode, event);
430 }
431
432 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700433 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700434 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
435 mDigits.onKeyDown(keyCode, event);
436 }
437
438 @Override
439 public boolean onKey(View view, int keyCode, KeyEvent event) {
440 switch (view.getId()) {
441 case R.id.digits:
442 // Happen when "Done" button of the IME is pressed. This can happen when this
443 // Activity is forced into landscape mode due to a desk dock.
444 if (keyCode == KeyEvent.KEYCODE_ENTER
445 && event.getAction() == KeyEvent.ACTION_UP) {
446 placeCall();
447 return true;
448 }
449 break;
450 }
451 return false;
452 }
453
454 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700455 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800456 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700457 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800458 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700459 return handled;
460 }
461
462 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800463 public void onConfirmClick(EmergencyShortcutButton button) {
464 if (button == null) return;
465
466 String phoneNumber = button.getPhoneNumber();
467
468 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800469 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Lif27c74d2018-07-19 17:32:42 +0800470 Bundle extras = new Bundle();
471 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
472 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800473 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800474 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800475 } else {
476 Log.d(LOG_TAG, "emergency number is empty");
477 }
478 }
479
480 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700481 public void onClick(View view) {
482 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700483 case R.id.deleteButton: {
484 keyPressed(KeyEvent.KEYCODE_DEL);
485 return;
486 }
487 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700488 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700489 placeCall();
490 return;
491 }
492 case R.id.digits: {
493 if (mDigits.length() != 0) {
494 mDigits.setCursorVisible(true);
495 }
496 return;
497 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800498 case R.id.floating_action_button_dialpad: {
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800499 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800500 switchView(mDialpadView, mEmergencyShortcutView, true);
501 return;
502 }
503 case R.id.emergency_info_button: {
504 Intent intent = (Intent) view.getTag(R.id.tag_intent);
505 if (intent != null) {
506 startActivity(intent);
507 }
508 return;
509 }
Yorke Lee23a70732014-08-14 17:12:01 -0700510 }
511 }
512
513 @Override
514 public void onPressed(View view, boolean pressed) {
515 if (!pressed) {
516 return;
517 }
518 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700519 case R.id.one: {
520 playTone(ToneGenerator.TONE_DTMF_1);
521 keyPressed(KeyEvent.KEYCODE_1);
522 return;
523 }
524 case R.id.two: {
525 playTone(ToneGenerator.TONE_DTMF_2);
526 keyPressed(KeyEvent.KEYCODE_2);
527 return;
528 }
529 case R.id.three: {
530 playTone(ToneGenerator.TONE_DTMF_3);
531 keyPressed(KeyEvent.KEYCODE_3);
532 return;
533 }
534 case R.id.four: {
535 playTone(ToneGenerator.TONE_DTMF_4);
536 keyPressed(KeyEvent.KEYCODE_4);
537 return;
538 }
539 case R.id.five: {
540 playTone(ToneGenerator.TONE_DTMF_5);
541 keyPressed(KeyEvent.KEYCODE_5);
542 return;
543 }
544 case R.id.six: {
545 playTone(ToneGenerator.TONE_DTMF_6);
546 keyPressed(KeyEvent.KEYCODE_6);
547 return;
548 }
549 case R.id.seven: {
550 playTone(ToneGenerator.TONE_DTMF_7);
551 keyPressed(KeyEvent.KEYCODE_7);
552 return;
553 }
554 case R.id.eight: {
555 playTone(ToneGenerator.TONE_DTMF_8);
556 keyPressed(KeyEvent.KEYCODE_8);
557 return;
558 }
559 case R.id.nine: {
560 playTone(ToneGenerator.TONE_DTMF_9);
561 keyPressed(KeyEvent.KEYCODE_9);
562 return;
563 }
564 case R.id.zero: {
565 playTone(ToneGenerator.TONE_DTMF_0);
566 keyPressed(KeyEvent.KEYCODE_0);
567 return;
568 }
569 case R.id.pound: {
570 playTone(ToneGenerator.TONE_DTMF_P);
571 keyPressed(KeyEvent.KEYCODE_POUND);
572 return;
573 }
574 case R.id.star: {
575 playTone(ToneGenerator.TONE_DTMF_S);
576 keyPressed(KeyEvent.KEYCODE_STAR);
577 return;
578 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 }
580 }
581
582 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700583 * called for long touch events
584 */
585 @Override
586 public boolean onLongClick(View view) {
587 int id = view.getId();
588 switch (id) {
589 case R.id.deleteButton: {
590 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 return true;
592 }
593 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700594 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700595 keyPressed(KeyEvent.KEYCODE_PLUS);
596 return true;
597 }
598 }
599 return false;
600 }
601
602 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700603 protected void onStart() {
604 super.onStart();
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800605 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
606 // And set background color to black.
607 if (mAreEmergencyDialerShortcutsEnabled) {
608 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
609 updateTheme(false);
610 } else {
611 mColorExtractor.addOnColorsChangedListener(this);
612 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
613 ColorExtractor.TYPE_EXTRA_DARK);
614 // Do not animate when view isn't visible yet, just set an initial state.
615 mBackgroundGradient.setColors(lockScreenColors, false);
616 updateTheme(lockScreenColors.supportsDarkText());
617 }
CY Cheng42873672018-06-15 21:20:10 +0800618
619 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
620 final Context context = this;
621 mEccInfoHelper.getCountryEccInfoAsync(context,
622 new EccInfoHelper.CountryEccInfoResultCallback() {
623 @Override
624 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
625 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
626 + Rlog.pii(LOG_TAG, iso));
627 updateLocationAndEccInfo(iso, countryEccInfo);
628 }
629
630 @Override
631 public void onDetectCountryFailed() {
632 Log.w(LOG_TAG, "Cannot detect current country.");
633 updateLocationAndEccInfo(null, null);
634 }
635
636 @Override
637 public void onRetrieveCountryEccInfoFailed(String iso) {
638 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
639 + Rlog.pii(LOG_TAG, iso));
640 updateLocationAndEccInfo(iso, null);
641 }
642 });
643 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700644 }
645
646 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 protected void onResume() {
648 super.onResume();
649
650 // retrieve the DTMF tone play back setting.
651 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
652 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
653
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700654 // if the mToneGenerator creation fails, just continue without it. It is
655 // a local audio signal, and is not as important as the dtmf tone itself.
656 synchronized (mToneGeneratorLock) {
657 if (mToneGenerator == null) {
658 try {
659 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
660 TONE_RELATIVE_VOLUME);
661 } catch (RuntimeException e) {
662 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
663 mToneGenerator = null;
664 }
665 }
666 }
667
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 updateDialAndDeleteButtonStateEnabledAttr();
669 }
670
671 @Override
672 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700673 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700674 }
675
Lucas Dupineb9c5702017-05-10 16:57:09 -0700676 @Override
677 protected void onStop() {
678 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700679 mColorExtractor.removeOnColorsChangedListener(this);
680 }
681
682 /**
683 * Sets theme based on gradient colors
684 * @param supportsDarkText true if gradient supports dark text
685 */
686 private void updateTheme(boolean supportsDarkText) {
687 if (mSupportsDarkText == supportsDarkText) {
688 return;
689 }
690 mSupportsDarkText = supportsDarkText;
691
692 // We can't change themes after inflation, in this case we'll have to recreate
693 // the whole activity.
694 if (mBackgroundGradient != null) {
695 recreate();
696 return;
697 }
698
699 int vis = getWindow().getDecorView().getSystemUiVisibility();
700 if (supportsDarkText) {
701 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
702 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
703 setTheme(R.style.EmergencyDialerThemeDark);
704 } else {
705 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
706 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
707 setTheme(R.style.EmergencyDialerTheme);
708 }
709 getWindow().getDecorView().setSystemUiVisibility(vis);
710 }
711
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700712 /**
713 * place the call, but check to make sure it is a viable number.
714 */
715 private void placeCall() {
716 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900717
718 // Convert into emergency number according to emergency conversion map.
719 // If conversion map is not defined (this is default), this method does
720 // nothing and just returns input number.
721 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
722
Yorke Lee36bb2542014-06-05 08:09:52 -0700723 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700724 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
725
726 // place the call if it is a valid number
727 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
728 // There is no number entered.
729 playTone(ToneGenerator.TONE_PROP_NACK);
730 return;
731 }
Shaotang Lif27c74d2018-07-19 17:32:42 +0800732 Bundle extras = new Bundle();
733 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
734 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000735 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Lif27c74d2018-07-19 17:32:42 +0800736 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700737 } else {
738 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
739
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700740 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
741 }
Yorke Lee9b341512014-10-17 11:36:41 -0700742 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700743 }
744
745 /**
746 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
747 *
748 * The tone is played locally, using the audio stream for phone calls.
749 * Tones are played only if the "Audible touch tones" user preference
750 * is checked, and are NOT played if the device is in silent mode.
751 *
752 * @param tone a tone code from {@link ToneGenerator}
753 */
754 void playTone(int tone) {
755 // if local tone playback is disabled, just return.
756 if (!mDTMFToneEnabled) {
757 return;
758 }
759
760 // Also do nothing if the phone is in silent mode.
761 // We need to re-check the ringer mode for *every* playTone()
762 // call, rather than keeping a local flag that's updated in
763 // onResume(), since it's possible to toggle silent mode without
764 // leaving the current activity (via the ENDCALL-longpress menu.)
765 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
766 int ringerMode = audioManager.getRingerMode();
767 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
768 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
769 return;
770 }
771
772 synchronized (mToneGeneratorLock) {
773 if (mToneGenerator == null) {
774 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
775 return;
776 }
777
778 // Start the new tone (will stop any playing tone)
779 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
780 }
781 }
782
783 private CharSequence createErrorMessage(String number) {
784 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800785 String errorString = getString(R.string.dial_emergency_error, number);
786 int startingPosition = errorString.indexOf(number);
787 int endingPosition = startingPosition + number.length();
788 Spannable result = new SpannableString(errorString);
789 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
790 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 } else {
792 return getText(R.string.dial_emergency_empty_error).toString();
793 }
794 }
795
796 @Override
797 protected Dialog onCreateDialog(int id) {
798 AlertDialog dialog = null;
799 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
800 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700801 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700802 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
803 .setMessage(createErrorMessage(mLastNumber))
804 .setPositiveButton(R.string.ok, null)
805 .setCancelable(true).create();
806
807 // blur stuff behind the dialog
808 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700809 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700810 }
811 return dialog;
812 }
813
814 @Override
815 protected void onPrepareDialog(int id, Dialog dialog) {
816 super.onPrepareDialog(id, dialog);
817 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
818 AlertDialog alert = (AlertDialog) dialog;
819 alert.setMessage(createErrorMessage(mLastNumber));
820 }
821 }
822
Andrew Leed5631e82014-10-08 16:03:58 -0700823 @Override
824 public boolean onOptionsItemSelected(MenuItem item) {
825 final int itemId = item.getItemId();
826 if (itemId == android.R.id.home) {
827 onBackPressed();
828 return true;
829 }
830 return super.onOptionsItemSelected(item);
831 }
832
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 /**
834 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
835 */
836 private void updateDialAndDeleteButtonStateEnabledAttr() {
837 final boolean notEmpty = mDigits.length() != 0;
838
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 mDelete.setEnabled(notEmpty);
840 }
Yorke Lee91311662014-10-24 14:50:45 -0700841
842 /**
843 * Remove the digit just before the current position. Used by various long pressed callbacks
844 * to remove the digit that was populated as a result of the short click.
845 */
846 private void removePreviousDigitIfPossible() {
847 final int currentPosition = mDigits.getSelectionStart();
848 if (currentPosition > 0) {
849 mDigits.setSelection(currentPosition);
850 mDigits.getText().delete(currentPosition - 1, currentPosition);
851 }
852 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800853
854 /**
855 * Update the text-to-speech annotations in the edit field.
856 */
857 private void updateTtsSpans() {
858 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
859 mDigits.getText().removeSpan(o);
860 }
861 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
862 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700863
864 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700865 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700866 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700867 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
868 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700869 mBackgroundGradient.setColors(colors);
870 updateTheme(colors.supportsDarkText());
871 }
872 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700873
874 /**
875 * Where a carrier requires a warning that emergency calling is not available while on WFC,
876 * add hint text above the dial pad which warns the user of this case.
877 */
878 private void maybeShowWfcEmergencyCallingWarning() {
879 if (!mIsWfcEmergencyCallingWarningEnabled) {
880 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
881 return;
882 }
883
884 // Use an async task rather than calling into Telephony on UI thread.
885 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
886 @Override
887 protected Boolean doInBackground(Void... voids) {
888 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
889 boolean isWfcAvailable = tm.isWifiCallingAvailable();
890 ServiceState ss = tm.getServiceState();
891 boolean isCellAvailable =
892 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
893 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
894 + " isCellAvailable=" + isCellAvailable
895 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
896 return isWfcAvailable && !isCellAvailable;
897 }
898
899 @Override
900 protected void onPostExecute(Boolean result) {
901 if (result.booleanValue()) {
902 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
903 mDigits.setHint(R.string.dial_emergency_calling_not_available);
904 } else {
905 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
906 mDigits.setHint("");
907 }
908 maybeChangeHintSize();
909 }
910 };
911 showWfcWarningTask.execute((Void) null);
912 }
913
914 /**
915 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
916 * edit view and set the font size to a smaller size appropriate for the emergency calling
917 * warning.
918 */
919 private void maybeChangeHintSize() {
920 if (TextUtils.isEmpty(mDigits.getHint())
921 || !TextUtils.isEmpty(mDigits.getText().toString())) {
922 // No hint or there are dialed digits, so use default size.
923 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
924 // By default, the digits view auto-resizes to fit the text it contains, so
925 // enable that now.
926 mDigits.setResizeEnabled(true);
927 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
928 } else {
929 // Hint present and no dialed digits, set custom font size appropriate for the warning.
930 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
931 R.dimen.emergency_call_warning_size));
932 // Since we're populating this with a static text string, disable auto-resize.
933 mDigits.setResizeEnabled(false);
934 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
935 }
936 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800937
938 private void setupEmergencyShortcutsView() {
939 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
940 mDialpadView = findViewById(R.id.emergency_dialer);
941
942 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
943 dialpadButton.setOnClickListener(this);
944
945 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
946 emergencyInfoButton.setOnClickListener(this);
947
948 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
949 mEmergencyActionGroup.setVisibility(View.GONE);
950
951 // Setup dialpad title.
952 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
953 emergencyDialpadTitle.setVisibility(View.VISIBLE);
954
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800955 mEmergencyShortcutButtonList = new ArrayList<>();
956 setupEmergencyCallShortcutButton();
957
CY Cheng42873672018-06-15 21:20:10 +0800958 updateLocationAndEccInfo(null, null);
959
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800960 switchView(mEmergencyShortcutView, mDialpadView, false);
961 }
962
CY Cheng42873672018-06-15 21:20:10 +0800963 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800964 final View locationInfo = findViewById(R.id.location_info);
965
CY Cheng42873672018-06-15 21:20:10 +0800966 String countryName = null;
967 if (!TextUtils.isEmpty(countryIso)) {
968 Locale locale = Locale.getDefault();
969 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
970 .getDisplayCountry();
971 }
972 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800973 locationInfo.setVisibility(View.INVISIBLE);
974 } else {
975 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +0800976 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800977 locationInfo.setVisibility(View.VISIBLE);
978 }
979 }
980
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800981 private void setupEmergencyCallShortcutButton() {
982 final ViewGroup shortcutButtonContainer = findViewById(
983 R.id.emergency_shortcut_buttons_container);
984 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +0800985 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800986
CY Cheng42873672018-06-15 21:20:10 +0800987 mShortcutAdapter = new EccShortcutAdapter(this) {
988 @Override
989 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
990 CharSequence description, int iconRes) {
991 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
992 .inflate(R.layout.emergency_shortcut_button, parent, false);
993 button.setPhoneNumber(number);
994 button.setPhoneDescription(description);
995 button.setPhoneTypeIcon(iconRes);
996 button.setOnConfirmClickListener(EmergencyDialer.this);
997 return button;
998 }
999 };
1000 mShortcutDataSetObserver = new DataSetObserver() {
1001 @Override
1002 public void onChanged() {
1003 super.onChanged();
1004 updateLayout();
1005 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001006
CY Cheng42873672018-06-15 21:20:10 +08001007 @Override
1008 public void onInvalidated() {
1009 super.onInvalidated();
1010 updateLayout();
1011 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001012
CY Cheng42873672018-06-15 21:20:10 +08001013 private void updateLayout() {
1014 // clear previous added buttons
1015 shortcutButtonContainer.removeAllViews();
1016 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001017
CY Cheng42873672018-06-15 21:20:10 +08001018 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1019 EmergencyShortcutButton button = (EmergencyShortcutButton)
1020 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1021 mEmergencyShortcutButtonList.add(button);
1022 shortcutButtonContainer.addView(button);
1023 }
1024
1025 // update emergency numbers title for numerous buttons.
1026 if (mEmergencyShortcutButtonList.size() > 1) {
1027 emergencyNumberTitle.setText(getString(
1028 R.string.numerous_emergency_numbers_title));
1029 } else {
1030 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1031 }
1032 }
1033 };
1034 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1035 }
1036
1037 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1038 if (!isFinishing() && !isDestroyed()) {
1039 setLocationInfo(iso);
1040 if (mShortcutAdapter != null) {
1041 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1042 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001043 }
1044 }
1045
1046 /**
1047 * Called by the activity before a touch event is dispatched to the view hierarchy.
1048 */
1049 private void onPreTouchEvent(MotionEvent event) {
1050 mEmergencyActionGroup.onPreTouchEvent(event);
1051
1052 if (mEmergencyShortcutButtonList != null) {
1053 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1054 button.onPreTouchEvent(event);
1055 }
1056 }
1057 }
1058
1059 /**
1060 * Called by the activity after a touch event is dispatched to the view hierarchy.
1061 */
1062 private void onPostTouchEvent(MotionEvent event) {
1063 mEmergencyActionGroup.onPostTouchEvent(event);
1064
1065 if (mEmergencyShortcutButtonList != null) {
1066 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1067 button.onPostTouchEvent(event);
1068 }
1069 }
1070 }
1071
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001072 /**
1073 * Switch two view.
1074 *
1075 * @param displayView the view would be displayed.
1076 * @param hideView the view would be hidden.
1077 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1078 */
1079 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1080 if (displayView == null || hideView == null) {
1081 return;
1082 }
1083
1084 if (displayView.getVisibility() == View.VISIBLE) {
1085 return;
1086 }
1087
1088 if (hasAnimation) {
1089 crossfade(hideView, displayView);
1090 } else {
1091 hideView.setVisibility(View.GONE);
1092 displayView.setVisibility(View.VISIBLE);
1093 }
1094 }
1095
1096 /**
1097 * Fade out and fade in animation between two view transition.
1098 */
1099 private void crossfade(View fadeOutView, View fadeInView) {
1100 if (fadeOutView == null || fadeInView == null) {
1101 return;
1102 }
1103 final int shortAnimationDuration = getResources().getInteger(
1104 android.R.integer.config_shortAnimTime);
1105
1106 fadeInView.setAlpha(0f);
1107 fadeInView.setVisibility(View.VISIBLE);
1108
1109 fadeInView.animate()
1110 .alpha(1f)
1111 .setDuration(shortAnimationDuration)
1112 .setListener(null);
1113
1114 fadeOutView.animate()
1115 .alpha(0f)
1116 .setDuration(shortAnimationDuration)
1117 .setListener(new AnimatorListenerAdapter() {
1118 @Override
1119 public void onAnimationEnd(Animator animation) {
1120 fadeOutView.setVisibility(View.GONE);
1121 }
1122 });
1123 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124}