blob: 7e5b1ce581d071eed3f0bd902c1d9adff6d5915d [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080031import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-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 Gunnc5428972018-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 Gunnfa77e202018-03-23 07:47:00 -070042import 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 Chi17ec2282018-06-15 19:00:15 +080045import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070046import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070047import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-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 Gunnc5428972018-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 Chuang92cfe512018-06-07 16:25:27 +080064import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.WindowManager;
Chihhang Chuang92cfe512018-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 Gunnc5428972018-03-28 14:15:34 -070073import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080074import com.android.phone.ecc.CountryEccInfo;
75import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080076import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang92cfe512018-06-07 16:25:27 +080078import java.util.ArrayList;
79import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080080import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080081
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082/**
83 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
84 *
85 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
86 * activity from apps/Contacts) that:
87 * 1. Allows ONLY emergency calls to be dialed
88 * 2. Disallows voicemail functionality
89 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
90 * activity to stay in front of the keyguard.
91 *
92 * TODO: Even though this is an ultra-simplified version of the normal
93 * dialer, there's still lots of code duplication between this class and
94 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
95 * moved into a shared base class that would live in the framework?
96 * Or could we figure out some way to move *this* class into apps/Contacts
97 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080098 *
99 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800100 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
101 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800102 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800103 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 */
105public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700106 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800107 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
108 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 // Keys used with onSaveInstanceState().
110 private static final String LAST_NUMBER = "lastNumber";
111
112 // Intent action for this activity.
113 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
114
115 // List of dialer button IDs.
116 private static final int[] DIALER_KEYS = new int[] {
117 R.id.one, R.id.two, R.id.three,
118 R.id.four, R.id.five, R.id.six,
119 R.id.seven, R.id.eight, R.id.nine,
120 R.id.star, R.id.zero, R.id.pound };
121
122 // Debug constants.
123 private static final boolean DBG = false;
124 private static final String LOG_TAG = "EmergencyDialer";
125
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 /** The length of DTMF tones in milliseconds */
127 private static final int TONE_LENGTH_MS = 150;
128
129 /** The DTMF tone volume relative to other sounds in the stream */
130 private static final int TONE_RELATIVE_VOLUME = 80;
131
132 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
133 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
134
135 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
136
Lucas Dupineb9c5702017-05-10 16:57:09 -0700137 /** 90% opacity, different from other gradients **/
138 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
139
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800140 /** 85% opacity for black background **/
141 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
142
CY Cheng9a69c182018-06-15 21:20:10 +0800143 /** Size limit of emergency shortcut buttons container. **/
144 private static final int SHORTCUT_SIZE_LIMIT = 3;
145
Tyler Gunnc5428972018-03-28 14:15:34 -0700146 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private View mDialButton;
148 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800149 private View mEmergencyShortcutView;
150 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
CY Cheng9a69c182018-06-15 21:20:10 +0800152 private EccInfoHelper mEccInfoHelper;
153
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800154 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800155 private EccShortcutAdapter mShortcutAdapter;
156 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 private ToneGenerator mToneGenerator;
159 private Object mToneGeneratorLock = new Object();
160
161 // determines if we want to playback local DTMF tones.
162 private boolean mDTMFToneEnabled;
163
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700164 private EmergencyActionGroup mEmergencyActionGroup;
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 // close activity when screen turns off
167 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
168 @Override
169 public void onReceive(Context context, Intent intent) {
170 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700171 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 }
173 }
174 };
175
176 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
177
Lucas Dupineb9c5702017-05-10 16:57:09 -0700178 // Background gradient
179 private ColorExtractor mColorExtractor;
180 private GradientDrawable mBackgroundGradient;
181 private boolean mSupportsDarkText;
182
Tyler Gunnc5428972018-03-28 14:15:34 -0700183 private boolean mIsWfcEmergencyCallingWarningEnabled;
184 private float mDefaultDigitsTextSize;
185
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800186 private boolean mAreEmergencyDialerShortcutsEnabled;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 @Override
189 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
190 // Do nothing
191 }
192
193 @Override
194 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700195 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 }
197
198 @Override
199 public void afterTextChanged(Editable input) {
200 // Check for special sequences, in particular the "**04" or "**05"
201 // sequences that allow you to enter PIN or PUK-related codes.
202 //
203 // But note we *don't* allow most other special sequences here,
204 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
205 // since those shouldn't be available if the device is locked.
206 //
207 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
208 // here, not the regular handleChars() method.
209 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
210 // A special sequence was entered, clear the digits
211 mDigits.getText().clear();
212 }
213
214 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800215 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 }
217
218 @Override
219 protected void onCreate(Bundle icicle) {
220 super.onCreate(icicle);
221
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800223 setShowWhenLocked(true);
224 // Allow turning screen on
225 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800227 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
228 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng9a69c182018-06-15 21:20:10 +0800229 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
230 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800231
Lucas Dupineb9c5702017-05-10 16:57:09 -0700232 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800233
234 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
235 // And the background color is black with 85% opacity.
236 if (mAreEmergencyDialerShortcutsEnabled) {
237 updateTheme(false);
238 } else {
239 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
240 ColorExtractor.TYPE_EXTRA_DARK);
241 updateTheme(lockScreenColors.supportsDarkText());
242 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 setContentView(R.layout.emergency_dialer);
245
Tyler Gunnc5428972018-03-28 14:15:34 -0700246 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 mDigits.setKeyListener(DialerKeyListener.getInstance());
248 mDigits.setOnClickListener(this);
249 mDigits.setOnKeyListener(this);
250 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700251 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700252 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 maybeAddNumberFormatting();
254
Lucas Dupineb9c5702017-05-10 16:57:09 -0700255 mBackgroundGradient = new GradientDrawable(this);
256 Point displaySize = new Point();
257 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
258 .getDefaultDisplay().getSize(displaySize);
259 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800260 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
261 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700262 getWindow().setBackgroundDrawable(mBackgroundGradient);
263
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 // Check for the presence of the keypad
265 View view = findViewById(R.id.one);
266 if (view != null) {
267 setupKeypad();
268 }
269
270 mDelete = findViewById(R.id.deleteButton);
271 mDelete.setOnClickListener(this);
272 mDelete.setOnLongClickListener(this);
273
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700274 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275
276 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700277 // Read carrier config through the public API because PhoneGlobals is not available when we
278 // run as a secondary user.
279 CarrierConfigManager configMgr =
280 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
281 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800282 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700283
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700284 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 mDialButton.setOnClickListener(this);
286 } else {
287 mDialButton.setVisibility(View.GONE);
288 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700289 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
290 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
291 maybeShowWfcEmergencyCallingWarning();
292
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700293 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294
295 if (icicle != null) {
296 super.onRestoreInstanceState(icicle);
297 }
298
299 // Extract phone number from intent
300 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700301 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
303 if (number != null) {
304 mDigits.setText(number);
305 }
306 }
307
308 // if the mToneGenerator creation fails, just continue without it. It is
309 // a local audio signal, and is not as important as the dtmf tone itself.
310 synchronized (mToneGeneratorLock) {
311 if (mToneGenerator == null) {
312 try {
313 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
314 } catch (RuntimeException e) {
315 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
316 mToneGenerator = null;
317 }
318 }
319 }
320
321 final IntentFilter intentFilter = new IntentFilter();
322 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
323 registerReceiver(mBroadcastReceiver, intentFilter);
324
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700325 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800326
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800327 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu779569a2018-07-10 11:37:52 +0800328 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800329 setupEmergencyShortcutsView();
330 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 }
332
333 @Override
334 protected void onDestroy() {
335 super.onDestroy();
336 synchronized (mToneGeneratorLock) {
337 if (mToneGenerator != null) {
338 mToneGenerator.release();
339 mToneGenerator = null;
340 }
341 }
342 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800343 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
344 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
345 mShortcutDataSetObserver = null;
346 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 }
348
349 @Override
350 protected void onRestoreInstanceState(Bundle icicle) {
351 mLastNumber = icicle.getString(LAST_NUMBER);
352 }
353
354 @Override
355 protected void onSaveInstanceState(Bundle outState) {
356 super.onSaveInstanceState(outState);
357 outState.putString(LAST_NUMBER, mLastNumber);
358 }
359
360 /**
361 * Explicitly turn off number formatting, since it gets in the way of the emergency
362 * number detector
363 */
364 protected void maybeAddNumberFormatting() {
365 // Do nothing.
366 }
367
368 @Override
369 protected void onPostCreate(Bundle savedInstanceState) {
370 super.onPostCreate(savedInstanceState);
371
372 // This can't be done in onCreate(), since the auto-restoring of the digits
373 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
374 // is called. This method will be called every time the activity is created, and
375 // will always happen after onRestoreSavedInstanceState().
376 mDigits.addTextChangedListener(this);
377 }
378
379 private void setupKeypad() {
380 // Setup the listeners for the buttons
381 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700382 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
383 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 }
385
386 View view = findViewById(R.id.zero);
387 view.setOnLongClickListener(this);
388 }
389
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800390 @Override
391 public void onBackPressed() {
392 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800393 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800394 // Otherwise, it would finish the activity.
395 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
396 && mDialpadView.getVisibility() == View.VISIBLE) {
397 switchView(mEmergencyShortcutView, mDialpadView, true);
398 return;
399 }
400 super.onBackPressed();
401 }
402
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700403 /**
404 * handle key events
405 */
406 @Override
407 public boolean onKeyDown(int keyCode, KeyEvent event) {
408 switch (keyCode) {
409 // Happen when there's a "Call" hard button.
410 case KeyEvent.KEYCODE_CALL: {
411 if (TextUtils.isEmpty(mDigits.getText().toString())) {
412 // if we are adding a call from the InCallScreen and the phone
413 // number entered is empty, we just close the dialer to expose
414 // the InCallScreen under it.
415 finish();
416 } else {
417 // otherwise, we place the call.
418 placeCall();
419 }
420 return true;
421 }
422 }
423 return super.onKeyDown(keyCode, event);
424 }
425
426 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700427 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700428 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
429 mDigits.onKeyDown(keyCode, event);
430 }
431
432 @Override
433 public boolean onKey(View view, int keyCode, KeyEvent event) {
434 switch (view.getId()) {
435 case R.id.digits:
436 // Happen when "Done" button of the IME is pressed. This can happen when this
437 // Activity is forced into landscape mode due to a desk dock.
438 if (keyCode == KeyEvent.KEYCODE_ENTER
439 && event.getAction() == KeyEvent.ACTION_UP) {
440 placeCall();
441 return true;
442 }
443 break;
444 }
445 return false;
446 }
447
448 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700449 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800450 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700451 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800452 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700453 return handled;
454 }
455
456 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800457 public void onConfirmClick(EmergencyShortcutButton button) {
458 if (button == null) return;
459
460 String phoneNumber = button.getPhoneNumber();
461
462 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800463 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800464 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
465 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
466 } else {
467 Log.d(LOG_TAG, "emergency number is empty");
468 }
469 }
470
471 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700472 public void onClick(View view) {
473 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700474 case R.id.deleteButton: {
475 keyPressed(KeyEvent.KEYCODE_DEL);
476 return;
477 }
478 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700479 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700480 placeCall();
481 return;
482 }
483 case R.id.digits: {
484 if (mDigits.length() != 0) {
485 mDigits.setCursorVisible(true);
486 }
487 return;
488 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800489 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800490 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800491 switchView(mDialpadView, mEmergencyShortcutView, true);
492 return;
493 }
494 case R.id.emergency_info_button: {
495 Intent intent = (Intent) view.getTag(R.id.tag_intent);
496 if (intent != null) {
497 startActivity(intent);
498 }
499 return;
500 }
Yorke Lee23a70732014-08-14 17:12:01 -0700501 }
502 }
503
504 @Override
505 public void onPressed(View view, boolean pressed) {
506 if (!pressed) {
507 return;
508 }
509 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700510 case R.id.one: {
511 playTone(ToneGenerator.TONE_DTMF_1);
512 keyPressed(KeyEvent.KEYCODE_1);
513 return;
514 }
515 case R.id.two: {
516 playTone(ToneGenerator.TONE_DTMF_2);
517 keyPressed(KeyEvent.KEYCODE_2);
518 return;
519 }
520 case R.id.three: {
521 playTone(ToneGenerator.TONE_DTMF_3);
522 keyPressed(KeyEvent.KEYCODE_3);
523 return;
524 }
525 case R.id.four: {
526 playTone(ToneGenerator.TONE_DTMF_4);
527 keyPressed(KeyEvent.KEYCODE_4);
528 return;
529 }
530 case R.id.five: {
531 playTone(ToneGenerator.TONE_DTMF_5);
532 keyPressed(KeyEvent.KEYCODE_5);
533 return;
534 }
535 case R.id.six: {
536 playTone(ToneGenerator.TONE_DTMF_6);
537 keyPressed(KeyEvent.KEYCODE_6);
538 return;
539 }
540 case R.id.seven: {
541 playTone(ToneGenerator.TONE_DTMF_7);
542 keyPressed(KeyEvent.KEYCODE_7);
543 return;
544 }
545 case R.id.eight: {
546 playTone(ToneGenerator.TONE_DTMF_8);
547 keyPressed(KeyEvent.KEYCODE_8);
548 return;
549 }
550 case R.id.nine: {
551 playTone(ToneGenerator.TONE_DTMF_9);
552 keyPressed(KeyEvent.KEYCODE_9);
553 return;
554 }
555 case R.id.zero: {
556 playTone(ToneGenerator.TONE_DTMF_0);
557 keyPressed(KeyEvent.KEYCODE_0);
558 return;
559 }
560 case R.id.pound: {
561 playTone(ToneGenerator.TONE_DTMF_P);
562 keyPressed(KeyEvent.KEYCODE_POUND);
563 return;
564 }
565 case R.id.star: {
566 playTone(ToneGenerator.TONE_DTMF_S);
567 keyPressed(KeyEvent.KEYCODE_STAR);
568 return;
569 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700570 }
571 }
572
573 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700574 * called for long touch events
575 */
576 @Override
577 public boolean onLongClick(View view) {
578 int id = view.getId();
579 switch (id) {
580 case R.id.deleteButton: {
581 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700582 return true;
583 }
584 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700585 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700586 keyPressed(KeyEvent.KEYCODE_PLUS);
587 return true;
588 }
589 }
590 return false;
591 }
592
593 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700594 protected void onStart() {
595 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800596 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
597 // And set background color to black.
598 if (mAreEmergencyDialerShortcutsEnabled) {
599 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
600 updateTheme(false);
601 } else {
602 mColorExtractor.addOnColorsChangedListener(this);
603 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
604 ColorExtractor.TYPE_EXTRA_DARK);
605 // Do not animate when view isn't visible yet, just set an initial state.
606 mBackgroundGradient.setColors(lockScreenColors, false);
607 updateTheme(lockScreenColors.supportsDarkText());
608 }
CY Cheng9a69c182018-06-15 21:20:10 +0800609
610 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
611 final Context context = this;
612 mEccInfoHelper.getCountryEccInfoAsync(context,
613 new EccInfoHelper.CountryEccInfoResultCallback() {
614 @Override
615 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
616 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
617 + Rlog.pii(LOG_TAG, iso));
618 updateLocationAndEccInfo(iso, countryEccInfo);
619 }
620
621 @Override
622 public void onDetectCountryFailed() {
623 Log.w(LOG_TAG, "Cannot detect current country.");
624 updateLocationAndEccInfo(null, null);
625 }
626
627 @Override
628 public void onRetrieveCountryEccInfoFailed(String iso) {
629 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
630 + Rlog.pii(LOG_TAG, iso));
631 updateLocationAndEccInfo(iso, null);
632 }
633 });
634 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700635 }
636
637 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700638 protected void onResume() {
639 super.onResume();
640
641 // retrieve the DTMF tone play back setting.
642 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
643 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
644
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700645 // if the mToneGenerator creation fails, just continue without it. It is
646 // a local audio signal, and is not as important as the dtmf tone itself.
647 synchronized (mToneGeneratorLock) {
648 if (mToneGenerator == null) {
649 try {
650 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
651 TONE_RELATIVE_VOLUME);
652 } catch (RuntimeException e) {
653 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
654 mToneGenerator = null;
655 }
656 }
657 }
658
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700659 updateDialAndDeleteButtonStateEnabledAttr();
660 }
661
662 @Override
663 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700665 }
666
Lucas Dupineb9c5702017-05-10 16:57:09 -0700667 @Override
668 protected void onStop() {
669 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700670 mColorExtractor.removeOnColorsChangedListener(this);
671 }
672
673 /**
674 * Sets theme based on gradient colors
675 * @param supportsDarkText true if gradient supports dark text
676 */
677 private void updateTheme(boolean supportsDarkText) {
678 if (mSupportsDarkText == supportsDarkText) {
679 return;
680 }
681 mSupportsDarkText = supportsDarkText;
682
683 // We can't change themes after inflation, in this case we'll have to recreate
684 // the whole activity.
685 if (mBackgroundGradient != null) {
686 recreate();
687 return;
688 }
689
690 int vis = getWindow().getDecorView().getSystemUiVisibility();
691 if (supportsDarkText) {
692 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
693 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
694 setTheme(R.style.EmergencyDialerThemeDark);
695 } else {
696 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
697 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
698 setTheme(R.style.EmergencyDialerTheme);
699 }
700 getWindow().getDecorView().setSystemUiVisibility(vis);
701 }
702
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700703 /**
704 * place the call, but check to make sure it is a viable number.
705 */
706 private void placeCall() {
707 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900708
709 // Convert into emergency number according to emergency conversion map.
710 // If conversion map is not defined (this is default), this method does
711 // nothing and just returns input number.
712 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
713
Yorke Lee36bb2542014-06-05 08:09:52 -0700714 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700715 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
716
717 // place the call if it is a valid number
718 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
719 // There is no number entered.
720 playTone(ToneGenerator.TONE_PROP_NACK);
721 return;
722 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700723 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
724 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700725 } else {
726 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
727
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700728 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
729 }
Yorke Lee9b341512014-10-17 11:36:41 -0700730 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700731 }
732
733 /**
734 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
735 *
736 * The tone is played locally, using the audio stream for phone calls.
737 * Tones are played only if the "Audible touch tones" user preference
738 * is checked, and are NOT played if the device is in silent mode.
739 *
740 * @param tone a tone code from {@link ToneGenerator}
741 */
742 void playTone(int tone) {
743 // if local tone playback is disabled, just return.
744 if (!mDTMFToneEnabled) {
745 return;
746 }
747
748 // Also do nothing if the phone is in silent mode.
749 // We need to re-check the ringer mode for *every* playTone()
750 // call, rather than keeping a local flag that's updated in
751 // onResume(), since it's possible to toggle silent mode without
752 // leaving the current activity (via the ENDCALL-longpress menu.)
753 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
754 int ringerMode = audioManager.getRingerMode();
755 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
756 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
757 return;
758 }
759
760 synchronized (mToneGeneratorLock) {
761 if (mToneGenerator == null) {
762 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
763 return;
764 }
765
766 // Start the new tone (will stop any playing tone)
767 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
768 }
769 }
770
771 private CharSequence createErrorMessage(String number) {
772 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800773 String errorString = getString(R.string.dial_emergency_error, number);
774 int startingPosition = errorString.indexOf(number);
775 int endingPosition = startingPosition + number.length();
776 Spannable result = new SpannableString(errorString);
777 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
778 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 } else {
780 return getText(R.string.dial_emergency_empty_error).toString();
781 }
782 }
783
784 @Override
785 protected Dialog onCreateDialog(int id) {
786 AlertDialog dialog = null;
787 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
788 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700789 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
791 .setMessage(createErrorMessage(mLastNumber))
792 .setPositiveButton(R.string.ok, null)
793 .setCancelable(true).create();
794
795 // blur stuff behind the dialog
796 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700797 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700798 }
799 return dialog;
800 }
801
802 @Override
803 protected void onPrepareDialog(int id, Dialog dialog) {
804 super.onPrepareDialog(id, dialog);
805 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
806 AlertDialog alert = (AlertDialog) dialog;
807 alert.setMessage(createErrorMessage(mLastNumber));
808 }
809 }
810
Andrew Leed5631e82014-10-08 16:03:58 -0700811 @Override
812 public boolean onOptionsItemSelected(MenuItem item) {
813 final int itemId = item.getItemId();
814 if (itemId == android.R.id.home) {
815 onBackPressed();
816 return true;
817 }
818 return super.onOptionsItemSelected(item);
819 }
820
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700821 /**
822 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
823 */
824 private void updateDialAndDeleteButtonStateEnabledAttr() {
825 final boolean notEmpty = mDigits.length() != 0;
826
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700827 mDelete.setEnabled(notEmpty);
828 }
Yorke Lee91311662014-10-24 14:50:45 -0700829
830 /**
831 * Remove the digit just before the current position. Used by various long pressed callbacks
832 * to remove the digit that was populated as a result of the short click.
833 */
834 private void removePreviousDigitIfPossible() {
835 final int currentPosition = mDigits.getSelectionStart();
836 if (currentPosition > 0) {
837 mDigits.setSelection(currentPosition);
838 mDigits.getText().delete(currentPosition - 1, currentPosition);
839 }
840 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800841
842 /**
843 * Update the text-to-speech annotations in the edit field.
844 */
845 private void updateTtsSpans() {
846 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
847 mDigits.getText().removeSpan(o);
848 }
849 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
850 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700851
852 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700853 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700854 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700855 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
856 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700857 mBackgroundGradient.setColors(colors);
858 updateTheme(colors.supportsDarkText());
859 }
860 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700861
862 /**
863 * Where a carrier requires a warning that emergency calling is not available while on WFC,
864 * add hint text above the dial pad which warns the user of this case.
865 */
866 private void maybeShowWfcEmergencyCallingWarning() {
867 if (!mIsWfcEmergencyCallingWarningEnabled) {
868 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
869 return;
870 }
871
872 // Use an async task rather than calling into Telephony on UI thread.
873 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
874 @Override
875 protected Boolean doInBackground(Void... voids) {
876 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
877 boolean isWfcAvailable = tm.isWifiCallingAvailable();
878 ServiceState ss = tm.getServiceState();
879 boolean isCellAvailable =
880 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
881 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
882 + " isCellAvailable=" + isCellAvailable
883 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
884 return isWfcAvailable && !isCellAvailable;
885 }
886
887 @Override
888 protected void onPostExecute(Boolean result) {
889 if (result.booleanValue()) {
890 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
891 mDigits.setHint(R.string.dial_emergency_calling_not_available);
892 } else {
893 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
894 mDigits.setHint("");
895 }
896 maybeChangeHintSize();
897 }
898 };
899 showWfcWarningTask.execute((Void) null);
900 }
901
902 /**
903 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
904 * edit view and set the font size to a smaller size appropriate for the emergency calling
905 * warning.
906 */
907 private void maybeChangeHintSize() {
908 if (TextUtils.isEmpty(mDigits.getHint())
909 || !TextUtils.isEmpty(mDigits.getText().toString())) {
910 // No hint or there are dialed digits, so use default size.
911 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
912 // By default, the digits view auto-resizes to fit the text it contains, so
913 // enable that now.
914 mDigits.setResizeEnabled(true);
915 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
916 } else {
917 // Hint present and no dialed digits, set custom font size appropriate for the warning.
918 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
919 R.dimen.emergency_call_warning_size));
920 // Since we're populating this with a static text string, disable auto-resize.
921 mDigits.setResizeEnabled(false);
922 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
923 }
924 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800925
926 private void setupEmergencyShortcutsView() {
927 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
928 mDialpadView = findViewById(R.id.emergency_dialer);
929
930 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
931 dialpadButton.setOnClickListener(this);
932
933 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
934 emergencyInfoButton.setOnClickListener(this);
935
936 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
937 mEmergencyActionGroup.setVisibility(View.GONE);
938
939 // Setup dialpad title.
940 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
941 emergencyDialpadTitle.setVisibility(View.VISIBLE);
942
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800943 mEmergencyShortcutButtonList = new ArrayList<>();
944 setupEmergencyCallShortcutButton();
945
CY Cheng9a69c182018-06-15 21:20:10 +0800946 updateLocationAndEccInfo(null, null);
947
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800948 switchView(mEmergencyShortcutView, mDialpadView, false);
949 }
950
CY Cheng9a69c182018-06-15 21:20:10 +0800951 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800952 final View locationInfo = findViewById(R.id.location_info);
953
CY Cheng9a69c182018-06-15 21:20:10 +0800954 String countryName = null;
955 if (!TextUtils.isEmpty(countryIso)) {
956 Locale locale = Locale.getDefault();
957 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
958 .getDisplayCountry();
959 }
960 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800961 locationInfo.setVisibility(View.INVISIBLE);
962 } else {
963 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800964 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800965 locationInfo.setVisibility(View.VISIBLE);
966 }
967 }
968
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800969 private void setupEmergencyCallShortcutButton() {
970 final ViewGroup shortcutButtonContainer = findViewById(
971 R.id.emergency_shortcut_buttons_container);
972 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +0800973 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800974
CY Cheng9a69c182018-06-15 21:20:10 +0800975 mShortcutAdapter = new EccShortcutAdapter(this) {
976 @Override
977 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
978 CharSequence description, int iconRes) {
979 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
980 .inflate(R.layout.emergency_shortcut_button, parent, false);
981 button.setPhoneNumber(number);
982 button.setPhoneDescription(description);
983 button.setPhoneTypeIcon(iconRes);
984 button.setOnConfirmClickListener(EmergencyDialer.this);
985 return button;
986 }
987 };
988 mShortcutDataSetObserver = new DataSetObserver() {
989 @Override
990 public void onChanged() {
991 super.onChanged();
992 updateLayout();
993 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800994
CY Cheng9a69c182018-06-15 21:20:10 +0800995 @Override
996 public void onInvalidated() {
997 super.onInvalidated();
998 updateLayout();
999 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001000
CY Cheng9a69c182018-06-15 21:20:10 +08001001 private void updateLayout() {
1002 // clear previous added buttons
1003 shortcutButtonContainer.removeAllViews();
1004 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001005
CY Cheng9a69c182018-06-15 21:20:10 +08001006 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1007 EmergencyShortcutButton button = (EmergencyShortcutButton)
1008 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1009 mEmergencyShortcutButtonList.add(button);
1010 shortcutButtonContainer.addView(button);
1011 }
1012
1013 // update emergency numbers title for numerous buttons.
1014 if (mEmergencyShortcutButtonList.size() > 1) {
1015 emergencyNumberTitle.setText(getString(
1016 R.string.numerous_emergency_numbers_title));
1017 } else {
1018 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1019 }
1020 }
1021 };
1022 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1023 }
1024
1025 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1026 if (!isFinishing() && !isDestroyed()) {
1027 setLocationInfo(iso);
1028 if (mShortcutAdapter != null) {
1029 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1030 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001031 }
1032 }
1033
1034 /**
1035 * Called by the activity before a touch event is dispatched to the view hierarchy.
1036 */
1037 private void onPreTouchEvent(MotionEvent event) {
1038 mEmergencyActionGroup.onPreTouchEvent(event);
1039
1040 if (mEmergencyShortcutButtonList != null) {
1041 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1042 button.onPreTouchEvent(event);
1043 }
1044 }
1045 }
1046
1047 /**
1048 * Called by the activity after a touch event is dispatched to the view hierarchy.
1049 */
1050 private void onPostTouchEvent(MotionEvent event) {
1051 mEmergencyActionGroup.onPostTouchEvent(event);
1052
1053 if (mEmergencyShortcutButtonList != null) {
1054 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1055 button.onPostTouchEvent(event);
1056 }
1057 }
1058 }
1059
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001060 /**
1061 * Switch two view.
1062 *
1063 * @param displayView the view would be displayed.
1064 * @param hideView the view would be hidden.
1065 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1066 */
1067 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1068 if (displayView == null || hideView == null) {
1069 return;
1070 }
1071
1072 if (displayView.getVisibility() == View.VISIBLE) {
1073 return;
1074 }
1075
1076 if (hasAnimation) {
1077 crossfade(hideView, displayView);
1078 } else {
1079 hideView.setVisibility(View.GONE);
1080 displayView.setVisibility(View.VISIBLE);
1081 }
1082 }
1083
1084 /**
1085 * Fade out and fade in animation between two view transition.
1086 */
1087 private void crossfade(View fadeOutView, View fadeInView) {
1088 if (fadeOutView == null || fadeInView == null) {
1089 return;
1090 }
1091 final int shortAnimationDuration = getResources().getInteger(
1092 android.R.integer.config_shortAnimTime);
1093
1094 fadeInView.setAlpha(0f);
1095 fadeInView.setVisibility(View.VISIBLE);
1096
1097 fadeInView.animate()
1098 .alpha(1f)
1099 .setDuration(shortAnimationDuration)
1100 .setListener(null);
1101
1102 fadeOutView.animate()
1103 .alpha(0f)
1104 .setDuration(shortAnimationDuration)
1105 .setListener(new AnimatorListenerAdapter() {
1106 @Override
1107 public void onAnimationEnd(Animator animation) {
1108 fadeOutView.setVisibility(View.GONE);
1109 }
1110 });
1111 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112}