blob: 7d20dc564633a5abc98c29cbecc5adc8e9a58daf [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 Chuangcaba0da2018-08-02 22:25:06 +080064import android.view.View.AccessibilityDelegate;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080065import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.WindowManager;
Chihhang Chuangcaba0da2018-08-02 22:25:06 +080067import android.view.accessibility.AccessibilityEvent;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080068import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069
Lucas Dupinaf9e9912017-06-22 12:39:39 -070070import com.android.internal.colorextraction.ColorExtractor;
71import com.android.internal.colorextraction.ColorExtractor.GradientColors;
72import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070073import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070074import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070075import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080076import com.android.phone.ecc.CountryEccInfo;
77import com.android.phone.ecc.EccInfoHelper;
Leo Hsu779569a2018-07-10 11:37:52 +080078import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079
Chihhang Chuang92cfe512018-06-07 16:25:27 +080080import java.util.ArrayList;
81import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080082import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080083
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084/**
85 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
86 *
87 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
88 * activity from apps/Contacts) that:
89 * 1. Allows ONLY emergency calls to be dialed
90 * 2. Disallows voicemail functionality
91 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
92 * activity to stay in front of the keyguard.
93 *
94 * TODO: Even though this is an ultra-simplified version of the normal
95 * dialer, there's still lots of code duplication between this class and
96 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
97 * moved into a shared base class that would live in the framework?
98 * Or could we figure out some way to move *this* class into apps/Contacts
99 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800100 *
101 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800102 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
103 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800104 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800105 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106 */
107public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700108 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800109 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
110 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 // Keys used with onSaveInstanceState().
112 private static final String LAST_NUMBER = "lastNumber";
113
114 // Intent action for this activity.
115 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
116
117 // List of dialer button IDs.
118 private static final int[] DIALER_KEYS = new int[] {
119 R.id.one, R.id.two, R.id.three,
120 R.id.four, R.id.five, R.id.six,
121 R.id.seven, R.id.eight, R.id.nine,
122 R.id.star, R.id.zero, R.id.pound };
123
124 // Debug constants.
125 private static final boolean DBG = false;
126 private static final String LOG_TAG = "EmergencyDialer";
127
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700128 /** The length of DTMF tones in milliseconds */
129 private static final int TONE_LENGTH_MS = 150;
130
131 /** The DTMF tone volume relative to other sounds in the stream */
132 private static final int TONE_RELATIVE_VOLUME = 80;
133
134 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
135 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
136
137 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
138
Lucas Dupineb9c5702017-05-10 16:57:09 -0700139 /** 90% opacity, different from other gradients **/
140 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
141
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800142 /** 85% opacity for black background **/
143 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
144
CY Cheng9a69c182018-06-15 21:20:10 +0800145 /** Size limit of emergency shortcut buttons container. **/
146 private static final int SHORTCUT_SIZE_LIMIT = 3;
147
Tyler Gunnc5428972018-03-28 14:15:34 -0700148 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700149 private View mDialButton;
150 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800151 private View mEmergencyShortcutView;
152 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
CY Cheng9a69c182018-06-15 21:20:10 +0800154 private EccInfoHelper mEccInfoHelper;
155
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800156 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800157 private EccShortcutAdapter mShortcutAdapter;
158 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800159
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 private ToneGenerator mToneGenerator;
161 private Object mToneGeneratorLock = new Object();
162
163 // determines if we want to playback local DTMF tones.
164 private boolean mDTMFToneEnabled;
165
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700166 private EmergencyActionGroup mEmergencyActionGroup;
167
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168 // close activity when screen turns off
169 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
170 @Override
171 public void onReceive(Context context, Intent intent) {
172 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700173 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174 }
175 }
176 };
177
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800178 /**
179 * Customize accessibility methods in View.
180 */
181 private AccessibilityDelegate mAccessibilityDelegate = new AccessibilityDelegate() {
182
183 /**
184 * Stop AccessiblityService from reading the title of a hidden View.
185 *
186 * <p>The crossfade animation will set the visibility of fade out view to {@link View.GONE}
187 * in the animation end. The view with an accessibility pane title would call the
188 * {@link AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED} event, which would trigger the
189 * accessibility service to read the pane title of fade out view instead of pane title of
190 * fade in view. So it need to filter out the event called by vanished pane.
191 */
192 @Override
193 public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
194 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
195 && host.getVisibility() == View.GONE) {
196 return;
197 }
198 super.onPopulateAccessibilityEvent(host, event);
199 }
200 };
201
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
203
Lucas Dupineb9c5702017-05-10 16:57:09 -0700204 // Background gradient
205 private ColorExtractor mColorExtractor;
206 private GradientDrawable mBackgroundGradient;
207 private boolean mSupportsDarkText;
208
Tyler Gunnc5428972018-03-28 14:15:34 -0700209 private boolean mIsWfcEmergencyCallingWarningEnabled;
210 private float mDefaultDigitsTextSize;
211
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800212 private boolean mAreEmergencyDialerShortcutsEnabled;
213
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214 @Override
215 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
216 // Do nothing
217 }
218
219 @Override
220 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700221 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 }
223
224 @Override
225 public void afterTextChanged(Editable input) {
226 // Check for special sequences, in particular the "**04" or "**05"
227 // sequences that allow you to enter PIN or PUK-related codes.
228 //
229 // But note we *don't* allow most other special sequences here,
230 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
231 // since those shouldn't be available if the device is locked.
232 //
233 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
234 // here, not the regular handleChars() method.
235 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
236 // A special sequence was entered, clear the digits
237 mDigits.getText().clear();
238 }
239
240 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800241 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 }
243
244 @Override
245 protected void onCreate(Bundle icicle) {
246 super.onCreate(icicle);
247
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700248 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800249 setShowWhenLocked(true);
250 // Allow turning screen on
251 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800253 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
254 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng9a69c182018-06-15 21:20:10 +0800255 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
256 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800257
Lucas Dupineb9c5702017-05-10 16:57:09 -0700258 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800259
260 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
261 // And the background color is black with 85% opacity.
262 if (mAreEmergencyDialerShortcutsEnabled) {
263 updateTheme(false);
264 } else {
265 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
266 ColorExtractor.TYPE_EXTRA_DARK);
267 updateTheme(lockScreenColors.supportsDarkText());
268 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700269
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270 setContentView(R.layout.emergency_dialer);
271
Tyler Gunnc5428972018-03-28 14:15:34 -0700272 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 mDigits.setKeyListener(DialerKeyListener.getInstance());
274 mDigits.setOnClickListener(this);
275 mDigits.setOnKeyListener(this);
276 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700277 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700278 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279 maybeAddNumberFormatting();
280
Lucas Dupineb9c5702017-05-10 16:57:09 -0700281 mBackgroundGradient = new GradientDrawable(this);
282 Point displaySize = new Point();
283 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
284 .getDefaultDisplay().getSize(displaySize);
285 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800286 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
287 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700288 getWindow().setBackgroundDrawable(mBackgroundGradient);
289
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 // Check for the presence of the keypad
291 View view = findViewById(R.id.one);
292 if (view != null) {
293 setupKeypad();
294 }
295
296 mDelete = findViewById(R.id.deleteButton);
297 mDelete.setOnClickListener(this);
298 mDelete.setOnLongClickListener(this);
299
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700300 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301
302 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700303 // Read carrier config through the public API because PhoneGlobals is not available when we
304 // run as a secondary user.
305 CarrierConfigManager configMgr =
306 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
307 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800308 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700309
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700310 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 mDialButton.setOnClickListener(this);
312 } else {
313 mDialButton.setVisibility(View.GONE);
314 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700315 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
316 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
317 maybeShowWfcEmergencyCallingWarning();
318
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700319 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320
321 if (icicle != null) {
322 super.onRestoreInstanceState(icicle);
323 }
324
325 // Extract phone number from intent
326 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700327 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
329 if (number != null) {
330 mDigits.setText(number);
331 }
332 }
333
334 // if the mToneGenerator creation fails, just continue without it. It is
335 // a local audio signal, and is not as important as the dtmf tone itself.
336 synchronized (mToneGeneratorLock) {
337 if (mToneGenerator == null) {
338 try {
339 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
340 } catch (RuntimeException e) {
341 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
342 mToneGenerator = null;
343 }
344 }
345 }
346
347 final IntentFilter intentFilter = new IntentFilter();
348 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
349 registerReceiver(mBroadcastReceiver, intentFilter);
350
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700351 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800352
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800353 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu779569a2018-07-10 11:37:52 +0800354 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800355 setupEmergencyShortcutsView();
356 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700357 }
358
359 @Override
360 protected void onDestroy() {
361 super.onDestroy();
362 synchronized (mToneGeneratorLock) {
363 if (mToneGenerator != null) {
364 mToneGenerator.release();
365 mToneGenerator = null;
366 }
367 }
368 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800369 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
370 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
371 mShortcutDataSetObserver = null;
372 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 }
374
375 @Override
376 protected void onRestoreInstanceState(Bundle icicle) {
377 mLastNumber = icicle.getString(LAST_NUMBER);
378 }
379
380 @Override
381 protected void onSaveInstanceState(Bundle outState) {
382 super.onSaveInstanceState(outState);
383 outState.putString(LAST_NUMBER, mLastNumber);
384 }
385
386 /**
387 * Explicitly turn off number formatting, since it gets in the way of the emergency
388 * number detector
389 */
390 protected void maybeAddNumberFormatting() {
391 // Do nothing.
392 }
393
394 @Override
395 protected void onPostCreate(Bundle savedInstanceState) {
396 super.onPostCreate(savedInstanceState);
397
398 // This can't be done in onCreate(), since the auto-restoring of the digits
399 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
400 // is called. This method will be called every time the activity is created, and
401 // will always happen after onRestoreSavedInstanceState().
402 mDigits.addTextChangedListener(this);
403 }
404
405 private void setupKeypad() {
406 // Setup the listeners for the buttons
407 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700408 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
409 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700410 }
411
412 View view = findViewById(R.id.zero);
413 view.setOnLongClickListener(this);
414 }
415
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800416 @Override
417 public void onBackPressed() {
418 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800419 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800420 // Otherwise, it would finish the activity.
421 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
422 && mDialpadView.getVisibility() == View.VISIBLE) {
423 switchView(mEmergencyShortcutView, mDialpadView, true);
424 return;
425 }
426 super.onBackPressed();
427 }
428
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700429 /**
430 * handle key events
431 */
432 @Override
433 public boolean onKeyDown(int keyCode, KeyEvent event) {
434 switch (keyCode) {
435 // Happen when there's a "Call" hard button.
436 case KeyEvent.KEYCODE_CALL: {
437 if (TextUtils.isEmpty(mDigits.getText().toString())) {
438 // if we are adding a call from the InCallScreen and the phone
439 // number entered is empty, we just close the dialer to expose
440 // the InCallScreen under it.
441 finish();
442 } else {
443 // otherwise, we place the call.
444 placeCall();
445 }
446 return true;
447 }
448 }
449 return super.onKeyDown(keyCode, event);
450 }
451
452 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700453 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700454 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
455 mDigits.onKeyDown(keyCode, event);
456 }
457
458 @Override
459 public boolean onKey(View view, int keyCode, KeyEvent event) {
460 switch (view.getId()) {
461 case R.id.digits:
462 // Happen when "Done" button of the IME is pressed. This can happen when this
463 // Activity is forced into landscape mode due to a desk dock.
464 if (keyCode == KeyEvent.KEYCODE_ENTER
465 && event.getAction() == KeyEvent.ACTION_UP) {
466 placeCall();
467 return true;
468 }
469 break;
470 }
471 return false;
472 }
473
474 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700475 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800476 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700477 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800478 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700479 return handled;
480 }
481
482 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800483 public void onConfirmClick(EmergencyShortcutButton button) {
484 if (button == null) return;
485
486 String phoneNumber = button.getPhoneNumber();
487
488 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800489 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800490 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
491 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
492 } else {
493 Log.d(LOG_TAG, "emergency number is empty");
494 }
495 }
496
497 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700498 public void onClick(View view) {
499 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700500 case R.id.deleteButton: {
501 keyPressed(KeyEvent.KEYCODE_DEL);
502 return;
503 }
504 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700505 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700506 placeCall();
507 return;
508 }
509 case R.id.digits: {
510 if (mDigits.length() != 0) {
511 mDigits.setCursorVisible(true);
512 }
513 return;
514 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800515 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800516 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800517 switchView(mDialpadView, mEmergencyShortcutView, true);
518 return;
519 }
520 case R.id.emergency_info_button: {
521 Intent intent = (Intent) view.getTag(R.id.tag_intent);
522 if (intent != null) {
523 startActivity(intent);
524 }
525 return;
526 }
Yorke Lee23a70732014-08-14 17:12:01 -0700527 }
528 }
529
530 @Override
531 public void onPressed(View view, boolean pressed) {
532 if (!pressed) {
533 return;
534 }
535 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700536 case R.id.one: {
537 playTone(ToneGenerator.TONE_DTMF_1);
538 keyPressed(KeyEvent.KEYCODE_1);
539 return;
540 }
541 case R.id.two: {
542 playTone(ToneGenerator.TONE_DTMF_2);
543 keyPressed(KeyEvent.KEYCODE_2);
544 return;
545 }
546 case R.id.three: {
547 playTone(ToneGenerator.TONE_DTMF_3);
548 keyPressed(KeyEvent.KEYCODE_3);
549 return;
550 }
551 case R.id.four: {
552 playTone(ToneGenerator.TONE_DTMF_4);
553 keyPressed(KeyEvent.KEYCODE_4);
554 return;
555 }
556 case R.id.five: {
557 playTone(ToneGenerator.TONE_DTMF_5);
558 keyPressed(KeyEvent.KEYCODE_5);
559 return;
560 }
561 case R.id.six: {
562 playTone(ToneGenerator.TONE_DTMF_6);
563 keyPressed(KeyEvent.KEYCODE_6);
564 return;
565 }
566 case R.id.seven: {
567 playTone(ToneGenerator.TONE_DTMF_7);
568 keyPressed(KeyEvent.KEYCODE_7);
569 return;
570 }
571 case R.id.eight: {
572 playTone(ToneGenerator.TONE_DTMF_8);
573 keyPressed(KeyEvent.KEYCODE_8);
574 return;
575 }
576 case R.id.nine: {
577 playTone(ToneGenerator.TONE_DTMF_9);
578 keyPressed(KeyEvent.KEYCODE_9);
579 return;
580 }
581 case R.id.zero: {
582 playTone(ToneGenerator.TONE_DTMF_0);
583 keyPressed(KeyEvent.KEYCODE_0);
584 return;
585 }
586 case R.id.pound: {
587 playTone(ToneGenerator.TONE_DTMF_P);
588 keyPressed(KeyEvent.KEYCODE_POUND);
589 return;
590 }
591 case R.id.star: {
592 playTone(ToneGenerator.TONE_DTMF_S);
593 keyPressed(KeyEvent.KEYCODE_STAR);
594 return;
595 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700596 }
597 }
598
599 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 * called for long touch events
601 */
602 @Override
603 public boolean onLongClick(View view) {
604 int id = view.getId();
605 switch (id) {
606 case R.id.deleteButton: {
607 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700608 return true;
609 }
610 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700611 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700612 keyPressed(KeyEvent.KEYCODE_PLUS);
613 return true;
614 }
615 }
616 return false;
617 }
618
619 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700620 protected void onStart() {
621 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800622 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
623 // And set background color to black.
624 if (mAreEmergencyDialerShortcutsEnabled) {
625 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
626 updateTheme(false);
627 } else {
628 mColorExtractor.addOnColorsChangedListener(this);
629 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
630 ColorExtractor.TYPE_EXTRA_DARK);
631 // Do not animate when view isn't visible yet, just set an initial state.
632 mBackgroundGradient.setColors(lockScreenColors, false);
633 updateTheme(lockScreenColors.supportsDarkText());
634 }
CY Cheng9a69c182018-06-15 21:20:10 +0800635
636 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
637 final Context context = this;
638 mEccInfoHelper.getCountryEccInfoAsync(context,
639 new EccInfoHelper.CountryEccInfoResultCallback() {
640 @Override
641 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
642 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
643 + Rlog.pii(LOG_TAG, iso));
644 updateLocationAndEccInfo(iso, countryEccInfo);
645 }
646
647 @Override
648 public void onDetectCountryFailed() {
649 Log.w(LOG_TAG, "Cannot detect current country.");
650 updateLocationAndEccInfo(null, null);
651 }
652
653 @Override
654 public void onRetrieveCountryEccInfoFailed(String iso) {
655 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
656 + Rlog.pii(LOG_TAG, iso));
657 updateLocationAndEccInfo(iso, null);
658 }
659 });
660 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700661 }
662
663 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 protected void onResume() {
665 super.onResume();
666
667 // retrieve the DTMF tone play back setting.
668 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
669 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
670
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700671 // if the mToneGenerator creation fails, just continue without it. It is
672 // a local audio signal, and is not as important as the dtmf tone itself.
673 synchronized (mToneGeneratorLock) {
674 if (mToneGenerator == null) {
675 try {
676 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
677 TONE_RELATIVE_VOLUME);
678 } catch (RuntimeException e) {
679 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
680 mToneGenerator = null;
681 }
682 }
683 }
684
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700685 updateDialAndDeleteButtonStateEnabledAttr();
686 }
687
688 @Override
689 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700690 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700691 }
692
Lucas Dupineb9c5702017-05-10 16:57:09 -0700693 @Override
694 protected void onStop() {
695 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700696 mColorExtractor.removeOnColorsChangedListener(this);
697 }
698
699 /**
700 * Sets theme based on gradient colors
701 * @param supportsDarkText true if gradient supports dark text
702 */
703 private void updateTheme(boolean supportsDarkText) {
704 if (mSupportsDarkText == supportsDarkText) {
705 return;
706 }
707 mSupportsDarkText = supportsDarkText;
708
709 // We can't change themes after inflation, in this case we'll have to recreate
710 // the whole activity.
711 if (mBackgroundGradient != null) {
712 recreate();
713 return;
714 }
715
716 int vis = getWindow().getDecorView().getSystemUiVisibility();
717 if (supportsDarkText) {
718 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
719 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
720 setTheme(R.style.EmergencyDialerThemeDark);
721 } else {
722 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
723 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
724 setTheme(R.style.EmergencyDialerTheme);
725 }
726 getWindow().getDecorView().setSystemUiVisibility(vis);
727 }
728
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 /**
730 * place the call, but check to make sure it is a viable number.
731 */
732 private void placeCall() {
733 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900734
735 // Convert into emergency number according to emergency conversion map.
736 // If conversion map is not defined (this is default), this method does
737 // nothing and just returns input number.
738 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
739
Yorke Lee36bb2542014-06-05 08:09:52 -0700740 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700741 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
742
743 // place the call if it is a valid number
744 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
745 // There is no number entered.
746 playTone(ToneGenerator.TONE_PROP_NACK);
747 return;
748 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700749 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
750 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 } else {
752 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
753
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
755 }
Yorke Lee9b341512014-10-17 11:36:41 -0700756 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700757 }
758
759 /**
760 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
761 *
762 * The tone is played locally, using the audio stream for phone calls.
763 * Tones are played only if the "Audible touch tones" user preference
764 * is checked, and are NOT played if the device is in silent mode.
765 *
766 * @param tone a tone code from {@link ToneGenerator}
767 */
768 void playTone(int tone) {
769 // if local tone playback is disabled, just return.
770 if (!mDTMFToneEnabled) {
771 return;
772 }
773
774 // Also do nothing if the phone is in silent mode.
775 // We need to re-check the ringer mode for *every* playTone()
776 // call, rather than keeping a local flag that's updated in
777 // onResume(), since it's possible to toggle silent mode without
778 // leaving the current activity (via the ENDCALL-longpress menu.)
779 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
780 int ringerMode = audioManager.getRingerMode();
781 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
782 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
783 return;
784 }
785
786 synchronized (mToneGeneratorLock) {
787 if (mToneGenerator == null) {
788 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
789 return;
790 }
791
792 // Start the new tone (will stop any playing tone)
793 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
794 }
795 }
796
797 private CharSequence createErrorMessage(String number) {
798 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800799 String errorString = getString(R.string.dial_emergency_error, number);
800 int startingPosition = errorString.indexOf(number);
801 int endingPosition = startingPosition + number.length();
802 Spannable result = new SpannableString(errorString);
803 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
804 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700805 } else {
806 return getText(R.string.dial_emergency_empty_error).toString();
807 }
808 }
809
810 @Override
811 protected Dialog onCreateDialog(int id) {
812 AlertDialog dialog = null;
813 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
814 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700815 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700816 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
817 .setMessage(createErrorMessage(mLastNumber))
818 .setPositiveButton(R.string.ok, null)
819 .setCancelable(true).create();
820
821 // blur stuff behind the dialog
822 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700823 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700824 }
825 return dialog;
826 }
827
828 @Override
829 protected void onPrepareDialog(int id, Dialog dialog) {
830 super.onPrepareDialog(id, dialog);
831 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
832 AlertDialog alert = (AlertDialog) dialog;
833 alert.setMessage(createErrorMessage(mLastNumber));
834 }
835 }
836
Andrew Leed5631e82014-10-08 16:03:58 -0700837 @Override
838 public boolean onOptionsItemSelected(MenuItem item) {
839 final int itemId = item.getItemId();
840 if (itemId == android.R.id.home) {
841 onBackPressed();
842 return true;
843 }
844 return super.onOptionsItemSelected(item);
845 }
846
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700847 /**
848 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
849 */
850 private void updateDialAndDeleteButtonStateEnabledAttr() {
851 final boolean notEmpty = mDigits.length() != 0;
852
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 mDelete.setEnabled(notEmpty);
854 }
Yorke Lee91311662014-10-24 14:50:45 -0700855
856 /**
857 * Remove the digit just before the current position. Used by various long pressed callbacks
858 * to remove the digit that was populated as a result of the short click.
859 */
860 private void removePreviousDigitIfPossible() {
861 final int currentPosition = mDigits.getSelectionStart();
862 if (currentPosition > 0) {
863 mDigits.setSelection(currentPosition);
864 mDigits.getText().delete(currentPosition - 1, currentPosition);
865 }
866 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800867
868 /**
869 * Update the text-to-speech annotations in the edit field.
870 */
871 private void updateTtsSpans() {
872 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
873 mDigits.getText().removeSpan(o);
874 }
875 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
876 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700877
878 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700879 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700880 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700881 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
882 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700883 mBackgroundGradient.setColors(colors);
884 updateTheme(colors.supportsDarkText());
885 }
886 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700887
888 /**
889 * Where a carrier requires a warning that emergency calling is not available while on WFC,
890 * add hint text above the dial pad which warns the user of this case.
891 */
892 private void maybeShowWfcEmergencyCallingWarning() {
893 if (!mIsWfcEmergencyCallingWarningEnabled) {
894 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
895 return;
896 }
897
898 // Use an async task rather than calling into Telephony on UI thread.
899 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
900 @Override
901 protected Boolean doInBackground(Void... voids) {
902 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
903 boolean isWfcAvailable = tm.isWifiCallingAvailable();
904 ServiceState ss = tm.getServiceState();
905 boolean isCellAvailable =
906 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
907 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
908 + " isCellAvailable=" + isCellAvailable
909 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
910 return isWfcAvailable && !isCellAvailable;
911 }
912
913 @Override
914 protected void onPostExecute(Boolean result) {
915 if (result.booleanValue()) {
916 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
917 mDigits.setHint(R.string.dial_emergency_calling_not_available);
918 } else {
919 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
920 mDigits.setHint("");
921 }
922 maybeChangeHintSize();
923 }
924 };
925 showWfcWarningTask.execute((Void) null);
926 }
927
928 /**
929 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
930 * edit view and set the font size to a smaller size appropriate for the emergency calling
931 * warning.
932 */
933 private void maybeChangeHintSize() {
934 if (TextUtils.isEmpty(mDigits.getHint())
935 || !TextUtils.isEmpty(mDigits.getText().toString())) {
936 // No hint or there are dialed digits, so use default size.
937 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
938 // By default, the digits view auto-resizes to fit the text it contains, so
939 // enable that now.
940 mDigits.setResizeEnabled(true);
941 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
942 } else {
943 // Hint present and no dialed digits, set custom font size appropriate for the warning.
944 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
945 R.dimen.emergency_call_warning_size));
946 // Since we're populating this with a static text string, disable auto-resize.
947 mDigits.setResizeEnabled(false);
948 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
949 }
950 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800951
952 private void setupEmergencyShortcutsView() {
953 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
954 mDialpadView = findViewById(R.id.emergency_dialer);
955
Chihhang Chuangcaba0da2018-08-02 22:25:06 +0800956 mEmergencyShortcutView.setAccessibilityDelegate(mAccessibilityDelegate);
957 mDialpadView.setAccessibilityDelegate(mAccessibilityDelegate);
958
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800959 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
960 dialpadButton.setOnClickListener(this);
961
962 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
963 emergencyInfoButton.setOnClickListener(this);
964
965 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
966 mEmergencyActionGroup.setVisibility(View.GONE);
967
968 // Setup dialpad title.
969 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
970 emergencyDialpadTitle.setVisibility(View.VISIBLE);
971
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800972 mEmergencyShortcutButtonList = new ArrayList<>();
973 setupEmergencyCallShortcutButton();
974
CY Cheng9a69c182018-06-15 21:20:10 +0800975 updateLocationAndEccInfo(null, null);
976
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800977 switchView(mEmergencyShortcutView, mDialpadView, false);
978 }
979
CY Cheng9a69c182018-06-15 21:20:10 +0800980 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800981 final View locationInfo = findViewById(R.id.location_info);
982
CY Cheng9a69c182018-06-15 21:20:10 +0800983 String countryName = null;
984 if (!TextUtils.isEmpty(countryIso)) {
985 Locale locale = Locale.getDefault();
986 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
987 .getDisplayCountry();
988 }
989 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800990 locationInfo.setVisibility(View.INVISIBLE);
991 } else {
992 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800993 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800994 locationInfo.setVisibility(View.VISIBLE);
995 }
996 }
997
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800998 private void setupEmergencyCallShortcutButton() {
999 final ViewGroup shortcutButtonContainer = findViewById(
1000 R.id.emergency_shortcut_buttons_container);
1001 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +08001002 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001003
CY Cheng9a69c182018-06-15 21:20:10 +08001004 mShortcutAdapter = new EccShortcutAdapter(this) {
1005 @Override
1006 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
1007 CharSequence description, int iconRes) {
1008 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
1009 .inflate(R.layout.emergency_shortcut_button, parent, false);
1010 button.setPhoneNumber(number);
1011 button.setPhoneDescription(description);
1012 button.setPhoneTypeIcon(iconRes);
1013 button.setOnConfirmClickListener(EmergencyDialer.this);
1014 return button;
1015 }
1016 };
1017 mShortcutDataSetObserver = new DataSetObserver() {
1018 @Override
1019 public void onChanged() {
1020 super.onChanged();
1021 updateLayout();
1022 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001023
CY Cheng9a69c182018-06-15 21:20:10 +08001024 @Override
1025 public void onInvalidated() {
1026 super.onInvalidated();
1027 updateLayout();
1028 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001029
CY Cheng9a69c182018-06-15 21:20:10 +08001030 private void updateLayout() {
1031 // clear previous added buttons
1032 shortcutButtonContainer.removeAllViews();
1033 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001034
CY Cheng9a69c182018-06-15 21:20:10 +08001035 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1036 EmergencyShortcutButton button = (EmergencyShortcutButton)
1037 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1038 mEmergencyShortcutButtonList.add(button);
1039 shortcutButtonContainer.addView(button);
1040 }
1041
1042 // update emergency numbers title for numerous buttons.
1043 if (mEmergencyShortcutButtonList.size() > 1) {
1044 emergencyNumberTitle.setText(getString(
1045 R.string.numerous_emergency_numbers_title));
1046 } else {
1047 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1048 }
1049 }
1050 };
1051 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1052 }
1053
1054 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1055 if (!isFinishing() && !isDestroyed()) {
1056 setLocationInfo(iso);
1057 if (mShortcutAdapter != null) {
1058 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1059 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001060 }
1061 }
1062
1063 /**
1064 * Called by the activity before a touch event is dispatched to the view hierarchy.
1065 */
1066 private void onPreTouchEvent(MotionEvent event) {
1067 mEmergencyActionGroup.onPreTouchEvent(event);
1068
1069 if (mEmergencyShortcutButtonList != null) {
1070 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1071 button.onPreTouchEvent(event);
1072 }
1073 }
1074 }
1075
1076 /**
1077 * Called by the activity after a touch event is dispatched to the view hierarchy.
1078 */
1079 private void onPostTouchEvent(MotionEvent event) {
1080 mEmergencyActionGroup.onPostTouchEvent(event);
1081
1082 if (mEmergencyShortcutButtonList != null) {
1083 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1084 button.onPostTouchEvent(event);
1085 }
1086 }
1087 }
1088
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001089 /**
1090 * Switch two view.
1091 *
1092 * @param displayView the view would be displayed.
1093 * @param hideView the view would be hidden.
1094 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1095 */
1096 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1097 if (displayView == null || hideView == null) {
1098 return;
1099 }
1100
1101 if (displayView.getVisibility() == View.VISIBLE) {
1102 return;
1103 }
1104
1105 if (hasAnimation) {
1106 crossfade(hideView, displayView);
1107 } else {
1108 hideView.setVisibility(View.GONE);
1109 displayView.setVisibility(View.VISIBLE);
1110 }
1111 }
1112
1113 /**
1114 * Fade out and fade in animation between two view transition.
1115 */
1116 private void crossfade(View fadeOutView, View fadeInView) {
1117 if (fadeOutView == null || fadeInView == null) {
1118 return;
1119 }
1120 final int shortAnimationDuration = getResources().getInteger(
1121 android.R.integer.config_shortAnimTime);
1122
1123 fadeInView.setAlpha(0f);
1124 fadeInView.setVisibility(View.VISIBLE);
1125
1126 fadeInView.animate()
1127 .alpha(1f)
1128 .setDuration(shortAnimationDuration)
1129 .setListener(null);
1130
1131 fadeOutView.animate()
1132 .alpha(0f)
1133 .setDuration(shortAnimationDuration)
1134 .setListener(new AnimatorListenerAdapter() {
1135 @Override
1136 public void onAnimationEnd(Animator animation) {
1137 fadeOutView.setVisibility(View.GONE);
1138 }
1139 });
1140 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141}