Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.phone; |
| 18 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 19 | import static android.telephony.ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 20 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorListenerAdapter; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 23 | import android.app.Activity; |
| 24 | import android.app.AlertDialog; |
| 25 | import android.app.Dialog; |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 26 | import android.app.WallpaperManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 31 | import android.database.DataSetObserver; |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 32 | import android.graphics.Color; |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 33 | import android.graphics.Point; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 34 | import android.media.AudioManager; |
| 35 | import android.media.ToneGenerator; |
| 36 | import android.net.Uri; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 37 | import android.os.AsyncTask; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 38 | import android.os.Bundle; |
Jonathan Basseri | c31f1f3 | 2015-05-12 10:13:03 -0700 | [diff] [blame] | 39 | import android.os.PersistableBundle; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Tyler Gunn | 4d45d1c | 2014-09-12 22:17:53 -0700 | [diff] [blame] | 41 | import android.telecom.PhoneAccount; |
Tyler Gunn | fa77e20 | 2018-03-23 07:47:00 -0700 | [diff] [blame] | 42 | import android.telecom.TelecomManager; |
Jonathan Basseri | 3649bdb | 2015-04-30 22:39:11 -0700 | [diff] [blame] | 43 | import android.telephony.CarrierConfigManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 44 | import android.telephony.PhoneNumberUtils; |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 45 | import android.telephony.Rlog; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 46 | import android.telephony.ServiceState; |
Santos Cordon | e137eed | 2015-06-23 15:34:47 -0700 | [diff] [blame] | 47 | import android.telephony.SubscriptionManager; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 48 | import android.telephony.TelephonyManager; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 49 | import android.text.Editable; |
Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 50 | import android.text.InputType; |
Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 51 | import android.text.Spannable; |
| 52 | import android.text.SpannableString; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 53 | import android.text.TextUtils; |
| 54 | import android.text.TextWatcher; |
| 55 | import android.text.method.DialerKeyListener; |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 56 | import android.text.style.TtsSpan; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 57 | import android.util.Log; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 58 | import android.util.TypedValue; |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 59 | import android.view.HapticFeedbackConstants; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 60 | import android.view.KeyEvent; |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 61 | import android.view.MenuItem; |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 62 | import android.view.MotionEvent; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 63 | import android.view.View; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 64 | import android.view.ViewGroup; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 65 | import android.view.WindowManager; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 66 | import android.widget.TextView; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 67 | |
Lucas Dupin | af9e991 | 2017-06-22 12:39:39 -0700 | [diff] [blame] | 68 | import com.android.internal.colorextraction.ColorExtractor; |
| 69 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
| 70 | import com.android.internal.colorextraction.drawable.GradientDrawable; |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 71 | import com.android.phone.common.dialpad.DialpadKeyButton; |
Sai Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 72 | import com.android.phone.common.util.ViewUtil; |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 73 | import com.android.phone.common.widget.ResizingTextEditText; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 74 | import com.android.phone.ecc.CountryEccInfo; |
| 75 | import com.android.phone.ecc.EccInfoHelper; |
Leo Hsu | 779569a | 2018-07-10 11:37:52 +0800 | [diff] [blame] | 76 | import com.android.phone.ecc.IsoToEccProtobufRepository; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 77 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 78 | import java.util.ArrayList; |
| 79 | import java.util.List; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 80 | import java.util.Locale; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 81 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 82 | /** |
| 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 Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 98 | * |
| 99 | * TODO: Implement emergency dialer shortcut. |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 100 | * 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 Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 102 | * TODO item: |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 103 | * 1.integrate emergency phone number table. |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 104 | */ |
| 105 | public class EmergencyDialer extends Activity implements View.OnClickListener, |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 106 | View.OnLongClickListener, View.OnKeyListener, TextWatcher, |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 107 | DialpadKeyButton.OnPressedListener, ColorExtractor.OnColorsChangedListener, |
| 108 | EmergencyShortcutButton.OnConfirmClickListener { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 109 | // 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 Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 126 | /** 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 Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 137 | /** 90% opacity, different from other gradients **/ |
| 138 | private static final int BACKGROUND_GRADIENT_ALPHA = 230; |
| 139 | |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 140 | /** 85% opacity for black background **/ |
| 141 | private static final int BLACK_BACKGROUND_GRADIENT_ALPHA = 217; |
| 142 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 143 | /** Size limit of emergency shortcut buttons container. **/ |
| 144 | private static final int SHORTCUT_SIZE_LIMIT = 3; |
| 145 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 146 | ResizingTextEditText mDigits; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 147 | private View mDialButton; |
| 148 | private View mDelete; |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 149 | private View mEmergencyShortcutView; |
| 150 | private View mDialpadView; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 151 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 152 | private EccInfoHelper mEccInfoHelper; |
| 153 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 154 | private List<EmergencyShortcutButton> mEmergencyShortcutButtonList; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 155 | private EccShortcutAdapter mShortcutAdapter; |
| 156 | private DataSetObserver mShortcutDataSetObserver = null; |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 157 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 158 | 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 Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 164 | private EmergencyActionGroup mEmergencyActionGroup; |
| 165 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 166 | // 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 Roos | 061c723 | 2015-04-21 12:37:07 -0700 | [diff] [blame] | 171 | finishAndRemoveTask(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | private String mLastNumber; // last number we tried to dial. Used to restore error dialog. |
| 177 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 178 | // Background gradient |
| 179 | private ColorExtractor mColorExtractor; |
| 180 | private GradientDrawable mBackgroundGradient; |
| 181 | private boolean mSupportsDarkText; |
| 182 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 183 | private boolean mIsWfcEmergencyCallingWarningEnabled; |
| 184 | private float mDefaultDigitsTextSize; |
| 185 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 186 | private boolean mAreEmergencyDialerShortcutsEnabled; |
| 187 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 188 | @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 Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 195 | maybeChangeHintSize(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 196 | } |
| 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 Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 215 | updateTtsSpans(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | protected void onCreate(Bundle icicle) { |
| 220 | super.onCreate(icicle); |
| 221 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 222 | // Allow this activity to be displayed in front of the keyguard / lockscreen. |
Mengjun Leng | b9d14f0 | 2017-10-31 14:28:14 +0800 | [diff] [blame] | 223 | setShowWhenLocked(true); |
| 224 | // Allow turning screen on |
| 225 | setTurnScreenOn(true); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 226 | |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 227 | mAreEmergencyDialerShortcutsEnabled = Settings.Global.getInt(getContentResolver(), |
| 228 | Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0; |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 229 | Log.d(LOG_TAG, "Enable emergency dialer shortcut: " |
| 230 | + mAreEmergencyDialerShortcutsEnabled); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 231 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 232 | mColorExtractor = new ColorExtractor(this); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 233 | |
| 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 Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 243 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 244 | setContentView(R.layout.emergency_dialer); |
| 245 | |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 246 | mDigits = (ResizingTextEditText) findViewById(R.id.digits); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 247 | mDigits.setKeyListener(DialerKeyListener.getInstance()); |
| 248 | mDigits.setOnClickListener(this); |
| 249 | mDigits.setOnKeyListener(this); |
| 250 | mDigits.setLongClickable(false); |
Hall Liu | bdc9c88 | 2016-05-25 15:25:28 -0700 | [diff] [blame] | 251 | mDigits.setInputType(InputType.TYPE_NULL); |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 252 | mDefaultDigitsTextSize = mDigits.getScaledTextSize(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 253 | maybeAddNumberFormatting(); |
| 254 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 255 | 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 Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 260 | mBackgroundGradient.setAlpha(mAreEmergencyDialerShortcutsEnabled |
| 261 | ? BLACK_BACKGROUND_GRADIENT_ALPHA : BACKGROUND_GRADIENT_ALPHA); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 262 | getWindow().setBackgroundDrawable(mBackgroundGradient); |
| 263 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 264 | // 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 Cheemalapati | 14462b6 | 2014-06-18 13:53:56 -0700 | [diff] [blame] | 274 | mDialButton = findViewById(R.id.floating_action_button); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 275 | |
| 276 | // Check whether we should show the onscreen "Dial" button and co. |
Jonathan Basseri | e619a4c | 2015-06-26 14:52:26 -0700 | [diff] [blame] | 277 | // 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 Agrawal | d3480e0 | 2016-01-25 13:05:49 -0800 | [diff] [blame] | 282 | configMgr.getConfigForSubId(SubscriptionManager.getDefaultVoiceSubscriptionId()); |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 283 | |
Jonathan Basseri | 9504c6b | 2015-06-04 14:23:32 -0700 | [diff] [blame] | 284 | if (carrierConfig.getBoolean(CarrierConfigManager.KEY_SHOW_ONSCREEN_DIAL_BUTTON_BOOL)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 285 | mDialButton.setOnClickListener(this); |
| 286 | } else { |
| 287 | mDialButton.setVisibility(View.GONE); |
| 288 | } |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 289 | mIsWfcEmergencyCallingWarningEnabled = carrierConfig.getInt( |
| 290 | CarrierConfigManager.KEY_EMERGENCY_NOTIFICATION_DELAY_INT) > -1; |
| 291 | maybeShowWfcEmergencyCallingWarning(); |
| 292 | |
Adrian Roos | c9fdb6c | 2015-05-25 15:10:21 -0700 | [diff] [blame] | 293 | ViewUtil.setupFloatingActionButton(mDialButton, getResources()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 294 | |
| 295 | if (icicle != null) { |
| 296 | super.onRestoreInstanceState(icicle); |
| 297 | } |
| 298 | |
| 299 | // Extract phone number from intent |
| 300 | Uri data = getIntent().getData(); |
Jay Shrauner | 137458b | 2014-09-05 14:27:25 -0700 | [diff] [blame] | 301 | if (data != null && (PhoneAccount.SCHEME_TEL.equals(data.getScheme()))) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 302 | 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 Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 325 | mEmergencyActionGroup = (EmergencyActionGroup) findViewById(R.id.emergency_action_group); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 326 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 327 | if (mAreEmergencyDialerShortcutsEnabled) { |
Leo Hsu | 779569a | 2018-07-10 11:37:52 +0800 | [diff] [blame] | 328 | mEccInfoHelper = new EccInfoHelper(new IsoToEccProtobufRepository()); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 329 | setupEmergencyShortcutsView(); |
| 330 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @Override |
| 334 | protected void onDestroy() { |
| 335 | super.onDestroy(); |
| 336 | synchronized (mToneGeneratorLock) { |
| 337 | if (mToneGenerator != null) { |
| 338 | mToneGenerator.release(); |
| 339 | mToneGenerator = null; |
| 340 | } |
| 341 | } |
| 342 | unregisterReceiver(mBroadcastReceiver); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 343 | if (mShortcutAdapter != null && mShortcutDataSetObserver != null) { |
| 344 | mShortcutAdapter.unregisterDataSetObserver(mShortcutDataSetObserver); |
| 345 | mShortcutDataSetObserver = null; |
| 346 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | @Override |
| 350 | protected void onRestoreInstanceState(Bundle icicle) { |
| 351 | mLastNumber = icicle.getString(LAST_NUMBER); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | protected void onSaveInstanceState(Bundle outState) { |
| 356 | super.onSaveInstanceState(outState); |
| 357 | outState.putString(LAST_NUMBER, mLastNumber); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Explicitly turn off number formatting, since it gets in the way of the emergency |
| 362 | * number detector |
| 363 | */ |
| 364 | protected void maybeAddNumberFormatting() { |
| 365 | // Do nothing. |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | protected void onPostCreate(Bundle savedInstanceState) { |
| 370 | super.onPostCreate(savedInstanceState); |
| 371 | |
| 372 | // This can't be done in onCreate(), since the auto-restoring of the digits |
| 373 | // will play DTMF tones for all the old digits if it is when onRestoreSavedInstanceState() |
| 374 | // is called. This method will be called every time the activity is created, and |
| 375 | // will always happen after onRestoreSavedInstanceState(). |
| 376 | mDigits.addTextChangedListener(this); |
| 377 | } |
| 378 | |
| 379 | private void setupKeypad() { |
| 380 | // Setup the listeners for the buttons |
| 381 | for (int id : DIALER_KEYS) { |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 382 | final DialpadKeyButton key = (DialpadKeyButton) findViewById(id); |
| 383 | key.setOnPressedListener(this); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | View view = findViewById(R.id.zero); |
| 387 | view.setOnLongClickListener(this); |
| 388 | } |
| 389 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 390 | @Override |
| 391 | public void onBackPressed() { |
| 392 | // If emergency dialer shortcut is enabled and Dialpad view is visible, pressing the |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 393 | // back key will back to display EmergencyShortcutView view. |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 394 | // Otherwise, it would finish the activity. |
| 395 | if (mAreEmergencyDialerShortcutsEnabled && mDialpadView != null |
| 396 | && mDialpadView.getVisibility() == View.VISIBLE) { |
| 397 | switchView(mEmergencyShortcutView, mDialpadView, true); |
| 398 | return; |
| 399 | } |
| 400 | super.onBackPressed(); |
| 401 | } |
| 402 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 403 | /** |
| 404 | * handle key events |
| 405 | */ |
| 406 | @Override |
| 407 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 408 | switch (keyCode) { |
| 409 | // Happen when there's a "Call" hard button. |
| 410 | case KeyEvent.KEYCODE_CALL: { |
| 411 | if (TextUtils.isEmpty(mDigits.getText().toString())) { |
| 412 | // if we are adding a call from the InCallScreen and the phone |
| 413 | // number entered is empty, we just close the dialer to expose |
| 414 | // the InCallScreen under it. |
| 415 | finish(); |
| 416 | } else { |
| 417 | // otherwise, we place the call. |
| 418 | placeCall(); |
| 419 | } |
| 420 | return true; |
| 421 | } |
| 422 | } |
| 423 | return super.onKeyDown(keyCode, event); |
| 424 | } |
| 425 | |
| 426 | private void keyPressed(int keyCode) { |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 427 | mDigits.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 428 | KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode); |
| 429 | mDigits.onKeyDown(keyCode, event); |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public boolean onKey(View view, int keyCode, KeyEvent event) { |
| 434 | switch (view.getId()) { |
| 435 | case R.id.digits: |
| 436 | // Happen when "Done" button of the IME is pressed. This can happen when this |
| 437 | // Activity is forced into landscape mode due to a desk dock. |
| 438 | if (keyCode == KeyEvent.KEYCODE_ENTER |
| 439 | && event.getAction() == KeyEvent.ACTION_UP) { |
| 440 | placeCall(); |
| 441 | return true; |
| 442 | } |
| 443 | break; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | @Override |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 449 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 450 | onPreTouchEvent(ev); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 451 | boolean handled = super.dispatchTouchEvent(ev); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 452 | onPostTouchEvent(ev); |
Adrian Roos | 1c4b47f | 2015-04-13 14:53:32 -0700 | [diff] [blame] | 453 | return handled; |
| 454 | } |
| 455 | |
| 456 | @Override |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 457 | public void onConfirmClick(EmergencyShortcutButton button) { |
| 458 | if (button == null) return; |
| 459 | |
| 460 | String phoneNumber = button.getPhoneNumber(); |
| 461 | |
| 462 | if (!TextUtils.isEmpty(phoneNumber)) { |
Billy Chi | 17ec228 | 2018-06-15 19:00:15 +0800 | [diff] [blame] | 463 | if (DBG) Log.d(LOG_TAG, "dial emergency number: " + Rlog.pii(LOG_TAG, phoneNumber)); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 464 | TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE); |
| 465 | tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null), null); |
| 466 | } else { |
| 467 | Log.d(LOG_TAG, "emergency number is empty"); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | @Override |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 472 | public void onClick(View view) { |
| 473 | switch (view.getId()) { |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 474 | case R.id.deleteButton: { |
| 475 | keyPressed(KeyEvent.KEYCODE_DEL); |
| 476 | return; |
| 477 | } |
| 478 | case R.id.floating_action_button: { |
Yorke Lee | 116dd07 | 2015-08-31 11:38:39 -0700 | [diff] [blame] | 479 | view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 480 | placeCall(); |
| 481 | return; |
| 482 | } |
| 483 | case R.id.digits: { |
| 484 | if (mDigits.length() != 0) { |
| 485 | mDigits.setCursorVisible(true); |
| 486 | } |
| 487 | return; |
| 488 | } |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 489 | case R.id.floating_action_button_dialpad: { |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 490 | mDigits.getText().clear(); |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 491 | switchView(mDialpadView, mEmergencyShortcutView, true); |
| 492 | return; |
| 493 | } |
| 494 | case R.id.emergency_info_button: { |
| 495 | Intent intent = (Intent) view.getTag(R.id.tag_intent); |
| 496 | if (intent != null) { |
| 497 | startActivity(intent); |
| 498 | } |
| 499 | return; |
| 500 | } |
Yorke Lee | 23a7073 | 2014-08-14 17:12:01 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void onPressed(View view, boolean pressed) { |
| 506 | if (!pressed) { |
| 507 | return; |
| 508 | } |
| 509 | switch (view.getId()) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 510 | case R.id.one: { |
| 511 | playTone(ToneGenerator.TONE_DTMF_1); |
| 512 | keyPressed(KeyEvent.KEYCODE_1); |
| 513 | return; |
| 514 | } |
| 515 | case R.id.two: { |
| 516 | playTone(ToneGenerator.TONE_DTMF_2); |
| 517 | keyPressed(KeyEvent.KEYCODE_2); |
| 518 | return; |
| 519 | } |
| 520 | case R.id.three: { |
| 521 | playTone(ToneGenerator.TONE_DTMF_3); |
| 522 | keyPressed(KeyEvent.KEYCODE_3); |
| 523 | return; |
| 524 | } |
| 525 | case R.id.four: { |
| 526 | playTone(ToneGenerator.TONE_DTMF_4); |
| 527 | keyPressed(KeyEvent.KEYCODE_4); |
| 528 | return; |
| 529 | } |
| 530 | case R.id.five: { |
| 531 | playTone(ToneGenerator.TONE_DTMF_5); |
| 532 | keyPressed(KeyEvent.KEYCODE_5); |
| 533 | return; |
| 534 | } |
| 535 | case R.id.six: { |
| 536 | playTone(ToneGenerator.TONE_DTMF_6); |
| 537 | keyPressed(KeyEvent.KEYCODE_6); |
| 538 | return; |
| 539 | } |
| 540 | case R.id.seven: { |
| 541 | playTone(ToneGenerator.TONE_DTMF_7); |
| 542 | keyPressed(KeyEvent.KEYCODE_7); |
| 543 | return; |
| 544 | } |
| 545 | case R.id.eight: { |
| 546 | playTone(ToneGenerator.TONE_DTMF_8); |
| 547 | keyPressed(KeyEvent.KEYCODE_8); |
| 548 | return; |
| 549 | } |
| 550 | case R.id.nine: { |
| 551 | playTone(ToneGenerator.TONE_DTMF_9); |
| 552 | keyPressed(KeyEvent.KEYCODE_9); |
| 553 | return; |
| 554 | } |
| 555 | case R.id.zero: { |
| 556 | playTone(ToneGenerator.TONE_DTMF_0); |
| 557 | keyPressed(KeyEvent.KEYCODE_0); |
| 558 | return; |
| 559 | } |
| 560 | case R.id.pound: { |
| 561 | playTone(ToneGenerator.TONE_DTMF_P); |
| 562 | keyPressed(KeyEvent.KEYCODE_POUND); |
| 563 | return; |
| 564 | } |
| 565 | case R.id.star: { |
| 566 | playTone(ToneGenerator.TONE_DTMF_S); |
| 567 | keyPressed(KeyEvent.KEYCODE_STAR); |
| 568 | return; |
| 569 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 574 | * called for long touch events |
| 575 | */ |
| 576 | @Override |
| 577 | public boolean onLongClick(View view) { |
| 578 | int id = view.getId(); |
| 579 | switch (id) { |
| 580 | case R.id.deleteButton: { |
| 581 | mDigits.getText().clear(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | case R.id.zero: { |
Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 585 | removePreviousDigitIfPossible(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 586 | keyPressed(KeyEvent.KEYCODE_PLUS); |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | @Override |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 594 | protected void onStart() { |
| 595 | super.onStart(); |
Chihhang Chuang | f8d3300 | 2018-07-02 11:08:50 +0800 | [diff] [blame] | 596 | // It does not support dark text theme, when emergency dialer shortcuts are enabled. |
| 597 | // And set background color to black. |
| 598 | if (mAreEmergencyDialerShortcutsEnabled) { |
| 599 | mBackgroundGradient.setColors(Color.BLACK, Color.BLACK, false); |
| 600 | updateTheme(false); |
| 601 | } else { |
| 602 | mColorExtractor.addOnColorsChangedListener(this); |
| 603 | GradientColors lockScreenColors = mColorExtractor.getColors(WallpaperManager.FLAG_LOCK, |
| 604 | ColorExtractor.TYPE_EXTRA_DARK); |
| 605 | // Do not animate when view isn't visible yet, just set an initial state. |
| 606 | mBackgroundGradient.setColors(lockScreenColors, false); |
| 607 | updateTheme(lockScreenColors.supportsDarkText()); |
| 608 | } |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 609 | |
| 610 | if (mAreEmergencyDialerShortcutsEnabled && mEccInfoHelper != null) { |
| 611 | final Context context = this; |
| 612 | mEccInfoHelper.getCountryEccInfoAsync(context, |
| 613 | new EccInfoHelper.CountryEccInfoResultCallback() { |
| 614 | @Override |
| 615 | public void onSuccess(String iso, CountryEccInfo countryEccInfo) { |
| 616 | Log.d(LOG_TAG, "Retrieve ECC info success, country ISO: " |
| 617 | + Rlog.pii(LOG_TAG, iso)); |
| 618 | updateLocationAndEccInfo(iso, countryEccInfo); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void onDetectCountryFailed() { |
| 623 | Log.w(LOG_TAG, "Cannot detect current country."); |
| 624 | updateLocationAndEccInfo(null, null); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void onRetrieveCountryEccInfoFailed(String iso) { |
| 629 | Log.w(LOG_TAG, "Retrieve ECC info failed, country ISO: " |
| 630 | + Rlog.pii(LOG_TAG, iso)); |
| 631 | updateLocationAndEccInfo(iso, null); |
| 632 | } |
| 633 | }); |
| 634 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | @Override |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 638 | protected void onResume() { |
| 639 | super.onResume(); |
| 640 | |
| 641 | // retrieve the DTMF tone play back setting. |
| 642 | mDTMFToneEnabled = Settings.System.getInt(getContentResolver(), |
| 643 | Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1; |
| 644 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 645 | // if the mToneGenerator creation fails, just continue without it. It is |
| 646 | // a local audio signal, and is not as important as the dtmf tone itself. |
| 647 | synchronized (mToneGeneratorLock) { |
| 648 | if (mToneGenerator == null) { |
| 649 | try { |
| 650 | mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, |
| 651 | TONE_RELATIVE_VOLUME); |
| 652 | } catch (RuntimeException e) { |
| 653 | Log.w(LOG_TAG, "Exception caught while creating local tone generator: " + e); |
| 654 | mToneGenerator = null; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 659 | updateDialAndDeleteButtonStateEnabledAttr(); |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public void onPause() { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 664 | super.onPause(); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 667 | @Override |
| 668 | protected void onStop() { |
| 669 | super.onStop(); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 670 | mColorExtractor.removeOnColorsChangedListener(this); |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Sets theme based on gradient colors |
| 675 | * @param supportsDarkText true if gradient supports dark text |
| 676 | */ |
| 677 | private void updateTheme(boolean supportsDarkText) { |
| 678 | if (mSupportsDarkText == supportsDarkText) { |
| 679 | return; |
| 680 | } |
| 681 | mSupportsDarkText = supportsDarkText; |
| 682 | |
| 683 | // We can't change themes after inflation, in this case we'll have to recreate |
| 684 | // the whole activity. |
| 685 | if (mBackgroundGradient != null) { |
| 686 | recreate(); |
| 687 | return; |
| 688 | } |
| 689 | |
| 690 | int vis = getWindow().getDecorView().getSystemUiVisibility(); |
| 691 | if (supportsDarkText) { |
| 692 | vis |= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 693 | vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 694 | setTheme(R.style.EmergencyDialerThemeDark); |
| 695 | } else { |
| 696 | vis &= View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR; |
| 697 | vis &= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; |
| 698 | setTheme(R.style.EmergencyDialerTheme); |
| 699 | } |
| 700 | getWindow().getDecorView().setSystemUiVisibility(vis); |
| 701 | } |
| 702 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 703 | /** |
| 704 | * place the call, but check to make sure it is a viable number. |
| 705 | */ |
| 706 | private void placeCall() { |
| 707 | mLastNumber = mDigits.getText().toString(); |
Wei Huang | 6904b14 | 2017-04-21 19:06:40 +0900 | [diff] [blame] | 708 | |
| 709 | // Convert into emergency number according to emergency conversion map. |
| 710 | // If conversion map is not defined (this is default), this method does |
| 711 | // nothing and just returns input number. |
| 712 | mLastNumber = PhoneNumberUtils.convertToEmergencyNumber(this, mLastNumber); |
| 713 | |
Yorke Lee | 36bb254 | 2014-06-05 08:09:52 -0700 | [diff] [blame] | 714 | if (PhoneNumberUtils.isLocalEmergencyNumber(this, mLastNumber)) { |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 715 | if (DBG) Log.d(LOG_TAG, "placing call to " + mLastNumber); |
| 716 | |
| 717 | // place the call if it is a valid number |
| 718 | if (mLastNumber == null || !TextUtils.isGraphic(mLastNumber)) { |
| 719 | // There is no number entered. |
| 720 | playTone(ToneGenerator.TONE_PROP_NACK); |
| 721 | return; |
| 722 | } |
Tyler Gunn | fa77e20 | 2018-03-23 07:47:00 -0700 | [diff] [blame] | 723 | TelecomManager tm = (TelecomManager) getSystemService(TELECOM_SERVICE); |
| 724 | tm.placeCall(Uri.fromParts(PhoneAccount.SCHEME_TEL, mLastNumber, null), null); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 725 | } else { |
| 726 | if (DBG) Log.d(LOG_TAG, "rejecting bad requested number " + mLastNumber); |
| 727 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 728 | showDialog(BAD_EMERGENCY_NUMBER_DIALOG); |
| 729 | } |
Yorke Lee | 9b34151 | 2014-10-17 11:36:41 -0700 | [diff] [blame] | 730 | mDigits.getText().delete(0, mDigits.getText().length()); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Plays the specified tone for TONE_LENGTH_MS milliseconds. |
| 735 | * |
| 736 | * The tone is played locally, using the audio stream for phone calls. |
| 737 | * Tones are played only if the "Audible touch tones" user preference |
| 738 | * is checked, and are NOT played if the device is in silent mode. |
| 739 | * |
| 740 | * @param tone a tone code from {@link ToneGenerator} |
| 741 | */ |
| 742 | void playTone(int tone) { |
| 743 | // if local tone playback is disabled, just return. |
| 744 | if (!mDTMFToneEnabled) { |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | // Also do nothing if the phone is in silent mode. |
| 749 | // We need to re-check the ringer mode for *every* playTone() |
| 750 | // call, rather than keeping a local flag that's updated in |
| 751 | // onResume(), since it's possible to toggle silent mode without |
| 752 | // leaving the current activity (via the ENDCALL-longpress menu.) |
| 753 | AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); |
| 754 | int ringerMode = audioManager.getRingerMode(); |
| 755 | if ((ringerMode == AudioManager.RINGER_MODE_SILENT) |
| 756 | || (ringerMode == AudioManager.RINGER_MODE_VIBRATE)) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | synchronized (mToneGeneratorLock) { |
| 761 | if (mToneGenerator == null) { |
| 762 | Log.w(LOG_TAG, "playTone: mToneGenerator == null, tone: " + tone); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // Start the new tone (will stop any playing tone) |
| 767 | mToneGenerator.startTone(tone, TONE_LENGTH_MS); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | private CharSequence createErrorMessage(String number) { |
| 772 | if (!TextUtils.isEmpty(number)) { |
Hall Liu | dc27431 | 2016-03-01 16:34:45 -0800 | [diff] [blame] | 773 | String errorString = getString(R.string.dial_emergency_error, number); |
| 774 | int startingPosition = errorString.indexOf(number); |
| 775 | int endingPosition = startingPosition + number.length(); |
| 776 | Spannable result = new SpannableString(errorString); |
| 777 | PhoneNumberUtils.addTtsSpan(result, startingPosition, endingPosition); |
| 778 | return result; |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 779 | } else { |
| 780 | return getText(R.string.dial_emergency_empty_error).toString(); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | protected Dialog onCreateDialog(int id) { |
| 786 | AlertDialog dialog = null; |
| 787 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 788 | // construct dialog |
Lucas Dupin | 0d666f9 | 2017-06-01 14:04:48 -0700 | [diff] [blame] | 789 | dialog = new AlertDialog.Builder(this, R.style.EmergencyDialerAlertDialogTheme) |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 790 | .setTitle(getText(R.string.emergency_enable_radio_dialog_title)) |
| 791 | .setMessage(createErrorMessage(mLastNumber)) |
| 792 | .setPositiveButton(R.string.ok, null) |
| 793 | .setCancelable(true).create(); |
| 794 | |
| 795 | // blur stuff behind the dialog |
| 796 | dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
Tyler Gunn | cd6a1a9 | 2018-03-29 13:48:29 -0700 | [diff] [blame] | 797 | setShowWhenLocked(true); |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 798 | } |
| 799 | return dialog; |
| 800 | } |
| 801 | |
| 802 | @Override |
| 803 | protected void onPrepareDialog(int id, Dialog dialog) { |
| 804 | super.onPrepareDialog(id, dialog); |
| 805 | if (id == BAD_EMERGENCY_NUMBER_DIALOG) { |
| 806 | AlertDialog alert = (AlertDialog) dialog; |
| 807 | alert.setMessage(createErrorMessage(mLastNumber)); |
| 808 | } |
| 809 | } |
| 810 | |
Andrew Lee | d5631e8 | 2014-10-08 16:03:58 -0700 | [diff] [blame] | 811 | @Override |
| 812 | public boolean onOptionsItemSelected(MenuItem item) { |
| 813 | final int itemId = item.getItemId(); |
| 814 | if (itemId == android.R.id.home) { |
| 815 | onBackPressed(); |
| 816 | return true; |
| 817 | } |
| 818 | return super.onOptionsItemSelected(item); |
| 819 | } |
| 820 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 821 | /** |
| 822 | * Update the enabledness of the "Dial" and "Backspace" buttons if applicable. |
| 823 | */ |
| 824 | private void updateDialAndDeleteButtonStateEnabledAttr() { |
| 825 | final boolean notEmpty = mDigits.length() != 0; |
| 826 | |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 827 | mDelete.setEnabled(notEmpty); |
| 828 | } |
Yorke Lee | 9131166 | 2014-10-24 14:50:45 -0700 | [diff] [blame] | 829 | |
| 830 | /** |
| 831 | * Remove the digit just before the current position. Used by various long pressed callbacks |
| 832 | * to remove the digit that was populated as a result of the short click. |
| 833 | */ |
| 834 | private void removePreviousDigitIfPossible() { |
| 835 | final int currentPosition = mDigits.getSelectionStart(); |
| 836 | if (currentPosition > 0) { |
| 837 | mDigits.setSelection(currentPosition); |
| 838 | mDigits.getText().delete(currentPosition - 1, currentPosition); |
| 839 | } |
| 840 | } |
Ihab Awad | f7c1a5a | 2014-12-08 19:24:23 -0800 | [diff] [blame] | 841 | |
| 842 | /** |
| 843 | * Update the text-to-speech annotations in the edit field. |
| 844 | */ |
| 845 | private void updateTtsSpans() { |
| 846 | for (Object o : mDigits.getText().getSpans(0, mDigits.getText().length(), TtsSpan.class)) { |
| 847 | mDigits.getText().removeSpan(o); |
| 848 | } |
| 849 | PhoneNumberUtils.ttsSpanAsPhoneNumber(mDigits.getText(), 0, mDigits.getText().length()); |
| 850 | } |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 851 | |
| 852 | @Override |
Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 853 | public void onColorsChanged(ColorExtractor extractor, int which) { |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 854 | if ((which & WallpaperManager.FLAG_LOCK) != 0) { |
Lucas Dupin | 94b566f | 2017-05-28 11:45:52 -0700 | [diff] [blame] | 855 | GradientColors colors = extractor.getColors(WallpaperManager.FLAG_LOCK, |
| 856 | ColorExtractor.TYPE_EXTRA_DARK); |
Lucas Dupin | eb9c570 | 2017-05-10 16:57:09 -0700 | [diff] [blame] | 857 | mBackgroundGradient.setColors(colors); |
| 858 | updateTheme(colors.supportsDarkText()); |
| 859 | } |
| 860 | } |
Tyler Gunn | c542897 | 2018-03-28 14:15:34 -0700 | [diff] [blame] | 861 | |
| 862 | /** |
| 863 | * Where a carrier requires a warning that emergency calling is not available while on WFC, |
| 864 | * add hint text above the dial pad which warns the user of this case. |
| 865 | */ |
| 866 | private void maybeShowWfcEmergencyCallingWarning() { |
| 867 | if (!mIsWfcEmergencyCallingWarningEnabled) { |
| 868 | Log.i(LOG_TAG, "maybeShowWfcEmergencyCallingWarning: warning disabled by carrier."); |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | // Use an async task rather than calling into Telephony on UI thread. |
| 873 | AsyncTask<Void, Void, Boolean> showWfcWarningTask = new AsyncTask<Void, Void, Boolean>() { |
| 874 | @Override |
| 875 | protected Boolean doInBackground(Void... voids) { |
| 876 | TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); |
| 877 | boolean isWfcAvailable = tm.isWifiCallingAvailable(); |
| 878 | ServiceState ss = tm.getServiceState(); |
| 879 | boolean isCellAvailable = |
| 880 | ss.getRilVoiceRadioTechnology() != RIL_RADIO_TECHNOLOGY_UNKNOWN; |
| 881 | Log.i(LOG_TAG, "showWfcWarningTask: isWfcAvailable=" + isWfcAvailable |
| 882 | + " isCellAvailable=" + isCellAvailable |
| 883 | + "(rat=" + ss.getRilVoiceRadioTechnology() + ")"); |
| 884 | return isWfcAvailable && !isCellAvailable; |
| 885 | } |
| 886 | |
| 887 | @Override |
| 888 | protected void onPostExecute(Boolean result) { |
| 889 | if (result.booleanValue()) { |
| 890 | Log.i(LOG_TAG, "showWfcWarningTask: showing ecall warning"); |
| 891 | mDigits.setHint(R.string.dial_emergency_calling_not_available); |
| 892 | } else { |
| 893 | Log.i(LOG_TAG, "showWfcWarningTask: hiding ecall warning"); |
| 894 | mDigits.setHint(""); |
| 895 | } |
| 896 | maybeChangeHintSize(); |
| 897 | } |
| 898 | }; |
| 899 | showWfcWarningTask.execute((Void) null); |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * Where a hint is applied and there are no digits dialed, disable autoresize of the dial digits |
| 904 | * edit view and set the font size to a smaller size appropriate for the emergency calling |
| 905 | * warning. |
| 906 | */ |
| 907 | private void maybeChangeHintSize() { |
| 908 | if (TextUtils.isEmpty(mDigits.getHint()) |
| 909 | || !TextUtils.isEmpty(mDigits.getText().toString())) { |
| 910 | // No hint or there are dialed digits, so use default size. |
| 911 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_SP, mDefaultDigitsTextSize); |
| 912 | // By default, the digits view auto-resizes to fit the text it contains, so |
| 913 | // enable that now. |
| 914 | mDigits.setResizeEnabled(true); |
| 915 | Log.i(LOG_TAG, "no hint - setting to " + mDigits.getScaledTextSize()); |
| 916 | } else { |
| 917 | // Hint present and no dialed digits, set custom font size appropriate for the warning. |
| 918 | mDigits.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize( |
| 919 | R.dimen.emergency_call_warning_size)); |
| 920 | // Since we're populating this with a static text string, disable auto-resize. |
| 921 | mDigits.setResizeEnabled(false); |
| 922 | Log.i(LOG_TAG, "hint - setting to " + mDigits.getScaledTextSize()); |
| 923 | } |
| 924 | } |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 925 | |
| 926 | private void setupEmergencyShortcutsView() { |
| 927 | mEmergencyShortcutView = findViewById(R.id.emergency_dialer_shortcuts); |
| 928 | mDialpadView = findViewById(R.id.emergency_dialer); |
| 929 | |
| 930 | final View dialpadButton = findViewById(R.id.floating_action_button_dialpad); |
| 931 | dialpadButton.setOnClickListener(this); |
| 932 | |
| 933 | final View emergencyInfoButton = findViewById(R.id.emergency_info_button); |
| 934 | emergencyInfoButton.setOnClickListener(this); |
| 935 | |
| 936 | // EmergencyActionGroup is replaced by EmergencyInfoGroup. |
| 937 | mEmergencyActionGroup.setVisibility(View.GONE); |
| 938 | |
| 939 | // Setup dialpad title. |
| 940 | final View emergencyDialpadTitle = findViewById(R.id.emergency_dialpad_title_container); |
| 941 | emergencyDialpadTitle.setVisibility(View.VISIBLE); |
| 942 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 943 | mEmergencyShortcutButtonList = new ArrayList<>(); |
| 944 | setupEmergencyCallShortcutButton(); |
| 945 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 946 | updateLocationAndEccInfo(null, null); |
| 947 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 948 | switchView(mEmergencyShortcutView, mDialpadView, false); |
| 949 | } |
| 950 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 951 | private void setLocationInfo(String countryIso) { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 952 | final View locationInfo = findViewById(R.id.location_info); |
| 953 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 954 | String countryName = null; |
| 955 | if (!TextUtils.isEmpty(countryIso)) { |
| 956 | Locale locale = Locale.getDefault(); |
| 957 | countryName = new Locale(locale.getLanguage(), countryIso, locale.getVariant()) |
| 958 | .getDisplayCountry(); |
| 959 | } |
| 960 | if (TextUtils.isEmpty(countryName)) { |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 961 | locationInfo.setVisibility(View.INVISIBLE); |
| 962 | } else { |
| 963 | final TextView location = (TextView) locationInfo.findViewById(R.id.location_text); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 964 | location.setText(countryName); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 965 | locationInfo.setVisibility(View.VISIBLE); |
| 966 | } |
| 967 | } |
| 968 | |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 969 | private void setupEmergencyCallShortcutButton() { |
| 970 | final ViewGroup shortcutButtonContainer = findViewById( |
| 971 | R.id.emergency_shortcut_buttons_container); |
| 972 | shortcutButtonContainer.setClipToOutline(true); |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 973 | final TextView emergencyNumberTitle = findViewById(R.id.emergency_number_title); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 974 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 975 | mShortcutAdapter = new EccShortcutAdapter(this) { |
| 976 | @Override |
| 977 | public View inflateView(View convertView, ViewGroup parent, CharSequence number, |
| 978 | CharSequence description, int iconRes) { |
| 979 | EmergencyShortcutButton button = (EmergencyShortcutButton) getLayoutInflater() |
| 980 | .inflate(R.layout.emergency_shortcut_button, parent, false); |
| 981 | button.setPhoneNumber(number); |
| 982 | button.setPhoneDescription(description); |
| 983 | button.setPhoneTypeIcon(iconRes); |
| 984 | button.setOnConfirmClickListener(EmergencyDialer.this); |
| 985 | return button; |
| 986 | } |
| 987 | }; |
| 988 | mShortcutDataSetObserver = new DataSetObserver() { |
| 989 | @Override |
| 990 | public void onChanged() { |
| 991 | super.onChanged(); |
| 992 | updateLayout(); |
| 993 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 994 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 995 | @Override |
| 996 | public void onInvalidated() { |
| 997 | super.onInvalidated(); |
| 998 | updateLayout(); |
| 999 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1000 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1001 | private void updateLayout() { |
| 1002 | // clear previous added buttons |
| 1003 | shortcutButtonContainer.removeAllViews(); |
| 1004 | mEmergencyShortcutButtonList.clear(); |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1005 | |
CY Cheng | 9a69c18 | 2018-06-15 21:20:10 +0800 | [diff] [blame] | 1006 | for (int i = 0; i < mShortcutAdapter.getCount() && i < SHORTCUT_SIZE_LIMIT; ++i) { |
| 1007 | EmergencyShortcutButton button = (EmergencyShortcutButton) |
| 1008 | mShortcutAdapter.getView(i, null, shortcutButtonContainer); |
| 1009 | mEmergencyShortcutButtonList.add(button); |
| 1010 | shortcutButtonContainer.addView(button); |
| 1011 | } |
| 1012 | |
| 1013 | // update emergency numbers title for numerous buttons. |
| 1014 | if (mEmergencyShortcutButtonList.size() > 1) { |
| 1015 | emergencyNumberTitle.setText(getString( |
| 1016 | R.string.numerous_emergency_numbers_title)); |
| 1017 | } else { |
| 1018 | emergencyNumberTitle.setText(getText(R.string.single_emergency_number_title)); |
| 1019 | } |
| 1020 | } |
| 1021 | }; |
| 1022 | mShortcutAdapter.registerDataSetObserver(mShortcutDataSetObserver); |
| 1023 | } |
| 1024 | |
| 1025 | private void updateLocationAndEccInfo(String iso, CountryEccInfo countryEccInfo) { |
| 1026 | if (!isFinishing() && !isDestroyed()) { |
| 1027 | setLocationInfo(iso); |
| 1028 | if (mShortcutAdapter != null) { |
| 1029 | mShortcutAdapter.updateCountryEccInfo(this, countryEccInfo); |
| 1030 | } |
Chihhang Chuang | 92cfe51 | 2018-06-07 16:25:27 +0800 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Called by the activity before a touch event is dispatched to the view hierarchy. |
| 1036 | */ |
| 1037 | private void onPreTouchEvent(MotionEvent event) { |
| 1038 | mEmergencyActionGroup.onPreTouchEvent(event); |
| 1039 | |
| 1040 | if (mEmergencyShortcutButtonList != null) { |
| 1041 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 1042 | button.onPreTouchEvent(event); |
| 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Called by the activity after a touch event is dispatched to the view hierarchy. |
| 1049 | */ |
| 1050 | private void onPostTouchEvent(MotionEvent event) { |
| 1051 | mEmergencyActionGroup.onPostTouchEvent(event); |
| 1052 | |
| 1053 | if (mEmergencyShortcutButtonList != null) { |
| 1054 | for (EmergencyShortcutButton button : mEmergencyShortcutButtonList) { |
| 1055 | button.onPostTouchEvent(event); |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
Chihhang Chuang | f264cfb | 2018-06-05 15:29:06 +0800 | [diff] [blame] | 1060 | /** |
| 1061 | * Switch two view. |
| 1062 | * |
| 1063 | * @param displayView the view would be displayed. |
| 1064 | * @param hideView the view would be hidden. |
| 1065 | * @param hasAnimation is {@code true} when the view should be displayed with animation. |
| 1066 | */ |
| 1067 | private void switchView(View displayView, View hideView, boolean hasAnimation) { |
| 1068 | if (displayView == null || hideView == null) { |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | if (displayView.getVisibility() == View.VISIBLE) { |
| 1073 | return; |
| 1074 | } |
| 1075 | |
| 1076 | if (hasAnimation) { |
| 1077 | crossfade(hideView, displayView); |
| 1078 | } else { |
| 1079 | hideView.setVisibility(View.GONE); |
| 1080 | displayView.setVisibility(View.VISIBLE); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * Fade out and fade in animation between two view transition. |
| 1086 | */ |
| 1087 | private void crossfade(View fadeOutView, View fadeInView) { |
| 1088 | if (fadeOutView == null || fadeInView == null) { |
| 1089 | return; |
| 1090 | } |
| 1091 | final int shortAnimationDuration = getResources().getInteger( |
| 1092 | android.R.integer.config_shortAnimTime); |
| 1093 | |
| 1094 | fadeInView.setAlpha(0f); |
| 1095 | fadeInView.setVisibility(View.VISIBLE); |
| 1096 | |
| 1097 | fadeInView.animate() |
| 1098 | .alpha(1f) |
| 1099 | .setDuration(shortAnimationDuration) |
| 1100 | .setListener(null); |
| 1101 | |
| 1102 | fadeOutView.animate() |
| 1103 | .alpha(0f) |
| 1104 | .setDuration(shortAnimationDuration) |
| 1105 | .setListener(new AnimatorListenerAdapter() { |
| 1106 | @Override |
| 1107 | public void onAnimationEnd(Animator animation) { |
| 1108 | fadeOutView.setVisibility(View.GONE); |
| 1109 | } |
| 1110 | }); |
| 1111 | } |
Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 1112 | } |