The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [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 | |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 17 | package com.android.server.policy; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 18 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 19 | import com.android.internal.app.AlertController; |
| 20 | import com.android.internal.app.AlertController.AlertParams; |
Felipe Leme | d426d12b | 2016-02-23 10:28:55 -0800 | [diff] [blame] | 21 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 22 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Selim Cinek | 705442f | 2016-09-13 16:02:33 -0700 | [diff] [blame] | 23 | import com.android.internal.policy.EmergencyAffordanceManager; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 24 | import com.android.internal.telephony.TelephonyIntents; |
| 25 | import com.android.internal.telephony.TelephonyProperties; |
| 26 | import com.android.internal.R; |
Adrian Roos | d4d6136 | 2014-06-25 23:30:50 +0200 | [diff] [blame] | 27 | import com.android.internal.widget.LockPatternUtils; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 28 | |
Guang Zhu | 98a8814 | 2014-10-03 12:23:43 -0700 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 30 | import android.app.Dialog; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 31 | import android.content.BroadcastReceiver; |
| 32 | import android.content.Context; |
| 33 | import android.content.DialogInterface; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 36 | import android.content.pm.UserInfo; |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 37 | import android.database.ContentObserver; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 38 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 39 | import android.media.AudioManager; |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 40 | import android.net.ConnectivityManager; |
John Spurlock | 887aa00 | 2014-06-02 14:10:41 -0400 | [diff] [blame] | 41 | import android.os.Build; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 42 | import android.os.Bundle; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 43 | import android.os.Handler; |
| 44 | import android.os.Message; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 45 | import android.os.RemoteException; |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 46 | import android.os.ServiceManager; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 47 | import android.os.SystemClock; |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 48 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 49 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 50 | import android.os.UserManager; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 51 | import android.os.Vibrator; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 52 | import android.provider.Settings; |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 53 | import android.service.dreams.DreamService; |
| 54 | import android.service.dreams.IDreamManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 55 | import android.telephony.PhoneStateListener; |
| 56 | import android.telephony.ServiceState; |
| 57 | import android.telephony.TelephonyManager; |
John Spurlock | 887aa00 | 2014-06-02 14:10:41 -0400 | [diff] [blame] | 58 | import android.text.TextUtils; |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 59 | import android.util.ArraySet; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 60 | import android.util.Log; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 61 | import android.util.TypedValue; |
| 62 | import android.view.InputDevice; |
| 63 | import android.view.KeyEvent; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 64 | import android.view.LayoutInflater; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 65 | import android.view.MotionEvent; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 66 | import android.view.View; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 67 | import android.view.ViewConfiguration; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 68 | import android.view.ViewGroup; |
| 69 | import android.view.WindowManager; |
Adrian Roos | d4d6136 | 2014-06-25 23:30:50 +0200 | [diff] [blame] | 70 | import android.view.WindowManagerGlobal; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 71 | import android.view.WindowManagerPolicy.WindowManagerFuncs; |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 72 | import android.view.accessibility.AccessibilityEvent; |
Dianne Hackborn | 19caadc | 2012-04-20 17:49:10 -0700 | [diff] [blame] | 73 | import android.widget.AdapterView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 74 | import android.widget.BaseAdapter; |
| 75 | import android.widget.ImageView; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 76 | import android.widget.ImageView.ScaleType; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 77 | import android.widget.ListView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 78 | import android.widget.TextView; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 79 | |
| 80 | import java.util.ArrayList; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 81 | import java.util.List; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * Helper to show the global actions dialog. Each item is an {@link Action} that |
| 85 | * may show depending on whether the keyguard is showing, and whether the device |
| 86 | * is provisioned. |
| 87 | */ |
| 88 | class GlobalActions implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener { |
| 89 | |
| 90 | private static final String TAG = "GlobalActions"; |
| 91 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 92 | private static final boolean SHOW_SILENT_TOGGLE = true; |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 93 | |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 94 | /* Valid settings for global actions keys. |
| 95 | * see config.xml config_globalActionList */ |
| 96 | private static final String GLOBAL_ACTION_KEY_POWER = "power"; |
| 97 | private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane"; |
| 98 | private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport"; |
| 99 | private static final String GLOBAL_ACTION_KEY_SILENT = "silent"; |
| 100 | private static final String GLOBAL_ACTION_KEY_USERS = "users"; |
| 101 | private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings"; |
Adrian Roos | d4d6136 | 2014-06-25 23:30:50 +0200 | [diff] [blame] | 102 | private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown"; |
Bryce Lee | cc8b467 | 2015-02-05 18:39:01 -0800 | [diff] [blame] | 103 | private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist"; |
Bryce Lee | d8379b9 | 2015-03-19 00:32:17 +0000 | [diff] [blame] | 104 | private static final String GLOBAL_ACTION_KEY_ASSIST = "assist"; |
Alison Cichowlas | f8eca40 | 2016-08-05 14:50:27 -0400 | [diff] [blame] | 105 | private static final String GLOBAL_ACTION_KEY_RESTART = "restart"; |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 106 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 107 | private final Context mContext; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 108 | private final WindowManagerFuncs mWindowManagerFuncs; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 109 | private final AudioManager mAudioManager; |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 110 | private final IDreamManager mDreamManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 111 | |
| 112 | private ArrayList<Action> mItems; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 113 | private GlobalActionsDialog mDialog; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 114 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 115 | private Action mSilentModeAction; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 116 | private ToggleAction mAirplaneModeOn; |
| 117 | |
| 118 | private MyAdapter mAdapter; |
| 119 | |
| 120 | private boolean mKeyguardShowing = false; |
| 121 | private boolean mDeviceProvisioned = false; |
| 122 | private ToggleAction.State mAirplaneState = ToggleAction.State.Off; |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 123 | private boolean mIsWaitingForEcmExit = false; |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 124 | private boolean mHasTelephony; |
| 125 | private boolean mHasVibrator; |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 126 | private final boolean mShowSilentToggle; |
Selim Cinek | 705442f | 2016-09-13 16:02:33 -0700 | [diff] [blame] | 127 | private final EmergencyAffordanceManager mEmergencyAffordanceManager; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @param context everything needs a context :( |
| 131 | */ |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 132 | public GlobalActions(Context context, WindowManagerFuncs windowManagerFuncs) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 133 | mContext = context; |
Jeff Brown | 7304c34 | 2012-05-11 18:42:42 -0700 | [diff] [blame] | 134 | mWindowManagerFuncs = windowManagerFuncs; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 135 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 136 | mDreamManager = IDreamManager.Stub.asInterface( |
| 137 | ServiceManager.getService(DreamService.DREAM_SERVICE)); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 138 | |
| 139 | // receive broadcasts |
| 140 | IntentFilter filter = new IntentFilter(); |
| 141 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
Joe Onorato | 78866b1 | 2009-07-17 13:09:27 -0400 | [diff] [blame] | 142 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 143 | filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 144 | context.registerReceiver(mBroadcastReceiver, filter); |
| 145 | |
nagarw | c3b7e6f | 2014-02-20 17:10:46 +0530 | [diff] [blame] | 146 | ConnectivityManager cm = (ConnectivityManager) |
| 147 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 148 | mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
| 149 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 150 | // get notified of phone state changes |
| 151 | TelephonyManager telephonyManager = |
| 152 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 153 | telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 154 | mContext.getContentResolver().registerContentObserver( |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 155 | Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true, |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 156 | mAirplaneModeObserver); |
| 157 | Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 158 | mHasVibrator = vibrator != null && vibrator.hasVibrator(); |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 159 | |
| 160 | mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean( |
| 161 | com.android.internal.R.bool.config_useFixedVolume); |
Selim Cinek | 705442f | 2016-09-13 16:02:33 -0700 | [diff] [blame] | 162 | |
| 163 | mEmergencyAffordanceManager = new EmergencyAffordanceManager(context); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Show the global actions dialog (creating if necessary) |
| 168 | * @param keyguardShowing True if keyguard is showing |
| 169 | */ |
| 170 | public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) { |
| 171 | mKeyguardShowing = keyguardShowing; |
| 172 | mDeviceProvisioned = isDeviceProvisioned; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 173 | if (mDialog != null) { |
| 174 | mDialog.dismiss(); |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 175 | mDialog = null; |
| 176 | // Show delayed, so that the dismiss of the previous dialog completes |
| 177 | mHandler.sendEmptyMessage(MESSAGE_SHOW); |
| 178 | } else { |
| 179 | handleShow(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 180 | } |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 181 | } |
| 182 | |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 183 | private void awakenIfNecessary() { |
| 184 | if (mDreamManager != null) { |
| 185 | try { |
| 186 | if (mDreamManager.isDreaming()) { |
| 187 | mDreamManager.awaken(); |
| 188 | } |
| 189 | } catch (RemoteException e) { |
| 190 | // we tried |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 195 | private void handleShow() { |
John Spurlock | 542a51c | 2012-10-03 16:54:23 -0400 | [diff] [blame] | 196 | awakenIfNecessary(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 197 | mDialog = createDialog(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 198 | prepareDialog(); |
| 199 | |
Justin Koh | 1c46c2d78 | 2014-05-12 16:00:36 -0700 | [diff] [blame] | 200 | // If we only have 1 item and it's a simple press action, just do this action. |
| 201 | if (mAdapter.getCount() == 1 |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 202 | && mAdapter.getItem(0) instanceof SinglePressAction |
| 203 | && !(mAdapter.getItem(0) instanceof LongPressAction)) { |
Justin Koh | 1c46c2d78 | 2014-05-12 16:00:36 -0700 | [diff] [blame] | 204 | ((SinglePressAction) mAdapter.getItem(0)).onPress(); |
| 205 | } else { |
| 206 | WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes(); |
| 207 | attrs.setTitle("GlobalActions"); |
| 208 | mDialog.getWindow().setAttributes(attrs); |
| 209 | mDialog.show(); |
| 210 | mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND); |
| 211 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 212 | } |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 213 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Create the global actions dialog. |
| 216 | * @return A new dialog. |
| 217 | */ |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 218 | private GlobalActionsDialog createDialog() { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 219 | // Simple toggle style if there's no vibrator, otherwise use a tri-state |
| 220 | if (!mHasVibrator) { |
| 221 | mSilentModeAction = new SilentModeToggleAction(); |
| 222 | } else { |
| 223 | mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler); |
| 224 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 225 | mAirplaneModeOn = new ToggleAction( |
| 226 | R.drawable.ic_lock_airplane_mode, |
| 227 | R.drawable.ic_lock_airplane_mode_off, |
| 228 | R.string.global_actions_toggle_airplane_mode, |
| 229 | R.string.global_actions_airplane_mode_on_status, |
| 230 | R.string.global_actions_airplane_mode_off_status) { |
| 231 | |
| 232 | void onToggle(boolean on) { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 233 | if (mHasTelephony && Boolean.parseBoolean( |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 234 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
| 235 | mIsWaitingForEcmExit = true; |
| 236 | // Launch ECM exit dialog |
| 237 | Intent ecmDialogIntent = |
| 238 | new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null); |
| 239 | ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 240 | mContext.startActivity(ecmDialogIntent); |
| 241 | } else { |
| 242 | changeAirplaneModeSystemSetting(on); |
| 243 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | @Override |
| 247 | protected void changeStateFromPress(boolean buttonOn) { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 248 | if (!mHasTelephony) return; |
| 249 | |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 250 | // In ECM mode airplane state cannot be changed |
| 251 | if (!(Boolean.parseBoolean( |
| 252 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) { |
| 253 | mState = buttonOn ? State.TurningOn : State.TurningOff; |
| 254 | mAirplaneState = mState; |
| 255 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | public boolean showDuringKeyguard() { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | public boolean showBeforeProvisioning() { |
| 263 | return false; |
| 264 | } |
| 265 | }; |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 266 | onAirplaneModeChanged(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 267 | |
Amith Yamasani | 2ef6f1b | 2011-12-01 14:01:30 -0800 | [diff] [blame] | 268 | mItems = new ArrayList<Action>(); |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 269 | String[] defaultActions = mContext.getResources().getStringArray( |
| 270 | com.android.internal.R.array.config_globalActionsList); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 271 | |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 272 | ArraySet<String> addedKeys = new ArraySet<String>(); |
| 273 | for (int i = 0; i < defaultActions.length; i++) { |
| 274 | String actionKey = defaultActions[i]; |
| 275 | if (addedKeys.contains(actionKey)) { |
| 276 | // If we already have added this, don't add it again. |
| 277 | continue; |
| 278 | } |
| 279 | if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) { |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 280 | mItems.add(new PowerAction()); |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 281 | } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) { |
| 282 | mItems.add(mAirplaneModeOn); |
John Spurlock | 68539a6 | 2014-08-20 10:36:33 -0400 | [diff] [blame] | 283 | } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) { |
| 284 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 285 | Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) { |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 286 | mItems.add(new BugReportAction()); |
John Spurlock | 68539a6 | 2014-08-20 10:36:33 -0400 | [diff] [blame] | 287 | } |
| 288 | } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) { |
| 289 | if (mShowSilentToggle) { |
| 290 | mItems.add(mSilentModeAction); |
| 291 | } |
| 292 | } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) { |
| 293 | if (SystemProperties.getBoolean("fw.power_user_switcher", false)) { |
| 294 | addUsersToMenu(mItems); |
| 295 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 296 | } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) { |
| 297 | mItems.add(getSettingsAction()); |
Adrian Roos | 4ebcdfd | 2014-08-12 23:33:49 +0200 | [diff] [blame] | 298 | } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) { |
Adrian Roos | d4d6136 | 2014-06-25 23:30:50 +0200 | [diff] [blame] | 299 | mItems.add(getLockdownAction()); |
Bryce Lee | cc8b467 | 2015-02-05 18:39:01 -0800 | [diff] [blame] | 300 | } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) { |
| 301 | mItems.add(getVoiceAssistAction()); |
Bryce Lee | d8379b9 | 2015-03-19 00:32:17 +0000 | [diff] [blame] | 302 | } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) { |
| 303 | mItems.add(getAssistAction()); |
Alison Cichowlas | f8eca40 | 2016-08-05 14:50:27 -0400 | [diff] [blame] | 304 | } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) { |
| 305 | mItems.add(new RestartAction()); |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 306 | } else { |
| 307 | Log.e(TAG, "Invalid global action key " + actionKey); |
| 308 | } |
| 309 | // Add here so we don't add more than one. |
| 310 | addedKeys.add(actionKey); |
Amith Yamasani | bc4ba82 | 2012-09-28 10:14:01 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Selim Cinek | 705442f | 2016-09-13 16:02:33 -0700 | [diff] [blame] | 313 | if (mEmergencyAffordanceManager.needsEmergencyAffordance()) { |
| 314 | mItems.add(getEmergencyAction()); |
| 315 | } |
| 316 | |
Amith Yamasani | bc4ba82 | 2012-09-28 10:14:01 -0700 | [diff] [blame] | 317 | mAdapter = new MyAdapter(); |
| 318 | |
| 319 | AlertParams params = new AlertParams(mContext); |
| 320 | params.mAdapter = mAdapter; |
| 321 | params.mOnClickListener = this; |
| 322 | params.mForceInverseBackground = true; |
| 323 | |
| 324 | GlobalActionsDialog dialog = new GlobalActionsDialog(mContext, params); |
| 325 | dialog.setCanceledOnTouchOutside(false); // Handled by the custom class. |
| 326 | |
| 327 | dialog.getListView().setItemsCanFocus(true); |
| 328 | dialog.getListView().setLongClickable(true); |
| 329 | dialog.getListView().setOnItemLongClickListener( |
| 330 | new AdapterView.OnItemLongClickListener() { |
| 331 | @Override |
| 332 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, |
| 333 | long id) { |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 334 | final Action action = mAdapter.getItem(position); |
| 335 | if (action instanceof LongPressAction) { |
| 336 | return ((LongPressAction) action).onLongPress(); |
| 337 | } |
| 338 | return false; |
Amith Yamasani | bc4ba82 | 2012-09-28 10:14:01 -0700 | [diff] [blame] | 339 | } |
| 340 | }); |
Dianne Hackborn | bef0f61 | 2012-10-16 18:34:48 -0700 | [diff] [blame] | 341 | dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
Amith Yamasani | bc4ba82 | 2012-09-28 10:14:01 -0700 | [diff] [blame] | 342 | |
| 343 | dialog.setOnDismissListener(this); |
| 344 | |
| 345 | return dialog; |
| 346 | } |
| 347 | |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 348 | private final class PowerAction extends SinglePressAction implements LongPressAction { |
| 349 | private PowerAction() { |
| 350 | super(com.android.internal.R.drawable.ic_lock_power_off, |
| 351 | R.string.global_action_power_off); |
| 352 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 353 | |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 354 | @Override |
| 355 | public boolean onLongPress() { |
Benjamin Franz | bff46ba | 2015-03-05 18:33:51 +0000 | [diff] [blame] | 356 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 357 | if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) { |
| 358 | mWindowManagerFuncs.rebootSafeMode(true); |
| 359 | return true; |
| 360 | } |
| 361 | return false; |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 362 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 363 | |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 364 | @Override |
| 365 | public boolean showDuringKeyguard() { |
| 366 | return true; |
| 367 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 368 | |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 369 | @Override |
| 370 | public boolean showBeforeProvisioning() { |
| 371 | return true; |
| 372 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 373 | |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 374 | @Override |
| 375 | public void onPress() { |
| 376 | // shutdown by making sure radio and power are handled accordingly. |
| 377 | mWindowManagerFuncs.shutdown(false /* confirm */); |
| 378 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Alison Cichowlas | f8eca40 | 2016-08-05 14:50:27 -0400 | [diff] [blame] | 381 | private final class RestartAction extends SinglePressAction implements LongPressAction { |
| 382 | private RestartAction() { |
| 383 | super(R.drawable.ic_restart, R.string.global_action_restart); |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public boolean onLongPress() { |
| 388 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 389 | if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) { |
| 390 | mWindowManagerFuncs.rebootSafeMode(true); |
| 391 | return true; |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public boolean showDuringKeyguard() { |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public boolean showBeforeProvisioning() { |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public void onPress() { |
| 408 | mWindowManagerFuncs.reboot(false /* confirm */); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 413 | private class BugReportAction extends SinglePressAction implements LongPressAction { |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 414 | |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 415 | public BugReportAction() { |
| 416 | super(com.android.internal.R.drawable.ic_lock_bugreport, R.string.bugreport_title); |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void onPress() { |
| 421 | // don't actually trigger the bugreport if we are running stability |
| 422 | // tests via monkey |
| 423 | if (ActivityManager.isUserAMonkey()) { |
| 424 | return; |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 425 | } |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 426 | // Add a little delay before executing, to give the |
| 427 | // dialog a chance to go away before it takes a |
| 428 | // screenshot. |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 429 | mHandler.postDelayed(new Runnable() { |
| 430 | @Override |
| 431 | public void run() { |
| 432 | try { |
Felipe Leme | 43b013f | 2015-12-22 14:09:25 -0800 | [diff] [blame] | 433 | // Take an "interactive" bugreport. |
Felipe Leme | d426d12b | 2016-02-23 10:28:55 -0800 | [diff] [blame] | 434 | MetricsLogger.action(mContext, |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 435 | MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 436 | ActivityManager.getService().requestBugReport( |
Michal Karpinski | 3da5c97 | 2015-12-11 18:16:30 +0000 | [diff] [blame] | 437 | ActivityManager.BUGREPORT_OPTION_INTERACTIVE); |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 438 | } catch (RemoteException e) { |
| 439 | } |
| 440 | } |
| 441 | }, 500); |
| 442 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 443 | |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 444 | @Override |
| 445 | public boolean onLongPress() { |
| 446 | // don't actually trigger the bugreport if we are running stability |
| 447 | // tests via monkey |
| 448 | if (ActivityManager.isUserAMonkey()) { |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 451 | try { |
Felipe Leme | 43b013f | 2015-12-22 14:09:25 -0800 | [diff] [blame] | 452 | // Take a "full" bugreport. |
Felipe Leme | d426d12b | 2016-02-23 10:28:55 -0800 | [diff] [blame] | 453 | MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL); |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 454 | ActivityManager.getService().requestBugReport( |
Michal Karpinski | 3da5c97 | 2015-12-11 18:16:30 +0000 | [diff] [blame] | 455 | ActivityManager.BUGREPORT_OPTION_FULL); |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 456 | } catch (RemoteException e) { |
John Spurlock | 887aa00 | 2014-06-02 14:10:41 -0400 | [diff] [blame] | 457 | } |
Felipe Leme | 43b013f | 2015-12-22 14:09:25 -0800 | [diff] [blame] | 458 | return false; |
Felipe Leme | e974f7e | 2015-12-15 14:15:56 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | public boolean showDuringKeyguard() { |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public boolean showBeforeProvisioning() { |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public String getStatus() { |
| 472 | return mContext.getString( |
| 473 | com.android.internal.R.string.bugreport_status, |
| 474 | Build.VERSION.RELEASE, |
| 475 | Build.ID); |
| 476 | } |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | private Action getSettingsAction() { |
| 480 | return new SinglePressAction(com.android.internal.R.drawable.ic_settings, |
| 481 | R.string.global_action_settings) { |
| 482 | |
| 483 | @Override |
| 484 | public void onPress() { |
| 485 | Intent intent = new Intent(Settings.ACTION_SETTINGS); |
Wei Liu | 636f3ea | 2014-09-03 22:10:57 -0700 | [diff] [blame] | 486 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 487 | mContext.startActivity(intent); |
| 488 | } |
| 489 | |
| 490 | @Override |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 491 | public boolean showDuringKeyguard() { |
| 492 | return true; |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public boolean showBeforeProvisioning() { |
Justin Koh | 1c46c2d78 | 2014-05-12 16:00:36 -0700 | [diff] [blame] | 497 | return true; |
Justin Koh | 3b9a3cc | 2014-04-21 17:50:40 -0700 | [diff] [blame] | 498 | } |
| 499 | }; |
| 500 | } |
| 501 | |
Selim Cinek | 705442f | 2016-09-13 16:02:33 -0700 | [diff] [blame] | 502 | private Action getEmergencyAction() { |
| 503 | return new SinglePressAction(com.android.internal.R.drawable.emergency_icon, |
| 504 | R.string.global_action_emergency) { |
| 505 | @Override |
| 506 | public void onPress() { |
| 507 | mEmergencyAffordanceManager.performEmergencyCall(); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public boolean showDuringKeyguard() { |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public boolean showBeforeProvisioning() { |
| 517 | return true; |
| 518 | } |
| 519 | }; |
| 520 | } |
| 521 | |
Bryce Lee | d8379b9 | 2015-03-19 00:32:17 +0000 | [diff] [blame] | 522 | private Action getAssistAction() { |
| 523 | return new SinglePressAction(com.android.internal.R.drawable.ic_action_assist_focused, |
| 524 | R.string.global_action_assist) { |
| 525 | @Override |
| 526 | public void onPress() { |
| 527 | Intent intent = new Intent(Intent.ACTION_ASSIST); |
| 528 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 529 | mContext.startActivity(intent); |
| 530 | } |
| 531 | |
| 532 | @Override |
| 533 | public boolean showDuringKeyguard() { |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public boolean showBeforeProvisioning() { |
| 539 | return true; |
| 540 | } |
| 541 | }; |
| 542 | } |
| 543 | |
Bryce Lee | cc8b467 | 2015-02-05 18:39:01 -0800 | [diff] [blame] | 544 | private Action getVoiceAssistAction() { |
| 545 | return new SinglePressAction(com.android.internal.R.drawable.ic_voice_search, |
| 546 | R.string.global_action_voice_assist) { |
| 547 | @Override |
| 548 | public void onPress() { |
| 549 | Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST); |
| 550 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 551 | mContext.startActivity(intent); |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public boolean showDuringKeyguard() { |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public boolean showBeforeProvisioning() { |
| 561 | return true; |
| 562 | } |
| 563 | }; |
| 564 | } |
| 565 | |
Adrian Roos | d4d6136 | 2014-06-25 23:30:50 +0200 | [diff] [blame] | 566 | private Action getLockdownAction() { |
| 567 | return new SinglePressAction(com.android.internal.R.drawable.ic_lock_lock, |
| 568 | R.string.global_action_lockdown) { |
| 569 | |
| 570 | @Override |
| 571 | public void onPress() { |
| 572 | new LockPatternUtils(mContext).requireCredentialEntry(UserHandle.USER_ALL); |
| 573 | try { |
| 574 | WindowManagerGlobal.getWindowManagerService().lockNow(null); |
| 575 | } catch (RemoteException e) { |
| 576 | Log.e(TAG, "Error while trying to lock device.", e); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public boolean showDuringKeyguard() { |
| 582 | return true; |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public boolean showBeforeProvisioning() { |
| 587 | return false; |
| 588 | } |
| 589 | }; |
| 590 | } |
| 591 | |
Amith Yamasani | 3df2172 | 2013-06-10 13:43:10 -0700 | [diff] [blame] | 592 | private UserInfo getCurrentUser() { |
| 593 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 594 | return ActivityManager.getService().getCurrentUser(); |
Amith Yamasani | 3df2172 | 2013-06-10 13:43:10 -0700 | [diff] [blame] | 595 | } catch (RemoteException re) { |
| 596 | return null; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | private boolean isCurrentUserOwner() { |
| 601 | UserInfo currentUser = getCurrentUser(); |
| 602 | return currentUser == null || currentUser.isPrimary(); |
| 603 | } |
| 604 | |
Amith Yamasani | bc4ba82 | 2012-09-28 10:14:01 -0700 | [diff] [blame] | 605 | private void addUsersToMenu(ArrayList<Action> items) { |
Kenny Guy | 1a44753 | 2014-02-20 21:55:32 +0000 | [diff] [blame] | 606 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 607 | if (um.isUserSwitcherEnabled()) { |
| 608 | List<UserInfo> users = um.getUsers(); |
Amith Yamasani | 3df2172 | 2013-06-10 13:43:10 -0700 | [diff] [blame] | 609 | UserInfo currentUser = getCurrentUser(); |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 610 | for (final UserInfo user : users) { |
Xiaohui Chen | 7cb69df | 2015-07-13 16:01:01 -0700 | [diff] [blame] | 611 | if (user.supportsSwitchToByUser()) { |
Kenny Guy | 1a44753 | 2014-02-20 21:55:32 +0000 | [diff] [blame] | 612 | boolean isCurrentUser = currentUser == null |
| 613 | ? user.id == 0 : (currentUser.id == user.id); |
| 614 | Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath) |
| 615 | : null; |
| 616 | SinglePressAction switchToUser = new SinglePressAction( |
| 617 | com.android.internal.R.drawable.ic_menu_cc, icon, |
| 618 | (user.name != null ? user.name : "Primary") |
| 619 | + (isCurrentUser ? " \u2714" : "")) { |
| 620 | public void onPress() { |
| 621 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 622 | ActivityManager.getService().switchUser(user.id); |
Kenny Guy | 1a44753 | 2014-02-20 21:55:32 +0000 | [diff] [blame] | 623 | } catch (RemoteException re) { |
| 624 | Log.e(TAG, "Couldn't switch user " + re); |
| 625 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 626 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 627 | |
Kenny Guy | 1a44753 | 2014-02-20 21:55:32 +0000 | [diff] [blame] | 628 | public boolean showDuringKeyguard() { |
| 629 | return true; |
| 630 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 631 | |
Kenny Guy | 1a44753 | 2014-02-20 21:55:32 +0000 | [diff] [blame] | 632 | public boolean showBeforeProvisioning() { |
| 633 | return false; |
| 634 | } |
| 635 | }; |
| 636 | items.add(switchToUser); |
| 637 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 638 | } |
| 639 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | private void prepareDialog() { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 643 | refreshSilentMode(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 644 | mAirplaneModeOn.updateState(mAirplaneState); |
| 645 | mAdapter.notifyDataSetChanged(); |
Dianne Hackborn | bef0f61 | 2012-10-16 18:34:48 -0700 | [diff] [blame] | 646 | mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 647 | if (mShowSilentToggle) { |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 648 | IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 649 | mContext.registerReceiver(mRingerModeReceiver, filter); |
| 650 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 653 | private void refreshSilentMode() { |
| 654 | if (!mHasVibrator) { |
| 655 | final boolean silentModeOn = |
| 656 | mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; |
| 657 | ((ToggleAction)mSilentModeAction).updateState( |
| 658 | silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off); |
| 659 | } |
| 660 | } |
| 661 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 662 | /** {@inheritDoc} */ |
| 663 | public void onDismiss(DialogInterface dialog) { |
Eric Laurent | 83a017b | 2013-03-19 18:15:31 -0700 | [diff] [blame] | 664 | if (mShowSilentToggle) { |
Masanori Ogino | 7d4adea | 2012-10-11 18:51:35 +0900 | [diff] [blame] | 665 | try { |
| 666 | mContext.unregisterReceiver(mRingerModeReceiver); |
| 667 | } catch (IllegalArgumentException ie) { |
| 668 | // ignore this |
| 669 | Log.w(TAG, ie); |
| 670 | } |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 671 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** {@inheritDoc} */ |
| 675 | public void onClick(DialogInterface dialog, int which) { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 676 | if (!(mAdapter.getItem(which) instanceof SilentModeTriStateAction)) { |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 677 | dialog.dismiss(); |
| 678 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 679 | mAdapter.getItem(which).onPress(); |
| 680 | } |
| 681 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 682 | /** |
| 683 | * The adapter used for the list within the global actions dialog, taking |
| 684 | * into account whether the keyguard is showing via |
| 685 | * {@link GlobalActions#mKeyguardShowing} and whether the device is provisioned |
| 686 | * via {@link GlobalActions#mDeviceProvisioned}. |
| 687 | */ |
| 688 | private class MyAdapter extends BaseAdapter { |
| 689 | |
| 690 | public int getCount() { |
| 691 | int count = 0; |
| 692 | |
| 693 | for (int i = 0; i < mItems.size(); i++) { |
| 694 | final Action action = mItems.get(i); |
| 695 | |
| 696 | if (mKeyguardShowing && !action.showDuringKeyguard()) { |
| 697 | continue; |
| 698 | } |
| 699 | if (!mDeviceProvisioned && !action.showBeforeProvisioning()) { |
| 700 | continue; |
| 701 | } |
| 702 | count++; |
| 703 | } |
| 704 | return count; |
| 705 | } |
| 706 | |
| 707 | @Override |
| 708 | public boolean isEnabled(int position) { |
| 709 | return getItem(position).isEnabled(); |
| 710 | } |
| 711 | |
| 712 | @Override |
| 713 | public boolean areAllItemsEnabled() { |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | public Action getItem(int position) { |
| 718 | |
| 719 | int filteredPos = 0; |
| 720 | for (int i = 0; i < mItems.size(); i++) { |
| 721 | final Action action = mItems.get(i); |
| 722 | if (mKeyguardShowing && !action.showDuringKeyguard()) { |
| 723 | continue; |
| 724 | } |
| 725 | if (!mDeviceProvisioned && !action.showBeforeProvisioning()) { |
| 726 | continue; |
| 727 | } |
| 728 | if (filteredPos == position) { |
| 729 | return action; |
| 730 | } |
| 731 | filteredPos++; |
| 732 | } |
| 733 | |
Bernhard Reutner-Fischer | c1a9b03 | 2011-04-28 20:06:44 +0200 | [diff] [blame] | 734 | throw new IllegalArgumentException("position " + position |
| 735 | + " out of range of showable actions" |
| 736 | + ", filtered count=" + getCount() |
| 737 | + ", keyguardshowing=" + mKeyguardShowing |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 738 | + ", provisioned=" + mDeviceProvisioned); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | public long getItemId(int position) { |
| 743 | return position; |
| 744 | } |
| 745 | |
| 746 | public View getView(int position, View convertView, ViewGroup parent) { |
| 747 | Action action = getItem(position); |
| 748 | return action.create(mContext, convertView, parent, LayoutInflater.from(mContext)); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | // note: the scheme below made more sense when we were planning on having |
| 753 | // 8 different things in the global actions dialog. seems overkill with |
| 754 | // only 3 items now, but may as well keep this flexible approach so it will |
| 755 | // be easy should someone decide at the last minute to include something |
| 756 | // else, such as 'enable wifi', or 'enable bluetooth' |
| 757 | |
| 758 | /** |
| 759 | * What each item in the global actions dialog must be able to support. |
| 760 | */ |
| 761 | private interface Action { |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 762 | /** |
| 763 | * @return Text that will be announced when dialog is created. null |
| 764 | * for none. |
| 765 | */ |
| 766 | CharSequence getLabelForAccessibility(Context context); |
| 767 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 768 | View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater); |
| 769 | |
| 770 | void onPress(); |
| 771 | |
| 772 | /** |
| 773 | * @return whether this action should appear in the dialog when the keygaurd |
| 774 | * is showing. |
| 775 | */ |
| 776 | boolean showDuringKeyguard(); |
| 777 | |
| 778 | /** |
| 779 | * @return whether this action should appear in the dialog before the |
| 780 | * device is provisioned. |
| 781 | */ |
| 782 | boolean showBeforeProvisioning(); |
| 783 | |
| 784 | boolean isEnabled(); |
| 785 | } |
| 786 | |
| 787 | /** |
John Spurlock | 822e5cc | 2014-05-30 17:48:54 -0400 | [diff] [blame] | 788 | * An action that also supports long press. |
| 789 | */ |
| 790 | private interface LongPressAction extends Action { |
| 791 | boolean onLongPress(); |
| 792 | } |
| 793 | |
| 794 | /** |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 795 | * A single press action maintains no state, just responds to a press |
| 796 | * and takes an action. |
| 797 | */ |
| 798 | private static abstract class SinglePressAction implements Action { |
| 799 | private final int mIconResId; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 800 | private final Drawable mIcon; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 801 | private final int mMessageResId; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 802 | private final CharSequence mMessage; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 803 | |
| 804 | protected SinglePressAction(int iconResId, int messageResId) { |
| 805 | mIconResId = iconResId; |
| 806 | mMessageResId = messageResId; |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 807 | mMessage = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 808 | mIcon = null; |
| 809 | } |
| 810 | |
| 811 | protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) { |
| 812 | mIconResId = iconResId; |
| 813 | mMessageResId = 0; |
| 814 | mMessage = message; |
| 815 | mIcon = icon; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | public boolean isEnabled() { |
| 819 | return true; |
| 820 | } |
| 821 | |
John Spurlock | 887aa00 | 2014-06-02 14:10:41 -0400 | [diff] [blame] | 822 | public String getStatus() { |
| 823 | return null; |
| 824 | } |
| 825 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 826 | abstract public void onPress(); |
| 827 | |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 828 | public CharSequence getLabelForAccessibility(Context context) { |
| 829 | if (mMessage != null) { |
| 830 | return mMessage; |
| 831 | } else { |
| 832 | return context.getString(mMessageResId); |
| 833 | } |
| 834 | } |
| 835 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 836 | public View create( |
| 837 | Context context, View convertView, ViewGroup parent, LayoutInflater inflater) { |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 838 | View v = inflater.inflate(R.layout.global_actions_item, parent, false); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 839 | |
| 840 | ImageView icon = (ImageView) v.findViewById(R.id.icon); |
| 841 | TextView messageView = (TextView) v.findViewById(R.id.message); |
| 842 | |
John Spurlock | 887aa00 | 2014-06-02 14:10:41 -0400 | [diff] [blame] | 843 | TextView statusView = (TextView) v.findViewById(R.id.status); |
| 844 | final String status = getStatus(); |
| 845 | if (!TextUtils.isEmpty(status)) { |
| 846 | statusView.setText(status); |
| 847 | } else { |
| 848 | statusView.setVisibility(View.GONE); |
| 849 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 850 | if (mIcon != null) { |
| 851 | icon.setImageDrawable(mIcon); |
| 852 | icon.setScaleType(ScaleType.CENTER_CROP); |
| 853 | } else if (mIconResId != 0) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 854 | icon.setImageDrawable(context.getDrawable(mIconResId)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 855 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 856 | if (mMessage != null) { |
| 857 | messageView.setText(mMessage); |
| 858 | } else { |
| 859 | messageView.setText(mMessageResId); |
| 860 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 861 | |
| 862 | return v; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * A toggle action knows whether it is on or off, and displays an icon |
| 868 | * and status message accordingly. |
| 869 | */ |
| 870 | private static abstract class ToggleAction implements Action { |
| 871 | |
| 872 | enum State { |
| 873 | Off(false), |
| 874 | TurningOn(true), |
| 875 | TurningOff(true), |
| 876 | On(false); |
| 877 | |
| 878 | private final boolean inTransition; |
| 879 | |
| 880 | State(boolean intermediate) { |
| 881 | inTransition = intermediate; |
| 882 | } |
| 883 | |
| 884 | public boolean inTransition() { |
| 885 | return inTransition; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | protected State mState = State.Off; |
| 890 | |
| 891 | // prefs |
Daniel Sandler | 82d6c68 | 2010-02-26 15:16:54 -0500 | [diff] [blame] | 892 | protected int mEnabledIconResId; |
| 893 | protected int mDisabledIconResid; |
| 894 | protected int mMessageResId; |
| 895 | protected int mEnabledStatusMessageResId; |
| 896 | protected int mDisabledStatusMessageResId; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 897 | |
| 898 | /** |
| 899 | * @param enabledIconResId The icon for when this action is on. |
| 900 | * @param disabledIconResid The icon for when this action is off. |
| 901 | * @param essage The general information message, e.g 'Silent Mode' |
| 902 | * @param enabledStatusMessageResId The on status message, e.g 'sound disabled' |
| 903 | * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled' |
| 904 | */ |
| 905 | public ToggleAction(int enabledIconResId, |
| 906 | int disabledIconResid, |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 907 | int message, |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 908 | int enabledStatusMessageResId, |
| 909 | int disabledStatusMessageResId) { |
| 910 | mEnabledIconResId = enabledIconResId; |
| 911 | mDisabledIconResid = disabledIconResid; |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 912 | mMessageResId = message; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 913 | mEnabledStatusMessageResId = enabledStatusMessageResId; |
| 914 | mDisabledStatusMessageResId = disabledStatusMessageResId; |
| 915 | } |
| 916 | |
Daniel Sandler | 82d6c68 | 2010-02-26 15:16:54 -0500 | [diff] [blame] | 917 | /** |
| 918 | * Override to make changes to resource IDs just before creating the |
| 919 | * View. |
| 920 | */ |
| 921 | void willCreate() { |
| 922 | |
| 923 | } |
| 924 | |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 925 | @Override |
| 926 | public CharSequence getLabelForAccessibility(Context context) { |
| 927 | return context.getString(mMessageResId); |
| 928 | } |
| 929 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 930 | public View create(Context context, View convertView, ViewGroup parent, |
| 931 | LayoutInflater inflater) { |
Daniel Sandler | 82d6c68 | 2010-02-26 15:16:54 -0500 | [diff] [blame] | 932 | willCreate(); |
| 933 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 934 | View v = inflater.inflate(R |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 935 | .layout.global_actions_item, parent, false); |
| 936 | |
| 937 | ImageView icon = (ImageView) v.findViewById(R.id.icon); |
| 938 | TextView messageView = (TextView) v.findViewById(R.id.message); |
| 939 | TextView statusView = (TextView) v.findViewById(R.id.status); |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 940 | final boolean enabled = isEnabled(); |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 941 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 942 | if (messageView != null) { |
| 943 | messageView.setText(mMessageResId); |
| 944 | messageView.setEnabled(enabled); |
| 945 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 946 | |
| 947 | boolean on = ((mState == State.On) || (mState == State.TurningOn)); |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 948 | if (icon != null) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 949 | icon.setImageDrawable(context.getDrawable( |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 950 | (on ? mEnabledIconResId : mDisabledIconResid))); |
| 951 | icon.setEnabled(enabled); |
| 952 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 953 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 954 | if (statusView != null) { |
| 955 | statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId); |
| 956 | statusView.setVisibility(View.VISIBLE); |
| 957 | statusView.setEnabled(enabled); |
| 958 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 959 | v.setEnabled(enabled); |
| 960 | |
| 961 | return v; |
| 962 | } |
| 963 | |
| 964 | public final void onPress() { |
| 965 | if (mState.inTransition()) { |
| 966 | Log.w(TAG, "shouldn't be able to toggle when in transition"); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | final boolean nowOn = !(mState == State.On); |
| 971 | onToggle(nowOn); |
| 972 | changeStateFromPress(nowOn); |
| 973 | } |
| 974 | |
| 975 | public boolean isEnabled() { |
| 976 | return !mState.inTransition(); |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Implementations may override this if their state can be in on of the intermediate |
| 981 | * states until some notification is received (e.g airplane mode is 'turning off' until |
| 982 | * we know the wireless connections are back online |
| 983 | * @param buttonOn Whether the button was turned on or off |
| 984 | */ |
| 985 | protected void changeStateFromPress(boolean buttonOn) { |
| 986 | mState = buttonOn ? State.On : State.Off; |
| 987 | } |
| 988 | |
| 989 | abstract void onToggle(boolean on); |
| 990 | |
| 991 | public void updateState(State state) { |
| 992 | mState = state; |
| 993 | } |
| 994 | } |
| 995 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 996 | private class SilentModeToggleAction extends ToggleAction { |
| 997 | public SilentModeToggleAction() { |
| 998 | super(R.drawable.ic_audio_vol_mute, |
| 999 | R.drawable.ic_audio_vol, |
| 1000 | R.string.global_action_toggle_silent_mode, |
| 1001 | R.string.global_action_silent_mode_on_status, |
| 1002 | R.string.global_action_silent_mode_off_status); |
| 1003 | } |
| 1004 | |
| 1005 | void onToggle(boolean on) { |
| 1006 | if (on) { |
| 1007 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); |
| 1008 | } else { |
| 1009 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | public boolean showDuringKeyguard() { |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | public boolean showBeforeProvisioning() { |
| 1018 | return false; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | private static class SilentModeTriStateAction implements Action, View.OnClickListener { |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1023 | |
| 1024 | private final int[] ITEM_IDS = { R.id.option1, R.id.option2, R.id.option3 }; |
| 1025 | |
| 1026 | private final AudioManager mAudioManager; |
| 1027 | private final Handler mHandler; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1028 | private final Context mContext; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1029 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1030 | SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) { |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1031 | mAudioManager = audioManager; |
| 1032 | mHandler = handler; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1033 | mContext = context; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | private int ringerModeToIndex(int ringerMode) { |
| 1037 | // They just happen to coincide |
| 1038 | return ringerMode; |
| 1039 | } |
| 1040 | |
| 1041 | private int indexToRingerMode(int index) { |
| 1042 | // They just happen to coincide |
| 1043 | return index; |
| 1044 | } |
| 1045 | |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 1046 | @Override |
| 1047 | public CharSequence getLabelForAccessibility(Context context) { |
| 1048 | return null; |
| 1049 | } |
| 1050 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1051 | public View create(Context context, View convertView, ViewGroup parent, |
| 1052 | LayoutInflater inflater) { |
| 1053 | View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false); |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1054 | |
| 1055 | int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode()); |
| 1056 | for (int i = 0; i < 3; i++) { |
| 1057 | View itemView = v.findViewById(ITEM_IDS[i]); |
| 1058 | itemView.setSelected(selectedIndex == i); |
| 1059 | // Set up click handler |
| 1060 | itemView.setTag(i); |
| 1061 | itemView.setOnClickListener(this); |
| 1062 | } |
| 1063 | return v; |
| 1064 | } |
| 1065 | |
| 1066 | public void onPress() { |
| 1067 | } |
| 1068 | |
| 1069 | public boolean showDuringKeyguard() { |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | public boolean showBeforeProvisioning() { |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | public boolean isEnabled() { |
| 1078 | return true; |
| 1079 | } |
| 1080 | |
| 1081 | void willCreate() { |
| 1082 | } |
| 1083 | |
| 1084 | public void onClick(View v) { |
| 1085 | if (!(v.getTag() instanceof Integer)) return; |
| 1086 | |
| 1087 | int index = (Integer) v.getTag(); |
| 1088 | mAudioManager.setRingerMode(indexToRingerMode(index)); |
| 1089 | mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY); |
| 1090 | } |
| 1091 | } |
| 1092 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1093 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1094 | public void onReceive(Context context, Intent intent) { |
| 1095 | String action = intent.getAction(); |
Joe Onorato | 78866b1 | 2009-07-17 13:09:27 -0400 | [diff] [blame] | 1096 | if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action) |
| 1097 | || Intent.ACTION_SCREEN_OFF.equals(action)) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1098 | String reason = intent.getStringExtra(PhoneWindowManager.SYSTEM_DIALOG_REASON_KEY); |
Joe Onorato | 78866b1 | 2009-07-17 13:09:27 -0400 | [diff] [blame] | 1099 | if (!PhoneWindowManager.SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) { |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1100 | mHandler.sendEmptyMessage(MESSAGE_DISMISS); |
| 1101 | } |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1102 | } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) { |
| 1103 | // Airplane mode can be changed after ECM exits if airplane toggle button |
| 1104 | // is pressed during ECM mode |
| 1105 | if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) && |
| 1106 | mIsWaitingForEcmExit) { |
| 1107 | mIsWaitingForEcmExit = false; |
| 1108 | changeAirplaneModeSystemSetting(true); |
| 1109 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | }; |
| 1113 | |
| 1114 | PhoneStateListener mPhoneStateListener = new PhoneStateListener() { |
| 1115 | @Override |
| 1116 | public void onServiceStateChanged(ServiceState serviceState) { |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1117 | if (!mHasTelephony) return; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1118 | final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF; |
| 1119 | mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1120 | mAirplaneModeOn.updateState(mAirplaneState); |
| 1121 | mAdapter.notifyDataSetChanged(); |
| 1122 | } |
| 1123 | }; |
| 1124 | |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1125 | private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() { |
| 1126 | @Override |
| 1127 | public void onReceive(Context context, Intent intent) { |
| 1128 | if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { |
| 1129 | mHandler.sendEmptyMessage(MESSAGE_REFRESH); |
| 1130 | } |
| 1131 | } |
| 1132 | }; |
| 1133 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1134 | private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { |
| 1135 | @Override |
| 1136 | public void onChange(boolean selfChange) { |
| 1137 | onAirplaneModeChanged(); |
| 1138 | } |
| 1139 | }; |
| 1140 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1141 | private static final int MESSAGE_DISMISS = 0; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1142 | private static final int MESSAGE_REFRESH = 1; |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 1143 | private static final int MESSAGE_SHOW = 2; |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1144 | private static final int DIALOG_DISMISS_DELAY = 300; // ms |
| 1145 | |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1146 | private Handler mHandler = new Handler() { |
| 1147 | public void handleMessage(Message msg) { |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 1148 | switch (msg.what) { |
| 1149 | case MESSAGE_DISMISS: |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1150 | if (mDialog != null) { |
| 1151 | mDialog.dismiss(); |
Henrik Baard | ae15594 | 2013-07-23 09:31:57 +0200 | [diff] [blame] | 1152 | mDialog = null; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1153 | } |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 1154 | break; |
| 1155 | case MESSAGE_REFRESH: |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1156 | refreshSilentMode(); |
Amith Yamasani | 6243edd | 2011-12-05 19:58:48 -0800 | [diff] [blame] | 1157 | mAdapter.notifyDataSetChanged(); |
Amith Yamasani | 2efb6a9 | 2012-04-25 17:31:03 -0700 | [diff] [blame] | 1158 | break; |
| 1159 | case MESSAGE_SHOW: |
| 1160 | handleShow(); |
| 1161 | break; |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | }; |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1165 | |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1166 | private void onAirplaneModeChanged() { |
| 1167 | // Let the service state callbacks handle the state. |
| 1168 | if (mHasTelephony) return; |
| 1169 | |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 1170 | boolean airplaneModeOn = Settings.Global.getInt( |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1171 | mContext.getContentResolver(), |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 1172 | Settings.Global.AIRPLANE_MODE_ON, |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1173 | 0) == 1; |
| 1174 | mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1175 | mAirplaneModeOn.updateState(mAirplaneState); |
| 1176 | } |
| 1177 | |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1178 | /** |
| 1179 | * Change the airplane mode system setting |
| 1180 | */ |
| 1181 | private void changeAirplaneModeSystemSetting(boolean on) { |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 1182 | Settings.Global.putInt( |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1183 | mContext.getContentResolver(), |
Christopher Tate | c09cdce | 2012-09-10 16:50:14 -0700 | [diff] [blame] | 1184 | Settings.Global.AIRPLANE_MODE_ON, |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1185 | on ? 1 : 0); |
| 1186 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
Dianne Hackborn | 818ba11 | 2009-12-11 11:12:02 -0800 | [diff] [blame] | 1187 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1188 | intent.putExtra("state", on); |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1189 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Amith Yamasani | 7ee994d | 2012-05-24 13:53:26 -0700 | [diff] [blame] | 1190 | if (!mHasTelephony) { |
| 1191 | mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1192 | } |
Jinghui Guo | fe1fee5 | 2009-09-17 14:05:27 -0500 | [diff] [blame] | 1193 | } |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1194 | |
| 1195 | private static final class GlobalActionsDialog extends Dialog implements DialogInterface { |
| 1196 | private final Context mContext; |
| 1197 | private final int mWindowTouchSlop; |
| 1198 | private final AlertController mAlert; |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 1199 | private final MyAdapter mAdapter; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1200 | |
| 1201 | private EnableAccessibilityController mEnableAccessibilityController; |
| 1202 | |
| 1203 | private boolean mIntercepted; |
| 1204 | private boolean mCancelOnUp; |
| 1205 | |
| 1206 | public GlobalActionsDialog(Context context, AlertParams params) { |
| 1207 | super(context, getDialogTheme(context)); |
Alan Viverette | e54d245 | 2015-05-06 10:41:43 -0700 | [diff] [blame] | 1208 | mContext = getContext(); |
Michael Kwan | cc6e6f0 | 2016-05-16 12:54:57 -0700 | [diff] [blame] | 1209 | mAlert = AlertController.create(mContext, this, getWindow()); |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 1210 | mAdapter = (MyAdapter) params.mAdapter; |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1211 | mWindowTouchSlop = ViewConfiguration.get(context).getScaledWindowTouchSlop(); |
| 1212 | params.apply(mAlert); |
| 1213 | } |
| 1214 | |
| 1215 | private static int getDialogTheme(Context context) { |
| 1216 | TypedValue outValue = new TypedValue(); |
| 1217 | context.getTheme().resolveAttribute(com.android.internal.R.attr.alertDialogTheme, |
| 1218 | outValue, true); |
| 1219 | return outValue.resourceId; |
| 1220 | } |
| 1221 | |
| 1222 | @Override |
| 1223 | protected void onStart() { |
| 1224 | // If global accessibility gesture can be performed, we will take care |
| 1225 | // of dismissing the dialog on touch outside. This is because the dialog |
| 1226 | // is dismissed on the first down while the global gesture is a long press |
| 1227 | // with two fingers anywhere on the screen. |
| 1228 | if (EnableAccessibilityController.canEnableAccessibilityViaGesture(mContext)) { |
Svetoslav | dc6d1a9 | 2014-08-28 18:40:57 -0700 | [diff] [blame] | 1229 | mEnableAccessibilityController = new EnableAccessibilityController(mContext, |
| 1230 | new Runnable() { |
| 1231 | @Override |
| 1232 | public void run() { |
| 1233 | dismiss(); |
| 1234 | } |
| 1235 | }); |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1236 | super.setCanceledOnTouchOutside(false); |
| 1237 | } else { |
| 1238 | mEnableAccessibilityController = null; |
| 1239 | super.setCanceledOnTouchOutside(true); |
| 1240 | } |
Justin Koh | 1c46c2d78 | 2014-05-12 16:00:36 -0700 | [diff] [blame] | 1241 | |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1242 | super.onStart(); |
| 1243 | } |
| 1244 | |
| 1245 | @Override |
| 1246 | protected void onStop() { |
| 1247 | if (mEnableAccessibilityController != null) { |
| 1248 | mEnableAccessibilityController.onDestroy(); |
| 1249 | } |
| 1250 | super.onStop(); |
| 1251 | } |
| 1252 | |
| 1253 | @Override |
| 1254 | public boolean dispatchTouchEvent(MotionEvent event) { |
| 1255 | if (mEnableAccessibilityController != null) { |
| 1256 | final int action = event.getActionMasked(); |
| 1257 | if (action == MotionEvent.ACTION_DOWN) { |
| 1258 | View decor = getWindow().getDecorView(); |
| 1259 | final int eventX = (int) event.getX(); |
| 1260 | final int eventY = (int) event.getY(); |
| 1261 | if (eventX < -mWindowTouchSlop |
| 1262 | || eventY < -mWindowTouchSlop |
| 1263 | || eventX >= decor.getWidth() + mWindowTouchSlop |
| 1264 | || eventY >= decor.getHeight() + mWindowTouchSlop) { |
| 1265 | mCancelOnUp = true; |
| 1266 | } |
| 1267 | } |
| 1268 | try { |
| 1269 | if (!mIntercepted) { |
| 1270 | mIntercepted = mEnableAccessibilityController.onInterceptTouchEvent(event); |
| 1271 | if (mIntercepted) { |
| 1272 | final long now = SystemClock.uptimeMillis(); |
| 1273 | event = MotionEvent.obtain(now, now, |
| 1274 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 1275 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1276 | mCancelOnUp = true; |
| 1277 | } |
| 1278 | } else { |
| 1279 | return mEnableAccessibilityController.onTouchEvent(event); |
| 1280 | } |
| 1281 | } finally { |
| 1282 | if (action == MotionEvent.ACTION_UP) { |
| 1283 | if (mCancelOnUp) { |
| 1284 | cancel(); |
| 1285 | } |
| 1286 | mCancelOnUp = false; |
| 1287 | mIntercepted = false; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | return super.dispatchTouchEvent(event); |
| 1292 | } |
| 1293 | |
| 1294 | public ListView getListView() { |
| 1295 | return mAlert.getListView(); |
| 1296 | } |
| 1297 | |
| 1298 | @Override |
| 1299 | protected void onCreate(Bundle savedInstanceState) { |
| 1300 | super.onCreate(savedInstanceState); |
| 1301 | mAlert.installContent(); |
| 1302 | } |
| 1303 | |
| 1304 | @Override |
Jason Monk | 30443b1 | 2014-09-23 09:09:52 -0400 | [diff] [blame] | 1305 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1306 | if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { |
| 1307 | for (int i = 0; i < mAdapter.getCount(); ++i) { |
| 1308 | CharSequence label = |
| 1309 | mAdapter.getItem(i).getLabelForAccessibility(getContext()); |
| 1310 | if (label != null) { |
| 1311 | event.getText().add(label); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | return super.dispatchPopulateAccessibilityEvent(event); |
| 1316 | } |
| 1317 | |
| 1318 | @Override |
Svetoslav Ganov | 7befb7d | 2012-09-27 16:49:23 -0700 | [diff] [blame] | 1319 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1320 | if (mAlert.onKeyDown(keyCode, event)) { |
| 1321 | return true; |
| 1322 | } |
| 1323 | return super.onKeyDown(keyCode, event); |
| 1324 | } |
| 1325 | |
| 1326 | @Override |
| 1327 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 1328 | if (mAlert.onKeyUp(keyCode, event)) { |
| 1329 | return true; |
| 1330 | } |
| 1331 | return super.onKeyUp(keyCode, event); |
| 1332 | } |
| 1333 | } |
The Android Open Source Project | 1f838aa | 2009-03-03 19:32:13 -0800 | [diff] [blame] | 1334 | } |