blob: 48cd62f07040bf3e7dfa038ecaa0bb5e640615df [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;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070027import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000028import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010029import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040030import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
35import android.content.pm.UserInfo;
36import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070037import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040038import 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 Dupine2292a92017-07-06 14:35:30 -070075import com.android.internal.colorextraction.drawable.GradientDrawable;
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;
Charles He9851a8d2017-10-10 17:31:30 +010083import com.android.internal.widget.LockPatternUtils;
84import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050086import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050087import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.systemui.colorextraction.SysuiColorExtractor;
89import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050090import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070091import com.android.systemui.statusbar.policy.ConfigurationController;
Steve Elliott9b87a442019-03-05 10:24:16 -050092import com.android.systemui.statusbar.policy.ExtensionController;
93import com.android.systemui.statusbar.policy.ExtensionController.Extension;
Shaotang Li5c422632018-07-04 14:18:40 +080094import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050095import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050096import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070097
Jason Monk361915c2017-03-21 20:33:59 -040098import java.util.ArrayList;
99import java.util.List;
100
101/**
102 * Helper to show the global actions dialog. Each item is an {@link Action} that
103 * may show depending on whether the keyguard is showing, and whether the device
104 * is provisioned.
105 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500106public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
107 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400108
109 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
110 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700111 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400112
113 private static final String TAG = "GlobalActionsDialog";
114
115 private static final boolean SHOW_SILENT_TOGGLE = true;
116
117 /* Valid settings for global actions keys.
118 * see config.xml config_globalActionList */
119 private static final String GLOBAL_ACTION_KEY_POWER = "power";
120 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
121 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
122 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
123 private static final String GLOBAL_ACTION_KEY_USERS = "users";
124 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
125 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
126 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
127 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
128 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000129 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800130 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500131 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400132
133 private final Context mContext;
134 private final GlobalActionsManager mWindowManagerFuncs;
135 private final AudioManager mAudioManager;
136 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000137 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800138 private final LockPatternUtils mLockPatternUtils;
139 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400140
141 private ArrayList<Action> mItems;
142 private ActionsDialog mDialog;
143
144 private Action mSilentModeAction;
145 private ToggleAction mAirplaneModeOn;
146
147 private MyAdapter mAdapter;
148
149 private boolean mKeyguardShowing = false;
150 private boolean mDeviceProvisioned = false;
151 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
152 private boolean mIsWaitingForEcmExit = false;
153 private boolean mHasTelephony;
154 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000155 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800156 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400157 private final boolean mShowSilentToggle;
158 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500159 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400160 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400161
Steve Elliott9b87a442019-03-05 10:24:16 -0500162 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
163
Jason Monk361915c2017-03-21 20:33:59 -0400164 /**
165 * @param context everything needs a context :(
166 */
167 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700168 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400169 mWindowManagerFuncs = windowManagerFuncs;
170 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
171 mDreamManager = IDreamManager.Stub.asInterface(
172 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000173 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
174 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800175 mLockPatternUtils = new LockPatternUtils(mContext);
176 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400177
178 // receive broadcasts
179 IntentFilter filter = new IntentFilter();
180 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
181 filter.addAction(Intent.ACTION_SCREEN_OFF);
182 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
183 context.registerReceiver(mBroadcastReceiver, filter);
184
185 ConnectivityManager cm = (ConnectivityManager)
186 context.getSystemService(Context.CONNECTIVITY_SERVICE);
187 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
188
189 // get notified of phone state changes
190 TelephonyManager telephonyManager =
191 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
192 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
193 mContext.getContentResolver().registerContentObserver(
194 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
195 mAirplaneModeObserver);
196 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
197 mHasVibrator = vibrator != null && vibrator.hasVibrator();
198
199 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
200 R.bool.config_useFixedVolume);
201
202 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500203 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400204 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700205
206 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500207
208 mPanelExtension = Dependency.get(ExtensionController.class)
209 .newExtension(GlobalActionsPanelPlugin.class)
210 .withPlugin(GlobalActionsPanelPlugin.class)
211 .build();
Jason Monk361915c2017-03-21 20:33:59 -0400212 }
213
214 /**
215 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400216 *
Jason Monk361915c2017-03-21 20:33:59 -0400217 * @param keyguardShowing True if keyguard is showing
218 */
219 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
220 mKeyguardShowing = keyguardShowing;
221 mDeviceProvisioned = isDeviceProvisioned;
222 if (mDialog != null) {
223 mDialog.dismiss();
224 mDialog = null;
225 // Show delayed, so that the dismiss of the previous dialog completes
226 mHandler.sendEmptyMessage(MESSAGE_SHOW);
227 } else {
228 handleShow();
229 }
230 }
231
Charles He9851a8d2017-10-10 17:31:30 +0100232 /**
233 * Dismiss the global actions dialog, if it's currently shown
234 */
235 public void dismissDialog() {
236 mHandler.removeMessages(MESSAGE_DISMISS);
237 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
238 }
239
Jason Monk361915c2017-03-21 20:33:59 -0400240 private void awakenIfNecessary() {
241 if (mDreamManager != null) {
242 try {
243 if (mDreamManager.isDreaming()) {
244 mDreamManager.awaken();
245 }
246 } catch (RemoteException e) {
247 // we tried
248 }
249 }
250 }
251
252 private void handleShow() {
253 awakenIfNecessary();
254 mDialog = createDialog();
255 prepareDialog();
256
257 // If we only have 1 item and it's a simple press action, just do this action.
258 if (mAdapter.getCount() == 1
259 && mAdapter.getItem(0) instanceof SinglePressAction
260 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
261 ((SinglePressAction) mAdapter.getItem(0)).onPress();
262 } else {
263 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
264 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100265 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400266 mDialog.getWindow().setAttributes(attrs);
267 mDialog.show();
268 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400269 }
270 }
271
272 /**
273 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400274 *
Jason Monk361915c2017-03-21 20:33:59 -0400275 * @return A new dialog.
276 */
277 private ActionsDialog createDialog() {
278 // Simple toggle style if there's no vibrator, otherwise use a tri-state
279 if (!mHasVibrator) {
280 mSilentModeAction = new SilentModeToggleAction();
281 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700282 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400283 }
284 mAirplaneModeOn = new ToggleAction(
285 R.drawable.ic_lock_airplane_mode,
286 R.drawable.ic_lock_airplane_mode_off,
287 R.string.global_actions_toggle_airplane_mode,
288 R.string.global_actions_airplane_mode_on_status,
289 R.string.global_actions_airplane_mode_off_status) {
290
291 void onToggle(boolean on) {
292 if (mHasTelephony && Boolean.parseBoolean(
293 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
294 mIsWaitingForEcmExit = true;
295 // Launch ECM exit dialog
296 Intent ecmDialogIntent =
297 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
298 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
299 mContext.startActivity(ecmDialogIntent);
300 } else {
301 changeAirplaneModeSystemSetting(on);
302 }
303 }
304
305 @Override
306 protected void changeStateFromPress(boolean buttonOn) {
307 if (!mHasTelephony) return;
308
309 // In ECM mode airplane state cannot be changed
310 if (!(Boolean.parseBoolean(
311 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
312 mState = buttonOn ? State.TurningOn : State.TurningOff;
313 mAirplaneState = mState;
314 }
315 }
316
317 public boolean showDuringKeyguard() {
318 return true;
319 }
320
321 public boolean showBeforeProvisioning() {
322 return false;
323 }
324 };
325 onAirplaneModeChanged();
326
327 mItems = new ArrayList<Action>();
328 String[] defaultActions = mContext.getResources().getStringArray(
329 R.array.config_globalActionsList);
330
331 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000332 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800333 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400334 for (int i = 0; i < defaultActions.length; i++) {
335 String actionKey = defaultActions[i];
336 if (addedKeys.contains(actionKey)) {
337 // If we already have added this, don't add it again.
338 continue;
339 }
340 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
341 mItems.add(new PowerAction());
342 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
343 mItems.add(mAirplaneModeOn);
344 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
345 if (Settings.Global.getInt(mContext.getContentResolver(),
346 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
347 mItems.add(new BugReportAction());
348 }
349 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
350 if (mShowSilentToggle) {
351 mItems.add(mSilentModeAction);
352 }
353 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
354 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
355 addUsersToMenu(mItems);
356 }
357 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
358 mItems.add(getSettingsAction());
359 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700360 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
361 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800362 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700363 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800364 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700365 }
Jason Monk361915c2017-03-21 20:33:59 -0400366 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
367 mItems.add(getVoiceAssistAction());
368 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
369 mItems.add(getAssistAction());
370 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
371 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500372 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
373 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000374 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000375 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000376 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
377 mItems.add(new LogoutAction());
378 mHasLogoutButton = true;
379 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800380 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400381 if (shouldUseSeparatedView()
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800382 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
383 mItems.add(new EmergencyDialerAction());
384 }
Jason Monk361915c2017-03-21 20:33:59 -0400385 } else {
386 Log.e(TAG, "Invalid global action key " + actionKey);
387 }
388 // Add here so we don't add more than one.
389 addedKeys.add(actionKey);
390 }
391
392 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
393 mItems.add(getEmergencyAction());
394 }
395
396 mAdapter = new MyAdapter();
397
Steve Elliott9b87a442019-03-05 10:24:16 -0500398 GlobalActionsPanelPlugin.PanelViewController panelViewController =
399 mPanelExtension.get() != null
400 ? mPanelExtension.get().onPanelShown(() -> {
401 if (mDialog != null) {
402 mDialog.dismiss();
403 }
404 })
405 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400406 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400407 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700408 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400409
410 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800411 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400412
413 return dialog;
414 }
415
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800416 private boolean shouldDisplayLockdown() {
417 int userId = getCurrentUser().id;
418 // Lockdown is meaningless without a place to go.
419 if (!mKeyguardManager.isDeviceSecure(userId)) {
420 return false;
421 }
422
423 // Only show the lockdown button if the device isn't locked down (for whatever reason).
424 int state = mLockPatternUtils.getStrongAuthForUser(userId);
425 return (state == STRONG_AUTH_NOT_REQUIRED
426 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
427 }
428
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700429 @Override
430 public void onUiModeChanged() {
431 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
432 }
433
434 public void destroy() {
435 Dependency.get(ConfigurationController.class).removeCallback(this);
436 }
437
Jason Monk361915c2017-03-21 20:33:59 -0400438 private final class PowerAction extends SinglePressAction implements LongPressAction {
439 private PowerAction() {
440 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400441 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400442 }
443
444 @Override
445 public boolean onLongPress() {
446 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
447 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
448 mWindowManagerFuncs.reboot(true);
449 return true;
450 }
451 return false;
452 }
453
454 @Override
455 public boolean showDuringKeyguard() {
456 return true;
457 }
458
459 @Override
460 public boolean showBeforeProvisioning() {
461 return true;
462 }
463
464 @Override
465 public void onPress() {
466 // shutdown by making sure radio and power are handled accordingly.
467 mWindowManagerFuncs.shutdown();
468 }
469 }
470
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800471 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800472 private EmergencyDialerAction() {
473 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800474 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800475 }
476
477 @Override
478 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800479 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800480 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800481 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
482 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
483 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800484 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
485 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800486 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
487 }
488
489 @Override
490 public boolean showDuringKeyguard() {
491 return true;
492 }
493
494 @Override
495 public boolean showBeforeProvisioning() {
496 return true;
497 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500498
499 @Override
500 public boolean shouldBeSeparated() {
501 return true;
502 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800503 }
504
Jason Monk361915c2017-03-21 20:33:59 -0400505 private final class RestartAction extends SinglePressAction implements LongPressAction {
506 private RestartAction() {
507 super(R.drawable.ic_restart, R.string.global_action_restart);
508 }
509
510 @Override
511 public boolean onLongPress() {
512 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
513 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
514 mWindowManagerFuncs.reboot(true);
515 return true;
516 }
517 return false;
518 }
519
520 @Override
521 public boolean showDuringKeyguard() {
522 return true;
523 }
524
525 @Override
526 public boolean showBeforeProvisioning() {
527 return true;
528 }
529
530 @Override
531 public void onPress() {
532 mWindowManagerFuncs.reboot(false);
533 }
534 }
535
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400536 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500537 public ScreenshotAction() {
538 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
539 }
540
541 @Override
542 public void onPress() {
543 // Add a little delay before executing, to give the
544 // dialog a chance to go away before it takes a
545 // screenshot.
546 // TODO: instead, omit global action dialog layer
547 mHandler.postDelayed(new Runnable() {
548 @Override
549 public void run() {
550 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
551 MetricsLogger.action(mContext,
552 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
553 }
554 }, 500);
555 }
556
557 @Override
558 public boolean showDuringKeyguard() {
559 return true;
560 }
561
562 @Override
563 public boolean showBeforeProvisioning() {
564 return false;
565 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400566
567 @Override
568 public boolean onLongPress() {
569 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
570 mScreenRecordHelper.launchRecordPrompt();
571 } else {
572 onPress();
573 }
574 return true;
575 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500576 }
577
Jason Monk361915c2017-03-21 20:33:59 -0400578 private class BugReportAction extends SinglePressAction implements LongPressAction {
579
580 public BugReportAction() {
581 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
582 }
583
584 @Override
585 public void onPress() {
586 // don't actually trigger the bugreport if we are running stability
587 // tests via monkey
588 if (ActivityManager.isUserAMonkey()) {
589 return;
590 }
591 // Add a little delay before executing, to give the
592 // dialog a chance to go away before it takes a
593 // screenshot.
594 mHandler.postDelayed(new Runnable() {
595 @Override
596 public void run() {
597 try {
598 // Take an "interactive" bugreport.
599 MetricsLogger.action(mContext,
600 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
601 ActivityManager.getService().requestBugReport(
602 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
603 } catch (RemoteException e) {
604 }
605 }
606 }, 500);
607 }
608
609 @Override
610 public boolean onLongPress() {
611 // don't actually trigger the bugreport if we are running stability
612 // tests via monkey
613 if (ActivityManager.isUserAMonkey()) {
614 return false;
615 }
616 try {
617 // Take a "full" bugreport.
618 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
619 ActivityManager.getService().requestBugReport(
620 ActivityManager.BUGREPORT_OPTION_FULL);
621 } catch (RemoteException e) {
622 }
623 return false;
624 }
625
626 public boolean showDuringKeyguard() {
627 return true;
628 }
629
630 @Override
631 public boolean showBeforeProvisioning() {
632 return false;
633 }
Jason Monk361915c2017-03-21 20:33:59 -0400634 }
635
Alex Chau04458852017-11-27 18:21:23 +0000636 private final class LogoutAction extends SinglePressAction {
637 private LogoutAction() {
638 super(R.drawable.ic_logout, R.string.global_action_logout);
639 }
640
641 @Override
642 public boolean showDuringKeyguard() {
643 return true;
644 }
645
646 @Override
647 public boolean showBeforeProvisioning() {
648 return false;
649 }
650
651 @Override
652 public void onPress() {
653 // Add a little delay before executing, to give the dialog a chance to go away before
654 // switching user
655 mHandler.postDelayed(() -> {
656 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000657 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000658 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000659 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000660 } catch (RemoteException re) {
661 Log.e(TAG, "Couldn't logout user " + re);
662 }
663 }, 500);
664 }
665 }
666
Jason Monk361915c2017-03-21 20:33:59 -0400667 private Action getSettingsAction() {
668 return new SinglePressAction(R.drawable.ic_settings,
669 R.string.global_action_settings) {
670
671 @Override
672 public void onPress() {
673 Intent intent = new Intent(Settings.ACTION_SETTINGS);
674 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
675 mContext.startActivity(intent);
676 }
677
678 @Override
679 public boolean showDuringKeyguard() {
680 return true;
681 }
682
683 @Override
684 public boolean showBeforeProvisioning() {
685 return true;
686 }
687 };
688 }
689
690 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800691 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400692 if (!shouldUseSeparatedView()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800693 // use un-colored legacy treatment
694 emergencyIcon.setTintList(null);
695 }
696
Jason Monk361915c2017-03-21 20:33:59 -0400697 return new SinglePressAction(R.drawable.emergency_icon,
698 R.string.global_action_emergency) {
699 @Override
700 public void onPress() {
701 mEmergencyAffordanceManager.performEmergencyCall();
702 }
703
704 @Override
705 public boolean showDuringKeyguard() {
706 return true;
707 }
708
709 @Override
710 public boolean showBeforeProvisioning() {
711 return true;
712 }
713 };
714 }
715
716 private Action getAssistAction() {
717 return new SinglePressAction(R.drawable.ic_action_assist_focused,
718 R.string.global_action_assist) {
719 @Override
720 public void onPress() {
721 Intent intent = new Intent(Intent.ACTION_ASSIST);
722 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
723 mContext.startActivity(intent);
724 }
725
726 @Override
727 public boolean showDuringKeyguard() {
728 return true;
729 }
730
731 @Override
732 public boolean showBeforeProvisioning() {
733 return true;
734 }
735 };
736 }
737
738 private Action getVoiceAssistAction() {
739 return new SinglePressAction(R.drawable.ic_voice_search,
740 R.string.global_action_voice_assist) {
741 @Override
742 public void onPress() {
743 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
744 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
745 mContext.startActivity(intent);
746 }
747
748 @Override
749 public boolean showDuringKeyguard() {
750 return true;
751 }
752
753 @Override
754 public boolean showBeforeProvisioning() {
755 return true;
756 }
757 };
758 }
759
760 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400761 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400762 R.string.global_action_lockdown) {
763
764 @Override
765 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700766 new LockPatternUtils(mContext)
767 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
768 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400769 try {
770 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100771 // Lock profiles (if any) on the background thread.
772 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
773 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400774 } catch (RemoteException e) {
775 Log.e(TAG, "Error while trying to lock device.", e);
776 }
777 }
778
779 @Override
780 public boolean showDuringKeyguard() {
781 return true;
782 }
783
784 @Override
785 public boolean showBeforeProvisioning() {
786 return false;
787 }
788 };
789 }
790
Pavel Grafov059021b2018-05-02 13:44:46 +0100791 private void lockProfiles() {
792 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
793 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
794 final int currentUserId = getCurrentUser().id;
795 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
796 for (final int id : profileIds) {
797 if (id != currentUserId) {
798 tm.setDeviceLockedForUser(id, true);
799 }
800 }
801 }
802
Jason Monk361915c2017-03-21 20:33:59 -0400803 private UserInfo getCurrentUser() {
804 try {
805 return ActivityManager.getService().getCurrentUser();
806 } catch (RemoteException re) {
807 return null;
808 }
809 }
810
811 private boolean isCurrentUserOwner() {
812 UserInfo currentUser = getCurrentUser();
813 return currentUser == null || currentUser.isPrimary();
814 }
815
816 private void addUsersToMenu(ArrayList<Action> items) {
817 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
818 if (um.isUserSwitcherEnabled()) {
819 List<UserInfo> users = um.getUsers();
820 UserInfo currentUser = getCurrentUser();
821 for (final UserInfo user : users) {
822 if (user.supportsSwitchToByUser()) {
823 boolean isCurrentUser = currentUser == null
824 ? user.id == 0 : (currentUser.id == user.id);
825 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
826 : null;
827 SinglePressAction switchToUser = new SinglePressAction(
828 R.drawable.ic_menu_cc, icon,
829 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400830 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400831 public void onPress() {
832 try {
833 ActivityManager.getService().switchUser(user.id);
834 } catch (RemoteException re) {
835 Log.e(TAG, "Couldn't switch user " + re);
836 }
837 }
838
839 public boolean showDuringKeyguard() {
840 return true;
841 }
842
843 public boolean showBeforeProvisioning() {
844 return false;
845 }
846 };
847 items.add(switchToUser);
848 }
849 }
850 }
851 }
852
853 private void prepareDialog() {
854 refreshSilentMode();
855 mAirplaneModeOn.updateState(mAirplaneState);
856 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400857 if (mShowSilentToggle) {
858 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
859 mContext.registerReceiver(mRingerModeReceiver, filter);
860 }
861 }
862
863 private void refreshSilentMode() {
864 if (!mHasVibrator) {
865 final boolean silentModeOn =
866 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400867 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400868 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
869 }
870 }
871
872 /** {@inheritDoc} */
873 public void onDismiss(DialogInterface dialog) {
874 mWindowManagerFuncs.onGlobalActionsHidden();
875 if (mShowSilentToggle) {
876 try {
877 mContext.unregisterReceiver(mRingerModeReceiver);
878 } catch (IllegalArgumentException ie) {
879 // ignore this
880 Log.w(TAG, ie);
881 }
882 }
883 }
884
885 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800886 public void onShow(DialogInterface dialog) {
887 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
888 }
889
Jason Monk361915c2017-03-21 20:33:59 -0400890 /**
891 * The adapter used for the list within the global actions dialog, taking
892 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400893 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
894 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400895 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
896 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500897 public class MyAdapter extends MultiListAdapter {
898 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400899 public int getCount() {
900 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400901 for (int i = 0; i < mItems.size(); i++) {
902 final Action action = mItems.get(i);
903
904 if (mKeyguardShowing && !action.showDuringKeyguard()) {
905 continue;
906 }
907 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
908 continue;
909 }
910 count++;
911 }
912 return count;
913 }
914
915 @Override
916 public boolean isEnabled(int position) {
917 return getItem(position).isEnabled();
918 }
919
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500920 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400921 public ArrayList<Action> getSeparatedItems() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500922 ArrayList<Action> separatedActions = new ArrayList<Action>();
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400923 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500924 return separatedActions;
925 }
926 for (int i = 0; i < mItems.size(); i++) {
927 final Action action = mItems.get(i);
928 if (action.shouldBeSeparated()) {
929 separatedActions.add(action);
930 }
931 }
932 return separatedActions;
933 }
934
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500935 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400936 public ArrayList<Action> getListItems() {
937 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500938 return new ArrayList<Action>(mItems);
939 }
940 ArrayList<Action> listActions = new ArrayList<Action>();
941 for (int i = 0; i < mItems.size(); i++) {
942 final Action action = mItems.get(i);
943 if (!action.shouldBeSeparated()) {
944 listActions.add(action);
945 }
946 }
947 return listActions;
948 }
949
Jason Monk361915c2017-03-21 20:33:59 -0400950 @Override
951 public boolean areAllItemsEnabled() {
952 return false;
953 }
954
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500955 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400956 public Action getItem(int position) {
957
958 int filteredPos = 0;
959 for (int i = 0; i < mItems.size(); i++) {
960 final Action action = mItems.get(i);
961 if (mKeyguardShowing && !action.showDuringKeyguard()) {
962 continue;
963 }
964 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
965 continue;
966 }
967 if (filteredPos == position) {
968 return action;
969 }
970 filteredPos++;
971 }
972
973 throw new IllegalArgumentException("position " + position
974 + " out of range of showable actions"
975 + ", filtered count=" + getCount()
976 + ", keyguardshowing=" + mKeyguardShowing
977 + ", provisioned=" + mDeviceProvisioned);
978 }
979
980
981 public long getItemId(int position) {
982 return position;
983 }
984
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500985 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400986 public View getView(int position, View convertView, ViewGroup parent) {
987 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400988 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500989 // Everything but screenshot, the last item, gets white background.
990 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500991 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400992 }
993 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400994 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500995
996 @Override
997 public boolean onLongClickItem(int position) {
998 final Action action = mAdapter.getItem(position);
999 if (action instanceof LongPressAction) {
1000 mDialog.dismiss();
1001 return ((LongPressAction) action).onLongPress();
1002 }
1003 return false;
1004 }
1005
1006 @Override
1007 public void onClickItem(int position) {
1008 Action item = mAdapter.getItem(position);
1009 if (!(item instanceof SilentModeTriStateAction)) {
1010 mDialog.dismiss();
1011 }
1012 item.onPress();
1013 }
Jason Monk361915c2017-03-21 20:33:59 -04001014 }
1015
1016 // note: the scheme below made more sense when we were planning on having
1017 // 8 different things in the global actions dialog. seems overkill with
1018 // only 3 items now, but may as well keep this flexible approach so it will
1019 // be easy should someone decide at the last minute to include something
1020 // else, such as 'enable wifi', or 'enable bluetooth'
1021
1022 /**
1023 * What each item in the global actions dialog must be able to support.
1024 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001025 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001026 /**
1027 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001028 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001029 */
1030 CharSequence getLabelForAccessibility(Context context);
1031
1032 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1033
1034 void onPress();
1035
1036 /**
1037 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001038 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001039 */
1040 boolean showDuringKeyguard();
1041
1042 /**
1043 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001044 * device is provisioned.onlongpress
1045 *
Jason Monk361915c2017-03-21 20:33:59 -04001046 */
1047 boolean showBeforeProvisioning();
1048
1049 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001050
1051 default boolean shouldBeSeparated() {
1052 return false;
1053 }
Jason Monk361915c2017-03-21 20:33:59 -04001054 }
1055
1056 /**
1057 * An action that also supports long press.
1058 */
1059 private interface LongPressAction extends Action {
1060 boolean onLongPress();
1061 }
1062
1063 /**
1064 * A single press action maintains no state, just responds to a press
1065 * and takes an action.
1066 */
1067 private static abstract class SinglePressAction implements Action {
1068 private final int mIconResId;
1069 private final Drawable mIcon;
1070 private final int mMessageResId;
1071 private final CharSequence mMessage;
1072
1073 protected SinglePressAction(int iconResId, int messageResId) {
1074 mIconResId = iconResId;
1075 mMessageResId = messageResId;
1076 mMessage = null;
1077 mIcon = null;
1078 }
1079
1080 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1081 mIconResId = iconResId;
1082 mMessageResId = 0;
1083 mMessage = message;
1084 mIcon = icon;
1085 }
1086
1087 public boolean isEnabled() {
1088 return true;
1089 }
1090
1091 public String getStatus() {
1092 return null;
1093 }
1094
1095 abstract public void onPress();
1096
1097 public CharSequence getLabelForAccessibility(Context context) {
1098 if (mMessage != null) {
1099 return mMessage;
1100 } else {
1101 return context.getString(mMessageResId);
1102 }
1103 }
1104
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001105 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001106 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001107 return com.android.systemui.R.layout.global_actions_grid_item;
1108 }
1109 return com.android.systemui.R.layout.global_actions_item;
1110 }
1111
Jason Monk361915c2017-03-21 20:33:59 -04001112 public View create(
1113 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001114 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001115 false);
Jason Monk361915c2017-03-21 20:33:59 -04001116
1117 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1118 TextView messageView = (TextView) v.findViewById(R.id.message);
1119
1120 TextView statusView = (TextView) v.findViewById(R.id.status);
1121 final String status = getStatus();
1122 if (!TextUtils.isEmpty(status)) {
1123 statusView.setText(status);
1124 } else {
1125 statusView.setVisibility(View.GONE);
1126 }
1127 if (mIcon != null) {
1128 icon.setImageDrawable(mIcon);
1129 icon.setScaleType(ScaleType.CENTER_CROP);
1130 } else if (mIconResId != 0) {
1131 icon.setImageDrawable(context.getDrawable(mIconResId));
1132 }
1133 if (mMessage != null) {
1134 messageView.setText(mMessage);
1135 } else {
1136 messageView.setText(mMessageResId);
1137 }
1138
1139 return v;
1140 }
1141 }
1142
1143 /**
1144 * A toggle action knows whether it is on or off, and displays an icon
1145 * and status message accordingly.
1146 */
1147 private static abstract class ToggleAction implements Action {
1148
1149 enum State {
1150 Off(false),
1151 TurningOn(true),
1152 TurningOff(true),
1153 On(false);
1154
1155 private final boolean inTransition;
1156
1157 State(boolean intermediate) {
1158 inTransition = intermediate;
1159 }
1160
1161 public boolean inTransition() {
1162 return inTransition;
1163 }
1164 }
1165
1166 protected State mState = State.Off;
1167
1168 // prefs
1169 protected int mEnabledIconResId;
1170 protected int mDisabledIconResid;
1171 protected int mMessageResId;
1172 protected int mEnabledStatusMessageResId;
1173 protected int mDisabledStatusMessageResId;
1174
1175 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001176 * @param enabledIconResId The icon for when this action is on.
1177 * @param disabledIconResid The icon for when this action is off.
1178 * @param message The general information message, e.g 'Silent Mode'
1179 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001180 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1181 */
1182 public ToggleAction(int enabledIconResId,
1183 int disabledIconResid,
1184 int message,
1185 int enabledStatusMessageResId,
1186 int disabledStatusMessageResId) {
1187 mEnabledIconResId = enabledIconResId;
1188 mDisabledIconResid = disabledIconResid;
1189 mMessageResId = message;
1190 mEnabledStatusMessageResId = enabledStatusMessageResId;
1191 mDisabledStatusMessageResId = disabledStatusMessageResId;
1192 }
1193
1194 /**
1195 * Override to make changes to resource IDs just before creating the
1196 * View.
1197 */
1198 void willCreate() {
1199
1200 }
1201
1202 @Override
1203 public CharSequence getLabelForAccessibility(Context context) {
1204 return context.getString(mMessageResId);
1205 }
1206
1207 public View create(Context context, View convertView, ViewGroup parent,
1208 LayoutInflater inflater) {
1209 willCreate();
1210
1211 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001212 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001213
1214 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1215 TextView messageView = (TextView) v.findViewById(R.id.message);
1216 TextView statusView = (TextView) v.findViewById(R.id.status);
1217 final boolean enabled = isEnabled();
1218
1219 if (messageView != null) {
1220 messageView.setText(mMessageResId);
1221 messageView.setEnabled(enabled);
1222 }
1223
1224 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1225 if (icon != null) {
1226 icon.setImageDrawable(context.getDrawable(
1227 (on ? mEnabledIconResId : mDisabledIconResid)));
1228 icon.setEnabled(enabled);
1229 }
1230
1231 if (statusView != null) {
1232 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1233 statusView.setVisibility(View.VISIBLE);
1234 statusView.setEnabled(enabled);
1235 }
1236 v.setEnabled(enabled);
1237
1238 return v;
1239 }
1240
1241 public final void onPress() {
1242 if (mState.inTransition()) {
1243 Log.w(TAG, "shouldn't be able to toggle when in transition");
1244 return;
1245 }
1246
1247 final boolean nowOn = !(mState == State.On);
1248 onToggle(nowOn);
1249 changeStateFromPress(nowOn);
1250 }
1251
1252 public boolean isEnabled() {
1253 return !mState.inTransition();
1254 }
1255
1256 /**
1257 * Implementations may override this if their state can be in on of the intermediate
1258 * states until some notification is received (e.g airplane mode is 'turning off' until
1259 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001260 *
Jason Monk361915c2017-03-21 20:33:59 -04001261 * @param buttonOn Whether the button was turned on or off
1262 */
1263 protected void changeStateFromPress(boolean buttonOn) {
1264 mState = buttonOn ? State.On : State.Off;
1265 }
1266
1267 abstract void onToggle(boolean on);
1268
1269 public void updateState(State state) {
1270 mState = state;
1271 }
1272 }
1273
1274 private class SilentModeToggleAction extends ToggleAction {
1275 public SilentModeToggleAction() {
1276 super(R.drawable.ic_audio_vol_mute,
1277 R.drawable.ic_audio_vol,
1278 R.string.global_action_toggle_silent_mode,
1279 R.string.global_action_silent_mode_on_status,
1280 R.string.global_action_silent_mode_off_status);
1281 }
1282
1283 void onToggle(boolean on) {
1284 if (on) {
1285 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1286 } else {
1287 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1288 }
1289 }
1290
1291 public boolean showDuringKeyguard() {
1292 return true;
1293 }
1294
1295 public boolean showBeforeProvisioning() {
1296 return false;
1297 }
1298 }
1299
1300 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1301
Jason Monk16fbd9d2017-04-27 14:28:49 -04001302 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001303
1304 private final AudioManager mAudioManager;
1305 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001306
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001307 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001308 mAudioManager = audioManager;
1309 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001310 }
1311
1312 private int ringerModeToIndex(int ringerMode) {
1313 // They just happen to coincide
1314 return ringerMode;
1315 }
1316
1317 private int indexToRingerMode(int index) {
1318 // They just happen to coincide
1319 return index;
1320 }
1321
1322 @Override
1323 public CharSequence getLabelForAccessibility(Context context) {
1324 return null;
1325 }
1326
1327 public View create(Context context, View convertView, ViewGroup parent,
1328 LayoutInflater inflater) {
1329 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1330
1331 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1332 for (int i = 0; i < 3; i++) {
1333 View itemView = v.findViewById(ITEM_IDS[i]);
1334 itemView.setSelected(selectedIndex == i);
1335 // Set up click handler
1336 itemView.setTag(i);
1337 itemView.setOnClickListener(this);
1338 }
1339 return v;
1340 }
1341
1342 public void onPress() {
1343 }
1344
1345 public boolean showDuringKeyguard() {
1346 return true;
1347 }
1348
1349 public boolean showBeforeProvisioning() {
1350 return false;
1351 }
1352
1353 public boolean isEnabled() {
1354 return true;
1355 }
1356
1357 void willCreate() {
1358 }
1359
1360 public void onClick(View v) {
1361 if (!(v.getTag() instanceof Integer)) return;
1362
1363 int index = (Integer) v.getTag();
1364 mAudioManager.setRingerMode(indexToRingerMode(index));
1365 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1366 }
1367 }
1368
1369 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1370 public void onReceive(Context context, Intent intent) {
1371 String action = intent.getAction();
1372 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1373 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1374 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1375 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001376 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001377 }
1378 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1379 // Airplane mode can be changed after ECM exits if airplane toggle button
1380 // is pressed during ECM mode
1381 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1382 mIsWaitingForEcmExit) {
1383 mIsWaitingForEcmExit = false;
1384 changeAirplaneModeSystemSetting(true);
1385 }
1386 }
1387 }
1388 };
1389
1390 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1391 @Override
1392 public void onServiceStateChanged(ServiceState serviceState) {
1393 if (!mHasTelephony) return;
1394 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1395 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1396 mAirplaneModeOn.updateState(mAirplaneState);
1397 mAdapter.notifyDataSetChanged();
1398 }
1399 };
1400
1401 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1402 @Override
1403 public void onReceive(Context context, Intent intent) {
1404 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1405 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1406 }
1407 }
1408 };
1409
1410 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1411 @Override
1412 public void onChange(boolean selfChange) {
1413 onAirplaneModeChanged();
1414 }
1415 };
1416
1417 private static final int MESSAGE_DISMISS = 0;
1418 private static final int MESSAGE_REFRESH = 1;
1419 private static final int MESSAGE_SHOW = 2;
1420 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1421
1422 private Handler mHandler = new Handler() {
1423 public void handleMessage(Message msg) {
1424 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001425 case MESSAGE_DISMISS:
1426 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001427 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1428 mDialog.dismissImmediately();
1429 } else {
1430 mDialog.dismiss();
1431 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001432 mDialog = null;
1433 }
1434 break;
1435 case MESSAGE_REFRESH:
1436 refreshSilentMode();
1437 mAdapter.notifyDataSetChanged();
1438 break;
1439 case MESSAGE_SHOW:
1440 handleShow();
1441 break;
Jason Monk361915c2017-03-21 20:33:59 -04001442 }
1443 }
1444 };
1445
1446 private void onAirplaneModeChanged() {
1447 // Let the service state callbacks handle the state.
1448 if (mHasTelephony) return;
1449
1450 boolean airplaneModeOn = Settings.Global.getInt(
1451 mContext.getContentResolver(),
1452 Settings.Global.AIRPLANE_MODE_ON,
1453 0) == 1;
1454 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1455 mAirplaneModeOn.updateState(mAirplaneState);
1456 }
1457
1458 /**
1459 * Change the airplane mode system setting
1460 */
1461 private void changeAirplaneModeSystemSetting(boolean on) {
1462 Settings.Global.putInt(
1463 mContext.getContentResolver(),
1464 Settings.Global.AIRPLANE_MODE_ON,
1465 on ? 1 : 0);
1466 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1467 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1468 intent.putExtra("state", on);
1469 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1470 if (!mHasTelephony) {
1471 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1472 }
1473 }
1474
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001475 private static final class ActionsDialog extends Dialog implements DialogInterface,
1476 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001477
Jason Monk16fbd9d2017-04-27 14:28:49 -04001478 private final Context mContext;
1479 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001480 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001481 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001482 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001483 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001484 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001485 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001486 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001487
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001488 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001489 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001490 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001491 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001492 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001493 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001494
1495 // Window initialization
1496 Window window = getWindow();
1497 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001498 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1499 window.getDecorView();
1500 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1501 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1502 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1503 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1504 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001505 window.addFlags(
1506 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001507 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001508 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001509 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1510 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1511 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001512 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1513
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001514 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001515
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001516 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001517
1518 mPanelController = plugin;
1519 View panelView = initializePanel();
1520 if (panelView == null) {
1521 mBackgroundDrawable = new GradientDrawable(context);
1522 mScrimAlpha = 0.7f;
1523 } else {
1524 mBackgroundDrawable = context.getDrawable(
1525 com.android.systemui.R.drawable.global_action_panel_scrim);
1526 mScrimAlpha = 1f;
1527 addContentView(
1528 panelView,
1529 new ViewGroup.LayoutParams(
1530 ViewGroup.LayoutParams.MATCH_PARENT,
1531 ViewGroup.LayoutParams.MATCH_PARENT));
1532 }
1533 window.setBackgroundDrawable(mBackgroundDrawable);
1534 }
1535
1536 private View initializePanel() {
1537 if (isPanelEnabled(mContext) && mPanelController != null) {
1538 View panelView = mPanelController.getPanelContent();
1539 if (panelView != null) {
1540 FrameLayout panelContainer = new FrameLayout(mContext);
1541 FrameLayout.LayoutParams panelParams =
1542 new FrameLayout.LayoutParams(
1543 FrameLayout.LayoutParams.MATCH_PARENT,
1544 FrameLayout.LayoutParams.WRAP_CONTENT);
1545 panelParams.topMargin = mContext.getResources().getDimensionPixelSize(
1546 com.android.systemui.R.dimen.global_actions_panel_top_margin);
1547 panelContainer.addView(panelView, panelParams);
1548 return panelContainer;
1549 }
1550 }
1551 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001552 }
1553
1554 private void initializeLayout() {
1555 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001556 mGlobalActionsLayout = (MultiListLayout)
1557 findViewById(com.android.systemui.R.id.global_actions_view);
1558 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001559 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001560 @Override
1561 public boolean dispatchPopulateAccessibilityEvent(
1562 View host, AccessibilityEvent event) {
1563 // Populate the title here, just as Activity does
1564 event.getText().add(mContext.getString(R.string.global_actions));
1565 return true;
1566 }
1567 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001568 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001569 mGlobalActionsLayout.setAdapter(mAdapter);
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001570 mGlobalActionsLayout.setSnapToEdge(isPanelEnabled(mContext)
1571 && mPanelController != null);
Steve Elliott9b87a442019-03-05 10:24:16 -05001572 }
1573
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001574 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001575 if (isGridEnabled(context)) {
1576 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1577 return com.android.systemui.R.layout.global_actions_grid_seascape;
1578 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001579 return com.android.systemui.R.layout.global_actions_grid;
1580 }
1581 return com.android.systemui.R.layout.global_actions_wrapped;
1582 }
1583
Jason Monk361915c2017-03-21 20:33:59 -04001584 @Override
1585 protected void onStart() {
1586 super.setCanceledOnTouchOutside(true);
1587 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001588 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001589
Steve Elliott9b87a442019-03-05 10:24:16 -05001590 if (mBackgroundDrawable instanceof GradientDrawable) {
1591 Point displaySize = new Point();
1592 mContext.getDisplay().getRealSize(displaySize);
1593 mColorExtractor.addOnColorsChangedListener(this);
1594 ((GradientDrawable) mBackgroundDrawable)
1595 .setScreenSize(displaySize.x, displaySize.y);
1596 GradientColors colors = mColorExtractor.getColors(
1597 mKeyguardShowing
1598 ? WallpaperManager.FLAG_LOCK
1599 : WallpaperManager.FLAG_SYSTEM);
1600 updateColors(colors, false /* animate */);
1601 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001602 }
1603
1604 /**
1605 * Updates background and system bars according to current GradientColors.
1606 * @param colors Colors and hints to use.
1607 * @param animate Interpolates gradient if true, just sets otherwise.
1608 */
1609 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001610 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1611 return;
1612 }
1613 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001614 View decorView = getWindow().getDecorView();
1615 if (colors.supportsDarkText()) {
1616 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001617 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001618 } else {
1619 decorView.setSystemUiVisibility(0);
1620 }
Jason Monk361915c2017-03-21 20:33:59 -04001621 }
1622
1623 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001624 protected void onStop() {
1625 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001626 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001627 }
1628
1629 @Override
1630 public void show() {
1631 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001632 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001633 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001634 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1635 mGlobalActionsLayout.setAlpha(0);
1636 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001637 .alpha(1)
1638 .translationX(0)
1639 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001640 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001641 .setUpdateListener(animation -> {
1642 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001643 * mScrimAlpha * 255);
1644 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001645 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001646 .start();
1647 }
1648
1649 @Override
1650 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001651 if (!mShowing) {
1652 return;
1653 }
1654 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001655 mGlobalActionsLayout.setTranslationX(0);
1656 mGlobalActionsLayout.setAlpha(1);
1657 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001658 .alpha(0)
1659 .translationX(getAnimTranslation())
1660 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001661 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001662 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001663 .setUpdateListener(animation -> {
1664 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001665 * mScrimAlpha * 255);
1666 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001667 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001668 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001669 if (mPanelController != null) {
1670 mPanelController.onDismissed();
1671 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001672 }
1673
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001674 void dismissImmediately() {
1675 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001676 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001677 }
1678
Jason Monk16fbd9d2017-04-27 14:28:49 -04001679 private float getAnimTranslation() {
1680 return getContext().getResources().getDimension(
1681 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001682 }
1683
1684 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001685 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001686 if (mKeyguardShowing) {
1687 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001688 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1689 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001690 }
1691 } else {
1692 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001693 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1694 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001695 }
1696 }
1697 }
1698
1699 public void setKeyguardShowing(boolean keyguardShowing) {
1700 mKeyguardShowing = keyguardShowing;
1701 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001702
1703 public void onRotate(int from, int to) {
1704 if (mShowing && isGridEnabled(mContext)) {
1705 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001706 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001707 }
1708 }
Jason Monk361915c2017-03-21 20:33:59 -04001709 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001710
1711 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001712 * Determines whether or not the Global Actions menu should use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001713 */
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001714 private static boolean isGridEnabled(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001715 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1716 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001717
1718 /**
1719 * Determines whether or not the Global Actions Panel should appear when the power button
1720 * is held.
1721 */
1722 private static boolean isPanelEnabled(Context context) {
1723 return FeatureFlagUtils.isEnabled(
1724 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED); }
1725
1726 /**
1727 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1728 */
1729 private static boolean shouldUseSeparatedView() {
1730 return true;
1731 }
Jason Monk361915c2017-03-21 20:33:59 -04001732}