blob: a89b52dd4f4c81f5e9bcef96cd4d0681615124e7 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Tyler Gunnc5428972018-03-28 14:15:34 -070019import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN;
20
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080021import android.animation.Animator;
22import android.animation.AnimatorListenerAdapter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.Activity;
24import android.app.AlertDialog;
25import android.app.Dialog;
Lucas Dupineb9c5702017-05-10 16:57:09 -070026import android.app.WallpaperManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
CY Cheng9a69c182018-06-15 21:20:10 +080031import android.database.DataSetObserver;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080032import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070033import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070034import android.media.AudioManager;
35import android.media.ToneGenerator;
36import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070037import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070039import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070041import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070042import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070043import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080045import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070046import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070047import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070048import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070050import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080051import android.text.Spannable;
52import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.text.TextUtils;
54import android.text.TextWatcher;
55import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080056import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070058import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070059import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070061import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070062import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.view.View;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080064import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080066import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067
Lucas Dupinaf9e9912017-06-22 12:39:39 -070068import com.android.internal.colorextraction.ColorExtractor;
69import com.android.internal.colorextraction.ColorExtractor.GradientColors;
70import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070071import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070072import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070073import com.android.phone.common.widget.ResizingTextEditText;
CY Cheng9a69c182018-06-15 21:20:10 +080074import com.android.phone.ecc.CountryEccInfo;
75import com.android.phone.ecc.EccInfoHelper;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076
Chihhang Chuang92cfe512018-06-07 16:25:27 +080077import java.util.ArrayList;
78import java.util.List;
CY Cheng9a69c182018-06-15 21:20:10 +080079import java.util.Locale;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080080
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081/**
82 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
83 *
84 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
85 * activity from apps/Contacts) that:
86 * 1. Allows ONLY emergency calls to be dialed
87 * 2. Disallows voicemail functionality
88 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
89 * activity to stay in front of the keyguard.
90 *
91 * TODO: Even though this is an ultra-simplified version of the normal
92 * dialer, there's still lots of code duplication between this class and
93 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
94 * moved into a shared base class that would live in the framework?
95 * Or could we figure out some way to move *this* class into apps/Contacts
96 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080097 *
98 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +080099 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
100 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800101 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800102 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103 */
104public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700105 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800106 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
107 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108 // Keys used with onSaveInstanceState().
109 private static final String LAST_NUMBER = "lastNumber";
110
111 // Intent action for this activity.
112 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
113
114 // List of dialer button IDs.
115 private static final int[] DIALER_KEYS = new int[] {
116 R.id.one, R.id.two, R.id.three,
117 R.id.four, R.id.five, R.id.six,
118 R.id.seven, R.id.eight, R.id.nine,
119 R.id.star, R.id.zero, R.id.pound };
120
121 // Debug constants.
122 private static final boolean DBG = false;
123 private static final String LOG_TAG = "EmergencyDialer";
124
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700125 /** The length of DTMF tones in milliseconds */
126 private static final int TONE_LENGTH_MS = 150;
127
128 /** The DTMF tone volume relative to other sounds in the stream */
129 private static final int TONE_RELATIVE_VOLUME = 80;
130
131 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
132 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
133
134 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
135
Lucas Dupineb9c5702017-05-10 16:57:09 -0700136 /** 90% opacity, different from other gradients **/
137 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
138
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800139 /** 85% opacity for black background **/
140 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
141
CY Cheng9a69c182018-06-15 21:20:10 +0800142 /** Size limit of emergency shortcut buttons container. **/
143 private static final int SHORTCUT_SIZE_LIMIT = 3;
144
Tyler Gunnc5428972018-03-28 14:15:34 -0700145 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 private View mDialButton;
147 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800148 private View mEmergencyShortcutView;
149 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
CY Cheng9a69c182018-06-15 21:20:10 +0800151 private EccInfoHelper mEccInfoHelper;
152
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800153 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
CY Cheng9a69c182018-06-15 21:20:10 +0800154 private EccShortcutAdapter mShortcutAdapter;
155 private DataSetObserver mShortcutDataSetObserver = null;
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800156
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157 private ToneGenerator mToneGenerator;
158 private Object mToneGeneratorLock = new Object();
159
160 // determines if we want to playback local DTMF tones.
161 private boolean mDTMFToneEnabled;
162
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700163 private EmergencyActionGroup mEmergencyActionGroup;
164
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165 // close activity when screen turns off
166 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
167 @Override
168 public void onReceive(Context context, Intent intent) {
169 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700170 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171 }
172 }
173 };
174
175 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
176
Lucas Dupineb9c5702017-05-10 16:57:09 -0700177 // Background gradient
178 private ColorExtractor mColorExtractor;
179 private GradientDrawable mBackgroundGradient;
180 private boolean mSupportsDarkText;
181
Tyler Gunnc5428972018-03-28 14:15:34 -0700182 private boolean mIsWfcEmergencyCallingWarningEnabled;
183 private float mDefaultDigitsTextSize;
184
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800185 private boolean mAreEmergencyDialerShortcutsEnabled;
186
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700187 @Override
188 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
189 // Do nothing
190 }
191
192 @Override
193 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700194 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 }
196
197 @Override
198 public void afterTextChanged(Editable input) {
199 // Check for special sequences, in particular the "**04" or "**05"
200 // sequences that allow you to enter PIN or PUK-related codes.
201 //
202 // But note we *don't* allow most other special sequences here,
203 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
204 // since those shouldn't be available if the device is locked.
205 //
206 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
207 // here, not the regular handleChars() method.
208 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
209 // A special sequence was entered, clear the digits
210 mDigits.getText().clear();
211 }
212
213 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800214 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 }
216
217 @Override
218 protected void onCreate(Bundle icicle) {
219 super.onCreate(icicle);
220
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800222 setShowWhenLocked(true);
223 // Allow turning screen on
224 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800226 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
227 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
CY Cheng9a69c182018-06-15 21:20:10 +0800228 Log.d(LOG_TAG, "Enable emergency dialer shortcut: "
229 + mAreEmergencyDialerShortcutsEnabled);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800230
Lucas Dupineb9c5702017-05-10 16:57:09 -0700231 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800232
233 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
234 // And the background color is black with 85% opacity.
235 if (mAreEmergencyDialerShortcutsEnabled) {
236 updateTheme(false);
237 } else {
238 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
239 ColorExtractor.TYPE_EXTRA_DARK);
240 updateTheme(lockScreenColors.supportsDarkText());
241 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700242
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 setContentView(R.layout.emergency_dialer);
244
Tyler Gunnc5428972018-03-28 14:15:34 -0700245 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 mDigits.setKeyListener(DialerKeyListener.getInstance());
247 mDigits.setOnClickListener(this);
248 mDigits.setOnKeyListener(this);
249 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700250 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700251 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 maybeAddNumberFormatting();
253
Lucas Dupineb9c5702017-05-10 16:57:09 -0700254 mBackgroundGradient = new GradientDrawable(this);
255 Point displaySize = new Point();
256 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
257 .getDefaultDisplay().getSize(displaySize);
258 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800259 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
260 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700261 getWindow().setBackgroundDrawable(mBackgroundGradient);
262
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700263 // Check for the presence of the keypad
264 View view = findViewById(R.id.one);
265 if (view != null) {
266 setupKeypad();
267 }
268
269 mDelete = findViewById(R.id.deleteButton);
270 mDelete.setOnClickListener(this);
271 mDelete.setOnLongClickListener(this);
272
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700273 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274
275 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700276 // Read carrier config through the public API because PhoneGlobals is not available when we
277 // run as a secondary user.
278 CarrierConfigManager configMgr =
279 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
280 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800281 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700282
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700283 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 mDialButton.setOnClickListener(this);
285 } else {
286 mDialButton.setVisibility(View.GONE);
287 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700288 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
289 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
290 maybeShowWfcEmergencyCallingWarning();
291
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700292 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293
294 if (icicle != null) {
295 super.onRestoreInstanceState(icicle);
296 }
297
298 // Extract phone number from intent
299 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700300 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
302 if (number != null) {
303 mDigits.setText(number);
304 }
305 }
306
307 // if the mToneGenerator creation fails, just continue without it. It is
308 // a local audio signal, and is not as important as the dtmf tone itself.
309 synchronized (mToneGeneratorLock) {
310 if (mToneGenerator == null) {
311 try {
312 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
313 } catch (RuntimeException e) {
314 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
315 mToneGenerator = null;
316 }
317 }
318 }
319
320 final IntentFilter intentFilter = new IntentFilter();
321 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
322 registerReceiver(mBroadcastReceiver, intentFilter);
323
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700324 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800325
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800326 if (mAreEmergencyDialerShortcutsEnabled) {
327 setupEmergencyShortcutsView();
328 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 }
330
331 @Override
332 protected void onDestroy() {
333 super.onDestroy();
334 synchronized (mToneGeneratorLock) {
335 if (mToneGenerator != null) {
336 mToneGenerator.release();
337 mToneGenerator = null;
338 }
339 }
340 unregisterReceiver(mBroadcastReceiver);
CY Cheng9a69c182018-06-15 21:20:10 +0800341 if (mShortcutAdapter != null && mShortcutDataSetObserver != null) {
342 mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver);
343 mShortcutDataSetObserver = null;
344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700345 }
346
347 @Override
348 protected void onRestoreInstanceState(Bundle icicle) {
349 mLastNumber = icicle.getString(LAST_NUMBER);
350 }
351
352 @Override
353 protected void onSaveInstanceState(Bundle outState) {
354 super.onSaveInstanceState(outState);
355 outState.putString(LAST_NUMBER, mLastNumber);
356 }
357
358 /**
359 * Explicitly turn off number formatting, since it gets in the way of the emergency
360 * number detector
361 */
362 protected void maybeAddNumberFormatting() {
363 // Do nothing.
364 }
365
366 @Override
367 protected void onPostCreate(Bundle savedInstanceState) {
368 super.onPostCreate(savedInstanceState);
369
370 // This can't be done in onCreate(), since the auto-restoring of the digits
371 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
372 // is called. This method will be called every time the activity is created, and
373 // will always happen after onRestoreSavedInstanceState().
374 mDigits.addTextChangedListener(this);
375 }
376
377 private void setupKeypad() {
378 // Setup the listeners for the buttons
379 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700380 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
381 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700382 }
383
384 View view = findViewById(R.id.zero);
385 view.setOnLongClickListener(this);
386 }
387
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800388 @Override
389 public void onBackPressed() {
390 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800391 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800392 // Otherwise, it would finish the activity.
393 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
394 && mDialpadView.getVisibility() == View.VISIBLE) {
395 switchView(mEmergencyShortcutView, mDialpadView, true);
396 return;
397 }
398 super.onBackPressed();
399 }
400
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700401 /**
402 * handle key events
403 */
404 @Override
405 public boolean onKeyDown(int keyCode, KeyEvent event) {
406 switch (keyCode) {
407 // Happen when there's a "Call" hard button.
408 case KeyEvent.KEYCODE_CALL: {
409 if (TextUtils.isEmpty(mDigits.getText().toString())) {
410 // if we are adding a call from the InCallScreen and the phone
411 // number entered is empty, we just close the dialer to expose
412 // the InCallScreen under it.
413 finish();
414 } else {
415 // otherwise, we place the call.
416 placeCall();
417 }
418 return true;
419 }
420 }
421 return super.onKeyDown(keyCode, event);
422 }
423
424 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700425 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700426 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
427 mDigits.onKeyDown(keyCode, event);
428 }
429
430 @Override
431 public boolean onKey(View view, int keyCode, KeyEvent event) {
432 switch (view.getId()) {
433 case R.id.digits:
434 // Happen when "Done" button of the IME is pressed. This can happen when this
435 // Activity is forced into landscape mode due to a desk dock.
436 if (keyCode == KeyEvent.KEYCODE_ENTER
437 && event.getAction() == KeyEvent.ACTION_UP) {
438 placeCall();
439 return true;
440 }
441 break;
442 }
443 return false;
444 }
445
446 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700447 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800448 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700449 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800450 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700451 return handled;
452 }
453
454 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800455 public void onConfirmClick(EmergencyShortcutButton button) {
456 if (button == null) return;
457
458 String phoneNumber = button.getPhoneNumber();
459
460 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800461 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800462 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
463 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
464 } else {
465 Log.d(LOG_TAG, "emergency number is empty");
466 }
467 }
468
469 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700470 public void onClick(View view) {
471 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700472 case R.id.deleteButton: {
473 keyPressed(KeyEvent.KEYCODE_DEL);
474 return;
475 }
476 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700477 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700478 placeCall();
479 return;
480 }
481 case R.id.digits: {
482 if (mDigits.length() != 0) {
483 mDigits.setCursorVisible(true);
484 }
485 return;
486 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800487 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800488 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800489 switchView(mDialpadView, mEmergencyShortcutView, true);
490 return;
491 }
492 case R.id.emergency_info_button: {
493 Intent intent = (Intent) view.getTag(R.id.tag_intent);
494 if (intent != null) {
495 startActivity(intent);
496 }
497 return;
498 }
Yorke Lee23a70732014-08-14 17:12:01 -0700499 }
500 }
501
502 @Override
503 public void onPressed(View view, boolean pressed) {
504 if (!pressed) {
505 return;
506 }
507 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700508 case R.id.one: {
509 playTone(ToneGenerator.TONE_DTMF_1);
510 keyPressed(KeyEvent.KEYCODE_1);
511 return;
512 }
513 case R.id.two: {
514 playTone(ToneGenerator.TONE_DTMF_2);
515 keyPressed(KeyEvent.KEYCODE_2);
516 return;
517 }
518 case R.id.three: {
519 playTone(ToneGenerator.TONE_DTMF_3);
520 keyPressed(KeyEvent.KEYCODE_3);
521 return;
522 }
523 case R.id.four: {
524 playTone(ToneGenerator.TONE_DTMF_4);
525 keyPressed(KeyEvent.KEYCODE_4);
526 return;
527 }
528 case R.id.five: {
529 playTone(ToneGenerator.TONE_DTMF_5);
530 keyPressed(KeyEvent.KEYCODE_5);
531 return;
532 }
533 case R.id.six: {
534 playTone(ToneGenerator.TONE_DTMF_6);
535 keyPressed(KeyEvent.KEYCODE_6);
536 return;
537 }
538 case R.id.seven: {
539 playTone(ToneGenerator.TONE_DTMF_7);
540 keyPressed(KeyEvent.KEYCODE_7);
541 return;
542 }
543 case R.id.eight: {
544 playTone(ToneGenerator.TONE_DTMF_8);
545 keyPressed(KeyEvent.KEYCODE_8);
546 return;
547 }
548 case R.id.nine: {
549 playTone(ToneGenerator.TONE_DTMF_9);
550 keyPressed(KeyEvent.KEYCODE_9);
551 return;
552 }
553 case R.id.zero: {
554 playTone(ToneGenerator.TONE_DTMF_0);
555 keyPressed(KeyEvent.KEYCODE_0);
556 return;
557 }
558 case R.id.pound: {
559 playTone(ToneGenerator.TONE_DTMF_P);
560 keyPressed(KeyEvent.KEYCODE_POUND);
561 return;
562 }
563 case R.id.star: {
564 playTone(ToneGenerator.TONE_DTMF_S);
565 keyPressed(KeyEvent.KEYCODE_STAR);
566 return;
567 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700568 }
569 }
570
571 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700572 * called for long touch events
573 */
574 @Override
575 public boolean onLongClick(View view) {
576 int id = view.getId();
577 switch (id) {
578 case R.id.deleteButton: {
579 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700580 return true;
581 }
582 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700583 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700584 keyPressed(KeyEvent.KEYCODE_PLUS);
585 return true;
586 }
587 }
588 return false;
589 }
590
591 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700592 protected void onStart() {
593 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800594 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
595 // And set background color to black.
596 if (mAreEmergencyDialerShortcutsEnabled) {
597 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
598 updateTheme(false);
599 } else {
600 mColorExtractor.addOnColorsChangedListener(this);
601 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
602 ColorExtractor.TYPE_EXTRA_DARK);
603 // Do not animate when view isn't visible yet, just set an initial state.
604 mBackgroundGradient.setColors(lockScreenColors, false);
605 updateTheme(lockScreenColors.supportsDarkText());
606 }
CY Cheng9a69c182018-06-15 21:20:10 +0800607
608 if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) {
609 final Context context = this;
610 mEccInfoHelper.getCountryEccInfoAsync(context,
611 new EccInfoHelper.CountryEccInfoResultCallback() {
612 @Override
613 public void onSuccess(String iso, CountryEccInfo countryEccInfo) {
614 Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: "
615 + Rlog.pii(LOG_TAG, iso));
616 updateLocationAndEccInfo(iso, countryEccInfo);
617 }
618
619 @Override
620 public void onDetectCountryFailed() {
621 Log.w(LOG_TAG, "Cannot detect current country.");
622 updateLocationAndEccInfo(null, null);
623 }
624
625 @Override
626 public void onRetrieveCountryEccInfoFailed(String iso) {
627 Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: "
628 + Rlog.pii(LOG_TAG, iso));
629 updateLocationAndEccInfo(iso, null);
630 }
631 });
632 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700633 }
634
635 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700636 protected void onResume() {
637 super.onResume();
638
639 // retrieve the DTMF tone play back setting.
640 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
641 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
642
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700643 // if the mToneGenerator creation fails, just continue without it. It is
644 // a local audio signal, and is not as important as the dtmf tone itself.
645 synchronized (mToneGeneratorLock) {
646 if (mToneGenerator == null) {
647 try {
648 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
649 TONE_RELATIVE_VOLUME);
650 } catch (RuntimeException e) {
651 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
652 mToneGenerator = null;
653 }
654 }
655 }
656
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700657 updateDialAndDeleteButtonStateEnabledAttr();
658 }
659
660 @Override
661 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700662 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700663 }
664
Lucas Dupineb9c5702017-05-10 16:57:09 -0700665 @Override
666 protected void onStop() {
667 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700668 mColorExtractor.removeOnColorsChangedListener(this);
669 }
670
671 /**
672 * Sets theme based on gradient colors
673 * @param supportsDarkText true if gradient supports dark text
674 */
675 private void updateTheme(boolean supportsDarkText) {
676 if (mSupportsDarkText == supportsDarkText) {
677 return;
678 }
679 mSupportsDarkText = supportsDarkText;
680
681 // We can't change themes after inflation, in this case we'll have to recreate
682 // the whole activity.
683 if (mBackgroundGradient != null) {
684 recreate();
685 return;
686 }
687
688 int vis = getWindow().getDecorView().getSystemUiVisibility();
689 if (supportsDarkText) {
690 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
691 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
692 setTheme(R.style.EmergencyDialerThemeDark);
693 } else {
694 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
695 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
696 setTheme(R.style.EmergencyDialerTheme);
697 }
698 getWindow().getDecorView().setSystemUiVisibility(vis);
699 }
700
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700701 /**
702 * place the call, but check to make sure it is a viable number.
703 */
704 private void placeCall() {
705 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900706
707 // Convert into emergency number according to emergency conversion map.
708 // If conversion map is not defined (this is default), this method does
709 // nothing and just returns input number.
710 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
711
Yorke Lee36bb2542014-06-05 08:09:52 -0700712 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700713 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
714
715 // place the call if it is a valid number
716 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
717 // There is no number entered.
718 playTone(ToneGenerator.TONE_PROP_NACK);
719 return;
720 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700721 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
722 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700723 } else {
724 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
725
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700726 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
727 }
Yorke Lee9b341512014-10-17 11:36:41 -0700728 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700729 }
730
731 /**
732 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
733 *
734 * The tone is played locally, using the audio stream for phone calls.
735 * Tones are played only if the "Audible touch tones" user preference
736 * is checked, and are NOT played if the device is in silent mode.
737 *
738 * @param tone a tone code from {@link ToneGenerator}
739 */
740 void playTone(int tone) {
741 // if local tone playback is disabled, just return.
742 if (!mDTMFToneEnabled) {
743 return;
744 }
745
746 // Also do nothing if the phone is in silent mode.
747 // We need to re-check the ringer mode for *every* playTone()
748 // call, rather than keeping a local flag that's updated in
749 // onResume(), since it's possible to toggle silent mode without
750 // leaving the current activity (via the ENDCALL-longpress menu.)
751 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
752 int ringerMode = audioManager.getRingerMode();
753 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
754 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
755 return;
756 }
757
758 synchronized (mToneGeneratorLock) {
759 if (mToneGenerator == null) {
760 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
761 return;
762 }
763
764 // Start the new tone (will stop any playing tone)
765 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
766 }
767 }
768
769 private CharSequence createErrorMessage(String number) {
770 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800771 String errorString = getString(R.string.dial_emergency_error, number);
772 int startingPosition = errorString.indexOf(number);
773 int endingPosition = startingPosition + number.length();
774 Spannable result = new SpannableString(errorString);
775 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
776 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 } else {
778 return getText(R.string.dial_emergency_empty_error).toString();
779 }
780 }
781
782 @Override
783 protected Dialog onCreateDialog(int id) {
784 AlertDialog dialog = null;
785 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
786 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700787 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
789 .setMessage(createErrorMessage(mLastNumber))
790 .setPositiveButton(R.string.ok, null)
791 .setCancelable(true).create();
792
793 // blur stuff behind the dialog
794 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700795 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 }
797 return dialog;
798 }
799
800 @Override
801 protected void onPrepareDialog(int id, Dialog dialog) {
802 super.onPrepareDialog(id, dialog);
803 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
804 AlertDialog alert = (AlertDialog) dialog;
805 alert.setMessage(createErrorMessage(mLastNumber));
806 }
807 }
808
Andrew Leed5631e82014-10-08 16:03:58 -0700809 @Override
810 public boolean onOptionsItemSelected(MenuItem item) {
811 final int itemId = item.getItemId();
812 if (itemId == android.R.id.home) {
813 onBackPressed();
814 return true;
815 }
816 return super.onOptionsItemSelected(item);
817 }
818
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700819 /**
820 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
821 */
822 private void updateDialAndDeleteButtonStateEnabledAttr() {
823 final boolean notEmpty = mDigits.length() != 0;
824
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700825 mDelete.setEnabled(notEmpty);
826 }
Yorke Lee91311662014-10-24 14:50:45 -0700827
828 /**
829 * Remove the digit just before the current position. Used by various long pressed callbacks
830 * to remove the digit that was populated as a result of the short click.
831 */
832 private void removePreviousDigitIfPossible() {
833 final int currentPosition = mDigits.getSelectionStart();
834 if (currentPosition > 0) {
835 mDigits.setSelection(currentPosition);
836 mDigits.getText().delete(currentPosition - 1, currentPosition);
837 }
838 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800839
840 /**
841 * Update the text-to-speech annotations in the edit field.
842 */
843 private void updateTtsSpans() {
844 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
845 mDigits.getText().removeSpan(o);
846 }
847 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
848 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700849
850 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700851 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700852 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700853 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
854 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700855 mBackgroundGradient.setColors(colors);
856 updateTheme(colors.supportsDarkText());
857 }
858 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700859
860 /**
861 * Where a carrier requires a warning that emergency calling is not available while on WFC,
862 * add hint text above the dial pad which warns the user of this case.
863 */
864 private void maybeShowWfcEmergencyCallingWarning() {
865 if (!mIsWfcEmergencyCallingWarningEnabled) {
866 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
867 return;
868 }
869
870 // Use an async task rather than calling into Telephony on UI thread.
871 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
872 @Override
873 protected Boolean doInBackground(Void... voids) {
874 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
875 boolean isWfcAvailable = tm.isWifiCallingAvailable();
876 ServiceState ss = tm.getServiceState();
877 boolean isCellAvailable =
878 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
879 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
880 + " isCellAvailable=" + isCellAvailable
881 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
882 return isWfcAvailable && !isCellAvailable;
883 }
884
885 @Override
886 protected void onPostExecute(Boolean result) {
887 if (result.booleanValue()) {
888 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
889 mDigits.setHint(R.string.dial_emergency_calling_not_available);
890 } else {
891 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
892 mDigits.setHint("");
893 }
894 maybeChangeHintSize();
895 }
896 };
897 showWfcWarningTask.execute((Void) null);
898 }
899
900 /**
901 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
902 * edit view and set the font size to a smaller size appropriate for the emergency calling
903 * warning.
904 */
905 private void maybeChangeHintSize() {
906 if (TextUtils.isEmpty(mDigits.getHint())
907 || !TextUtils.isEmpty(mDigits.getText().toString())) {
908 // No hint or there are dialed digits, so use default size.
909 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
910 // By default, the digits view auto-resizes to fit the text it contains, so
911 // enable that now.
912 mDigits.setResizeEnabled(true);
913 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
914 } else {
915 // Hint present and no dialed digits, set custom font size appropriate for the warning.
916 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
917 R.dimen.emergency_call_warning_size));
918 // Since we're populating this with a static text string, disable auto-resize.
919 mDigits.setResizeEnabled(false);
920 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
921 }
922 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800923
924 private void setupEmergencyShortcutsView() {
925 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
926 mDialpadView = findViewById(R.id.emergency_dialer);
927
928 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
929 dialpadButton.setOnClickListener(this);
930
931 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
932 emergencyInfoButton.setOnClickListener(this);
933
934 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
935 mEmergencyActionGroup.setVisibility(View.GONE);
936
937 // Setup dialpad title.
938 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
939 emergencyDialpadTitle.setVisibility(View.VISIBLE);
940
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800941 mEmergencyShortcutButtonList = new ArrayList<>();
942 setupEmergencyCallShortcutButton();
943
CY Cheng9a69c182018-06-15 21:20:10 +0800944 updateLocationAndEccInfo(null, null);
945
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800946 switchView(mEmergencyShortcutView, mDialpadView, false);
947 }
948
CY Cheng9a69c182018-06-15 21:20:10 +0800949 private void setLocationInfo(String countryIso) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800950 final View locationInfo = findViewById(R.id.location_info);
951
CY Cheng9a69c182018-06-15 21:20:10 +0800952 String countryName = null;
953 if (!TextUtils.isEmpty(countryIso)) {
954 Locale locale = Locale.getDefault();
955 countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant())
956 .getDisplayCountry();
957 }
958 if (TextUtils.isEmpty(countryName)) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800959 locationInfo.setVisibility(View.INVISIBLE);
960 } else {
961 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
CY Cheng9a69c182018-06-15 21:20:10 +0800962 location.setText(countryName);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800963 locationInfo.setVisibility(View.VISIBLE);
964 }
965 }
966
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800967 private void setupEmergencyCallShortcutButton() {
968 final ViewGroup shortcutButtonContainer = findViewById(
969 R.id.emergency_shortcut_buttons_container);
970 shortcutButtonContainer.setClipToOutline(true);
CY Cheng9a69c182018-06-15 21:20:10 +0800971 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800972
CY Cheng9a69c182018-06-15 21:20:10 +0800973 mShortcutAdapter = new EccShortcutAdapter(this) {
974 @Override
975 public View inflateView(View convertView, ViewGroup parent, CharSequence number,
976 CharSequence description, int iconRes) {
977 EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater()
978 .inflate(R.layout.emergency_shortcut_button, parent, false);
979 button.setPhoneNumber(number);
980 button.setPhoneDescription(description);
981 button.setPhoneTypeIcon(iconRes);
982 button.setOnConfirmClickListener(EmergencyDialer.this);
983 return button;
984 }
985 };
986 mShortcutDataSetObserver = new DataSetObserver() {
987 @Override
988 public void onChanged() {
989 super.onChanged();
990 updateLayout();
991 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800992
CY Cheng9a69c182018-06-15 21:20:10 +0800993 @Override
994 public void onInvalidated() {
995 super.onInvalidated();
996 updateLayout();
997 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800998
CY Cheng9a69c182018-06-15 21:20:10 +0800999 private void updateLayout() {
1000 // clear previous added buttons
1001 shortcutButtonContainer.removeAllViews();
1002 mEmergencyShortcutButtonList.clear();
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001003
CY Cheng9a69c182018-06-15 21:20:10 +08001004 for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) {
1005 EmergencyShortcutButton button = (EmergencyShortcutButton)
1006 mShortcutAdapter.getView(i, null, shortcutButtonContainer);
1007 mEmergencyShortcutButtonList.add(button);
1008 shortcutButtonContainer.addView(button);
1009 }
1010
1011 // update emergency numbers title for numerous buttons.
1012 if (mEmergencyShortcutButtonList.size() > 1) {
1013 emergencyNumberTitle.setText(getString(
1014 R.string.numerous_emergency_numbers_title));
1015 } else {
1016 emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title));
1017 }
1018 }
1019 };
1020 mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver);
1021 }
1022
1023 private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) {
1024 if (!isFinishing() && !isDestroyed()) {
1025 setLocationInfo(iso);
1026 if (mShortcutAdapter != null) {
1027 mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo);
1028 }
Chihhang Chuang92cfe512018-06-07 16:25:27 +08001029 }
1030 }
1031
1032 /**
1033 * Called by the activity before a touch event is dispatched to the view hierarchy.
1034 */
1035 private void onPreTouchEvent(MotionEvent event) {
1036 mEmergencyActionGroup.onPreTouchEvent(event);
1037
1038 if (mEmergencyShortcutButtonList != null) {
1039 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1040 button.onPreTouchEvent(event);
1041 }
1042 }
1043 }
1044
1045 /**
1046 * Called by the activity after a touch event is dispatched to the view hierarchy.
1047 */
1048 private void onPostTouchEvent(MotionEvent event) {
1049 mEmergencyActionGroup.onPostTouchEvent(event);
1050
1051 if (mEmergencyShortcutButtonList != null) {
1052 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
1053 button.onPostTouchEvent(event);
1054 }
1055 }
1056 }
1057
Chihhang Chuangf264cfb2018-06-05 15:29:06 +08001058 /**
1059 * Switch two view.
1060 *
1061 * @param displayView the view would be displayed.
1062 * @param hideView the view would be hidden.
1063 * @param hasAnimation is {@code true} when the view should be displayed with animation.
1064 */
1065 private void switchView(View displayView, View hideView, boolean hasAnimation) {
1066 if (displayView == null || hideView == null) {
1067 return;
1068 }
1069
1070 if (displayView.getVisibility() == View.VISIBLE) {
1071 return;
1072 }
1073
1074 if (hasAnimation) {
1075 crossfade(hideView, displayView);
1076 } else {
1077 hideView.setVisibility(View.GONE);
1078 displayView.setVisibility(View.VISIBLE);
1079 }
1080 }
1081
1082 /**
1083 * Fade out and fade in animation between two view transition.
1084 */
1085 private void crossfade(View fadeOutView, View fadeInView) {
1086 if (fadeOutView == null || fadeInView == null) {
1087 return;
1088 }
1089 final int shortAnimationDuration = getResources().getInteger(
1090 android.R.integer.config_shortAnimTime);
1091
1092 fadeInView.setAlpha(0f);
1093 fadeInView.setVisibility(View.VISIBLE);
1094
1095 fadeInView.animate()
1096 .alpha(1f)
1097 .setDuration(shortAnimationDuration)
1098 .setListener(null);
1099
1100 fadeOutView.animate()
1101 .alpha(0f)
1102 .setDuration(shortAnimationDuration)
1103 .setListener(new AnimatorListenerAdapter() {
1104 @Override
1105 public void onAnimationEnd(Animator animation) {
1106 fadeOutView.setVisibility(View.GONE);
1107 }
1108 });
1109 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110}