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