Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui.globalactions; |
| 16 | |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 17 | import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; |
Adrian Roos | 2f05bb3 | 2018-02-19 16:42:27 +0100 | [diff] [blame] | 18 | import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
| 19 | |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 20 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST; |
| 21 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED; |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 22 | import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN; |
| 23 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 24 | import android.app.ActivityManager; |
| 25 | import android.app.Dialog; |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 26 | import android.app.KeyguardManager; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 27 | import android.app.WallpaperManager; |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 28 | import android.app.admin.DevicePolicyManager; |
Pavel Grafov | 059021b | 2018-05-02 13:44:46 +0100 | [diff] [blame] | 29 | import android.app.trust.TrustManager; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 30 | import android.content.BroadcastReceiver; |
| 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
| 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
| 35 | import android.content.pm.UserInfo; |
| 36 | import android.database.ContentObserver; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 37 | import android.graphics.Point; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 38 | import android.graphics.drawable.Drawable; |
| 39 | import android.media.AudioManager; |
| 40 | import android.net.ConnectivityManager; |
| 41 | import android.os.Build; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 42 | import android.os.Handler; |
| 43 | import android.os.Message; |
| 44 | import android.os.RemoteException; |
| 45 | import android.os.ServiceManager; |
| 46 | import android.os.SystemProperties; |
| 47 | import android.os.UserHandle; |
| 48 | import android.os.UserManager; |
| 49 | import android.os.Vibrator; |
| 50 | import android.provider.Settings; |
| 51 | import android.service.dreams.DreamService; |
| 52 | import android.service.dreams.IDreamManager; |
| 53 | import android.telephony.PhoneStateListener; |
| 54 | import android.telephony.ServiceState; |
| 55 | import android.telephony.TelephonyManager; |
| 56 | import android.text.TextUtils; |
| 57 | import android.util.ArraySet; |
| 58 | import android.util.Log; |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 59 | import android.view.ContextThemeWrapper; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 60 | import android.view.LayoutInflater; |
| 61 | import android.view.View; |
| 62 | import android.view.ViewGroup; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 63 | import android.view.Window; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 64 | import android.view.WindowManager; |
| 65 | import android.view.WindowManagerGlobal; |
| 66 | import android.view.accessibility.AccessibilityEvent; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 67 | import android.widget.AdapterView.OnItemLongClickListener; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 68 | import android.widget.BaseAdapter; |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 69 | import android.widget.FrameLayout; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 70 | import android.widget.ImageView; |
| 71 | import android.widget.ImageView.ScaleType; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 72 | import android.widget.LinearLayout; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 73 | import android.widget.TextView; |
| 74 | |
Charles He | 9851a8d | 2017-10-10 17:31:30 +0100 | [diff] [blame] | 75 | import com.android.internal.R; |
| 76 | import com.android.internal.colorextraction.ColorExtractor; |
| 77 | import com.android.internal.colorextraction.ColorExtractor.GradientColors; |
Lucas Dupin | e2292a9 | 2017-07-06 14:35:30 -0700 | [diff] [blame] | 78 | import com.android.internal.colorextraction.drawable.GradientDrawable; |
Charles He | 9851a8d | 2017-10-10 17:31:30 +0100 | [diff] [blame] | 79 | import com.android.internal.logging.MetricsLogger; |
| 80 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
| 81 | import com.android.internal.telephony.TelephonyIntents; |
| 82 | import com.android.internal.telephony.TelephonyProperties; |
| 83 | import com.android.internal.util.EmergencyAffordanceManager; |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 84 | import com.android.internal.util.ScreenshotHelper; |
Charles He | 9851a8d | 2017-10-10 17:31:30 +0100 | [diff] [blame] | 85 | import com.android.internal.widget.LockPatternUtils; |
| 86 | import com.android.systemui.Dependency; |
| 87 | import com.android.systemui.HardwareUiLayout; |
| 88 | import com.android.systemui.Interpolators; |
| 89 | import com.android.systemui.colorextraction.SysuiColorExtractor; |
| 90 | import com.android.systemui.plugins.GlobalActions.GlobalActionsManager; |
| 91 | import com.android.systemui.statusbar.phone.ScrimController; |
Shaotang Li | f9a69e2 | 2018-07-04 14:18:40 +0800 | [diff] [blame] | 92 | import com.android.systemui.util.EmergencyDialerConstants; |
Julia Reynolds | 4241192 | 2017-11-08 11:19:09 -0500 | [diff] [blame] | 93 | import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 94 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 95 | import java.util.ArrayList; |
| 96 | import java.util.List; |
| 97 | |
| 98 | /** |
| 99 | * Helper to show the global actions dialog. Each item is an {@link Action} that |
| 100 | * may show depending on whether the keyguard is showing, and whether the device |
| 101 | * is provisioned. |
| 102 | */ |
Charles He | 9851a8d | 2017-10-10 17:31:30 +0100 | [diff] [blame] | 103 | class GlobalActionsDialog implements DialogInterface.OnDismissListener, |
Shaotang Li | 8af3a24 | 2018-08-02 11:18:00 +0800 | [diff] [blame] | 104 | DialogInterface.OnClickListener, DialogInterface.OnShowListener { |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 105 | |
| 106 | static public final String SYSTEM_DIALOG_REASON_KEY = "reason"; |
| 107 | static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"; |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 108 | static public final String SYSTEM_DIALOG_REASON_DREAM = "dream"; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 109 | |
| 110 | private static final String TAG = "GlobalActionsDialog"; |
| 111 | |
| 112 | private static final boolean SHOW_SILENT_TOGGLE = true; |
| 113 | |
| 114 | /* Valid settings for global actions keys. |
| 115 | * see config.xml config_globalActionList */ |
| 116 | private static final String GLOBAL_ACTION_KEY_POWER = "power"; |
| 117 | private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane"; |
| 118 | private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport"; |
| 119 | private static final String GLOBAL_ACTION_KEY_SILENT = "silent"; |
| 120 | private static final String GLOBAL_ACTION_KEY_USERS = "users"; |
| 121 | private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings"; |
| 122 | private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown"; |
| 123 | private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist"; |
| 124 | private static final String GLOBAL_ACTION_KEY_ASSIST = "assist"; |
| 125 | private static final String GLOBAL_ACTION_KEY_RESTART = "restart"; |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 126 | private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout"; |
Wesley.CW Wang | 74b9579 | 2018-05-28 16:39:27 +0800 | [diff] [blame] | 127 | private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency"; |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 128 | private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot"; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 129 | |
| 130 | private final Context mContext; |
| 131 | private final GlobalActionsManager mWindowManagerFuncs; |
| 132 | private final AudioManager mAudioManager; |
| 133 | private final IDreamManager mDreamManager; |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 134 | private final DevicePolicyManager mDevicePolicyManager; |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 135 | private final LockPatternUtils mLockPatternUtils; |
| 136 | private final KeyguardManager mKeyguardManager; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 137 | |
| 138 | private ArrayList<Action> mItems; |
| 139 | private ActionsDialog mDialog; |
| 140 | |
| 141 | private Action mSilentModeAction; |
| 142 | private ToggleAction mAirplaneModeOn; |
| 143 | |
| 144 | private MyAdapter mAdapter; |
| 145 | |
| 146 | private boolean mKeyguardShowing = false; |
| 147 | private boolean mDeviceProvisioned = false; |
| 148 | private ToggleAction.State mAirplaneState = ToggleAction.State.Off; |
| 149 | private boolean mIsWaitingForEcmExit = false; |
| 150 | private boolean mHasTelephony; |
| 151 | private boolean mHasVibrator; |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 152 | private boolean mHasLogoutButton; |
Chad Brubaker | 72a73ea | 2018-01-26 15:56:55 -0800 | [diff] [blame] | 153 | private boolean mHasLockdownButton; |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 154 | private boolean mSeparatedEmergencyButtonEnabled; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 155 | private final boolean mShowSilentToggle; |
| 156 | private final EmergencyAffordanceManager mEmergencyAffordanceManager; |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 157 | private final ScreenshotHelper mScreenshotHelper; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 158 | |
| 159 | /** |
| 160 | * @param context everything needs a context :( |
| 161 | */ |
| 162 | public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) { |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 163 | mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 164 | mWindowManagerFuncs = windowManagerFuncs; |
| 165 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 166 | mDreamManager = IDreamManager.Stub.asInterface( |
| 167 | ServiceManager.getService(DreamService.DREAM_SERVICE)); |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 168 | mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService( |
| 169 | Context.DEVICE_POLICY_SERVICE); |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 170 | mLockPatternUtils = new LockPatternUtils(mContext); |
| 171 | mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 172 | |
| 173 | // receive broadcasts |
| 174 | IntentFilter filter = new IntentFilter(); |
| 175 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 176 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 177 | filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED); |
| 178 | context.registerReceiver(mBroadcastReceiver, filter); |
| 179 | |
| 180 | ConnectivityManager cm = (ConnectivityManager) |
| 181 | context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 182 | mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); |
| 183 | |
| 184 | // get notified of phone state changes |
| 185 | TelephonyManager telephonyManager = |
| 186 | (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 187 | telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); |
| 188 | mContext.getContentResolver().registerContentObserver( |
| 189 | Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true, |
| 190 | mAirplaneModeObserver); |
| 191 | Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); |
| 192 | mHasVibrator = vibrator != null && vibrator.hasVibrator(); |
| 193 | |
| 194 | mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean( |
| 195 | R.bool.config_useFixedVolume); |
| 196 | |
| 197 | mEmergencyAffordanceManager = new EmergencyAffordanceManager(context); |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 198 | mScreenshotHelper = new ScreenshotHelper(context); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Show the global actions dialog (creating if necessary) |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 203 | * |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 204 | * @param keyguardShowing True if keyguard is showing |
| 205 | */ |
| 206 | public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) { |
| 207 | mKeyguardShowing = keyguardShowing; |
| 208 | mDeviceProvisioned = isDeviceProvisioned; |
| 209 | if (mDialog != null) { |
| 210 | mDialog.dismiss(); |
| 211 | mDialog = null; |
| 212 | // Show delayed, so that the dismiss of the previous dialog completes |
| 213 | mHandler.sendEmptyMessage(MESSAGE_SHOW); |
| 214 | } else { |
| 215 | handleShow(); |
| 216 | } |
| 217 | } |
| 218 | |
Charles He | 9851a8d | 2017-10-10 17:31:30 +0100 | [diff] [blame] | 219 | /** |
| 220 | * Dismiss the global actions dialog, if it's currently shown |
| 221 | */ |
| 222 | public void dismissDialog() { |
| 223 | mHandler.removeMessages(MESSAGE_DISMISS); |
| 224 | mHandler.sendEmptyMessage(MESSAGE_DISMISS); |
| 225 | } |
| 226 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 227 | private void awakenIfNecessary() { |
| 228 | if (mDreamManager != null) { |
| 229 | try { |
| 230 | if (mDreamManager.isDreaming()) { |
| 231 | mDreamManager.awaken(); |
| 232 | } |
| 233 | } catch (RemoteException e) { |
| 234 | // we tried |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | private void handleShow() { |
| 240 | awakenIfNecessary(); |
| 241 | mDialog = createDialog(); |
| 242 | prepareDialog(); |
| 243 | |
| 244 | // If we only have 1 item and it's a simple press action, just do this action. |
| 245 | if (mAdapter.getCount() == 1 |
| 246 | && mAdapter.getItem(0) instanceof SinglePressAction |
| 247 | && !(mAdapter.getItem(0) instanceof LongPressAction)) { |
| 248 | ((SinglePressAction) mAdapter.getItem(0)).onPress(); |
| 249 | } else { |
| 250 | WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes(); |
| 251 | attrs.setTitle("ActionsDialog"); |
Adrian Roos | 2f05bb3 | 2018-02-19 16:42:27 +0100 | [diff] [blame] | 252 | attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 253 | mDialog.getWindow().setAttributes(attrs); |
| 254 | mDialog.show(); |
| 255 | mWindowManagerFuncs.onGlobalActionsShown(); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Create the global actions dialog. |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 261 | * |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 262 | * @return A new dialog. |
| 263 | */ |
| 264 | private ActionsDialog createDialog() { |
| 265 | // Simple toggle style if there's no vibrator, otherwise use a tri-state |
| 266 | if (!mHasVibrator) { |
| 267 | mSilentModeAction = new SilentModeToggleAction(); |
| 268 | } else { |
| 269 | mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler); |
| 270 | } |
| 271 | mAirplaneModeOn = new ToggleAction( |
| 272 | R.drawable.ic_lock_airplane_mode, |
| 273 | R.drawable.ic_lock_airplane_mode_off, |
| 274 | R.string.global_actions_toggle_airplane_mode, |
| 275 | R.string.global_actions_airplane_mode_on_status, |
| 276 | R.string.global_actions_airplane_mode_off_status) { |
| 277 | |
| 278 | void onToggle(boolean on) { |
| 279 | if (mHasTelephony && Boolean.parseBoolean( |
| 280 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
| 281 | mIsWaitingForEcmExit = true; |
| 282 | // Launch ECM exit dialog |
| 283 | Intent ecmDialogIntent = |
| 284 | new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null); |
| 285 | ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 286 | mContext.startActivity(ecmDialogIntent); |
| 287 | } else { |
| 288 | changeAirplaneModeSystemSetting(on); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | protected void changeStateFromPress(boolean buttonOn) { |
| 294 | if (!mHasTelephony) return; |
| 295 | |
| 296 | // In ECM mode airplane state cannot be changed |
| 297 | if (!(Boolean.parseBoolean( |
| 298 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) { |
| 299 | mState = buttonOn ? State.TurningOn : State.TurningOff; |
| 300 | mAirplaneState = mState; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | public boolean showDuringKeyguard() { |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | public boolean showBeforeProvisioning() { |
| 309 | return false; |
| 310 | } |
| 311 | }; |
| 312 | onAirplaneModeChanged(); |
| 313 | |
| 314 | mItems = new ArrayList<Action>(); |
| 315 | String[] defaultActions = mContext.getResources().getStringArray( |
| 316 | R.array.config_globalActionsList); |
| 317 | |
| 318 | ArraySet<String> addedKeys = new ArraySet<String>(); |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 319 | mHasLogoutButton = false; |
Chad Brubaker | 72a73ea | 2018-01-26 15:56:55 -0800 | [diff] [blame] | 320 | mHasLockdownButton = false; |
yuanjiahsu | 2c69b0b | 2018-11-16 21:37:33 +0800 | [diff] [blame] | 321 | mSeparatedEmergencyButtonEnabled = true; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 322 | for (int i = 0; i < defaultActions.length; i++) { |
| 323 | String actionKey = defaultActions[i]; |
| 324 | if (addedKeys.contains(actionKey)) { |
| 325 | // If we already have added this, don't add it again. |
| 326 | continue; |
| 327 | } |
| 328 | if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) { |
| 329 | mItems.add(new PowerAction()); |
| 330 | } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) { |
| 331 | mItems.add(mAirplaneModeOn); |
| 332 | } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) { |
| 333 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 334 | Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) { |
| 335 | mItems.add(new BugReportAction()); |
| 336 | } |
| 337 | } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) { |
| 338 | if (mShowSilentToggle) { |
| 339 | mItems.add(mSilentModeAction); |
| 340 | } |
| 341 | } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) { |
| 342 | if (SystemProperties.getBoolean("fw.power_user_switcher", false)) { |
| 343 | addUsersToMenu(mItems); |
| 344 | } |
| 345 | } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) { |
| 346 | mItems.add(getSettingsAction()); |
| 347 | } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) { |
Chad Brubaker | 02cd6cf | 2018-05-01 14:59:33 -0700 | [diff] [blame] | 348 | if (Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 349 | Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0 |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 350 | && shouldDisplayLockdown()) { |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 351 | mItems.add(getLockdownAction()); |
Chad Brubaker | 72a73ea | 2018-01-26 15:56:55 -0800 | [diff] [blame] | 352 | mHasLockdownButton = true; |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 353 | } |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 354 | } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) { |
| 355 | mItems.add(getVoiceAssistAction()); |
| 356 | } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) { |
| 357 | mItems.add(getAssistAction()); |
| 358 | } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) { |
| 359 | mItems.add(new RestartAction()); |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 360 | } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) { |
| 361 | mItems.add(new ScreenshotAction()); |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 362 | } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) { |
Alex Chau | d795827 | 2017-12-08 11:30:52 +0000 | [diff] [blame] | 363 | if (mDevicePolicyManager.isLogoutEnabled() |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 364 | && getCurrentUser().id != UserHandle.USER_SYSTEM) { |
| 365 | mItems.add(new LogoutAction()); |
| 366 | mHasLogoutButton = true; |
| 367 | } |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 368 | } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) { |
| 369 | if (mSeparatedEmergencyButtonEnabled |
| 370 | && !mEmergencyAffordanceManager.needsEmergencyAffordance()) { |
| 371 | mItems.add(new EmergencyDialerAction()); |
| 372 | } |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 373 | } else { |
| 374 | Log.e(TAG, "Invalid global action key " + actionKey); |
| 375 | } |
| 376 | // Add here so we don't add more than one. |
| 377 | addedKeys.add(actionKey); |
| 378 | } |
| 379 | |
| 380 | if (mEmergencyAffordanceManager.needsEmergencyAffordance()) { |
| 381 | mItems.add(getEmergencyAction()); |
| 382 | } |
| 383 | |
| 384 | mAdapter = new MyAdapter(); |
| 385 | |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 386 | OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> { |
| 387 | final Action action = mAdapter.getItem(position); |
| 388 | if (action instanceof LongPressAction) { |
| 389 | mDialog.dismiss(); |
| 390 | return ((LongPressAction) action).onLongPress(); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 391 | } |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 392 | return false; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 393 | }; |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 394 | ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener, |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 395 | mSeparatedEmergencyButtonEnabled); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 396 | dialog.setCanceledOnTouchOutside(false); // Handled by the custom class. |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 397 | dialog.setKeyguardShowing(mKeyguardShowing); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 398 | |
| 399 | dialog.setOnDismissListener(this); |
Shaotang Li | 8af3a24 | 2018-08-02 11:18:00 +0800 | [diff] [blame] | 400 | dialog.setOnShowListener(this); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 401 | |
| 402 | return dialog; |
| 403 | } |
| 404 | |
Chad Brubaker | f4075fe | 2018-01-03 13:23:22 -0800 | [diff] [blame] | 405 | private boolean shouldDisplayLockdown() { |
| 406 | int userId = getCurrentUser().id; |
| 407 | // Lockdown is meaningless without a place to go. |
| 408 | if (!mKeyguardManager.isDeviceSecure(userId)) { |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | // Only show the lockdown button if the device isn't locked down (for whatever reason). |
| 413 | int state = mLockPatternUtils.getStrongAuthForUser(userId); |
| 414 | return (state == STRONG_AUTH_NOT_REQUIRED |
| 415 | || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST); |
| 416 | } |
| 417 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 418 | private final class PowerAction extends SinglePressAction implements LongPressAction { |
| 419 | private PowerAction() { |
| 420 | super(R.drawable.ic_lock_power_off, |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 421 | R.string.global_action_power_off); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public boolean onLongPress() { |
| 426 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 427 | if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) { |
| 428 | mWindowManagerFuncs.reboot(true); |
| 429 | return true; |
| 430 | } |
| 431 | return false; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public boolean showDuringKeyguard() { |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public boolean showBeforeProvisioning() { |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void onPress() { |
| 446 | // shutdown by making sure radio and power are handled accordingly. |
| 447 | mWindowManagerFuncs.shutdown(); |
| 448 | } |
| 449 | } |
| 450 | |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 451 | private class EmergencyDialerAction extends SinglePressAction { |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 452 | private EmergencyDialerAction() { |
| 453 | super(R.drawable.ic_faster_emergency, |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 454 | R.string.global_action_emergency); |
Wesley.CW Wang | 74b9579 | 2018-05-28 16:39:27 +0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Override |
| 458 | public void onPress() { |
Shaotang Li | 8af3a24 | 2018-08-02 11:18:00 +0800 | [diff] [blame] | 459 | MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU); |
Shaotang Li | f9a69e2 | 2018-07-04 14:18:40 +0800 | [diff] [blame] | 460 | Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL); |
Wesley.CW Wang | 74b9579 | 2018-05-28 16:39:27 +0800 | [diff] [blame] | 461 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
Shaotang Li | f9a69e2 | 2018-07-04 14:18:40 +0800 | [diff] [blame] | 462 | intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE, |
| 463 | EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU); |
Wesley.CW Wang | 74b9579 | 2018-05-28 16:39:27 +0800 | [diff] [blame] | 464 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public boolean showDuringKeyguard() { |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public boolean showBeforeProvisioning() { |
| 474 | return true; |
| 475 | } |
| 476 | } |
| 477 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 478 | private final class RestartAction extends SinglePressAction implements LongPressAction { |
| 479 | private RestartAction() { |
| 480 | super(R.drawable.ic_restart, R.string.global_action_restart); |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public boolean onLongPress() { |
| 485 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 486 | if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) { |
| 487 | mWindowManagerFuncs.reboot(true); |
| 488 | return true; |
| 489 | } |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public boolean showDuringKeyguard() { |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean showBeforeProvisioning() { |
| 500 | return true; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void onPress() { |
| 505 | mWindowManagerFuncs.reboot(false); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
Alison Cichowlas | a2cd19e | 2017-12-06 10:51:21 -0500 | [diff] [blame] | 510 | private class ScreenshotAction extends SinglePressAction { |
| 511 | public ScreenshotAction() { |
| 512 | super(R.drawable.ic_screenshot, R.string.global_action_screenshot); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public void onPress() { |
| 517 | // Add a little delay before executing, to give the |
| 518 | // dialog a chance to go away before it takes a |
| 519 | // screenshot. |
| 520 | // TODO: instead, omit global action dialog layer |
| 521 | mHandler.postDelayed(new Runnable() { |
| 522 | @Override |
| 523 | public void run() { |
| 524 | mScreenshotHelper.takeScreenshot(1, true, true, mHandler); |
| 525 | MetricsLogger.action(mContext, |
| 526 | MetricsEvent.ACTION_SCREENSHOT_POWER_MENU); |
| 527 | } |
| 528 | }, 500); |
| 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public boolean showDuringKeyguard() { |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | @Override |
| 537 | public boolean showBeforeProvisioning() { |
| 538 | return false; |
| 539 | } |
| 540 | } |
| 541 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 542 | private class BugReportAction extends SinglePressAction implements LongPressAction { |
| 543 | |
| 544 | public BugReportAction() { |
| 545 | super(R.drawable.ic_lock_bugreport, R.string.bugreport_title); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void onPress() { |
| 550 | // don't actually trigger the bugreport if we are running stability |
| 551 | // tests via monkey |
| 552 | if (ActivityManager.isUserAMonkey()) { |
| 553 | return; |
| 554 | } |
| 555 | // Add a little delay before executing, to give the |
| 556 | // dialog a chance to go away before it takes a |
| 557 | // screenshot. |
| 558 | mHandler.postDelayed(new Runnable() { |
| 559 | @Override |
| 560 | public void run() { |
| 561 | try { |
| 562 | // Take an "interactive" bugreport. |
| 563 | MetricsLogger.action(mContext, |
| 564 | MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE); |
| 565 | ActivityManager.getService().requestBugReport( |
| 566 | ActivityManager.BUGREPORT_OPTION_INTERACTIVE); |
| 567 | } catch (RemoteException e) { |
| 568 | } |
| 569 | } |
| 570 | }, 500); |
| 571 | } |
| 572 | |
| 573 | @Override |
| 574 | public boolean onLongPress() { |
| 575 | // don't actually trigger the bugreport if we are running stability |
| 576 | // tests via monkey |
| 577 | if (ActivityManager.isUserAMonkey()) { |
| 578 | return false; |
| 579 | } |
| 580 | try { |
| 581 | // Take a "full" bugreport. |
| 582 | MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL); |
| 583 | ActivityManager.getService().requestBugReport( |
| 584 | ActivityManager.BUGREPORT_OPTION_FULL); |
| 585 | } catch (RemoteException e) { |
| 586 | } |
| 587 | return false; |
| 588 | } |
| 589 | |
| 590 | public boolean showDuringKeyguard() { |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public boolean showBeforeProvisioning() { |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | public String getStatus() { |
| 601 | return mContext.getString( |
| 602 | R.string.bugreport_status, |
| 603 | Build.VERSION.RELEASE, |
| 604 | Build.ID); |
| 605 | } |
| 606 | } |
| 607 | |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 608 | private final class LogoutAction extends SinglePressAction { |
| 609 | private LogoutAction() { |
| 610 | super(R.drawable.ic_logout, R.string.global_action_logout); |
| 611 | } |
| 612 | |
| 613 | @Override |
| 614 | public boolean showDuringKeyguard() { |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public boolean showBeforeProvisioning() { |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | @Override |
| 624 | public void onPress() { |
| 625 | // Add a little delay before executing, to give the dialog a chance to go away before |
| 626 | // switching user |
| 627 | mHandler.postDelayed(() -> { |
| 628 | try { |
Alex Chau | edb6a01 | 2018-01-26 12:52:43 +0000 | [diff] [blame] | 629 | int currentUserId = getCurrentUser().id; |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 630 | ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM); |
Alex Chau | edb6a01 | 2018-01-26 12:52:43 +0000 | [diff] [blame] | 631 | ActivityManager.getService().stopUser(currentUserId, true /*force*/, null); |
Alex Chau | 0445885 | 2017-11-27 18:21:23 +0000 | [diff] [blame] | 632 | } catch (RemoteException re) { |
| 633 | Log.e(TAG, "Couldn't logout user " + re); |
| 634 | } |
| 635 | }, 500); |
| 636 | } |
| 637 | } |
| 638 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 639 | private Action getSettingsAction() { |
| 640 | return new SinglePressAction(R.drawable.ic_settings, |
| 641 | R.string.global_action_settings) { |
| 642 | |
| 643 | @Override |
| 644 | public void onPress() { |
| 645 | Intent intent = new Intent(Settings.ACTION_SETTINGS); |
| 646 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 647 | mContext.startActivity(intent); |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public boolean showDuringKeyguard() { |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | @Override |
| 656 | public boolean showBeforeProvisioning() { |
| 657 | return true; |
| 658 | } |
| 659 | }; |
| 660 | } |
| 661 | |
| 662 | private Action getEmergencyAction() { |
Wesley.CW Wang | d560e8f | 2018-07-06 15:26:21 +0800 | [diff] [blame] | 663 | Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon); |
| 664 | if(!mSeparatedEmergencyButtonEnabled) { |
| 665 | // use un-colored legacy treatment |
| 666 | emergencyIcon.setTintList(null); |
| 667 | } |
| 668 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 669 | return new SinglePressAction(R.drawable.emergency_icon, |
| 670 | R.string.global_action_emergency) { |
| 671 | @Override |
| 672 | public void onPress() { |
| 673 | mEmergencyAffordanceManager.performEmergencyCall(); |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public boolean showDuringKeyguard() { |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | @Override |
| 682 | public boolean showBeforeProvisioning() { |
| 683 | return true; |
| 684 | } |
| 685 | }; |
| 686 | } |
| 687 | |
| 688 | private Action getAssistAction() { |
| 689 | return new SinglePressAction(R.drawable.ic_action_assist_focused, |
| 690 | R.string.global_action_assist) { |
| 691 | @Override |
| 692 | public void onPress() { |
| 693 | Intent intent = new Intent(Intent.ACTION_ASSIST); |
| 694 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 695 | mContext.startActivity(intent); |
| 696 | } |
| 697 | |
| 698 | @Override |
| 699 | public boolean showDuringKeyguard() { |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public boolean showBeforeProvisioning() { |
| 705 | return true; |
| 706 | } |
| 707 | }; |
| 708 | } |
| 709 | |
| 710 | private Action getVoiceAssistAction() { |
| 711 | return new SinglePressAction(R.drawable.ic_voice_search, |
| 712 | R.string.global_action_voice_assist) { |
| 713 | @Override |
| 714 | public void onPress() { |
| 715 | Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST); |
| 716 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| 717 | mContext.startActivity(intent); |
| 718 | } |
| 719 | |
| 720 | @Override |
| 721 | public boolean showDuringKeyguard() { |
| 722 | return true; |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public boolean showBeforeProvisioning() { |
| 727 | return true; |
| 728 | } |
| 729 | }; |
| 730 | } |
| 731 | |
| 732 | private Action getLockdownAction() { |
Alison Cichowlas | d372cde | 2018-05-16 15:40:45 -0400 | [diff] [blame] | 733 | return new SinglePressAction(R.drawable.ic_lock_lockdown, |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 734 | R.string.global_action_lockdown) { |
| 735 | |
| 736 | @Override |
| 737 | public void onPress() { |
Chad Brubaker | 4f28f0d | 2017-09-07 14:28:13 -0700 | [diff] [blame] | 738 | new LockPatternUtils(mContext) |
| 739 | .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN, |
| 740 | UserHandle.USER_ALL); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 741 | try { |
| 742 | WindowManagerGlobal.getWindowManagerService().lockNow(null); |
Pavel Grafov | 059021b | 2018-05-02 13:44:46 +0100 | [diff] [blame] | 743 | // Lock profiles (if any) on the background thread. |
| 744 | final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER)); |
| 745 | bgHandler.post(() -> lockProfiles()); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 746 | } catch (RemoteException e) { |
| 747 | Log.e(TAG, "Error while trying to lock device.", e); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public boolean showDuringKeyguard() { |
| 753 | return true; |
| 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public boolean showBeforeProvisioning() { |
| 758 | return false; |
| 759 | } |
| 760 | }; |
| 761 | } |
| 762 | |
Pavel Grafov | 059021b | 2018-05-02 13:44:46 +0100 | [diff] [blame] | 763 | private void lockProfiles() { |
| 764 | final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 765 | final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE); |
| 766 | final int currentUserId = getCurrentUser().id; |
| 767 | final int[] profileIds = um.getEnabledProfileIds(currentUserId); |
| 768 | for (final int id : profileIds) { |
| 769 | if (id != currentUserId) { |
| 770 | tm.setDeviceLockedForUser(id, true); |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 775 | private UserInfo getCurrentUser() { |
| 776 | try { |
| 777 | return ActivityManager.getService().getCurrentUser(); |
| 778 | } catch (RemoteException re) { |
| 779 | return null; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private boolean isCurrentUserOwner() { |
| 784 | UserInfo currentUser = getCurrentUser(); |
| 785 | return currentUser == null || currentUser.isPrimary(); |
| 786 | } |
| 787 | |
| 788 | private void addUsersToMenu(ArrayList<Action> items) { |
| 789 | UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 790 | if (um.isUserSwitcherEnabled()) { |
| 791 | List<UserInfo> users = um.getUsers(); |
| 792 | UserInfo currentUser = getCurrentUser(); |
| 793 | for (final UserInfo user : users) { |
| 794 | if (user.supportsSwitchToByUser()) { |
| 795 | boolean isCurrentUser = currentUser == null |
| 796 | ? user.id == 0 : (currentUser.id == user.id); |
| 797 | Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath) |
| 798 | : null; |
| 799 | SinglePressAction switchToUser = new SinglePressAction( |
| 800 | R.drawable.ic_menu_cc, icon, |
| 801 | (user.name != null ? user.name : "Primary") |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 802 | + (isCurrentUser ? " \u2714" : "")) { |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 803 | public void onPress() { |
| 804 | try { |
| 805 | ActivityManager.getService().switchUser(user.id); |
| 806 | } catch (RemoteException re) { |
| 807 | Log.e(TAG, "Couldn't switch user " + re); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | public boolean showDuringKeyguard() { |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | public boolean showBeforeProvisioning() { |
| 816 | return false; |
| 817 | } |
| 818 | }; |
| 819 | items.add(switchToUser); |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | private void prepareDialog() { |
| 826 | refreshSilentMode(); |
| 827 | mAirplaneModeOn.updateState(mAirplaneState); |
| 828 | mAdapter.notifyDataSetChanged(); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 829 | if (mShowSilentToggle) { |
| 830 | IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION); |
| 831 | mContext.registerReceiver(mRingerModeReceiver, filter); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | private void refreshSilentMode() { |
| 836 | if (!mHasVibrator) { |
| 837 | final boolean silentModeOn = |
| 838 | mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 839 | ((ToggleAction) mSilentModeAction).updateState( |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 840 | silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** {@inheritDoc} */ |
| 845 | public void onDismiss(DialogInterface dialog) { |
| 846 | mWindowManagerFuncs.onGlobalActionsHidden(); |
| 847 | if (mShowSilentToggle) { |
| 848 | try { |
| 849 | mContext.unregisterReceiver(mRingerModeReceiver); |
| 850 | } catch (IllegalArgumentException ie) { |
| 851 | // ignore this |
| 852 | Log.w(TAG, ie); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /** {@inheritDoc} */ |
| 858 | public void onClick(DialogInterface dialog, int which) { |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 859 | Action item = mAdapter.getItem(which); |
Jason Monk | b430218 | 2017-08-04 13:39:17 -0400 | [diff] [blame] | 860 | if (!(item instanceof SilentModeTriStateAction)) { |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 861 | dialog.dismiss(); |
| 862 | } |
Jason Monk | fd27966 | 2017-06-29 19:37:48 -0400 | [diff] [blame] | 863 | item.onPress(); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Shaotang Li | 8af3a24 | 2018-08-02 11:18:00 +0800 | [diff] [blame] | 866 | /** {@inheritDoc} */ |
| 867 | public void onShow(DialogInterface dialog) { |
| 868 | MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU); |
| 869 | } |
| 870 | |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 871 | /** |
| 872 | * The adapter used for the list within the global actions dialog, taking |
| 873 | * into account whether the keyguard is showing via |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 874 | * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether |
| 875 | * the device is provisioned |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 876 | * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}. |
| 877 | */ |
| 878 | private class MyAdapter extends BaseAdapter { |
| 879 | |
| 880 | public int getCount() { |
| 881 | int count = 0; |
| 882 | |
| 883 | for (int i = 0; i < mItems.size(); i++) { |
| 884 | final Action action = mItems.get(i); |
| 885 | |
| 886 | if (mKeyguardShowing && !action.showDuringKeyguard()) { |
| 887 | continue; |
| 888 | } |
| 889 | if (!mDeviceProvisioned && !action.showBeforeProvisioning()) { |
| 890 | continue; |
| 891 | } |
| 892 | count++; |
| 893 | } |
| 894 | return count; |
| 895 | } |
| 896 | |
| 897 | @Override |
| 898 | public boolean isEnabled(int position) { |
| 899 | return getItem(position).isEnabled(); |
| 900 | } |
| 901 | |
| 902 | @Override |
| 903 | public boolean areAllItemsEnabled() { |
| 904 | return false; |
| 905 | } |
| 906 | |
| 907 | public Action getItem(int position) { |
| 908 | |
| 909 | int filteredPos = 0; |
| 910 | for (int i = 0; i < mItems.size(); i++) { |
| 911 | final Action action = mItems.get(i); |
| 912 | if (mKeyguardShowing && !action.showDuringKeyguard()) { |
| 913 | continue; |
| 914 | } |
| 915 | if (!mDeviceProvisioned && !action.showBeforeProvisioning()) { |
| 916 | continue; |
| 917 | } |
| 918 | if (filteredPos == position) { |
| 919 | return action; |
| 920 | } |
| 921 | filteredPos++; |
| 922 | } |
| 923 | |
| 924 | throw new IllegalArgumentException("position " + position |
| 925 | + " out of range of showable actions" |
| 926 | + ", filtered count=" + getCount() |
| 927 | + ", keyguardshowing=" + mKeyguardShowing |
| 928 | + ", provisioned=" + mDeviceProvisioned); |
| 929 | } |
| 930 | |
| 931 | |
| 932 | public long getItemId(int position) { |
| 933 | return position; |
| 934 | } |
| 935 | |
| 936 | public View getView(int position, View convertView, ViewGroup parent) { |
| 937 | Action action = getItem(position); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 938 | View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext)); |
Alison Cichowlas | 3be52db | 2018-03-06 19:48:06 -0500 | [diff] [blame] | 939 | // Everything but screenshot, the last item, gets white background. |
| 940 | if (position == getCount() - 1) { |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 941 | HardwareUiLayout.get(parent).setDivisionView(view); |
| 942 | } |
| 943 | return view; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | // note: the scheme below made more sense when we were planning on having |
| 948 | // 8 different things in the global actions dialog. seems overkill with |
| 949 | // only 3 items now, but may as well keep this flexible approach so it will |
| 950 | // be easy should someone decide at the last minute to include something |
| 951 | // else, such as 'enable wifi', or 'enable bluetooth' |
| 952 | |
| 953 | /** |
| 954 | * What each item in the global actions dialog must be able to support. |
| 955 | */ |
| 956 | private interface Action { |
| 957 | /** |
| 958 | * @return Text that will be announced when dialog is created. null |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 959 | * for none. |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 960 | */ |
| 961 | CharSequence getLabelForAccessibility(Context context); |
| 962 | |
| 963 | View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater); |
| 964 | |
| 965 | void onPress(); |
| 966 | |
| 967 | /** |
| 968 | * @return whether this action should appear in the dialog when the keygaurd |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 969 | * is showing. |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 970 | */ |
| 971 | boolean showDuringKeyguard(); |
| 972 | |
| 973 | /** |
| 974 | * @return whether this action should appear in the dialog before the |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 975 | * device is provisioned. |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 976 | */ |
| 977 | boolean showBeforeProvisioning(); |
| 978 | |
| 979 | boolean isEnabled(); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * An action that also supports long press. |
| 984 | */ |
| 985 | private interface LongPressAction extends Action { |
| 986 | boolean onLongPress(); |
| 987 | } |
| 988 | |
| 989 | /** |
| 990 | * A single press action maintains no state, just responds to a press |
| 991 | * and takes an action. |
| 992 | */ |
| 993 | private static abstract class SinglePressAction implements Action { |
| 994 | private final int mIconResId; |
| 995 | private final Drawable mIcon; |
| 996 | private final int mMessageResId; |
| 997 | private final CharSequence mMessage; |
| 998 | |
| 999 | protected SinglePressAction(int iconResId, int messageResId) { |
| 1000 | mIconResId = iconResId; |
| 1001 | mMessageResId = messageResId; |
| 1002 | mMessage = null; |
| 1003 | mIcon = null; |
| 1004 | } |
| 1005 | |
| 1006 | protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) { |
| 1007 | mIconResId = iconResId; |
| 1008 | mMessageResId = 0; |
| 1009 | mMessage = message; |
| 1010 | mIcon = icon; |
| 1011 | } |
| 1012 | |
| 1013 | public boolean isEnabled() { |
| 1014 | return true; |
| 1015 | } |
| 1016 | |
| 1017 | public String getStatus() { |
| 1018 | return null; |
| 1019 | } |
| 1020 | |
| 1021 | abstract public void onPress(); |
| 1022 | |
| 1023 | public CharSequence getLabelForAccessibility(Context context) { |
| 1024 | if (mMessage != null) { |
| 1025 | return mMessage; |
| 1026 | } else { |
| 1027 | return context.getString(mMessageResId); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | public View create( |
| 1032 | Context context, View convertView, ViewGroup parent, LayoutInflater inflater) { |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1033 | View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent, |
| 1034 | false); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1035 | |
| 1036 | ImageView icon = (ImageView) v.findViewById(R.id.icon); |
| 1037 | TextView messageView = (TextView) v.findViewById(R.id.message); |
| 1038 | |
| 1039 | TextView statusView = (TextView) v.findViewById(R.id.status); |
| 1040 | final String status = getStatus(); |
| 1041 | if (!TextUtils.isEmpty(status)) { |
| 1042 | statusView.setText(status); |
| 1043 | } else { |
| 1044 | statusView.setVisibility(View.GONE); |
| 1045 | } |
| 1046 | if (mIcon != null) { |
| 1047 | icon.setImageDrawable(mIcon); |
| 1048 | icon.setScaleType(ScaleType.CENTER_CROP); |
| 1049 | } else if (mIconResId != 0) { |
| 1050 | icon.setImageDrawable(context.getDrawable(mIconResId)); |
| 1051 | } |
| 1052 | if (mMessage != null) { |
| 1053 | messageView.setText(mMessage); |
| 1054 | } else { |
| 1055 | messageView.setText(mMessageResId); |
| 1056 | } |
| 1057 | |
| 1058 | return v; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * A toggle action knows whether it is on or off, and displays an icon |
| 1064 | * and status message accordingly. |
| 1065 | */ |
| 1066 | private static abstract class ToggleAction implements Action { |
| 1067 | |
| 1068 | enum State { |
| 1069 | Off(false), |
| 1070 | TurningOn(true), |
| 1071 | TurningOff(true), |
| 1072 | On(false); |
| 1073 | |
| 1074 | private final boolean inTransition; |
| 1075 | |
| 1076 | State(boolean intermediate) { |
| 1077 | inTransition = intermediate; |
| 1078 | } |
| 1079 | |
| 1080 | public boolean inTransition() { |
| 1081 | return inTransition; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | protected State mState = State.Off; |
| 1086 | |
| 1087 | // prefs |
| 1088 | protected int mEnabledIconResId; |
| 1089 | protected int mDisabledIconResid; |
| 1090 | protected int mMessageResId; |
| 1091 | protected int mEnabledStatusMessageResId; |
| 1092 | protected int mDisabledStatusMessageResId; |
| 1093 | |
| 1094 | /** |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1095 | * @param enabledIconResId The icon for when this action is on. |
| 1096 | * @param disabledIconResid The icon for when this action is off. |
| 1097 | * @param message The general information message, e.g 'Silent Mode' |
| 1098 | * @param enabledStatusMessageResId The on status message, e.g 'sound disabled' |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1099 | * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled' |
| 1100 | */ |
| 1101 | public ToggleAction(int enabledIconResId, |
| 1102 | int disabledIconResid, |
| 1103 | int message, |
| 1104 | int enabledStatusMessageResId, |
| 1105 | int disabledStatusMessageResId) { |
| 1106 | mEnabledIconResId = enabledIconResId; |
| 1107 | mDisabledIconResid = disabledIconResid; |
| 1108 | mMessageResId = message; |
| 1109 | mEnabledStatusMessageResId = enabledStatusMessageResId; |
| 1110 | mDisabledStatusMessageResId = disabledStatusMessageResId; |
| 1111 | } |
| 1112 | |
| 1113 | /** |
| 1114 | * Override to make changes to resource IDs just before creating the |
| 1115 | * View. |
| 1116 | */ |
| 1117 | void willCreate() { |
| 1118 | |
| 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public CharSequence getLabelForAccessibility(Context context) { |
| 1123 | return context.getString(mMessageResId); |
| 1124 | } |
| 1125 | |
| 1126 | public View create(Context context, View convertView, ViewGroup parent, |
| 1127 | LayoutInflater inflater) { |
| 1128 | willCreate(); |
| 1129 | |
| 1130 | View v = inflater.inflate(R |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1131 | .layout.global_actions_item, parent, false); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1132 | |
| 1133 | ImageView icon = (ImageView) v.findViewById(R.id.icon); |
| 1134 | TextView messageView = (TextView) v.findViewById(R.id.message); |
| 1135 | TextView statusView = (TextView) v.findViewById(R.id.status); |
| 1136 | final boolean enabled = isEnabled(); |
| 1137 | |
| 1138 | if (messageView != null) { |
| 1139 | messageView.setText(mMessageResId); |
| 1140 | messageView.setEnabled(enabled); |
| 1141 | } |
| 1142 | |
| 1143 | boolean on = ((mState == State.On) || (mState == State.TurningOn)); |
| 1144 | if (icon != null) { |
| 1145 | icon.setImageDrawable(context.getDrawable( |
| 1146 | (on ? mEnabledIconResId : mDisabledIconResid))); |
| 1147 | icon.setEnabled(enabled); |
| 1148 | } |
| 1149 | |
| 1150 | if (statusView != null) { |
| 1151 | statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId); |
| 1152 | statusView.setVisibility(View.VISIBLE); |
| 1153 | statusView.setEnabled(enabled); |
| 1154 | } |
| 1155 | v.setEnabled(enabled); |
| 1156 | |
| 1157 | return v; |
| 1158 | } |
| 1159 | |
| 1160 | public final void onPress() { |
| 1161 | if (mState.inTransition()) { |
| 1162 | Log.w(TAG, "shouldn't be able to toggle when in transition"); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | final boolean nowOn = !(mState == State.On); |
| 1167 | onToggle(nowOn); |
| 1168 | changeStateFromPress(nowOn); |
| 1169 | } |
| 1170 | |
| 1171 | public boolean isEnabled() { |
| 1172 | return !mState.inTransition(); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Implementations may override this if their state can be in on of the intermediate |
| 1177 | * states until some notification is received (e.g airplane mode is 'turning off' until |
| 1178 | * we know the wireless connections are back online |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1179 | * |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1180 | * @param buttonOn Whether the button was turned on or off |
| 1181 | */ |
| 1182 | protected void changeStateFromPress(boolean buttonOn) { |
| 1183 | mState = buttonOn ? State.On : State.Off; |
| 1184 | } |
| 1185 | |
| 1186 | abstract void onToggle(boolean on); |
| 1187 | |
| 1188 | public void updateState(State state) { |
| 1189 | mState = state; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | private class SilentModeToggleAction extends ToggleAction { |
| 1194 | public SilentModeToggleAction() { |
| 1195 | super(R.drawable.ic_audio_vol_mute, |
| 1196 | R.drawable.ic_audio_vol, |
| 1197 | R.string.global_action_toggle_silent_mode, |
| 1198 | R.string.global_action_silent_mode_on_status, |
| 1199 | R.string.global_action_silent_mode_off_status); |
| 1200 | } |
| 1201 | |
| 1202 | void onToggle(boolean on) { |
| 1203 | if (on) { |
| 1204 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); |
| 1205 | } else { |
| 1206 | mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | public boolean showDuringKeyguard() { |
| 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | public boolean showBeforeProvisioning() { |
| 1215 | return false; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | private static class SilentModeTriStateAction implements Action, View.OnClickListener { |
| 1220 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1221 | private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3}; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1222 | |
| 1223 | private final AudioManager mAudioManager; |
| 1224 | private final Handler mHandler; |
| 1225 | private final Context mContext; |
| 1226 | |
| 1227 | SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) { |
| 1228 | mAudioManager = audioManager; |
| 1229 | mHandler = handler; |
| 1230 | mContext = context; |
| 1231 | } |
| 1232 | |
| 1233 | private int ringerModeToIndex(int ringerMode) { |
| 1234 | // They just happen to coincide |
| 1235 | return ringerMode; |
| 1236 | } |
| 1237 | |
| 1238 | private int indexToRingerMode(int index) { |
| 1239 | // They just happen to coincide |
| 1240 | return index; |
| 1241 | } |
| 1242 | |
| 1243 | @Override |
| 1244 | public CharSequence getLabelForAccessibility(Context context) { |
| 1245 | return null; |
| 1246 | } |
| 1247 | |
| 1248 | public View create(Context context, View convertView, ViewGroup parent, |
| 1249 | LayoutInflater inflater) { |
| 1250 | View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false); |
| 1251 | |
| 1252 | int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode()); |
| 1253 | for (int i = 0; i < 3; i++) { |
| 1254 | View itemView = v.findViewById(ITEM_IDS[i]); |
| 1255 | itemView.setSelected(selectedIndex == i); |
| 1256 | // Set up click handler |
| 1257 | itemView.setTag(i); |
| 1258 | itemView.setOnClickListener(this); |
| 1259 | } |
| 1260 | return v; |
| 1261 | } |
| 1262 | |
| 1263 | public void onPress() { |
| 1264 | } |
| 1265 | |
| 1266 | public boolean showDuringKeyguard() { |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | public boolean showBeforeProvisioning() { |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
| 1274 | public boolean isEnabled() { |
| 1275 | return true; |
| 1276 | } |
| 1277 | |
| 1278 | void willCreate() { |
| 1279 | } |
| 1280 | |
| 1281 | public void onClick(View v) { |
| 1282 | if (!(v.getTag() instanceof Integer)) return; |
| 1283 | |
| 1284 | int index = (Integer) v.getTag(); |
| 1285 | mAudioManager.setRingerMode(indexToRingerMode(index)); |
| 1286 | mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 1291 | public void onReceive(Context context, Intent intent) { |
| 1292 | String action = intent.getAction(); |
| 1293 | if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action) |
| 1294 | || Intent.ACTION_SCREEN_OFF.equals(action)) { |
| 1295 | String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); |
| 1296 | if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) { |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 1297 | mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason)); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1298 | } |
| 1299 | } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) { |
| 1300 | // Airplane mode can be changed after ECM exits if airplane toggle button |
| 1301 | // is pressed during ECM mode |
| 1302 | if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) && |
| 1303 | mIsWaitingForEcmExit) { |
| 1304 | mIsWaitingForEcmExit = false; |
| 1305 | changeAirplaneModeSystemSetting(true); |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | }; |
| 1310 | |
| 1311 | PhoneStateListener mPhoneStateListener = new PhoneStateListener() { |
| 1312 | @Override |
| 1313 | public void onServiceStateChanged(ServiceState serviceState) { |
| 1314 | if (!mHasTelephony) return; |
| 1315 | final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF; |
| 1316 | mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1317 | mAirplaneModeOn.updateState(mAirplaneState); |
| 1318 | mAdapter.notifyDataSetChanged(); |
| 1319 | } |
| 1320 | }; |
| 1321 | |
| 1322 | private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() { |
| 1323 | @Override |
| 1324 | public void onReceive(Context context, Intent intent) { |
| 1325 | if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { |
| 1326 | mHandler.sendEmptyMessage(MESSAGE_REFRESH); |
| 1327 | } |
| 1328 | } |
| 1329 | }; |
| 1330 | |
| 1331 | private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { |
| 1332 | @Override |
| 1333 | public void onChange(boolean selfChange) { |
| 1334 | onAirplaneModeChanged(); |
| 1335 | } |
| 1336 | }; |
| 1337 | |
| 1338 | private static final int MESSAGE_DISMISS = 0; |
| 1339 | private static final int MESSAGE_REFRESH = 1; |
| 1340 | private static final int MESSAGE_SHOW = 2; |
| 1341 | private static final int DIALOG_DISMISS_DELAY = 300; // ms |
| 1342 | |
| 1343 | private Handler mHandler = new Handler() { |
| 1344 | public void handleMessage(Message msg) { |
| 1345 | switch (msg.what) { |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1346 | case MESSAGE_DISMISS: |
| 1347 | if (mDialog != null) { |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 1348 | if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) { |
| 1349 | mDialog.dismissImmediately(); |
| 1350 | } else { |
| 1351 | mDialog.dismiss(); |
| 1352 | } |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1353 | mDialog = null; |
| 1354 | } |
| 1355 | break; |
| 1356 | case MESSAGE_REFRESH: |
| 1357 | refreshSilentMode(); |
| 1358 | mAdapter.notifyDataSetChanged(); |
| 1359 | break; |
| 1360 | case MESSAGE_SHOW: |
| 1361 | handleShow(); |
| 1362 | break; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | }; |
| 1366 | |
| 1367 | private void onAirplaneModeChanged() { |
| 1368 | // Let the service state callbacks handle the state. |
| 1369 | if (mHasTelephony) return; |
| 1370 | |
| 1371 | boolean airplaneModeOn = Settings.Global.getInt( |
| 1372 | mContext.getContentResolver(), |
| 1373 | Settings.Global.AIRPLANE_MODE_ON, |
| 1374 | 0) == 1; |
| 1375 | mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1376 | mAirplaneModeOn.updateState(mAirplaneState); |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Change the airplane mode system setting |
| 1381 | */ |
| 1382 | private void changeAirplaneModeSystemSetting(boolean on) { |
| 1383 | Settings.Global.putInt( |
| 1384 | mContext.getContentResolver(), |
| 1385 | Settings.Global.AIRPLANE_MODE_ON, |
| 1386 | on ? 1 : 0); |
| 1387 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 1388 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
| 1389 | intent.putExtra("state", on); |
| 1390 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1391 | if (!mHasTelephony) { |
| 1392 | mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off; |
| 1393 | } |
| 1394 | } |
| 1395 | |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1396 | private static final class ActionsDialog extends Dialog implements DialogInterface, |
| 1397 | ColorExtractor.OnColorsChangedListener { |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1398 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1399 | private final Context mContext; |
| 1400 | private final MyAdapter mAdapter; |
| 1401 | private final LinearLayout mListView; |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1402 | private final FrameLayout mSeparatedView; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1403 | private final HardwareUiLayout mHardwareLayout; |
| 1404 | private final OnClickListener mClickListener; |
| 1405 | private final OnItemLongClickListener mLongClickListener; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1406 | private final GradientDrawable mGradientDrawable; |
| 1407 | private final ColorExtractor mColorExtractor; |
| 1408 | private boolean mKeyguardShowing; |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1409 | private boolean mShouldDisplaySeparatedButton; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1410 | |
| 1411 | public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter, |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1412 | OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) { |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1413 | super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions); |
Lucas Dupin | 448786c | 2017-07-24 17:44:25 -0700 | [diff] [blame] | 1414 | mContext = context; |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1415 | mAdapter = adapter; |
| 1416 | mClickListener = clickListener; |
| 1417 | mLongClickListener = longClickListener; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1418 | mGradientDrawable = new GradientDrawable(mContext); |
Lucas Dupin | 1ead7fc | 2017-05-24 14:14:44 -0700 | [diff] [blame] | 1419 | mColorExtractor = Dependency.get(SysuiColorExtractor.class); |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1420 | mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton; |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1421 | |
| 1422 | // Window initialization |
| 1423 | Window window = getWindow(); |
| 1424 | window.requestFeature(Window.FEATURE_NO_TITLE); |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 1425 | // Inflate the decor view, so the attributes below are not overwritten by the theme. |
| 1426 | window.getDecorView(); |
| 1427 | window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 1428 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 1429 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| 1430 | window.setLayout(MATCH_PARENT, MATCH_PARENT); |
| 1431 | window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); |
Alison Cichowlas | 4f19f4a | 2017-07-25 10:56:16 -0400 | [diff] [blame] | 1432 | window.addFlags( |
| 1433 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1434 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
Adrian Roos | edfab3b | 2018-03-08 18:39:20 +0100 | [diff] [blame] | 1435 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1436 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 1437 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 1438 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 1439 | window.setBackgroundDrawable(mGradientDrawable); |
| 1440 | window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); |
| 1441 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1442 | setContentView(com.android.systemui.R.layout.global_actions_wrapped); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1443 | mListView = findViewById(android.R.id.list); |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1444 | mSeparatedView = findViewById(com.android.systemui.R.id.separated_button); |
| 1445 | if (!mShouldDisplaySeparatedButton) { |
| 1446 | mSeparatedView.setVisibility(View.GONE); |
| 1447 | } |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1448 | mHardwareLayout = HardwareUiLayout.get(mListView); |
| 1449 | mHardwareLayout.setOutsideTouchListener(view -> dismiss()); |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1450 | mHardwareLayout.setHasSeparatedButton(mShouldDisplaySeparatedButton); |
Phil Weaver | 8583ae8 | 2018-02-13 11:01:24 -0800 | [diff] [blame] | 1451 | setTitle(R.string.global_actions); |
Phil Weaver | 9054e09 | 2018-04-27 16:28:50 -0700 | [diff] [blame] | 1452 | mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() { |
| 1453 | @Override |
| 1454 | public boolean dispatchPopulateAccessibilityEvent( |
| 1455 | View host, AccessibilityEvent event) { |
| 1456 | // Populate the title here, just as Activity does |
| 1457 | event.getText().add(mContext.getString(R.string.global_actions)); |
| 1458 | return true; |
| 1459 | } |
| 1460 | }); |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1461 | } |
| 1462 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1463 | private void updateList() { |
| 1464 | mListView.removeAllViews(); |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1465 | mSeparatedView.removeAllViews(); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1466 | for (int i = 0; i < mAdapter.getCount(); i++) { |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1467 | ViewGroup parentView = mShouldDisplaySeparatedButton && i == mAdapter.getCount() - 1 |
| 1468 | ? mSeparatedView : mListView; |
| 1469 | View v = mAdapter.getView(i, null, parentView); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1470 | final int pos = i; |
| 1471 | v.setOnClickListener(view -> mClickListener.onClick(this, pos)); |
| 1472 | v.setOnLongClickListener(view -> |
| 1473 | mLongClickListener.onItemLongClick(null, v, pos, 0)); |
Wesley.CW Wang | 00e2fcf | 2018-06-15 16:24:57 +0800 | [diff] [blame] | 1474 | parentView.addView(v); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1475 | } |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | @Override |
| 1479 | protected void onStart() { |
| 1480 | super.setCanceledOnTouchOutside(true); |
| 1481 | super.onStart(); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1482 | updateList(); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1483 | |
| 1484 | Point displaySize = new Point(); |
| 1485 | mContext.getDisplay().getRealSize(displaySize); |
| 1486 | mColorExtractor.addOnColorsChangedListener(this); |
| 1487 | mGradientDrawable.setScreenSize(displaySize.x, displaySize.y); |
| 1488 | GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ? |
| 1489 | WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM); |
Lucas Dupin | b5f59fe | 2017-09-14 17:09:39 -0700 | [diff] [blame] | 1490 | updateColors(colors, false /* animate */); |
| 1491 | } |
| 1492 | |
| 1493 | /** |
| 1494 | * Updates background and system bars according to current GradientColors. |
| 1495 | * @param colors Colors and hints to use. |
| 1496 | * @param animate Interpolates gradient if true, just sets otherwise. |
| 1497 | */ |
| 1498 | private void updateColors(GradientColors colors, boolean animate) { |
| 1499 | mGradientDrawable.setColors(colors, animate); |
| 1500 | View decorView = getWindow().getDecorView(); |
| 1501 | if (colors.supportsDarkText()) { |
| 1502 | decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR | |
| 1503 | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); |
| 1504 | } else { |
| 1505 | decorView.setSystemUiVisibility(0); |
| 1506 | } |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | @Override |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1510 | protected void onStop() { |
| 1511 | super.onStop(); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1512 | mColorExtractor.removeOnColorsChangedListener(this); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | public void show() { |
| 1517 | super.show(); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1518 | mGradientDrawable.setAlpha(0); |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1519 | mHardwareLayout.setTranslationX(getAnimTranslation()); |
| 1520 | mHardwareLayout.setAlpha(0); |
| 1521 | mHardwareLayout.animate() |
| 1522 | .alpha(1) |
| 1523 | .translationX(0) |
| 1524 | .setDuration(300) |
Lucas Dupin | de9db42 | 2017-07-19 17:15:41 -0700 | [diff] [blame] | 1525 | .setInterpolator(Interpolators.FAST_OUT_SLOW_IN) |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1526 | .setUpdateListener(animation -> { |
| 1527 | int alpha = (int) ((Float) animation.getAnimatedValue() |
| 1528 | * ScrimController.GRADIENT_SCRIM_ALPHA * 255); |
| 1529 | mGradientDrawable.setAlpha(alpha); |
| 1530 | }) |
Jason Monk | a7af3b6 | 2017-07-07 11:35:13 -0400 | [diff] [blame] | 1531 | .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus()) |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1532 | .start(); |
| 1533 | } |
| 1534 | |
| 1535 | @Override |
| 1536 | public void dismiss() { |
| 1537 | mHardwareLayout.setTranslationX(0); |
| 1538 | mHardwareLayout.setAlpha(1); |
| 1539 | mHardwareLayout.animate() |
| 1540 | .alpha(0) |
| 1541 | .translationX(getAnimTranslation()) |
| 1542 | .setDuration(300) |
| 1543 | .withEndAction(() -> super.dismiss()) |
| 1544 | .setInterpolator(new LogAccelerateInterpolator()) |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1545 | .setUpdateListener(animation -> { |
| 1546 | int alpha = (int) ((1f - (Float) animation.getAnimatedValue()) |
| 1547 | * ScrimController.GRADIENT_SCRIM_ALPHA * 255); |
| 1548 | mGradientDrawable.setAlpha(alpha); |
| 1549 | }) |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1550 | .start(); |
| 1551 | } |
| 1552 | |
Lucas Dupin | 1d4a579 | 2018-04-02 15:14:59 -0700 | [diff] [blame] | 1553 | void dismissImmediately() { |
| 1554 | super.dismiss(); |
| 1555 | } |
| 1556 | |
Jason Monk | 16fbd9d | 2017-04-27 14:28:49 -0400 | [diff] [blame] | 1557 | private float getAnimTranslation() { |
| 1558 | return getContext().getResources().getDimension( |
| 1559 | com.android.systemui.R.dimen.global_actions_panel_width) / 2; |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | @Override |
Lucas Dupin | 7aaa353 | 2017-05-28 08:51:07 -0700 | [diff] [blame] | 1563 | public void onColorsChanged(ColorExtractor extractor, int which) { |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1564 | if (mKeyguardShowing) { |
| 1565 | if ((WallpaperManager.FLAG_LOCK & which) != 0) { |
Lucas Dupin | b5f59fe | 2017-09-14 17:09:39 -0700 | [diff] [blame] | 1566 | updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK), |
| 1567 | true /* animate */); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } else { |
| 1570 | if ((WallpaperManager.FLAG_SYSTEM & which) != 0) { |
Lucas Dupin | b5f59fe | 2017-09-14 17:09:39 -0700 | [diff] [blame] | 1571 | updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM), |
| 1572 | true /* animate */); |
Lucas Dupin | c1cc759 | 2017-05-22 15:56:16 -0700 | [diff] [blame] | 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | public void setKeyguardShowing(boolean keyguardShowing) { |
| 1578 | mKeyguardShowing = keyguardShowing; |
| 1579 | } |
Jason Monk | 361915c | 2017-03-21 20:33:59 -0400 | [diff] [blame] | 1580 | } |
| 1581 | } |