blob: a098fee6a77791b93bd75eac99dbb5c5dd1bf445 [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;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070028import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000029import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010030import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040031import android.content.BroadcastReceiver;
32import android.content.Context;
33import android.content.DialogInterface;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.UserInfo;
37import android.database.ContentObserver;
38import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.net.ConnectivityManager;
Jason Monk361915c2017-03-21 20:33:59 -040041import android.os.Handler;
42import android.os.Message;
43import android.os.RemoteException;
44import android.os.ServiceManager;
45import android.os.SystemProperties;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.os.Vibrator;
49import android.provider.Settings;
50import android.service.dreams.DreamService;
51import android.service.dreams.IDreamManager;
52import android.telephony.PhoneStateListener;
53import android.telephony.ServiceState;
54import android.telephony.TelephonyManager;
55import android.text.TextUtils;
56import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080057import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040058import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070059import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040060import android.view.LayoutInflater;
61import android.view.View;
62import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070063import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.view.WindowManager;
65import android.view.WindowManagerGlobal;
66import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050067import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040068import android.widget.ImageView;
69import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.widget.TextView;
71
Charles He9851a8d2017-10-10 17:31:30 +010072import com.android.internal.R;
73import com.android.internal.colorextraction.ColorExtractor;
74import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070075import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010076import com.android.internal.logging.MetricsLogger;
77import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
78import com.android.internal.telephony.TelephonyIntents;
79import com.android.internal.telephony.TelephonyProperties;
80import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040081import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050082import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040083import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.widget.LockPatternUtils;
85import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050087import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050088import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040090import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050092import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050093import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliott92475042019-05-06 18:56:40 -040094import com.android.systemui.statusbar.phone.UnlockMethodCache;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070095import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080096import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050097import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050098import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070099
Jason Monk361915c2017-03-21 20:33:59 -0400100import java.util.ArrayList;
101import java.util.List;
102
103/**
104 * Helper to show the global actions dialog. Each item is an {@link Action} that
105 * may show depending on whether the keyguard is showing, and whether the device
106 * is provisioned.
107 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500108public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
109 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400110
111 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
112 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700113 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400114
115 private static final String TAG = "GlobalActionsDialog";
116
117 private static final boolean SHOW_SILENT_TOGGLE = true;
118
119 /* Valid settings for global actions keys.
120 * see config.xml config_globalActionList */
121 private static final String GLOBAL_ACTION_KEY_POWER = "power";
122 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
123 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
124 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
125 private static final String GLOBAL_ACTION_KEY_USERS = "users";
126 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
127 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
128 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
129 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
130 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000131 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800132 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500133 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400134
135 private final Context mContext;
136 private final GlobalActionsManager mWindowManagerFuncs;
137 private final AudioManager mAudioManager;
138 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000139 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800140 private final LockPatternUtils mLockPatternUtils;
141 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400142
143 private ArrayList<Action> mItems;
144 private ActionsDialog mDialog;
145
146 private Action mSilentModeAction;
147 private ToggleAction mAirplaneModeOn;
148
149 private MyAdapter mAdapter;
150
151 private boolean mKeyguardShowing = false;
152 private boolean mDeviceProvisioned = false;
153 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
154 private boolean mIsWaitingForEcmExit = false;
155 private boolean mHasTelephony;
156 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000157 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800158 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400159 private final boolean mShowSilentToggle;
160 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500161 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400162 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400163 private final ActivityStarter mActivityStarter;
164 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500165
Jason Monk361915c2017-03-21 20:33:59 -0400166 /**
167 * @param context everything needs a context :(
168 */
169 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700170 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400171 mWindowManagerFuncs = windowManagerFuncs;
172 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
173 mDreamManager = IDreamManager.Stub.asInterface(
174 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000175 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
176 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800177 mLockPatternUtils = new LockPatternUtils(mContext);
178 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400179
180 // receive broadcasts
181 IntentFilter filter = new IntentFilter();
182 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
183 filter.addAction(Intent.ACTION_SCREEN_OFF);
184 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
185 context.registerReceiver(mBroadcastReceiver, filter);
186
187 ConnectivityManager cm = (ConnectivityManager)
188 context.getSystemService(Context.CONNECTIVITY_SERVICE);
189 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
190
191 // get notified of phone state changes
192 TelephonyManager telephonyManager =
193 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
194 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
195 mContext.getContentResolver().registerContentObserver(
196 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
197 mAirplaneModeObserver);
198 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
199 mHasVibrator = vibrator != null && vibrator.hasVibrator();
200
201 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
202 R.bool.config_useFixedVolume);
203
204 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500205 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400206 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700207
208 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500209
Steve Elliott4c868852019-03-14 16:25:41 -0400210 mActivityStarter = Dependency.get(ActivityStarter.class);
Steve Elliott92475042019-05-06 18:56:40 -0400211 UnlockMethodCache unlockMethodCache = UnlockMethodCache.getInstance(context);
212 unlockMethodCache.addListener(
213 () -> {
214 if (mDialog != null && mDialog.mPanelController != null) {
215 boolean locked = !unlockMethodCache.canSkipBouncer();
216 mDialog.mPanelController.onDeviceLockStateChanged(locked);
217 }
218 });
Jason Monk361915c2017-03-21 20:33:59 -0400219 }
220
221 /**
222 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400223 *
Jason Monk361915c2017-03-21 20:33:59 -0400224 * @param keyguardShowing True if keyguard is showing
225 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400226 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
227 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400228 mKeyguardShowing = keyguardShowing;
229 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400230 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400231 if (mDialog != null) {
232 mDialog.dismiss();
233 mDialog = null;
234 // Show delayed, so that the dismiss of the previous dialog completes
235 mHandler.sendEmptyMessage(MESSAGE_SHOW);
236 } else {
237 handleShow();
238 }
239 }
240
Charles He9851a8d2017-10-10 17:31:30 +0100241 /**
242 * Dismiss the global actions dialog, if it's currently shown
243 */
244 public void dismissDialog() {
245 mHandler.removeMessages(MESSAGE_DISMISS);
246 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
247 }
248
Jason Monk361915c2017-03-21 20:33:59 -0400249 private void awakenIfNecessary() {
250 if (mDreamManager != null) {
251 try {
252 if (mDreamManager.isDreaming()) {
253 mDreamManager.awaken();
254 }
255 } catch (RemoteException e) {
256 // we tried
257 }
258 }
259 }
260
261 private void handleShow() {
262 awakenIfNecessary();
263 mDialog = createDialog();
264 prepareDialog();
265
266 // If we only have 1 item and it's a simple press action, just do this action.
267 if (mAdapter.getCount() == 1
268 && mAdapter.getItem(0) instanceof SinglePressAction
269 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
270 ((SinglePressAction) mAdapter.getItem(0)).onPress();
271 } else {
272 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
273 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100274 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400275 mDialog.getWindow().setAttributes(attrs);
276 mDialog.show();
277 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400278 }
279 }
280
281 /**
282 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400283 *
Jason Monk361915c2017-03-21 20:33:59 -0400284 * @return A new dialog.
285 */
286 private ActionsDialog createDialog() {
287 // Simple toggle style if there's no vibrator, otherwise use a tri-state
288 if (!mHasVibrator) {
289 mSilentModeAction = new SilentModeToggleAction();
290 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700291 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400292 }
293 mAirplaneModeOn = new ToggleAction(
294 R.drawable.ic_lock_airplane_mode,
295 R.drawable.ic_lock_airplane_mode_off,
296 R.string.global_actions_toggle_airplane_mode,
297 R.string.global_actions_airplane_mode_on_status,
298 R.string.global_actions_airplane_mode_off_status) {
299
300 void onToggle(boolean on) {
301 if (mHasTelephony && Boolean.parseBoolean(
302 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
303 mIsWaitingForEcmExit = true;
304 // Launch ECM exit dialog
305 Intent ecmDialogIntent =
306 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
307 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
308 mContext.startActivity(ecmDialogIntent);
309 } else {
310 changeAirplaneModeSystemSetting(on);
311 }
312 }
313
314 @Override
315 protected void changeStateFromPress(boolean buttonOn) {
316 if (!mHasTelephony) return;
317
318 // In ECM mode airplane state cannot be changed
319 if (!(Boolean.parseBoolean(
320 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
321 mState = buttonOn ? State.TurningOn : State.TurningOff;
322 mAirplaneState = mState;
323 }
324 }
325
326 public boolean showDuringKeyguard() {
327 return true;
328 }
329
330 public boolean showBeforeProvisioning() {
331 return false;
332 }
333 };
334 onAirplaneModeChanged();
335
336 mItems = new ArrayList<Action>();
337 String[] defaultActions = mContext.getResources().getStringArray(
338 R.array.config_globalActionsList);
339
340 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000341 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800342 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400343 for (int i = 0; i < defaultActions.length; i++) {
344 String actionKey = defaultActions[i];
345 if (addedKeys.contains(actionKey)) {
346 // If we already have added this, don't add it again.
347 continue;
348 }
349 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
350 mItems.add(new PowerAction());
351 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
352 mItems.add(mAirplaneModeOn);
353 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
354 if (Settings.Global.getInt(mContext.getContentResolver(),
355 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
356 mItems.add(new BugReportAction());
357 }
358 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
359 if (mShowSilentToggle) {
360 mItems.add(mSilentModeAction);
361 }
362 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
363 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
364 addUsersToMenu(mItems);
365 }
366 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
367 mItems.add(getSettingsAction());
368 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700369 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
370 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800371 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700372 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800373 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700374 }
Jason Monk361915c2017-03-21 20:33:59 -0400375 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
376 mItems.add(getVoiceAssistAction());
377 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
378 mItems.add(getAssistAction());
379 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
380 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500381 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
382 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000383 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000384 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000385 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
386 mItems.add(new LogoutAction());
387 mHasLogoutButton = true;
388 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800389 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400390 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800391 mItems.add(new EmergencyDialerAction());
392 }
Jason Monk361915c2017-03-21 20:33:59 -0400393 } else {
394 Log.e(TAG, "Invalid global action key " + actionKey);
395 }
396 // Add here so we don't add more than one.
397 addedKeys.add(actionKey);
398 }
399
400 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400401 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400402 }
403
404 mAdapter = new MyAdapter();
405
Steve Elliott9b87a442019-03-05 10:24:16 -0500406 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400407 mPanelPlugin != null
408 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400409 new GlobalActionsPanelPlugin.Callbacks() {
410 @Override
411 public void dismissGlobalActionsMenu() {
412 if (mDialog != null) {
413 mDialog.dismiss();
414 }
415 }
416
417 @Override
418 public void startPendingIntentDismissingKeyguard(
419 PendingIntent intent) {
420 mActivityStarter
421 .startPendingIntentDismissingKeyguard(intent);
422 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400423 },
424 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500425 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400426
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400427 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400428 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700429 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400430
431 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800432 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400433
434 return dialog;
435 }
436
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800437 private boolean shouldDisplayLockdown() {
438 int userId = getCurrentUser().id;
439 // Lockdown is meaningless without a place to go.
440 if (!mKeyguardManager.isDeviceSecure(userId)) {
441 return false;
442 }
443
444 // Only show the lockdown button if the device isn't locked down (for whatever reason).
445 int state = mLockPatternUtils.getStrongAuthForUser(userId);
446 return (state == STRONG_AUTH_NOT_REQUIRED
447 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
448 }
449
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700450 @Override
451 public void onUiModeChanged() {
452 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700453 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400454 mDialog.refreshDialog();
455 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700456 }
457
458 public void destroy() {
459 Dependency.get(ConfigurationController.class).removeCallback(this);
460 }
461
Jason Monk361915c2017-03-21 20:33:59 -0400462 private final class PowerAction extends SinglePressAction implements LongPressAction {
463 private PowerAction() {
464 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400465 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400466 }
467
468 @Override
469 public boolean onLongPress() {
470 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
471 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
472 mWindowManagerFuncs.reboot(true);
473 return true;
474 }
475 return false;
476 }
477
478 @Override
479 public boolean showDuringKeyguard() {
480 return true;
481 }
482
483 @Override
484 public boolean showBeforeProvisioning() {
485 return true;
486 }
487
488 @Override
489 public void onPress() {
490 // shutdown by making sure radio and power are handled accordingly.
491 mWindowManagerFuncs.shutdown();
492 }
493 }
494
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400495 private abstract class EmergencyAction extends SinglePressAction {
496 EmergencyAction(int iconResId, int messageResId) {
497 super(iconResId, messageResId);
498 }
499
500 @Override
501 public boolean shouldBeSeparated() {
502 return shouldUseSeparatedView();
503 }
504
505 @Override
506 public View create(
507 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
508 View v = super.create(context, convertView, parent, inflater);
509 int textColor;
510 if (shouldBeSeparated()) {
511 textColor = v.getResources().getColor(
512 com.android.systemui.R.color.global_actions_alert_text);
513 } else {
514 textColor = v.getResources().getColor(
515 com.android.systemui.R.color.global_actions_text);
516 }
517 TextView messageView = v.findViewById(R.id.message);
518 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400519 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400520 ImageView icon = (ImageView) v.findViewById(R.id.icon);
521 icon.getDrawable().setTint(textColor);
522 return v;
523 }
524
525 @Override
526 public boolean showDuringKeyguard() {
527 return true;
528 }
529
530 @Override
531 public boolean showBeforeProvisioning() {
532 return true;
533 }
534 }
535
536 private class EmergencyAffordanceAction extends EmergencyAction {
537 EmergencyAffordanceAction() {
538 super(R.drawable.emergency_icon,
539 R.string.global_action_emergency);
540 }
541
542 @Override
543 public void onPress() {
544 mEmergencyAffordanceManager.performEmergencyCall();
545 }
546 }
547
548 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800549 private EmergencyDialerAction() {
550 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800551 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800552 }
553
554 @Override
555 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800556 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800557 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800558 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
559 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
560 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800561 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
562 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800563 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
564 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800565 }
566
Jason Monk361915c2017-03-21 20:33:59 -0400567 private final class RestartAction extends SinglePressAction implements LongPressAction {
568 private RestartAction() {
569 super(R.drawable.ic_restart, R.string.global_action_restart);
570 }
571
572 @Override
573 public boolean onLongPress() {
574 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
575 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
576 mWindowManagerFuncs.reboot(true);
577 return true;
578 }
579 return false;
580 }
581
582 @Override
583 public boolean showDuringKeyguard() {
584 return true;
585 }
586
587 @Override
588 public boolean showBeforeProvisioning() {
589 return true;
590 }
591
592 @Override
593 public void onPress() {
594 mWindowManagerFuncs.reboot(false);
595 }
596 }
597
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400598 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500599 public ScreenshotAction() {
600 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
601 }
602
603 @Override
604 public void onPress() {
605 // Add a little delay before executing, to give the
606 // dialog a chance to go away before it takes a
607 // screenshot.
608 // TODO: instead, omit global action dialog layer
609 mHandler.postDelayed(new Runnable() {
610 @Override
611 public void run() {
612 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
613 MetricsLogger.action(mContext,
614 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
615 }
616 }, 500);
617 }
618
619 @Override
620 public boolean showDuringKeyguard() {
621 return true;
622 }
623
624 @Override
625 public boolean showBeforeProvisioning() {
626 return false;
627 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400628
629 @Override
630 public boolean onLongPress() {
631 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
632 mScreenRecordHelper.launchRecordPrompt();
633 } else {
634 onPress();
635 }
636 return true;
637 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500638 }
639
Jason Monk361915c2017-03-21 20:33:59 -0400640 private class BugReportAction extends SinglePressAction implements LongPressAction {
641
642 public BugReportAction() {
643 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
644 }
645
646 @Override
647 public void onPress() {
648 // don't actually trigger the bugreport if we are running stability
649 // tests via monkey
650 if (ActivityManager.isUserAMonkey()) {
651 return;
652 }
653 // Add a little delay before executing, to give the
654 // dialog a chance to go away before it takes a
655 // screenshot.
656 mHandler.postDelayed(new Runnable() {
657 @Override
658 public void run() {
659 try {
660 // Take an "interactive" bugreport.
661 MetricsLogger.action(mContext,
662 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
663 ActivityManager.getService().requestBugReport(
664 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
665 } catch (RemoteException e) {
666 }
667 }
668 }, 500);
669 }
670
671 @Override
672 public boolean onLongPress() {
673 // don't actually trigger the bugreport if we are running stability
674 // tests via monkey
675 if (ActivityManager.isUserAMonkey()) {
676 return false;
677 }
678 try {
679 // Take a "full" bugreport.
680 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
681 ActivityManager.getService().requestBugReport(
682 ActivityManager.BUGREPORT_OPTION_FULL);
683 } catch (RemoteException e) {
684 }
685 return false;
686 }
687
688 public boolean showDuringKeyguard() {
689 return true;
690 }
691
692 @Override
693 public boolean showBeforeProvisioning() {
694 return false;
695 }
Jason Monk361915c2017-03-21 20:33:59 -0400696 }
697
Alex Chau04458852017-11-27 18:21:23 +0000698 private final class LogoutAction extends SinglePressAction {
699 private LogoutAction() {
700 super(R.drawable.ic_logout, R.string.global_action_logout);
701 }
702
703 @Override
704 public boolean showDuringKeyguard() {
705 return true;
706 }
707
708 @Override
709 public boolean showBeforeProvisioning() {
710 return false;
711 }
712
713 @Override
714 public void onPress() {
715 // Add a little delay before executing, to give the dialog a chance to go away before
716 // switching user
717 mHandler.postDelayed(() -> {
718 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000719 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000720 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000721 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000722 } catch (RemoteException re) {
723 Log.e(TAG, "Couldn't logout user " + re);
724 }
725 }, 500);
726 }
727 }
728
Jason Monk361915c2017-03-21 20:33:59 -0400729 private Action getSettingsAction() {
730 return new SinglePressAction(R.drawable.ic_settings,
731 R.string.global_action_settings) {
732
733 @Override
734 public void onPress() {
735 Intent intent = new Intent(Settings.ACTION_SETTINGS);
736 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
737 mContext.startActivity(intent);
738 }
739
740 @Override
741 public boolean showDuringKeyguard() {
742 return true;
743 }
744
745 @Override
746 public boolean showBeforeProvisioning() {
747 return true;
748 }
749 };
750 }
751
Jason Monk361915c2017-03-21 20:33:59 -0400752 private Action getAssistAction() {
753 return new SinglePressAction(R.drawable.ic_action_assist_focused,
754 R.string.global_action_assist) {
755 @Override
756 public void onPress() {
757 Intent intent = new Intent(Intent.ACTION_ASSIST);
758 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
759 mContext.startActivity(intent);
760 }
761
762 @Override
763 public boolean showDuringKeyguard() {
764 return true;
765 }
766
767 @Override
768 public boolean showBeforeProvisioning() {
769 return true;
770 }
771 };
772 }
773
774 private Action getVoiceAssistAction() {
775 return new SinglePressAction(R.drawable.ic_voice_search,
776 R.string.global_action_voice_assist) {
777 @Override
778 public void onPress() {
779 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
780 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
781 mContext.startActivity(intent);
782 }
783
784 @Override
785 public boolean showDuringKeyguard() {
786 return true;
787 }
788
789 @Override
790 public boolean showBeforeProvisioning() {
791 return true;
792 }
793 };
794 }
795
796 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400797 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400798 R.string.global_action_lockdown) {
799
800 @Override
801 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700802 new LockPatternUtils(mContext)
803 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
804 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400805 try {
806 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100807 // Lock profiles (if any) on the background thread.
808 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
809 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400810 } catch (RemoteException e) {
811 Log.e(TAG, "Error while trying to lock device.", e);
812 }
813 }
814
815 @Override
816 public boolean showDuringKeyguard() {
817 return true;
818 }
819
820 @Override
821 public boolean showBeforeProvisioning() {
822 return false;
823 }
824 };
825 }
826
Pavel Grafov059021b2018-05-02 13:44:46 +0100827 private void lockProfiles() {
828 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
829 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
830 final int currentUserId = getCurrentUser().id;
831 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
832 for (final int id : profileIds) {
833 if (id != currentUserId) {
834 tm.setDeviceLockedForUser(id, true);
835 }
836 }
837 }
838
Jason Monk361915c2017-03-21 20:33:59 -0400839 private UserInfo getCurrentUser() {
840 try {
841 return ActivityManager.getService().getCurrentUser();
842 } catch (RemoteException re) {
843 return null;
844 }
845 }
846
847 private boolean isCurrentUserOwner() {
848 UserInfo currentUser = getCurrentUser();
849 return currentUser == null || currentUser.isPrimary();
850 }
851
852 private void addUsersToMenu(ArrayList<Action> items) {
853 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
854 if (um.isUserSwitcherEnabled()) {
855 List<UserInfo> users = um.getUsers();
856 UserInfo currentUser = getCurrentUser();
857 for (final UserInfo user : users) {
858 if (user.supportsSwitchToByUser()) {
859 boolean isCurrentUser = currentUser == null
860 ? user.id == 0 : (currentUser.id == user.id);
861 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
862 : null;
863 SinglePressAction switchToUser = new SinglePressAction(
864 R.drawable.ic_menu_cc, icon,
865 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400866 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400867 public void onPress() {
868 try {
869 ActivityManager.getService().switchUser(user.id);
870 } catch (RemoteException re) {
871 Log.e(TAG, "Couldn't switch user " + re);
872 }
873 }
874
875 public boolean showDuringKeyguard() {
876 return true;
877 }
878
879 public boolean showBeforeProvisioning() {
880 return false;
881 }
882 };
883 items.add(switchToUser);
884 }
885 }
886 }
887 }
888
889 private void prepareDialog() {
890 refreshSilentMode();
891 mAirplaneModeOn.updateState(mAirplaneState);
892 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400893 if (mShowSilentToggle) {
894 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
895 mContext.registerReceiver(mRingerModeReceiver, filter);
896 }
897 }
898
899 private void refreshSilentMode() {
900 if (!mHasVibrator) {
901 final boolean silentModeOn =
902 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400903 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400904 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
905 }
906 }
907
908 /** {@inheritDoc} */
909 public void onDismiss(DialogInterface dialog) {
910 mWindowManagerFuncs.onGlobalActionsHidden();
911 if (mShowSilentToggle) {
912 try {
913 mContext.unregisterReceiver(mRingerModeReceiver);
914 } catch (IllegalArgumentException ie) {
915 // ignore this
916 Log.w(TAG, ie);
917 }
918 }
919 }
920
921 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800922 public void onShow(DialogInterface dialog) {
923 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
924 }
925
Jason Monk361915c2017-03-21 20:33:59 -0400926 /**
927 * The adapter used for the list within the global actions dialog, taking
928 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400929 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
930 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400931 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
932 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500933 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400934 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400935 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400936 for (int i = 0; i < mItems.size(); i++) {
937 final Action action = mItems.get(i);
938
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400939 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
940 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400941 }
Jason Monk361915c2017-03-21 20:33:59 -0400942 }
943 return count;
944 }
945
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400946 private boolean shouldBeShown(Action action) {
947 if (mKeyguardShowing && !action.showDuringKeyguard()) {
948 return false;
949 }
950 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
951 return false;
952 }
953 return true;
954 }
955
956 @Override
957 public int countSeparatedItems() {
958 return countItems(true);
959 }
960
961 @Override
962 public int countListItems() {
963 return countItems(false);
964 }
965
966 @Override
967 public int getCount() {
968 return countSeparatedItems() + countListItems();
969 }
970
Jason Monk361915c2017-03-21 20:33:59 -0400971 @Override
972 public boolean isEnabled(int position) {
973 return getItem(position).isEnabled();
974 }
975
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500976 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400977 public boolean areAllItemsEnabled() {
978 return false;
979 }
980
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500981 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400982 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400983 int filteredPos = 0;
984 for (int i = 0; i < mItems.size(); i++) {
985 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400986 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400987 continue;
988 }
989 if (filteredPos == position) {
990 return action;
991 }
992 filteredPos++;
993 }
994
995 throw new IllegalArgumentException("position " + position
996 + " out of range of showable actions"
997 + ", filtered count=" + getCount()
998 + ", keyguardshowing=" + mKeyguardShowing
999 + ", provisioned=" + mDeviceProvisioned);
1000 }
1001
1002
1003 public long getItemId(int position) {
1004 return position;
1005 }
1006
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001007 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001008 public View getView(int position, View convertView, ViewGroup parent) {
1009 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001010 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001011 view.setOnClickListener(v -> onClickItem(position));
1012 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001013 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001014 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001015
1016 @Override
1017 public boolean onLongClickItem(int position) {
1018 final Action action = mAdapter.getItem(position);
1019 if (action instanceof LongPressAction) {
1020 mDialog.dismiss();
1021 return ((LongPressAction) action).onLongPress();
1022 }
1023 return false;
1024 }
1025
1026 @Override
1027 public void onClickItem(int position) {
1028 Action item = mAdapter.getItem(position);
1029 if (!(item instanceof SilentModeTriStateAction)) {
1030 mDialog.dismiss();
1031 }
1032 item.onPress();
1033 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001034
1035 @Override
1036 public boolean shouldBeSeparated(int position) {
1037 return getItem(position).shouldBeSeparated();
1038 }
Jason Monk361915c2017-03-21 20:33:59 -04001039 }
1040
1041 // note: the scheme below made more sense when we were planning on having
1042 // 8 different things in the global actions dialog. seems overkill with
1043 // only 3 items now, but may as well keep this flexible approach so it will
1044 // be easy should someone decide at the last minute to include something
1045 // else, such as 'enable wifi', or 'enable bluetooth'
1046
1047 /**
1048 * What each item in the global actions dialog must be able to support.
1049 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001050 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001051 /**
1052 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001053 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001054 */
1055 CharSequence getLabelForAccessibility(Context context);
1056
1057 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1058
1059 void onPress();
1060
1061 /**
1062 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001063 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001064 */
1065 boolean showDuringKeyguard();
1066
1067 /**
1068 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001069 * device is provisioned.onlongpress
1070 *
Jason Monk361915c2017-03-21 20:33:59 -04001071 */
1072 boolean showBeforeProvisioning();
1073
1074 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001075
1076 default boolean shouldBeSeparated() {
1077 return false;
1078 }
Jason Monk361915c2017-03-21 20:33:59 -04001079 }
1080
1081 /**
1082 * An action that also supports long press.
1083 */
1084 private interface LongPressAction extends Action {
1085 boolean onLongPress();
1086 }
1087
1088 /**
1089 * A single press action maintains no state, just responds to a press
1090 * and takes an action.
1091 */
1092 private static abstract class SinglePressAction implements Action {
1093 private final int mIconResId;
1094 private final Drawable mIcon;
1095 private final int mMessageResId;
1096 private final CharSequence mMessage;
1097
1098 protected SinglePressAction(int iconResId, int messageResId) {
1099 mIconResId = iconResId;
1100 mMessageResId = messageResId;
1101 mMessage = null;
1102 mIcon = null;
1103 }
1104
1105 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1106 mIconResId = iconResId;
1107 mMessageResId = 0;
1108 mMessage = message;
1109 mIcon = icon;
1110 }
1111
1112 public boolean isEnabled() {
1113 return true;
1114 }
1115
1116 public String getStatus() {
1117 return null;
1118 }
1119
1120 abstract public void onPress();
1121
1122 public CharSequence getLabelForAccessibility(Context context) {
1123 if (mMessage != null) {
1124 return mMessage;
1125 } else {
1126 return context.getString(mMessageResId);
1127 }
1128 }
1129
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001130 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001131 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001132 }
1133
Jason Monk361915c2017-03-21 20:33:59 -04001134 public View create(
1135 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001136 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001137 false);
Jason Monk361915c2017-03-21 20:33:59 -04001138
1139 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1140 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001141 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001142
1143 TextView statusView = (TextView) v.findViewById(R.id.status);
1144 final String status = getStatus();
1145 if (!TextUtils.isEmpty(status)) {
1146 statusView.setText(status);
1147 } else {
1148 statusView.setVisibility(View.GONE);
1149 }
1150 if (mIcon != null) {
1151 icon.setImageDrawable(mIcon);
1152 icon.setScaleType(ScaleType.CENTER_CROP);
1153 } else if (mIconResId != 0) {
1154 icon.setImageDrawable(context.getDrawable(mIconResId));
1155 }
1156 if (mMessage != null) {
1157 messageView.setText(mMessage);
1158 } else {
1159 messageView.setText(mMessageResId);
1160 }
1161
1162 return v;
1163 }
1164 }
1165
1166 /**
1167 * A toggle action knows whether it is on or off, and displays an icon
1168 * and status message accordingly.
1169 */
1170 private static abstract class ToggleAction implements Action {
1171
1172 enum State {
1173 Off(false),
1174 TurningOn(true),
1175 TurningOff(true),
1176 On(false);
1177
1178 private final boolean inTransition;
1179
1180 State(boolean intermediate) {
1181 inTransition = intermediate;
1182 }
1183
1184 public boolean inTransition() {
1185 return inTransition;
1186 }
1187 }
1188
1189 protected State mState = State.Off;
1190
1191 // prefs
1192 protected int mEnabledIconResId;
1193 protected int mDisabledIconResid;
1194 protected int mMessageResId;
1195 protected int mEnabledStatusMessageResId;
1196 protected int mDisabledStatusMessageResId;
1197
1198 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001199 * @param enabledIconResId The icon for when this action is on.
1200 * @param disabledIconResid The icon for when this action is off.
1201 * @param message The general information message, e.g 'Silent Mode'
1202 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001203 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1204 */
1205 public ToggleAction(int enabledIconResId,
1206 int disabledIconResid,
1207 int message,
1208 int enabledStatusMessageResId,
1209 int disabledStatusMessageResId) {
1210 mEnabledIconResId = enabledIconResId;
1211 mDisabledIconResid = disabledIconResid;
1212 mMessageResId = message;
1213 mEnabledStatusMessageResId = enabledStatusMessageResId;
1214 mDisabledStatusMessageResId = disabledStatusMessageResId;
1215 }
1216
1217 /**
1218 * Override to make changes to resource IDs just before creating the
1219 * View.
1220 */
1221 void willCreate() {
1222
1223 }
1224
1225 @Override
1226 public CharSequence getLabelForAccessibility(Context context) {
1227 return context.getString(mMessageResId);
1228 }
1229
1230 public View create(Context context, View convertView, ViewGroup parent,
1231 LayoutInflater inflater) {
1232 willCreate();
1233
1234 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001235 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001236
1237 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1238 TextView messageView = (TextView) v.findViewById(R.id.message);
1239 TextView statusView = (TextView) v.findViewById(R.id.status);
1240 final boolean enabled = isEnabled();
1241
1242 if (messageView != null) {
1243 messageView.setText(mMessageResId);
1244 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001245 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001246 }
1247
1248 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1249 if (icon != null) {
1250 icon.setImageDrawable(context.getDrawable(
1251 (on ? mEnabledIconResId : mDisabledIconResid)));
1252 icon.setEnabled(enabled);
1253 }
1254
1255 if (statusView != null) {
1256 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1257 statusView.setVisibility(View.VISIBLE);
1258 statusView.setEnabled(enabled);
1259 }
1260 v.setEnabled(enabled);
1261
1262 return v;
1263 }
1264
1265 public final void onPress() {
1266 if (mState.inTransition()) {
1267 Log.w(TAG, "shouldn't be able to toggle when in transition");
1268 return;
1269 }
1270
1271 final boolean nowOn = !(mState == State.On);
1272 onToggle(nowOn);
1273 changeStateFromPress(nowOn);
1274 }
1275
1276 public boolean isEnabled() {
1277 return !mState.inTransition();
1278 }
1279
1280 /**
1281 * Implementations may override this if their state can be in on of the intermediate
1282 * states until some notification is received (e.g airplane mode is 'turning off' until
1283 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001284 *
Jason Monk361915c2017-03-21 20:33:59 -04001285 * @param buttonOn Whether the button was turned on or off
1286 */
1287 protected void changeStateFromPress(boolean buttonOn) {
1288 mState = buttonOn ? State.On : State.Off;
1289 }
1290
1291 abstract void onToggle(boolean on);
1292
1293 public void updateState(State state) {
1294 mState = state;
1295 }
1296 }
1297
1298 private class SilentModeToggleAction extends ToggleAction {
1299 public SilentModeToggleAction() {
1300 super(R.drawable.ic_audio_vol_mute,
1301 R.drawable.ic_audio_vol,
1302 R.string.global_action_toggle_silent_mode,
1303 R.string.global_action_silent_mode_on_status,
1304 R.string.global_action_silent_mode_off_status);
1305 }
1306
1307 void onToggle(boolean on) {
1308 if (on) {
1309 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1310 } else {
1311 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1312 }
1313 }
1314
1315 public boolean showDuringKeyguard() {
1316 return true;
1317 }
1318
1319 public boolean showBeforeProvisioning() {
1320 return false;
1321 }
1322 }
1323
1324 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1325
Jason Monk16fbd9d2017-04-27 14:28:49 -04001326 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001327
1328 private final AudioManager mAudioManager;
1329 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001330
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001331 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001332 mAudioManager = audioManager;
1333 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001334 }
1335
1336 private int ringerModeToIndex(int ringerMode) {
1337 // They just happen to coincide
1338 return ringerMode;
1339 }
1340
1341 private int indexToRingerMode(int index) {
1342 // They just happen to coincide
1343 return index;
1344 }
1345
1346 @Override
1347 public CharSequence getLabelForAccessibility(Context context) {
1348 return null;
1349 }
1350
1351 public View create(Context context, View convertView, ViewGroup parent,
1352 LayoutInflater inflater) {
1353 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1354
1355 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1356 for (int i = 0; i < 3; i++) {
1357 View itemView = v.findViewById(ITEM_IDS[i]);
1358 itemView.setSelected(selectedIndex == i);
1359 // Set up click handler
1360 itemView.setTag(i);
1361 itemView.setOnClickListener(this);
1362 }
1363 return v;
1364 }
1365
1366 public void onPress() {
1367 }
1368
1369 public boolean showDuringKeyguard() {
1370 return true;
1371 }
1372
1373 public boolean showBeforeProvisioning() {
1374 return false;
1375 }
1376
1377 public boolean isEnabled() {
1378 return true;
1379 }
1380
1381 void willCreate() {
1382 }
1383
1384 public void onClick(View v) {
1385 if (!(v.getTag() instanceof Integer)) return;
1386
1387 int index = (Integer) v.getTag();
1388 mAudioManager.setRingerMode(indexToRingerMode(index));
1389 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1390 }
1391 }
1392
1393 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1394 public void onReceive(Context context, Intent intent) {
1395 String action = intent.getAction();
1396 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1397 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1398 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1399 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001400 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001401 }
1402 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1403 // Airplane mode can be changed after ECM exits if airplane toggle button
1404 // is pressed during ECM mode
1405 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1406 mIsWaitingForEcmExit) {
1407 mIsWaitingForEcmExit = false;
1408 changeAirplaneModeSystemSetting(true);
1409 }
1410 }
1411 }
1412 };
1413
1414 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1415 @Override
1416 public void onServiceStateChanged(ServiceState serviceState) {
1417 if (!mHasTelephony) return;
1418 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1419 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1420 mAirplaneModeOn.updateState(mAirplaneState);
1421 mAdapter.notifyDataSetChanged();
1422 }
1423 };
1424
1425 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1426 @Override
1427 public void onReceive(Context context, Intent intent) {
1428 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1429 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1430 }
1431 }
1432 };
1433
1434 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1435 @Override
1436 public void onChange(boolean selfChange) {
1437 onAirplaneModeChanged();
1438 }
1439 };
1440
1441 private static final int MESSAGE_DISMISS = 0;
1442 private static final int MESSAGE_REFRESH = 1;
1443 private static final int MESSAGE_SHOW = 2;
1444 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1445
1446 private Handler mHandler = new Handler() {
1447 public void handleMessage(Message msg) {
1448 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001449 case MESSAGE_DISMISS:
1450 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001451 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1452 mDialog.dismissImmediately();
1453 } else {
1454 mDialog.dismiss();
1455 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001456 mDialog = null;
1457 }
1458 break;
1459 case MESSAGE_REFRESH:
1460 refreshSilentMode();
1461 mAdapter.notifyDataSetChanged();
1462 break;
1463 case MESSAGE_SHOW:
1464 handleShow();
1465 break;
Jason Monk361915c2017-03-21 20:33:59 -04001466 }
1467 }
1468 };
1469
1470 private void onAirplaneModeChanged() {
1471 // Let the service state callbacks handle the state.
1472 if (mHasTelephony) return;
1473
1474 boolean airplaneModeOn = Settings.Global.getInt(
1475 mContext.getContentResolver(),
1476 Settings.Global.AIRPLANE_MODE_ON,
1477 0) == 1;
1478 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1479 mAirplaneModeOn.updateState(mAirplaneState);
1480 }
1481
1482 /**
1483 * Change the airplane mode system setting
1484 */
1485 private void changeAirplaneModeSystemSetting(boolean on) {
1486 Settings.Global.putInt(
1487 mContext.getContentResolver(),
1488 Settings.Global.AIRPLANE_MODE_ON,
1489 on ? 1 : 0);
1490 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1491 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1492 intent.putExtra("state", on);
1493 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1494 if (!mHasTelephony) {
1495 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1496 }
1497 }
1498
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001499 private static final class ActionsDialog extends Dialog implements DialogInterface,
1500 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001501
Jason Monk16fbd9d2017-04-27 14:28:49 -04001502 private final Context mContext;
1503 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001504 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001505 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001506 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001507 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001508 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001509 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001510 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001511 private ResetOrientationData mResetOrientationData;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001512
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001513 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001514 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001515 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001516 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001517 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001518 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001519
1520 // Window initialization
1521 Window window = getWindow();
1522 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001523 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1524 window.getDecorView();
1525 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1526 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1527 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1528 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1529 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001530 window.addFlags(
1531 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001532 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001533 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001534 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1535 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1536 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001537 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001538 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001539
1540 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001541 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001542 }
1543
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001544 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001545 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001546 }
1547
1548 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001549 int rotation = RotationUtils.getRotation(mContext);
1550 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1551 if (rotation != RotationUtils.ROTATION_NONE) {
1552 if (rotationLocked) {
1553 if (mResetOrientationData == null) {
1554 mResetOrientationData = new ResetOrientationData();
1555 mResetOrientationData.locked = true;
1556 mResetOrientationData.rotation = rotation;
1557 }
1558
1559 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
1560 RotationPolicy.setRotationLockAtAngle(
1561 mContext, false, RotationUtils.ROTATION_NONE);
1562 }
1563 } else {
1564 if (!rotationLocked) {
1565 if (mResetOrientationData == null) {
1566 mResetOrientationData = new ResetOrientationData();
1567 mResetOrientationData.locked = false;
1568 }
1569
1570 // Lock to portrait, so the user doesn't accidentally hide the panel.
1571 RotationPolicy.setRotationLockAtAngle(
1572 mContext, true, RotationUtils.ROTATION_NONE);
1573 }
1574
1575 FrameLayout panelContainer = new FrameLayout(mContext);
1576 FrameLayout.LayoutParams panelParams =
1577 new FrameLayout.LayoutParams(
1578 FrameLayout.LayoutParams.MATCH_PARENT,
1579 FrameLayout.LayoutParams.WRAP_CONTENT);
1580 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
1581 addContentView(
1582 panelContainer,
1583 new ViewGroup.LayoutParams(
1584 ViewGroup.LayoutParams.MATCH_PARENT,
1585 ViewGroup.LayoutParams.MATCH_PARENT));
Steve Elliott86ef6282019-05-08 14:45:04 -04001586 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1587 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001588 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001589 }
1590
1591 private void initializeLayout() {
1592 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001593 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001594 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001595 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001596 @Override
1597 public boolean dispatchPopulateAccessibilityEvent(
1598 View host, AccessibilityEvent event) {
1599 // Populate the title here, just as Activity does
1600 event.getText().add(mContext.getString(R.string.global_actions));
1601 return true;
1602 }
1603 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001604 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001605 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001606
Steve Elliott86ef6282019-05-08 14:45:04 -04001607 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001608 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001609 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001610 if (mBackgroundDrawable == null) {
1611 mBackgroundDrawable = new ScrimDrawable();
1612 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1613 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001614 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001615 }
1616
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001617 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001618 boolean useGridLayout = isForceGridEnabled(context) || shouldUsePanel();
1619 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1620 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001621 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001622 } else {
1623 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001624 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001625 } else {
1626 if (useGridLayout) {
1627 return com.android.systemui.R.layout.global_actions_grid;
1628 } else {
1629 return com.android.systemui.R.layout.global_actions_column;
1630 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001631 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001632 }
1633
Jason Monk361915c2017-03-21 20:33:59 -04001634 @Override
1635 protected void onStart() {
1636 super.setCanceledOnTouchOutside(true);
1637 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001638 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001639
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001640 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001641 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001642 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001643 updateColors(colors, false /* animate */);
1644 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001645 }
1646
1647 /**
1648 * Updates background and system bars according to current GradientColors.
1649 * @param colors Colors and hints to use.
1650 * @param animate Interpolates gradient if true, just sets otherwise.
1651 */
1652 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001653 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001654 return;
1655 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001656 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001657 View decorView = getWindow().getDecorView();
1658 if (colors.supportsDarkText()) {
1659 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001660 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001661 } else {
1662 decorView.setSystemUiVisibility(0);
1663 }
Jason Monk361915c2017-03-21 20:33:59 -04001664 }
1665
1666 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001667 protected void onStop() {
1668 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001669 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001670 }
1671
1672 @Override
1673 public void show() {
1674 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001675 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001676 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001677 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1678 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001679 mGlobalActionsLayout.setAlpha(0);
1680 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001681 .alpha(1)
1682 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001683 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001684 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001685 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001686 .setUpdateListener(animation -> {
1687 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001688 * mScrimAlpha * 255);
1689 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001690 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001691 .start();
1692 }
1693
1694 @Override
1695 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001696 if (!mShowing) {
1697 return;
1698 }
1699 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001700 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001701 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001702 mGlobalActionsLayout.setAlpha(1);
1703 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001704 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001705 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1706 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001707 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001708 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001709 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001710 .setUpdateListener(animation -> {
1711 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001712 * mScrimAlpha * 255);
1713 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001714 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001715 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001716 dismissPanel();
1717 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001718 }
1719
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001720 void dismissImmediately() {
1721 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001722 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001723 dismissPanel();
1724 resetOrientation();
1725 }
1726
1727 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001728 if (mPanelController != null) {
1729 mPanelController.onDismissed();
1730 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001731 }
1732
Steve Elliott48f75db2019-05-03 15:03:38 -04001733 private void resetOrientation() {
1734 if (mResetOrientationData != null) {
1735 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1736 mResetOrientationData.rotation);
1737 }
1738 }
1739
Jason Monk361915c2017-03-21 20:33:59 -04001740 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001741 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001742 if (mKeyguardShowing) {
1743 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001744 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1745 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001746 }
1747 } else {
1748 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001749 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1750 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001751 }
1752 }
1753 }
1754
1755 public void setKeyguardShowing(boolean keyguardShowing) {
1756 mKeyguardShowing = keyguardShowing;
1757 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001758
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001759 public void refreshDialog() {
1760 initializeLayout();
1761 mGlobalActionsLayout.updateList();
1762 }
1763
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001764 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001765 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001766 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001767 }
1768 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001769
1770 private static class ResetOrientationData {
1771 public boolean locked;
1772 public int rotation;
1773 }
Jason Monk361915c2017-03-21 20:33:59 -04001774 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001775
1776 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001777 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1778 */
1779 private static boolean isPanelDebugModeEnabled(Context context) {
1780 return Settings.Secure.getInt(context.getContentResolver(),
1781 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1782 }
1783
1784 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001785 * Determines whether or not the Global Actions menu should be forced to
1786 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001787 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001788 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001789 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001790 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001791
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001792
1793 /**
1794 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1795 */
1796 private static boolean shouldUseSeparatedView() {
1797 return true;
1798 }
Jason Monk361915c2017-03-21 20:33:59 -04001799}