blob: aaf6db85f92a5aead17636749c24c3f77ec5d577 [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
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
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Steve Elliott4c868852019-03-14 16:25:41 -040027import android.app.PendingIntent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040028import android.app.StatusBarManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070029import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000030import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010031import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040032import android.content.BroadcastReceiver;
33import android.content.Context;
34import android.content.DialogInterface;
35import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.UserInfo;
38import android.database.ContentObserver;
39import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040042import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040044import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.os.Message;
46import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.os.UserHandle;
50import android.os.UserManager;
51import android.os.Vibrator;
52import android.provider.Settings;
53import android.service.dreams.DreamService;
54import android.service.dreams.IDreamManager;
55import android.telephony.PhoneStateListener;
56import android.telephony.ServiceState;
57import android.telephony.TelephonyManager;
58import android.text.TextUtils;
59import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080060import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040061import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070062import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040063import android.view.LayoutInflater;
64import android.view.View;
65import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070066import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040067import android.view.WindowManager;
68import android.view.WindowManagerGlobal;
69import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050070import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.ImageView;
72import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.TextView;
74
Charles He9851a8d2017-10-10 17:31:30 +010075import com.android.internal.R;
76import com.android.internal.colorextraction.ColorExtractor;
77import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070078import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010079import com.android.internal.logging.MetricsLogger;
80import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040081import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010082import com.android.internal.telephony.TelephonyIntents;
83import com.android.internal.telephony.TelephonyProperties;
84import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040085import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050086import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040087import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.internal.widget.LockPatternUtils;
89import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050091import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050092import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010093import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040094import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010095import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050096import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050097import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -040098import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070099import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400100import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800101import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500102import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500103import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700104
Jason Monk361915c2017-03-21 20:33:59 -0400105import java.util.ArrayList;
106import java.util.List;
107
108/**
109 * Helper to show the global actions dialog. Each item is an {@link Action} that
110 * may show depending on whether the keyguard is showing, and whether the device
111 * is provisioned.
112 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500113public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
114 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400115
116 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
117 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700118 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400119
120 private static final String TAG = "GlobalActionsDialog";
121
122 private static final boolean SHOW_SILENT_TOGGLE = true;
123
124 /* Valid settings for global actions keys.
125 * see config.xml config_globalActionList */
126 private static final String GLOBAL_ACTION_KEY_POWER = "power";
127 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
128 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
129 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
130 private static final String GLOBAL_ACTION_KEY_USERS = "users";
131 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
132 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
133 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
134 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
135 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000136 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800137 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500138 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400139
140 private final Context mContext;
141 private final GlobalActionsManager mWindowManagerFuncs;
142 private final AudioManager mAudioManager;
143 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000144 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800145 private final LockPatternUtils mLockPatternUtils;
146 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400147
148 private ArrayList<Action> mItems;
149 private ActionsDialog mDialog;
150
151 private Action mSilentModeAction;
152 private ToggleAction mAirplaneModeOn;
153
154 private MyAdapter mAdapter;
155
156 private boolean mKeyguardShowing = false;
157 private boolean mDeviceProvisioned = false;
158 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
159 private boolean mIsWaitingForEcmExit = false;
160 private boolean mHasTelephony;
161 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000162 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800163 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400164 private final boolean mShowSilentToggle;
165 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500166 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400167 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400168 private final ActivityStarter mActivityStarter;
169 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500170
Jason Monk361915c2017-03-21 20:33:59 -0400171 /**
172 * @param context everything needs a context :(
173 */
174 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700175 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400176 mWindowManagerFuncs = windowManagerFuncs;
177 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
178 mDreamManager = IDreamManager.Stub.asInterface(
179 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000180 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
181 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800182 mLockPatternUtils = new LockPatternUtils(mContext);
183 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400184
185 // receive broadcasts
186 IntentFilter filter = new IntentFilter();
187 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
188 filter.addAction(Intent.ACTION_SCREEN_OFF);
189 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
190 context.registerReceiver(mBroadcastReceiver, filter);
191
192 ConnectivityManager cm = (ConnectivityManager)
193 context.getSystemService(Context.CONNECTIVITY_SERVICE);
194 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
195
196 // get notified of phone state changes
197 TelephonyManager telephonyManager =
198 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
199 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
200 mContext.getContentResolver().registerContentObserver(
201 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
202 mAirplaneModeObserver);
203 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
204 mHasVibrator = vibrator != null && vibrator.hasVibrator();
205
206 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
207 R.bool.config_useFixedVolume);
208
209 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500210 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400211 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700212
213 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500214
Steve Elliott4c868852019-03-14 16:25:41 -0400215 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400216 KeyguardStateController keyguardStateController =
217 Dependency.get(KeyguardStateController.class);
218 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
219 @Override
220 public void onUnlockedChanged() {
221 if (mDialog != null && mDialog.mPanelController != null) {
222 boolean locked = !keyguardStateController.canDismissLockScreen();
223 mDialog.mPanelController.onDeviceLockStateChanged(locked);
224 }
225 }
226 });
Jason Monk361915c2017-03-21 20:33:59 -0400227 }
228
229 /**
230 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400231 *
Jason Monk361915c2017-03-21 20:33:59 -0400232 * @param keyguardShowing True if keyguard is showing
233 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400234 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
235 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400236 mKeyguardShowing = keyguardShowing;
237 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400238 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400239 if (mDialog != null) {
240 mDialog.dismiss();
241 mDialog = null;
242 // Show delayed, so that the dismiss of the previous dialog completes
243 mHandler.sendEmptyMessage(MESSAGE_SHOW);
244 } else {
245 handleShow();
246 }
247 }
248
Charles He9851a8d2017-10-10 17:31:30 +0100249 /**
250 * Dismiss the global actions dialog, if it's currently shown
251 */
252 public void dismissDialog() {
253 mHandler.removeMessages(MESSAGE_DISMISS);
254 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
255 }
256
Jason Monk361915c2017-03-21 20:33:59 -0400257 private void awakenIfNecessary() {
258 if (mDreamManager != null) {
259 try {
260 if (mDreamManager.isDreaming()) {
261 mDreamManager.awaken();
262 }
263 } catch (RemoteException e) {
264 // we tried
265 }
266 }
267 }
268
269 private void handleShow() {
270 awakenIfNecessary();
271 mDialog = createDialog();
272 prepareDialog();
273
274 // If we only have 1 item and it's a simple press action, just do this action.
275 if (mAdapter.getCount() == 1
276 && mAdapter.getItem(0) instanceof SinglePressAction
277 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
278 ((SinglePressAction) mAdapter.getItem(0)).onPress();
279 } else {
280 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
281 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100282 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400283 mDialog.getWindow().setAttributes(attrs);
284 mDialog.show();
285 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400286 }
287 }
288
289 /**
290 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400291 *
Jason Monk361915c2017-03-21 20:33:59 -0400292 * @return A new dialog.
293 */
294 private ActionsDialog createDialog() {
295 // Simple toggle style if there's no vibrator, otherwise use a tri-state
296 if (!mHasVibrator) {
297 mSilentModeAction = new SilentModeToggleAction();
298 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700299 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400300 }
301 mAirplaneModeOn = new ToggleAction(
302 R.drawable.ic_lock_airplane_mode,
303 R.drawable.ic_lock_airplane_mode_off,
304 R.string.global_actions_toggle_airplane_mode,
305 R.string.global_actions_airplane_mode_on_status,
306 R.string.global_actions_airplane_mode_off_status) {
307
308 void onToggle(boolean on) {
309 if (mHasTelephony && Boolean.parseBoolean(
310 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
311 mIsWaitingForEcmExit = true;
312 // Launch ECM exit dialog
313 Intent ecmDialogIntent =
314 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
315 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
316 mContext.startActivity(ecmDialogIntent);
317 } else {
318 changeAirplaneModeSystemSetting(on);
319 }
320 }
321
322 @Override
323 protected void changeStateFromPress(boolean buttonOn) {
324 if (!mHasTelephony) return;
325
326 // In ECM mode airplane state cannot be changed
327 if (!(Boolean.parseBoolean(
328 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
329 mState = buttonOn ? State.TurningOn : State.TurningOff;
330 mAirplaneState = mState;
331 }
332 }
333
334 public boolean showDuringKeyguard() {
335 return true;
336 }
337
338 public boolean showBeforeProvisioning() {
339 return false;
340 }
341 };
342 onAirplaneModeChanged();
343
344 mItems = new ArrayList<Action>();
345 String[] defaultActions = mContext.getResources().getStringArray(
346 R.array.config_globalActionsList);
347
348 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000349 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800350 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400351 for (int i = 0; i < defaultActions.length; i++) {
352 String actionKey = defaultActions[i];
353 if (addedKeys.contains(actionKey)) {
354 // If we already have added this, don't add it again.
355 continue;
356 }
357 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
358 mItems.add(new PowerAction());
359 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
360 mItems.add(mAirplaneModeOn);
361 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
362 if (Settings.Global.getInt(mContext.getContentResolver(),
363 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
364 mItems.add(new BugReportAction());
365 }
366 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
367 if (mShowSilentToggle) {
368 mItems.add(mSilentModeAction);
369 }
370 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
371 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
372 addUsersToMenu(mItems);
373 }
374 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
375 mItems.add(getSettingsAction());
376 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700377 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
378 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800379 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700380 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800381 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700382 }
Jason Monk361915c2017-03-21 20:33:59 -0400383 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
384 mItems.add(getVoiceAssistAction());
385 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
386 mItems.add(getAssistAction());
387 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
388 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500389 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
390 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000391 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000392 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000393 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
394 mItems.add(new LogoutAction());
395 mHasLogoutButton = true;
396 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800397 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400398 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800399 mItems.add(new EmergencyDialerAction());
400 }
Jason Monk361915c2017-03-21 20:33:59 -0400401 } else {
402 Log.e(TAG, "Invalid global action key " + actionKey);
403 }
404 // Add here so we don't add more than one.
405 addedKeys.add(actionKey);
406 }
407
408 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400409 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400410 }
411
412 mAdapter = new MyAdapter();
413
Steve Elliott9b87a442019-03-05 10:24:16 -0500414 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400415 mPanelPlugin != null
416 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400417 new GlobalActionsPanelPlugin.Callbacks() {
418 @Override
419 public void dismissGlobalActionsMenu() {
420 if (mDialog != null) {
421 mDialog.dismiss();
422 }
423 }
424
425 @Override
426 public void startPendingIntentDismissingKeyguard(
427 PendingIntent intent) {
428 mActivityStarter
429 .startPendingIntentDismissingKeyguard(intent);
430 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400431 },
432 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500433 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400434
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400435 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400436 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700437 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400438
439 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800440 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400441
442 return dialog;
443 }
444
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800445 private boolean shouldDisplayLockdown() {
446 int userId = getCurrentUser().id;
447 // Lockdown is meaningless without a place to go.
448 if (!mKeyguardManager.isDeviceSecure(userId)) {
449 return false;
450 }
451
452 // Only show the lockdown button if the device isn't locked down (for whatever reason).
453 int state = mLockPatternUtils.getStrongAuthForUser(userId);
454 return (state == STRONG_AUTH_NOT_REQUIRED
455 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
456 }
457
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700458 @Override
459 public void onUiModeChanged() {
460 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700461 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400462 mDialog.refreshDialog();
463 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700464 }
465
466 public void destroy() {
467 Dependency.get(ConfigurationController.class).removeCallback(this);
468 }
469
Jason Monk361915c2017-03-21 20:33:59 -0400470 private final class PowerAction extends SinglePressAction implements LongPressAction {
471 private PowerAction() {
472 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400473 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400474 }
475
476 @Override
477 public boolean onLongPress() {
478 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
479 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
480 mWindowManagerFuncs.reboot(true);
481 return true;
482 }
483 return false;
484 }
485
486 @Override
487 public boolean showDuringKeyguard() {
488 return true;
489 }
490
491 @Override
492 public boolean showBeforeProvisioning() {
493 return true;
494 }
495
496 @Override
497 public void onPress() {
498 // shutdown by making sure radio and power are handled accordingly.
499 mWindowManagerFuncs.shutdown();
500 }
501 }
502
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400503 private abstract class EmergencyAction extends SinglePressAction {
504 EmergencyAction(int iconResId, int messageResId) {
505 super(iconResId, messageResId);
506 }
507
508 @Override
509 public boolean shouldBeSeparated() {
510 return shouldUseSeparatedView();
511 }
512
513 @Override
514 public View create(
515 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
516 View v = super.create(context, convertView, parent, inflater);
517 int textColor;
518 if (shouldBeSeparated()) {
519 textColor = v.getResources().getColor(
520 com.android.systemui.R.color.global_actions_alert_text);
521 } else {
522 textColor = v.getResources().getColor(
523 com.android.systemui.R.color.global_actions_text);
524 }
525 TextView messageView = v.findViewById(R.id.message);
526 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400527 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400528 ImageView icon = (ImageView) v.findViewById(R.id.icon);
529 icon.getDrawable().setTint(textColor);
530 return v;
531 }
532
533 @Override
534 public boolean showDuringKeyguard() {
535 return true;
536 }
537
538 @Override
539 public boolean showBeforeProvisioning() {
540 return true;
541 }
542 }
543
544 private class EmergencyAffordanceAction extends EmergencyAction {
545 EmergencyAffordanceAction() {
546 super(R.drawable.emergency_icon,
547 R.string.global_action_emergency);
548 }
549
550 @Override
551 public void onPress() {
552 mEmergencyAffordanceManager.performEmergencyCall();
553 }
554 }
555
556 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800557 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400558 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800559 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800560 }
561
562 @Override
563 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800564 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800565 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800566 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
567 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
568 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800569 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
570 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800571 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
572 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800573 }
574
Jason Monk361915c2017-03-21 20:33:59 -0400575 private final class RestartAction extends SinglePressAction implements LongPressAction {
576 private RestartAction() {
577 super(R.drawable.ic_restart, R.string.global_action_restart);
578 }
579
580 @Override
581 public boolean onLongPress() {
582 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
583 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
584 mWindowManagerFuncs.reboot(true);
585 return true;
586 }
587 return false;
588 }
589
590 @Override
591 public boolean showDuringKeyguard() {
592 return true;
593 }
594
595 @Override
596 public boolean showBeforeProvisioning() {
597 return true;
598 }
599
600 @Override
601 public void onPress() {
602 mWindowManagerFuncs.reboot(false);
603 }
604 }
605
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400606 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500607 public ScreenshotAction() {
608 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
609 }
610
611 @Override
612 public void onPress() {
613 // Add a little delay before executing, to give the
614 // dialog a chance to go away before it takes a
615 // screenshot.
616 // TODO: instead, omit global action dialog layer
617 mHandler.postDelayed(new Runnable() {
618 @Override
619 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400620 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500621 MetricsLogger.action(mContext,
622 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
623 }
624 }, 500);
625 }
626
627 @Override
628 public boolean showDuringKeyguard() {
629 return true;
630 }
631
632 @Override
633 public boolean showBeforeProvisioning() {
634 return false;
635 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400636
637 @Override
638 public boolean onLongPress() {
639 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
640 mScreenRecordHelper.launchRecordPrompt();
641 } else {
642 onPress();
643 }
644 return true;
645 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500646 }
647
Jason Monk361915c2017-03-21 20:33:59 -0400648 private class BugReportAction extends SinglePressAction implements LongPressAction {
649
650 public BugReportAction() {
651 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
652 }
653
654 @Override
655 public void onPress() {
656 // don't actually trigger the bugreport if we are running stability
657 // tests via monkey
658 if (ActivityManager.isUserAMonkey()) {
659 return;
660 }
661 // Add a little delay before executing, to give the
662 // dialog a chance to go away before it takes a
663 // screenshot.
664 mHandler.postDelayed(new Runnable() {
665 @Override
666 public void run() {
667 try {
668 // Take an "interactive" bugreport.
669 MetricsLogger.action(mContext,
670 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
671 ActivityManager.getService().requestBugReport(
672 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
673 } catch (RemoteException e) {
674 }
675 }
676 }, 500);
677 }
678
679 @Override
680 public boolean onLongPress() {
681 // don't actually trigger the bugreport if we are running stability
682 // tests via monkey
683 if (ActivityManager.isUserAMonkey()) {
684 return false;
685 }
686 try {
687 // Take a "full" bugreport.
688 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
689 ActivityManager.getService().requestBugReport(
690 ActivityManager.BUGREPORT_OPTION_FULL);
691 } catch (RemoteException e) {
692 }
693 return false;
694 }
695
696 public boolean showDuringKeyguard() {
697 return true;
698 }
699
700 @Override
701 public boolean showBeforeProvisioning() {
702 return false;
703 }
Jason Monk361915c2017-03-21 20:33:59 -0400704 }
705
Alex Chau04458852017-11-27 18:21:23 +0000706 private final class LogoutAction extends SinglePressAction {
707 private LogoutAction() {
708 super(R.drawable.ic_logout, R.string.global_action_logout);
709 }
710
711 @Override
712 public boolean showDuringKeyguard() {
713 return true;
714 }
715
716 @Override
717 public boolean showBeforeProvisioning() {
718 return false;
719 }
720
721 @Override
722 public void onPress() {
723 // Add a little delay before executing, to give the dialog a chance to go away before
724 // switching user
725 mHandler.postDelayed(() -> {
726 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000727 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000728 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000729 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000730 } catch (RemoteException re) {
731 Log.e(TAG, "Couldn't logout user " + re);
732 }
733 }, 500);
734 }
735 }
736
Jason Monk361915c2017-03-21 20:33:59 -0400737 private Action getSettingsAction() {
738 return new SinglePressAction(R.drawable.ic_settings,
739 R.string.global_action_settings) {
740
741 @Override
742 public void onPress() {
743 Intent intent = new Intent(Settings.ACTION_SETTINGS);
744 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
745 mContext.startActivity(intent);
746 }
747
748 @Override
749 public boolean showDuringKeyguard() {
750 return true;
751 }
752
753 @Override
754 public boolean showBeforeProvisioning() {
755 return true;
756 }
757 };
758 }
759
Jason Monk361915c2017-03-21 20:33:59 -0400760 private Action getAssistAction() {
761 return new SinglePressAction(R.drawable.ic_action_assist_focused,
762 R.string.global_action_assist) {
763 @Override
764 public void onPress() {
765 Intent intent = new Intent(Intent.ACTION_ASSIST);
766 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
767 mContext.startActivity(intent);
768 }
769
770 @Override
771 public boolean showDuringKeyguard() {
772 return true;
773 }
774
775 @Override
776 public boolean showBeforeProvisioning() {
777 return true;
778 }
779 };
780 }
781
782 private Action getVoiceAssistAction() {
783 return new SinglePressAction(R.drawable.ic_voice_search,
784 R.string.global_action_voice_assist) {
785 @Override
786 public void onPress() {
787 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
788 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
789 mContext.startActivity(intent);
790 }
791
792 @Override
793 public boolean showDuringKeyguard() {
794 return true;
795 }
796
797 @Override
798 public boolean showBeforeProvisioning() {
799 return true;
800 }
801 };
802 }
803
804 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400805 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400806 R.string.global_action_lockdown) {
807
808 @Override
809 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700810 new LockPatternUtils(mContext)
811 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
812 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400813 try {
814 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100815 // Lock profiles (if any) on the background thread.
816 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
817 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400818 } catch (RemoteException e) {
819 Log.e(TAG, "Error while trying to lock device.", e);
820 }
821 }
822
823 @Override
824 public boolean showDuringKeyguard() {
825 return true;
826 }
827
828 @Override
829 public boolean showBeforeProvisioning() {
830 return false;
831 }
832 };
833 }
834
Pavel Grafov059021b2018-05-02 13:44:46 +0100835 private void lockProfiles() {
836 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
837 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
838 final int currentUserId = getCurrentUser().id;
839 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
840 for (final int id : profileIds) {
841 if (id != currentUserId) {
842 tm.setDeviceLockedForUser(id, true);
843 }
844 }
845 }
846
Jason Monk361915c2017-03-21 20:33:59 -0400847 private UserInfo getCurrentUser() {
848 try {
849 return ActivityManager.getService().getCurrentUser();
850 } catch (RemoteException re) {
851 return null;
852 }
853 }
854
855 private boolean isCurrentUserOwner() {
856 UserInfo currentUser = getCurrentUser();
857 return currentUser == null || currentUser.isPrimary();
858 }
859
860 private void addUsersToMenu(ArrayList<Action> items) {
861 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
862 if (um.isUserSwitcherEnabled()) {
863 List<UserInfo> users = um.getUsers();
864 UserInfo currentUser = getCurrentUser();
865 for (final UserInfo user : users) {
866 if (user.supportsSwitchToByUser()) {
867 boolean isCurrentUser = currentUser == null
868 ? user.id == 0 : (currentUser.id == user.id);
869 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
870 : null;
871 SinglePressAction switchToUser = new SinglePressAction(
872 R.drawable.ic_menu_cc, icon,
873 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400874 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400875 public void onPress() {
876 try {
877 ActivityManager.getService().switchUser(user.id);
878 } catch (RemoteException re) {
879 Log.e(TAG, "Couldn't switch user " + re);
880 }
881 }
882
883 public boolean showDuringKeyguard() {
884 return true;
885 }
886
887 public boolean showBeforeProvisioning() {
888 return false;
889 }
890 };
891 items.add(switchToUser);
892 }
893 }
894 }
895 }
896
897 private void prepareDialog() {
898 refreshSilentMode();
899 mAirplaneModeOn.updateState(mAirplaneState);
900 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400901 if (mShowSilentToggle) {
902 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
903 mContext.registerReceiver(mRingerModeReceiver, filter);
904 }
905 }
906
907 private void refreshSilentMode() {
908 if (!mHasVibrator) {
909 final boolean silentModeOn =
910 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400911 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400912 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
913 }
914 }
915
916 /** {@inheritDoc} */
917 public void onDismiss(DialogInterface dialog) {
918 mWindowManagerFuncs.onGlobalActionsHidden();
919 if (mShowSilentToggle) {
920 try {
921 mContext.unregisterReceiver(mRingerModeReceiver);
922 } catch (IllegalArgumentException ie) {
923 // ignore this
924 Log.w(TAG, ie);
925 }
926 }
927 }
928
929 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800930 public void onShow(DialogInterface dialog) {
931 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
932 }
933
Jason Monk361915c2017-03-21 20:33:59 -0400934 /**
935 * The adapter used for the list within the global actions dialog, taking
936 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400937 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
938 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400939 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
940 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500941 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400942 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400943 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400944 for (int i = 0; i < mItems.size(); i++) {
945 final Action action = mItems.get(i);
946
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400947 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
948 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400949 }
Jason Monk361915c2017-03-21 20:33:59 -0400950 }
951 return count;
952 }
953
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400954 private boolean shouldBeShown(Action action) {
955 if (mKeyguardShowing && !action.showDuringKeyguard()) {
956 return false;
957 }
958 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
959 return false;
960 }
961 return true;
962 }
963
964 @Override
965 public int countSeparatedItems() {
966 return countItems(true);
967 }
968
969 @Override
970 public int countListItems() {
971 return countItems(false);
972 }
973
974 @Override
975 public int getCount() {
976 return countSeparatedItems() + countListItems();
977 }
978
Jason Monk361915c2017-03-21 20:33:59 -0400979 @Override
980 public boolean isEnabled(int position) {
981 return getItem(position).isEnabled();
982 }
983
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500984 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400985 public boolean areAllItemsEnabled() {
986 return false;
987 }
988
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500989 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400990 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400991 int filteredPos = 0;
992 for (int i = 0; i < mItems.size(); i++) {
993 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400994 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400995 continue;
996 }
997 if (filteredPos == position) {
998 return action;
999 }
1000 filteredPos++;
1001 }
1002
1003 throw new IllegalArgumentException("position " + position
1004 + " out of range of showable actions"
1005 + ", filtered count=" + getCount()
1006 + ", keyguardshowing=" + mKeyguardShowing
1007 + ", provisioned=" + mDeviceProvisioned);
1008 }
1009
1010
1011 public long getItemId(int position) {
1012 return position;
1013 }
1014
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001015 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001016 public View getView(int position, View convertView, ViewGroup parent) {
1017 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001018 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001019 view.setOnClickListener(v -> onClickItem(position));
1020 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001021 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001022 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001023
1024 @Override
1025 public boolean onLongClickItem(int position) {
1026 final Action action = mAdapter.getItem(position);
1027 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001028 if (mDialog != null) {
1029 mDialog.dismiss();
1030 } else {
1031 Log.w(TAG, "Action long-clicked while mDialog is null.");
1032 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001033 return ((LongPressAction) action).onLongPress();
1034 }
1035 return false;
1036 }
1037
1038 @Override
1039 public void onClickItem(int position) {
1040 Action item = mAdapter.getItem(position);
1041 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001042 if (mDialog != null) {
1043 mDialog.dismiss();
1044 } else {
1045 Log.w(TAG, "Action clicked while mDialog is null.");
1046 }
1047 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001048 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001049 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001050
1051 @Override
1052 public boolean shouldBeSeparated(int position) {
1053 return getItem(position).shouldBeSeparated();
1054 }
Jason Monk361915c2017-03-21 20:33:59 -04001055 }
1056
1057 // note: the scheme below made more sense when we were planning on having
1058 // 8 different things in the global actions dialog. seems overkill with
1059 // only 3 items now, but may as well keep this flexible approach so it will
1060 // be easy should someone decide at the last minute to include something
1061 // else, such as 'enable wifi', or 'enable bluetooth'
1062
1063 /**
1064 * What each item in the global actions dialog must be able to support.
1065 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001066 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001067 /**
1068 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001069 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001070 */
1071 CharSequence getLabelForAccessibility(Context context);
1072
1073 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1074
1075 void onPress();
1076
1077 /**
1078 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001079 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001080 */
1081 boolean showDuringKeyguard();
1082
1083 /**
1084 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001085 * device is provisioned.onlongpress
1086 *
Jason Monk361915c2017-03-21 20:33:59 -04001087 */
1088 boolean showBeforeProvisioning();
1089
1090 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001091
1092 default boolean shouldBeSeparated() {
1093 return false;
1094 }
Jason Monk361915c2017-03-21 20:33:59 -04001095 }
1096
1097 /**
1098 * An action that also supports long press.
1099 */
1100 private interface LongPressAction extends Action {
1101 boolean onLongPress();
1102 }
1103
1104 /**
1105 * A single press action maintains no state, just responds to a press
1106 * and takes an action.
1107 */
1108 private static abstract class SinglePressAction implements Action {
1109 private final int mIconResId;
1110 private final Drawable mIcon;
1111 private final int mMessageResId;
1112 private final CharSequence mMessage;
1113
1114 protected SinglePressAction(int iconResId, int messageResId) {
1115 mIconResId = iconResId;
1116 mMessageResId = messageResId;
1117 mMessage = null;
1118 mIcon = null;
1119 }
1120
1121 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1122 mIconResId = iconResId;
1123 mMessageResId = 0;
1124 mMessage = message;
1125 mIcon = icon;
1126 }
1127
1128 public boolean isEnabled() {
1129 return true;
1130 }
1131
1132 public String getStatus() {
1133 return null;
1134 }
1135
1136 abstract public void onPress();
1137
1138 public CharSequence getLabelForAccessibility(Context context) {
1139 if (mMessage != null) {
1140 return mMessage;
1141 } else {
1142 return context.getString(mMessageResId);
1143 }
1144 }
1145
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001146 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001147 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001148 }
1149
Jason Monk361915c2017-03-21 20:33:59 -04001150 public View create(
1151 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001152 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001153 false);
Jason Monk361915c2017-03-21 20:33:59 -04001154
1155 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1156 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001157 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001158
1159 TextView statusView = (TextView) v.findViewById(R.id.status);
1160 final String status = getStatus();
1161 if (!TextUtils.isEmpty(status)) {
1162 statusView.setText(status);
1163 } else {
1164 statusView.setVisibility(View.GONE);
1165 }
1166 if (mIcon != null) {
1167 icon.setImageDrawable(mIcon);
1168 icon.setScaleType(ScaleType.CENTER_CROP);
1169 } else if (mIconResId != 0) {
1170 icon.setImageDrawable(context.getDrawable(mIconResId));
1171 }
1172 if (mMessage != null) {
1173 messageView.setText(mMessage);
1174 } else {
1175 messageView.setText(mMessageResId);
1176 }
1177
1178 return v;
1179 }
1180 }
1181
1182 /**
1183 * A toggle action knows whether it is on or off, and displays an icon
1184 * and status message accordingly.
1185 */
1186 private static abstract class ToggleAction implements Action {
1187
1188 enum State {
1189 Off(false),
1190 TurningOn(true),
1191 TurningOff(true),
1192 On(false);
1193
1194 private final boolean inTransition;
1195
1196 State(boolean intermediate) {
1197 inTransition = intermediate;
1198 }
1199
1200 public boolean inTransition() {
1201 return inTransition;
1202 }
1203 }
1204
1205 protected State mState = State.Off;
1206
1207 // prefs
1208 protected int mEnabledIconResId;
1209 protected int mDisabledIconResid;
1210 protected int mMessageResId;
1211 protected int mEnabledStatusMessageResId;
1212 protected int mDisabledStatusMessageResId;
1213
1214 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001215 * @param enabledIconResId The icon for when this action is on.
1216 * @param disabledIconResid The icon for when this action is off.
1217 * @param message The general information message, e.g 'Silent Mode'
1218 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001219 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1220 */
1221 public ToggleAction(int enabledIconResId,
1222 int disabledIconResid,
1223 int message,
1224 int enabledStatusMessageResId,
1225 int disabledStatusMessageResId) {
1226 mEnabledIconResId = enabledIconResId;
1227 mDisabledIconResid = disabledIconResid;
1228 mMessageResId = message;
1229 mEnabledStatusMessageResId = enabledStatusMessageResId;
1230 mDisabledStatusMessageResId = disabledStatusMessageResId;
1231 }
1232
1233 /**
1234 * Override to make changes to resource IDs just before creating the
1235 * View.
1236 */
1237 void willCreate() {
1238
1239 }
1240
1241 @Override
1242 public CharSequence getLabelForAccessibility(Context context) {
1243 return context.getString(mMessageResId);
1244 }
1245
1246 public View create(Context context, View convertView, ViewGroup parent,
1247 LayoutInflater inflater) {
1248 willCreate();
1249
1250 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001251 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001252
1253 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1254 TextView messageView = (TextView) v.findViewById(R.id.message);
1255 TextView statusView = (TextView) v.findViewById(R.id.status);
1256 final boolean enabled = isEnabled();
1257
1258 if (messageView != null) {
1259 messageView.setText(mMessageResId);
1260 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001261 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001262 }
1263
1264 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1265 if (icon != null) {
1266 icon.setImageDrawable(context.getDrawable(
1267 (on ? mEnabledIconResId : mDisabledIconResid)));
1268 icon.setEnabled(enabled);
1269 }
1270
1271 if (statusView != null) {
1272 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1273 statusView.setVisibility(View.VISIBLE);
1274 statusView.setEnabled(enabled);
1275 }
1276 v.setEnabled(enabled);
1277
1278 return v;
1279 }
1280
1281 public final void onPress() {
1282 if (mState.inTransition()) {
1283 Log.w(TAG, "shouldn't be able to toggle when in transition");
1284 return;
1285 }
1286
1287 final boolean nowOn = !(mState == State.On);
1288 onToggle(nowOn);
1289 changeStateFromPress(nowOn);
1290 }
1291
1292 public boolean isEnabled() {
1293 return !mState.inTransition();
1294 }
1295
1296 /**
1297 * Implementations may override this if their state can be in on of the intermediate
1298 * states until some notification is received (e.g airplane mode is 'turning off' until
1299 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001300 *
Jason Monk361915c2017-03-21 20:33:59 -04001301 * @param buttonOn Whether the button was turned on or off
1302 */
1303 protected void changeStateFromPress(boolean buttonOn) {
1304 mState = buttonOn ? State.On : State.Off;
1305 }
1306
1307 abstract void onToggle(boolean on);
1308
1309 public void updateState(State state) {
1310 mState = state;
1311 }
1312 }
1313
1314 private class SilentModeToggleAction extends ToggleAction {
1315 public SilentModeToggleAction() {
1316 super(R.drawable.ic_audio_vol_mute,
1317 R.drawable.ic_audio_vol,
1318 R.string.global_action_toggle_silent_mode,
1319 R.string.global_action_silent_mode_on_status,
1320 R.string.global_action_silent_mode_off_status);
1321 }
1322
1323 void onToggle(boolean on) {
1324 if (on) {
1325 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1326 } else {
1327 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1328 }
1329 }
1330
1331 public boolean showDuringKeyguard() {
1332 return true;
1333 }
1334
1335 public boolean showBeforeProvisioning() {
1336 return false;
1337 }
1338 }
1339
1340 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1341
Jason Monk16fbd9d2017-04-27 14:28:49 -04001342 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001343
1344 private final AudioManager mAudioManager;
1345 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001346
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001347 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001348 mAudioManager = audioManager;
1349 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001350 }
1351
1352 private int ringerModeToIndex(int ringerMode) {
1353 // They just happen to coincide
1354 return ringerMode;
1355 }
1356
1357 private int indexToRingerMode(int index) {
1358 // They just happen to coincide
1359 return index;
1360 }
1361
1362 @Override
1363 public CharSequence getLabelForAccessibility(Context context) {
1364 return null;
1365 }
1366
1367 public View create(Context context, View convertView, ViewGroup parent,
1368 LayoutInflater inflater) {
1369 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1370
1371 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1372 for (int i = 0; i < 3; i++) {
1373 View itemView = v.findViewById(ITEM_IDS[i]);
1374 itemView.setSelected(selectedIndex == i);
1375 // Set up click handler
1376 itemView.setTag(i);
1377 itemView.setOnClickListener(this);
1378 }
1379 return v;
1380 }
1381
1382 public void onPress() {
1383 }
1384
1385 public boolean showDuringKeyguard() {
1386 return true;
1387 }
1388
1389 public boolean showBeforeProvisioning() {
1390 return false;
1391 }
1392
1393 public boolean isEnabled() {
1394 return true;
1395 }
1396
1397 void willCreate() {
1398 }
1399
1400 public void onClick(View v) {
1401 if (!(v.getTag() instanceof Integer)) return;
1402
1403 int index = (Integer) v.getTag();
1404 mAudioManager.setRingerMode(indexToRingerMode(index));
1405 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1406 }
1407 }
1408
1409 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1410 public void onReceive(Context context, Intent intent) {
1411 String action = intent.getAction();
1412 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1413 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1414 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1415 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001416 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001417 }
1418 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1419 // Airplane mode can be changed after ECM exits if airplane toggle button
1420 // is pressed during ECM mode
1421 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1422 mIsWaitingForEcmExit) {
1423 mIsWaitingForEcmExit = false;
1424 changeAirplaneModeSystemSetting(true);
1425 }
1426 }
1427 }
1428 };
1429
1430 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1431 @Override
1432 public void onServiceStateChanged(ServiceState serviceState) {
1433 if (!mHasTelephony) return;
1434 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1435 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1436 mAirplaneModeOn.updateState(mAirplaneState);
1437 mAdapter.notifyDataSetChanged();
1438 }
1439 };
1440
1441 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1442 @Override
1443 public void onReceive(Context context, Intent intent) {
1444 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1445 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1446 }
1447 }
1448 };
1449
1450 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1451 @Override
1452 public void onChange(boolean selfChange) {
1453 onAirplaneModeChanged();
1454 }
1455 };
1456
1457 private static final int MESSAGE_DISMISS = 0;
1458 private static final int MESSAGE_REFRESH = 1;
1459 private static final int MESSAGE_SHOW = 2;
1460 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1461
1462 private Handler mHandler = new Handler() {
1463 public void handleMessage(Message msg) {
1464 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001465 case MESSAGE_DISMISS:
1466 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001467 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1468 mDialog.dismissImmediately();
1469 } else {
1470 mDialog.dismiss();
1471 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001472 mDialog = null;
1473 }
1474 break;
1475 case MESSAGE_REFRESH:
1476 refreshSilentMode();
1477 mAdapter.notifyDataSetChanged();
1478 break;
1479 case MESSAGE_SHOW:
1480 handleShow();
1481 break;
Jason Monk361915c2017-03-21 20:33:59 -04001482 }
1483 }
1484 };
1485
1486 private void onAirplaneModeChanged() {
1487 // Let the service state callbacks handle the state.
1488 if (mHasTelephony) return;
1489
1490 boolean airplaneModeOn = Settings.Global.getInt(
1491 mContext.getContentResolver(),
1492 Settings.Global.AIRPLANE_MODE_ON,
1493 0) == 1;
1494 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1495 mAirplaneModeOn.updateState(mAirplaneState);
1496 }
1497
1498 /**
1499 * Change the airplane mode system setting
1500 */
1501 private void changeAirplaneModeSystemSetting(boolean on) {
1502 Settings.Global.putInt(
1503 mContext.getContentResolver(),
1504 Settings.Global.AIRPLANE_MODE_ON,
1505 on ? 1 : 0);
1506 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1507 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1508 intent.putExtra("state", on);
1509 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1510 if (!mHasTelephony) {
1511 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1512 }
1513 }
1514
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001515 private static final class ActionsDialog extends Dialog implements DialogInterface,
1516 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001517
Jason Monk16fbd9d2017-04-27 14:28:49 -04001518 private final Context mContext;
1519 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001520 private final IStatusBarService mStatusBarService;
1521 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001522 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001523 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001524 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001525 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001526 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001527 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001528 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001529 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001530 private boolean mHadTopUi;
1531 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001532
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001533 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001534 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001535 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001536 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001537 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001538 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001539 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001540 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001541
1542 // Window initialization
1543 Window window = getWindow();
1544 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001545 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1546 window.getDecorView();
1547 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1548 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1549 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1550 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1551 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001552 window.addFlags(
1553 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001554 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001555 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001556 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1557 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1558 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001559 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001560 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001561
1562 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001563 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001564 }
1565
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001566 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001567 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001568 }
1569
1570 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001571 int rotation = RotationUtils.getRotation(mContext);
1572 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1573 if (rotation != RotationUtils.ROTATION_NONE) {
1574 if (rotationLocked) {
1575 if (mResetOrientationData == null) {
1576 mResetOrientationData = new ResetOrientationData();
1577 mResetOrientationData.locked = true;
1578 mResetOrientationData.rotation = rotation;
1579 }
1580
1581 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001582 // This call is posted so that the rotation does not change until post-layout,
1583 // otherwise onConfigurationChanged() may not get invoked.
1584 mGlobalActionsLayout.post(() ->
1585 RotationPolicy.setRotationLockAtAngle(
1586 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001587 }
1588 } else {
1589 if (!rotationLocked) {
1590 if (mResetOrientationData == null) {
1591 mResetOrientationData = new ResetOrientationData();
1592 mResetOrientationData.locked = false;
1593 }
1594
1595 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001596 // This call is posted so that the rotation does not change until post-layout,
1597 // otherwise onConfigurationChanged() may not get invoked.
1598 mGlobalActionsLayout.post(() ->
1599 RotationPolicy.setRotationLockAtAngle(
1600 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001601 }
1602
Steve Elliott53f12ae2019-05-13 17:14:15 -04001603 // Disable rotation suggestions, if enabled
1604 setRotationSuggestionsEnabled(false);
1605
Steve Elliott300b48f2019-05-29 14:13:50 -04001606 FrameLayout panelContainer =
1607 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001608 FrameLayout.LayoutParams panelParams =
1609 new FrameLayout.LayoutParams(
1610 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001611 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001612 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001613 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1614 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001615 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001616 }
1617
1618 private void initializeLayout() {
1619 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001620 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001621 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001622 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001623 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001624 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001625 @Override
1626 public boolean dispatchPopulateAccessibilityEvent(
1627 View host, AccessibilityEvent event) {
1628 // Populate the title here, just as Activity does
1629 event.getText().add(mContext.getString(R.string.global_actions));
1630 return true;
1631 }
1632 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001633 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001634 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001635
Steve Elliott86ef6282019-05-08 14:45:04 -04001636 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001637 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001638 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001639 if (mBackgroundDrawable == null) {
1640 mBackgroundDrawable = new ScrimDrawable();
1641 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1642 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001643 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001644 }
1645
Steve Elliott300b48f2019-05-29 14:13:50 -04001646 private void fixNavBarClipping() {
1647 ViewGroup content = findViewById(android.R.id.content);
1648 content.setClipChildren(false);
1649 content.setClipToPadding(false);
1650 ViewGroup contentParent = (ViewGroup) content.getParent();
1651 contentParent.setClipChildren(false);
1652 contentParent.setClipToPadding(false);
1653 }
1654
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001655 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001656 int rotation = RotationUtils.getRotation(context);
1657 boolean useGridLayout = isForceGridEnabled(context)
1658 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1659 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001660 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001661 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001662 } else {
1663 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001664 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001665 } else {
1666 if (useGridLayout) {
1667 return com.android.systemui.R.layout.global_actions_grid;
1668 } else {
1669 return com.android.systemui.R.layout.global_actions_column;
1670 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001671 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001672 }
1673
Jason Monk361915c2017-03-21 20:33:59 -04001674 @Override
1675 protected void onStart() {
1676 super.setCanceledOnTouchOutside(true);
1677 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001678 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001679
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001680 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001681 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001682 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001683 updateColors(colors, false /* animate */);
1684 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001685 }
1686
1687 /**
1688 * Updates background and system bars according to current GradientColors.
1689 * @param colors Colors and hints to use.
1690 * @param animate Interpolates gradient if true, just sets otherwise.
1691 */
1692 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001693 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001694 return;
1695 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001696 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001697 View decorView = getWindow().getDecorView();
1698 if (colors.supportsDarkText()) {
1699 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001700 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001701 } else {
1702 decorView.setSystemUiVisibility(0);
1703 }
Jason Monk361915c2017-03-21 20:33:59 -04001704 }
1705
1706 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001707 protected void onStop() {
1708 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001709 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001710 }
1711
1712 @Override
1713 public void show() {
1714 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001715 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001716 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1717 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001718 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001719 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1720 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001721 mGlobalActionsLayout.setAlpha(0);
1722 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001723 .alpha(1)
1724 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001725 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001726 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001727 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001728 .setUpdateListener(animation -> {
1729 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001730 * mScrimAlpha * 255);
1731 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001732 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001733 .start();
1734 }
1735
1736 @Override
1737 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001738 if (!mShowing) {
1739 return;
1740 }
1741 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001742 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001743 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001744 mGlobalActionsLayout.setAlpha(1);
1745 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001746 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001747 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1748 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001749 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001750 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001751 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001752 .setUpdateListener(animation -> {
1753 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001754 * mScrimAlpha * 255);
1755 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001756 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001757 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001758 dismissPanel();
1759 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001760 }
1761
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001762 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001763 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001764 dismissPanel();
1765 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001766 completeDismiss();
1767 }
1768
1769 private void completeDismiss() {
1770 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1771 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001772 }
1773
1774 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001775 if (mPanelController != null) {
1776 mPanelController.onDismissed();
1777 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001778 }
1779
Steve Elliott53f12ae2019-05-13 17:14:15 -04001780 private void setRotationSuggestionsEnabled(boolean enabled) {
1781 try {
1782 final int userId = Binder.getCallingUserHandle().getIdentifier();
1783 final int what = enabled
1784 ? StatusBarManager.DISABLE2_NONE
1785 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1786 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1787 } catch (RemoteException ex) {
1788 throw ex.rethrowFromSystemServer();
1789 }
1790 }
1791
Steve Elliott48f75db2019-05-03 15:03:38 -04001792 private void resetOrientation() {
1793 if (mResetOrientationData != null) {
1794 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1795 mResetOrientationData.rotation);
1796 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001797 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001798 }
1799
Jason Monk361915c2017-03-21 20:33:59 -04001800 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001801 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001802 if (mKeyguardShowing) {
1803 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001804 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1805 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001806 }
1807 } else {
1808 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001809 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1810 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001811 }
1812 }
1813 }
1814
1815 public void setKeyguardShowing(boolean keyguardShowing) {
1816 mKeyguardShowing = keyguardShowing;
1817 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001818
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001819 public void refreshDialog() {
1820 initializeLayout();
1821 mGlobalActionsLayout.updateList();
1822 }
1823
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001824 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001825 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001826 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001827 }
1828 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001829
1830 private static class ResetOrientationData {
1831 public boolean locked;
1832 public int rotation;
1833 }
Jason Monk361915c2017-03-21 20:33:59 -04001834 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001835
1836 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001837 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1838 */
1839 private static boolean isPanelDebugModeEnabled(Context context) {
1840 return Settings.Secure.getInt(context.getContentResolver(),
1841 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1842 }
1843
1844 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001845 * Determines whether or not the Global Actions menu should be forced to
1846 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001847 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001848 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001849 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001850 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001851
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001852
1853 /**
1854 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1855 */
1856 private static boolean shouldUseSeparatedView() {
1857 return true;
1858 }
Jason Monk361915c2017-03-21 20:33:59 -04001859}