blob: ff16ed0f14772267a25ecccff06262b5e2fe2503 [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 Elliott92475042019-05-06 18:56:40 -040098import com.android.systemui.statusbar.phone.UnlockMethodCache;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070099import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +0800100import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500101import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500102import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700103
Jason Monk361915c2017-03-21 20:33:59 -0400104import java.util.ArrayList;
105import java.util.List;
106
107/**
108 * Helper to show the global actions dialog. Each item is an {@link Action} that
109 * may show depending on whether the keyguard is showing, and whether the device
110 * is provisioned.
111 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500112public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
113 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400114
115 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
116 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700117 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400118
119 private static final String TAG = "GlobalActionsDialog";
120
121 private static final boolean SHOW_SILENT_TOGGLE = true;
122
123 /* Valid settings for global actions keys.
124 * see config.xml config_globalActionList */
125 private static final String GLOBAL_ACTION_KEY_POWER = "power";
126 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
127 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
128 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
129 private static final String GLOBAL_ACTION_KEY_USERS = "users";
130 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
131 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
132 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
133 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
134 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000135 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800136 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500137 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400138
139 private final Context mContext;
140 private final GlobalActionsManager mWindowManagerFuncs;
141 private final AudioManager mAudioManager;
142 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000143 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800144 private final LockPatternUtils mLockPatternUtils;
145 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400146
147 private ArrayList<Action> mItems;
148 private ActionsDialog mDialog;
149
150 private Action mSilentModeAction;
151 private ToggleAction mAirplaneModeOn;
152
153 private MyAdapter mAdapter;
154
155 private boolean mKeyguardShowing = false;
156 private boolean mDeviceProvisioned = false;
157 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
158 private boolean mIsWaitingForEcmExit = false;
159 private boolean mHasTelephony;
160 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000161 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800162 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400163 private final boolean mShowSilentToggle;
164 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500165 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400166 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400167 private final ActivityStarter mActivityStarter;
168 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500169
Jason Monk361915c2017-03-21 20:33:59 -0400170 /**
171 * @param context everything needs a context :(
172 */
173 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700174 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400175 mWindowManagerFuncs = windowManagerFuncs;
176 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
177 mDreamManager = IDreamManager.Stub.asInterface(
178 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000179 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
180 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800181 mLockPatternUtils = new LockPatternUtils(mContext);
182 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400183
184 // receive broadcasts
185 IntentFilter filter = new IntentFilter();
186 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
187 filter.addAction(Intent.ACTION_SCREEN_OFF);
188 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
189 context.registerReceiver(mBroadcastReceiver, filter);
190
191 ConnectivityManager cm = (ConnectivityManager)
192 context.getSystemService(Context.CONNECTIVITY_SERVICE);
193 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
194
195 // get notified of phone state changes
196 TelephonyManager telephonyManager =
197 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
198 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
199 mContext.getContentResolver().registerContentObserver(
200 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
201 mAirplaneModeObserver);
202 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
203 mHasVibrator = vibrator != null && vibrator.hasVibrator();
204
205 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
206 R.bool.config_useFixedVolume);
207
208 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500209 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400210 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700211
212 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500213
Steve Elliott4c868852019-03-14 16:25:41 -0400214 mActivityStarter = Dependency.get(ActivityStarter.class);
Steve Elliott92475042019-05-06 18:56:40 -0400215 UnlockMethodCache unlockMethodCache = UnlockMethodCache.getInstance(context);
216 unlockMethodCache.addListener(
217 () -> {
218 if (mDialog != null && mDialog.mPanelController != null) {
219 boolean locked = !unlockMethodCache.canSkipBouncer();
220 mDialog.mPanelController.onDeviceLockStateChanged(locked);
221 }
222 });
Jason Monk361915c2017-03-21 20:33:59 -0400223 }
224
225 /**
226 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400227 *
Jason Monk361915c2017-03-21 20:33:59 -0400228 * @param keyguardShowing True if keyguard is showing
229 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400230 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
231 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400232 mKeyguardShowing = keyguardShowing;
233 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400234 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400235 if (mDialog != null) {
236 mDialog.dismiss();
237 mDialog = null;
238 // Show delayed, so that the dismiss of the previous dialog completes
239 mHandler.sendEmptyMessage(MESSAGE_SHOW);
240 } else {
241 handleShow();
242 }
243 }
244
Charles He9851a8d2017-10-10 17:31:30 +0100245 /**
246 * Dismiss the global actions dialog, if it's currently shown
247 */
248 public void dismissDialog() {
249 mHandler.removeMessages(MESSAGE_DISMISS);
250 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
251 }
252
Jason Monk361915c2017-03-21 20:33:59 -0400253 private void awakenIfNecessary() {
254 if (mDreamManager != null) {
255 try {
256 if (mDreamManager.isDreaming()) {
257 mDreamManager.awaken();
258 }
259 } catch (RemoteException e) {
260 // we tried
261 }
262 }
263 }
264
265 private void handleShow() {
266 awakenIfNecessary();
267 mDialog = createDialog();
268 prepareDialog();
269
270 // If we only have 1 item and it's a simple press action, just do this action.
271 if (mAdapter.getCount() == 1
272 && mAdapter.getItem(0) instanceof SinglePressAction
273 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
274 ((SinglePressAction) mAdapter.getItem(0)).onPress();
275 } else {
276 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
277 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100278 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400279 mDialog.getWindow().setAttributes(attrs);
280 mDialog.show();
281 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400282 }
283 }
284
285 /**
286 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400287 *
Jason Monk361915c2017-03-21 20:33:59 -0400288 * @return A new dialog.
289 */
290 private ActionsDialog createDialog() {
291 // Simple toggle style if there's no vibrator, otherwise use a tri-state
292 if (!mHasVibrator) {
293 mSilentModeAction = new SilentModeToggleAction();
294 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700295 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400296 }
297 mAirplaneModeOn = new ToggleAction(
298 R.drawable.ic_lock_airplane_mode,
299 R.drawable.ic_lock_airplane_mode_off,
300 R.string.global_actions_toggle_airplane_mode,
301 R.string.global_actions_airplane_mode_on_status,
302 R.string.global_actions_airplane_mode_off_status) {
303
304 void onToggle(boolean on) {
305 if (mHasTelephony && Boolean.parseBoolean(
306 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
307 mIsWaitingForEcmExit = true;
308 // Launch ECM exit dialog
309 Intent ecmDialogIntent =
310 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
311 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
312 mContext.startActivity(ecmDialogIntent);
313 } else {
314 changeAirplaneModeSystemSetting(on);
315 }
316 }
317
318 @Override
319 protected void changeStateFromPress(boolean buttonOn) {
320 if (!mHasTelephony) return;
321
322 // In ECM mode airplane state cannot be changed
323 if (!(Boolean.parseBoolean(
324 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
325 mState = buttonOn ? State.TurningOn : State.TurningOff;
326 mAirplaneState = mState;
327 }
328 }
329
330 public boolean showDuringKeyguard() {
331 return true;
332 }
333
334 public boolean showBeforeProvisioning() {
335 return false;
336 }
337 };
338 onAirplaneModeChanged();
339
340 mItems = new ArrayList<Action>();
341 String[] defaultActions = mContext.getResources().getStringArray(
342 R.array.config_globalActionsList);
343
344 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000345 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800346 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400347 for (int i = 0; i < defaultActions.length; i++) {
348 String actionKey = defaultActions[i];
349 if (addedKeys.contains(actionKey)) {
350 // If we already have added this, don't add it again.
351 continue;
352 }
353 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
354 mItems.add(new PowerAction());
355 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
356 mItems.add(mAirplaneModeOn);
357 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
358 if (Settings.Global.getInt(mContext.getContentResolver(),
359 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
360 mItems.add(new BugReportAction());
361 }
362 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
363 if (mShowSilentToggle) {
364 mItems.add(mSilentModeAction);
365 }
366 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
367 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
368 addUsersToMenu(mItems);
369 }
370 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
371 mItems.add(getSettingsAction());
372 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700373 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
374 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800375 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700376 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800377 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700378 }
Jason Monk361915c2017-03-21 20:33:59 -0400379 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
380 mItems.add(getVoiceAssistAction());
381 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
382 mItems.add(getAssistAction());
383 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
384 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500385 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
386 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000387 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000388 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000389 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
390 mItems.add(new LogoutAction());
391 mHasLogoutButton = true;
392 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800393 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400394 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800395 mItems.add(new EmergencyDialerAction());
396 }
Jason Monk361915c2017-03-21 20:33:59 -0400397 } else {
398 Log.e(TAG, "Invalid global action key " + actionKey);
399 }
400 // Add here so we don't add more than one.
401 addedKeys.add(actionKey);
402 }
403
404 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400405 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400406 }
407
408 mAdapter = new MyAdapter();
409
Steve Elliott9b87a442019-03-05 10:24:16 -0500410 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400411 mPanelPlugin != null
412 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400413 new GlobalActionsPanelPlugin.Callbacks() {
414 @Override
415 public void dismissGlobalActionsMenu() {
416 if (mDialog != null) {
417 mDialog.dismiss();
418 }
419 }
420
421 @Override
422 public void startPendingIntentDismissingKeyguard(
423 PendingIntent intent) {
424 mActivityStarter
425 .startPendingIntentDismissingKeyguard(intent);
426 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400427 },
428 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500429 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400430
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400431 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400432 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700433 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400434
435 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800436 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400437
438 return dialog;
439 }
440
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800441 private boolean shouldDisplayLockdown() {
442 int userId = getCurrentUser().id;
443 // Lockdown is meaningless without a place to go.
444 if (!mKeyguardManager.isDeviceSecure(userId)) {
445 return false;
446 }
447
448 // Only show the lockdown button if the device isn't locked down (for whatever reason).
449 int state = mLockPatternUtils.getStrongAuthForUser(userId);
450 return (state == STRONG_AUTH_NOT_REQUIRED
451 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
452 }
453
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700454 @Override
455 public void onUiModeChanged() {
456 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700457 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400458 mDialog.refreshDialog();
459 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700460 }
461
462 public void destroy() {
463 Dependency.get(ConfigurationController.class).removeCallback(this);
464 }
465
Jason Monk361915c2017-03-21 20:33:59 -0400466 private final class PowerAction extends SinglePressAction implements LongPressAction {
467 private PowerAction() {
468 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400469 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400470 }
471
472 @Override
473 public boolean onLongPress() {
474 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
475 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
476 mWindowManagerFuncs.reboot(true);
477 return true;
478 }
479 return false;
480 }
481
482 @Override
483 public boolean showDuringKeyguard() {
484 return true;
485 }
486
487 @Override
488 public boolean showBeforeProvisioning() {
489 return true;
490 }
491
492 @Override
493 public void onPress() {
494 // shutdown by making sure radio and power are handled accordingly.
495 mWindowManagerFuncs.shutdown();
496 }
497 }
498
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400499 private abstract class EmergencyAction extends SinglePressAction {
500 EmergencyAction(int iconResId, int messageResId) {
501 super(iconResId, messageResId);
502 }
503
504 @Override
505 public boolean shouldBeSeparated() {
506 return shouldUseSeparatedView();
507 }
508
509 @Override
510 public View create(
511 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
512 View v = super.create(context, convertView, parent, inflater);
513 int textColor;
514 if (shouldBeSeparated()) {
515 textColor = v.getResources().getColor(
516 com.android.systemui.R.color.global_actions_alert_text);
517 } else {
518 textColor = v.getResources().getColor(
519 com.android.systemui.R.color.global_actions_text);
520 }
521 TextView messageView = v.findViewById(R.id.message);
522 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400523 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400524 ImageView icon = (ImageView) v.findViewById(R.id.icon);
525 icon.getDrawable().setTint(textColor);
526 return v;
527 }
528
529 @Override
530 public boolean showDuringKeyguard() {
531 return true;
532 }
533
534 @Override
535 public boolean showBeforeProvisioning() {
536 return true;
537 }
538 }
539
540 private class EmergencyAffordanceAction extends EmergencyAction {
541 EmergencyAffordanceAction() {
542 super(R.drawable.emergency_icon,
543 R.string.global_action_emergency);
544 }
545
546 @Override
547 public void onPress() {
548 mEmergencyAffordanceManager.performEmergencyCall();
549 }
550 }
551
552 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800553 private EmergencyDialerAction() {
554 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800555 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800556 }
557
558 @Override
559 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800560 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800561 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800562 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
563 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
564 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800565 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
566 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800567 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
568 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800569 }
570
Jason Monk361915c2017-03-21 20:33:59 -0400571 private final class RestartAction extends SinglePressAction implements LongPressAction {
572 private RestartAction() {
573 super(R.drawable.ic_restart, R.string.global_action_restart);
574 }
575
576 @Override
577 public boolean onLongPress() {
578 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
579 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
580 mWindowManagerFuncs.reboot(true);
581 return true;
582 }
583 return false;
584 }
585
586 @Override
587 public boolean showDuringKeyguard() {
588 return true;
589 }
590
591 @Override
592 public boolean showBeforeProvisioning() {
593 return true;
594 }
595
596 @Override
597 public void onPress() {
598 mWindowManagerFuncs.reboot(false);
599 }
600 }
601
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400602 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500603 public ScreenshotAction() {
604 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
605 }
606
607 @Override
608 public void onPress() {
609 // Add a little delay before executing, to give the
610 // dialog a chance to go away before it takes a
611 // screenshot.
612 // TODO: instead, omit global action dialog layer
613 mHandler.postDelayed(new Runnable() {
614 @Override
615 public void run() {
616 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
617 MetricsLogger.action(mContext,
618 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
619 }
620 }, 500);
621 }
622
623 @Override
624 public boolean showDuringKeyguard() {
625 return true;
626 }
627
628 @Override
629 public boolean showBeforeProvisioning() {
630 return false;
631 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400632
633 @Override
634 public boolean onLongPress() {
635 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
636 mScreenRecordHelper.launchRecordPrompt();
637 } else {
638 onPress();
639 }
640 return true;
641 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500642 }
643
Jason Monk361915c2017-03-21 20:33:59 -0400644 private class BugReportAction extends SinglePressAction implements LongPressAction {
645
646 public BugReportAction() {
647 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
648 }
649
650 @Override
651 public void onPress() {
652 // don't actually trigger the bugreport if we are running stability
653 // tests via monkey
654 if (ActivityManager.isUserAMonkey()) {
655 return;
656 }
657 // Add a little delay before executing, to give the
658 // dialog a chance to go away before it takes a
659 // screenshot.
660 mHandler.postDelayed(new Runnable() {
661 @Override
662 public void run() {
663 try {
664 // Take an "interactive" bugreport.
665 MetricsLogger.action(mContext,
666 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
667 ActivityManager.getService().requestBugReport(
668 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
669 } catch (RemoteException e) {
670 }
671 }
672 }, 500);
673 }
674
675 @Override
676 public boolean onLongPress() {
677 // don't actually trigger the bugreport if we are running stability
678 // tests via monkey
679 if (ActivityManager.isUserAMonkey()) {
680 return false;
681 }
682 try {
683 // Take a "full" bugreport.
684 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
685 ActivityManager.getService().requestBugReport(
686 ActivityManager.BUGREPORT_OPTION_FULL);
687 } catch (RemoteException e) {
688 }
689 return false;
690 }
691
692 public boolean showDuringKeyguard() {
693 return true;
694 }
695
696 @Override
697 public boolean showBeforeProvisioning() {
698 return false;
699 }
Jason Monk361915c2017-03-21 20:33:59 -0400700 }
701
Alex Chau04458852017-11-27 18:21:23 +0000702 private final class LogoutAction extends SinglePressAction {
703 private LogoutAction() {
704 super(R.drawable.ic_logout, R.string.global_action_logout);
705 }
706
707 @Override
708 public boolean showDuringKeyguard() {
709 return true;
710 }
711
712 @Override
713 public boolean showBeforeProvisioning() {
714 return false;
715 }
716
717 @Override
718 public void onPress() {
719 // Add a little delay before executing, to give the dialog a chance to go away before
720 // switching user
721 mHandler.postDelayed(() -> {
722 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000723 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000724 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000725 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000726 } catch (RemoteException re) {
727 Log.e(TAG, "Couldn't logout user " + re);
728 }
729 }, 500);
730 }
731 }
732
Jason Monk361915c2017-03-21 20:33:59 -0400733 private Action getSettingsAction() {
734 return new SinglePressAction(R.drawable.ic_settings,
735 R.string.global_action_settings) {
736
737 @Override
738 public void onPress() {
739 Intent intent = new Intent(Settings.ACTION_SETTINGS);
740 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
741 mContext.startActivity(intent);
742 }
743
744 @Override
745 public boolean showDuringKeyguard() {
746 return true;
747 }
748
749 @Override
750 public boolean showBeforeProvisioning() {
751 return true;
752 }
753 };
754 }
755
Jason Monk361915c2017-03-21 20:33:59 -0400756 private Action getAssistAction() {
757 return new SinglePressAction(R.drawable.ic_action_assist_focused,
758 R.string.global_action_assist) {
759 @Override
760 public void onPress() {
761 Intent intent = new Intent(Intent.ACTION_ASSIST);
762 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
763 mContext.startActivity(intent);
764 }
765
766 @Override
767 public boolean showDuringKeyguard() {
768 return true;
769 }
770
771 @Override
772 public boolean showBeforeProvisioning() {
773 return true;
774 }
775 };
776 }
777
778 private Action getVoiceAssistAction() {
779 return new SinglePressAction(R.drawable.ic_voice_search,
780 R.string.global_action_voice_assist) {
781 @Override
782 public void onPress() {
783 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
784 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
785 mContext.startActivity(intent);
786 }
787
788 @Override
789 public boolean showDuringKeyguard() {
790 return true;
791 }
792
793 @Override
794 public boolean showBeforeProvisioning() {
795 return true;
796 }
797 };
798 }
799
800 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400801 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400802 R.string.global_action_lockdown) {
803
804 @Override
805 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700806 new LockPatternUtils(mContext)
807 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
808 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400809 try {
810 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100811 // Lock profiles (if any) on the background thread.
812 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
813 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400814 } catch (RemoteException e) {
815 Log.e(TAG, "Error while trying to lock device.", e);
816 }
817 }
818
819 @Override
820 public boolean showDuringKeyguard() {
821 return true;
822 }
823
824 @Override
825 public boolean showBeforeProvisioning() {
826 return false;
827 }
828 };
829 }
830
Pavel Grafov059021b2018-05-02 13:44:46 +0100831 private void lockProfiles() {
832 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
833 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
834 final int currentUserId = getCurrentUser().id;
835 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
836 for (final int id : profileIds) {
837 if (id != currentUserId) {
838 tm.setDeviceLockedForUser(id, true);
839 }
840 }
841 }
842
Jason Monk361915c2017-03-21 20:33:59 -0400843 private UserInfo getCurrentUser() {
844 try {
845 return ActivityManager.getService().getCurrentUser();
846 } catch (RemoteException re) {
847 return null;
848 }
849 }
850
851 private boolean isCurrentUserOwner() {
852 UserInfo currentUser = getCurrentUser();
853 return currentUser == null || currentUser.isPrimary();
854 }
855
856 private void addUsersToMenu(ArrayList<Action> items) {
857 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
858 if (um.isUserSwitcherEnabled()) {
859 List<UserInfo> users = um.getUsers();
860 UserInfo currentUser = getCurrentUser();
861 for (final UserInfo user : users) {
862 if (user.supportsSwitchToByUser()) {
863 boolean isCurrentUser = currentUser == null
864 ? user.id == 0 : (currentUser.id == user.id);
865 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
866 : null;
867 SinglePressAction switchToUser = new SinglePressAction(
868 R.drawable.ic_menu_cc, icon,
869 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400870 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400871 public void onPress() {
872 try {
873 ActivityManager.getService().switchUser(user.id);
874 } catch (RemoteException re) {
875 Log.e(TAG, "Couldn't switch user " + re);
876 }
877 }
878
879 public boolean showDuringKeyguard() {
880 return true;
881 }
882
883 public boolean showBeforeProvisioning() {
884 return false;
885 }
886 };
887 items.add(switchToUser);
888 }
889 }
890 }
891 }
892
893 private void prepareDialog() {
894 refreshSilentMode();
895 mAirplaneModeOn.updateState(mAirplaneState);
896 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400897 if (mShowSilentToggle) {
898 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
899 mContext.registerReceiver(mRingerModeReceiver, filter);
900 }
901 }
902
903 private void refreshSilentMode() {
904 if (!mHasVibrator) {
905 final boolean silentModeOn =
906 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400907 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400908 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
909 }
910 }
911
912 /** {@inheritDoc} */
913 public void onDismiss(DialogInterface dialog) {
914 mWindowManagerFuncs.onGlobalActionsHidden();
915 if (mShowSilentToggle) {
916 try {
917 mContext.unregisterReceiver(mRingerModeReceiver);
918 } catch (IllegalArgumentException ie) {
919 // ignore this
920 Log.w(TAG, ie);
921 }
922 }
923 }
924
925 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800926 public void onShow(DialogInterface dialog) {
927 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
928 }
929
Jason Monk361915c2017-03-21 20:33:59 -0400930 /**
931 * The adapter used for the list within the global actions dialog, taking
932 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400933 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
934 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400935 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
936 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500937 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400938 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400939 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400940 for (int i = 0; i < mItems.size(); i++) {
941 final Action action = mItems.get(i);
942
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400943 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
944 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400945 }
Jason Monk361915c2017-03-21 20:33:59 -0400946 }
947 return count;
948 }
949
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400950 private boolean shouldBeShown(Action action) {
951 if (mKeyguardShowing && !action.showDuringKeyguard()) {
952 return false;
953 }
954 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
955 return false;
956 }
957 return true;
958 }
959
960 @Override
961 public int countSeparatedItems() {
962 return countItems(true);
963 }
964
965 @Override
966 public int countListItems() {
967 return countItems(false);
968 }
969
970 @Override
971 public int getCount() {
972 return countSeparatedItems() + countListItems();
973 }
974
Jason Monk361915c2017-03-21 20:33:59 -0400975 @Override
976 public boolean isEnabled(int position) {
977 return getItem(position).isEnabled();
978 }
979
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500980 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400981 public boolean areAllItemsEnabled() {
982 return false;
983 }
984
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500985 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400986 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400987 int filteredPos = 0;
988 for (int i = 0; i < mItems.size(); i++) {
989 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400990 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400991 continue;
992 }
993 if (filteredPos == position) {
994 return action;
995 }
996 filteredPos++;
997 }
998
999 throw new IllegalArgumentException("position " + position
1000 + " out of range of showable actions"
1001 + ", filtered count=" + getCount()
1002 + ", keyguardshowing=" + mKeyguardShowing
1003 + ", provisioned=" + mDeviceProvisioned);
1004 }
1005
1006
1007 public long getItemId(int position) {
1008 return position;
1009 }
1010
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001011 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001012 public View getView(int position, View convertView, ViewGroup parent) {
1013 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001014 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001015 view.setOnClickListener(v -> onClickItem(position));
1016 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001017 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001018 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001019
1020 @Override
1021 public boolean onLongClickItem(int position) {
1022 final Action action = mAdapter.getItem(position);
1023 if (action instanceof LongPressAction) {
1024 mDialog.dismiss();
1025 return ((LongPressAction) action).onLongPress();
1026 }
1027 return false;
1028 }
1029
1030 @Override
1031 public void onClickItem(int position) {
1032 Action item = mAdapter.getItem(position);
1033 if (!(item instanceof SilentModeTriStateAction)) {
1034 mDialog.dismiss();
1035 }
1036 item.onPress();
1037 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001038
1039 @Override
1040 public boolean shouldBeSeparated(int position) {
1041 return getItem(position).shouldBeSeparated();
1042 }
Jason Monk361915c2017-03-21 20:33:59 -04001043 }
1044
1045 // note: the scheme below made more sense when we were planning on having
1046 // 8 different things in the global actions dialog. seems overkill with
1047 // only 3 items now, but may as well keep this flexible approach so it will
1048 // be easy should someone decide at the last minute to include something
1049 // else, such as 'enable wifi', or 'enable bluetooth'
1050
1051 /**
1052 * What each item in the global actions dialog must be able to support.
1053 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001054 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001055 /**
1056 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001057 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001058 */
1059 CharSequence getLabelForAccessibility(Context context);
1060
1061 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1062
1063 void onPress();
1064
1065 /**
1066 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001067 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001068 */
1069 boolean showDuringKeyguard();
1070
1071 /**
1072 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001073 * device is provisioned.onlongpress
1074 *
Jason Monk361915c2017-03-21 20:33:59 -04001075 */
1076 boolean showBeforeProvisioning();
1077
1078 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001079
1080 default boolean shouldBeSeparated() {
1081 return false;
1082 }
Jason Monk361915c2017-03-21 20:33:59 -04001083 }
1084
1085 /**
1086 * An action that also supports long press.
1087 */
1088 private interface LongPressAction extends Action {
1089 boolean onLongPress();
1090 }
1091
1092 /**
1093 * A single press action maintains no state, just responds to a press
1094 * and takes an action.
1095 */
1096 private static abstract class SinglePressAction implements Action {
1097 private final int mIconResId;
1098 private final Drawable mIcon;
1099 private final int mMessageResId;
1100 private final CharSequence mMessage;
1101
1102 protected SinglePressAction(int iconResId, int messageResId) {
1103 mIconResId = iconResId;
1104 mMessageResId = messageResId;
1105 mMessage = null;
1106 mIcon = null;
1107 }
1108
1109 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1110 mIconResId = iconResId;
1111 mMessageResId = 0;
1112 mMessage = message;
1113 mIcon = icon;
1114 }
1115
1116 public boolean isEnabled() {
1117 return true;
1118 }
1119
1120 public String getStatus() {
1121 return null;
1122 }
1123
1124 abstract public void onPress();
1125
1126 public CharSequence getLabelForAccessibility(Context context) {
1127 if (mMessage != null) {
1128 return mMessage;
1129 } else {
1130 return context.getString(mMessageResId);
1131 }
1132 }
1133
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001134 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001135 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001136 }
1137
Jason Monk361915c2017-03-21 20:33:59 -04001138 public View create(
1139 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001140 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001141 false);
Jason Monk361915c2017-03-21 20:33:59 -04001142
1143 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1144 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001145 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001146
1147 TextView statusView = (TextView) v.findViewById(R.id.status);
1148 final String status = getStatus();
1149 if (!TextUtils.isEmpty(status)) {
1150 statusView.setText(status);
1151 } else {
1152 statusView.setVisibility(View.GONE);
1153 }
1154 if (mIcon != null) {
1155 icon.setImageDrawable(mIcon);
1156 icon.setScaleType(ScaleType.CENTER_CROP);
1157 } else if (mIconResId != 0) {
1158 icon.setImageDrawable(context.getDrawable(mIconResId));
1159 }
1160 if (mMessage != null) {
1161 messageView.setText(mMessage);
1162 } else {
1163 messageView.setText(mMessageResId);
1164 }
1165
1166 return v;
1167 }
1168 }
1169
1170 /**
1171 * A toggle action knows whether it is on or off, and displays an icon
1172 * and status message accordingly.
1173 */
1174 private static abstract class ToggleAction implements Action {
1175
1176 enum State {
1177 Off(false),
1178 TurningOn(true),
1179 TurningOff(true),
1180 On(false);
1181
1182 private final boolean inTransition;
1183
1184 State(boolean intermediate) {
1185 inTransition = intermediate;
1186 }
1187
1188 public boolean inTransition() {
1189 return inTransition;
1190 }
1191 }
1192
1193 protected State mState = State.Off;
1194
1195 // prefs
1196 protected int mEnabledIconResId;
1197 protected int mDisabledIconResid;
1198 protected int mMessageResId;
1199 protected int mEnabledStatusMessageResId;
1200 protected int mDisabledStatusMessageResId;
1201
1202 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001203 * @param enabledIconResId The icon for when this action is on.
1204 * @param disabledIconResid The icon for when this action is off.
1205 * @param message The general information message, e.g 'Silent Mode'
1206 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001207 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1208 */
1209 public ToggleAction(int enabledIconResId,
1210 int disabledIconResid,
1211 int message,
1212 int enabledStatusMessageResId,
1213 int disabledStatusMessageResId) {
1214 mEnabledIconResId = enabledIconResId;
1215 mDisabledIconResid = disabledIconResid;
1216 mMessageResId = message;
1217 mEnabledStatusMessageResId = enabledStatusMessageResId;
1218 mDisabledStatusMessageResId = disabledStatusMessageResId;
1219 }
1220
1221 /**
1222 * Override to make changes to resource IDs just before creating the
1223 * View.
1224 */
1225 void willCreate() {
1226
1227 }
1228
1229 @Override
1230 public CharSequence getLabelForAccessibility(Context context) {
1231 return context.getString(mMessageResId);
1232 }
1233
1234 public View create(Context context, View convertView, ViewGroup parent,
1235 LayoutInflater inflater) {
1236 willCreate();
1237
1238 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001239 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001240
1241 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1242 TextView messageView = (TextView) v.findViewById(R.id.message);
1243 TextView statusView = (TextView) v.findViewById(R.id.status);
1244 final boolean enabled = isEnabled();
1245
1246 if (messageView != null) {
1247 messageView.setText(mMessageResId);
1248 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001249 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001250 }
1251
1252 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1253 if (icon != null) {
1254 icon.setImageDrawable(context.getDrawable(
1255 (on ? mEnabledIconResId : mDisabledIconResid)));
1256 icon.setEnabled(enabled);
1257 }
1258
1259 if (statusView != null) {
1260 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1261 statusView.setVisibility(View.VISIBLE);
1262 statusView.setEnabled(enabled);
1263 }
1264 v.setEnabled(enabled);
1265
1266 return v;
1267 }
1268
1269 public final void onPress() {
1270 if (mState.inTransition()) {
1271 Log.w(TAG, "shouldn't be able to toggle when in transition");
1272 return;
1273 }
1274
1275 final boolean nowOn = !(mState == State.On);
1276 onToggle(nowOn);
1277 changeStateFromPress(nowOn);
1278 }
1279
1280 public boolean isEnabled() {
1281 return !mState.inTransition();
1282 }
1283
1284 /**
1285 * Implementations may override this if their state can be in on of the intermediate
1286 * states until some notification is received (e.g airplane mode is 'turning off' until
1287 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001288 *
Jason Monk361915c2017-03-21 20:33:59 -04001289 * @param buttonOn Whether the button was turned on or off
1290 */
1291 protected void changeStateFromPress(boolean buttonOn) {
1292 mState = buttonOn ? State.On : State.Off;
1293 }
1294
1295 abstract void onToggle(boolean on);
1296
1297 public void updateState(State state) {
1298 mState = state;
1299 }
1300 }
1301
1302 private class SilentModeToggleAction extends ToggleAction {
1303 public SilentModeToggleAction() {
1304 super(R.drawable.ic_audio_vol_mute,
1305 R.drawable.ic_audio_vol,
1306 R.string.global_action_toggle_silent_mode,
1307 R.string.global_action_silent_mode_on_status,
1308 R.string.global_action_silent_mode_off_status);
1309 }
1310
1311 void onToggle(boolean on) {
1312 if (on) {
1313 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1314 } else {
1315 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1316 }
1317 }
1318
1319 public boolean showDuringKeyguard() {
1320 return true;
1321 }
1322
1323 public boolean showBeforeProvisioning() {
1324 return false;
1325 }
1326 }
1327
1328 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1329
Jason Monk16fbd9d2017-04-27 14:28:49 -04001330 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001331
1332 private final AudioManager mAudioManager;
1333 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001334
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001335 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001336 mAudioManager = audioManager;
1337 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001338 }
1339
1340 private int ringerModeToIndex(int ringerMode) {
1341 // They just happen to coincide
1342 return ringerMode;
1343 }
1344
1345 private int indexToRingerMode(int index) {
1346 // They just happen to coincide
1347 return index;
1348 }
1349
1350 @Override
1351 public CharSequence getLabelForAccessibility(Context context) {
1352 return null;
1353 }
1354
1355 public View create(Context context, View convertView, ViewGroup parent,
1356 LayoutInflater inflater) {
1357 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1358
1359 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1360 for (int i = 0; i < 3; i++) {
1361 View itemView = v.findViewById(ITEM_IDS[i]);
1362 itemView.setSelected(selectedIndex == i);
1363 // Set up click handler
1364 itemView.setTag(i);
1365 itemView.setOnClickListener(this);
1366 }
1367 return v;
1368 }
1369
1370 public void onPress() {
1371 }
1372
1373 public boolean showDuringKeyguard() {
1374 return true;
1375 }
1376
1377 public boolean showBeforeProvisioning() {
1378 return false;
1379 }
1380
1381 public boolean isEnabled() {
1382 return true;
1383 }
1384
1385 void willCreate() {
1386 }
1387
1388 public void onClick(View v) {
1389 if (!(v.getTag() instanceof Integer)) return;
1390
1391 int index = (Integer) v.getTag();
1392 mAudioManager.setRingerMode(indexToRingerMode(index));
1393 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1394 }
1395 }
1396
1397 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1398 public void onReceive(Context context, Intent intent) {
1399 String action = intent.getAction();
1400 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1401 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1402 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1403 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001404 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001405 }
1406 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1407 // Airplane mode can be changed after ECM exits if airplane toggle button
1408 // is pressed during ECM mode
1409 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1410 mIsWaitingForEcmExit) {
1411 mIsWaitingForEcmExit = false;
1412 changeAirplaneModeSystemSetting(true);
1413 }
1414 }
1415 }
1416 };
1417
1418 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1419 @Override
1420 public void onServiceStateChanged(ServiceState serviceState) {
1421 if (!mHasTelephony) return;
1422 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1423 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1424 mAirplaneModeOn.updateState(mAirplaneState);
1425 mAdapter.notifyDataSetChanged();
1426 }
1427 };
1428
1429 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1430 @Override
1431 public void onReceive(Context context, Intent intent) {
1432 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1433 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1434 }
1435 }
1436 };
1437
1438 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1439 @Override
1440 public void onChange(boolean selfChange) {
1441 onAirplaneModeChanged();
1442 }
1443 };
1444
1445 private static final int MESSAGE_DISMISS = 0;
1446 private static final int MESSAGE_REFRESH = 1;
1447 private static final int MESSAGE_SHOW = 2;
1448 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1449
1450 private Handler mHandler = new Handler() {
1451 public void handleMessage(Message msg) {
1452 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001453 case MESSAGE_DISMISS:
1454 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001455 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1456 mDialog.dismissImmediately();
1457 } else {
1458 mDialog.dismiss();
1459 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001460 mDialog = null;
1461 }
1462 break;
1463 case MESSAGE_REFRESH:
1464 refreshSilentMode();
1465 mAdapter.notifyDataSetChanged();
1466 break;
1467 case MESSAGE_SHOW:
1468 handleShow();
1469 break;
Jason Monk361915c2017-03-21 20:33:59 -04001470 }
1471 }
1472 };
1473
1474 private void onAirplaneModeChanged() {
1475 // Let the service state callbacks handle the state.
1476 if (mHasTelephony) return;
1477
1478 boolean airplaneModeOn = Settings.Global.getInt(
1479 mContext.getContentResolver(),
1480 Settings.Global.AIRPLANE_MODE_ON,
1481 0) == 1;
1482 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1483 mAirplaneModeOn.updateState(mAirplaneState);
1484 }
1485
1486 /**
1487 * Change the airplane mode system setting
1488 */
1489 private void changeAirplaneModeSystemSetting(boolean on) {
1490 Settings.Global.putInt(
1491 mContext.getContentResolver(),
1492 Settings.Global.AIRPLANE_MODE_ON,
1493 on ? 1 : 0);
1494 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1495 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1496 intent.putExtra("state", on);
1497 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1498 if (!mHasTelephony) {
1499 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1500 }
1501 }
1502
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001503 private static final class ActionsDialog extends Dialog implements DialogInterface,
1504 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001505
Jason Monk16fbd9d2017-04-27 14:28:49 -04001506 private final Context mContext;
1507 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001508 private final IStatusBarService mStatusBarService;
1509 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001510 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001511 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001512 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001513 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001514 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001515 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001516 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001517 private ResetOrientationData mResetOrientationData;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001518
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001519 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001520 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001521 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001522 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001523 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001524 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001525 mStatusBarService = Dependency.get(IStatusBarService.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001526
1527 // Window initialization
1528 Window window = getWindow();
1529 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001530 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1531 window.getDecorView();
1532 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1533 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1534 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1535 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1536 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001537 window.addFlags(
1538 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001539 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001540 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001541 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1542 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1543 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001544 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001545 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001546
1547 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001548 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001549 }
1550
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001551 private boolean shouldUsePanel() {
Aran Inkf4744c62019-05-10 15:27:36 -04001552 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001553 }
1554
1555 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001556 int rotation = RotationUtils.getRotation(mContext);
1557 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1558 if (rotation != RotationUtils.ROTATION_NONE) {
1559 if (rotationLocked) {
1560 if (mResetOrientationData == null) {
1561 mResetOrientationData = new ResetOrientationData();
1562 mResetOrientationData.locked = true;
1563 mResetOrientationData.rotation = rotation;
1564 }
1565
1566 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001567 // This call is posted so that the rotation does not change until post-layout,
1568 // otherwise onConfigurationChanged() may not get invoked.
1569 mGlobalActionsLayout.post(() ->
1570 RotationPolicy.setRotationLockAtAngle(
1571 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001572 }
1573 } else {
1574 if (!rotationLocked) {
1575 if (mResetOrientationData == null) {
1576 mResetOrientationData = new ResetOrientationData();
1577 mResetOrientationData.locked = false;
1578 }
1579
1580 // Lock to portrait, so the user doesn't accidentally hide 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, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001586 }
1587
Steve Elliott53f12ae2019-05-13 17:14:15 -04001588 // Disable rotation suggestions, if enabled
1589 setRotationSuggestionsEnabled(false);
1590
Steve Elliott48f75db2019-05-03 15:03:38 -04001591 FrameLayout panelContainer = new FrameLayout(mContext);
1592 FrameLayout.LayoutParams panelParams =
1593 new FrameLayout.LayoutParams(
1594 FrameLayout.LayoutParams.MATCH_PARENT,
1595 FrameLayout.LayoutParams.WRAP_CONTENT);
1596 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
1597 addContentView(
1598 panelContainer,
1599 new ViewGroup.LayoutParams(
1600 ViewGroup.LayoutParams.MATCH_PARENT,
1601 ViewGroup.LayoutParams.MATCH_PARENT));
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 Elliotte9e7e1f2019-03-18 15:54:59 -04001609 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001610 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001611 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001612 @Override
1613 public boolean dispatchPopulateAccessibilityEvent(
1614 View host, AccessibilityEvent event) {
1615 // Populate the title here, just as Activity does
1616 event.getText().add(mContext.getString(R.string.global_actions));
1617 return true;
1618 }
1619 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001620 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001621 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001622
Steve Elliott86ef6282019-05-08 14:45:04 -04001623 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001624 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001625 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001626 if (mBackgroundDrawable == null) {
1627 mBackgroundDrawable = new ScrimDrawable();
1628 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1629 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001630 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001631 }
1632
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001633 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001634 int rotation = RotationUtils.getRotation(context);
1635 boolean useGridLayout = isForceGridEnabled(context)
1636 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1637 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001638 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001639 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001640 } else {
1641 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001642 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001643 } else {
1644 if (useGridLayout) {
1645 return com.android.systemui.R.layout.global_actions_grid;
1646 } else {
1647 return com.android.systemui.R.layout.global_actions_column;
1648 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001649 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001650 }
1651
Jason Monk361915c2017-03-21 20:33:59 -04001652 @Override
1653 protected void onStart() {
1654 super.setCanceledOnTouchOutside(true);
1655 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001656 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001657
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001658 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001659 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001660 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001661 updateColors(colors, false /* animate */);
1662 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001663 }
1664
1665 /**
1666 * Updates background and system bars according to current GradientColors.
1667 * @param colors Colors and hints to use.
1668 * @param animate Interpolates gradient if true, just sets otherwise.
1669 */
1670 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001671 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001672 return;
1673 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001674 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001675 View decorView = getWindow().getDecorView();
1676 if (colors.supportsDarkText()) {
1677 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001678 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001679 } else {
1680 decorView.setSystemUiVisibility(0);
1681 }
Jason Monk361915c2017-03-21 20:33:59 -04001682 }
1683
1684 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001685 protected void onStop() {
1686 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001687 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001688 }
1689
1690 @Override
1691 public void show() {
1692 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001693 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001694 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001695 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1696 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001697 mGlobalActionsLayout.setAlpha(0);
1698 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001699 .alpha(1)
1700 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001701 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001702 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001703 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001704 .setUpdateListener(animation -> {
1705 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001706 * mScrimAlpha * 255);
1707 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001708 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001709 .start();
1710 }
1711
1712 @Override
1713 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001714 if (!mShowing) {
1715 return;
1716 }
1717 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001718 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001719 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001720 mGlobalActionsLayout.setAlpha(1);
1721 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001722 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001723 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1724 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001725 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001726 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001727 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001728 .setUpdateListener(animation -> {
1729 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001730 * mScrimAlpha * 255);
1731 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001732 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001733 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001734 dismissPanel();
1735 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001736 }
1737
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001738 void dismissImmediately() {
1739 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001740 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001741 dismissPanel();
1742 resetOrientation();
1743 }
1744
1745 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001746 if (mPanelController != null) {
1747 mPanelController.onDismissed();
1748 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001749 }
1750
Steve Elliott53f12ae2019-05-13 17:14:15 -04001751 private void setRotationSuggestionsEnabled(boolean enabled) {
1752 try {
1753 final int userId = Binder.getCallingUserHandle().getIdentifier();
1754 final int what = enabled
1755 ? StatusBarManager.DISABLE2_NONE
1756 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1757 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1758 } catch (RemoteException ex) {
1759 throw ex.rethrowFromSystemServer();
1760 }
1761 }
1762
Steve Elliott48f75db2019-05-03 15:03:38 -04001763 private void resetOrientation() {
1764 if (mResetOrientationData != null) {
1765 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1766 mResetOrientationData.rotation);
1767 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001768 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001769 }
1770
Jason Monk361915c2017-03-21 20:33:59 -04001771 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001772 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001773 if (mKeyguardShowing) {
1774 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001775 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1776 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001777 }
1778 } else {
1779 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001780 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1781 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001782 }
1783 }
1784 }
1785
1786 public void setKeyguardShowing(boolean keyguardShowing) {
1787 mKeyguardShowing = keyguardShowing;
1788 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001789
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001790 public void refreshDialog() {
1791 initializeLayout();
1792 mGlobalActionsLayout.updateList();
1793 }
1794
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001795 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001796 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001797 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001798 }
1799 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001800
1801 private static class ResetOrientationData {
1802 public boolean locked;
1803 public int rotation;
1804 }
Jason Monk361915c2017-03-21 20:33:59 -04001805 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001806
1807 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001808 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1809 */
1810 private static boolean isPanelDebugModeEnabled(Context context) {
1811 return Settings.Secure.getInt(context.getContentResolver(),
1812 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1813 }
1814
1815 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001816 * Determines whether or not the Global Actions menu should be forced to
1817 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001818 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001819 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001820 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001821 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001822
1823 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001824 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1825 */
1826 private static boolean shouldUseSeparatedView() {
1827 return true;
1828 }
Jason Monk361915c2017-03-21 20:33:59 -04001829}