blob: 22846bc02a3805b36fdce715dee4222820aeb9d1 [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;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040093import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010094import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040095import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010096import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050097import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050098import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -040099import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700100import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400101import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800102import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500103import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500104import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700105
Jason Monk361915c2017-03-21 20:33:59 -0400106import java.util.ArrayList;
107import java.util.List;
108
109/**
110 * Helper to show the global actions dialog. Each item is an {@link Action} that
111 * may show depending on whether the keyguard is showing, and whether the device
112 * is provisioned.
113 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500114public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
115 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400116
117 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
118 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700119 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400120
121 private static final String TAG = "GlobalActionsDialog";
122
123 private static final boolean SHOW_SILENT_TOGGLE = true;
124
125 /* Valid settings for global actions keys.
126 * see config.xml config_globalActionList */
127 private static final String GLOBAL_ACTION_KEY_POWER = "power";
128 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
129 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
130 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
131 private static final String GLOBAL_ACTION_KEY_USERS = "users";
132 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
133 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
134 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
135 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
136 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000137 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800138 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500139 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400140
141 private final Context mContext;
142 private final GlobalActionsManager mWindowManagerFuncs;
143 private final AudioManager mAudioManager;
144 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000145 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800146 private final LockPatternUtils mLockPatternUtils;
147 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400148
149 private ArrayList<Action> mItems;
150 private ActionsDialog mDialog;
151
152 private Action mSilentModeAction;
153 private ToggleAction mAirplaneModeOn;
154
155 private MyAdapter mAdapter;
156
157 private boolean mKeyguardShowing = false;
158 private boolean mDeviceProvisioned = false;
159 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
160 private boolean mIsWaitingForEcmExit = false;
161 private boolean mHasTelephony;
162 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000163 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800164 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400165 private final boolean mShowSilentToggle;
166 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500167 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400168 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400169 private final ActivityStarter mActivityStarter;
170 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500171
Jason Monk361915c2017-03-21 20:33:59 -0400172 /**
173 * @param context everything needs a context :(
174 */
175 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700176 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400177 mWindowManagerFuncs = windowManagerFuncs;
178 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
179 mDreamManager = IDreamManager.Stub.asInterface(
180 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000181 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
182 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800183 mLockPatternUtils = new LockPatternUtils(mContext);
184 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400185
186 // receive broadcasts
187 IntentFilter filter = new IntentFilter();
188 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
189 filter.addAction(Intent.ACTION_SCREEN_OFF);
190 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -0400191 Dependency.get(BroadcastDispatcher.class).registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400192
193 ConnectivityManager cm = (ConnectivityManager)
194 context.getSystemService(Context.CONNECTIVITY_SERVICE);
195 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
196
197 // get notified of phone state changes
198 TelephonyManager telephonyManager =
199 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
200 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
201 mContext.getContentResolver().registerContentObserver(
202 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
203 mAirplaneModeObserver);
204 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
205 mHasVibrator = vibrator != null && vibrator.hasVibrator();
206
207 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
208 R.bool.config_useFixedVolume);
209
210 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500211 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400212 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700213
214 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500215
Steve Elliott4c868852019-03-14 16:25:41 -0400216 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400217 KeyguardStateController keyguardStateController =
218 Dependency.get(KeyguardStateController.class);
219 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
220 @Override
221 public void onUnlockedChanged() {
222 if (mDialog != null && mDialog.mPanelController != null) {
223 boolean locked = !keyguardStateController.canDismissLockScreen();
224 mDialog.mPanelController.onDeviceLockStateChanged(locked);
225 }
226 }
227 });
Jason Monk361915c2017-03-21 20:33:59 -0400228 }
229
230 /**
231 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400232 *
Jason Monk361915c2017-03-21 20:33:59 -0400233 * @param keyguardShowing True if keyguard is showing
234 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400235 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
236 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400237 mKeyguardShowing = keyguardShowing;
238 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400239 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400240 if (mDialog != null) {
241 mDialog.dismiss();
242 mDialog = null;
243 // Show delayed, so that the dismiss of the previous dialog completes
244 mHandler.sendEmptyMessage(MESSAGE_SHOW);
245 } else {
246 handleShow();
247 }
248 }
249
Charles He9851a8d2017-10-10 17:31:30 +0100250 /**
251 * Dismiss the global actions dialog, if it's currently shown
252 */
253 public void dismissDialog() {
254 mHandler.removeMessages(MESSAGE_DISMISS);
255 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
256 }
257
Jason Monk361915c2017-03-21 20:33:59 -0400258 private void awakenIfNecessary() {
259 if (mDreamManager != null) {
260 try {
261 if (mDreamManager.isDreaming()) {
262 mDreamManager.awaken();
263 }
264 } catch (RemoteException e) {
265 // we tried
266 }
267 }
268 }
269
270 private void handleShow() {
271 awakenIfNecessary();
272 mDialog = createDialog();
273 prepareDialog();
274
275 // If we only have 1 item and it's a simple press action, just do this action.
276 if (mAdapter.getCount() == 1
277 && mAdapter.getItem(0) instanceof SinglePressAction
278 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
279 ((SinglePressAction) mAdapter.getItem(0)).onPress();
280 } else {
281 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
282 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100283 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400284 mDialog.getWindow().setAttributes(attrs);
285 mDialog.show();
286 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400287 }
288 }
289
290 /**
291 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400292 *
Jason Monk361915c2017-03-21 20:33:59 -0400293 * @return A new dialog.
294 */
295 private ActionsDialog createDialog() {
296 // Simple toggle style if there's no vibrator, otherwise use a tri-state
297 if (!mHasVibrator) {
298 mSilentModeAction = new SilentModeToggleAction();
299 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700300 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400301 }
302 mAirplaneModeOn = new ToggleAction(
303 R.drawable.ic_lock_airplane_mode,
304 R.drawable.ic_lock_airplane_mode_off,
305 R.string.global_actions_toggle_airplane_mode,
306 R.string.global_actions_airplane_mode_on_status,
307 R.string.global_actions_airplane_mode_off_status) {
308
309 void onToggle(boolean on) {
310 if (mHasTelephony && Boolean.parseBoolean(
311 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
312 mIsWaitingForEcmExit = true;
313 // Launch ECM exit dialog
314 Intent ecmDialogIntent =
315 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
316 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
317 mContext.startActivity(ecmDialogIntent);
318 } else {
319 changeAirplaneModeSystemSetting(on);
320 }
321 }
322
323 @Override
324 protected void changeStateFromPress(boolean buttonOn) {
325 if (!mHasTelephony) return;
326
327 // In ECM mode airplane state cannot be changed
328 if (!(Boolean.parseBoolean(
329 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
330 mState = buttonOn ? State.TurningOn : State.TurningOff;
331 mAirplaneState = mState;
332 }
333 }
334
335 public boolean showDuringKeyguard() {
336 return true;
337 }
338
339 public boolean showBeforeProvisioning() {
340 return false;
341 }
342 };
343 onAirplaneModeChanged();
344
345 mItems = new ArrayList<Action>();
346 String[] defaultActions = mContext.getResources().getStringArray(
347 R.array.config_globalActionsList);
348
349 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000350 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800351 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400352 for (int i = 0; i < defaultActions.length; i++) {
353 String actionKey = defaultActions[i];
354 if (addedKeys.contains(actionKey)) {
355 // If we already have added this, don't add it again.
356 continue;
357 }
358 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
359 mItems.add(new PowerAction());
360 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
361 mItems.add(mAirplaneModeOn);
362 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
363 if (Settings.Global.getInt(mContext.getContentResolver(),
364 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
365 mItems.add(new BugReportAction());
366 }
367 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
368 if (mShowSilentToggle) {
369 mItems.add(mSilentModeAction);
370 }
371 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
372 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
373 addUsersToMenu(mItems);
374 }
375 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
376 mItems.add(getSettingsAction());
377 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700378 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
379 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800380 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700381 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800382 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
385 mItems.add(getVoiceAssistAction());
386 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
387 mItems.add(getAssistAction());
388 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
389 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500390 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
391 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000392 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000393 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000394 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
395 mItems.add(new LogoutAction());
396 mHasLogoutButton = true;
397 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800398 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400399 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800400 mItems.add(new EmergencyDialerAction());
401 }
Jason Monk361915c2017-03-21 20:33:59 -0400402 } else {
403 Log.e(TAG, "Invalid global action key " + actionKey);
404 }
405 // Add here so we don't add more than one.
406 addedKeys.add(actionKey);
407 }
408
409 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400410 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400411 }
412
413 mAdapter = new MyAdapter();
414
Steve Elliott9b87a442019-03-05 10:24:16 -0500415 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400416 mPanelPlugin != null
417 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400418 new GlobalActionsPanelPlugin.Callbacks() {
419 @Override
420 public void dismissGlobalActionsMenu() {
421 if (mDialog != null) {
422 mDialog.dismiss();
423 }
424 }
425
426 @Override
427 public void startPendingIntentDismissingKeyguard(
428 PendingIntent intent) {
429 mActivityStarter
430 .startPendingIntentDismissingKeyguard(intent);
431 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400432 },
433 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500434 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400435
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400436 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400437 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700438 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400439
440 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800441 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400442
443 return dialog;
444 }
445
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800446 private boolean shouldDisplayLockdown() {
447 int userId = getCurrentUser().id;
448 // Lockdown is meaningless without a place to go.
449 if (!mKeyguardManager.isDeviceSecure(userId)) {
450 return false;
451 }
452
453 // Only show the lockdown button if the device isn't locked down (for whatever reason).
454 int state = mLockPatternUtils.getStrongAuthForUser(userId);
455 return (state == STRONG_AUTH_NOT_REQUIRED
456 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
457 }
458
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700459 @Override
460 public void onUiModeChanged() {
461 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700462 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400463 mDialog.refreshDialog();
464 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700465 }
466
467 public void destroy() {
468 Dependency.get(ConfigurationController.class).removeCallback(this);
469 }
470
Jason Monk361915c2017-03-21 20:33:59 -0400471 private final class PowerAction extends SinglePressAction implements LongPressAction {
472 private PowerAction() {
473 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400474 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400475 }
476
477 @Override
478 public boolean onLongPress() {
479 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
480 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
481 mWindowManagerFuncs.reboot(true);
482 return true;
483 }
484 return false;
485 }
486
487 @Override
488 public boolean showDuringKeyguard() {
489 return true;
490 }
491
492 @Override
493 public boolean showBeforeProvisioning() {
494 return true;
495 }
496
497 @Override
498 public void onPress() {
499 // shutdown by making sure radio and power are handled accordingly.
500 mWindowManagerFuncs.shutdown();
501 }
502 }
503
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400504 private abstract class EmergencyAction extends SinglePressAction {
505 EmergencyAction(int iconResId, int messageResId) {
506 super(iconResId, messageResId);
507 }
508
509 @Override
510 public boolean shouldBeSeparated() {
511 return shouldUseSeparatedView();
512 }
513
514 @Override
515 public View create(
516 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
517 View v = super.create(context, convertView, parent, inflater);
518 int textColor;
519 if (shouldBeSeparated()) {
520 textColor = v.getResources().getColor(
521 com.android.systemui.R.color.global_actions_alert_text);
522 } else {
523 textColor = v.getResources().getColor(
524 com.android.systemui.R.color.global_actions_text);
525 }
526 TextView messageView = v.findViewById(R.id.message);
527 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400528 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400529 ImageView icon = (ImageView) v.findViewById(R.id.icon);
530 icon.getDrawable().setTint(textColor);
531 return v;
532 }
533
534 @Override
535 public boolean showDuringKeyguard() {
536 return true;
537 }
538
539 @Override
540 public boolean showBeforeProvisioning() {
541 return true;
542 }
543 }
544
545 private class EmergencyAffordanceAction extends EmergencyAction {
546 EmergencyAffordanceAction() {
547 super(R.drawable.emergency_icon,
548 R.string.global_action_emergency);
549 }
550
551 @Override
552 public void onPress() {
553 mEmergencyAffordanceManager.performEmergencyCall();
554 }
555 }
556
557 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800558 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400559 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800560 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800561 }
562
563 @Override
564 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800565 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800566 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800567 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
568 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
569 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800570 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
571 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800572 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
573 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800574 }
575
Jason Monk361915c2017-03-21 20:33:59 -0400576 private final class RestartAction extends SinglePressAction implements LongPressAction {
577 private RestartAction() {
578 super(R.drawable.ic_restart, R.string.global_action_restart);
579 }
580
581 @Override
582 public boolean onLongPress() {
583 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
584 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
585 mWindowManagerFuncs.reboot(true);
586 return true;
587 }
588 return false;
589 }
590
591 @Override
592 public boolean showDuringKeyguard() {
593 return true;
594 }
595
596 @Override
597 public boolean showBeforeProvisioning() {
598 return true;
599 }
600
601 @Override
602 public void onPress() {
603 mWindowManagerFuncs.reboot(false);
604 }
605 }
606
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400607 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500608 public ScreenshotAction() {
609 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
610 }
611
612 @Override
613 public void onPress() {
614 // Add a little delay before executing, to give the
615 // dialog a chance to go away before it takes a
616 // screenshot.
617 // TODO: instead, omit global action dialog layer
618 mHandler.postDelayed(new Runnable() {
619 @Override
620 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400621 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500622 MetricsLogger.action(mContext,
623 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
624 }
625 }, 500);
626 }
627
628 @Override
629 public boolean showDuringKeyguard() {
630 return true;
631 }
632
633 @Override
634 public boolean showBeforeProvisioning() {
635 return false;
636 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400637
638 @Override
639 public boolean onLongPress() {
640 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
641 mScreenRecordHelper.launchRecordPrompt();
642 } else {
643 onPress();
644 }
645 return true;
646 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500647 }
648
Jason Monk361915c2017-03-21 20:33:59 -0400649 private class BugReportAction extends SinglePressAction implements LongPressAction {
650
651 public BugReportAction() {
652 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
653 }
654
655 @Override
656 public void onPress() {
657 // don't actually trigger the bugreport if we are running stability
658 // tests via monkey
659 if (ActivityManager.isUserAMonkey()) {
660 return;
661 }
662 // Add a little delay before executing, to give the
663 // dialog a chance to go away before it takes a
664 // screenshot.
665 mHandler.postDelayed(new Runnable() {
666 @Override
667 public void run() {
668 try {
669 // Take an "interactive" bugreport.
670 MetricsLogger.action(mContext,
671 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100672 ActivityManager.getService().requestInteractiveBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400673 } 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);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100689 ActivityManager.getService().requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400690 } catch (RemoteException e) {
691 }
692 return false;
693 }
694
695 public boolean showDuringKeyguard() {
696 return true;
697 }
698
699 @Override
700 public boolean showBeforeProvisioning() {
701 return false;
702 }
Jason Monk361915c2017-03-21 20:33:59 -0400703 }
704
Alex Chau04458852017-11-27 18:21:23 +0000705 private final class LogoutAction extends SinglePressAction {
706 private LogoutAction() {
707 super(R.drawable.ic_logout, R.string.global_action_logout);
708 }
709
710 @Override
711 public boolean showDuringKeyguard() {
712 return true;
713 }
714
715 @Override
716 public boolean showBeforeProvisioning() {
717 return false;
718 }
719
720 @Override
721 public void onPress() {
722 // Add a little delay before executing, to give the dialog a chance to go away before
723 // switching user
724 mHandler.postDelayed(() -> {
725 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000726 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000727 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000728 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000729 } catch (RemoteException re) {
730 Log.e(TAG, "Couldn't logout user " + re);
731 }
732 }, 500);
733 }
734 }
735
Jason Monk361915c2017-03-21 20:33:59 -0400736 private Action getSettingsAction() {
737 return new SinglePressAction(R.drawable.ic_settings,
738 R.string.global_action_settings) {
739
740 @Override
741 public void onPress() {
742 Intent intent = new Intent(Settings.ACTION_SETTINGS);
743 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
744 mContext.startActivity(intent);
745 }
746
747 @Override
748 public boolean showDuringKeyguard() {
749 return true;
750 }
751
752 @Override
753 public boolean showBeforeProvisioning() {
754 return true;
755 }
756 };
757 }
758
Jason Monk361915c2017-03-21 20:33:59 -0400759 private Action getAssistAction() {
760 return new SinglePressAction(R.drawable.ic_action_assist_focused,
761 R.string.global_action_assist) {
762 @Override
763 public void onPress() {
764 Intent intent = new Intent(Intent.ACTION_ASSIST);
765 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
766 mContext.startActivity(intent);
767 }
768
769 @Override
770 public boolean showDuringKeyguard() {
771 return true;
772 }
773
774 @Override
775 public boolean showBeforeProvisioning() {
776 return true;
777 }
778 };
779 }
780
781 private Action getVoiceAssistAction() {
782 return new SinglePressAction(R.drawable.ic_voice_search,
783 R.string.global_action_voice_assist) {
784 @Override
785 public void onPress() {
786 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
787 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
788 mContext.startActivity(intent);
789 }
790
791 @Override
792 public boolean showDuringKeyguard() {
793 return true;
794 }
795
796 @Override
797 public boolean showBeforeProvisioning() {
798 return true;
799 }
800 };
801 }
802
803 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400804 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400805 R.string.global_action_lockdown) {
806
807 @Override
808 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700809 new LockPatternUtils(mContext)
810 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
811 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400812 try {
813 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100814 // Lock profiles (if any) on the background thread.
815 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
816 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400817 } catch (RemoteException e) {
818 Log.e(TAG, "Error while trying to lock device.", e);
819 }
820 }
821
822 @Override
823 public boolean showDuringKeyguard() {
824 return true;
825 }
826
827 @Override
828 public boolean showBeforeProvisioning() {
829 return false;
830 }
831 };
832 }
833
Pavel Grafov059021b2018-05-02 13:44:46 +0100834 private void lockProfiles() {
835 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
836 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
837 final int currentUserId = getCurrentUser().id;
838 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
839 for (final int id : profileIds) {
840 if (id != currentUserId) {
841 tm.setDeviceLockedForUser(id, true);
842 }
843 }
844 }
845
Jason Monk361915c2017-03-21 20:33:59 -0400846 private UserInfo getCurrentUser() {
847 try {
848 return ActivityManager.getService().getCurrentUser();
849 } catch (RemoteException re) {
850 return null;
851 }
852 }
853
854 private boolean isCurrentUserOwner() {
855 UserInfo currentUser = getCurrentUser();
856 return currentUser == null || currentUser.isPrimary();
857 }
858
859 private void addUsersToMenu(ArrayList<Action> items) {
860 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
861 if (um.isUserSwitcherEnabled()) {
862 List<UserInfo> users = um.getUsers();
863 UserInfo currentUser = getCurrentUser();
864 for (final UserInfo user : users) {
865 if (user.supportsSwitchToByUser()) {
866 boolean isCurrentUser = currentUser == null
867 ? user.id == 0 : (currentUser.id == user.id);
868 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
869 : null;
870 SinglePressAction switchToUser = new SinglePressAction(
871 R.drawable.ic_menu_cc, icon,
872 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400873 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400874 public void onPress() {
875 try {
876 ActivityManager.getService().switchUser(user.id);
877 } catch (RemoteException re) {
878 Log.e(TAG, "Couldn't switch user " + re);
879 }
880 }
881
882 public boolean showDuringKeyguard() {
883 return true;
884 }
885
886 public boolean showBeforeProvisioning() {
887 return false;
888 }
889 };
890 items.add(switchToUser);
891 }
892 }
893 }
894 }
895
896 private void prepareDialog() {
897 refreshSilentMode();
898 mAirplaneModeOn.updateState(mAirplaneState);
899 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400900 if (mShowSilentToggle) {
901 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
902 mContext.registerReceiver(mRingerModeReceiver, filter);
903 }
904 }
905
906 private void refreshSilentMode() {
907 if (!mHasVibrator) {
908 final boolean silentModeOn =
909 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400910 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400911 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
912 }
913 }
914
915 /** {@inheritDoc} */
916 public void onDismiss(DialogInterface dialog) {
917 mWindowManagerFuncs.onGlobalActionsHidden();
918 if (mShowSilentToggle) {
919 try {
920 mContext.unregisterReceiver(mRingerModeReceiver);
921 } catch (IllegalArgumentException ie) {
922 // ignore this
923 Log.w(TAG, ie);
924 }
925 }
926 }
927
928 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800929 public void onShow(DialogInterface dialog) {
930 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
931 }
932
Jason Monk361915c2017-03-21 20:33:59 -0400933 /**
934 * The adapter used for the list within the global actions dialog, taking
935 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400936 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
937 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400938 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
939 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500940 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400941 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400942 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400943 for (int i = 0; i < mItems.size(); i++) {
944 final Action action = mItems.get(i);
945
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400946 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
947 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400948 }
Jason Monk361915c2017-03-21 20:33:59 -0400949 }
950 return count;
951 }
952
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400953 private boolean shouldBeShown(Action action) {
954 if (mKeyguardShowing && !action.showDuringKeyguard()) {
955 return false;
956 }
957 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
958 return false;
959 }
960 return true;
961 }
962
963 @Override
964 public int countSeparatedItems() {
965 return countItems(true);
966 }
967
968 @Override
969 public int countListItems() {
970 return countItems(false);
971 }
972
973 @Override
974 public int getCount() {
975 return countSeparatedItems() + countListItems();
976 }
977
Jason Monk361915c2017-03-21 20:33:59 -0400978 @Override
979 public boolean isEnabled(int position) {
980 return getItem(position).isEnabled();
981 }
982
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500983 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400984 public boolean areAllItemsEnabled() {
985 return false;
986 }
987
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500988 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400989 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400990 int filteredPos = 0;
991 for (int i = 0; i < mItems.size(); i++) {
992 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400993 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400994 continue;
995 }
996 if (filteredPos == position) {
997 return action;
998 }
999 filteredPos++;
1000 }
1001
1002 throw new IllegalArgumentException("position " + position
1003 + " out of range of showable actions"
1004 + ", filtered count=" + getCount()
1005 + ", keyguardshowing=" + mKeyguardShowing
1006 + ", provisioned=" + mDeviceProvisioned);
1007 }
1008
1009
1010 public long getItemId(int position) {
1011 return position;
1012 }
1013
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001014 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001015 public View getView(int position, View convertView, ViewGroup parent) {
1016 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001017 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001018 view.setOnClickListener(v -> onClickItem(position));
1019 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001020 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001021 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001022
1023 @Override
1024 public boolean onLongClickItem(int position) {
1025 final Action action = mAdapter.getItem(position);
1026 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001027 if (mDialog != null) {
1028 mDialog.dismiss();
1029 } else {
1030 Log.w(TAG, "Action long-clicked while mDialog is null.");
1031 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001032 return ((LongPressAction) action).onLongPress();
1033 }
1034 return false;
1035 }
1036
1037 @Override
1038 public void onClickItem(int position) {
1039 Action item = mAdapter.getItem(position);
1040 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001041 if (mDialog != null) {
1042 mDialog.dismiss();
1043 } else {
1044 Log.w(TAG, "Action clicked while mDialog is null.");
1045 }
1046 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001047 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001048 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001049
1050 @Override
1051 public boolean shouldBeSeparated(int position) {
1052 return getItem(position).shouldBeSeparated();
1053 }
Jason Monk361915c2017-03-21 20:33:59 -04001054 }
1055
1056 // note: the scheme below made more sense when we were planning on having
1057 // 8 different things in the global actions dialog. seems overkill with
1058 // only 3 items now, but may as well keep this flexible approach so it will
1059 // be easy should someone decide at the last minute to include something
1060 // else, such as 'enable wifi', or 'enable bluetooth'
1061
1062 /**
1063 * What each item in the global actions dialog must be able to support.
1064 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001065 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001066 /**
1067 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001068 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001069 */
1070 CharSequence getLabelForAccessibility(Context context);
1071
1072 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1073
1074 void onPress();
1075
1076 /**
1077 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001078 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001079 */
1080 boolean showDuringKeyguard();
1081
1082 /**
1083 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001084 * device is provisioned.onlongpress
1085 *
Jason Monk361915c2017-03-21 20:33:59 -04001086 */
1087 boolean showBeforeProvisioning();
1088
1089 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001090
1091 default boolean shouldBeSeparated() {
1092 return false;
1093 }
Jason Monk361915c2017-03-21 20:33:59 -04001094 }
1095
1096 /**
1097 * An action that also supports long press.
1098 */
1099 private interface LongPressAction extends Action {
1100 boolean onLongPress();
1101 }
1102
1103 /**
1104 * A single press action maintains no state, just responds to a press
1105 * and takes an action.
1106 */
1107 private static abstract class SinglePressAction implements Action {
1108 private final int mIconResId;
1109 private final Drawable mIcon;
1110 private final int mMessageResId;
1111 private final CharSequence mMessage;
1112
1113 protected SinglePressAction(int iconResId, int messageResId) {
1114 mIconResId = iconResId;
1115 mMessageResId = messageResId;
1116 mMessage = null;
1117 mIcon = null;
1118 }
1119
1120 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1121 mIconResId = iconResId;
1122 mMessageResId = 0;
1123 mMessage = message;
1124 mIcon = icon;
1125 }
1126
1127 public boolean isEnabled() {
1128 return true;
1129 }
1130
1131 public String getStatus() {
1132 return null;
1133 }
1134
1135 abstract public void onPress();
1136
1137 public CharSequence getLabelForAccessibility(Context context) {
1138 if (mMessage != null) {
1139 return mMessage;
1140 } else {
1141 return context.getString(mMessageResId);
1142 }
1143 }
1144
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001145 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001146 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001147 }
1148
Jason Monk361915c2017-03-21 20:33:59 -04001149 public View create(
1150 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001151 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001152 false);
Jason Monk361915c2017-03-21 20:33:59 -04001153
1154 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1155 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001156 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001157
1158 TextView statusView = (TextView) v.findViewById(R.id.status);
1159 final String status = getStatus();
1160 if (!TextUtils.isEmpty(status)) {
1161 statusView.setText(status);
1162 } else {
1163 statusView.setVisibility(View.GONE);
1164 }
1165 if (mIcon != null) {
1166 icon.setImageDrawable(mIcon);
1167 icon.setScaleType(ScaleType.CENTER_CROP);
1168 } else if (mIconResId != 0) {
1169 icon.setImageDrawable(context.getDrawable(mIconResId));
1170 }
1171 if (mMessage != null) {
1172 messageView.setText(mMessage);
1173 } else {
1174 messageView.setText(mMessageResId);
1175 }
1176
1177 return v;
1178 }
1179 }
1180
1181 /**
1182 * A toggle action knows whether it is on or off, and displays an icon
1183 * and status message accordingly.
1184 */
1185 private static abstract class ToggleAction implements Action {
1186
1187 enum State {
1188 Off(false),
1189 TurningOn(true),
1190 TurningOff(true),
1191 On(false);
1192
1193 private final boolean inTransition;
1194
1195 State(boolean intermediate) {
1196 inTransition = intermediate;
1197 }
1198
1199 public boolean inTransition() {
1200 return inTransition;
1201 }
1202 }
1203
1204 protected State mState = State.Off;
1205
1206 // prefs
1207 protected int mEnabledIconResId;
1208 protected int mDisabledIconResid;
1209 protected int mMessageResId;
1210 protected int mEnabledStatusMessageResId;
1211 protected int mDisabledStatusMessageResId;
1212
1213 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001214 * @param enabledIconResId The icon for when this action is on.
1215 * @param disabledIconResid The icon for when this action is off.
1216 * @param message The general information message, e.g 'Silent Mode'
1217 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001218 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1219 */
1220 public ToggleAction(int enabledIconResId,
1221 int disabledIconResid,
1222 int message,
1223 int enabledStatusMessageResId,
1224 int disabledStatusMessageResId) {
1225 mEnabledIconResId = enabledIconResId;
1226 mDisabledIconResid = disabledIconResid;
1227 mMessageResId = message;
1228 mEnabledStatusMessageResId = enabledStatusMessageResId;
1229 mDisabledStatusMessageResId = disabledStatusMessageResId;
1230 }
1231
1232 /**
1233 * Override to make changes to resource IDs just before creating the
1234 * View.
1235 */
1236 void willCreate() {
1237
1238 }
1239
1240 @Override
1241 public CharSequence getLabelForAccessibility(Context context) {
1242 return context.getString(mMessageResId);
1243 }
1244
1245 public View create(Context context, View convertView, ViewGroup parent,
1246 LayoutInflater inflater) {
1247 willCreate();
1248
1249 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001250 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001251
1252 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1253 TextView messageView = (TextView) v.findViewById(R.id.message);
1254 TextView statusView = (TextView) v.findViewById(R.id.status);
1255 final boolean enabled = isEnabled();
1256
1257 if (messageView != null) {
1258 messageView.setText(mMessageResId);
1259 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001260 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001261 }
1262
1263 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1264 if (icon != null) {
1265 icon.setImageDrawable(context.getDrawable(
1266 (on ? mEnabledIconResId : mDisabledIconResid)));
1267 icon.setEnabled(enabled);
1268 }
1269
1270 if (statusView != null) {
1271 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1272 statusView.setVisibility(View.VISIBLE);
1273 statusView.setEnabled(enabled);
1274 }
1275 v.setEnabled(enabled);
1276
1277 return v;
1278 }
1279
1280 public final void onPress() {
1281 if (mState.inTransition()) {
1282 Log.w(TAG, "shouldn't be able to toggle when in transition");
1283 return;
1284 }
1285
1286 final boolean nowOn = !(mState == State.On);
1287 onToggle(nowOn);
1288 changeStateFromPress(nowOn);
1289 }
1290
1291 public boolean isEnabled() {
1292 return !mState.inTransition();
1293 }
1294
1295 /**
1296 * Implementations may override this if their state can be in on of the intermediate
1297 * states until some notification is received (e.g airplane mode is 'turning off' until
1298 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001299 *
Jason Monk361915c2017-03-21 20:33:59 -04001300 * @param buttonOn Whether the button was turned on or off
1301 */
1302 protected void changeStateFromPress(boolean buttonOn) {
1303 mState = buttonOn ? State.On : State.Off;
1304 }
1305
1306 abstract void onToggle(boolean on);
1307
1308 public void updateState(State state) {
1309 mState = state;
1310 }
1311 }
1312
1313 private class SilentModeToggleAction extends ToggleAction {
1314 public SilentModeToggleAction() {
1315 super(R.drawable.ic_audio_vol_mute,
1316 R.drawable.ic_audio_vol,
1317 R.string.global_action_toggle_silent_mode,
1318 R.string.global_action_silent_mode_on_status,
1319 R.string.global_action_silent_mode_off_status);
1320 }
1321
1322 void onToggle(boolean on) {
1323 if (on) {
1324 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1325 } else {
1326 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1327 }
1328 }
1329
1330 public boolean showDuringKeyguard() {
1331 return true;
1332 }
1333
1334 public boolean showBeforeProvisioning() {
1335 return false;
1336 }
1337 }
1338
1339 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1340
Jason Monk16fbd9d2017-04-27 14:28:49 -04001341 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001342
1343 private final AudioManager mAudioManager;
1344 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001345
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001346 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001347 mAudioManager = audioManager;
1348 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001349 }
1350
1351 private int ringerModeToIndex(int ringerMode) {
1352 // They just happen to coincide
1353 return ringerMode;
1354 }
1355
1356 private int indexToRingerMode(int index) {
1357 // They just happen to coincide
1358 return index;
1359 }
1360
1361 @Override
1362 public CharSequence getLabelForAccessibility(Context context) {
1363 return null;
1364 }
1365
1366 public View create(Context context, View convertView, ViewGroup parent,
1367 LayoutInflater inflater) {
1368 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1369
1370 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1371 for (int i = 0; i < 3; i++) {
1372 View itemView = v.findViewById(ITEM_IDS[i]);
1373 itemView.setSelected(selectedIndex == i);
1374 // Set up click handler
1375 itemView.setTag(i);
1376 itemView.setOnClickListener(this);
1377 }
1378 return v;
1379 }
1380
1381 public void onPress() {
1382 }
1383
1384 public boolean showDuringKeyguard() {
1385 return true;
1386 }
1387
1388 public boolean showBeforeProvisioning() {
1389 return false;
1390 }
1391
1392 public boolean isEnabled() {
1393 return true;
1394 }
1395
1396 void willCreate() {
1397 }
1398
1399 public void onClick(View v) {
1400 if (!(v.getTag() instanceof Integer)) return;
1401
1402 int index = (Integer) v.getTag();
1403 mAudioManager.setRingerMode(indexToRingerMode(index));
1404 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1405 }
1406 }
1407
1408 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1409 public void onReceive(Context context, Intent intent) {
1410 String action = intent.getAction();
1411 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1412 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1413 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1414 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001415 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001416 }
1417 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1418 // Airplane mode can be changed after ECM exits if airplane toggle button
1419 // is pressed during ECM mode
1420 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1421 mIsWaitingForEcmExit) {
1422 mIsWaitingForEcmExit = false;
1423 changeAirplaneModeSystemSetting(true);
1424 }
1425 }
1426 }
1427 };
1428
1429 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1430 @Override
1431 public void onServiceStateChanged(ServiceState serviceState) {
1432 if (!mHasTelephony) return;
1433 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1434 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1435 mAirplaneModeOn.updateState(mAirplaneState);
1436 mAdapter.notifyDataSetChanged();
1437 }
1438 };
1439
1440 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1441 @Override
1442 public void onReceive(Context context, Intent intent) {
1443 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1444 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1445 }
1446 }
1447 };
1448
1449 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1450 @Override
1451 public void onChange(boolean selfChange) {
1452 onAirplaneModeChanged();
1453 }
1454 };
1455
1456 private static final int MESSAGE_DISMISS = 0;
1457 private static final int MESSAGE_REFRESH = 1;
1458 private static final int MESSAGE_SHOW = 2;
1459 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1460
1461 private Handler mHandler = new Handler() {
1462 public void handleMessage(Message msg) {
1463 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001464 case MESSAGE_DISMISS:
1465 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001466 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1467 mDialog.dismissImmediately();
1468 } else {
1469 mDialog.dismiss();
1470 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001471 mDialog = null;
1472 }
1473 break;
1474 case MESSAGE_REFRESH:
1475 refreshSilentMode();
1476 mAdapter.notifyDataSetChanged();
1477 break;
1478 case MESSAGE_SHOW:
1479 handleShow();
1480 break;
Jason Monk361915c2017-03-21 20:33:59 -04001481 }
1482 }
1483 };
1484
1485 private void onAirplaneModeChanged() {
1486 // Let the service state callbacks handle the state.
1487 if (mHasTelephony) return;
1488
1489 boolean airplaneModeOn = Settings.Global.getInt(
1490 mContext.getContentResolver(),
1491 Settings.Global.AIRPLANE_MODE_ON,
1492 0) == 1;
1493 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1494 mAirplaneModeOn.updateState(mAirplaneState);
1495 }
1496
1497 /**
1498 * Change the airplane mode system setting
1499 */
1500 private void changeAirplaneModeSystemSetting(boolean on) {
1501 Settings.Global.putInt(
1502 mContext.getContentResolver(),
1503 Settings.Global.AIRPLANE_MODE_ON,
1504 on ? 1 : 0);
1505 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1506 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1507 intent.putExtra("state", on);
1508 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1509 if (!mHasTelephony) {
1510 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1511 }
1512 }
1513
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001514 private static final class ActionsDialog extends Dialog implements DialogInterface,
1515 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001516
Jason Monk16fbd9d2017-04-27 14:28:49 -04001517 private final Context mContext;
1518 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001519 private final IStatusBarService mStatusBarService;
1520 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001521 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001522 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001523 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001524 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001525 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001526 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001527 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001528 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001529 private boolean mHadTopUi;
1530 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001531
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001532 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001533 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001534 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001535 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001536 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001537 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001538 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001539 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001540
1541 // Window initialization
1542 Window window = getWindow();
1543 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001544 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1545 window.getDecorView();
1546 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1547 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1548 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1549 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1550 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001551 window.addFlags(
1552 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001553 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001554 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001555 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1556 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1557 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001558 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001559 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001560
1561 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001562 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001563 }
1564
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001565 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001566 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001567 }
1568
1569 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001570 int rotation = RotationUtils.getRotation(mContext);
1571 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1572 if (rotation != RotationUtils.ROTATION_NONE) {
1573 if (rotationLocked) {
1574 if (mResetOrientationData == null) {
1575 mResetOrientationData = new ResetOrientationData();
1576 mResetOrientationData.locked = true;
1577 mResetOrientationData.rotation = rotation;
1578 }
1579
1580 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001581 // This call is posted so that the rotation does not change until post-layout,
1582 // otherwise onConfigurationChanged() may not get invoked.
1583 mGlobalActionsLayout.post(() ->
1584 RotationPolicy.setRotationLockAtAngle(
1585 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001586 }
1587 } else {
1588 if (!rotationLocked) {
1589 if (mResetOrientationData == null) {
1590 mResetOrientationData = new ResetOrientationData();
1591 mResetOrientationData.locked = false;
1592 }
1593
1594 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001595 // This call is posted so that the rotation does not change until post-layout,
1596 // otherwise onConfigurationChanged() may not get invoked.
1597 mGlobalActionsLayout.post(() ->
1598 RotationPolicy.setRotationLockAtAngle(
1599 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001600 }
1601
Steve Elliott53f12ae2019-05-13 17:14:15 -04001602 // Disable rotation suggestions, if enabled
1603 setRotationSuggestionsEnabled(false);
1604
Steve Elliott300b48f2019-05-29 14:13:50 -04001605 FrameLayout panelContainer =
1606 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001607 FrameLayout.LayoutParams panelParams =
1608 new FrameLayout.LayoutParams(
1609 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001610 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001611 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001612 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1613 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001614 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001615 }
1616
1617 private void initializeLayout() {
1618 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001619 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001620 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001621 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001622 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001623 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001624 @Override
1625 public boolean dispatchPopulateAccessibilityEvent(
1626 View host, AccessibilityEvent event) {
1627 // Populate the title here, just as Activity does
1628 event.getText().add(mContext.getString(R.string.global_actions));
1629 return true;
1630 }
1631 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001632 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001633 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001634
Steve Elliott86ef6282019-05-08 14:45:04 -04001635 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001636 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001637 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001638 if (mBackgroundDrawable == null) {
1639 mBackgroundDrawable = new ScrimDrawable();
1640 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1641 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001642 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001643 }
1644
Steve Elliott300b48f2019-05-29 14:13:50 -04001645 private void fixNavBarClipping() {
1646 ViewGroup content = findViewById(android.R.id.content);
1647 content.setClipChildren(false);
1648 content.setClipToPadding(false);
1649 ViewGroup contentParent = (ViewGroup) content.getParent();
1650 contentParent.setClipChildren(false);
1651 contentParent.setClipToPadding(false);
1652 }
1653
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001654 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001655 int rotation = RotationUtils.getRotation(context);
1656 boolean useGridLayout = isForceGridEnabled(context)
1657 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1658 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001659 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001660 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001661 } else {
1662 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001663 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001664 } else {
1665 if (useGridLayout) {
1666 return com.android.systemui.R.layout.global_actions_grid;
1667 } else {
1668 return com.android.systemui.R.layout.global_actions_column;
1669 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001670 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001671 }
1672
Jason Monk361915c2017-03-21 20:33:59 -04001673 @Override
1674 protected void onStart() {
1675 super.setCanceledOnTouchOutside(true);
1676 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001677 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001678
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001679 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001680 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001681 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001682 updateColors(colors, false /* animate */);
1683 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001684 }
1685
1686 /**
1687 * Updates background and system bars according to current GradientColors.
1688 * @param colors Colors and hints to use.
1689 * @param animate Interpolates gradient if true, just sets otherwise.
1690 */
1691 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001692 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001693 return;
1694 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001695 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001696 View decorView = getWindow().getDecorView();
1697 if (colors.supportsDarkText()) {
1698 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001699 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001700 } else {
1701 decorView.setSystemUiVisibility(0);
1702 }
Jason Monk361915c2017-03-21 20:33:59 -04001703 }
1704
1705 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001706 protected void onStop() {
1707 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001708 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001709 }
1710
1711 @Override
1712 public void show() {
1713 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001714 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001715 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1716 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001717 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001718 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1719 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001720 mGlobalActionsLayout.setAlpha(0);
1721 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001722 .alpha(1)
1723 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001724 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001725 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001726 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001727 .setUpdateListener(animation -> {
1728 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001729 * mScrimAlpha * 255);
1730 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001731 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001732 .start();
1733 }
1734
1735 @Override
1736 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001737 if (!mShowing) {
1738 return;
1739 }
1740 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001741 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001742 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001743 mGlobalActionsLayout.setAlpha(1);
1744 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001745 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001746 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1747 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001748 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001749 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001750 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001751 .setUpdateListener(animation -> {
1752 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001753 * mScrimAlpha * 255);
1754 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001755 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001756 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001757 dismissPanel();
1758 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001759 }
1760
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001761 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001762 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001763 dismissPanel();
1764 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001765 completeDismiss();
1766 }
1767
1768 private void completeDismiss() {
1769 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1770 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001771 }
1772
1773 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001774 if (mPanelController != null) {
1775 mPanelController.onDismissed();
1776 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001777 }
1778
Steve Elliott53f12ae2019-05-13 17:14:15 -04001779 private void setRotationSuggestionsEnabled(boolean enabled) {
1780 try {
1781 final int userId = Binder.getCallingUserHandle().getIdentifier();
1782 final int what = enabled
1783 ? StatusBarManager.DISABLE2_NONE
1784 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1785 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1786 } catch (RemoteException ex) {
1787 throw ex.rethrowFromSystemServer();
1788 }
1789 }
1790
Steve Elliott48f75db2019-05-03 15:03:38 -04001791 private void resetOrientation() {
1792 if (mResetOrientationData != null) {
1793 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1794 mResetOrientationData.rotation);
1795 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001796 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001797 }
1798
Jason Monk361915c2017-03-21 20:33:59 -04001799 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001800 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001801 if (mKeyguardShowing) {
1802 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001803 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1804 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001805 }
1806 } else {
1807 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001808 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1809 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001810 }
1811 }
1812 }
1813
1814 public void setKeyguardShowing(boolean keyguardShowing) {
1815 mKeyguardShowing = keyguardShowing;
1816 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001817
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001818 public void refreshDialog() {
1819 initializeLayout();
1820 mGlobalActionsLayout.updateList();
1821 }
1822
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001823 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001824 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001825 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001826 }
1827 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001828
1829 private static class ResetOrientationData {
1830 public boolean locked;
1831 public int rotation;
1832 }
Jason Monk361915c2017-03-21 20:33:59 -04001833 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001834
1835 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001836 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1837 */
1838 private static boolean isPanelDebugModeEnabled(Context context) {
1839 return Settings.Secure.getInt(context.getContentResolver(),
1840 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1841 }
1842
1843 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001844 * Determines whether or not the Global Actions menu should be forced to
1845 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001846 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001847 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001848 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001849 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001850
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001851
1852 /**
1853 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1854 */
1855 private static boolean shouldUseSeparatedView() {
1856 return true;
1857 }
Jason Monk361915c2017-03-21 20:33:59 -04001858}