blob: c57615d5d07a0ea4b1abae23ce4ca8f0305fb98e [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;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070041import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000042import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.PhoneNumberUtils;
Billy Chi9614de82018-06-15 19:00:15 +080045import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070046import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070047import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070050import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080051import android.text.Spannable;
52import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.text.TextUtils;
54import android.text.TextWatcher;
55import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080056import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070058import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070059import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070061import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070062import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.View;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080064import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080066import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Lucas Dupinaf9e9912017-06-22 12:39:39 -070068import com.android.internal.colorextraction.ColorExtractor;
69import com.android.internal.colorextraction.ColorExtractor.GradientColors;
70import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070071import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070072import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070073import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080074import com.android.phone.ecc.CountryEccInfo;
75import com.android.phone.ecc.EccInfoHelper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080077import java.util.ArrayList;
78import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080079import java.util.Locale;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080080
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
CY Cheng42873672018-06-15 21:20:10 +0800142 /** Size limit of emergency shortcut buttons container. **/
143 private static final int SHORTCUT_SIZE_LIMIT = 3;
144
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700145 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 private View mDialButton;
147 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800148 private View mEmergencyShortcutView;
149 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
CY Cheng42873672018-06-15 21:20:10 +0800151 private EccInfoHelper mEccInfoHelper;
152
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800153 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800154 private EccShortcutAdapter mShortcutAdapter;
155 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 private ToneGenerator mToneGenerator;
158 private Object mToneGeneratorLock = new Object();
159
160 // determines if we want to playback local DTMF tones.
161 private boolean mDTMFToneEnabled;
162
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700163 private EmergencyActionGroup mEmergencyActionGroup;
164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 // close activity when screen turns off
166 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
167 @Override
168 public void onReceive(Context context, Intent intent) {
169 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700170 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 }
172 }
173 };
174
175 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
176
Lucas Dupineb9c5702017-05-10 16:57:09 -0700177 // Background gradient
178 private ColorExtractor mColorExtractor;
179 private GradientDrawable mBackgroundGradient;
180 private boolean mSupportsDarkText;
181
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700182 private boolean mIsWfcEmergencyCallingWarningEnabled;
183 private float mDefaultDigitsTextSize;
184
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800185 private boolean mAreEmergencyDialerShortcutsEnabled;
186
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 @Override
188 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
189 // Do nothing
190 }
191
192 @Override
193 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700194 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 }
196
197 @Override
198 public void afterTextChanged(Editable input) {
199 // Check for special sequences, in particular the "**04" or "**05"
200 // sequences that allow you to enter PIN or PUK-related codes.
201 //
202 // But note we *don't* allow most other special sequences here,
203 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
204 // since those shouldn't be available if the device is locked.
205 //
206 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
207 // here, not the regular handleChars() method.
208 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
209 // A special sequence was entered, clear the digits
210 mDigits.getText().clear();
211 }
212
213 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800214 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 }
216
217 @Override
218 protected void onCreate(Bundle icicle) {
219 super.onCreate(icicle);
220
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 // Allow this activity to be displayed in front of the keyguard / lockscreen.
222 WindowManager.LayoutParams lp = getWindow().getAttributes();
223 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700224
225 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700226 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700227 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
228
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229 getWindow().setAttributes(lp);
230
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800231 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
232 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng42873672018-06-15 21:20:10 +0800233 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
234 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800235
Lucas Dupineb9c5702017-05-10 16:57:09 -0700236 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800237
238 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
239 // And the background color is black with 85% opacity.
240 if (mAreEmergencyDialerShortcutsEnabled) {
241 updateTheme(false);
242 } else {
243 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
244 ColorExtractor.TYPE_EXTRA_DARK);
245 updateTheme(lockScreenColors.supportsDarkText());
246 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700247
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 setContentView(R.layout.emergency_dialer);
249
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700250 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 mDigits.setKeyListener(DialerKeyListener.getInstance());
252 mDigits.setOnClickListener(this);
253 mDigits.setOnKeyListener(this);
254 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700255 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700256 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 maybeAddNumberFormatting();
258
Lucas Dupineb9c5702017-05-10 16:57:09 -0700259 mBackgroundGradient = new GradientDrawable(this);
260 Point displaySize = new Point();
261 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
262 .getDefaultDisplay().getSize(displaySize);
263 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800264 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
265 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700266 getWindow().setBackgroundDrawable(mBackgroundGradient);
267
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700268 // Check for the presence of the keypad
269 View view = findViewById(R.id.one);
270 if (view != null) {
271 setupKeypad();
272 }
273
274 mDelete = findViewById(R.id.deleteButton);
275 mDelete.setOnClickListener(this);
276 mDelete.setOnLongClickListener(this);
277
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700278 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279
280 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700281 // Read carrier config through the public API because PhoneGlobals is not available when we
282 // run as a secondary user.
283 CarrierConfigManager configMgr =
284 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
285 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800286 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700287
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700288 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 mDialButton.setOnClickListener(this);
290 } else {
291 mDialButton.setVisibility(View.GONE);
292 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700293 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
294 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
295 maybeShowWfcEmergencyCallingWarning();
296
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700297 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298
299 if (icicle != null) {
300 super.onRestoreInstanceState(icicle);
301 }
302
303 // Extract phone number from intent
304 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700305 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
307 if (number != null) {
308 mDigits.setText(number);
309 }
310 }
311
312 // if the mToneGenerator creation fails, just continue without it. It is
313 // a local audio signal, and is not as important as the dtmf tone itself.
314 synchronized (mToneGeneratorLock) {
315 if (mToneGenerator == null) {
316 try {
317 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
318 } catch (RuntimeException e) {
319 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
320 mToneGenerator = null;
321 }
322 }
323 }
324
325 final IntentFilter intentFilter = new IntentFilter();
326 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
327 registerReceiver(mBroadcastReceiver, intentFilter);
328
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700329 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800330
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800331 if (mAreEmergencyDialerShortcutsEnabled) {
332 setupEmergencyShortcutsView();
333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 }
335
336 @Override
337 protected void onDestroy() {
338 super.onDestroy();
339 synchronized (mToneGeneratorLock) {
340 if (mToneGenerator != null) {
341 mToneGenerator.release();
342 mToneGenerator = null;
343 }
344 }
345 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800346 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
347 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
348 mShortcutDataSetObserver = null;
349 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 }
351
352 @Override
353 protected void onRestoreInstanceState(Bundle icicle) {
354 mLastNumber = icicle.getString(LAST_NUMBER);
355 }
356
357 @Override
358 protected void onSaveInstanceState(Bundle outState) {
359 super.onSaveInstanceState(outState);
360 outState.putString(LAST_NUMBER, mLastNumber);
361 }
362
363 /**
364 * Explicitly turn off number formatting, since it gets in the way of the emergency
365 * number detector
366 */
367 protected void maybeAddNumberFormatting() {
368 // Do nothing.
369 }
370
371 @Override
372 protected void onPostCreate(Bundle savedInstanceState) {
373 super.onPostCreate(savedInstanceState);
374
375 // This can't be done in onCreate(), since the auto-restoring of the digits
376 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
377 // is called. This method will be called every time the activity is created, and
378 // will always happen after onRestoreSavedInstanceState().
379 mDigits.addTextChangedListener(this);
380 }
381
382 private void setupKeypad() {
383 // Setup the listeners for the buttons
384 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700385 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
386 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700387 }
388
389 View view = findViewById(R.id.zero);
390 view.setOnLongClickListener(this);
391 }
392
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800393 @Override
394 public void onBackPressed() {
395 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800396 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800397 // Otherwise, it would finish the activity.
398 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
399 && mDialpadView.getVisibility() == View.VISIBLE) {
400 switchView(mEmergencyShortcutView, mDialpadView, true);
401 return;
402 }
403 super.onBackPressed();
404 }
405
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 /**
407 * handle key events
408 */
409 @Override
410 public boolean onKeyDown(int keyCode, KeyEvent event) {
411 switch (keyCode) {
412 // Happen when there's a "Call" hard button.
413 case KeyEvent.KEYCODE_CALL: {
414 if (TextUtils.isEmpty(mDigits.getText().toString())) {
415 // if we are adding a call from the InCallScreen and the phone
416 // number entered is empty, we just close the dialer to expose
417 // the InCallScreen under it.
418 finish();
419 } else {
420 // otherwise, we place the call.
421 placeCall();
422 }
423 return true;
424 }
425 }
426 return super.onKeyDown(keyCode, event);
427 }
428
429 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700430 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700431 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
432 mDigits.onKeyDown(keyCode, event);
433 }
434
435 @Override
436 public boolean onKey(View view, int keyCode, KeyEvent event) {
437 switch (view.getId()) {
438 case R.id.digits:
439 // Happen when "Done" button of the IME is pressed. This can happen when this
440 // Activity is forced into landscape mode due to a desk dock.
441 if (keyCode == KeyEvent.KEYCODE_ENTER
442 && event.getAction() == KeyEvent.ACTION_UP) {
443 placeCall();
444 return true;
445 }
446 break;
447 }
448 return false;
449 }
450
451 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700452 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800453 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700454 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800455 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700456 return handled;
457 }
458
459 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800460 public void onConfirmClick(EmergencyShortcutButton button) {
461 if (button == null) return;
462
463 String phoneNumber = button.getPhoneNumber();
464
465 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800466 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800467 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
468 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
469 } else {
470 Log.d(LOG_TAG, "emergency number is empty");
471 }
472 }
473
474 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700475 public void onClick(View view) {
476 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700477 case R.id.deleteButton: {
478 keyPressed(KeyEvent.KEYCODE_DEL);
479 return;
480 }
481 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700482 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700483 placeCall();
484 return;
485 }
486 case R.id.digits: {
487 if (mDigits.length() != 0) {
488 mDigits.setCursorVisible(true);
489 }
490 return;
491 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800492 case R.id.floating_action_button_dialpad: {
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800493 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800494 switchView(mDialpadView, mEmergencyShortcutView, true);
495 return;
496 }
497 case R.id.emergency_info_button: {
498 Intent intent = (Intent) view.getTag(R.id.tag_intent);
499 if (intent != null) {
500 startActivity(intent);
501 }
502 return;
503 }
Yorke Lee23a70732014-08-14 17:12:01 -0700504 }
505 }
506
507 @Override
508 public void onPressed(View view, boolean pressed) {
509 if (!pressed) {
510 return;
511 }
512 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700513 case R.id.one: {
514 playTone(ToneGenerator.TONE_DTMF_1);
515 keyPressed(KeyEvent.KEYCODE_1);
516 return;
517 }
518 case R.id.two: {
519 playTone(ToneGenerator.TONE_DTMF_2);
520 keyPressed(KeyEvent.KEYCODE_2);
521 return;
522 }
523 case R.id.three: {
524 playTone(ToneGenerator.TONE_DTMF_3);
525 keyPressed(KeyEvent.KEYCODE_3);
526 return;
527 }
528 case R.id.four: {
529 playTone(ToneGenerator.TONE_DTMF_4);
530 keyPressed(KeyEvent.KEYCODE_4);
531 return;
532 }
533 case R.id.five: {
534 playTone(ToneGenerator.TONE_DTMF_5);
535 keyPressed(KeyEvent.KEYCODE_5);
536 return;
537 }
538 case R.id.six: {
539 playTone(ToneGenerator.TONE_DTMF_6);
540 keyPressed(KeyEvent.KEYCODE_6);
541 return;
542 }
543 case R.id.seven: {
544 playTone(ToneGenerator.TONE_DTMF_7);
545 keyPressed(KeyEvent.KEYCODE_7);
546 return;
547 }
548 case R.id.eight: {
549 playTone(ToneGenerator.TONE_DTMF_8);
550 keyPressed(KeyEvent.KEYCODE_8);
551 return;
552 }
553 case R.id.nine: {
554 playTone(ToneGenerator.TONE_DTMF_9);
555 keyPressed(KeyEvent.KEYCODE_9);
556 return;
557 }
558 case R.id.zero: {
559 playTone(ToneGenerator.TONE_DTMF_0);
560 keyPressed(KeyEvent.KEYCODE_0);
561 return;
562 }
563 case R.id.pound: {
564 playTone(ToneGenerator.TONE_DTMF_P);
565 keyPressed(KeyEvent.KEYCODE_POUND);
566 return;
567 }
568 case R.id.star: {
569 playTone(ToneGenerator.TONE_DTMF_S);
570 keyPressed(KeyEvent.KEYCODE_STAR);
571 return;
572 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700573 }
574 }
575
576 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700577 * called for long touch events
578 */
579 @Override
580 public boolean onLongClick(View view) {
581 int id = view.getId();
582 switch (id) {
583 case R.id.deleteButton: {
584 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700585 return true;
586 }
587 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700588 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700589 keyPressed(KeyEvent.KEYCODE_PLUS);
590 return true;
591 }
592 }
593 return false;
594 }
595
596 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700597 protected void onStart() {
598 super.onStart();
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800599 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
600 // And set background color to black.
601 if (mAreEmergencyDialerShortcutsEnabled) {
602 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
603 updateTheme(false);
604 } else {
605 mColorExtractor.addOnColorsChangedListener(this);
606 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
607 ColorExtractor.TYPE_EXTRA_DARK);
608 // Do not animate when view isn't visible yet, just set an initial state.
609 mBackgroundGradient.setColors(lockScreenColors, false);
610 updateTheme(lockScreenColors.supportsDarkText());
611 }
CY Cheng42873672018-06-15 21:20:10 +0800612
613 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
614 final Context context = this;
615 mEccInfoHelper.getCountryEccInfoAsync(context,
616 new EccInfoHelper.CountryEccInfoResultCallback() {
617 @Override
618 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
619 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
620 + Rlog.pii(LOG_TAG, iso));
621 updateLocationAndEccInfo(iso, countryEccInfo);
622 }
623
624 @Override
625 public void onDetectCountryFailed() {
626 Log.w(LOG_TAG, "Cannot detect current country.");
627 updateLocationAndEccInfo(null, null);
628 }
629
630 @Override
631 public void onRetrieveCountryEccInfoFailed(String iso) {
632 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
633 + Rlog.pii(LOG_TAG, iso));
634 updateLocationAndEccInfo(iso, null);
635 }
636 });
637 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700638 }
639
640 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700641 protected void onResume() {
642 super.onResume();
643
644 // retrieve the DTMF tone play back setting.
645 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
646 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
647
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700648 // if the mToneGenerator creation fails, just continue without it. It is
649 // a local audio signal, and is not as important as the dtmf tone itself.
650 synchronized (mToneGeneratorLock) {
651 if (mToneGenerator == null) {
652 try {
653 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
654 TONE_RELATIVE_VOLUME);
655 } catch (RuntimeException e) {
656 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
657 mToneGenerator = null;
658 }
659 }
660 }
661
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 updateDialAndDeleteButtonStateEnabledAttr();
663 }
664
665 @Override
666 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700668 }
669
Lucas Dupineb9c5702017-05-10 16:57:09 -0700670 @Override
671 protected void onStop() {
672 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700673 mColorExtractor.removeOnColorsChangedListener(this);
674 }
675
676 /**
677 * Sets theme based on gradient colors
678 * @param supportsDarkText true if gradient supports dark text
679 */
680 private void updateTheme(boolean supportsDarkText) {
681 if (mSupportsDarkText == supportsDarkText) {
682 return;
683 }
684 mSupportsDarkText = supportsDarkText;
685
686 // We can't change themes after inflation, in this case we'll have to recreate
687 // the whole activity.
688 if (mBackgroundGradient != null) {
689 recreate();
690 return;
691 }
692
693 int vis = getWindow().getDecorView().getSystemUiVisibility();
694 if (supportsDarkText) {
695 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
696 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
697 setTheme(R.style.EmergencyDialerThemeDark);
698 } else {
699 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
700 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
701 setTheme(R.style.EmergencyDialerTheme);
702 }
703 getWindow().getDecorView().setSystemUiVisibility(vis);
704 }
705
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700706 /**
707 * place the call, but check to make sure it is a viable number.
708 */
709 private void placeCall() {
710 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900711
712 // Convert into emergency number according to emergency conversion map.
713 // If conversion map is not defined (this is default), this method does
714 // nothing and just returns input number.
715 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
716
Yorke Lee36bb2542014-06-05 08:09:52 -0700717 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700718 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
719
720 // place the call if it is a valid number
721 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
722 // There is no number entered.
723 playTone(ToneGenerator.TONE_PROP_NACK);
724 return;
725 }
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000726 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
727 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 } else {
729 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
730
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
732 }
Yorke Lee9b341512014-10-17 11:36:41 -0700733 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 }
735
736 /**
737 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
738 *
739 * The tone is played locally, using the audio stream for phone calls.
740 * Tones are played only if the "Audible touch tones" user preference
741 * is checked, and are NOT played if the device is in silent mode.
742 *
743 * @param tone a tone code from {@link ToneGenerator}
744 */
745 void playTone(int tone) {
746 // if local tone playback is disabled, just return.
747 if (!mDTMFToneEnabled) {
748 return;
749 }
750
751 // Also do nothing if the phone is in silent mode.
752 // We need to re-check the ringer mode for *every* playTone()
753 // call, rather than keeping a local flag that's updated in
754 // onResume(), since it's possible to toggle silent mode without
755 // leaving the current activity (via the ENDCALL-longpress menu.)
756 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
757 int ringerMode = audioManager.getRingerMode();
758 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
759 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
760 return;
761 }
762
763 synchronized (mToneGeneratorLock) {
764 if (mToneGenerator == null) {
765 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
766 return;
767 }
768
769 // Start the new tone (will stop any playing tone)
770 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
771 }
772 }
773
774 private CharSequence createErrorMessage(String number) {
775 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800776 String errorString = getString(R.string.dial_emergency_error, number);
777 int startingPosition = errorString.indexOf(number);
778 int endingPosition = startingPosition + number.length();
779 Spannable result = new SpannableString(errorString);
780 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
781 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 } else {
783 return getText(R.string.dial_emergency_empty_error).toString();
784 }
785 }
786
787 @Override
788 protected Dialog onCreateDialog(int id) {
789 AlertDialog dialog = null;
790 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
791 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700792 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
794 .setMessage(createErrorMessage(mLastNumber))
795 .setPositiveButton(R.string.ok, null)
796 .setCancelable(true).create();
797
798 // blur stuff behind the dialog
799 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700800 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 }
802 return dialog;
803 }
804
805 @Override
806 protected void onPrepareDialog(int id, Dialog dialog) {
807 super.onPrepareDialog(id, dialog);
808 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
809 AlertDialog alert = (AlertDialog) dialog;
810 alert.setMessage(createErrorMessage(mLastNumber));
811 }
812 }
813
Andrew Leed5631e82014-10-08 16:03:58 -0700814 @Override
815 public boolean onOptionsItemSelected(MenuItem item) {
816 final int itemId = item.getItemId();
817 if (itemId == android.R.id.home) {
818 onBackPressed();
819 return true;
820 }
821 return super.onOptionsItemSelected(item);
822 }
823
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 /**
825 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
826 */
827 private void updateDialAndDeleteButtonStateEnabledAttr() {
828 final boolean notEmpty = mDigits.length() != 0;
829
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 mDelete.setEnabled(notEmpty);
831 }
Yorke Lee91311662014-10-24 14:50:45 -0700832
833 /**
834 * Remove the digit just before the current position. Used by various long pressed callbacks
835 * to remove the digit that was populated as a result of the short click.
836 */
837 private void removePreviousDigitIfPossible() {
838 final int currentPosition = mDigits.getSelectionStart();
839 if (currentPosition > 0) {
840 mDigits.setSelection(currentPosition);
841 mDigits.getText().delete(currentPosition - 1, currentPosition);
842 }
843 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800844
845 /**
846 * Update the text-to-speech annotations in the edit field.
847 */
848 private void updateTtsSpans() {
849 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
850 mDigits.getText().removeSpan(o);
851 }
852 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
853 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700854
855 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700856 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700857 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700858 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
859 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700860 mBackgroundGradient.setColors(colors);
861 updateTheme(colors.supportsDarkText());
862 }
863 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700864
865 /**
866 * Where a carrier requires a warning that emergency calling is not available while on WFC,
867 * add hint text above the dial pad which warns the user of this case.
868 */
869 private void maybeShowWfcEmergencyCallingWarning() {
870 if (!mIsWfcEmergencyCallingWarningEnabled) {
871 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
872 return;
873 }
874
875 // Use an async task rather than calling into Telephony on UI thread.
876 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
877 @Override
878 protected Boolean doInBackground(Void... voids) {
879 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
880 boolean isWfcAvailable = tm.isWifiCallingAvailable();
881 ServiceState ss = tm.getServiceState();
882 boolean isCellAvailable =
883 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
884 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
885 + " isCellAvailable=" + isCellAvailable
886 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
887 return isWfcAvailable && !isCellAvailable;
888 }
889
890 @Override
891 protected void onPostExecute(Boolean result) {
892 if (result.booleanValue()) {
893 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
894 mDigits.setHint(R.string.dial_emergency_calling_not_available);
895 } else {
896 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
897 mDigits.setHint("");
898 }
899 maybeChangeHintSize();
900 }
901 };
902 showWfcWarningTask.execute((Void) null);
903 }
904
905 /**
906 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
907 * edit view and set the font size to a smaller size appropriate for the emergency calling
908 * warning.
909 */
910 private void maybeChangeHintSize() {
911 if (TextUtils.isEmpty(mDigits.getHint())
912 || !TextUtils.isEmpty(mDigits.getText().toString())) {
913 // No hint or there are dialed digits, so use default size.
914 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
915 // By default, the digits view auto-resizes to fit the text it contains, so
916 // enable that now.
917 mDigits.setResizeEnabled(true);
918 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
919 } else {
920 // Hint present and no dialed digits, set custom font size appropriate for the warning.
921 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
922 R.dimen.emergency_call_warning_size));
923 // Since we're populating this with a static text string, disable auto-resize.
924 mDigits.setResizeEnabled(false);
925 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
926 }
927 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800928
929 private void setupEmergencyShortcutsView() {
930 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
931 mDialpadView = findViewById(R.id.emergency_dialer);
932
933 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
934 dialpadButton.setOnClickListener(this);
935
936 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
937 emergencyInfoButton.setOnClickListener(this);
938
939 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
940 mEmergencyActionGroup.setVisibility(View.GONE);
941
942 // Setup dialpad title.
943 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
944 emergencyDialpadTitle.setVisibility(View.VISIBLE);
945
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800946 mEmergencyShortcutButtonList = new ArrayList<>();
947 setupEmergencyCallShortcutButton();
948
CY Cheng42873672018-06-15 21:20:10 +0800949 updateLocationAndEccInfo(null, null);
950
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800951 switchView(mEmergencyShortcutView, mDialpadView, false);
952 }
953
CY Cheng42873672018-06-15 21:20:10 +0800954 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800955 final View locationInfo = findViewById(R.id.location_info);
956
CY Cheng42873672018-06-15 21:20:10 +0800957 String countryName = null;
958 if (!TextUtils.isEmpty(countryIso)) {
959 Locale locale = Locale.getDefault();
960 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
961 .getDisplayCountry();
962 }
963 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800964 locationInfo.setVisibility(View.INVISIBLE);
965 } else {
966 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +0800967 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800968 locationInfo.setVisibility(View.VISIBLE);
969 }
970 }
971
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800972 private void setupEmergencyCallShortcutButton() {
973 final ViewGroup shortcutButtonContainer = findViewById(
974 R.id.emergency_shortcut_buttons_container);
975 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +0800976 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800977
CY Cheng42873672018-06-15 21:20:10 +0800978 mShortcutAdapter = new EccShortcutAdapter(this) {
979 @Override
980 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
981 CharSequence description, int iconRes) {
982 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
983 .inflate(R.layout.emergency_shortcut_button, parent, false);
984 button.setPhoneNumber(number);
985 button.setPhoneDescription(description);
986 button.setPhoneTypeIcon(iconRes);
987 button.setOnConfirmClickListener(EmergencyDialer.this);
988 return button;
989 }
990 };
991 mShortcutDataSetObserver = new DataSetObserver() {
992 @Override
993 public void onChanged() {
994 super.onChanged();
995 updateLayout();
996 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800997
CY Cheng42873672018-06-15 21:20:10 +0800998 @Override
999 public void onInvalidated() {
1000 super.onInvalidated();
1001 updateLayout();
1002 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001003
CY Cheng42873672018-06-15 21:20:10 +08001004 private void updateLayout() {
1005 // clear previous added buttons
1006 shortcutButtonContainer.removeAllViews();
1007 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001008
CY Cheng42873672018-06-15 21:20:10 +08001009 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1010 EmergencyShortcutButton button = (EmergencyShortcutButton)
1011 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1012 mEmergencyShortcutButtonList.add(button);
1013 shortcutButtonContainer.addView(button);
1014 }
1015
1016 // update emergency numbers title for numerous buttons.
1017 if (mEmergencyShortcutButtonList.size() > 1) {
1018 emergencyNumberTitle.setText(getString(
1019 R.string.numerous_emergency_numbers_title));
1020 } else {
1021 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1022 }
1023 }
1024 };
1025 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1026 }
1027
1028 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1029 if (!isFinishing() && !isDestroyed()) {
1030 setLocationInfo(iso);
1031 if (mShortcutAdapter != null) {
1032 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1033 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001034 }
1035 }
1036
1037 /**
1038 * Called by the activity before a touch event is dispatched to the view hierarchy.
1039 */
1040 private void onPreTouchEvent(MotionEvent event) {
1041 mEmergencyActionGroup.onPreTouchEvent(event);
1042
1043 if (mEmergencyShortcutButtonList != null) {
1044 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1045 button.onPreTouchEvent(event);
1046 }
1047 }
1048 }
1049
1050 /**
1051 * Called by the activity after a touch event is dispatched to the view hierarchy.
1052 */
1053 private void onPostTouchEvent(MotionEvent event) {
1054 mEmergencyActionGroup.onPostTouchEvent(event);
1055
1056 if (mEmergencyShortcutButtonList != null) {
1057 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1058 button.onPostTouchEvent(event);
1059 }
1060 }
1061 }
1062
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001063 /**
1064 * Switch two view.
1065 *
1066 * @param displayView the view would be displayed.
1067 * @param hideView the view would be hidden.
1068 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1069 */
1070 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1071 if (displayView == null || hideView == null) {
1072 return;
1073 }
1074
1075 if (displayView.getVisibility() == View.VISIBLE) {
1076 return;
1077 }
1078
1079 if (hasAnimation) {
1080 crossfade(hideView, displayView);
1081 } else {
1082 hideView.setVisibility(View.GONE);
1083 displayView.setVisibility(View.VISIBLE);
1084 }
1085 }
1086
1087 /**
1088 * Fade out and fade in animation between two view transition.
1089 */
1090 private void crossfade(View fadeOutView, View fadeInView) {
1091 if (fadeOutView == null || fadeInView == null) {
1092 return;
1093 }
1094 final int shortAnimationDuration = getResources().getInteger(
1095 android.R.integer.config_shortAnimTime);
1096
1097 fadeInView.setAlpha(0f);
1098 fadeInView.setVisibility(View.VISIBLE);
1099
1100 fadeInView.animate()
1101 .alpha(1f)
1102 .setDuration(shortAnimationDuration)
1103 .setListener(null);
1104
1105 fadeOutView.animate()
1106 .alpha(0f)
1107 .setDuration(shortAnimationDuration)
1108 .setListener(new AnimatorListenerAdapter() {
1109 @Override
1110 public void onAnimationEnd(Animator animation) {
1111 fadeOutView.setVisibility(View.GONE);
1112 }
1113 });
1114 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115}