blob: 9b69dc50a1a1bf814998a01a6adf2fcc4c9b878d [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;
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;
Steve Elliott4c868852019-03-14 16:25:41 -040089import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050091import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050092import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070093import com.android.systemui.statusbar.policy.ConfigurationController;
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;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400161 private final ActivityStarter mActivityStarter;
162 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500163
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
Steve Elliott4c868852019-03-14 16:25:41 -0400208 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400209 }
210
211 /**
212 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400213 *
Jason Monk361915c2017-03-21 20:33:59 -0400214 * @param keyguardShowing True if keyguard is showing
215 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400216 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
217 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400218 mKeyguardShowing = keyguardShowing;
219 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400220 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400221 if (mDialog != null) {
222 mDialog.dismiss();
223 mDialog = null;
224 // Show delayed, so that the dismiss of the previous dialog completes
225 mHandler.sendEmptyMessage(MESSAGE_SHOW);
226 } else {
227 handleShow();
228 }
229 }
230
Charles He9851a8d2017-10-10 17:31:30 +0100231 /**
232 * Dismiss the global actions dialog, if it's currently shown
233 */
234 public void dismissDialog() {
235 mHandler.removeMessages(MESSAGE_DISMISS);
236 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
237 }
238
Jason Monk361915c2017-03-21 20:33:59 -0400239 private void awakenIfNecessary() {
240 if (mDreamManager != null) {
241 try {
242 if (mDreamManager.isDreaming()) {
243 mDreamManager.awaken();
244 }
245 } catch (RemoteException e) {
246 // we tried
247 }
248 }
249 }
250
251 private void handleShow() {
252 awakenIfNecessary();
253 mDialog = createDialog();
254 prepareDialog();
255
256 // If we only have 1 item and it's a simple press action, just do this action.
257 if (mAdapter.getCount() == 1
258 && mAdapter.getItem(0) instanceof SinglePressAction
259 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
260 ((SinglePressAction) mAdapter.getItem(0)).onPress();
261 } else {
262 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
263 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100264 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400265 mDialog.getWindow().setAttributes(attrs);
266 mDialog.show();
267 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400268 }
269 }
270
271 /**
272 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400273 *
Jason Monk361915c2017-03-21 20:33:59 -0400274 * @return A new dialog.
275 */
276 private ActionsDialog createDialog() {
277 // Simple toggle style if there's no vibrator, otherwise use a tri-state
278 if (!mHasVibrator) {
279 mSilentModeAction = new SilentModeToggleAction();
280 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700281 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400282 }
283 mAirplaneModeOn = new ToggleAction(
284 R.drawable.ic_lock_airplane_mode,
285 R.drawable.ic_lock_airplane_mode_off,
286 R.string.global_actions_toggle_airplane_mode,
287 R.string.global_actions_airplane_mode_on_status,
288 R.string.global_actions_airplane_mode_off_status) {
289
290 void onToggle(boolean on) {
291 if (mHasTelephony && Boolean.parseBoolean(
292 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
293 mIsWaitingForEcmExit = true;
294 // Launch ECM exit dialog
295 Intent ecmDialogIntent =
296 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
297 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
298 mContext.startActivity(ecmDialogIntent);
299 } else {
300 changeAirplaneModeSystemSetting(on);
301 }
302 }
303
304 @Override
305 protected void changeStateFromPress(boolean buttonOn) {
306 if (!mHasTelephony) return;
307
308 // In ECM mode airplane state cannot be changed
309 if (!(Boolean.parseBoolean(
310 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
311 mState = buttonOn ? State.TurningOn : State.TurningOff;
312 mAirplaneState = mState;
313 }
314 }
315
316 public boolean showDuringKeyguard() {
317 return true;
318 }
319
320 public boolean showBeforeProvisioning() {
321 return false;
322 }
323 };
324 onAirplaneModeChanged();
325
326 mItems = new ArrayList<Action>();
327 String[] defaultActions = mContext.getResources().getStringArray(
328 R.array.config_globalActionsList);
329
330 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000331 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800332 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400333 for (int i = 0; i < defaultActions.length; i++) {
334 String actionKey = defaultActions[i];
335 if (addedKeys.contains(actionKey)) {
336 // If we already have added this, don't add it again.
337 continue;
338 }
339 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
340 mItems.add(new PowerAction());
341 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
342 mItems.add(mAirplaneModeOn);
343 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
344 if (Settings.Global.getInt(mContext.getContentResolver(),
345 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
346 mItems.add(new BugReportAction());
347 }
348 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
349 if (mShowSilentToggle) {
350 mItems.add(mSilentModeAction);
351 }
352 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
353 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
354 addUsersToMenu(mItems);
355 }
356 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
357 mItems.add(getSettingsAction());
358 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700359 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
360 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800361 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700362 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800363 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700364 }
Jason Monk361915c2017-03-21 20:33:59 -0400365 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
366 mItems.add(getVoiceAssistAction());
367 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
368 mItems.add(getAssistAction());
369 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
370 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500371 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
372 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000373 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000374 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000375 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
376 mItems.add(new LogoutAction());
377 mHasLogoutButton = true;
378 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800379 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400380 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800381 mItems.add(new EmergencyDialerAction());
382 }
Jason Monk361915c2017-03-21 20:33:59 -0400383 } else {
384 Log.e(TAG, "Invalid global action key " + actionKey);
385 }
386 // Add here so we don't add more than one.
387 addedKeys.add(actionKey);
388 }
389
390 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400391 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400392 }
393
394 mAdapter = new MyAdapter();
395
Steve Elliott9b87a442019-03-05 10:24:16 -0500396 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400397 mPanelPlugin != null
398 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400399 new GlobalActionsPanelPlugin.Callbacks() {
400 @Override
401 public void dismissGlobalActionsMenu() {
402 if (mDialog != null) {
403 mDialog.dismiss();
404 }
405 }
406
407 @Override
408 public void startPendingIntentDismissingKeyguard(
409 PendingIntent intent) {
410 mActivityStarter
411 .startPendingIntentDismissingKeyguard(intent);
412 }
413 })
Steve Elliott9b87a442019-03-05 10:24:16 -0500414 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400415 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400416 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700417 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400418
419 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800420 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400421
422 return dialog;
423 }
424
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800425 private boolean shouldDisplayLockdown() {
426 int userId = getCurrentUser().id;
427 // Lockdown is meaningless without a place to go.
428 if (!mKeyguardManager.isDeviceSecure(userId)) {
429 return false;
430 }
431
432 // Only show the lockdown button if the device isn't locked down (for whatever reason).
433 int state = mLockPatternUtils.getStrongAuthForUser(userId);
434 return (state == STRONG_AUTH_NOT_REQUIRED
435 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
436 }
437
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700438 @Override
439 public void onUiModeChanged() {
440 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400441 if (mDialog.isShowing()) {
442 mDialog.refreshDialog();
443 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700444 }
445
446 public void destroy() {
447 Dependency.get(ConfigurationController.class).removeCallback(this);
448 }
449
Jason Monk361915c2017-03-21 20:33:59 -0400450 private final class PowerAction extends SinglePressAction implements LongPressAction {
451 private PowerAction() {
452 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400453 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400454 }
455
456 @Override
457 public boolean onLongPress() {
458 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
459 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
460 mWindowManagerFuncs.reboot(true);
461 return true;
462 }
463 return false;
464 }
465
466 @Override
467 public boolean showDuringKeyguard() {
468 return true;
469 }
470
471 @Override
472 public boolean showBeforeProvisioning() {
473 return true;
474 }
475
476 @Override
477 public void onPress() {
478 // shutdown by making sure radio and power are handled accordingly.
479 mWindowManagerFuncs.shutdown();
480 }
481 }
482
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400483 private abstract class EmergencyAction extends SinglePressAction {
484 EmergencyAction(int iconResId, int messageResId) {
485 super(iconResId, messageResId);
486 }
487
488 @Override
489 public boolean shouldBeSeparated() {
490 return shouldUseSeparatedView();
491 }
492
493 @Override
494 public View create(
495 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
496 View v = super.create(context, convertView, parent, inflater);
497 int textColor;
498 if (shouldBeSeparated()) {
499 textColor = v.getResources().getColor(
500 com.android.systemui.R.color.global_actions_alert_text);
501 } else {
502 textColor = v.getResources().getColor(
503 com.android.systemui.R.color.global_actions_text);
504 }
505 TextView messageView = v.findViewById(R.id.message);
506 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400507 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400508 ImageView icon = (ImageView) v.findViewById(R.id.icon);
509 icon.getDrawable().setTint(textColor);
510 return v;
511 }
512
513 @Override
514 public boolean showDuringKeyguard() {
515 return true;
516 }
517
518 @Override
519 public boolean showBeforeProvisioning() {
520 return true;
521 }
522 }
523
524 private class EmergencyAffordanceAction extends EmergencyAction {
525 EmergencyAffordanceAction() {
526 super(R.drawable.emergency_icon,
527 R.string.global_action_emergency);
528 }
529
530 @Override
531 public void onPress() {
532 mEmergencyAffordanceManager.performEmergencyCall();
533 }
534 }
535
536 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800537 private EmergencyDialerAction() {
538 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800539 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800540 }
541
542 @Override
543 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800544 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800545 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800546 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
547 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
548 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800549 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
550 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800551 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
552 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800553 }
554
Jason Monk361915c2017-03-21 20:33:59 -0400555 private final class RestartAction extends SinglePressAction implements LongPressAction {
556 private RestartAction() {
557 super(R.drawable.ic_restart, R.string.global_action_restart);
558 }
559
560 @Override
561 public boolean onLongPress() {
562 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
563 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
564 mWindowManagerFuncs.reboot(true);
565 return true;
566 }
567 return false;
568 }
569
570 @Override
571 public boolean showDuringKeyguard() {
572 return true;
573 }
574
575 @Override
576 public boolean showBeforeProvisioning() {
577 return true;
578 }
579
580 @Override
581 public void onPress() {
582 mWindowManagerFuncs.reboot(false);
583 }
584 }
585
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400586 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500587 public ScreenshotAction() {
588 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
589 }
590
591 @Override
592 public void onPress() {
593 // Add a little delay before executing, to give the
594 // dialog a chance to go away before it takes a
595 // screenshot.
596 // TODO: instead, omit global action dialog layer
597 mHandler.postDelayed(new Runnable() {
598 @Override
599 public void run() {
600 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
601 MetricsLogger.action(mContext,
602 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
603 }
604 }, 500);
605 }
606
607 @Override
608 public boolean showDuringKeyguard() {
609 return true;
610 }
611
612 @Override
613 public boolean showBeforeProvisioning() {
614 return false;
615 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400616
617 @Override
618 public boolean onLongPress() {
619 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
620 mScreenRecordHelper.launchRecordPrompt();
621 } else {
622 onPress();
623 }
624 return true;
625 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500626 }
627
Jason Monk361915c2017-03-21 20:33:59 -0400628 private class BugReportAction extends SinglePressAction implements LongPressAction {
629
630 public BugReportAction() {
631 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
632 }
633
634 @Override
635 public void onPress() {
636 // don't actually trigger the bugreport if we are running stability
637 // tests via monkey
638 if (ActivityManager.isUserAMonkey()) {
639 return;
640 }
641 // Add a little delay before executing, to give the
642 // dialog a chance to go away before it takes a
643 // screenshot.
644 mHandler.postDelayed(new Runnable() {
645 @Override
646 public void run() {
647 try {
648 // Take an "interactive" bugreport.
649 MetricsLogger.action(mContext,
650 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
651 ActivityManager.getService().requestBugReport(
652 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
653 } catch (RemoteException e) {
654 }
655 }
656 }, 500);
657 }
658
659 @Override
660 public boolean onLongPress() {
661 // don't actually trigger the bugreport if we are running stability
662 // tests via monkey
663 if (ActivityManager.isUserAMonkey()) {
664 return false;
665 }
666 try {
667 // Take a "full" bugreport.
668 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
669 ActivityManager.getService().requestBugReport(
670 ActivityManager.BUGREPORT_OPTION_FULL);
671 } catch (RemoteException e) {
672 }
673 return false;
674 }
675
676 public boolean showDuringKeyguard() {
677 return true;
678 }
679
680 @Override
681 public boolean showBeforeProvisioning() {
682 return false;
683 }
Jason Monk361915c2017-03-21 20:33:59 -0400684 }
685
Alex Chau04458852017-11-27 18:21:23 +0000686 private final class LogoutAction extends SinglePressAction {
687 private LogoutAction() {
688 super(R.drawable.ic_logout, R.string.global_action_logout);
689 }
690
691 @Override
692 public boolean showDuringKeyguard() {
693 return true;
694 }
695
696 @Override
697 public boolean showBeforeProvisioning() {
698 return false;
699 }
700
701 @Override
702 public void onPress() {
703 // Add a little delay before executing, to give the dialog a chance to go away before
704 // switching user
705 mHandler.postDelayed(() -> {
706 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000707 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000708 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000709 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000710 } catch (RemoteException re) {
711 Log.e(TAG, "Couldn't logout user " + re);
712 }
713 }, 500);
714 }
715 }
716
Jason Monk361915c2017-03-21 20:33:59 -0400717 private Action getSettingsAction() {
718 return new SinglePressAction(R.drawable.ic_settings,
719 R.string.global_action_settings) {
720
721 @Override
722 public void onPress() {
723 Intent intent = new Intent(Settings.ACTION_SETTINGS);
724 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
725 mContext.startActivity(intent);
726 }
727
728 @Override
729 public boolean showDuringKeyguard() {
730 return true;
731 }
732
733 @Override
734 public boolean showBeforeProvisioning() {
735 return true;
736 }
737 };
738 }
739
Jason Monk361915c2017-03-21 20:33:59 -0400740 private Action getAssistAction() {
741 return new SinglePressAction(R.drawable.ic_action_assist_focused,
742 R.string.global_action_assist) {
743 @Override
744 public void onPress() {
745 Intent intent = new Intent(Intent.ACTION_ASSIST);
746 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
747 mContext.startActivity(intent);
748 }
749
750 @Override
751 public boolean showDuringKeyguard() {
752 return true;
753 }
754
755 @Override
756 public boolean showBeforeProvisioning() {
757 return true;
758 }
759 };
760 }
761
762 private Action getVoiceAssistAction() {
763 return new SinglePressAction(R.drawable.ic_voice_search,
764 R.string.global_action_voice_assist) {
765 @Override
766 public void onPress() {
767 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
768 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
769 mContext.startActivity(intent);
770 }
771
772 @Override
773 public boolean showDuringKeyguard() {
774 return true;
775 }
776
777 @Override
778 public boolean showBeforeProvisioning() {
779 return true;
780 }
781 };
782 }
783
784 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400785 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400786 R.string.global_action_lockdown) {
787
788 @Override
789 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700790 new LockPatternUtils(mContext)
791 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
792 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400793 try {
794 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100795 // Lock profiles (if any) on the background thread.
796 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
797 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400798 } catch (RemoteException e) {
799 Log.e(TAG, "Error while trying to lock device.", e);
800 }
801 }
802
803 @Override
804 public boolean showDuringKeyguard() {
805 return true;
806 }
807
808 @Override
809 public boolean showBeforeProvisioning() {
810 return false;
811 }
812 };
813 }
814
Pavel Grafov059021b2018-05-02 13:44:46 +0100815 private void lockProfiles() {
816 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
817 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
818 final int currentUserId = getCurrentUser().id;
819 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
820 for (final int id : profileIds) {
821 if (id != currentUserId) {
822 tm.setDeviceLockedForUser(id, true);
823 }
824 }
825 }
826
Jason Monk361915c2017-03-21 20:33:59 -0400827 private UserInfo getCurrentUser() {
828 try {
829 return ActivityManager.getService().getCurrentUser();
830 } catch (RemoteException re) {
831 return null;
832 }
833 }
834
835 private boolean isCurrentUserOwner() {
836 UserInfo currentUser = getCurrentUser();
837 return currentUser == null || currentUser.isPrimary();
838 }
839
840 private void addUsersToMenu(ArrayList<Action> items) {
841 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
842 if (um.isUserSwitcherEnabled()) {
843 List<UserInfo> users = um.getUsers();
844 UserInfo currentUser = getCurrentUser();
845 for (final UserInfo user : users) {
846 if (user.supportsSwitchToByUser()) {
847 boolean isCurrentUser = currentUser == null
848 ? user.id == 0 : (currentUser.id == user.id);
849 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
850 : null;
851 SinglePressAction switchToUser = new SinglePressAction(
852 R.drawable.ic_menu_cc, icon,
853 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400854 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400855 public void onPress() {
856 try {
857 ActivityManager.getService().switchUser(user.id);
858 } catch (RemoteException re) {
859 Log.e(TAG, "Couldn't switch user " + re);
860 }
861 }
862
863 public boolean showDuringKeyguard() {
864 return true;
865 }
866
867 public boolean showBeforeProvisioning() {
868 return false;
869 }
870 };
871 items.add(switchToUser);
872 }
873 }
874 }
875 }
876
877 private void prepareDialog() {
878 refreshSilentMode();
879 mAirplaneModeOn.updateState(mAirplaneState);
880 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400881 if (mShowSilentToggle) {
882 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
883 mContext.registerReceiver(mRingerModeReceiver, filter);
884 }
885 }
886
887 private void refreshSilentMode() {
888 if (!mHasVibrator) {
889 final boolean silentModeOn =
890 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400891 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400892 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
893 }
894 }
895
896 /** {@inheritDoc} */
897 public void onDismiss(DialogInterface dialog) {
898 mWindowManagerFuncs.onGlobalActionsHidden();
899 if (mShowSilentToggle) {
900 try {
901 mContext.unregisterReceiver(mRingerModeReceiver);
902 } catch (IllegalArgumentException ie) {
903 // ignore this
904 Log.w(TAG, ie);
905 }
906 }
907 }
908
909 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800910 public void onShow(DialogInterface dialog) {
911 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
912 }
913
Jason Monk361915c2017-03-21 20:33:59 -0400914 /**
915 * The adapter used for the list within the global actions dialog, taking
916 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400917 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
918 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400919 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
920 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500921 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400922 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400923 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400924 for (int i = 0; i < mItems.size(); i++) {
925 final Action action = mItems.get(i);
926
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400927 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
928 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400929 }
Jason Monk361915c2017-03-21 20:33:59 -0400930 }
931 return count;
932 }
933
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400934 private boolean shouldBeShown(Action action) {
935 if (mKeyguardShowing && !action.showDuringKeyguard()) {
936 return false;
937 }
938 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
939 return false;
940 }
941 return true;
942 }
943
944 @Override
945 public int countSeparatedItems() {
946 return countItems(true);
947 }
948
949 @Override
950 public int countListItems() {
951 return countItems(false);
952 }
953
954 @Override
955 public int getCount() {
956 return countSeparatedItems() + countListItems();
957 }
958
Jason Monk361915c2017-03-21 20:33:59 -0400959 @Override
960 public boolean isEnabled(int position) {
961 return getItem(position).isEnabled();
962 }
963
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500964 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400965 public boolean areAllItemsEnabled() {
966 return false;
967 }
968
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500969 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400970 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400971 int filteredPos = 0;
972 for (int i = 0; i < mItems.size(); i++) {
973 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400974 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400975 continue;
976 }
977 if (filteredPos == position) {
978 return action;
979 }
980 filteredPos++;
981 }
982
983 throw new IllegalArgumentException("position " + position
984 + " out of range of showable actions"
985 + ", filtered count=" + getCount()
986 + ", keyguardshowing=" + mKeyguardShowing
987 + ", provisioned=" + mDeviceProvisioned);
988 }
989
990
991 public long getItemId(int position) {
992 return position;
993 }
994
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500995 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400996 public View getView(int position, View convertView, ViewGroup parent) {
997 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400998 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400999 view.setOnClickListener(v -> onClickItem(position));
1000 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001001 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001002 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001003
1004 @Override
1005 public boolean onLongClickItem(int position) {
1006 final Action action = mAdapter.getItem(position);
1007 if (action instanceof LongPressAction) {
1008 mDialog.dismiss();
1009 return ((LongPressAction) action).onLongPress();
1010 }
1011 return false;
1012 }
1013
1014 @Override
1015 public void onClickItem(int position) {
1016 Action item = mAdapter.getItem(position);
1017 if (!(item instanceof SilentModeTriStateAction)) {
1018 mDialog.dismiss();
1019 }
1020 item.onPress();
1021 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001022
1023 @Override
1024 public boolean shouldBeSeparated(int position) {
1025 return getItem(position).shouldBeSeparated();
1026 }
Jason Monk361915c2017-03-21 20:33:59 -04001027 }
1028
1029 // note: the scheme below made more sense when we were planning on having
1030 // 8 different things in the global actions dialog. seems overkill with
1031 // only 3 items now, but may as well keep this flexible approach so it will
1032 // be easy should someone decide at the last minute to include something
1033 // else, such as 'enable wifi', or 'enable bluetooth'
1034
1035 /**
1036 * What each item in the global actions dialog must be able to support.
1037 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001038 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001039 /**
1040 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001041 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001042 */
1043 CharSequence getLabelForAccessibility(Context context);
1044
1045 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1046
1047 void onPress();
1048
1049 /**
1050 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001051 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001052 */
1053 boolean showDuringKeyguard();
1054
1055 /**
1056 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001057 * device is provisioned.onlongpress
1058 *
Jason Monk361915c2017-03-21 20:33:59 -04001059 */
1060 boolean showBeforeProvisioning();
1061
1062 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001063
1064 default boolean shouldBeSeparated() {
1065 return false;
1066 }
Jason Monk361915c2017-03-21 20:33:59 -04001067 }
1068
1069 /**
1070 * An action that also supports long press.
1071 */
1072 private interface LongPressAction extends Action {
1073 boolean onLongPress();
1074 }
1075
1076 /**
1077 * A single press action maintains no state, just responds to a press
1078 * and takes an action.
1079 */
1080 private static abstract class SinglePressAction implements Action {
1081 private final int mIconResId;
1082 private final Drawable mIcon;
1083 private final int mMessageResId;
1084 private final CharSequence mMessage;
1085
1086 protected SinglePressAction(int iconResId, int messageResId) {
1087 mIconResId = iconResId;
1088 mMessageResId = messageResId;
1089 mMessage = null;
1090 mIcon = null;
1091 }
1092
1093 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1094 mIconResId = iconResId;
1095 mMessageResId = 0;
1096 mMessage = message;
1097 mIcon = icon;
1098 }
1099
1100 public boolean isEnabled() {
1101 return true;
1102 }
1103
1104 public String getStatus() {
1105 return null;
1106 }
1107
1108 abstract public void onPress();
1109
1110 public CharSequence getLabelForAccessibility(Context context) {
1111 if (mMessage != null) {
1112 return mMessage;
1113 } else {
1114 return context.getString(mMessageResId);
1115 }
1116 }
1117
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001118 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001119 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001120 }
1121
Jason Monk361915c2017-03-21 20:33:59 -04001122 public View create(
1123 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001124 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001125 false);
Jason Monk361915c2017-03-21 20:33:59 -04001126
1127 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1128 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001129 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001130
1131 TextView statusView = (TextView) v.findViewById(R.id.status);
1132 final String status = getStatus();
1133 if (!TextUtils.isEmpty(status)) {
1134 statusView.setText(status);
1135 } else {
1136 statusView.setVisibility(View.GONE);
1137 }
1138 if (mIcon != null) {
1139 icon.setImageDrawable(mIcon);
1140 icon.setScaleType(ScaleType.CENTER_CROP);
1141 } else if (mIconResId != 0) {
1142 icon.setImageDrawable(context.getDrawable(mIconResId));
1143 }
1144 if (mMessage != null) {
1145 messageView.setText(mMessage);
1146 } else {
1147 messageView.setText(mMessageResId);
1148 }
1149
1150 return v;
1151 }
1152 }
1153
1154 /**
1155 * A toggle action knows whether it is on or off, and displays an icon
1156 * and status message accordingly.
1157 */
1158 private static abstract class ToggleAction implements Action {
1159
1160 enum State {
1161 Off(false),
1162 TurningOn(true),
1163 TurningOff(true),
1164 On(false);
1165
1166 private final boolean inTransition;
1167
1168 State(boolean intermediate) {
1169 inTransition = intermediate;
1170 }
1171
1172 public boolean inTransition() {
1173 return inTransition;
1174 }
1175 }
1176
1177 protected State mState = State.Off;
1178
1179 // prefs
1180 protected int mEnabledIconResId;
1181 protected int mDisabledIconResid;
1182 protected int mMessageResId;
1183 protected int mEnabledStatusMessageResId;
1184 protected int mDisabledStatusMessageResId;
1185
1186 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001187 * @param enabledIconResId The icon for when this action is on.
1188 * @param disabledIconResid The icon for when this action is off.
1189 * @param message The general information message, e.g 'Silent Mode'
1190 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001191 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1192 */
1193 public ToggleAction(int enabledIconResId,
1194 int disabledIconResid,
1195 int message,
1196 int enabledStatusMessageResId,
1197 int disabledStatusMessageResId) {
1198 mEnabledIconResId = enabledIconResId;
1199 mDisabledIconResid = disabledIconResid;
1200 mMessageResId = message;
1201 mEnabledStatusMessageResId = enabledStatusMessageResId;
1202 mDisabledStatusMessageResId = disabledStatusMessageResId;
1203 }
1204
1205 /**
1206 * Override to make changes to resource IDs just before creating the
1207 * View.
1208 */
1209 void willCreate() {
1210
1211 }
1212
1213 @Override
1214 public CharSequence getLabelForAccessibility(Context context) {
1215 return context.getString(mMessageResId);
1216 }
1217
1218 public View create(Context context, View convertView, ViewGroup parent,
1219 LayoutInflater inflater) {
1220 willCreate();
1221
1222 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001223 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001224
1225 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1226 TextView messageView = (TextView) v.findViewById(R.id.message);
1227 TextView statusView = (TextView) v.findViewById(R.id.status);
1228 final boolean enabled = isEnabled();
1229
1230 if (messageView != null) {
1231 messageView.setText(mMessageResId);
1232 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001233 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001234 }
1235
1236 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1237 if (icon != null) {
1238 icon.setImageDrawable(context.getDrawable(
1239 (on ? mEnabledIconResId : mDisabledIconResid)));
1240 icon.setEnabled(enabled);
1241 }
1242
1243 if (statusView != null) {
1244 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1245 statusView.setVisibility(View.VISIBLE);
1246 statusView.setEnabled(enabled);
1247 }
1248 v.setEnabled(enabled);
1249
1250 return v;
1251 }
1252
1253 public final void onPress() {
1254 if (mState.inTransition()) {
1255 Log.w(TAG, "shouldn't be able to toggle when in transition");
1256 return;
1257 }
1258
1259 final boolean nowOn = !(mState == State.On);
1260 onToggle(nowOn);
1261 changeStateFromPress(nowOn);
1262 }
1263
1264 public boolean isEnabled() {
1265 return !mState.inTransition();
1266 }
1267
1268 /**
1269 * Implementations may override this if their state can be in on of the intermediate
1270 * states until some notification is received (e.g airplane mode is 'turning off' until
1271 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001272 *
Jason Monk361915c2017-03-21 20:33:59 -04001273 * @param buttonOn Whether the button was turned on or off
1274 */
1275 protected void changeStateFromPress(boolean buttonOn) {
1276 mState = buttonOn ? State.On : State.Off;
1277 }
1278
1279 abstract void onToggle(boolean on);
1280
1281 public void updateState(State state) {
1282 mState = state;
1283 }
1284 }
1285
1286 private class SilentModeToggleAction extends ToggleAction {
1287 public SilentModeToggleAction() {
1288 super(R.drawable.ic_audio_vol_mute,
1289 R.drawable.ic_audio_vol,
1290 R.string.global_action_toggle_silent_mode,
1291 R.string.global_action_silent_mode_on_status,
1292 R.string.global_action_silent_mode_off_status);
1293 }
1294
1295 void onToggle(boolean on) {
1296 if (on) {
1297 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1298 } else {
1299 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1300 }
1301 }
1302
1303 public boolean showDuringKeyguard() {
1304 return true;
1305 }
1306
1307 public boolean showBeforeProvisioning() {
1308 return false;
1309 }
1310 }
1311
1312 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1313
Jason Monk16fbd9d2017-04-27 14:28:49 -04001314 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001315
1316 private final AudioManager mAudioManager;
1317 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001318
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001319 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001320 mAudioManager = audioManager;
1321 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001322 }
1323
1324 private int ringerModeToIndex(int ringerMode) {
1325 // They just happen to coincide
1326 return ringerMode;
1327 }
1328
1329 private int indexToRingerMode(int index) {
1330 // They just happen to coincide
1331 return index;
1332 }
1333
1334 @Override
1335 public CharSequence getLabelForAccessibility(Context context) {
1336 return null;
1337 }
1338
1339 public View create(Context context, View convertView, ViewGroup parent,
1340 LayoutInflater inflater) {
1341 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1342
1343 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1344 for (int i = 0; i < 3; i++) {
1345 View itemView = v.findViewById(ITEM_IDS[i]);
1346 itemView.setSelected(selectedIndex == i);
1347 // Set up click handler
1348 itemView.setTag(i);
1349 itemView.setOnClickListener(this);
1350 }
1351 return v;
1352 }
1353
1354 public void onPress() {
1355 }
1356
1357 public boolean showDuringKeyguard() {
1358 return true;
1359 }
1360
1361 public boolean showBeforeProvisioning() {
1362 return false;
1363 }
1364
1365 public boolean isEnabled() {
1366 return true;
1367 }
1368
1369 void willCreate() {
1370 }
1371
1372 public void onClick(View v) {
1373 if (!(v.getTag() instanceof Integer)) return;
1374
1375 int index = (Integer) v.getTag();
1376 mAudioManager.setRingerMode(indexToRingerMode(index));
1377 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1378 }
1379 }
1380
1381 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1382 public void onReceive(Context context, Intent intent) {
1383 String action = intent.getAction();
1384 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1385 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1386 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1387 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001388 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001389 }
1390 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1391 // Airplane mode can be changed after ECM exits if airplane toggle button
1392 // is pressed during ECM mode
1393 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1394 mIsWaitingForEcmExit) {
1395 mIsWaitingForEcmExit = false;
1396 changeAirplaneModeSystemSetting(true);
1397 }
1398 }
1399 }
1400 };
1401
1402 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1403 @Override
1404 public void onServiceStateChanged(ServiceState serviceState) {
1405 if (!mHasTelephony) return;
1406 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1407 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1408 mAirplaneModeOn.updateState(mAirplaneState);
1409 mAdapter.notifyDataSetChanged();
1410 }
1411 };
1412
1413 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1414 @Override
1415 public void onReceive(Context context, Intent intent) {
1416 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1417 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1418 }
1419 }
1420 };
1421
1422 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1423 @Override
1424 public void onChange(boolean selfChange) {
1425 onAirplaneModeChanged();
1426 }
1427 };
1428
1429 private static final int MESSAGE_DISMISS = 0;
1430 private static final int MESSAGE_REFRESH = 1;
1431 private static final int MESSAGE_SHOW = 2;
1432 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1433
1434 private Handler mHandler = new Handler() {
1435 public void handleMessage(Message msg) {
1436 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001437 case MESSAGE_DISMISS:
1438 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001439 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1440 mDialog.dismissImmediately();
1441 } else {
1442 mDialog.dismiss();
1443 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001444 mDialog = null;
1445 }
1446 break;
1447 case MESSAGE_REFRESH:
1448 refreshSilentMode();
1449 mAdapter.notifyDataSetChanged();
1450 break;
1451 case MESSAGE_SHOW:
1452 handleShow();
1453 break;
Jason Monk361915c2017-03-21 20:33:59 -04001454 }
1455 }
1456 };
1457
1458 private void onAirplaneModeChanged() {
1459 // Let the service state callbacks handle the state.
1460 if (mHasTelephony) return;
1461
1462 boolean airplaneModeOn = Settings.Global.getInt(
1463 mContext.getContentResolver(),
1464 Settings.Global.AIRPLANE_MODE_ON,
1465 0) == 1;
1466 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1467 mAirplaneModeOn.updateState(mAirplaneState);
1468 }
1469
1470 /**
1471 * Change the airplane mode system setting
1472 */
1473 private void changeAirplaneModeSystemSetting(boolean on) {
1474 Settings.Global.putInt(
1475 mContext.getContentResolver(),
1476 Settings.Global.AIRPLANE_MODE_ON,
1477 on ? 1 : 0);
1478 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1479 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1480 intent.putExtra("state", on);
1481 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1482 if (!mHasTelephony) {
1483 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1484 }
1485 }
1486
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001487 private static final class ActionsDialog extends Dialog implements DialogInterface,
1488 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001489
Jason Monk16fbd9d2017-04-27 14:28:49 -04001490 private final Context mContext;
1491 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001492 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001493 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001494 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001495 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001496 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001497 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001498 private float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001499
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001500 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001501 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001502 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001503 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001504 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001505 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001506
1507 // Window initialization
1508 Window window = getWindow();
1509 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001510 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1511 window.getDecorView();
1512 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1513 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1514 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1515 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1516 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001517 window.addFlags(
1518 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001519 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001520 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001521 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1522 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1523 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001524 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001525 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001526
1527 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001528 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001529 }
1530
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001531 private boolean shouldUsePanel() {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001532 if (!isPanelEnabled(mContext) || mPanelController == null) {
1533 return false;
Steve Elliott9b87a442019-03-05 10:24:16 -05001534 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001535 if (mPanelController.getPanelContent() == null) {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001536 return false;
1537 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001538 return true;
1539 }
1540
1541 private void initializePanel() {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001542 FrameLayout panelContainer = new FrameLayout(mContext);
1543 FrameLayout.LayoutParams panelParams =
1544 new FrameLayout.LayoutParams(
1545 FrameLayout.LayoutParams.MATCH_PARENT,
1546 FrameLayout.LayoutParams.WRAP_CONTENT);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001547 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001548 addContentView(
1549 panelContainer,
1550 new ViewGroup.LayoutParams(
1551 ViewGroup.LayoutParams.MATCH_PARENT,
1552 ViewGroup.LayoutParams.MATCH_PARENT));
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001553 }
1554
1555 private void initializeLayout() {
1556 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001557 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001558 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);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001570
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001571 if (!shouldUsePanel()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001572 if (mBackgroundDrawable == null) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001573 mBackgroundDrawable = new ScrimDrawable();
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001574 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001575 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1576 } else {
1577 mBackgroundDrawable = mContext.getDrawable(
1578 com.android.systemui.R.drawable.global_action_panel_scrim);
1579 mScrimAlpha = 1f;
1580 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001581 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001582 }
1583
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001584 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001585 if (isForceGridEnabled(context) || shouldUsePanel()) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001586 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1587 return com.android.systemui.R.layout.global_actions_grid_seascape;
1588 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001589 return com.android.systemui.R.layout.global_actions_grid;
1590 }
1591 return com.android.systemui.R.layout.global_actions_wrapped;
1592 }
1593
Jason Monk361915c2017-03-21 20:33:59 -04001594 @Override
1595 protected void onStart() {
1596 super.setCanceledOnTouchOutside(true);
1597 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001598 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001599
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001600 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001601 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001602 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001603 updateColors(colors, false /* animate */);
1604 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001605 }
1606
1607 /**
1608 * Updates background and system bars according to current GradientColors.
1609 * @param colors Colors and hints to use.
1610 * @param animate Interpolates gradient if true, just sets otherwise.
1611 */
1612 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001613 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001614 return;
1615 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001616 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001617 View decorView = getWindow().getDecorView();
1618 if (colors.supportsDarkText()) {
1619 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001620 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001621 } else {
1622 decorView.setSystemUiVisibility(0);
1623 }
Jason Monk361915c2017-03-21 20:33:59 -04001624 }
1625
1626 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001627 protected void onStop() {
1628 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001629 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001630 }
1631
1632 @Override
1633 public void show() {
1634 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001635 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001636 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001637 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1638 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001639 mGlobalActionsLayout.setAlpha(0);
1640 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001641 .alpha(1)
1642 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001643 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001644 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001645 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001646 .setUpdateListener(animation -> {
1647 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001648 * mScrimAlpha * 255);
1649 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001650 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001651 .start();
1652 }
1653
1654 @Override
1655 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001656 if (!mShowing) {
1657 return;
1658 }
1659 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001660 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001661 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001662 mGlobalActionsLayout.setAlpha(1);
1663 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001664 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001665 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1666 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001667 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001668 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001669 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001670 .setUpdateListener(animation -> {
1671 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001672 * mScrimAlpha * 255);
1673 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001674 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001675 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001676 if (mPanelController != null) {
1677 mPanelController.onDismissed();
1678 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001679 }
1680
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001681 void dismissImmediately() {
1682 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001683 mShowing = false;
Steve Elliott2d206d32019-03-14 16:30:33 -04001684 if (mPanelController != null) {
1685 mPanelController.onDismissed();
1686 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001687 }
1688
Jason Monk361915c2017-03-21 20:33:59 -04001689 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001690 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001691 if (mKeyguardShowing) {
1692 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001693 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1694 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001695 }
1696 } else {
1697 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001698 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1699 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001700 }
1701 }
1702 }
1703
1704 public void setKeyguardShowing(boolean keyguardShowing) {
1705 mKeyguardShowing = keyguardShowing;
1706 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001707
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001708 public void refreshDialog() {
1709 initializeLayout();
1710 mGlobalActionsLayout.updateList();
1711 }
1712
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001713 public void onRotate(int from, int to) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001714 if (mShowing && (shouldUsePanel() || isForceGridEnabled(mContext))) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001715 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001716 }
1717 }
Jason Monk361915c2017-03-21 20:33:59 -04001718 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001719
1720 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001721 * Determines whether or not the Global Actions menu should be forced to
1722 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001723 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001724 private static boolean isForceGridEnabled(Context context) {
1725 return FeatureFlagUtils.isEnabled(context,
1726 FeatureFlagUtils.FORCE_GLOBAL_ACTIONS_GRID_ENABLED);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001727 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001728
1729 /**
1730 * Determines whether or not the Global Actions Panel should appear when the power button
1731 * is held.
1732 */
1733 private static boolean isPanelEnabled(Context context) {
1734 return FeatureFlagUtils.isEnabled(
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001735 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1736 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001737
1738 /**
1739 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1740 */
1741 private static boolean shouldUseSeparatedView() {
1742 return true;
1743 }
Jason Monk361915c2017-03-21 20:33:59 -04001744}