blob: 8518abf2474337fd832aa9a1cf9d8dc5cd9d292c [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;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070094import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080095import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050096import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050097import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070098
Jason Monk361915c2017-03-21 20:33:59 -040099import java.util.ArrayList;
100import java.util.List;
101
102/**
103 * Helper to show the global actions dialog. Each item is an {@link Action} that
104 * may show depending on whether the keyguard is showing, and whether the device
105 * is provisioned.
106 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500107public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
108 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400109
110 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
111 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700112 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400113
114 private static final String TAG = "GlobalActionsDialog";
115
116 private static final boolean SHOW_SILENT_TOGGLE = true;
117
118 /* Valid settings for global actions keys.
119 * see config.xml config_globalActionList */
120 private static final String GLOBAL_ACTION_KEY_POWER = "power";
121 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
122 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
123 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
124 private static final String GLOBAL_ACTION_KEY_USERS = "users";
125 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
126 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
127 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
128 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
129 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000130 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800131 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500132 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400133
134 private final Context mContext;
135 private final GlobalActionsManager mWindowManagerFuncs;
136 private final AudioManager mAudioManager;
137 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000138 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800139 private final LockPatternUtils mLockPatternUtils;
140 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400141
142 private ArrayList<Action> mItems;
143 private ActionsDialog mDialog;
144
145 private Action mSilentModeAction;
146 private ToggleAction mAirplaneModeOn;
147
148 private MyAdapter mAdapter;
149
150 private boolean mKeyguardShowing = false;
151 private boolean mDeviceProvisioned = false;
152 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
153 private boolean mIsWaitingForEcmExit = false;
154 private boolean mHasTelephony;
155 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000156 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800157 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400158 private final boolean mShowSilentToggle;
159 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500160 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400161 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400162 private final ActivityStarter mActivityStarter;
163 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500164
Jason Monk361915c2017-03-21 20:33:59 -0400165 /**
166 * @param context everything needs a context :(
167 */
168 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700169 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400170 mWindowManagerFuncs = windowManagerFuncs;
171 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
172 mDreamManager = IDreamManager.Stub.asInterface(
173 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000174 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
175 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800176 mLockPatternUtils = new LockPatternUtils(mContext);
177 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400178
179 // receive broadcasts
180 IntentFilter filter = new IntentFilter();
181 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
182 filter.addAction(Intent.ACTION_SCREEN_OFF);
183 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
184 context.registerReceiver(mBroadcastReceiver, filter);
185
186 ConnectivityManager cm = (ConnectivityManager)
187 context.getSystemService(Context.CONNECTIVITY_SERVICE);
188 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
189
190 // get notified of phone state changes
191 TelephonyManager telephonyManager =
192 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
193 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
194 mContext.getContentResolver().registerContentObserver(
195 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
196 mAirplaneModeObserver);
197 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
198 mHasVibrator = vibrator != null && vibrator.hasVibrator();
199
200 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
201 R.bool.config_useFixedVolume);
202
203 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500204 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400205 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700206
207 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500208
Steve Elliott4c868852019-03-14 16:25:41 -0400209 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400210 }
211
212 /**
213 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400214 *
Jason Monk361915c2017-03-21 20:33:59 -0400215 * @param keyguardShowing True if keyguard is showing
216 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400217 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
218 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400219 mKeyguardShowing = keyguardShowing;
220 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400221 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400222 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 Heuckrothc8343402019-03-13 16:27:15 -0400381 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800382 mItems.add(new EmergencyDialerAction());
383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else {
385 Log.e(TAG, "Invalid global action key " + actionKey);
386 }
387 // Add here so we don't add more than one.
388 addedKeys.add(actionKey);
389 }
390
391 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400392 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400393 }
394
395 mAdapter = new MyAdapter();
396
Steve Elliott9b87a442019-03-05 10:24:16 -0500397 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400398 mPanelPlugin != null
399 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400400 new GlobalActionsPanelPlugin.Callbacks() {
401 @Override
402 public void dismissGlobalActionsMenu() {
403 if (mDialog != null) {
404 mDialog.dismiss();
405 }
406 }
407
408 @Override
409 public void startPendingIntentDismissingKeyguard(
410 PendingIntent intent) {
411 mActivityStarter
412 .startPendingIntentDismissingKeyguard(intent);
413 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400414 },
415 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500416 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400417
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400418 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400419 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700420 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400421
422 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800423 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400424
425 return dialog;
426 }
427
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800428 private boolean shouldDisplayLockdown() {
429 int userId = getCurrentUser().id;
430 // Lockdown is meaningless without a place to go.
431 if (!mKeyguardManager.isDeviceSecure(userId)) {
432 return false;
433 }
434
435 // Only show the lockdown button if the device isn't locked down (for whatever reason).
436 int state = mLockPatternUtils.getStrongAuthForUser(userId);
437 return (state == STRONG_AUTH_NOT_REQUIRED
438 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
439 }
440
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700441 @Override
442 public void onUiModeChanged() {
443 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700444 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400445 mDialog.refreshDialog();
446 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700447 }
448
449 public void destroy() {
450 Dependency.get(ConfigurationController.class).removeCallback(this);
451 }
452
Jason Monk361915c2017-03-21 20:33:59 -0400453 private final class PowerAction extends SinglePressAction implements LongPressAction {
454 private PowerAction() {
455 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400456 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400457 }
458
459 @Override
460 public boolean onLongPress() {
461 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
462 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
463 mWindowManagerFuncs.reboot(true);
464 return true;
465 }
466 return false;
467 }
468
469 @Override
470 public boolean showDuringKeyguard() {
471 return true;
472 }
473
474 @Override
475 public boolean showBeforeProvisioning() {
476 return true;
477 }
478
479 @Override
480 public void onPress() {
481 // shutdown by making sure radio and power are handled accordingly.
482 mWindowManagerFuncs.shutdown();
483 }
484 }
485
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400486 private abstract class EmergencyAction extends SinglePressAction {
487 EmergencyAction(int iconResId, int messageResId) {
488 super(iconResId, messageResId);
489 }
490
491 @Override
492 public boolean shouldBeSeparated() {
493 return shouldUseSeparatedView();
494 }
495
496 @Override
497 public View create(
498 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
499 View v = super.create(context, convertView, parent, inflater);
500 int textColor;
501 if (shouldBeSeparated()) {
502 textColor = v.getResources().getColor(
503 com.android.systemui.R.color.global_actions_alert_text);
504 } else {
505 textColor = v.getResources().getColor(
506 com.android.systemui.R.color.global_actions_text);
507 }
508 TextView messageView = v.findViewById(R.id.message);
509 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400510 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400511 ImageView icon = (ImageView) v.findViewById(R.id.icon);
512 icon.getDrawable().setTint(textColor);
513 return v;
514 }
515
516 @Override
517 public boolean showDuringKeyguard() {
518 return true;
519 }
520
521 @Override
522 public boolean showBeforeProvisioning() {
523 return true;
524 }
525 }
526
527 private class EmergencyAffordanceAction extends EmergencyAction {
528 EmergencyAffordanceAction() {
529 super(R.drawable.emergency_icon,
530 R.string.global_action_emergency);
531 }
532
533 @Override
534 public void onPress() {
535 mEmergencyAffordanceManager.performEmergencyCall();
536 }
537 }
538
539 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800540 private EmergencyDialerAction() {
541 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800542 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800543 }
544
545 @Override
546 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800547 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800548 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800549 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
550 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
551 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800552 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
553 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800554 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
555 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800556 }
557
Jason Monk361915c2017-03-21 20:33:59 -0400558 private final class RestartAction extends SinglePressAction implements LongPressAction {
559 private RestartAction() {
560 super(R.drawable.ic_restart, R.string.global_action_restart);
561 }
562
563 @Override
564 public boolean onLongPress() {
565 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
566 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
567 mWindowManagerFuncs.reboot(true);
568 return true;
569 }
570 return false;
571 }
572
573 @Override
574 public boolean showDuringKeyguard() {
575 return true;
576 }
577
578 @Override
579 public boolean showBeforeProvisioning() {
580 return true;
581 }
582
583 @Override
584 public void onPress() {
585 mWindowManagerFuncs.reboot(false);
586 }
587 }
588
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400589 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500590 public ScreenshotAction() {
591 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
592 }
593
594 @Override
595 public void onPress() {
596 // Add a little delay before executing, to give the
597 // dialog a chance to go away before it takes a
598 // screenshot.
599 // TODO: instead, omit global action dialog layer
600 mHandler.postDelayed(new Runnable() {
601 @Override
602 public void run() {
603 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
604 MetricsLogger.action(mContext,
605 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
606 }
607 }, 500);
608 }
609
610 @Override
611 public boolean showDuringKeyguard() {
612 return true;
613 }
614
615 @Override
616 public boolean showBeforeProvisioning() {
617 return false;
618 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400619
620 @Override
621 public boolean onLongPress() {
622 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
623 mScreenRecordHelper.launchRecordPrompt();
624 } else {
625 onPress();
626 }
627 return true;
628 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500629 }
630
Jason Monk361915c2017-03-21 20:33:59 -0400631 private class BugReportAction extends SinglePressAction implements LongPressAction {
632
633 public BugReportAction() {
634 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
635 }
636
637 @Override
638 public void onPress() {
639 // don't actually trigger the bugreport if we are running stability
640 // tests via monkey
641 if (ActivityManager.isUserAMonkey()) {
642 return;
643 }
644 // Add a little delay before executing, to give the
645 // dialog a chance to go away before it takes a
646 // screenshot.
647 mHandler.postDelayed(new Runnable() {
648 @Override
649 public void run() {
650 try {
651 // Take an "interactive" bugreport.
652 MetricsLogger.action(mContext,
653 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
654 ActivityManager.getService().requestBugReport(
655 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
656 } catch (RemoteException e) {
657 }
658 }
659 }, 500);
660 }
661
662 @Override
663 public boolean onLongPress() {
664 // don't actually trigger the bugreport if we are running stability
665 // tests via monkey
666 if (ActivityManager.isUserAMonkey()) {
667 return false;
668 }
669 try {
670 // Take a "full" bugreport.
671 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
672 ActivityManager.getService().requestBugReport(
673 ActivityManager.BUGREPORT_OPTION_FULL);
674 } catch (RemoteException e) {
675 }
676 return false;
677 }
678
679 public boolean showDuringKeyguard() {
680 return true;
681 }
682
683 @Override
684 public boolean showBeforeProvisioning() {
685 return false;
686 }
Jason Monk361915c2017-03-21 20:33:59 -0400687 }
688
Alex Chau04458852017-11-27 18:21:23 +0000689 private final class LogoutAction extends SinglePressAction {
690 private LogoutAction() {
691 super(R.drawable.ic_logout, R.string.global_action_logout);
692 }
693
694 @Override
695 public boolean showDuringKeyguard() {
696 return true;
697 }
698
699 @Override
700 public boolean showBeforeProvisioning() {
701 return false;
702 }
703
704 @Override
705 public void onPress() {
706 // Add a little delay before executing, to give the dialog a chance to go away before
707 // switching user
708 mHandler.postDelayed(() -> {
709 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000710 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000711 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000712 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000713 } catch (RemoteException re) {
714 Log.e(TAG, "Couldn't logout user " + re);
715 }
716 }, 500);
717 }
718 }
719
Jason Monk361915c2017-03-21 20:33:59 -0400720 private Action getSettingsAction() {
721 return new SinglePressAction(R.drawable.ic_settings,
722 R.string.global_action_settings) {
723
724 @Override
725 public void onPress() {
726 Intent intent = new Intent(Settings.ACTION_SETTINGS);
727 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
728 mContext.startActivity(intent);
729 }
730
731 @Override
732 public boolean showDuringKeyguard() {
733 return true;
734 }
735
736 @Override
737 public boolean showBeforeProvisioning() {
738 return true;
739 }
740 };
741 }
742
Jason Monk361915c2017-03-21 20:33:59 -0400743 private Action getAssistAction() {
744 return new SinglePressAction(R.drawable.ic_action_assist_focused,
745 R.string.global_action_assist) {
746 @Override
747 public void onPress() {
748 Intent intent = new Intent(Intent.ACTION_ASSIST);
749 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
750 mContext.startActivity(intent);
751 }
752
753 @Override
754 public boolean showDuringKeyguard() {
755 return true;
756 }
757
758 @Override
759 public boolean showBeforeProvisioning() {
760 return true;
761 }
762 };
763 }
764
765 private Action getVoiceAssistAction() {
766 return new SinglePressAction(R.drawable.ic_voice_search,
767 R.string.global_action_voice_assist) {
768 @Override
769 public void onPress() {
770 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
771 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
772 mContext.startActivity(intent);
773 }
774
775 @Override
776 public boolean showDuringKeyguard() {
777 return true;
778 }
779
780 @Override
781 public boolean showBeforeProvisioning() {
782 return true;
783 }
784 };
785 }
786
787 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400788 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400789 R.string.global_action_lockdown) {
790
791 @Override
792 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700793 new LockPatternUtils(mContext)
794 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
795 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400796 try {
797 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100798 // Lock profiles (if any) on the background thread.
799 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
800 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400801 } catch (RemoteException e) {
802 Log.e(TAG, "Error while trying to lock device.", e);
803 }
804 }
805
806 @Override
807 public boolean showDuringKeyguard() {
808 return true;
809 }
810
811 @Override
812 public boolean showBeforeProvisioning() {
813 return false;
814 }
815 };
816 }
817
Pavel Grafov059021b2018-05-02 13:44:46 +0100818 private void lockProfiles() {
819 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
820 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
821 final int currentUserId = getCurrentUser().id;
822 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
823 for (final int id : profileIds) {
824 if (id != currentUserId) {
825 tm.setDeviceLockedForUser(id, true);
826 }
827 }
828 }
829
Jason Monk361915c2017-03-21 20:33:59 -0400830 private UserInfo getCurrentUser() {
831 try {
832 return ActivityManager.getService().getCurrentUser();
833 } catch (RemoteException re) {
834 return null;
835 }
836 }
837
838 private boolean isCurrentUserOwner() {
839 UserInfo currentUser = getCurrentUser();
840 return currentUser == null || currentUser.isPrimary();
841 }
842
843 private void addUsersToMenu(ArrayList<Action> items) {
844 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
845 if (um.isUserSwitcherEnabled()) {
846 List<UserInfo> users = um.getUsers();
847 UserInfo currentUser = getCurrentUser();
848 for (final UserInfo user : users) {
849 if (user.supportsSwitchToByUser()) {
850 boolean isCurrentUser = currentUser == null
851 ? user.id == 0 : (currentUser.id == user.id);
852 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
853 : null;
854 SinglePressAction switchToUser = new SinglePressAction(
855 R.drawable.ic_menu_cc, icon,
856 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400857 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400858 public void onPress() {
859 try {
860 ActivityManager.getService().switchUser(user.id);
861 } catch (RemoteException re) {
862 Log.e(TAG, "Couldn't switch user " + re);
863 }
864 }
865
866 public boolean showDuringKeyguard() {
867 return true;
868 }
869
870 public boolean showBeforeProvisioning() {
871 return false;
872 }
873 };
874 items.add(switchToUser);
875 }
876 }
877 }
878 }
879
880 private void prepareDialog() {
881 refreshSilentMode();
882 mAirplaneModeOn.updateState(mAirplaneState);
883 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400884 if (mShowSilentToggle) {
885 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
886 mContext.registerReceiver(mRingerModeReceiver, filter);
887 }
888 }
889
890 private void refreshSilentMode() {
891 if (!mHasVibrator) {
892 final boolean silentModeOn =
893 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400894 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400895 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
896 }
897 }
898
899 /** {@inheritDoc} */
900 public void onDismiss(DialogInterface dialog) {
901 mWindowManagerFuncs.onGlobalActionsHidden();
902 if (mShowSilentToggle) {
903 try {
904 mContext.unregisterReceiver(mRingerModeReceiver);
905 } catch (IllegalArgumentException ie) {
906 // ignore this
907 Log.w(TAG, ie);
908 }
909 }
910 }
911
912 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800913 public void onShow(DialogInterface dialog) {
914 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
915 }
916
Jason Monk361915c2017-03-21 20:33:59 -0400917 /**
918 * The adapter used for the list within the global actions dialog, taking
919 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400920 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
921 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400922 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
923 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500924 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400925 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400926 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400927 for (int i = 0; i < mItems.size(); i++) {
928 final Action action = mItems.get(i);
929
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400930 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
931 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400932 }
Jason Monk361915c2017-03-21 20:33:59 -0400933 }
934 return count;
935 }
936
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400937 private boolean shouldBeShown(Action action) {
938 if (mKeyguardShowing && !action.showDuringKeyguard()) {
939 return false;
940 }
941 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
942 return false;
943 }
944 return true;
945 }
946
947 @Override
948 public int countSeparatedItems() {
949 return countItems(true);
950 }
951
952 @Override
953 public int countListItems() {
954 return countItems(false);
955 }
956
957 @Override
958 public int getCount() {
959 return countSeparatedItems() + countListItems();
960 }
961
Jason Monk361915c2017-03-21 20:33:59 -0400962 @Override
963 public boolean isEnabled(int position) {
964 return getItem(position).isEnabled();
965 }
966
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500967 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400968 public boolean areAllItemsEnabled() {
969 return false;
970 }
971
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500972 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400973 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400974 int filteredPos = 0;
975 for (int i = 0; i < mItems.size(); i++) {
976 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400977 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400978 continue;
979 }
980 if (filteredPos == position) {
981 return action;
982 }
983 filteredPos++;
984 }
985
986 throw new IllegalArgumentException("position " + position
987 + " out of range of showable actions"
988 + ", filtered count=" + getCount()
989 + ", keyguardshowing=" + mKeyguardShowing
990 + ", provisioned=" + mDeviceProvisioned);
991 }
992
993
994 public long getItemId(int position) {
995 return position;
996 }
997
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500998 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400999 public View getView(int position, View convertView, ViewGroup parent) {
1000 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001001 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001002 view.setOnClickListener(v -> onClickItem(position));
1003 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001004 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001005 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001006
1007 @Override
1008 public boolean onLongClickItem(int position) {
1009 final Action action = mAdapter.getItem(position);
1010 if (action instanceof LongPressAction) {
1011 mDialog.dismiss();
1012 return ((LongPressAction) action).onLongPress();
1013 }
1014 return false;
1015 }
1016
1017 @Override
1018 public void onClickItem(int position) {
1019 Action item = mAdapter.getItem(position);
1020 if (!(item instanceof SilentModeTriStateAction)) {
1021 mDialog.dismiss();
1022 }
1023 item.onPress();
1024 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001025
1026 @Override
1027 public boolean shouldBeSeparated(int position) {
1028 return getItem(position).shouldBeSeparated();
1029 }
Jason Monk361915c2017-03-21 20:33:59 -04001030 }
1031
1032 // note: the scheme below made more sense when we were planning on having
1033 // 8 different things in the global actions dialog. seems overkill with
1034 // only 3 items now, but may as well keep this flexible approach so it will
1035 // be easy should someone decide at the last minute to include something
1036 // else, such as 'enable wifi', or 'enable bluetooth'
1037
1038 /**
1039 * What each item in the global actions dialog must be able to support.
1040 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001041 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001042 /**
1043 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001044 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001045 */
1046 CharSequence getLabelForAccessibility(Context context);
1047
1048 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1049
1050 void onPress();
1051
1052 /**
1053 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001054 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001055 */
1056 boolean showDuringKeyguard();
1057
1058 /**
1059 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001060 * device is provisioned.onlongpress
1061 *
Jason Monk361915c2017-03-21 20:33:59 -04001062 */
1063 boolean showBeforeProvisioning();
1064
1065 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001066
1067 default boolean shouldBeSeparated() {
1068 return false;
1069 }
Jason Monk361915c2017-03-21 20:33:59 -04001070 }
1071
1072 /**
1073 * An action that also supports long press.
1074 */
1075 private interface LongPressAction extends Action {
1076 boolean onLongPress();
1077 }
1078
1079 /**
1080 * A single press action maintains no state, just responds to a press
1081 * and takes an action.
1082 */
1083 private static abstract class SinglePressAction implements Action {
1084 private final int mIconResId;
1085 private final Drawable mIcon;
1086 private final int mMessageResId;
1087 private final CharSequence mMessage;
1088
1089 protected SinglePressAction(int iconResId, int messageResId) {
1090 mIconResId = iconResId;
1091 mMessageResId = messageResId;
1092 mMessage = null;
1093 mIcon = null;
1094 }
1095
1096 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1097 mIconResId = iconResId;
1098 mMessageResId = 0;
1099 mMessage = message;
1100 mIcon = icon;
1101 }
1102
1103 public boolean isEnabled() {
1104 return true;
1105 }
1106
1107 public String getStatus() {
1108 return null;
1109 }
1110
1111 abstract public void onPress();
1112
1113 public CharSequence getLabelForAccessibility(Context context) {
1114 if (mMessage != null) {
1115 return mMessage;
1116 } else {
1117 return context.getString(mMessageResId);
1118 }
1119 }
1120
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001121 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001122 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001123 }
1124
Jason Monk361915c2017-03-21 20:33:59 -04001125 public View create(
1126 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001127 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001128 false);
Jason Monk361915c2017-03-21 20:33:59 -04001129
1130 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1131 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001132 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001133
1134 TextView statusView = (TextView) v.findViewById(R.id.status);
1135 final String status = getStatus();
1136 if (!TextUtils.isEmpty(status)) {
1137 statusView.setText(status);
1138 } else {
1139 statusView.setVisibility(View.GONE);
1140 }
1141 if (mIcon != null) {
1142 icon.setImageDrawable(mIcon);
1143 icon.setScaleType(ScaleType.CENTER_CROP);
1144 } else if (mIconResId != 0) {
1145 icon.setImageDrawable(context.getDrawable(mIconResId));
1146 }
1147 if (mMessage != null) {
1148 messageView.setText(mMessage);
1149 } else {
1150 messageView.setText(mMessageResId);
1151 }
1152
1153 return v;
1154 }
1155 }
1156
1157 /**
1158 * A toggle action knows whether it is on or off, and displays an icon
1159 * and status message accordingly.
1160 */
1161 private static abstract class ToggleAction implements Action {
1162
1163 enum State {
1164 Off(false),
1165 TurningOn(true),
1166 TurningOff(true),
1167 On(false);
1168
1169 private final boolean inTransition;
1170
1171 State(boolean intermediate) {
1172 inTransition = intermediate;
1173 }
1174
1175 public boolean inTransition() {
1176 return inTransition;
1177 }
1178 }
1179
1180 protected State mState = State.Off;
1181
1182 // prefs
1183 protected int mEnabledIconResId;
1184 protected int mDisabledIconResid;
1185 protected int mMessageResId;
1186 protected int mEnabledStatusMessageResId;
1187 protected int mDisabledStatusMessageResId;
1188
1189 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001190 * @param enabledIconResId The icon for when this action is on.
1191 * @param disabledIconResid The icon for when this action is off.
1192 * @param message The general information message, e.g 'Silent Mode'
1193 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001194 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1195 */
1196 public ToggleAction(int enabledIconResId,
1197 int disabledIconResid,
1198 int message,
1199 int enabledStatusMessageResId,
1200 int disabledStatusMessageResId) {
1201 mEnabledIconResId = enabledIconResId;
1202 mDisabledIconResid = disabledIconResid;
1203 mMessageResId = message;
1204 mEnabledStatusMessageResId = enabledStatusMessageResId;
1205 mDisabledStatusMessageResId = disabledStatusMessageResId;
1206 }
1207
1208 /**
1209 * Override to make changes to resource IDs just before creating the
1210 * View.
1211 */
1212 void willCreate() {
1213
1214 }
1215
1216 @Override
1217 public CharSequence getLabelForAccessibility(Context context) {
1218 return context.getString(mMessageResId);
1219 }
1220
1221 public View create(Context context, View convertView, ViewGroup parent,
1222 LayoutInflater inflater) {
1223 willCreate();
1224
1225 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001226 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001227
1228 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1229 TextView messageView = (TextView) v.findViewById(R.id.message);
1230 TextView statusView = (TextView) v.findViewById(R.id.status);
1231 final boolean enabled = isEnabled();
1232
1233 if (messageView != null) {
1234 messageView.setText(mMessageResId);
1235 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001236 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001237 }
1238
1239 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1240 if (icon != null) {
1241 icon.setImageDrawable(context.getDrawable(
1242 (on ? mEnabledIconResId : mDisabledIconResid)));
1243 icon.setEnabled(enabled);
1244 }
1245
1246 if (statusView != null) {
1247 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1248 statusView.setVisibility(View.VISIBLE);
1249 statusView.setEnabled(enabled);
1250 }
1251 v.setEnabled(enabled);
1252
1253 return v;
1254 }
1255
1256 public final void onPress() {
1257 if (mState.inTransition()) {
1258 Log.w(TAG, "shouldn't be able to toggle when in transition");
1259 return;
1260 }
1261
1262 final boolean nowOn = !(mState == State.On);
1263 onToggle(nowOn);
1264 changeStateFromPress(nowOn);
1265 }
1266
1267 public boolean isEnabled() {
1268 return !mState.inTransition();
1269 }
1270
1271 /**
1272 * Implementations may override this if their state can be in on of the intermediate
1273 * states until some notification is received (e.g airplane mode is 'turning off' until
1274 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001275 *
Jason Monk361915c2017-03-21 20:33:59 -04001276 * @param buttonOn Whether the button was turned on or off
1277 */
1278 protected void changeStateFromPress(boolean buttonOn) {
1279 mState = buttonOn ? State.On : State.Off;
1280 }
1281
1282 abstract void onToggle(boolean on);
1283
1284 public void updateState(State state) {
1285 mState = state;
1286 }
1287 }
1288
1289 private class SilentModeToggleAction extends ToggleAction {
1290 public SilentModeToggleAction() {
1291 super(R.drawable.ic_audio_vol_mute,
1292 R.drawable.ic_audio_vol,
1293 R.string.global_action_toggle_silent_mode,
1294 R.string.global_action_silent_mode_on_status,
1295 R.string.global_action_silent_mode_off_status);
1296 }
1297
1298 void onToggle(boolean on) {
1299 if (on) {
1300 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1301 } else {
1302 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1303 }
1304 }
1305
1306 public boolean showDuringKeyguard() {
1307 return true;
1308 }
1309
1310 public boolean showBeforeProvisioning() {
1311 return false;
1312 }
1313 }
1314
1315 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1316
Jason Monk16fbd9d2017-04-27 14:28:49 -04001317 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001318
1319 private final AudioManager mAudioManager;
1320 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001321
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001322 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001323 mAudioManager = audioManager;
1324 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001325 }
1326
1327 private int ringerModeToIndex(int ringerMode) {
1328 // They just happen to coincide
1329 return ringerMode;
1330 }
1331
1332 private int indexToRingerMode(int index) {
1333 // They just happen to coincide
1334 return index;
1335 }
1336
1337 @Override
1338 public CharSequence getLabelForAccessibility(Context context) {
1339 return null;
1340 }
1341
1342 public View create(Context context, View convertView, ViewGroup parent,
1343 LayoutInflater inflater) {
1344 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1345
1346 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1347 for (int i = 0; i < 3; i++) {
1348 View itemView = v.findViewById(ITEM_IDS[i]);
1349 itemView.setSelected(selectedIndex == i);
1350 // Set up click handler
1351 itemView.setTag(i);
1352 itemView.setOnClickListener(this);
1353 }
1354 return v;
1355 }
1356
1357 public void onPress() {
1358 }
1359
1360 public boolean showDuringKeyguard() {
1361 return true;
1362 }
1363
1364 public boolean showBeforeProvisioning() {
1365 return false;
1366 }
1367
1368 public boolean isEnabled() {
1369 return true;
1370 }
1371
1372 void willCreate() {
1373 }
1374
1375 public void onClick(View v) {
1376 if (!(v.getTag() instanceof Integer)) return;
1377
1378 int index = (Integer) v.getTag();
1379 mAudioManager.setRingerMode(indexToRingerMode(index));
1380 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1381 }
1382 }
1383
1384 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1385 public void onReceive(Context context, Intent intent) {
1386 String action = intent.getAction();
1387 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1388 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1389 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1390 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001391 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001392 }
1393 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1394 // Airplane mode can be changed after ECM exits if airplane toggle button
1395 // is pressed during ECM mode
1396 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1397 mIsWaitingForEcmExit) {
1398 mIsWaitingForEcmExit = false;
1399 changeAirplaneModeSystemSetting(true);
1400 }
1401 }
1402 }
1403 };
1404
1405 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1406 @Override
1407 public void onServiceStateChanged(ServiceState serviceState) {
1408 if (!mHasTelephony) return;
1409 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1410 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1411 mAirplaneModeOn.updateState(mAirplaneState);
1412 mAdapter.notifyDataSetChanged();
1413 }
1414 };
1415
1416 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1417 @Override
1418 public void onReceive(Context context, Intent intent) {
1419 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1420 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1421 }
1422 }
1423 };
1424
1425 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1426 @Override
1427 public void onChange(boolean selfChange) {
1428 onAirplaneModeChanged();
1429 }
1430 };
1431
1432 private static final int MESSAGE_DISMISS = 0;
1433 private static final int MESSAGE_REFRESH = 1;
1434 private static final int MESSAGE_SHOW = 2;
1435 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1436
1437 private Handler mHandler = new Handler() {
1438 public void handleMessage(Message msg) {
1439 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001440 case MESSAGE_DISMISS:
1441 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001442 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1443 mDialog.dismissImmediately();
1444 } else {
1445 mDialog.dismiss();
1446 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001447 mDialog = null;
1448 }
1449 break;
1450 case MESSAGE_REFRESH:
1451 refreshSilentMode();
1452 mAdapter.notifyDataSetChanged();
1453 break;
1454 case MESSAGE_SHOW:
1455 handleShow();
1456 break;
Jason Monk361915c2017-03-21 20:33:59 -04001457 }
1458 }
1459 };
1460
1461 private void onAirplaneModeChanged() {
1462 // Let the service state callbacks handle the state.
1463 if (mHasTelephony) return;
1464
1465 boolean airplaneModeOn = Settings.Global.getInt(
1466 mContext.getContentResolver(),
1467 Settings.Global.AIRPLANE_MODE_ON,
1468 0) == 1;
1469 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1470 mAirplaneModeOn.updateState(mAirplaneState);
1471 }
1472
1473 /**
1474 * Change the airplane mode system setting
1475 */
1476 private void changeAirplaneModeSystemSetting(boolean on) {
1477 Settings.Global.putInt(
1478 mContext.getContentResolver(),
1479 Settings.Global.AIRPLANE_MODE_ON,
1480 on ? 1 : 0);
1481 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1482 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1483 intent.putExtra("state", on);
1484 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1485 if (!mHasTelephony) {
1486 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1487 }
1488 }
1489
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001490 private static final class ActionsDialog extends Dialog implements DialogInterface,
1491 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001492
Jason Monk16fbd9d2017-04-27 14:28:49 -04001493 private final Context mContext;
1494 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001495 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001496 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001497 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001498 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001499 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001500 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001501 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001502 private ResetOrientationData mResetOrientationData;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001503
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001504 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001505 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001506 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001507 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001508 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001509 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001510
1511 // Window initialization
1512 Window window = getWindow();
1513 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001514 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1515 window.getDecorView();
1516 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1517 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1518 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1519 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1520 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001521 window.addFlags(
1522 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001523 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001524 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001525 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1526 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1527 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001528 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001529 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001530
1531 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001532 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001533 }
1534
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001535 private boolean shouldUsePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001536 return isPanelEnabled(mContext)
1537 && mPanelController != null
1538 && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001539 }
1540
1541 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001542 int rotation = RotationUtils.getRotation(mContext);
1543 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1544 if (rotation != RotationUtils.ROTATION_NONE) {
1545 if (rotationLocked) {
1546 if (mResetOrientationData == null) {
1547 mResetOrientationData = new ResetOrientationData();
1548 mResetOrientationData.locked = true;
1549 mResetOrientationData.rotation = rotation;
1550 }
1551
1552 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
1553 RotationPolicy.setRotationLockAtAngle(
1554 mContext, false, RotationUtils.ROTATION_NONE);
1555 }
1556 } else {
1557 if (!rotationLocked) {
1558 if (mResetOrientationData == null) {
1559 mResetOrientationData = new ResetOrientationData();
1560 mResetOrientationData.locked = false;
1561 }
1562
1563 // Lock to portrait, so the user doesn't accidentally hide the panel.
1564 RotationPolicy.setRotationLockAtAngle(
1565 mContext, true, RotationUtils.ROTATION_NONE);
1566 }
1567
1568 FrameLayout panelContainer = new FrameLayout(mContext);
1569 FrameLayout.LayoutParams panelParams =
1570 new FrameLayout.LayoutParams(
1571 FrameLayout.LayoutParams.MATCH_PARENT,
1572 FrameLayout.LayoutParams.WRAP_CONTENT);
1573 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
1574 addContentView(
1575 panelContainer,
1576 new ViewGroup.LayoutParams(
1577 ViewGroup.LayoutParams.MATCH_PARENT,
1578 ViewGroup.LayoutParams.MATCH_PARENT));
Steve Elliott86ef6282019-05-08 14:45:04 -04001579 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1580 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001581 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001582 }
1583
1584 private void initializeLayout() {
1585 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001586 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001587 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001588 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001589 @Override
1590 public boolean dispatchPopulateAccessibilityEvent(
1591 View host, AccessibilityEvent event) {
1592 // Populate the title here, just as Activity does
1593 event.getText().add(mContext.getString(R.string.global_actions));
1594 return true;
1595 }
1596 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001597 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001598 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001599
Steve Elliott86ef6282019-05-08 14:45:04 -04001600 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001601 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001602 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001603 if (mBackgroundDrawable == null) {
1604 mBackgroundDrawable = new ScrimDrawable();
1605 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1606 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001607 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001608 }
1609
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001610 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001611 boolean useGridLayout = isForceGridEnabled(context) || shouldUsePanel();
1612 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1613 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001614 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001615 } else {
1616 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001617 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001618 } else {
1619 if (useGridLayout) {
1620 return com.android.systemui.R.layout.global_actions_grid;
1621 } else {
1622 return com.android.systemui.R.layout.global_actions_column;
1623 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001624 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001625 }
1626
Jason Monk361915c2017-03-21 20:33:59 -04001627 @Override
1628 protected void onStart() {
1629 super.setCanceledOnTouchOutside(true);
1630 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001631 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001632
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001633 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001634 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001635 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001636 updateColors(colors, false /* animate */);
1637 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001638 }
1639
1640 /**
1641 * Updates background and system bars according to current GradientColors.
1642 * @param colors Colors and hints to use.
1643 * @param animate Interpolates gradient if true, just sets otherwise.
1644 */
1645 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001646 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001647 return;
1648 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001649 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001650 View decorView = getWindow().getDecorView();
1651 if (colors.supportsDarkText()) {
1652 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001653 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001654 } else {
1655 decorView.setSystemUiVisibility(0);
1656 }
Jason Monk361915c2017-03-21 20:33:59 -04001657 }
1658
1659 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001660 protected void onStop() {
1661 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001662 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001663 }
1664
1665 @Override
1666 public void show() {
1667 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001668 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001669 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001670 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1671 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001672 mGlobalActionsLayout.setAlpha(0);
1673 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001674 .alpha(1)
1675 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001676 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001677 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001678 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001679 .setUpdateListener(animation -> {
1680 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001681 * mScrimAlpha * 255);
1682 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001683 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001684 .start();
1685 }
1686
1687 @Override
1688 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001689 if (!mShowing) {
1690 return;
1691 }
1692 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001693 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001694 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001695 mGlobalActionsLayout.setAlpha(1);
1696 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001697 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001698 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1699 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001700 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001701 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001702 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001703 .setUpdateListener(animation -> {
1704 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001705 * mScrimAlpha * 255);
1706 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001707 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001708 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001709 dismissPanel();
1710 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001711 }
1712
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001713 void dismissImmediately() {
1714 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001715 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001716 dismissPanel();
1717 resetOrientation();
1718 }
1719
1720 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001721 if (mPanelController != null) {
1722 mPanelController.onDismissed();
1723 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001724 }
1725
Steve Elliott48f75db2019-05-03 15:03:38 -04001726 private void resetOrientation() {
1727 if (mResetOrientationData != null) {
1728 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1729 mResetOrientationData.rotation);
1730 }
1731 }
1732
Jason Monk361915c2017-03-21 20:33:59 -04001733 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001734 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001735 if (mKeyguardShowing) {
1736 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001737 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1738 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001739 }
1740 } else {
1741 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001742 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1743 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001744 }
1745 }
1746 }
1747
1748 public void setKeyguardShowing(boolean keyguardShowing) {
1749 mKeyguardShowing = keyguardShowing;
1750 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001751
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001752 public void refreshDialog() {
1753 initializeLayout();
1754 mGlobalActionsLayout.updateList();
1755 }
1756
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001757 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001758 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001759 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001760 }
1761 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001762
1763 private static class ResetOrientationData {
1764 public boolean locked;
1765 public int rotation;
1766 }
Jason Monk361915c2017-03-21 20:33:59 -04001767 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001768
1769 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001770 * Determines whether or not the Global Actions menu should be forced to
1771 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001772 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001773 private static boolean isForceGridEnabled(Context context) {
1774 return FeatureFlagUtils.isEnabled(context,
1775 FeatureFlagUtils.FORCE_GLOBAL_ACTIONS_GRID_ENABLED);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001776 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001777
1778 /**
1779 * Determines whether or not the Global Actions Panel should appear when the power button
1780 * is held.
1781 */
1782 private static boolean isPanelEnabled(Context context) {
1783 return FeatureFlagUtils.isEnabled(
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001784 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1785 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001786
1787 /**
1788 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1789 */
1790 private static boolean shouldUseSeparatedView() {
1791 return true;
1792 }
Jason Monk361915c2017-03-21 20:33:59 -04001793}