blob: 0e4c15576957026301bfb045bcc2c689b73bb585 [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;
Steve Elliott92475042019-05-06 18:56:40 -040099import com.android.systemui.statusbar.phone.UnlockMethodCache;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700100import com.android.systemui.statusbar.policy.ConfigurationController;
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);
Steve Elliott92475042019-05-06 18:56:40 -0400216 UnlockMethodCache unlockMethodCache = UnlockMethodCache.getInstance(context);
217 unlockMethodCache.addListener(
218 () -> {
219 if (mDialog != null && mDialog.mPanelController != null) {
220 boolean locked = !unlockMethodCache.canSkipBouncer();
221 mDialog.mPanelController.onDeviceLockStateChanged(locked);
222 }
223 });
Jason Monk361915c2017-03-21 20:33:59 -0400224 }
225
226 /**
227 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400228 *
Jason Monk361915c2017-03-21 20:33:59 -0400229 * @param keyguardShowing True if keyguard is showing
230 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400231 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
232 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400233 mKeyguardShowing = keyguardShowing;
234 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400235 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400236 if (mDialog != null) {
237 mDialog.dismiss();
238 mDialog = null;
239 // Show delayed, so that the dismiss of the previous dialog completes
240 mHandler.sendEmptyMessage(MESSAGE_SHOW);
241 } else {
242 handleShow();
243 }
244 }
245
Charles He9851a8d2017-10-10 17:31:30 +0100246 /**
247 * Dismiss the global actions dialog, if it's currently shown
248 */
249 public void dismissDialog() {
250 mHandler.removeMessages(MESSAGE_DISMISS);
251 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
252 }
253
Jason Monk361915c2017-03-21 20:33:59 -0400254 private void awakenIfNecessary() {
255 if (mDreamManager != null) {
256 try {
257 if (mDreamManager.isDreaming()) {
258 mDreamManager.awaken();
259 }
260 } catch (RemoteException e) {
261 // we tried
262 }
263 }
264 }
265
266 private void handleShow() {
267 awakenIfNecessary();
268 mDialog = createDialog();
269 prepareDialog();
270
271 // If we only have 1 item and it's a simple press action, just do this action.
272 if (mAdapter.getCount() == 1
273 && mAdapter.getItem(0) instanceof SinglePressAction
274 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
275 ((SinglePressAction) mAdapter.getItem(0)).onPress();
276 } else {
277 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
278 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100279 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400280 mDialog.getWindow().setAttributes(attrs);
281 mDialog.show();
282 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400283 }
284 }
285
286 /**
287 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400288 *
Jason Monk361915c2017-03-21 20:33:59 -0400289 * @return A new dialog.
290 */
291 private ActionsDialog createDialog() {
292 // Simple toggle style if there's no vibrator, otherwise use a tri-state
293 if (!mHasVibrator) {
294 mSilentModeAction = new SilentModeToggleAction();
295 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700296 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400297 }
298 mAirplaneModeOn = new ToggleAction(
299 R.drawable.ic_lock_airplane_mode,
300 R.drawable.ic_lock_airplane_mode_off,
301 R.string.global_actions_toggle_airplane_mode,
302 R.string.global_actions_airplane_mode_on_status,
303 R.string.global_actions_airplane_mode_off_status) {
304
305 void onToggle(boolean on) {
306 if (mHasTelephony && Boolean.parseBoolean(
307 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
308 mIsWaitingForEcmExit = true;
309 // Launch ECM exit dialog
310 Intent ecmDialogIntent =
311 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
312 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
313 mContext.startActivity(ecmDialogIntent);
314 } else {
315 changeAirplaneModeSystemSetting(on);
316 }
317 }
318
319 @Override
320 protected void changeStateFromPress(boolean buttonOn) {
321 if (!mHasTelephony) return;
322
323 // In ECM mode airplane state cannot be changed
324 if (!(Boolean.parseBoolean(
325 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
326 mState = buttonOn ? State.TurningOn : State.TurningOff;
327 mAirplaneState = mState;
328 }
329 }
330
331 public boolean showDuringKeyguard() {
332 return true;
333 }
334
335 public boolean showBeforeProvisioning() {
336 return false;
337 }
338 };
339 onAirplaneModeChanged();
340
341 mItems = new ArrayList<Action>();
342 String[] defaultActions = mContext.getResources().getStringArray(
343 R.array.config_globalActionsList);
344
345 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000346 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800347 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400348 for (int i = 0; i < defaultActions.length; i++) {
349 String actionKey = defaultActions[i];
350 if (addedKeys.contains(actionKey)) {
351 // If we already have added this, don't add it again.
352 continue;
353 }
354 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
355 mItems.add(new PowerAction());
356 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
357 mItems.add(mAirplaneModeOn);
358 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
359 if (Settings.Global.getInt(mContext.getContentResolver(),
360 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
361 mItems.add(new BugReportAction());
362 }
363 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
364 if (mShowSilentToggle) {
365 mItems.add(mSilentModeAction);
366 }
367 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
368 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
369 addUsersToMenu(mItems);
370 }
371 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
372 mItems.add(getSettingsAction());
373 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700374 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
375 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800376 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700377 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800378 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700379 }
Jason Monk361915c2017-03-21 20:33:59 -0400380 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
381 mItems.add(getVoiceAssistAction());
382 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
383 mItems.add(getAssistAction());
384 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
385 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500386 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
387 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000388 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000389 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000390 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
391 mItems.add(new LogoutAction());
392 mHasLogoutButton = true;
393 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800394 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400395 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800396 mItems.add(new EmergencyDialerAction());
397 }
Jason Monk361915c2017-03-21 20:33:59 -0400398 } else {
399 Log.e(TAG, "Invalid global action key " + actionKey);
400 }
401 // Add here so we don't add more than one.
402 addedKeys.add(actionKey);
403 }
404
405 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400406 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400407 }
408
409 mAdapter = new MyAdapter();
410
Steve Elliott9b87a442019-03-05 10:24:16 -0500411 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400412 mPanelPlugin != null
413 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400414 new GlobalActionsPanelPlugin.Callbacks() {
415 @Override
416 public void dismissGlobalActionsMenu() {
417 if (mDialog != null) {
418 mDialog.dismiss();
419 }
420 }
421
422 @Override
423 public void startPendingIntentDismissingKeyguard(
424 PendingIntent intent) {
425 mActivityStarter
426 .startPendingIntentDismissingKeyguard(intent);
427 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400428 },
429 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500430 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400431
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400432 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400433 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700434 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400435
436 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800437 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400438
439 return dialog;
440 }
441
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800442 private boolean shouldDisplayLockdown() {
443 int userId = getCurrentUser().id;
444 // Lockdown is meaningless without a place to go.
445 if (!mKeyguardManager.isDeviceSecure(userId)) {
446 return false;
447 }
448
449 // Only show the lockdown button if the device isn't locked down (for whatever reason).
450 int state = mLockPatternUtils.getStrongAuthForUser(userId);
451 return (state == STRONG_AUTH_NOT_REQUIRED
452 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
453 }
454
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700455 @Override
456 public void onUiModeChanged() {
457 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700458 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400459 mDialog.refreshDialog();
460 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700461 }
462
463 public void destroy() {
464 Dependency.get(ConfigurationController.class).removeCallback(this);
465 }
466
Jason Monk361915c2017-03-21 20:33:59 -0400467 private final class PowerAction extends SinglePressAction implements LongPressAction {
468 private PowerAction() {
469 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400470 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400471 }
472
473 @Override
474 public boolean onLongPress() {
475 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
476 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
477 mWindowManagerFuncs.reboot(true);
478 return true;
479 }
480 return false;
481 }
482
483 @Override
484 public boolean showDuringKeyguard() {
485 return true;
486 }
487
488 @Override
489 public boolean showBeforeProvisioning() {
490 return true;
491 }
492
493 @Override
494 public void onPress() {
495 // shutdown by making sure radio and power are handled accordingly.
496 mWindowManagerFuncs.shutdown();
497 }
498 }
499
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400500 private abstract class EmergencyAction extends SinglePressAction {
501 EmergencyAction(int iconResId, int messageResId) {
502 super(iconResId, messageResId);
503 }
504
505 @Override
506 public boolean shouldBeSeparated() {
507 return shouldUseSeparatedView();
508 }
509
510 @Override
511 public View create(
512 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
513 View v = super.create(context, convertView, parent, inflater);
514 int textColor;
515 if (shouldBeSeparated()) {
516 textColor = v.getResources().getColor(
517 com.android.systemui.R.color.global_actions_alert_text);
518 } else {
519 textColor = v.getResources().getColor(
520 com.android.systemui.R.color.global_actions_text);
521 }
522 TextView messageView = v.findViewById(R.id.message);
523 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400524 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400525 ImageView icon = (ImageView) v.findViewById(R.id.icon);
526 icon.getDrawable().setTint(textColor);
527 return v;
528 }
529
530 @Override
531 public boolean showDuringKeyguard() {
532 return true;
533 }
534
535 @Override
536 public boolean showBeforeProvisioning() {
537 return true;
538 }
539 }
540
541 private class EmergencyAffordanceAction extends EmergencyAction {
542 EmergencyAffordanceAction() {
543 super(R.drawable.emergency_icon,
544 R.string.global_action_emergency);
545 }
546
547 @Override
548 public void onPress() {
549 mEmergencyAffordanceManager.performEmergencyCall();
550 }
551 }
552
553 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800554 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400555 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800556 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800557 }
558
559 @Override
560 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800561 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800562 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800563 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
564 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
565 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800566 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
567 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800568 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
569 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800570 }
571
Jason Monk361915c2017-03-21 20:33:59 -0400572 private final class RestartAction extends SinglePressAction implements LongPressAction {
573 private RestartAction() {
574 super(R.drawable.ic_restart, R.string.global_action_restart);
575 }
576
577 @Override
578 public boolean onLongPress() {
579 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
580 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
581 mWindowManagerFuncs.reboot(true);
582 return true;
583 }
584 return false;
585 }
586
587 @Override
588 public boolean showDuringKeyguard() {
589 return true;
590 }
591
592 @Override
593 public boolean showBeforeProvisioning() {
594 return true;
595 }
596
597 @Override
598 public void onPress() {
599 mWindowManagerFuncs.reboot(false);
600 }
601 }
602
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400603 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500604 public ScreenshotAction() {
605 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
606 }
607
608 @Override
609 public void onPress() {
610 // Add a little delay before executing, to give the
611 // dialog a chance to go away before it takes a
612 // screenshot.
613 // TODO: instead, omit global action dialog layer
614 mHandler.postDelayed(new Runnable() {
615 @Override
616 public void run() {
617 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
618 MetricsLogger.action(mContext,
619 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
620 }
621 }, 500);
622 }
623
624 @Override
625 public boolean showDuringKeyguard() {
626 return true;
627 }
628
629 @Override
630 public boolean showBeforeProvisioning() {
631 return false;
632 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400633
634 @Override
635 public boolean onLongPress() {
636 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
637 mScreenRecordHelper.launchRecordPrompt();
638 } else {
639 onPress();
640 }
641 return true;
642 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500643 }
644
Jason Monk361915c2017-03-21 20:33:59 -0400645 private class BugReportAction extends SinglePressAction implements LongPressAction {
646
647 public BugReportAction() {
648 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
649 }
650
651 @Override
652 public void onPress() {
653 // don't actually trigger the bugreport if we are running stability
654 // tests via monkey
655 if (ActivityManager.isUserAMonkey()) {
656 return;
657 }
658 // Add a little delay before executing, to give the
659 // dialog a chance to go away before it takes a
660 // screenshot.
661 mHandler.postDelayed(new Runnable() {
662 @Override
663 public void run() {
664 try {
665 // Take an "interactive" bugreport.
666 MetricsLogger.action(mContext,
667 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
668 ActivityManager.getService().requestBugReport(
669 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
670 } catch (RemoteException e) {
671 }
672 }
673 }, 500);
674 }
675
676 @Override
677 public boolean onLongPress() {
678 // don't actually trigger the bugreport if we are running stability
679 // tests via monkey
680 if (ActivityManager.isUserAMonkey()) {
681 return false;
682 }
683 try {
684 // Take a "full" bugreport.
685 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
686 ActivityManager.getService().requestBugReport(
687 ActivityManager.BUGREPORT_OPTION_FULL);
688 } catch (RemoteException e) {
689 }
690 return false;
691 }
692
693 public boolean showDuringKeyguard() {
694 return true;
695 }
696
697 @Override
698 public boolean showBeforeProvisioning() {
699 return false;
700 }
Jason Monk361915c2017-03-21 20:33:59 -0400701 }
702
Alex Chau04458852017-11-27 18:21:23 +0000703 private final class LogoutAction extends SinglePressAction {
704 private LogoutAction() {
705 super(R.drawable.ic_logout, R.string.global_action_logout);
706 }
707
708 @Override
709 public boolean showDuringKeyguard() {
710 return true;
711 }
712
713 @Override
714 public boolean showBeforeProvisioning() {
715 return false;
716 }
717
718 @Override
719 public void onPress() {
720 // Add a little delay before executing, to give the dialog a chance to go away before
721 // switching user
722 mHandler.postDelayed(() -> {
723 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000724 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000725 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000726 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000727 } catch (RemoteException re) {
728 Log.e(TAG, "Couldn't logout user " + re);
729 }
730 }, 500);
731 }
732 }
733
Jason Monk361915c2017-03-21 20:33:59 -0400734 private Action getSettingsAction() {
735 return new SinglePressAction(R.drawable.ic_settings,
736 R.string.global_action_settings) {
737
738 @Override
739 public void onPress() {
740 Intent intent = new Intent(Settings.ACTION_SETTINGS);
741 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
742 mContext.startActivity(intent);
743 }
744
745 @Override
746 public boolean showDuringKeyguard() {
747 return true;
748 }
749
750 @Override
751 public boolean showBeforeProvisioning() {
752 return true;
753 }
754 };
755 }
756
Jason Monk361915c2017-03-21 20:33:59 -0400757 private Action getAssistAction() {
758 return new SinglePressAction(R.drawable.ic_action_assist_focused,
759 R.string.global_action_assist) {
760 @Override
761 public void onPress() {
762 Intent intent = new Intent(Intent.ACTION_ASSIST);
763 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
764 mContext.startActivity(intent);
765 }
766
767 @Override
768 public boolean showDuringKeyguard() {
769 return true;
770 }
771
772 @Override
773 public boolean showBeforeProvisioning() {
774 return true;
775 }
776 };
777 }
778
779 private Action getVoiceAssistAction() {
780 return new SinglePressAction(R.drawable.ic_voice_search,
781 R.string.global_action_voice_assist) {
782 @Override
783 public void onPress() {
784 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
785 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
786 mContext.startActivity(intent);
787 }
788
789 @Override
790 public boolean showDuringKeyguard() {
791 return true;
792 }
793
794 @Override
795 public boolean showBeforeProvisioning() {
796 return true;
797 }
798 };
799 }
800
801 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400802 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400803 R.string.global_action_lockdown) {
804
805 @Override
806 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700807 new LockPatternUtils(mContext)
808 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
809 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400810 try {
811 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100812 // Lock profiles (if any) on the background thread.
813 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
814 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400815 } catch (RemoteException e) {
816 Log.e(TAG, "Error while trying to lock device.", e);
817 }
818 }
819
820 @Override
821 public boolean showDuringKeyguard() {
822 return true;
823 }
824
825 @Override
826 public boolean showBeforeProvisioning() {
827 return false;
828 }
829 };
830 }
831
Pavel Grafov059021b2018-05-02 13:44:46 +0100832 private void lockProfiles() {
833 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
834 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
835 final int currentUserId = getCurrentUser().id;
836 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
837 for (final int id : profileIds) {
838 if (id != currentUserId) {
839 tm.setDeviceLockedForUser(id, true);
840 }
841 }
842 }
843
Jason Monk361915c2017-03-21 20:33:59 -0400844 private UserInfo getCurrentUser() {
845 try {
846 return ActivityManager.getService().getCurrentUser();
847 } catch (RemoteException re) {
848 return null;
849 }
850 }
851
852 private boolean isCurrentUserOwner() {
853 UserInfo currentUser = getCurrentUser();
854 return currentUser == null || currentUser.isPrimary();
855 }
856
857 private void addUsersToMenu(ArrayList<Action> items) {
858 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
859 if (um.isUserSwitcherEnabled()) {
860 List<UserInfo> users = um.getUsers();
861 UserInfo currentUser = getCurrentUser();
862 for (final UserInfo user : users) {
863 if (user.supportsSwitchToByUser()) {
864 boolean isCurrentUser = currentUser == null
865 ? user.id == 0 : (currentUser.id == user.id);
866 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
867 : null;
868 SinglePressAction switchToUser = new SinglePressAction(
869 R.drawable.ic_menu_cc, icon,
870 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400871 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400872 public void onPress() {
873 try {
874 ActivityManager.getService().switchUser(user.id);
875 } catch (RemoteException re) {
876 Log.e(TAG, "Couldn't switch user " + re);
877 }
878 }
879
880 public boolean showDuringKeyguard() {
881 return true;
882 }
883
884 public boolean showBeforeProvisioning() {
885 return false;
886 }
887 };
888 items.add(switchToUser);
889 }
890 }
891 }
892 }
893
894 private void prepareDialog() {
895 refreshSilentMode();
896 mAirplaneModeOn.updateState(mAirplaneState);
897 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400898 if (mShowSilentToggle) {
899 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
900 mContext.registerReceiver(mRingerModeReceiver, filter);
901 }
902 }
903
904 private void refreshSilentMode() {
905 if (!mHasVibrator) {
906 final boolean silentModeOn =
907 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400908 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400909 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
910 }
911 }
912
913 /** {@inheritDoc} */
914 public void onDismiss(DialogInterface dialog) {
915 mWindowManagerFuncs.onGlobalActionsHidden();
916 if (mShowSilentToggle) {
917 try {
918 mContext.unregisterReceiver(mRingerModeReceiver);
919 } catch (IllegalArgumentException ie) {
920 // ignore this
921 Log.w(TAG, ie);
922 }
923 }
924 }
925
926 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800927 public void onShow(DialogInterface dialog) {
928 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
929 }
930
Jason Monk361915c2017-03-21 20:33:59 -0400931 /**
932 * The adapter used for the list within the global actions dialog, taking
933 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400934 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
935 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400936 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
937 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500938 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400939 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400940 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400941 for (int i = 0; i < mItems.size(); i++) {
942 final Action action = mItems.get(i);
943
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400944 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
945 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400946 }
Jason Monk361915c2017-03-21 20:33:59 -0400947 }
948 return count;
949 }
950
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400951 private boolean shouldBeShown(Action action) {
952 if (mKeyguardShowing && !action.showDuringKeyguard()) {
953 return false;
954 }
955 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
956 return false;
957 }
958 return true;
959 }
960
961 @Override
962 public int countSeparatedItems() {
963 return countItems(true);
964 }
965
966 @Override
967 public int countListItems() {
968 return countItems(false);
969 }
970
971 @Override
972 public int getCount() {
973 return countSeparatedItems() + countListItems();
974 }
975
Jason Monk361915c2017-03-21 20:33:59 -0400976 @Override
977 public boolean isEnabled(int position) {
978 return getItem(position).isEnabled();
979 }
980
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500981 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400982 public boolean areAllItemsEnabled() {
983 return false;
984 }
985
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500986 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400987 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400988 int filteredPos = 0;
989 for (int i = 0; i < mItems.size(); i++) {
990 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400991 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400992 continue;
993 }
994 if (filteredPos == position) {
995 return action;
996 }
997 filteredPos++;
998 }
999
1000 throw new IllegalArgumentException("position " + position
1001 + " out of range of showable actions"
1002 + ", filtered count=" + getCount()
1003 + ", keyguardshowing=" + mKeyguardShowing
1004 + ", provisioned=" + mDeviceProvisioned);
1005 }
1006
1007
1008 public long getItemId(int position) {
1009 return position;
1010 }
1011
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001012 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001013 public View getView(int position, View convertView, ViewGroup parent) {
1014 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001015 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001016 view.setOnClickListener(v -> onClickItem(position));
1017 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001018 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001019 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001020
1021 @Override
1022 public boolean onLongClickItem(int position) {
1023 final Action action = mAdapter.getItem(position);
1024 if (action instanceof LongPressAction) {
1025 mDialog.dismiss();
1026 return ((LongPressAction) action).onLongPress();
1027 }
1028 return false;
1029 }
1030
1031 @Override
1032 public void onClickItem(int position) {
1033 Action item = mAdapter.getItem(position);
1034 if (!(item instanceof SilentModeTriStateAction)) {
1035 mDialog.dismiss();
1036 }
1037 item.onPress();
1038 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001039
1040 @Override
1041 public boolean shouldBeSeparated(int position) {
1042 return getItem(position).shouldBeSeparated();
1043 }
Jason Monk361915c2017-03-21 20:33:59 -04001044 }
1045
1046 // note: the scheme below made more sense when we were planning on having
1047 // 8 different things in the global actions dialog. seems overkill with
1048 // only 3 items now, but may as well keep this flexible approach so it will
1049 // be easy should someone decide at the last minute to include something
1050 // else, such as 'enable wifi', or 'enable bluetooth'
1051
1052 /**
1053 * What each item in the global actions dialog must be able to support.
1054 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001055 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001056 /**
1057 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001058 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001059 */
1060 CharSequence getLabelForAccessibility(Context context);
1061
1062 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1063
1064 void onPress();
1065
1066 /**
1067 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001068 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001069 */
1070 boolean showDuringKeyguard();
1071
1072 /**
1073 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001074 * device is provisioned.onlongpress
1075 *
Jason Monk361915c2017-03-21 20:33:59 -04001076 */
1077 boolean showBeforeProvisioning();
1078
1079 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001080
1081 default boolean shouldBeSeparated() {
1082 return false;
1083 }
Jason Monk361915c2017-03-21 20:33:59 -04001084 }
1085
1086 /**
1087 * An action that also supports long press.
1088 */
1089 private interface LongPressAction extends Action {
1090 boolean onLongPress();
1091 }
1092
1093 /**
1094 * A single press action maintains no state, just responds to a press
1095 * and takes an action.
1096 */
1097 private static abstract class SinglePressAction implements Action {
1098 private final int mIconResId;
1099 private final Drawable mIcon;
1100 private final int mMessageResId;
1101 private final CharSequence mMessage;
1102
1103 protected SinglePressAction(int iconResId, int messageResId) {
1104 mIconResId = iconResId;
1105 mMessageResId = messageResId;
1106 mMessage = null;
1107 mIcon = null;
1108 }
1109
1110 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1111 mIconResId = iconResId;
1112 mMessageResId = 0;
1113 mMessage = message;
1114 mIcon = icon;
1115 }
1116
1117 public boolean isEnabled() {
1118 return true;
1119 }
1120
1121 public String getStatus() {
1122 return null;
1123 }
1124
1125 abstract public void onPress();
1126
1127 public CharSequence getLabelForAccessibility(Context context) {
1128 if (mMessage != null) {
1129 return mMessage;
1130 } else {
1131 return context.getString(mMessageResId);
1132 }
1133 }
1134
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001135 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001136 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001137 }
1138
Jason Monk361915c2017-03-21 20:33:59 -04001139 public View create(
1140 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001141 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001142 false);
Jason Monk361915c2017-03-21 20:33:59 -04001143
1144 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1145 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001146 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001147
1148 TextView statusView = (TextView) v.findViewById(R.id.status);
1149 final String status = getStatus();
1150 if (!TextUtils.isEmpty(status)) {
1151 statusView.setText(status);
1152 } else {
1153 statusView.setVisibility(View.GONE);
1154 }
1155 if (mIcon != null) {
1156 icon.setImageDrawable(mIcon);
1157 icon.setScaleType(ScaleType.CENTER_CROP);
1158 } else if (mIconResId != 0) {
1159 icon.setImageDrawable(context.getDrawable(mIconResId));
1160 }
1161 if (mMessage != null) {
1162 messageView.setText(mMessage);
1163 } else {
1164 messageView.setText(mMessageResId);
1165 }
1166
1167 return v;
1168 }
1169 }
1170
1171 /**
1172 * A toggle action knows whether it is on or off, and displays an icon
1173 * and status message accordingly.
1174 */
1175 private static abstract class ToggleAction implements Action {
1176
1177 enum State {
1178 Off(false),
1179 TurningOn(true),
1180 TurningOff(true),
1181 On(false);
1182
1183 private final boolean inTransition;
1184
1185 State(boolean intermediate) {
1186 inTransition = intermediate;
1187 }
1188
1189 public boolean inTransition() {
1190 return inTransition;
1191 }
1192 }
1193
1194 protected State mState = State.Off;
1195
1196 // prefs
1197 protected int mEnabledIconResId;
1198 protected int mDisabledIconResid;
1199 protected int mMessageResId;
1200 protected int mEnabledStatusMessageResId;
1201 protected int mDisabledStatusMessageResId;
1202
1203 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001204 * @param enabledIconResId The icon for when this action is on.
1205 * @param disabledIconResid The icon for when this action is off.
1206 * @param message The general information message, e.g 'Silent Mode'
1207 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001208 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1209 */
1210 public ToggleAction(int enabledIconResId,
1211 int disabledIconResid,
1212 int message,
1213 int enabledStatusMessageResId,
1214 int disabledStatusMessageResId) {
1215 mEnabledIconResId = enabledIconResId;
1216 mDisabledIconResid = disabledIconResid;
1217 mMessageResId = message;
1218 mEnabledStatusMessageResId = enabledStatusMessageResId;
1219 mDisabledStatusMessageResId = disabledStatusMessageResId;
1220 }
1221
1222 /**
1223 * Override to make changes to resource IDs just before creating the
1224 * View.
1225 */
1226 void willCreate() {
1227
1228 }
1229
1230 @Override
1231 public CharSequence getLabelForAccessibility(Context context) {
1232 return context.getString(mMessageResId);
1233 }
1234
1235 public View create(Context context, View convertView, ViewGroup parent,
1236 LayoutInflater inflater) {
1237 willCreate();
1238
1239 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001240 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001241
1242 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1243 TextView messageView = (TextView) v.findViewById(R.id.message);
1244 TextView statusView = (TextView) v.findViewById(R.id.status);
1245 final boolean enabled = isEnabled();
1246
1247 if (messageView != null) {
1248 messageView.setText(mMessageResId);
1249 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001250 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001251 }
1252
1253 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1254 if (icon != null) {
1255 icon.setImageDrawable(context.getDrawable(
1256 (on ? mEnabledIconResId : mDisabledIconResid)));
1257 icon.setEnabled(enabled);
1258 }
1259
1260 if (statusView != null) {
1261 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1262 statusView.setVisibility(View.VISIBLE);
1263 statusView.setEnabled(enabled);
1264 }
1265 v.setEnabled(enabled);
1266
1267 return v;
1268 }
1269
1270 public final void onPress() {
1271 if (mState.inTransition()) {
1272 Log.w(TAG, "shouldn't be able to toggle when in transition");
1273 return;
1274 }
1275
1276 final boolean nowOn = !(mState == State.On);
1277 onToggle(nowOn);
1278 changeStateFromPress(nowOn);
1279 }
1280
1281 public boolean isEnabled() {
1282 return !mState.inTransition();
1283 }
1284
1285 /**
1286 * Implementations may override this if their state can be in on of the intermediate
1287 * states until some notification is received (e.g airplane mode is 'turning off' until
1288 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001289 *
Jason Monk361915c2017-03-21 20:33:59 -04001290 * @param buttonOn Whether the button was turned on or off
1291 */
1292 protected void changeStateFromPress(boolean buttonOn) {
1293 mState = buttonOn ? State.On : State.Off;
1294 }
1295
1296 abstract void onToggle(boolean on);
1297
1298 public void updateState(State state) {
1299 mState = state;
1300 }
1301 }
1302
1303 private class SilentModeToggleAction extends ToggleAction {
1304 public SilentModeToggleAction() {
1305 super(R.drawable.ic_audio_vol_mute,
1306 R.drawable.ic_audio_vol,
1307 R.string.global_action_toggle_silent_mode,
1308 R.string.global_action_silent_mode_on_status,
1309 R.string.global_action_silent_mode_off_status);
1310 }
1311
1312 void onToggle(boolean on) {
1313 if (on) {
1314 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1315 } else {
1316 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1317 }
1318 }
1319
1320 public boolean showDuringKeyguard() {
1321 return true;
1322 }
1323
1324 public boolean showBeforeProvisioning() {
1325 return false;
1326 }
1327 }
1328
1329 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1330
Jason Monk16fbd9d2017-04-27 14:28:49 -04001331 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001332
1333 private final AudioManager mAudioManager;
1334 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001335
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001336 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001337 mAudioManager = audioManager;
1338 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001339 }
1340
1341 private int ringerModeToIndex(int ringerMode) {
1342 // They just happen to coincide
1343 return ringerMode;
1344 }
1345
1346 private int indexToRingerMode(int index) {
1347 // They just happen to coincide
1348 return index;
1349 }
1350
1351 @Override
1352 public CharSequence getLabelForAccessibility(Context context) {
1353 return null;
1354 }
1355
1356 public View create(Context context, View convertView, ViewGroup parent,
1357 LayoutInflater inflater) {
1358 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1359
1360 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1361 for (int i = 0; i < 3; i++) {
1362 View itemView = v.findViewById(ITEM_IDS[i]);
1363 itemView.setSelected(selectedIndex == i);
1364 // Set up click handler
1365 itemView.setTag(i);
1366 itemView.setOnClickListener(this);
1367 }
1368 return v;
1369 }
1370
1371 public void onPress() {
1372 }
1373
1374 public boolean showDuringKeyguard() {
1375 return true;
1376 }
1377
1378 public boolean showBeforeProvisioning() {
1379 return false;
1380 }
1381
1382 public boolean isEnabled() {
1383 return true;
1384 }
1385
1386 void willCreate() {
1387 }
1388
1389 public void onClick(View v) {
1390 if (!(v.getTag() instanceof Integer)) return;
1391
1392 int index = (Integer) v.getTag();
1393 mAudioManager.setRingerMode(indexToRingerMode(index));
1394 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1395 }
1396 }
1397
1398 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1399 public void onReceive(Context context, Intent intent) {
1400 String action = intent.getAction();
1401 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1402 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1403 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1404 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001405 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001406 }
1407 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1408 // Airplane mode can be changed after ECM exits if airplane toggle button
1409 // is pressed during ECM mode
1410 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1411 mIsWaitingForEcmExit) {
1412 mIsWaitingForEcmExit = false;
1413 changeAirplaneModeSystemSetting(true);
1414 }
1415 }
1416 }
1417 };
1418
1419 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1420 @Override
1421 public void onServiceStateChanged(ServiceState serviceState) {
1422 if (!mHasTelephony) return;
1423 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1424 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1425 mAirplaneModeOn.updateState(mAirplaneState);
1426 mAdapter.notifyDataSetChanged();
1427 }
1428 };
1429
1430 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1431 @Override
1432 public void onReceive(Context context, Intent intent) {
1433 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1434 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1435 }
1436 }
1437 };
1438
1439 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1440 @Override
1441 public void onChange(boolean selfChange) {
1442 onAirplaneModeChanged();
1443 }
1444 };
1445
1446 private static final int MESSAGE_DISMISS = 0;
1447 private static final int MESSAGE_REFRESH = 1;
1448 private static final int MESSAGE_SHOW = 2;
1449 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1450
1451 private Handler mHandler = new Handler() {
1452 public void handleMessage(Message msg) {
1453 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001454 case MESSAGE_DISMISS:
1455 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001456 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1457 mDialog.dismissImmediately();
1458 } else {
1459 mDialog.dismiss();
1460 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001461 mDialog = null;
1462 }
1463 break;
1464 case MESSAGE_REFRESH:
1465 refreshSilentMode();
1466 mAdapter.notifyDataSetChanged();
1467 break;
1468 case MESSAGE_SHOW:
1469 handleShow();
1470 break;
Jason Monk361915c2017-03-21 20:33:59 -04001471 }
1472 }
1473 };
1474
1475 private void onAirplaneModeChanged() {
1476 // Let the service state callbacks handle the state.
1477 if (mHasTelephony) return;
1478
1479 boolean airplaneModeOn = Settings.Global.getInt(
1480 mContext.getContentResolver(),
1481 Settings.Global.AIRPLANE_MODE_ON,
1482 0) == 1;
1483 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1484 mAirplaneModeOn.updateState(mAirplaneState);
1485 }
1486
1487 /**
1488 * Change the airplane mode system setting
1489 */
1490 private void changeAirplaneModeSystemSetting(boolean on) {
1491 Settings.Global.putInt(
1492 mContext.getContentResolver(),
1493 Settings.Global.AIRPLANE_MODE_ON,
1494 on ? 1 : 0);
1495 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1496 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1497 intent.putExtra("state", on);
1498 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1499 if (!mHasTelephony) {
1500 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1501 }
1502 }
1503
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001504 private static final class ActionsDialog extends Dialog implements DialogInterface,
1505 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001506
Jason Monk16fbd9d2017-04-27 14:28:49 -04001507 private final Context mContext;
1508 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001509 private final IStatusBarService mStatusBarService;
1510 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001511 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001512 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001513 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001514 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001515 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001516 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001517 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001518 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001519 private boolean mHadTopUi;
1520 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001521
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001522 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001523 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001524 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001525 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001526 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001527 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001528 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001529 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001530
1531 // Window initialization
1532 Window window = getWindow();
1533 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001534 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1535 window.getDecorView();
1536 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1537 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1538 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1539 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1540 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001541 window.addFlags(
1542 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001543 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001544 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001545 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1546 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1547 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001548 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001549 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001550
1551 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001552 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001553 }
1554
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001555 private boolean shouldUsePanel() {
Aran Inkf4744c62019-05-10 15:27:36 -04001556 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001557 }
1558
1559 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001560 int rotation = RotationUtils.getRotation(mContext);
1561 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1562 if (rotation != RotationUtils.ROTATION_NONE) {
1563 if (rotationLocked) {
1564 if (mResetOrientationData == null) {
1565 mResetOrientationData = new ResetOrientationData();
1566 mResetOrientationData.locked = true;
1567 mResetOrientationData.rotation = rotation;
1568 }
1569
1570 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001571 // This call is posted so that the rotation does not change until post-layout,
1572 // otherwise onConfigurationChanged() may not get invoked.
1573 mGlobalActionsLayout.post(() ->
1574 RotationPolicy.setRotationLockAtAngle(
1575 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001576 }
1577 } else {
1578 if (!rotationLocked) {
1579 if (mResetOrientationData == null) {
1580 mResetOrientationData = new ResetOrientationData();
1581 mResetOrientationData.locked = false;
1582 }
1583
1584 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001585 // This call is posted so that the rotation does not change until post-layout,
1586 // otherwise onConfigurationChanged() may not get invoked.
1587 mGlobalActionsLayout.post(() ->
1588 RotationPolicy.setRotationLockAtAngle(
1589 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001590 }
1591
Steve Elliott53f12ae2019-05-13 17:14:15 -04001592 // Disable rotation suggestions, if enabled
1593 setRotationSuggestionsEnabled(false);
1594
Steve Elliott300b48f2019-05-29 14:13:50 -04001595 FrameLayout panelContainer =
1596 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001597 FrameLayout.LayoutParams panelParams =
1598 new FrameLayout.LayoutParams(
1599 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001600 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001601 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001602 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1603 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001604 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001605 }
1606
1607 private void initializeLayout() {
1608 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001609 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001610 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001611 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001612 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001613 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001614 @Override
1615 public boolean dispatchPopulateAccessibilityEvent(
1616 View host, AccessibilityEvent event) {
1617 // Populate the title here, just as Activity does
1618 event.getText().add(mContext.getString(R.string.global_actions));
1619 return true;
1620 }
1621 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001622 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001623 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001624
Steve Elliott86ef6282019-05-08 14:45:04 -04001625 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001626 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001627 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001628 if (mBackgroundDrawable == null) {
1629 mBackgroundDrawable = new ScrimDrawable();
1630 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1631 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001632 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001633 }
1634
Steve Elliott300b48f2019-05-29 14:13:50 -04001635 private void fixNavBarClipping() {
1636 ViewGroup content = findViewById(android.R.id.content);
1637 content.setClipChildren(false);
1638 content.setClipToPadding(false);
1639 ViewGroup contentParent = (ViewGroup) content.getParent();
1640 contentParent.setClipChildren(false);
1641 contentParent.setClipToPadding(false);
1642 }
1643
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001644 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001645 int rotation = RotationUtils.getRotation(context);
1646 boolean useGridLayout = isForceGridEnabled(context)
1647 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1648 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001649 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001650 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001651 } else {
1652 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001653 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001654 } else {
1655 if (useGridLayout) {
1656 return com.android.systemui.R.layout.global_actions_grid;
1657 } else {
1658 return com.android.systemui.R.layout.global_actions_column;
1659 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001660 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001661 }
1662
Jason Monk361915c2017-03-21 20:33:59 -04001663 @Override
1664 protected void onStart() {
1665 super.setCanceledOnTouchOutside(true);
1666 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001667 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001668
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001669 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001670 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001671 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001672 updateColors(colors, false /* animate */);
1673 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001674 }
1675
1676 /**
1677 * Updates background and system bars according to current GradientColors.
1678 * @param colors Colors and hints to use.
1679 * @param animate Interpolates gradient if true, just sets otherwise.
1680 */
1681 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001682 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001683 return;
1684 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001685 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001686 View decorView = getWindow().getDecorView();
1687 if (colors.supportsDarkText()) {
1688 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001689 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001690 } else {
1691 decorView.setSystemUiVisibility(0);
1692 }
Jason Monk361915c2017-03-21 20:33:59 -04001693 }
1694
1695 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001696 protected void onStop() {
1697 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001698 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001699 }
1700
1701 @Override
1702 public void show() {
1703 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001704 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001705 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1706 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001707 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001708 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1709 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001710 mGlobalActionsLayout.setAlpha(0);
1711 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001712 .alpha(1)
1713 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001714 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001715 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001716 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001717 .setUpdateListener(animation -> {
1718 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001719 * mScrimAlpha * 255);
1720 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001721 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001722 .start();
1723 }
1724
1725 @Override
1726 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001727 if (!mShowing) {
1728 return;
1729 }
1730 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001731 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001732 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001733 mGlobalActionsLayout.setAlpha(1);
1734 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001735 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001736 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1737 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001738 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001739 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001740 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001741 .setUpdateListener(animation -> {
1742 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001743 * mScrimAlpha * 255);
1744 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001745 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001746 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001747 dismissPanel();
1748 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001749 }
1750
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001751 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001752 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001753 dismissPanel();
1754 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001755 completeDismiss();
1756 }
1757
1758 private void completeDismiss() {
1759 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1760 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001761 }
1762
1763 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001764 if (mPanelController != null) {
1765 mPanelController.onDismissed();
1766 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001767 }
1768
Steve Elliott53f12ae2019-05-13 17:14:15 -04001769 private void setRotationSuggestionsEnabled(boolean enabled) {
1770 try {
1771 final int userId = Binder.getCallingUserHandle().getIdentifier();
1772 final int what = enabled
1773 ? StatusBarManager.DISABLE2_NONE
1774 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1775 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1776 } catch (RemoteException ex) {
1777 throw ex.rethrowFromSystemServer();
1778 }
1779 }
1780
Steve Elliott48f75db2019-05-03 15:03:38 -04001781 private void resetOrientation() {
1782 if (mResetOrientationData != null) {
1783 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1784 mResetOrientationData.rotation);
1785 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001786 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001787 }
1788
Jason Monk361915c2017-03-21 20:33:59 -04001789 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001790 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001791 if (mKeyguardShowing) {
1792 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001793 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1794 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001795 }
1796 } else {
1797 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001798 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1799 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001800 }
1801 }
1802 }
1803
1804 public void setKeyguardShowing(boolean keyguardShowing) {
1805 mKeyguardShowing = keyguardShowing;
1806 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001807
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001808 public void refreshDialog() {
1809 initializeLayout();
1810 mGlobalActionsLayout.updateList();
1811 }
1812
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001813 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001814 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001815 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001816 }
1817 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001818
1819 private static class ResetOrientationData {
1820 public boolean locked;
1821 public int rotation;
1822 }
Jason Monk361915c2017-03-21 20:33:59 -04001823 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001824
1825 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001826 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1827 */
1828 private static boolean isPanelDebugModeEnabled(Context context) {
1829 return Settings.Secure.getInt(context.getContentResolver(),
1830 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1831 }
1832
1833 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001834 * Determines whether or not the Global Actions menu should be forced to
1835 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001836 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001837 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001838 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001839 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001840
1841 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001842 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1843 */
1844 private static boolean shouldUseSeparatedView() {
1845 return true;
1846 }
Jason Monk361915c2017-03-21 20:33:59 -04001847}