blob: 5d948970e7658f7862fb992ffe45ba31c278536e [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunn1acf54a2018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangd301d9a2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
CY Cheng42873672018-06-15 21:20:10 +080031import android.database.DataSetObserver;
Chihhang Chuanga26f07b2018-07-02 11:08:50 +080032import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070033import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
35import android.media.ToneGenerator;
36import android.net.Uri;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070037import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070041import android.telecom.PhoneAccount;
Tyler Gunnca7dfef2018-03-27 18:50:05 +000042import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.PhoneNumberUtils;
Billy Chi9614de82018-06-15 19:00:15 +080045import android.telephony.Rlog;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070046import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070047import android.telephony.SubscriptionManager;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070050import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080051import android.text.Spannable;
52import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.text.TextUtils;
54import android.text.TextWatcher;
55import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080056import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.util.Log;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070058import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070059import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070061import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070062import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.View;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080064import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.WindowManager;
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080066import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Lucas Dupinaf9e9912017-06-22 12:39:39 -070068import com.android.internal.colorextraction.ColorExtractor;
69import com.android.internal.colorextraction.ColorExtractor.GradientColors;
70import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070071import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070072import com.android.phone.common.util.ViewUtil;
Tyler Gunn1acf54a2018-03-28 14:15:34 -070073import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng42873672018-06-15 21:20:10 +080074import com.android.phone.ecc.CountryEccInfo;
75import com.android.phone.ecc.EccInfoHelper;
Leo Hsu8f1c6712018-07-10 11:37:52 +080076import com.android.phone.ecc.IsoToEccProtobufRepository;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang4f2b7812018-06-07 16:25:27 +080078import java.util.ArrayList;
79import java.util.List;
CY Cheng42873672018-06-15 21:20:10 +080080import java.util.Locale;
Chihhang Chuang4f2b7812018-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 Chuangd301d9a2018-06-05 15:29:06 +080098 *
99 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang4f2b7812018-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 Chuangd301d9a2018-06-05 15:29:06 +0800102 * TODO item:
Chihhang Chuang4f2b7812018-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 Chuang4f2b7812018-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 Chuanga26f07b2018-07-02 11:08:50 +0800140 /** 85% opacity for black background **/
141 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
142
CY Cheng42873672018-06-15 21:20:10 +0800143 /** Size limit of emergency shortcut buttons container. **/
144 private static final int SHORTCUT_SIZE_LIMIT = 3;
145
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700146 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private View mDialButton;
148 private View mDelete;
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800149 private View mEmergencyShortcutView;
150 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
CY Cheng42873672018-06-15 21:20:10 +0800152 private EccInfoHelper mEccInfoHelper;
153
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800154 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng42873672018-06-15 21:20:10 +0800155 private EccShortcutAdapter mShortcutAdapter;
156 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang4f2b7812018-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 Gunn1acf54a2018-03-28 14:15:34 -0700183 private boolean mIsWfcEmergencyCallingWarningEnabled;
184 private float mDefaultDigitsTextSize;
185
Chihhang Chuangd301d9a2018-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 Gunn1acf54a2018-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.
223 WindowManager.LayoutParams lp = getWindow().getAttributes();
224 lp.flags |= WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
Santos Cordonfc309812013-08-20 18:33:16 -0700225
226 // When no proximity sensor is available, use a shorter timeout.
Christine Chen07fae162013-09-19 15:05:56 -0700227 // TODO: Do we enable this for non proximity devices any more?
Santos Cordonfc309812013-08-20 18:33:16 -0700228 // lp.userActivityTimeout = USER_ACTIVITY_TIMEOUT_WHEN_NO_PROX_SENSOR;
229
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 getWindow().setAttributes(lp);
231
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800232 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
233 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng42873672018-06-15 21:20:10 +0800234 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
235 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800236
Lucas Dupineb9c5702017-05-10 16:57:09 -0700237 mColorExtractor = new ColorExtractor(this);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800238
239 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
240 // And the background color is black with 85% opacity.
241 if (mAreEmergencyDialerShortcutsEnabled) {
242 updateTheme(false);
243 } else {
244 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
245 ColorExtractor.TYPE_EXTRA_DARK);
246 updateTheme(lockScreenColors.supportsDarkText());
247 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700248
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 setContentView(R.layout.emergency_dialer);
250
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700251 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 mDigits.setKeyListener(DialerKeyListener.getInstance());
253 mDigits.setOnClickListener(this);
254 mDigits.setOnKeyListener(this);
255 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700256 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700257 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 maybeAddNumberFormatting();
259
Lucas Dupineb9c5702017-05-10 16:57:09 -0700260 mBackgroundGradient = new GradientDrawable(this);
261 Point displaySize = new Point();
262 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
263 .getDefaultDisplay().getSize(displaySize);
264 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800265 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
266 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700267 getWindow().setBackgroundDrawable(mBackgroundGradient);
268
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700269 // Check for the presence of the keypad
270 View view = findViewById(R.id.one);
271 if (view != null) {
272 setupKeypad();
273 }
274
275 mDelete = findViewById(R.id.deleteButton);
276 mDelete.setOnClickListener(this);
277 mDelete.setOnLongClickListener(this);
278
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700279 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280
281 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700282 // Read carrier config through the public API because PhoneGlobals is not available when we
283 // run as a secondary user.
284 CarrierConfigManager configMgr =
285 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
286 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800287 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700288
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700289 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 mDialButton.setOnClickListener(this);
291 } else {
292 mDialButton.setVisibility(View.GONE);
293 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700294 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
295 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
296 maybeShowWfcEmergencyCallingWarning();
297
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700298 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299
300 if (icicle != null) {
301 super.onRestoreInstanceState(icicle);
302 }
303
304 // Extract phone number from intent
305 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700306 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700307 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
308 if (number != null) {
309 mDigits.setText(number);
310 }
311 }
312
313 // if the mToneGenerator creation fails, just continue without it. It is
314 // a local audio signal, and is not as important as the dtmf tone itself.
315 synchronized (mToneGeneratorLock) {
316 if (mToneGenerator == null) {
317 try {
318 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
319 } catch (RuntimeException e) {
320 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
321 mToneGenerator = null;
322 }
323 }
324 }
325
326 final IntentFilter intentFilter = new IntentFilter();
327 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
328 registerReceiver(mBroadcastReceiver, intentFilter);
329
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700330 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800331
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800332 if (mAreEmergencyDialerShortcutsEnabled) {
Leo Hsu8f1c6712018-07-10 11:37:52 +0800333 mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository());
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800334 setupEmergencyShortcutsView();
335 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 }
337
338 @Override
339 protected void onDestroy() {
340 super.onDestroy();
341 synchronized (mToneGeneratorLock) {
342 if (mToneGenerator != null) {
343 mToneGenerator.release();
344 mToneGenerator = null;
345 }
346 }
347 unregisterReceiver(mBroadcastReceiver);
CY Cheng42873672018-06-15 21:20:10 +0800348 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
349 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
350 mShortcutDataSetObserver = null;
351 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 }
353
354 @Override
355 protected void onRestoreInstanceState(Bundle icicle) {
356 mLastNumber = icicle.getString(LAST_NUMBER);
357 }
358
359 @Override
360 protected void onSaveInstanceState(Bundle outState) {
361 super.onSaveInstanceState(outState);
362 outState.putString(LAST_NUMBER, mLastNumber);
363 }
364
365 /**
366 * Explicitly turn off number formatting, since it gets in the way of the emergency
367 * number detector
368 */
369 protected void maybeAddNumberFormatting() {
370 // Do nothing.
371 }
372
373 @Override
374 protected void onPostCreate(Bundle savedInstanceState) {
375 super.onPostCreate(savedInstanceState);
376
377 // This can't be done in onCreate(), since the auto-restoring of the digits
378 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
379 // is called. This method will be called every time the activity is created, and
380 // will always happen after onRestoreSavedInstanceState().
381 mDigits.addTextChangedListener(this);
382 }
383
384 private void setupKeypad() {
385 // Setup the listeners for the buttons
386 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700387 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
388 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700389 }
390
391 View view = findViewById(R.id.zero);
392 view.setOnLongClickListener(this);
393 }
394
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800395 @Override
396 public void onBackPressed() {
397 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi9614de82018-06-15 19:00:15 +0800398 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800399 // Otherwise, it would finish the activity.
400 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
401 && mDialpadView.getVisibility() == View.VISIBLE) {
402 switchView(mEmergencyShortcutView, mDialpadView, true);
403 return;
404 }
405 super.onBackPressed();
406 }
407
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 /**
409 * handle key events
410 */
411 @Override
412 public boolean onKeyDown(int keyCode, KeyEvent event) {
413 switch (keyCode) {
414 // Happen when there's a "Call" hard button.
415 case KeyEvent.KEYCODE_CALL: {
416 if (TextUtils.isEmpty(mDigits.getText().toString())) {
417 // if we are adding a call from the InCallScreen and the phone
418 // number entered is empty, we just close the dialer to expose
419 // the InCallScreen under it.
420 finish();
421 } else {
422 // otherwise, we place the call.
423 placeCall();
424 }
425 return true;
426 }
427 }
428 return super.onKeyDown(keyCode, event);
429 }
430
431 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700432 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700433 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
434 mDigits.onKeyDown(keyCode, event);
435 }
436
437 @Override
438 public boolean onKey(View view, int keyCode, KeyEvent event) {
439 switch (view.getId()) {
440 case R.id.digits:
441 // Happen when "Done" button of the IME is pressed. This can happen when this
442 // Activity is forced into landscape mode due to a desk dock.
443 if (keyCode == KeyEvent.KEYCODE_ENTER
444 && event.getAction() == KeyEvent.ACTION_UP) {
445 placeCall();
446 return true;
447 }
448 break;
449 }
450 return false;
451 }
452
453 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700454 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800455 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700456 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800457 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700458 return handled;
459 }
460
461 @Override
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800462 public void onConfirmClick(EmergencyShortcutButton button) {
463 if (button == null) return;
464
465 String phoneNumber = button.getPhoneNumber();
466
467 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi9614de82018-06-15 19:00:15 +0800468 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800469 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
470 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
471 } else {
472 Log.d(LOG_TAG, "emergency number is empty");
473 }
474 }
475
476 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700477 public void onClick(View view) {
478 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700479 case R.id.deleteButton: {
480 keyPressed(KeyEvent.KEYCODE_DEL);
481 return;
482 }
483 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700484 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700485 placeCall();
486 return;
487 }
488 case R.id.digits: {
489 if (mDigits.length() != 0) {
490 mDigits.setCursorVisible(true);
491 }
492 return;
493 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800494 case R.id.floating_action_button_dialpad: {
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800495 mDigits.getText().clear();
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800496 switchView(mDialpadView, mEmergencyShortcutView, true);
497 return;
498 }
499 case R.id.emergency_info_button: {
500 Intent intent = (Intent) view.getTag(R.id.tag_intent);
501 if (intent != null) {
502 startActivity(intent);
503 }
504 return;
505 }
Yorke Lee23a70732014-08-14 17:12:01 -0700506 }
507 }
508
509 @Override
510 public void onPressed(View view, boolean pressed) {
511 if (!pressed) {
512 return;
513 }
514 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700515 case R.id.one: {
516 playTone(ToneGenerator.TONE_DTMF_1);
517 keyPressed(KeyEvent.KEYCODE_1);
518 return;
519 }
520 case R.id.two: {
521 playTone(ToneGenerator.TONE_DTMF_2);
522 keyPressed(KeyEvent.KEYCODE_2);
523 return;
524 }
525 case R.id.three: {
526 playTone(ToneGenerator.TONE_DTMF_3);
527 keyPressed(KeyEvent.KEYCODE_3);
528 return;
529 }
530 case R.id.four: {
531 playTone(ToneGenerator.TONE_DTMF_4);
532 keyPressed(KeyEvent.KEYCODE_4);
533 return;
534 }
535 case R.id.five: {
536 playTone(ToneGenerator.TONE_DTMF_5);
537 keyPressed(KeyEvent.KEYCODE_5);
538 return;
539 }
540 case R.id.six: {
541 playTone(ToneGenerator.TONE_DTMF_6);
542 keyPressed(KeyEvent.KEYCODE_6);
543 return;
544 }
545 case R.id.seven: {
546 playTone(ToneGenerator.TONE_DTMF_7);
547 keyPressed(KeyEvent.KEYCODE_7);
548 return;
549 }
550 case R.id.eight: {
551 playTone(ToneGenerator.TONE_DTMF_8);
552 keyPressed(KeyEvent.KEYCODE_8);
553 return;
554 }
555 case R.id.nine: {
556 playTone(ToneGenerator.TONE_DTMF_9);
557 keyPressed(KeyEvent.KEYCODE_9);
558 return;
559 }
560 case R.id.zero: {
561 playTone(ToneGenerator.TONE_DTMF_0);
562 keyPressed(KeyEvent.KEYCODE_0);
563 return;
564 }
565 case R.id.pound: {
566 playTone(ToneGenerator.TONE_DTMF_P);
567 keyPressed(KeyEvent.KEYCODE_POUND);
568 return;
569 }
570 case R.id.star: {
571 playTone(ToneGenerator.TONE_DTMF_S);
572 keyPressed(KeyEvent.KEYCODE_STAR);
573 return;
574 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700575 }
576 }
577
578 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700579 * called for long touch events
580 */
581 @Override
582 public boolean onLongClick(View view) {
583 int id = view.getId();
584 switch (id) {
585 case R.id.deleteButton: {
586 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700587 return true;
588 }
589 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700590 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700591 keyPressed(KeyEvent.KEYCODE_PLUS);
592 return true;
593 }
594 }
595 return false;
596 }
597
598 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700599 protected void onStart() {
600 super.onStart();
Chihhang Chuanga26f07b2018-07-02 11:08:50 +0800601 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
602 // And set background color to black.
603 if (mAreEmergencyDialerShortcutsEnabled) {
604 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
605 updateTheme(false);
606 } else {
607 mColorExtractor.addOnColorsChangedListener(this);
608 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
609 ColorExtractor.TYPE_EXTRA_DARK);
610 // Do not animate when view isn't visible yet, just set an initial state.
611 mBackgroundGradient.setColors(lockScreenColors, false);
612 updateTheme(lockScreenColors.supportsDarkText());
613 }
CY Cheng42873672018-06-15 21:20:10 +0800614
615 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
616 final Context context = this;
617 mEccInfoHelper.getCountryEccInfoAsync(context,
618 new EccInfoHelper.CountryEccInfoResultCallback() {
619 @Override
620 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
621 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
622 + Rlog.pii(LOG_TAG, iso));
623 updateLocationAndEccInfo(iso, countryEccInfo);
624 }
625
626 @Override
627 public void onDetectCountryFailed() {
628 Log.w(LOG_TAG, "Cannot detect current country.");
629 updateLocationAndEccInfo(null, null);
630 }
631
632 @Override
633 public void onRetrieveCountryEccInfoFailed(String iso) {
634 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
635 + Rlog.pii(LOG_TAG, iso));
636 updateLocationAndEccInfo(iso, null);
637 }
638 });
639 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700640 }
641
642 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 protected void onResume() {
644 super.onResume();
645
646 // retrieve the DTMF tone play back setting.
647 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
648 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
649
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700650 // if the mToneGenerator creation fails, just continue without it. It is
651 // a local audio signal, and is not as important as the dtmf tone itself.
652 synchronized (mToneGeneratorLock) {
653 if (mToneGenerator == null) {
654 try {
655 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
656 TONE_RELATIVE_VOLUME);
657 } catch (RuntimeException e) {
658 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
659 mToneGenerator = null;
660 }
661 }
662 }
663
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700664 updateDialAndDeleteButtonStateEnabledAttr();
665 }
666
667 @Override
668 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700669 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 }
671
Lucas Dupineb9c5702017-05-10 16:57:09 -0700672 @Override
673 protected void onStop() {
674 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700675 mColorExtractor.removeOnColorsChangedListener(this);
676 }
677
678 /**
679 * Sets theme based on gradient colors
680 * @param supportsDarkText true if gradient supports dark text
681 */
682 private void updateTheme(boolean supportsDarkText) {
683 if (mSupportsDarkText == supportsDarkText) {
684 return;
685 }
686 mSupportsDarkText = supportsDarkText;
687
688 // We can't change themes after inflation, in this case we'll have to recreate
689 // the whole activity.
690 if (mBackgroundGradient != null) {
691 recreate();
692 return;
693 }
694
695 int vis = getWindow().getDecorView().getSystemUiVisibility();
696 if (supportsDarkText) {
697 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
698 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
699 setTheme(R.style.EmergencyDialerThemeDark);
700 } else {
701 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
702 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
703 setTheme(R.style.EmergencyDialerTheme);
704 }
705 getWindow().getDecorView().setSystemUiVisibility(vis);
706 }
707
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700708 /**
709 * place the call, but check to make sure it is a viable number.
710 */
711 private void placeCall() {
712 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900713
714 // Convert into emergency number according to emergency conversion map.
715 // If conversion map is not defined (this is default), this method does
716 // nothing and just returns input number.
717 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
718
Yorke Lee36bb2542014-06-05 08:09:52 -0700719 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700720 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
721
722 // place the call if it is a valid number
723 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
724 // There is no number entered.
725 playTone(ToneGenerator.TONE_PROP_NACK);
726 return;
727 }
Tyler Gunnca7dfef2018-03-27 18:50:05 +0000728 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
729 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700730 } else {
731 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
732
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700733 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
734 }
Yorke Lee9b341512014-10-17 11:36:41 -0700735 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700736 }
737
738 /**
739 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
740 *
741 * The tone is played locally, using the audio stream for phone calls.
742 * Tones are played only if the "Audible touch tones" user preference
743 * is checked, and are NOT played if the device is in silent mode.
744 *
745 * @param tone a tone code from {@link ToneGenerator}
746 */
747 void playTone(int tone) {
748 // if local tone playback is disabled, just return.
749 if (!mDTMFToneEnabled) {
750 return;
751 }
752
753 // Also do nothing if the phone is in silent mode.
754 // We need to re-check the ringer mode for *every* playTone()
755 // call, rather than keeping a local flag that's updated in
756 // onResume(), since it's possible to toggle silent mode without
757 // leaving the current activity (via the ENDCALL-longpress menu.)
758 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
759 int ringerMode = audioManager.getRingerMode();
760 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
761 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
762 return;
763 }
764
765 synchronized (mToneGeneratorLock) {
766 if (mToneGenerator == null) {
767 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
768 return;
769 }
770
771 // Start the new tone (will stop any playing tone)
772 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
773 }
774 }
775
776 private CharSequence createErrorMessage(String number) {
777 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800778 String errorString = getString(R.string.dial_emergency_error, number);
779 int startingPosition = errorString.indexOf(number);
780 int endingPosition = startingPosition + number.length();
781 Spannable result = new SpannableString(errorString);
782 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
783 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 } else {
785 return getText(R.string.dial_emergency_empty_error).toString();
786 }
787 }
788
789 @Override
790 protected Dialog onCreateDialog(int id) {
791 AlertDialog dialog = null;
792 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
793 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700794 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
796 .setMessage(createErrorMessage(mLastNumber))
797 .setPositiveButton(R.string.ok, null)
798 .setCancelable(true).create();
799
800 // blur stuff behind the dialog
801 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunn75638602018-03-29 13:48:29 -0700802 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700803 }
804 return dialog;
805 }
806
807 @Override
808 protected void onPrepareDialog(int id, Dialog dialog) {
809 super.onPrepareDialog(id, dialog);
810 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
811 AlertDialog alert = (AlertDialog) dialog;
812 alert.setMessage(createErrorMessage(mLastNumber));
813 }
814 }
815
Andrew Leed5631e82014-10-08 16:03:58 -0700816 @Override
817 public boolean onOptionsItemSelected(MenuItem item) {
818 final int itemId = item.getItemId();
819 if (itemId == android.R.id.home) {
820 onBackPressed();
821 return true;
822 }
823 return super.onOptionsItemSelected(item);
824 }
825
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700826 /**
827 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
828 */
829 private void updateDialAndDeleteButtonStateEnabledAttr() {
830 final boolean notEmpty = mDigits.length() != 0;
831
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 mDelete.setEnabled(notEmpty);
833 }
Yorke Lee91311662014-10-24 14:50:45 -0700834
835 /**
836 * Remove the digit just before the current position. Used by various long pressed callbacks
837 * to remove the digit that was populated as a result of the short click.
838 */
839 private void removePreviousDigitIfPossible() {
840 final int currentPosition = mDigits.getSelectionStart();
841 if (currentPosition > 0) {
842 mDigits.setSelection(currentPosition);
843 mDigits.getText().delete(currentPosition - 1, currentPosition);
844 }
845 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800846
847 /**
848 * Update the text-to-speech annotations in the edit field.
849 */
850 private void updateTtsSpans() {
851 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
852 mDigits.getText().removeSpan(o);
853 }
854 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
855 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700856
857 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700858 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700859 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700860 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
861 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700862 mBackgroundGradient.setColors(colors);
863 updateTheme(colors.supportsDarkText());
864 }
865 }
Tyler Gunn1acf54a2018-03-28 14:15:34 -0700866
867 /**
868 * Where a carrier requires a warning that emergency calling is not available while on WFC,
869 * add hint text above the dial pad which warns the user of this case.
870 */
871 private void maybeShowWfcEmergencyCallingWarning() {
872 if (!mIsWfcEmergencyCallingWarningEnabled) {
873 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
874 return;
875 }
876
877 // Use an async task rather than calling into Telephony on UI thread.
878 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
879 @Override
880 protected Boolean doInBackground(Void... voids) {
881 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
882 boolean isWfcAvailable = tm.isWifiCallingAvailable();
883 ServiceState ss = tm.getServiceState();
884 boolean isCellAvailable =
885 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
886 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
887 + " isCellAvailable=" + isCellAvailable
888 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
889 return isWfcAvailable && !isCellAvailable;
890 }
891
892 @Override
893 protected void onPostExecute(Boolean result) {
894 if (result.booleanValue()) {
895 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
896 mDigits.setHint(R.string.dial_emergency_calling_not_available);
897 } else {
898 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
899 mDigits.setHint("");
900 }
901 maybeChangeHintSize();
902 }
903 };
904 showWfcWarningTask.execute((Void) null);
905 }
906
907 /**
908 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
909 * edit view and set the font size to a smaller size appropriate for the emergency calling
910 * warning.
911 */
912 private void maybeChangeHintSize() {
913 if (TextUtils.isEmpty(mDigits.getHint())
914 || !TextUtils.isEmpty(mDigits.getText().toString())) {
915 // No hint or there are dialed digits, so use default size.
916 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
917 // By default, the digits view auto-resizes to fit the text it contains, so
918 // enable that now.
919 mDigits.setResizeEnabled(true);
920 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
921 } else {
922 // Hint present and no dialed digits, set custom font size appropriate for the warning.
923 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
924 R.dimen.emergency_call_warning_size));
925 // Since we're populating this with a static text string, disable auto-resize.
926 mDigits.setResizeEnabled(false);
927 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
928 }
929 }
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800930
931 private void setupEmergencyShortcutsView() {
932 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
933 mDialpadView = findViewById(R.id.emergency_dialer);
934
935 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
936 dialpadButton.setOnClickListener(this);
937
938 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
939 emergencyInfoButton.setOnClickListener(this);
940
941 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
942 mEmergencyActionGroup.setVisibility(View.GONE);
943
944 // Setup dialpad title.
945 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
946 emergencyDialpadTitle.setVisibility(View.VISIBLE);
947
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800948 mEmergencyShortcutButtonList = new ArrayList<>();
949 setupEmergencyCallShortcutButton();
950
CY Cheng42873672018-06-15 21:20:10 +0800951 updateLocationAndEccInfo(null, null);
952
Chihhang Chuangd301d9a2018-06-05 15:29:06 +0800953 switchView(mEmergencyShortcutView, mDialpadView, false);
954 }
955
CY Cheng42873672018-06-15 21:20:10 +0800956 private void setLocationInfo(String countryIso) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800957 final View locationInfo = findViewById(R.id.location_info);
958
CY Cheng42873672018-06-15 21:20:10 +0800959 String countryName = null;
960 if (!TextUtils.isEmpty(countryIso)) {
961 Locale locale = Locale.getDefault();
962 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
963 .getDisplayCountry();
964 }
965 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800966 locationInfo.setVisibility(View.INVISIBLE);
967 } else {
968 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng42873672018-06-15 21:20:10 +0800969 location.setText(countryName);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800970 locationInfo.setVisibility(View.VISIBLE);
971 }
972 }
973
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800974 private void setupEmergencyCallShortcutButton() {
975 final ViewGroup shortcutButtonContainer = findViewById(
976 R.id.emergency_shortcut_buttons_container);
977 shortcutButtonContainer.setClipToOutline(true);
CY Cheng42873672018-06-15 21:20:10 +0800978 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800979
CY Cheng42873672018-06-15 21:20:10 +0800980 mShortcutAdapter = new EccShortcutAdapter(this) {
981 @Override
982 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
983 CharSequence description, int iconRes) {
984 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
985 .inflate(R.layout.emergency_shortcut_button, parent, false);
986 button.setPhoneNumber(number);
987 button.setPhoneDescription(description);
988 button.setPhoneTypeIcon(iconRes);
989 button.setOnConfirmClickListener(EmergencyDialer.this);
990 return button;
991 }
992 };
993 mShortcutDataSetObserver = new DataSetObserver() {
994 @Override
995 public void onChanged() {
996 super.onChanged();
997 updateLayout();
998 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +0800999
CY Cheng42873672018-06-15 21:20:10 +08001000 @Override
1001 public void onInvalidated() {
1002 super.onInvalidated();
1003 updateLayout();
1004 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001005
CY Cheng42873672018-06-15 21:20:10 +08001006 private void updateLayout() {
1007 // clear previous added buttons
1008 shortcutButtonContainer.removeAllViews();
1009 mEmergencyShortcutButtonList.clear();
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001010
CY Cheng42873672018-06-15 21:20:10 +08001011 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1012 EmergencyShortcutButton button = (EmergencyShortcutButton)
1013 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1014 mEmergencyShortcutButtonList.add(button);
1015 shortcutButtonContainer.addView(button);
1016 }
1017
1018 // update emergency numbers title for numerous buttons.
1019 if (mEmergencyShortcutButtonList.size() > 1) {
1020 emergencyNumberTitle.setText(getString(
1021 R.string.numerous_emergency_numbers_title));
1022 } else {
1023 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1024 }
1025 }
1026 };
1027 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1028 }
1029
1030 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1031 if (!isFinishing() && !isDestroyed()) {
1032 setLocationInfo(iso);
1033 if (mShortcutAdapter != null) {
1034 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1035 }
Chihhang Chuang4f2b7812018-06-07 16:25:27 +08001036 }
1037 }
1038
1039 /**
1040 * Called by the activity before a touch event is dispatched to the view hierarchy.
1041 */
1042 private void onPreTouchEvent(MotionEvent event) {
1043 mEmergencyActionGroup.onPreTouchEvent(event);
1044
1045 if (mEmergencyShortcutButtonList != null) {
1046 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1047 button.onPreTouchEvent(event);
1048 }
1049 }
1050 }
1051
1052 /**
1053 * Called by the activity after a touch event is dispatched to the view hierarchy.
1054 */
1055 private void onPostTouchEvent(MotionEvent event) {
1056 mEmergencyActionGroup.onPostTouchEvent(event);
1057
1058 if (mEmergencyShortcutButtonList != null) {
1059 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1060 button.onPostTouchEvent(event);
1061 }
1062 }
1063 }
1064
Chihhang Chuangd301d9a2018-06-05 15:29:06 +08001065 /**
1066 * Switch two view.
1067 *
1068 * @param displayView the view would be displayed.
1069 * @param hideView the view would be hidden.
1070 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1071 */
1072 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1073 if (displayView == null || hideView == null) {
1074 return;
1075 }
1076
1077 if (displayView.getVisibility() == View.VISIBLE) {
1078 return;
1079 }
1080
1081 if (hasAnimation) {
1082 crossfade(hideView, displayView);
1083 } else {
1084 hideView.setVisibility(View.GONE);
1085 displayView.setVisibility(View.VISIBLE);
1086 }
1087 }
1088
1089 /**
1090 * Fade out and fade in animation between two view transition.
1091 */
1092 private void crossfade(View fadeOutView, View fadeInView) {
1093 if (fadeOutView == null || fadeInView == null) {
1094 return;
1095 }
1096 final int shortAnimationDuration = getResources().getInteger(
1097 android.R.integer.config_shortAnimTime);
1098
1099 fadeInView.setAlpha(0f);
1100 fadeInView.setVisibility(View.VISIBLE);
1101
1102 fadeInView.animate()
1103 .alpha(1f)
1104 .setDuration(shortAnimationDuration)
1105 .setListener(null);
1106
1107 fadeOutView.animate()
1108 .alpha(0f)
1109 .setDuration(shortAnimationDuration)
1110 .setListener(new AnimatorListenerAdapter() {
1111 @Override
1112 public void onAnimationEnd(Animator animation) {
1113 fadeOutView.setVisibility(View.GONE);
1114 }
1115 });
1116 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117}