blob: fc4ef6a9a33ba59f93ad3256cdc8621b7870fde0 [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;
Chihhang Chuangf8d33002018-07-02 11:08:50 +080031import android.graphics.Color;
Lucas Dupineb9c5702017-05-10 16:57:09 -070032import android.graphics.Point;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070033import android.media.AudioManager;
34import android.media.ToneGenerator;
35import android.net.Uri;
Tyler Gunnc5428972018-03-28 14:15:34 -070036import android.os.AsyncTask;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070037import android.os.Bundle;
Jonathan Basseric31f1f32015-05-12 10:13:03 -070038import android.os.PersistableBundle;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070039import android.provider.Settings;
Tyler Gunn4d45d1c2014-09-12 22:17:53 -070040import android.telecom.PhoneAccount;
Tyler Gunnfa77e202018-03-23 07:47:00 -070041import android.telecom.TelecomManager;
Jonathan Basseri3649bdb2015-04-30 22:39:11 -070042import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.PhoneNumberUtils;
Billy Chi17ec2282018-06-15 19:00:15 +080044import android.telephony.Rlog;
Tyler Gunnc5428972018-03-28 14:15:34 -070045import android.telephony.ServiceState;
Santos Cordone137eed2015-06-23 15:34:47 -070046import android.telephony.SubscriptionManager;
Tyler Gunnc5428972018-03-28 14:15:34 -070047import android.telephony.TelephonyManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.text.Editable;
Hall Liubdc9c882016-05-25 15:25:28 -070049import android.text.InputType;
Hall Liudc274312016-03-01 16:34:45 -080050import android.text.Spannable;
51import android.text.SpannableString;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
53import android.text.TextWatcher;
54import android.text.method.DialerKeyListener;
Ihab Awadf7c1a5a2014-12-08 19:24:23 -080055import android.text.style.TtsSpan;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070056import android.util.Log;
Tyler Gunnc5428972018-03-28 14:15:34 -070057import android.util.TypedValue;
Yorke Lee116dd072015-08-31 11:38:39 -070058import android.view.HapticFeedbackConstants;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.view.KeyEvent;
Andrew Leed5631e82014-10-08 16:03:58 -070060import android.view.MenuItem;
Adrian Roos1c4b47f2015-04-13 14:53:32 -070061import android.view.MotionEvent;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import android.view.View;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080063import android.view.ViewGroup;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.view.WindowManager;
Chihhang Chuang92cfe512018-06-07 16:25:27 +080065import android.widget.TextView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066
Lucas Dupinaf9e9912017-06-22 12:39:39 -070067import com.android.internal.colorextraction.ColorExtractor;
68import com.android.internal.colorextraction.ColorExtractor.GradientColors;
69import com.android.internal.colorextraction.drawable.GradientDrawable;
Yorke Lee23a70732014-08-14 17:12:01 -070070import com.android.phone.common.dialpad.DialpadKeyButton;
Sai Cheemalapati14462b62014-06-18 13:53:56 -070071import com.android.phone.common.util.ViewUtil;
Tyler Gunnc5428972018-03-28 14:15:34 -070072import com.android.phone.common.widget.ResizingTextEditText;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073
Chihhang Chuang92cfe512018-06-07 16:25:27 +080074import java.util.ArrayList;
75import java.util.List;
76
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077/**
78 * EmergencyDialer is a special dialer that is used ONLY for dialing emergency calls.
79 *
80 * It's a simplified version of the regular dialer (i.e. the TwelveKeyDialer
81 * activity from apps/Contacts) that:
82 * 1. Allows ONLY emergency calls to be dialed
83 * 2. Disallows voicemail functionality
84 * 3. Uses the FLAG_SHOW_WHEN_LOCKED window manager flag to allow this
85 * activity to stay in front of the keyguard.
86 *
87 * TODO: Even though this is an ultra-simplified version of the normal
88 * dialer, there's still lots of code duplication between this class and
89 * the TwelveKeyDialer class from apps/Contacts. Could the common code be
90 * moved into a shared base class that would live in the framework?
91 * Or could we figure out some way to move *this* class into apps/Contacts
92 * also?
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080093 *
94 * TODO: Implement emergency dialer shortcut.
Chihhang Chuang92cfe512018-06-07 16:25:27 +080095 * Emergency dialer shortcut offer a local emergency number list. Directly clicking a call button
96 * to place an emergency phone call without entering numbers from dialpad.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +080097 * TODO item:
Chihhang Chuang92cfe512018-06-07 16:25:27 +080098 * 1.integrate emergency phone number table.
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099 */
100public class EmergencyDialer extends Activity implements View.OnClickListener,
Yorke Lee23a70732014-08-14 17:12:01 -0700101 View.OnLongClickListener, View.OnKeyListener, TextWatcher,
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800102 DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener,
103 EmergencyShortcutButton.OnConfirmClickListener {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104 // Keys used with onSaveInstanceState().
105 private static final String LAST_NUMBER = "lastNumber";
106
107 // Intent action for this activity.
108 public static final String ACTION_DIAL = "com.android.phone.EmergencyDialer.DIAL";
109
110 // List of dialer button IDs.
111 private static final int[] DIALER_KEYS = new int[] {
112 R.id.one, R.id.two, R.id.three,
113 R.id.four, R.id.five, R.id.six,
114 R.id.seven, R.id.eight, R.id.nine,
115 R.id.star, R.id.zero, R.id.pound };
116
117 // Debug constants.
118 private static final boolean DBG = false;
119 private static final String LOG_TAG = "EmergencyDialer";
120
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121 /** The length of DTMF tones in milliseconds */
122 private static final int TONE_LENGTH_MS = 150;
123
124 /** The DTMF tone volume relative to other sounds in the stream */
125 private static final int TONE_RELATIVE_VOLUME = 80;
126
127 /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
128 private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
129
130 private static final int BAD_EMERGENCY_NUMBER_DIALOG = 0;
131
Lucas Dupineb9c5702017-05-10 16:57:09 -0700132 /** 90% opacity, different from other gradients **/
133 private static final int BACKGROUND_GRADIENT_ALPHA = 230;
134
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800135 /** 85% opacity for black background **/
136 private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217;
137
Tyler Gunnc5428972018-03-28 14:15:34 -0700138 ResizingTextEditText mDigits;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700139 private View mDialButton;
140 private View mDelete;
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800141 private View mEmergencyShortcutView;
142 private View mDialpadView;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700143
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800144 private List<EmergencyShortcutButton> mEmergencyShortcutButtonList;
145
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146 private ToneGenerator mToneGenerator;
147 private Object mToneGeneratorLock = new Object();
148
149 // determines if we want to playback local DTMF tones.
150 private boolean mDTMFToneEnabled;
151
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700152 private EmergencyActionGroup mEmergencyActionGroup;
153
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700154 // close activity when screen turns off
155 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
156 @Override
157 public void onReceive(Context context, Intent intent) {
158 if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
Adrian Roos061c7232015-04-21 12:37:07 -0700159 finishAndRemoveTask();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700160 }
161 }
162 };
163
164 private String mLastNumber; // last number we tried to dial. Used to restore error dialog.
165
Lucas Dupineb9c5702017-05-10 16:57:09 -0700166 // Background gradient
167 private ColorExtractor mColorExtractor;
168 private GradientDrawable mBackgroundGradient;
169 private boolean mSupportsDarkText;
170
Tyler Gunnc5428972018-03-28 14:15:34 -0700171 private boolean mIsWfcEmergencyCallingWarningEnabled;
172 private float mDefaultDigitsTextSize;
173
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800174 private boolean mAreEmergencyDialerShortcutsEnabled;
175
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700176 @Override
177 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
178 // Do nothing
179 }
180
181 @Override
182 public void onTextChanged(CharSequence input, int start, int before, int changeCount) {
Tyler Gunnc5428972018-03-28 14:15:34 -0700183 maybeChangeHintSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184 }
185
186 @Override
187 public void afterTextChanged(Editable input) {
188 // Check for special sequences, in particular the "**04" or "**05"
189 // sequences that allow you to enter PIN or PUK-related codes.
190 //
191 // But note we *don't* allow most other special sequences here,
192 // like "secret codes" (*#*#<code>#*#*) or IMEI display ("*#06#"),
193 // since those shouldn't be available if the device is locked.
194 //
195 // So we call SpecialCharSequenceMgr.handleCharsForLockedDevice()
196 // here, not the regular handleChars() method.
197 if (SpecialCharSequenceMgr.handleCharsForLockedDevice(this, input.toString(), this)) {
198 // A special sequence was entered, clear the digits
199 mDigits.getText().clear();
200 }
201
202 updateDialAndDeleteButtonStateEnabledAttr();
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800203 updateTtsSpans();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700204 }
205
206 @Override
207 protected void onCreate(Bundle icicle) {
208 super.onCreate(icicle);
209
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700210 // Allow this activity to be displayed in front of the keyguard / lockscreen.
Mengjun Lengb9d14f02017-10-31 14:28:14 +0800211 setShowWhenLocked(true);
212 // Allow turning screen on
213 setTurnScreenOn(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700214
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800215 mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(),
216 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
217
Lucas Dupineb9c5702017-05-10 16:57:09 -0700218 mColorExtractor = new ColorExtractor(this);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800219
220 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
221 // And the background color is black with 85% opacity.
222 if (mAreEmergencyDialerShortcutsEnabled) {
223 updateTheme(false);
224 } else {
225 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
226 ColorExtractor.TYPE_EXTRA_DARK);
227 updateTheme(lockScreenColors.supportsDarkText());
228 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700229
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 setContentView(R.layout.emergency_dialer);
231
Tyler Gunnc5428972018-03-28 14:15:34 -0700232 mDigits = (ResizingTextEditText) findViewById(R.id.digits);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233 mDigits.setKeyListener(DialerKeyListener.getInstance());
234 mDigits.setOnClickListener(this);
235 mDigits.setOnKeyListener(this);
236 mDigits.setLongClickable(false);
Hall Liubdc9c882016-05-25 15:25:28 -0700237 mDigits.setInputType(InputType.TYPE_NULL);
Tyler Gunnc5428972018-03-28 14:15:34 -0700238 mDefaultDigitsTextSize = mDigits.getScaledTextSize();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 maybeAddNumberFormatting();
240
Lucas Dupineb9c5702017-05-10 16:57:09 -0700241 mBackgroundGradient = new GradientDrawable(this);
242 Point displaySize = new Point();
243 ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
244 .getDefaultDisplay().getSize(displaySize);
245 mBackgroundGradient.setScreenSize(displaySize.x, displaySize.y);
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800246 mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled
247 ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700248 getWindow().setBackgroundDrawable(mBackgroundGradient);
249
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 // Check for the presence of the keypad
251 View view = findViewById(R.id.one);
252 if (view != null) {
253 setupKeypad();
254 }
255
256 mDelete = findViewById(R.id.deleteButton);
257 mDelete.setOnClickListener(this);
258 mDelete.setOnLongClickListener(this);
259
Sai Cheemalapati14462b62014-06-18 13:53:56 -0700260 mDialButton = findViewById(R.id.floating_action_button);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
262 // Check whether we should show the onscreen "Dial" button and co.
Jonathan Basserie619a4c2015-06-26 14:52:26 -0700263 // Read carrier config through the public API because PhoneGlobals is not available when we
264 // run as a secondary user.
265 CarrierConfigManager configMgr =
266 (CarrierConfigManager) getSystemService(Context.CARRIER_CONFIG_SERVICE);
267 PersistableBundle carrierConfig =
Shishir Agrawald3480e02016-01-25 13:05:49 -0800268 configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId());
Tyler Gunnc5428972018-03-28 14:15:34 -0700269
Jonathan Basseri9504c6b2015-06-04 14:23:32 -0700270 if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271 mDialButton.setOnClickListener(this);
272 } else {
273 mDialButton.setVisibility(View.GONE);
274 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700275 mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt(
276 CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1;
277 maybeShowWfcEmergencyCallingWarning();
278
Adrian Roosc9fdb6c2015-05-25 15:10:21 -0700279 ViewUtil.setupFloatingActionButton(mDialButton, getResources());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280
281 if (icicle != null) {
282 super.onRestoreInstanceState(icicle);
283 }
284
285 // Extract phone number from intent
286 Uri data = getIntent().getData();
Jay Shrauner137458b2014-09-05 14:27:25 -0700287 if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 String number = PhoneNumberUtils.getNumberFromIntent(getIntent(), this);
289 if (number != null) {
290 mDigits.setText(number);
291 }
292 }
293
294 // if the mToneGenerator creation fails, just continue without it. It is
295 // a local audio signal, and is not as important as the dtmf tone itself.
296 synchronized (mToneGeneratorLock) {
297 if (mToneGenerator == null) {
298 try {
299 mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
300 } catch (RuntimeException e) {
301 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
302 mToneGenerator = null;
303 }
304 }
305 }
306
307 final IntentFilter intentFilter = new IntentFilter();
308 intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
309 registerReceiver(mBroadcastReceiver, intentFilter);
310
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700311 mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group);
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800312
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800313 if (mAreEmergencyDialerShortcutsEnabled) {
314 setupEmergencyShortcutsView();
315 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 }
317
318 @Override
319 protected void onDestroy() {
320 super.onDestroy();
321 synchronized (mToneGeneratorLock) {
322 if (mToneGenerator != null) {
323 mToneGenerator.release();
324 mToneGenerator = null;
325 }
326 }
327 unregisterReceiver(mBroadcastReceiver);
328 }
329
330 @Override
331 protected void onRestoreInstanceState(Bundle icicle) {
332 mLastNumber = icicle.getString(LAST_NUMBER);
333 }
334
335 @Override
336 protected void onSaveInstanceState(Bundle outState) {
337 super.onSaveInstanceState(outState);
338 outState.putString(LAST_NUMBER, mLastNumber);
339 }
340
341 /**
342 * Explicitly turn off number formatting, since it gets in the way of the emergency
343 * number detector
344 */
345 protected void maybeAddNumberFormatting() {
346 // Do nothing.
347 }
348
349 @Override
350 protected void onPostCreate(Bundle savedInstanceState) {
351 super.onPostCreate(savedInstanceState);
352
353 // This can't be done in onCreate(), since the auto-restoring of the digits
354 // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState()
355 // is called. This method will be called every time the activity is created, and
356 // will always happen after onRestoreSavedInstanceState().
357 mDigits.addTextChangedListener(this);
358 }
359
360 private void setupKeypad() {
361 // Setup the listeners for the buttons
362 for (int id : DIALER_KEYS) {
Yorke Lee23a70732014-08-14 17:12:01 -0700363 final DialpadKeyButton key = (DialpadKeyButton) findViewById(id);
364 key.setOnPressedListener(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700365 }
366
367 View view = findViewById(R.id.zero);
368 view.setOnLongClickListener(this);
369 }
370
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800371 @Override
372 public void onBackPressed() {
373 // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the
Billy Chi17ec2282018-06-15 19:00:15 +0800374 // back key will back to display EmergencyShortcutView view.
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800375 // Otherwise, it would finish the activity.
376 if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null
377 && mDialpadView.getVisibility() == View.VISIBLE) {
378 switchView(mEmergencyShortcutView, mDialpadView, true);
379 return;
380 }
381 super.onBackPressed();
382 }
383
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384 /**
385 * handle key events
386 */
387 @Override
388 public boolean onKeyDown(int keyCode, KeyEvent event) {
389 switch (keyCode) {
390 // Happen when there's a "Call" hard button.
391 case KeyEvent.KEYCODE_CALL: {
392 if (TextUtils.isEmpty(mDigits.getText().toString())) {
393 // if we are adding a call from the InCallScreen and the phone
394 // number entered is empty, we just close the dialer to expose
395 // the InCallScreen under it.
396 finish();
397 } else {
398 // otherwise, we place the call.
399 placeCall();
400 }
401 return true;
402 }
403 }
404 return super.onKeyDown(keyCode, event);
405 }
406
407 private void keyPressed(int keyCode) {
Yorke Lee116dd072015-08-31 11:38:39 -0700408 mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
410 mDigits.onKeyDown(keyCode, event);
411 }
412
413 @Override
414 public boolean onKey(View view, int keyCode, KeyEvent event) {
415 switch (view.getId()) {
416 case R.id.digits:
417 // Happen when "Done" button of the IME is pressed. This can happen when this
418 // Activity is forced into landscape mode due to a desk dock.
419 if (keyCode == KeyEvent.KEYCODE_ENTER
420 && event.getAction() == KeyEvent.ACTION_UP) {
421 placeCall();
422 return true;
423 }
424 break;
425 }
426 return false;
427 }
428
429 @Override
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700430 public boolean dispatchTouchEvent(MotionEvent ev) {
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800431 onPreTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700432 boolean handled = super.dispatchTouchEvent(ev);
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800433 onPostTouchEvent(ev);
Adrian Roos1c4b47f2015-04-13 14:53:32 -0700434 return handled;
435 }
436
437 @Override
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800438 public void onConfirmClick(EmergencyShortcutButton button) {
439 if (button == null) return;
440
441 String phoneNumber = button.getPhoneNumber();
442
443 if (!TextUtils.isEmpty(phoneNumber)) {
Billy Chi17ec2282018-06-15 19:00:15 +0800444 if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber));
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800445 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
446 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null);
447 } else {
448 Log.d(LOG_TAG, "emergency number is empty");
449 }
450 }
451
452 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700453 public void onClick(View view) {
454 switch (view.getId()) {
Yorke Lee23a70732014-08-14 17:12:01 -0700455 case R.id.deleteButton: {
456 keyPressed(KeyEvent.KEYCODE_DEL);
457 return;
458 }
459 case R.id.floating_action_button: {
Yorke Lee116dd072015-08-31 11:38:39 -0700460 view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Yorke Lee23a70732014-08-14 17:12:01 -0700461 placeCall();
462 return;
463 }
464 case R.id.digits: {
465 if (mDigits.length() != 0) {
466 mDigits.setCursorVisible(true);
467 }
468 return;
469 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800470 case R.id.floating_action_button_dialpad: {
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800471 mDigits.getText().clear();
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800472 switchView(mDialpadView, mEmergencyShortcutView, true);
473 return;
474 }
475 case R.id.emergency_info_button: {
476 Intent intent = (Intent) view.getTag(R.id.tag_intent);
477 if (intent != null) {
478 startActivity(intent);
479 }
480 return;
481 }
Yorke Lee23a70732014-08-14 17:12:01 -0700482 }
483 }
484
485 @Override
486 public void onPressed(View view, boolean pressed) {
487 if (!pressed) {
488 return;
489 }
490 switch (view.getId()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700491 case R.id.one: {
492 playTone(ToneGenerator.TONE_DTMF_1);
493 keyPressed(KeyEvent.KEYCODE_1);
494 return;
495 }
496 case R.id.two: {
497 playTone(ToneGenerator.TONE_DTMF_2);
498 keyPressed(KeyEvent.KEYCODE_2);
499 return;
500 }
501 case R.id.three: {
502 playTone(ToneGenerator.TONE_DTMF_3);
503 keyPressed(KeyEvent.KEYCODE_3);
504 return;
505 }
506 case R.id.four: {
507 playTone(ToneGenerator.TONE_DTMF_4);
508 keyPressed(KeyEvent.KEYCODE_4);
509 return;
510 }
511 case R.id.five: {
512 playTone(ToneGenerator.TONE_DTMF_5);
513 keyPressed(KeyEvent.KEYCODE_5);
514 return;
515 }
516 case R.id.six: {
517 playTone(ToneGenerator.TONE_DTMF_6);
518 keyPressed(KeyEvent.KEYCODE_6);
519 return;
520 }
521 case R.id.seven: {
522 playTone(ToneGenerator.TONE_DTMF_7);
523 keyPressed(KeyEvent.KEYCODE_7);
524 return;
525 }
526 case R.id.eight: {
527 playTone(ToneGenerator.TONE_DTMF_8);
528 keyPressed(KeyEvent.KEYCODE_8);
529 return;
530 }
531 case R.id.nine: {
532 playTone(ToneGenerator.TONE_DTMF_9);
533 keyPressed(KeyEvent.KEYCODE_9);
534 return;
535 }
536 case R.id.zero: {
537 playTone(ToneGenerator.TONE_DTMF_0);
538 keyPressed(KeyEvent.KEYCODE_0);
539 return;
540 }
541 case R.id.pound: {
542 playTone(ToneGenerator.TONE_DTMF_P);
543 keyPressed(KeyEvent.KEYCODE_POUND);
544 return;
545 }
546 case R.id.star: {
547 playTone(ToneGenerator.TONE_DTMF_S);
548 keyPressed(KeyEvent.KEYCODE_STAR);
549 return;
550 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700551 }
552 }
553
554 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700555 * called for long touch events
556 */
557 @Override
558 public boolean onLongClick(View view) {
559 int id = view.getId();
560 switch (id) {
561 case R.id.deleteButton: {
562 mDigits.getText().clear();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700563 return true;
564 }
565 case R.id.zero: {
Yorke Lee91311662014-10-24 14:50:45 -0700566 removePreviousDigitIfPossible();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700567 keyPressed(KeyEvent.KEYCODE_PLUS);
568 return true;
569 }
570 }
571 return false;
572 }
573
574 @Override
Lucas Dupineb9c5702017-05-10 16:57:09 -0700575 protected void onStart() {
576 super.onStart();
Chihhang Chuangf8d33002018-07-02 11:08:50 +0800577 // It does not support dark text theme, when emergency dialer shortcuts are enabled.
578 // And set background color to black.
579 if (mAreEmergencyDialerShortcutsEnabled) {
580 mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false);
581 updateTheme(false);
582 } else {
583 mColorExtractor.addOnColorsChangedListener(this);
584 GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK,
585 ColorExtractor.TYPE_EXTRA_DARK);
586 // Do not animate when view isn't visible yet, just set an initial state.
587 mBackgroundGradient.setColors(lockScreenColors, false);
588 updateTheme(lockScreenColors.supportsDarkText());
589 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700590 }
591
592 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700593 protected void onResume() {
594 super.onResume();
595
596 // retrieve the DTMF tone play back setting.
597 mDTMFToneEnabled = Settings.System.getInt(getContentResolver(),
598 Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
599
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700600 // if the mToneGenerator creation fails, just continue without it. It is
601 // a local audio signal, and is not as important as the dtmf tone itself.
602 synchronized (mToneGeneratorLock) {
603 if (mToneGenerator == null) {
604 try {
605 mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF,
606 TONE_RELATIVE_VOLUME);
607 } catch (RuntimeException e) {
608 Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e);
609 mToneGenerator = null;
610 }
611 }
612 }
613
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700614 updateDialAndDeleteButtonStateEnabledAttr();
615 }
616
617 @Override
618 public void onPause() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700619 super.onPause();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700620 }
621
Lucas Dupineb9c5702017-05-10 16:57:09 -0700622 @Override
623 protected void onStop() {
624 super.onStop();
Lucas Dupineb9c5702017-05-10 16:57:09 -0700625 mColorExtractor.removeOnColorsChangedListener(this);
626 }
627
628 /**
629 * Sets theme based on gradient colors
630 * @param supportsDarkText true if gradient supports dark text
631 */
632 private void updateTheme(boolean supportsDarkText) {
633 if (mSupportsDarkText == supportsDarkText) {
634 return;
635 }
636 mSupportsDarkText = supportsDarkText;
637
638 // We can't change themes after inflation, in this case we'll have to recreate
639 // the whole activity.
640 if (mBackgroundGradient != null) {
641 recreate();
642 return;
643 }
644
645 int vis = getWindow().getDecorView().getSystemUiVisibility();
646 if (supportsDarkText) {
647 vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
648 vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
649 setTheme(R.style.EmergencyDialerThemeDark);
650 } else {
651 vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
652 vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
653 setTheme(R.style.EmergencyDialerTheme);
654 }
655 getWindow().getDecorView().setSystemUiVisibility(vis);
656 }
657
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700658 /**
659 * place the call, but check to make sure it is a viable number.
660 */
661 private void placeCall() {
662 mLastNumber = mDigits.getText().toString();
Wei Huang6904b142017-04-21 19:06:40 +0900663
664 // Convert into emergency number according to emergency conversion map.
665 // If conversion map is not defined (this is default), this method does
666 // nothing and just returns input number.
667 mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber);
668
Yorke Lee36bb2542014-06-05 08:09:52 -0700669 if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700670 if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber);
671
672 // place the call if it is a valid number
673 if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) {
674 // There is no number entered.
675 playTone(ToneGenerator.TONE_PROP_NACK);
676 return;
677 }
Tyler Gunnfa77e202018-03-23 07:47:00 -0700678 TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE);
679 tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700680 } else {
681 if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber);
682
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700683 showDialog(BAD_EMERGENCY_NUMBER_DIALOG);
684 }
Yorke Lee9b341512014-10-17 11:36:41 -0700685 mDigits.getText().delete(0, mDigits.getText().length());
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700686 }
687
688 /**
689 * Plays the specified tone for TONE_LENGTH_MS milliseconds.
690 *
691 * The tone is played locally, using the audio stream for phone calls.
692 * Tones are played only if the "Audible touch tones" user preference
693 * is checked, and are NOT played if the device is in silent mode.
694 *
695 * @param tone a tone code from {@link ToneGenerator}
696 */
697 void playTone(int tone) {
698 // if local tone playback is disabled, just return.
699 if (!mDTMFToneEnabled) {
700 return;
701 }
702
703 // Also do nothing if the phone is in silent mode.
704 // We need to re-check the ringer mode for *every* playTone()
705 // call, rather than keeping a local flag that's updated in
706 // onResume(), since it's possible to toggle silent mode without
707 // leaving the current activity (via the ENDCALL-longpress menu.)
708 AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
709 int ringerMode = audioManager.getRingerMode();
710 if ((ringerMode == AudioManager.RINGER_MODE_SILENT)
711 || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) {
712 return;
713 }
714
715 synchronized (mToneGeneratorLock) {
716 if (mToneGenerator == null) {
717 Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone);
718 return;
719 }
720
721 // Start the new tone (will stop any playing tone)
722 mToneGenerator.startTone(tone, TONE_LENGTH_MS);
723 }
724 }
725
726 private CharSequence createErrorMessage(String number) {
727 if (!TextUtils.isEmpty(number)) {
Hall Liudc274312016-03-01 16:34:45 -0800728 String errorString = getString(R.string.dial_emergency_error, number);
729 int startingPosition = errorString.indexOf(number);
730 int endingPosition = startingPosition + number.length();
731 Spannable result = new SpannableString(errorString);
732 PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition);
733 return result;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700734 } else {
735 return getText(R.string.dial_emergency_empty_error).toString();
736 }
737 }
738
739 @Override
740 protected Dialog onCreateDialog(int id) {
741 AlertDialog dialog = null;
742 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
743 // construct dialog
Lucas Dupin0d666f92017-06-01 14:04:48 -0700744 dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme)
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700745 .setTitle(getText(R.string.emergency_enable_radio_dialog_title))
746 .setMessage(createErrorMessage(mLastNumber))
747 .setPositiveButton(R.string.ok, null)
748 .setCancelable(true).create();
749
750 // blur stuff behind the dialog
751 dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
Tyler Gunncd6a1a92018-03-29 13:48:29 -0700752 setShowWhenLocked(true);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700753 }
754 return dialog;
755 }
756
757 @Override
758 protected void onPrepareDialog(int id, Dialog dialog) {
759 super.onPrepareDialog(id, dialog);
760 if (id == BAD_EMERGENCY_NUMBER_DIALOG) {
761 AlertDialog alert = (AlertDialog) dialog;
762 alert.setMessage(createErrorMessage(mLastNumber));
763 }
764 }
765
Andrew Leed5631e82014-10-08 16:03:58 -0700766 @Override
767 public boolean onOptionsItemSelected(MenuItem item) {
768 final int itemId = item.getItemId();
769 if (itemId == android.R.id.home) {
770 onBackPressed();
771 return true;
772 }
773 return super.onOptionsItemSelected(item);
774 }
775
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700776 /**
777 * Update the enabledness of the "Dial" and "Backspace" buttons if applicable.
778 */
779 private void updateDialAndDeleteButtonStateEnabledAttr() {
780 final boolean notEmpty = mDigits.length() != 0;
781
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700782 mDelete.setEnabled(notEmpty);
783 }
Yorke Lee91311662014-10-24 14:50:45 -0700784
785 /**
786 * Remove the digit just before the current position. Used by various long pressed callbacks
787 * to remove the digit that was populated as a result of the short click.
788 */
789 private void removePreviousDigitIfPossible() {
790 final int currentPosition = mDigits.getSelectionStart();
791 if (currentPosition > 0) {
792 mDigits.setSelection(currentPosition);
793 mDigits.getText().delete(currentPosition - 1, currentPosition);
794 }
795 }
Ihab Awadf7c1a5a2014-12-08 19:24:23 -0800796
797 /**
798 * Update the text-to-speech annotations in the edit field.
799 */
800 private void updateTtsSpans() {
801 for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) {
802 mDigits.getText().removeSpan(o);
803 }
804 PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length());
805 }
Lucas Dupineb9c5702017-05-10 16:57:09 -0700806
807 @Override
Lucas Dupin94b566f2017-05-28 11:45:52 -0700808 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupineb9c5702017-05-10 16:57:09 -0700809 if ((which & WallpaperManager.FLAG_LOCK) != 0) {
Lucas Dupin94b566f2017-05-28 11:45:52 -0700810 GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK,
811 ColorExtractor.TYPE_EXTRA_DARK);
Lucas Dupineb9c5702017-05-10 16:57:09 -0700812 mBackgroundGradient.setColors(colors);
813 updateTheme(colors.supportsDarkText());
814 }
815 }
Tyler Gunnc5428972018-03-28 14:15:34 -0700816
817 /**
818 * Where a carrier requires a warning that emergency calling is not available while on WFC,
819 * add hint text above the dial pad which warns the user of this case.
820 */
821 private void maybeShowWfcEmergencyCallingWarning() {
822 if (!mIsWfcEmergencyCallingWarningEnabled) {
823 Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier.");
824 return;
825 }
826
827 // Use an async task rather than calling into Telephony on UI thread.
828 AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() {
829 @Override
830 protected Boolean doInBackground(Void... voids) {
831 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
832 boolean isWfcAvailable = tm.isWifiCallingAvailable();
833 ServiceState ss = tm.getServiceState();
834 boolean isCellAvailable =
835 ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN;
836 Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable
837 + " isCellAvailable=" + isCellAvailable
838 + "(rat=" + ss.getRilVoiceRadioTechnology() + ")");
839 return isWfcAvailable && !isCellAvailable;
840 }
841
842 @Override
843 protected void onPostExecute(Boolean result) {
844 if (result.booleanValue()) {
845 Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning");
846 mDigits.setHint(R.string.dial_emergency_calling_not_available);
847 } else {
848 Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning");
849 mDigits.setHint("");
850 }
851 maybeChangeHintSize();
852 }
853 };
854 showWfcWarningTask.execute((Void) null);
855 }
856
857 /**
858 * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits
859 * edit view and set the font size to a smaller size appropriate for the emergency calling
860 * warning.
861 */
862 private void maybeChangeHintSize() {
863 if (TextUtils.isEmpty(mDigits.getHint())
864 || !TextUtils.isEmpty(mDigits.getText().toString())) {
865 // No hint or there are dialed digits, so use default size.
866 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize);
867 // By default, the digits view auto-resizes to fit the text it contains, so
868 // enable that now.
869 mDigits.setResizeEnabled(true);
870 Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize());
871 } else {
872 // Hint present and no dialed digits, set custom font size appropriate for the warning.
873 mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(
874 R.dimen.emergency_call_warning_size));
875 // Since we're populating this with a static text string, disable auto-resize.
876 mDigits.setResizeEnabled(false);
877 Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize());
878 }
879 }
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800880
881 private void setupEmergencyShortcutsView() {
882 mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts);
883 mDialpadView = findViewById(R.id.emergency_dialer);
884
885 final View dialpadButton = findViewById(R.id.floating_action_button_dialpad);
886 dialpadButton.setOnClickListener(this);
887
888 final View emergencyInfoButton = findViewById(R.id.emergency_info_button);
889 emergencyInfoButton.setOnClickListener(this);
890
891 // EmergencyActionGroup is replaced by EmergencyInfoGroup.
892 mEmergencyActionGroup.setVisibility(View.GONE);
893
894 // Setup dialpad title.
895 final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container);
896 emergencyDialpadTitle.setVisibility(View.VISIBLE);
897
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800898 // TODO: Integrating emergency phone number table will get location information.
899 // Using null to present no location status.
900 setLocationInfo(null);
901
902 mEmergencyShortcutButtonList = new ArrayList<>();
903 setupEmergencyCallShortcutButton();
904
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800905 switchView(mEmergencyShortcutView, mDialpadView, false);
906 }
907
Chihhang Chuang92cfe512018-06-07 16:25:27 +0800908 private void setLocationInfo(String country) {
909 final View locationInfo = findViewById(R.id.location_info);
910
911 if (TextUtils.isEmpty(country)) {
912 locationInfo.setVisibility(View.INVISIBLE);
913 } else {
914 final TextView location = (TextView) locationInfo.findViewById(R.id.location_text);
915 location.setText(country);
916 locationInfo.setVisibility(View.VISIBLE);
917 }
918 }
919
920 // TODO: Integrate emergency phone number table.
921 // Using default layout(no location, phone number is 112, description is Emergency,
922 // and icon is cross shape) until integrating emergency phone number table.
923 private void setupEmergencyCallShortcutButton() {
924 final ViewGroup shortcutButtonContainer = findViewById(
925 R.id.emergency_shortcut_buttons_container);
926 shortcutButtonContainer.setClipToOutline(true);
927
928 final EmergencyShortcutButton button =
929 (EmergencyShortcutButton) getLayoutInflater().inflate(
930 R.layout.emergency_shortcut_button,
931 shortcutButtonContainer, false);
932
933 button.setPhoneNumber("112");
934 button.setPhoneDescription("Emergency");
935 button.setPhoneTypeIcon(R.drawable.ic_emergency_number_24);
936 button.setOnConfirmClickListener(this);
937
938 shortcutButtonContainer.addView(button);
939 mEmergencyShortcutButtonList.add(button);
940
941 //Set emergency number title for numerous buttons.
942 if (shortcutButtonContainer.getChildCount() > 1) {
943 final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title);
944 emergencyNumberTitle.setText(getString(R.string.numerous_emergency_numbers_title));
945 }
946 }
947
948 /**
949 * Called by the activity before a touch event is dispatched to the view hierarchy.
950 */
951 private void onPreTouchEvent(MotionEvent event) {
952 mEmergencyActionGroup.onPreTouchEvent(event);
953
954 if (mEmergencyShortcutButtonList != null) {
955 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
956 button.onPreTouchEvent(event);
957 }
958 }
959 }
960
961 /**
962 * Called by the activity after a touch event is dispatched to the view hierarchy.
963 */
964 private void onPostTouchEvent(MotionEvent event) {
965 mEmergencyActionGroup.onPostTouchEvent(event);
966
967 if (mEmergencyShortcutButtonList != null) {
968 for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) {
969 button.onPostTouchEvent(event);
970 }
971 }
972 }
973
Chihhang Chuangf264cfb2018-06-05 15:29:06 +0800974 /**
975 * Switch two view.
976 *
977 * @param displayView the view would be displayed.
978 * @param hideView the view would be hidden.
979 * @param hasAnimation is {@code true} when the view should be displayed with animation.
980 */
981 private void switchView(View displayView, View hideView, boolean hasAnimation) {
982 if (displayView == null || hideView == null) {
983 return;
984 }
985
986 if (displayView.getVisibility() == View.VISIBLE) {
987 return;
988 }
989
990 if (hasAnimation) {
991 crossfade(hideView, displayView);
992 } else {
993 hideView.setVisibility(View.GONE);
994 displayView.setVisibility(View.VISIBLE);
995 }
996 }
997
998 /**
999 * Fade out and fade in animation between two view transition.
1000 */
1001 private void crossfade(View fadeOutView, View fadeInView) {
1002 if (fadeOutView == null || fadeInView == null) {
1003 return;
1004 }
1005 final int shortAnimationDuration = getResources().getInteger(
1006 android.R.integer.config_shortAnimTime);
1007
1008 fadeInView.setAlpha(0f);
1009 fadeInView.setVisibility(View.VISIBLE);
1010
1011 fadeInView.animate()
1012 .alpha(1f)
1013 .setDuration(shortAnimationDuration)
1014 .setListener(null);
1015
1016 fadeOutView.animate()
1017 .alpha(0f)
1018 .setDuration(shortAnimationDuration)
1019 .setListener(new AnimatorListenerAdapter() {
1020 @Override
1021 public void onAnimationEnd(Animator animation) {
1022 fadeOutView.setVisibility(View.GONE);
1023 }
1024 });
1025 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026}