blob: 4b00061b186047a9af7b06f822b4326934221bb0 [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;
Shaotang Li5d906c12018-07-19 17:32:42 +080041import android.telecom.ParcelableCallAnalytics;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070042import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070043import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070044import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080046import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070047import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070048import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070049import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070050import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070051import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080052import android.text.Spannable;
53import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.text.TextUtils;
55import android.text.TextWatcher;
56import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080057import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070059import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070060import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070062import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070063import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.view.View;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080065import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080067import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068
Lucas Dupinaf9e9912017-06-22 12:39:39 -070069import com.android.internal.colorextraction.ColorExtractor;
70import com.android.internal.colorextraction.ColorExtractor.GradientColors;
71import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070072import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070073import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070074import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080075import com.android.phone.ecc.CountryEccInfo;
76import com.android.phone.ecc.EccInfoHelper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077
Chihhang Chuang92cfe512018-06-07 16:25:27 +080078import java.util.ArrayList;
79import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080080import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080081
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082/**
83 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
84 *
85 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
86 * activity from apps/Contacts) that:
87 * 1. Allows ONLY emergency calls to be dialed
88 * 2. Disallows voicemail functionality
89 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
90 * activity to stay in front of the keyguard.
91 *
92 * TODO: Even though this is an ultra-simplified version of the normal
93 * dialer, there's still lots of code duplication between this class and
94 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
95 * moved into a shared base class that would live in the framework?
96 * Or could we figure out some way to move *this* class into apps/Contacts
97 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080098 *
99 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800100 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
101 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800102 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800103 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 */
105public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700106 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800107 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
108 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109 // Keys used with onSaveInstanceState().
110 private static final String LAST_NUMBER = "lastNumber";
111
112 // Intent action for this activity.
113 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
114
115 // List of dialer button IDs.
116 private static final int[] DIALER_KEYS = new int[] {
117 R.id.one, R.id.two, R.id.three,
118 R.id.four, R.id.five, R.id.six,
119 R.id.seven, R.id.eight, R.id.nine,
120 R.id.star, R.id.zero, R.id.pound };
121
122 // Debug constants.
123 private static final boolean DBG = false;
124 private static final String LOG_TAG = "EmergencyDialer";
125
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700126 /** The length of DTMF tones in milliseconds */
127 private static final int TONE_LENGTH_MS = 150;
128
129 /** The DTMF tone volume relative to other sounds in the stream */
130 private static final int TONE_RELATIVE_VOLUME = 80;
131
132 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
133 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
134
135 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
136
Lucas Dupineb9c5702017-05-10 16:57:09 -0700137 /** 90% opacity, different from other gradients **/
138 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
139
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800140 /** 85% opacity for black background **/
141 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
142
CY Cheng9a69c182018-06-15 21:20:10 +0800143 /** Size limit of emergency shortcut buttons container. **/
144 private static final int SHORTCUT_SIZE_LIMIT = 3;
145
Tyler Gunnc5428972018-03-28 14:15:34 -0700146 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700147 private View mDialButton;
148 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800149 private View mEmergencyShortcutView;
150 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
CY Cheng9a69c182018-06-15 21:20:10 +0800152 private EccInfoHelper mEccInfoHelper;
153
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800154 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800155 private EccShortcutAdapter mShortcutAdapter;
156 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800157
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158 private ToneGenerator mToneGenerator;
159 private Object mToneGeneratorLock = new Object();
160
161 // determines if we want to playback local DTMF tones.
162 private boolean mDTMFToneEnabled;
163
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700164 private EmergencyActionGroup mEmergencyActionGroup;
165
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166 // close activity when screen turns off
167 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
168 @Override
169 public void onReceive(Context context, Intent intent) {
170 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700171 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172 }
173 }
174 };
175
176 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
177
Lucas Dupineb9c5702017-05-10 16:57:09 -0700178 // Background gradient
179 private ColorExtractor mColorExtractor;
180 private GradientDrawable mBackgroundGradient;
181 private boolean mSupportsDarkText;
182
Tyler Gunnc5428972018-03-28 14:15:34 -0700183 private boolean mIsWfcEmergencyCallingWarningEnabled;
184 private float mDefaultDigitsTextSize;
185
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800186 private boolean mAreEmergencyDialerShortcutsEnabled;
187
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700188 @Override
189 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
190 // Do nothing
191 }
192
193 @Override
194 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700195 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196 }
197
198 @Override
199 public void afterTextChanged(Editable input) {
200 // Check for special sequences, in particular the "**04" or "**05"
201 // sequences that allow you to enter PIN or PUK-related codes.
202 //
203 // But note we *don't* allow most other special sequences here,
204 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
205 // since those shouldn't be available if the device is locked.
206 //
207 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
208 // here, not the regular handleChars() method.
209 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
210 // A special sequence was entered, clear the digits
211 mDigits.getText().clear();
212 }
213
214 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800215 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 }
217
218 @Override
219 protected void onCreate(Bundle icicle) {
220 super.onCreate(icicle);
221
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800223 setShowWhenLocked(true);
224 // Allow turning screen on
225 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800227 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
228 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng9a69c182018-06-15 21:20:10 +0800229 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
230 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800231
Lucas Dupineb9c5702017-05-10 16:57:09 -0700232 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800233
234 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
235 // And the background color is black with 85% opacity.
236 if (mAreEmergencyDialerShortcutsEnabled) {
237 updateTheme(false);
238 } else {
239 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
240 ColorExtractor.TYPE_EXTRA_DARK);
241 updateTheme(lockScreenColors.supportsDarkText());
242 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700243
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 setContentView(R.layout.emergency_dialer);
245
Tyler Gunnc5428972018-03-28 14:15:34 -0700246 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 mDigits.setKeyListener(DialerKeyListener.getInstance());
248 mDigits.setOnClickListener(this);
249 mDigits.setOnKeyListener(this);
250 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700251 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700252 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 maybeAddNumberFormatting();
254
Lucas Dupineb9c5702017-05-10 16:57:09 -0700255 mBackgroundGradient = new GradientDrawable(this);
256 Point displaySize = new Point();
257 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
258 .getDefaultDisplay().getSize(displaySize);
259 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800260 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
261 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700262 getWindow().setBackgroundDrawable(mBackgroundGradient);
263
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700264 // Check for the presence of the keypad
265 View view = findViewById(R.id.one);
266 if (view != null) {
267 setupKeypad();
268 }
269
270 mDelete = findViewById(R.id.deleteButton);
271 mDelete.setOnClickListener(this);
272 mDelete.setOnLongClickListener(this);
273
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700274 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275
276 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700277 // Read carrier config through the public API because PhoneGlobals is not available when we
278 // run as a secondary user.
279 CarrierConfigManager configMgr =
280 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
281 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800282 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700283
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700284 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 mDialButton.setOnClickListener(this);
286 } else {
287 mDialButton.setVisibility(View.GONE);
288 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700289 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
290 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
291 maybeShowWfcEmergencyCallingWarning();
292
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700293 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294
295 if (icicle != null) {
296 super.onRestoreInstanceState(icicle);
297 }
298
299 // Extract phone number from intent
300 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700301 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
303 if (number != null) {
304 mDigits.setText(number);
305 }
306 }
307
308 // if the mToneGenerator creation fails, just continue without it. It is
309 // a local audio signal, and is not as important as the dtmf tone itself.
310 synchronized (mToneGeneratorLock) {
311 if (mToneGenerator == null) {
312 try {
313 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
314 } catch (RuntimeException e) {
315 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
316 mToneGenerator = null;
317 }
318 }
319 }
320
321 final IntentFilter intentFilter = new IntentFilter();
322 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
323 registerReceiver(mBroadcastReceiver, intentFilter);
324
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700325 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800326
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800327 if (mAreEmergencyDialerShortcutsEnabled) {
328 setupEmergencyShortcutsView();
329 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 }
331
332 @Override
333 protected void onDestroy() {
334 super.onDestroy();
335 synchronized (mToneGeneratorLock) {
336 if (mToneGenerator != null) {
337 mToneGenerator.release();
338 mToneGenerator = null;
339 }
340 }
341 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800342 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
343 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
344 mShortcutDataSetObserver = null;
345 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 }
347
348 @Override
349 protected void onRestoreInstanceState(Bundle icicle) {
350 mLastNumber = icicle.getString(LAST_NUMBER);
351 }
352
353 @Override
354 protected void onSaveInstanceState(Bundle outState) {
355 super.onSaveInstanceState(outState);
356 outState.putString(LAST_NUMBER, mLastNumber);
357 }
358
359 /**
360 * Explicitly turn off number formatting, since it gets in the way of the emergency
361 * number detector
362 */
363 protected void maybeAddNumberFormatting() {
364 // Do nothing.
365 }
366
367 @Override
368 protected void onPostCreate(Bundle savedInstanceState) {
369 super.onPostCreate(savedInstanceState);
370
371 // This can't be done in onCreate(), since the auto-restoring of the digits
372 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
373 // is called. This method will be called every time the activity is created, and
374 // will always happen after onRestoreSavedInstanceState().
375 mDigits.addTextChangedListener(this);
376 }
377
378 private void setupKeypad() {
379 // Setup the listeners for the buttons
380 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700381 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
382 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700383 }
384
385 View view = findViewById(R.id.zero);
386 view.setOnLongClickListener(this);
387 }
388
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800389 @Override
390 public void onBackPressed() {
391 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800392 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800393 // Otherwise, it would finish the activity.
394 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
395 && mDialpadView.getVisibility() == View.VISIBLE) {
396 switchView(mEmergencyShortcutView, mDialpadView, true);
397 return;
398 }
399 super.onBackPressed();
400 }
401
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700402 /**
403 * handle key events
404 */
405 @Override
406 public boolean onKeyDown(int keyCode, KeyEvent event) {
407 switch (keyCode) {
408 // Happen when there's a "Call" hard button.
409 case KeyEvent.KEYCODE_CALL: {
410 if (TextUtils.isEmpty(mDigits.getText().toString())) {
411 // if we are adding a call from the InCallScreen and the phone
412 // number entered is empty, we just close the dialer to expose
413 // the InCallScreen under it.
414 finish();
415 } else {
416 // otherwise, we place the call.
417 placeCall();
418 }
419 return true;
420 }
421 }
422 return super.onKeyDown(keyCode, event);
423 }
424
425 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700426 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700427 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
428 mDigits.onKeyDown(keyCode, event);
429 }
430
431 @Override
432 public boolean onKey(View view, int keyCode, KeyEvent event) {
433 switch (view.getId()) {
434 case R.id.digits:
435 // Happen when "Done" button of the IME is pressed. This can happen when this
436 // Activity is forced into landscape mode due to a desk dock.
437 if (keyCode == KeyEvent.KEYCODE_ENTER
438 && event.getAction() == KeyEvent.ACTION_UP) {
439 placeCall();
440 return true;
441 }
442 break;
443 }
444 return false;
445 }
446
447 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700448 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800449 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700450 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800451 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700452 return handled;
453 }
454
455 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800456 public void onConfirmClick(EmergencyShortcutButton button) {
457 if (button == null) return;
458
459 String phoneNumber = button.getPhoneNumber();
460
461 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800462 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Shaotang Li5d906c12018-07-19 17:32:42 +0800463 Bundle extras = new Bundle();
464 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
465 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_SHORTCUT);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800466 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800467 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), extras);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800468 } else {
469 Log.d(LOG_TAG, "emergency number is empty");
470 }
471 }
472
473 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700474 public void onClick(View view) {
475 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700476 case R.id.deleteButton: {
477 keyPressed(KeyEvent.KEYCODE_DEL);
478 return;
479 }
480 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700481 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700482 placeCall();
483 return;
484 }
485 case R.id.digits: {
486 if (mDigits.length() != 0) {
487 mDigits.setCursorVisible(true);
488 }
489 return;
490 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800491 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800492 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800493 switchView(mDialpadView, mEmergencyShortcutView, true);
494 return;
495 }
496 case R.id.emergency_info_button: {
497 Intent intent = (Intent) view.getTag(R.id.tag_intent);
498 if (intent != null) {
499 startActivity(intent);
500 }
501 return;
502 }
Yorke Lee23a70732014-08-14 17:12:01 -0700503 }
504 }
505
506 @Override
507 public void onPressed(View view, boolean pressed) {
508 if (!pressed) {
509 return;
510 }
511 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700512 case R.id.one: {
513 playTone(ToneGenerator.TONE_DTMF_1);
514 keyPressed(KeyEvent.KEYCODE_1);
515 return;
516 }
517 case R.id.two: {
518 playTone(ToneGenerator.TONE_DTMF_2);
519 keyPressed(KeyEvent.KEYCODE_2);
520 return;
521 }
522 case R.id.three: {
523 playTone(ToneGenerator.TONE_DTMF_3);
524 keyPressed(KeyEvent.KEYCODE_3);
525 return;
526 }
527 case R.id.four: {
528 playTone(ToneGenerator.TONE_DTMF_4);
529 keyPressed(KeyEvent.KEYCODE_4);
530 return;
531 }
532 case R.id.five: {
533 playTone(ToneGenerator.TONE_DTMF_5);
534 keyPressed(KeyEvent.KEYCODE_5);
535 return;
536 }
537 case R.id.six: {
538 playTone(ToneGenerator.TONE_DTMF_6);
539 keyPressed(KeyEvent.KEYCODE_6);
540 return;
541 }
542 case R.id.seven: {
543 playTone(ToneGenerator.TONE_DTMF_7);
544 keyPressed(KeyEvent.KEYCODE_7);
545 return;
546 }
547 case R.id.eight: {
548 playTone(ToneGenerator.TONE_DTMF_8);
549 keyPressed(KeyEvent.KEYCODE_8);
550 return;
551 }
552 case R.id.nine: {
553 playTone(ToneGenerator.TONE_DTMF_9);
554 keyPressed(KeyEvent.KEYCODE_9);
555 return;
556 }
557 case R.id.zero: {
558 playTone(ToneGenerator.TONE_DTMF_0);
559 keyPressed(KeyEvent.KEYCODE_0);
560 return;
561 }
562 case R.id.pound: {
563 playTone(ToneGenerator.TONE_DTMF_P);
564 keyPressed(KeyEvent.KEYCODE_POUND);
565 return;
566 }
567 case R.id.star: {
568 playTone(ToneGenerator.TONE_DTMF_S);
569 keyPressed(KeyEvent.KEYCODE_STAR);
570 return;
571 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 }
573 }
574
575 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700576 * called for long touch events
577 */
578 @Override
579 public boolean onLongClick(View view) {
580 int id = view.getId();
581 switch (id) {
582 case R.id.deleteButton: {
583 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 return true;
585 }
586 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700587 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700588 keyPressed(KeyEvent.KEYCODE_PLUS);
589 return true;
590 }
591 }
592 return false;
593 }
594
595 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700596 protected void onStart() {
597 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800598 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
599 // And set background color to black.
600 if (mAreEmergencyDialerShortcutsEnabled) {
601 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
602 updateTheme(false);
603 } else {
604 mColorExtractor.addOnColorsChangedListener(this);
605 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
606 ColorExtractor.TYPE_EXTRA_DARK);
607 // Do not animate when view isn't visible yet, just set an initial state.
608 mBackgroundGradient.setColors(lockScreenColors, false);
609 updateTheme(lockScreenColors.supportsDarkText());
610 }
CY Cheng9a69c182018-06-15 21:20:10 +0800611
612 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
613 final Context context = this;
614 mEccInfoHelper.getCountryEccInfoAsync(context,
615 new EccInfoHelper.CountryEccInfoResultCallback() {
616 @Override
617 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
618 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
619 + Rlog.pii(LOG_TAG, iso));
620 updateLocationAndEccInfo(iso, countryEccInfo);
621 }
622
623 @Override
624 public void onDetectCountryFailed() {
625 Log.w(LOG_TAG, "Cannot detect current country.");
626 updateLocationAndEccInfo(null, null);
627 }
628
629 @Override
630 public void onRetrieveCountryEccInfoFailed(String iso) {
631 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
632 + Rlog.pii(LOG_TAG, iso));
633 updateLocationAndEccInfo(iso, null);
634 }
635 });
636 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700637 }
638
639 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700640 protected void onResume() {
641 super.onResume();
642
643 // retrieve the DTMF tone play back setting.
644 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
645 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
646
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700647 // if the mToneGenerator creation fails, just continue without it. It is
648 // a local audio signal, and is not as important as the dtmf tone itself.
649 synchronized (mToneGeneratorLock) {
650 if (mToneGenerator == null) {
651 try {
652 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
653 TONE_RELATIVE_VOLUME);
654 } catch (RuntimeException e) {
655 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
656 mToneGenerator = null;
657 }
658 }
659 }
660
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700661 updateDialAndDeleteButtonStateEnabledAttr();
662 }
663
664 @Override
665 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700666 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700667 }
668
Lucas Dupineb9c5702017-05-10 16:57:09 -0700669 @Override
670 protected void onStop() {
671 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700672 mColorExtractor.removeOnColorsChangedListener(this);
673 }
674
675 /**
676 * Sets theme based on gradient colors
677 * @param supportsDarkText true if gradient supports dark text
678 */
679 private void updateTheme(boolean supportsDarkText) {
680 if (mSupportsDarkText == supportsDarkText) {
681 return;
682 }
683 mSupportsDarkText = supportsDarkText;
684
685 // We can't change themes after inflation, in this case we'll have to recreate
686 // the whole activity.
687 if (mBackgroundGradient != null) {
688 recreate();
689 return;
690 }
691
692 int vis = getWindow().getDecorView().getSystemUiVisibility();
693 if (supportsDarkText) {
694 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
695 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
696 setTheme(R.style.EmergencyDialerThemeDark);
697 } else {
698 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
699 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
700 setTheme(R.style.EmergencyDialerTheme);
701 }
702 getWindow().getDecorView().setSystemUiVisibility(vis);
703 }
704
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700705 /**
706 * place the call, but check to make sure it is a viable number.
707 */
708 private void placeCall() {
709 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900710
711 // Convert into emergency number according to emergency conversion map.
712 // If conversion map is not defined (this is default), this method does
713 // nothing and just returns input number.
714 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
715
Yorke Lee36bb2542014-06-05 08:09:52 -0700716 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700717 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
718
719 // place the call if it is a valid number
720 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
721 // There is no number entered.
722 playTone(ToneGenerator.TONE_PROP_NACK);
723 return;
724 }
Shaotang Li5d906c12018-07-19 17:32:42 +0800725 Bundle extras = new Bundle();
726 extras.putInt(TelecomManager.EXTRA_CALL_SOURCE,
727 ParcelableCallAnalytics.CALL_SOURCE_EMERGENCY_DIALPAD);
Tyler Gunnfa77e202018-03-23 07:47:00 -0700728 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
Shaotang Li5d906c12018-07-19 17:32:42 +0800729 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), extras);
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 Gunncd6a1a92018-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 Gunnc5428972018-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 Chuangf264cfb2018-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 Chuang92cfe512018-06-07 16:25:27 +0800948 mEmergencyShortcutButtonList = new ArrayList<>();
949 setupEmergencyCallShortcutButton();
950
CY Cheng9a69c182018-06-15 21:20:10 +0800951 updateLocationAndEccInfo(null, null);
952
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800953 switchView(mEmergencyShortcutView, mDialpadView, false);
954 }
955
CY Cheng9a69c182018-06-15 21:20:10 +0800956 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800957 final View locationInfo = findViewById(R.id.location_info);
958
CY Cheng9a69c182018-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 Chuang92cfe512018-06-07 16:25:27 +0800966 locationInfo.setVisibility(View.INVISIBLE);
967 } else {
968 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800969 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800970 locationInfo.setVisibility(View.VISIBLE);
971 }
972 }
973
Chihhang Chuang92cfe512018-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 Cheng9a69c182018-06-15 21:20:10 +0800978 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800979
CY Cheng9a69c182018-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 Chuang92cfe512018-06-07 16:25:27 +0800999
CY Cheng9a69c182018-06-15 21:20:10 +08001000 @Override
1001 public void onInvalidated() {
1002 super.onInvalidated();
1003 updateLayout();
1004 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001005
CY Cheng9a69c182018-06-15 21:20:10 +08001006 private void updateLayout() {
1007 // clear previous added buttons
1008 shortcutButtonContainer.removeAllViews();
1009 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001010
CY Cheng9a69c182018-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 Chuang92cfe512018-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 Chuangf264cfb2018-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}