blob: 66926527eb0247db359537632f36174eb11396d2 [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;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070038import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040039import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
42import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
44import android.os.Message;
45import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.SystemProperties;
48import android.os.UserHandle;
49import android.os.UserManager;
50import android.os.Vibrator;
51import android.provider.Settings;
52import android.service.dreams.DreamService;
53import android.service.dreams.IDreamManager;
54import android.telephony.PhoneStateListener;
55import android.telephony.ServiceState;
56import android.telephony.TelephonyManager;
57import android.text.TextUtils;
58import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080059import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040060import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070061import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040062import android.view.LayoutInflater;
63import android.view.View;
64import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070065import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040066import android.view.WindowManager;
67import android.view.WindowManagerGlobal;
68import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050069import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.widget.ImageView;
71import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040072import android.widget.TextView;
73
Charles He9851a8d2017-10-10 17:31:30 +010074import com.android.internal.R;
75import com.android.internal.colorextraction.ColorExtractor;
76import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070077import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010078import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
80import com.android.internal.telephony.TelephonyIntents;
81import com.android.internal.telephony.TelephonyProperties;
82import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040083import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050084import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.internal.widget.LockPatternUtils;
86import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010087import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050088import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050089import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040091import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010092import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050093import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050094import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070095import com.android.systemui.statusbar.policy.ConfigurationController;
Steve Elliott9b87a442019-03-05 10:24:16 -050096import com.android.systemui.statusbar.policy.ExtensionController;
97import com.android.systemui.statusbar.policy.ExtensionController.Extension;
Shaotang Li5c422632018-07-04 14:18:40 +080098import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050099import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500100import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700101
Jason Monk361915c2017-03-21 20:33:59 -0400102import java.util.ArrayList;
103import java.util.List;
104
105/**
106 * Helper to show the global actions dialog. Each item is an {@link Action} that
107 * may show depending on whether the keyguard is showing, and whether the device
108 * is provisioned.
109 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500110public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
111 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400112
113 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
114 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700115 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400116
117 private static final String TAG = "GlobalActionsDialog";
118
119 private static final boolean SHOW_SILENT_TOGGLE = true;
120
121 /* Valid settings for global actions keys.
122 * see config.xml config_globalActionList */
123 private static final String GLOBAL_ACTION_KEY_POWER = "power";
124 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
125 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
126 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
127 private static final String GLOBAL_ACTION_KEY_USERS = "users";
128 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
129 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
130 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
131 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
132 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000133 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800134 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500135 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400136
137 private final Context mContext;
138 private final GlobalActionsManager mWindowManagerFuncs;
139 private final AudioManager mAudioManager;
140 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000141 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800142 private final LockPatternUtils mLockPatternUtils;
143 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400144
145 private ArrayList<Action> mItems;
146 private ActionsDialog mDialog;
147
148 private Action mSilentModeAction;
149 private ToggleAction mAirplaneModeOn;
150
151 private MyAdapter mAdapter;
152
153 private boolean mKeyguardShowing = false;
154 private boolean mDeviceProvisioned = false;
155 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
156 private boolean mIsWaitingForEcmExit = false;
157 private boolean mHasTelephony;
158 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000159 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800160 private boolean mHasLockdownButton;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500161 private boolean mUseSeparatedList;
Jason Monk361915c2017-03-21 20:33:59 -0400162 private final boolean mShowSilentToggle;
163 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500164 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400165 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400166
Steve Elliott9b87a442019-03-05 10:24:16 -0500167 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
Steve Elliott4c868852019-03-14 16:25:41 -0400168 private ActivityStarter mActivityStarter;
Steve Elliott9b87a442019-03-05 10:24:16 -0500169
Jason Monk361915c2017-03-21 20:33:59 -0400170 /**
171 * @param context everything needs a context :(
172 */
173 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700174 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400175 mWindowManagerFuncs = windowManagerFuncs;
176 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
177 mDreamManager = IDreamManager.Stub.asInterface(
178 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000179 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
180 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800181 mLockPatternUtils = new LockPatternUtils(mContext);
182 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400183
184 // receive broadcasts
185 IntentFilter filter = new IntentFilter();
186 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
187 filter.addAction(Intent.ACTION_SCREEN_OFF);
188 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
189 context.registerReceiver(mBroadcastReceiver, filter);
190
191 ConnectivityManager cm = (ConnectivityManager)
192 context.getSystemService(Context.CONNECTIVITY_SERVICE);
193 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
194
195 // get notified of phone state changes
196 TelephonyManager telephonyManager =
197 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
198 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
199 mContext.getContentResolver().registerContentObserver(
200 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
201 mAirplaneModeObserver);
202 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
203 mHasVibrator = vibrator != null && vibrator.hasVibrator();
204
205 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
206 R.bool.config_useFixedVolume);
207
208 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500209 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400210 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700211
212 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500213
214 mPanelExtension = Dependency.get(ExtensionController.class)
215 .newExtension(GlobalActionsPanelPlugin.class)
216 .withPlugin(GlobalActionsPanelPlugin.class)
217 .build();
Steve Elliott4c868852019-03-14 16:25:41 -0400218 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400219 }
220
221 /**
222 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400223 *
Jason Monk361915c2017-03-21 20:33:59 -0400224 * @param keyguardShowing True if keyguard is showing
225 */
226 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
227 mKeyguardShowing = keyguardShowing;
228 mDeviceProvisioned = isDeviceProvisioned;
229 if (mDialog != null) {
230 mDialog.dismiss();
231 mDialog = null;
232 // Show delayed, so that the dismiss of the previous dialog completes
233 mHandler.sendEmptyMessage(MESSAGE_SHOW);
234 } else {
235 handleShow();
236 }
237 }
238
Charles He9851a8d2017-10-10 17:31:30 +0100239 /**
240 * Dismiss the global actions dialog, if it's currently shown
241 */
242 public void dismissDialog() {
243 mHandler.removeMessages(MESSAGE_DISMISS);
244 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
245 }
246
Jason Monk361915c2017-03-21 20:33:59 -0400247 private void awakenIfNecessary() {
248 if (mDreamManager != null) {
249 try {
250 if (mDreamManager.isDreaming()) {
251 mDreamManager.awaken();
252 }
253 } catch (RemoteException e) {
254 // we tried
255 }
256 }
257 }
258
259 private void handleShow() {
260 awakenIfNecessary();
261 mDialog = createDialog();
262 prepareDialog();
263
264 // If we only have 1 item and it's a simple press action, just do this action.
265 if (mAdapter.getCount() == 1
266 && mAdapter.getItem(0) instanceof SinglePressAction
267 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
268 ((SinglePressAction) mAdapter.getItem(0)).onPress();
269 } else {
270 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
271 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100272 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400273 mDialog.getWindow().setAttributes(attrs);
274 mDialog.show();
275 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400276 }
277 }
278
279 /**
280 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400281 *
Jason Monk361915c2017-03-21 20:33:59 -0400282 * @return A new dialog.
283 */
284 private ActionsDialog createDialog() {
285 // Simple toggle style if there's no vibrator, otherwise use a tri-state
286 if (!mHasVibrator) {
287 mSilentModeAction = new SilentModeToggleAction();
288 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700289 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400290 }
291 mAirplaneModeOn = new ToggleAction(
292 R.drawable.ic_lock_airplane_mode,
293 R.drawable.ic_lock_airplane_mode_off,
294 R.string.global_actions_toggle_airplane_mode,
295 R.string.global_actions_airplane_mode_on_status,
296 R.string.global_actions_airplane_mode_off_status) {
297
298 void onToggle(boolean on) {
299 if (mHasTelephony && Boolean.parseBoolean(
300 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
301 mIsWaitingForEcmExit = true;
302 // Launch ECM exit dialog
303 Intent ecmDialogIntent =
304 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
305 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
306 mContext.startActivity(ecmDialogIntent);
307 } else {
308 changeAirplaneModeSystemSetting(on);
309 }
310 }
311
312 @Override
313 protected void changeStateFromPress(boolean buttonOn) {
314 if (!mHasTelephony) return;
315
316 // In ECM mode airplane state cannot be changed
317 if (!(Boolean.parseBoolean(
318 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
319 mState = buttonOn ? State.TurningOn : State.TurningOff;
320 mAirplaneState = mState;
321 }
322 }
323
324 public boolean showDuringKeyguard() {
325 return true;
326 }
327
328 public boolean showBeforeProvisioning() {
329 return false;
330 }
331 };
332 onAirplaneModeChanged();
333
334 mItems = new ArrayList<Action>();
335 String[] defaultActions = mContext.getResources().getStringArray(
336 R.array.config_globalActionsList);
337
338 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000339 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800340 mHasLockdownButton = false;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500341 mUseSeparatedList = true;
Jason Monk361915c2017-03-21 20:33:59 -0400342 for (int i = 0; i < defaultActions.length; i++) {
343 String actionKey = defaultActions[i];
344 if (addedKeys.contains(actionKey)) {
345 // If we already have added this, don't add it again.
346 continue;
347 }
348 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
349 mItems.add(new PowerAction());
350 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
351 mItems.add(mAirplaneModeOn);
352 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
353 if (Settings.Global.getInt(mContext.getContentResolver(),
354 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
355 mItems.add(new BugReportAction());
356 }
357 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
358 if (mShowSilentToggle) {
359 mItems.add(mSilentModeAction);
360 }
361 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
362 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
363 addUsersToMenu(mItems);
364 }
365 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
366 mItems.add(getSettingsAction());
367 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700368 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
369 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800370 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700371 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800372 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700373 }
Jason Monk361915c2017-03-21 20:33:59 -0400374 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
375 mItems.add(getVoiceAssistAction());
376 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
377 mItems.add(getAssistAction());
378 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
379 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500380 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
381 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000382 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000383 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000384 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
385 mItems.add(new LogoutAction());
386 mHasLogoutButton = true;
387 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800388 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500389 if (mUseSeparatedList
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800390 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
391 mItems.add(new EmergencyDialerAction());
392 }
Jason Monk361915c2017-03-21 20:33:59 -0400393 } else {
394 Log.e(TAG, "Invalid global action key " + actionKey);
395 }
396 // Add here so we don't add more than one.
397 addedKeys.add(actionKey);
398 }
399
400 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
401 mItems.add(getEmergencyAction());
402 }
403
404 mAdapter = new MyAdapter();
405
Steve Elliott9b87a442019-03-05 10:24:16 -0500406 GlobalActionsPanelPlugin.PanelViewController panelViewController =
407 mPanelExtension.get() != null
Steve Elliott4c868852019-03-14 16:25:41 -0400408 ? mPanelExtension.get().onPanelShown(
409 new GlobalActionsPanelPlugin.Callbacks() {
410 @Override
411 public void dismissGlobalActionsMenu() {
412 if (mDialog != null) {
413 mDialog.dismiss();
414 }
415 }
416
417 @Override
418 public void startPendingIntentDismissingKeyguard(
419 PendingIntent intent) {
420 mActivityStarter
421 .startPendingIntentDismissingKeyguard(intent);
422 }
423 })
Steve Elliott9b87a442019-03-05 10:24:16 -0500424 : null;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500425 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, mUseSeparatedList,
426 panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400427 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700428 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400429
430 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800431 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400432
433 return dialog;
434 }
435
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800436 private boolean shouldDisplayLockdown() {
437 int userId = getCurrentUser().id;
438 // Lockdown is meaningless without a place to go.
439 if (!mKeyguardManager.isDeviceSecure(userId)) {
440 return false;
441 }
442
443 // Only show the lockdown button if the device isn't locked down (for whatever reason).
444 int state = mLockPatternUtils.getStrongAuthForUser(userId);
445 return (state == STRONG_AUTH_NOT_REQUIRED
446 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
447 }
448
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700449 @Override
450 public void onUiModeChanged() {
451 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
452 }
453
454 public void destroy() {
455 Dependency.get(ConfigurationController.class).removeCallback(this);
456 }
457
Jason Monk361915c2017-03-21 20:33:59 -0400458 private final class PowerAction extends SinglePressAction implements LongPressAction {
459 private PowerAction() {
460 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400461 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400462 }
463
464 @Override
465 public boolean onLongPress() {
466 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
467 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
468 mWindowManagerFuncs.reboot(true);
469 return true;
470 }
471 return false;
472 }
473
474 @Override
475 public boolean showDuringKeyguard() {
476 return true;
477 }
478
479 @Override
480 public boolean showBeforeProvisioning() {
481 return true;
482 }
483
484 @Override
485 public void onPress() {
486 // shutdown by making sure radio and power are handled accordingly.
487 mWindowManagerFuncs.shutdown();
488 }
489 }
490
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800491 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800492 private EmergencyDialerAction() {
493 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800494 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800495 }
496
497 @Override
498 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800499 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800500 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800501 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
502 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
503 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800504 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
505 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800506 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
507 }
508
509 @Override
510 public boolean showDuringKeyguard() {
511 return true;
512 }
513
514 @Override
515 public boolean showBeforeProvisioning() {
516 return true;
517 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500518
519 @Override
520 public boolean shouldBeSeparated() {
521 return true;
522 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800523 }
524
Jason Monk361915c2017-03-21 20:33:59 -0400525 private final class RestartAction extends SinglePressAction implements LongPressAction {
526 private RestartAction() {
527 super(R.drawable.ic_restart, R.string.global_action_restart);
528 }
529
530 @Override
531 public boolean onLongPress() {
532 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
533 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
534 mWindowManagerFuncs.reboot(true);
535 return true;
536 }
537 return false;
538 }
539
540 @Override
541 public boolean showDuringKeyguard() {
542 return true;
543 }
544
545 @Override
546 public boolean showBeforeProvisioning() {
547 return true;
548 }
549
550 @Override
551 public void onPress() {
552 mWindowManagerFuncs.reboot(false);
553 }
554 }
555
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400556 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500557 public ScreenshotAction() {
558 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
559 }
560
561 @Override
562 public void onPress() {
563 // Add a little delay before executing, to give the
564 // dialog a chance to go away before it takes a
565 // screenshot.
566 // TODO: instead, omit global action dialog layer
567 mHandler.postDelayed(new Runnable() {
568 @Override
569 public void run() {
570 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
571 MetricsLogger.action(mContext,
572 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
573 }
574 }, 500);
575 }
576
577 @Override
578 public boolean showDuringKeyguard() {
579 return true;
580 }
581
582 @Override
583 public boolean showBeforeProvisioning() {
584 return false;
585 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400586
587 @Override
588 public boolean onLongPress() {
589 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
590 mScreenRecordHelper.launchRecordPrompt();
591 } else {
592 onPress();
593 }
594 return true;
595 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500596 }
597
Jason Monk361915c2017-03-21 20:33:59 -0400598 private class BugReportAction extends SinglePressAction implements LongPressAction {
599
600 public BugReportAction() {
601 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
602 }
603
604 @Override
605 public void onPress() {
606 // don't actually trigger the bugreport if we are running stability
607 // tests via monkey
608 if (ActivityManager.isUserAMonkey()) {
609 return;
610 }
611 // Add a little delay before executing, to give the
612 // dialog a chance to go away before it takes a
613 // screenshot.
614 mHandler.postDelayed(new Runnable() {
615 @Override
616 public void run() {
617 try {
618 // Take an "interactive" bugreport.
619 MetricsLogger.action(mContext,
620 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
621 ActivityManager.getService().requestBugReport(
622 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
623 } catch (RemoteException e) {
624 }
625 }
626 }, 500);
627 }
628
629 @Override
630 public boolean onLongPress() {
631 // don't actually trigger the bugreport if we are running stability
632 // tests via monkey
633 if (ActivityManager.isUserAMonkey()) {
634 return false;
635 }
636 try {
637 // Take a "full" bugreport.
638 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
639 ActivityManager.getService().requestBugReport(
640 ActivityManager.BUGREPORT_OPTION_FULL);
641 } catch (RemoteException e) {
642 }
643 return false;
644 }
645
646 public boolean showDuringKeyguard() {
647 return true;
648 }
649
650 @Override
651 public boolean showBeforeProvisioning() {
652 return false;
653 }
654
655 @Override
656 public String getStatus() {
657 return mContext.getString(
658 R.string.bugreport_status,
659 Build.VERSION.RELEASE,
660 Build.ID);
661 }
662 }
663
Alex Chau04458852017-11-27 18:21:23 +0000664 private final class LogoutAction extends SinglePressAction {
665 private LogoutAction() {
666 super(R.drawable.ic_logout, R.string.global_action_logout);
667 }
668
669 @Override
670 public boolean showDuringKeyguard() {
671 return true;
672 }
673
674 @Override
675 public boolean showBeforeProvisioning() {
676 return false;
677 }
678
679 @Override
680 public void onPress() {
681 // Add a little delay before executing, to give the dialog a chance to go away before
682 // switching user
683 mHandler.postDelayed(() -> {
684 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000685 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000686 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000687 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000688 } catch (RemoteException re) {
689 Log.e(TAG, "Couldn't logout user " + re);
690 }
691 }, 500);
692 }
693 }
694
Jason Monk361915c2017-03-21 20:33:59 -0400695 private Action getSettingsAction() {
696 return new SinglePressAction(R.drawable.ic_settings,
697 R.string.global_action_settings) {
698
699 @Override
700 public void onPress() {
701 Intent intent = new Intent(Settings.ACTION_SETTINGS);
702 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
703 mContext.startActivity(intent);
704 }
705
706 @Override
707 public boolean showDuringKeyguard() {
708 return true;
709 }
710
711 @Override
712 public boolean showBeforeProvisioning() {
713 return true;
714 }
715 };
716 }
717
718 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800719 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500720 if (!mUseSeparatedList) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800721 // use un-colored legacy treatment
722 emergencyIcon.setTintList(null);
723 }
724
Jason Monk361915c2017-03-21 20:33:59 -0400725 return new SinglePressAction(R.drawable.emergency_icon,
726 R.string.global_action_emergency) {
727 @Override
728 public void onPress() {
729 mEmergencyAffordanceManager.performEmergencyCall();
730 }
731
732 @Override
733 public boolean showDuringKeyguard() {
734 return true;
735 }
736
737 @Override
738 public boolean showBeforeProvisioning() {
739 return true;
740 }
741 };
742 }
743
744 private Action getAssistAction() {
745 return new SinglePressAction(R.drawable.ic_action_assist_focused,
746 R.string.global_action_assist) {
747 @Override
748 public void onPress() {
749 Intent intent = new Intent(Intent.ACTION_ASSIST);
750 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
751 mContext.startActivity(intent);
752 }
753
754 @Override
755 public boolean showDuringKeyguard() {
756 return true;
757 }
758
759 @Override
760 public boolean showBeforeProvisioning() {
761 return true;
762 }
763 };
764 }
765
766 private Action getVoiceAssistAction() {
767 return new SinglePressAction(R.drawable.ic_voice_search,
768 R.string.global_action_voice_assist) {
769 @Override
770 public void onPress() {
771 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
772 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
773 mContext.startActivity(intent);
774 }
775
776 @Override
777 public boolean showDuringKeyguard() {
778 return true;
779 }
780
781 @Override
782 public boolean showBeforeProvisioning() {
783 return true;
784 }
785 };
786 }
787
788 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400789 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400790 R.string.global_action_lockdown) {
791
792 @Override
793 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700794 new LockPatternUtils(mContext)
795 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
796 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400797 try {
798 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100799 // Lock profiles (if any) on the background thread.
800 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
801 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400802 } catch (RemoteException e) {
803 Log.e(TAG, "Error while trying to lock device.", e);
804 }
805 }
806
807 @Override
808 public boolean showDuringKeyguard() {
809 return true;
810 }
811
812 @Override
813 public boolean showBeforeProvisioning() {
814 return false;
815 }
816 };
817 }
818
Pavel Grafov059021b2018-05-02 13:44:46 +0100819 private void lockProfiles() {
820 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
821 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
822 final int currentUserId = getCurrentUser().id;
823 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
824 for (final int id : profileIds) {
825 if (id != currentUserId) {
826 tm.setDeviceLockedForUser(id, true);
827 }
828 }
829 }
830
Jason Monk361915c2017-03-21 20:33:59 -0400831 private UserInfo getCurrentUser() {
832 try {
833 return ActivityManager.getService().getCurrentUser();
834 } catch (RemoteException re) {
835 return null;
836 }
837 }
838
839 private boolean isCurrentUserOwner() {
840 UserInfo currentUser = getCurrentUser();
841 return currentUser == null || currentUser.isPrimary();
842 }
843
844 private void addUsersToMenu(ArrayList<Action> items) {
845 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
846 if (um.isUserSwitcherEnabled()) {
847 List<UserInfo> users = um.getUsers();
848 UserInfo currentUser = getCurrentUser();
849 for (final UserInfo user : users) {
850 if (user.supportsSwitchToByUser()) {
851 boolean isCurrentUser = currentUser == null
852 ? user.id == 0 : (currentUser.id == user.id);
853 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
854 : null;
855 SinglePressAction switchToUser = new SinglePressAction(
856 R.drawable.ic_menu_cc, icon,
857 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400858 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400859 public void onPress() {
860 try {
861 ActivityManager.getService().switchUser(user.id);
862 } catch (RemoteException re) {
863 Log.e(TAG, "Couldn't switch user " + re);
864 }
865 }
866
867 public boolean showDuringKeyguard() {
868 return true;
869 }
870
871 public boolean showBeforeProvisioning() {
872 return false;
873 }
874 };
875 items.add(switchToUser);
876 }
877 }
878 }
879 }
880
881 private void prepareDialog() {
882 refreshSilentMode();
883 mAirplaneModeOn.updateState(mAirplaneState);
884 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400885 if (mShowSilentToggle) {
886 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
887 mContext.registerReceiver(mRingerModeReceiver, filter);
888 }
889 }
890
891 private void refreshSilentMode() {
892 if (!mHasVibrator) {
893 final boolean silentModeOn =
894 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400895 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400896 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
897 }
898 }
899
900 /** {@inheritDoc} */
901 public void onDismiss(DialogInterface dialog) {
902 mWindowManagerFuncs.onGlobalActionsHidden();
903 if (mShowSilentToggle) {
904 try {
905 mContext.unregisterReceiver(mRingerModeReceiver);
906 } catch (IllegalArgumentException ie) {
907 // ignore this
908 Log.w(TAG, ie);
909 }
910 }
911 }
912
913 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800914 public void onShow(DialogInterface dialog) {
915 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
916 }
917
Jason Monk361915c2017-03-21 20:33:59 -0400918 /**
919 * The adapter used for the list within the global actions dialog, taking
920 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400921 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
922 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400923 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
924 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500925 public class MyAdapter extends MultiListAdapter {
926 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400927 public int getCount() {
928 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400929 for (int i = 0; i < mItems.size(); i++) {
930 final Action action = mItems.get(i);
931
932 if (mKeyguardShowing && !action.showDuringKeyguard()) {
933 continue;
934 }
935 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
936 continue;
937 }
938 count++;
939 }
940 return count;
941 }
942
943 @Override
944 public boolean isEnabled(int position) {
945 return getItem(position).isEnabled();
946 }
947
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500948 @Override
949 public ArrayList<Action> getSeparatedItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500950 ArrayList<Action> separatedActions = new ArrayList<Action>();
951 if (!shouldUseSeparatedView) {
952 return separatedActions;
953 }
954 for (int i = 0; i < mItems.size(); i++) {
955 final Action action = mItems.get(i);
956 if (action.shouldBeSeparated()) {
957 separatedActions.add(action);
958 }
959 }
960 return separatedActions;
961 }
962
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500963 @Override
964 public ArrayList<Action> getListItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500965 if (!shouldUseSeparatedView) {
966 return new ArrayList<Action>(mItems);
967 }
968 ArrayList<Action> listActions = new ArrayList<Action>();
969 for (int i = 0; i < mItems.size(); i++) {
970 final Action action = mItems.get(i);
971 if (!action.shouldBeSeparated()) {
972 listActions.add(action);
973 }
974 }
975 return listActions;
976 }
977
Jason Monk361915c2017-03-21 20:33:59 -0400978 @Override
979 public boolean areAllItemsEnabled() {
980 return false;
981 }
982
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500983 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400984 public Action getItem(int position) {
985
986 int filteredPos = 0;
987 for (int i = 0; i < mItems.size(); i++) {
988 final Action action = mItems.get(i);
989 if (mKeyguardShowing && !action.showDuringKeyguard()) {
990 continue;
991 }
992 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
993 continue;
994 }
995 if (filteredPos == position) {
996 return action;
997 }
998 filteredPos++;
999 }
1000
1001 throw new IllegalArgumentException("position " + position
1002 + " out of range of showable actions"
1003 + ", filtered count=" + getCount()
1004 + ", keyguardshowing=" + mKeyguardShowing
1005 + ", provisioned=" + mDeviceProvisioned);
1006 }
1007
1008
1009 public long getItemId(int position) {
1010 return position;
1011 }
1012
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001013 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001014 public View getView(int position, View convertView, ViewGroup parent) {
1015 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001016 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001017 // Everything but screenshot, the last item, gets white background.
1018 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001019 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001020 }
1021 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001022 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001023
1024 @Override
1025 public boolean onLongClickItem(int position) {
1026 final Action action = mAdapter.getItem(position);
1027 if (action instanceof LongPressAction) {
1028 mDialog.dismiss();
1029 return ((LongPressAction) action).onLongPress();
1030 }
1031 return false;
1032 }
1033
1034 @Override
1035 public void onClickItem(int position) {
1036 Action item = mAdapter.getItem(position);
1037 if (!(item instanceof SilentModeTriStateAction)) {
1038 mDialog.dismiss();
1039 }
1040 item.onPress();
1041 }
Jason Monk361915c2017-03-21 20:33:59 -04001042 }
1043
1044 // note: the scheme below made more sense when we were planning on having
1045 // 8 different things in the global actions dialog. seems overkill with
1046 // only 3 items now, but may as well keep this flexible approach so it will
1047 // be easy should someone decide at the last minute to include something
1048 // else, such as 'enable wifi', or 'enable bluetooth'
1049
1050 /**
1051 * What each item in the global actions dialog must be able to support.
1052 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001053 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001054 /**
1055 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001056 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001057 */
1058 CharSequence getLabelForAccessibility(Context context);
1059
1060 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1061
1062 void onPress();
1063
1064 /**
1065 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001066 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001067 */
1068 boolean showDuringKeyguard();
1069
1070 /**
1071 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001072 * device is provisioned.onlongpress
1073 *
Jason Monk361915c2017-03-21 20:33:59 -04001074 */
1075 boolean showBeforeProvisioning();
1076
1077 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001078
1079 default boolean shouldBeSeparated() {
1080 return false;
1081 }
Jason Monk361915c2017-03-21 20:33:59 -04001082 }
1083
1084 /**
1085 * An action that also supports long press.
1086 */
1087 private interface LongPressAction extends Action {
1088 boolean onLongPress();
1089 }
1090
1091 /**
1092 * A single press action maintains no state, just responds to a press
1093 * and takes an action.
1094 */
1095 private static abstract class SinglePressAction implements Action {
1096 private final int mIconResId;
1097 private final Drawable mIcon;
1098 private final int mMessageResId;
1099 private final CharSequence mMessage;
1100
1101 protected SinglePressAction(int iconResId, int messageResId) {
1102 mIconResId = iconResId;
1103 mMessageResId = messageResId;
1104 mMessage = null;
1105 mIcon = null;
1106 }
1107
1108 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1109 mIconResId = iconResId;
1110 mMessageResId = 0;
1111 mMessage = message;
1112 mIcon = icon;
1113 }
1114
1115 public boolean isEnabled() {
1116 return true;
1117 }
1118
1119 public String getStatus() {
1120 return null;
1121 }
1122
1123 abstract public void onPress();
1124
1125 public CharSequence getLabelForAccessibility(Context context) {
1126 if (mMessage != null) {
1127 return mMessage;
1128 } else {
1129 return context.getString(mMessageResId);
1130 }
1131 }
1132
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001133 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001134 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001135 return com.android.systemui.R.layout.global_actions_grid_item;
1136 }
1137 return com.android.systemui.R.layout.global_actions_item;
1138 }
1139
Jason Monk361915c2017-03-21 20:33:59 -04001140 public View create(
1141 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001142 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001143 false);
Jason Monk361915c2017-03-21 20:33:59 -04001144
1145 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1146 TextView messageView = (TextView) v.findViewById(R.id.message);
1147
1148 TextView statusView = (TextView) v.findViewById(R.id.status);
1149 final String status = getStatus();
1150 if (!TextUtils.isEmpty(status)) {
1151 statusView.setText(status);
1152 } else {
1153 statusView.setVisibility(View.GONE);
1154 }
1155 if (mIcon != null) {
1156 icon.setImageDrawable(mIcon);
1157 icon.setScaleType(ScaleType.CENTER_CROP);
1158 } else if (mIconResId != 0) {
1159 icon.setImageDrawable(context.getDrawable(mIconResId));
1160 }
1161 if (mMessage != null) {
1162 messageView.setText(mMessage);
1163 } else {
1164 messageView.setText(mMessageResId);
1165 }
1166
1167 return v;
1168 }
1169 }
1170
1171 /**
1172 * A toggle action knows whether it is on or off, and displays an icon
1173 * and status message accordingly.
1174 */
1175 private static abstract class ToggleAction implements Action {
1176
1177 enum State {
1178 Off(false),
1179 TurningOn(true),
1180 TurningOff(true),
1181 On(false);
1182
1183 private final boolean inTransition;
1184
1185 State(boolean intermediate) {
1186 inTransition = intermediate;
1187 }
1188
1189 public boolean inTransition() {
1190 return inTransition;
1191 }
1192 }
1193
1194 protected State mState = State.Off;
1195
1196 // prefs
1197 protected int mEnabledIconResId;
1198 protected int mDisabledIconResid;
1199 protected int mMessageResId;
1200 protected int mEnabledStatusMessageResId;
1201 protected int mDisabledStatusMessageResId;
1202
1203 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001204 * @param enabledIconResId The icon for when this action is on.
1205 * @param disabledIconResid The icon for when this action is off.
1206 * @param message The general information message, e.g 'Silent Mode'
1207 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001208 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1209 */
1210 public ToggleAction(int enabledIconResId,
1211 int disabledIconResid,
1212 int message,
1213 int enabledStatusMessageResId,
1214 int disabledStatusMessageResId) {
1215 mEnabledIconResId = enabledIconResId;
1216 mDisabledIconResid = disabledIconResid;
1217 mMessageResId = message;
1218 mEnabledStatusMessageResId = enabledStatusMessageResId;
1219 mDisabledStatusMessageResId = disabledStatusMessageResId;
1220 }
1221
1222 /**
1223 * Override to make changes to resource IDs just before creating the
1224 * View.
1225 */
1226 void willCreate() {
1227
1228 }
1229
1230 @Override
1231 public CharSequence getLabelForAccessibility(Context context) {
1232 return context.getString(mMessageResId);
1233 }
1234
1235 public View create(Context context, View convertView, ViewGroup parent,
1236 LayoutInflater inflater) {
1237 willCreate();
1238
1239 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001240 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001241
1242 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1243 TextView messageView = (TextView) v.findViewById(R.id.message);
1244 TextView statusView = (TextView) v.findViewById(R.id.status);
1245 final boolean enabled = isEnabled();
1246
1247 if (messageView != null) {
1248 messageView.setText(mMessageResId);
1249 messageView.setEnabled(enabled);
1250 }
1251
1252 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1253 if (icon != null) {
1254 icon.setImageDrawable(context.getDrawable(
1255 (on ? mEnabledIconResId : mDisabledIconResid)));
1256 icon.setEnabled(enabled);
1257 }
1258
1259 if (statusView != null) {
1260 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1261 statusView.setVisibility(View.VISIBLE);
1262 statusView.setEnabled(enabled);
1263 }
1264 v.setEnabled(enabled);
1265
1266 return v;
1267 }
1268
1269 public final void onPress() {
1270 if (mState.inTransition()) {
1271 Log.w(TAG, "shouldn't be able to toggle when in transition");
1272 return;
1273 }
1274
1275 final boolean nowOn = !(mState == State.On);
1276 onToggle(nowOn);
1277 changeStateFromPress(nowOn);
1278 }
1279
1280 public boolean isEnabled() {
1281 return !mState.inTransition();
1282 }
1283
1284 /**
1285 * Implementations may override this if their state can be in on of the intermediate
1286 * states until some notification is received (e.g airplane mode is 'turning off' until
1287 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001288 *
Jason Monk361915c2017-03-21 20:33:59 -04001289 * @param buttonOn Whether the button was turned on or off
1290 */
1291 protected void changeStateFromPress(boolean buttonOn) {
1292 mState = buttonOn ? State.On : State.Off;
1293 }
1294
1295 abstract void onToggle(boolean on);
1296
1297 public void updateState(State state) {
1298 mState = state;
1299 }
1300 }
1301
1302 private class SilentModeToggleAction extends ToggleAction {
1303 public SilentModeToggleAction() {
1304 super(R.drawable.ic_audio_vol_mute,
1305 R.drawable.ic_audio_vol,
1306 R.string.global_action_toggle_silent_mode,
1307 R.string.global_action_silent_mode_on_status,
1308 R.string.global_action_silent_mode_off_status);
1309 }
1310
1311 void onToggle(boolean on) {
1312 if (on) {
1313 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1314 } else {
1315 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1316 }
1317 }
1318
1319 public boolean showDuringKeyguard() {
1320 return true;
1321 }
1322
1323 public boolean showBeforeProvisioning() {
1324 return false;
1325 }
1326 }
1327
1328 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1329
Jason Monk16fbd9d2017-04-27 14:28:49 -04001330 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001331
1332 private final AudioManager mAudioManager;
1333 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001334
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001335 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001336 mAudioManager = audioManager;
1337 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001338 }
1339
1340 private int ringerModeToIndex(int ringerMode) {
1341 // They just happen to coincide
1342 return ringerMode;
1343 }
1344
1345 private int indexToRingerMode(int index) {
1346 // They just happen to coincide
1347 return index;
1348 }
1349
1350 @Override
1351 public CharSequence getLabelForAccessibility(Context context) {
1352 return null;
1353 }
1354
1355 public View create(Context context, View convertView, ViewGroup parent,
1356 LayoutInflater inflater) {
1357 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1358
1359 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1360 for (int i = 0; i < 3; i++) {
1361 View itemView = v.findViewById(ITEM_IDS[i]);
1362 itemView.setSelected(selectedIndex == i);
1363 // Set up click handler
1364 itemView.setTag(i);
1365 itemView.setOnClickListener(this);
1366 }
1367 return v;
1368 }
1369
1370 public void onPress() {
1371 }
1372
1373 public boolean showDuringKeyguard() {
1374 return true;
1375 }
1376
1377 public boolean showBeforeProvisioning() {
1378 return false;
1379 }
1380
1381 public boolean isEnabled() {
1382 return true;
1383 }
1384
1385 void willCreate() {
1386 }
1387
1388 public void onClick(View v) {
1389 if (!(v.getTag() instanceof Integer)) return;
1390
1391 int index = (Integer) v.getTag();
1392 mAudioManager.setRingerMode(indexToRingerMode(index));
1393 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1394 }
1395 }
1396
1397 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1398 public void onReceive(Context context, Intent intent) {
1399 String action = intent.getAction();
1400 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1401 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1402 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1403 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001404 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001405 }
1406 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1407 // Airplane mode can be changed after ECM exits if airplane toggle button
1408 // is pressed during ECM mode
1409 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1410 mIsWaitingForEcmExit) {
1411 mIsWaitingForEcmExit = false;
1412 changeAirplaneModeSystemSetting(true);
1413 }
1414 }
1415 }
1416 };
1417
1418 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1419 @Override
1420 public void onServiceStateChanged(ServiceState serviceState) {
1421 if (!mHasTelephony) return;
1422 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1423 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1424 mAirplaneModeOn.updateState(mAirplaneState);
1425 mAdapter.notifyDataSetChanged();
1426 }
1427 };
1428
1429 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1430 @Override
1431 public void onReceive(Context context, Intent intent) {
1432 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1433 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1434 }
1435 }
1436 };
1437
1438 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1439 @Override
1440 public void onChange(boolean selfChange) {
1441 onAirplaneModeChanged();
1442 }
1443 };
1444
1445 private static final int MESSAGE_DISMISS = 0;
1446 private static final int MESSAGE_REFRESH = 1;
1447 private static final int MESSAGE_SHOW = 2;
1448 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1449
1450 private Handler mHandler = new Handler() {
1451 public void handleMessage(Message msg) {
1452 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001453 case MESSAGE_DISMISS:
1454 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001455 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1456 mDialog.dismissImmediately();
1457 } else {
1458 mDialog.dismiss();
1459 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001460 mDialog = null;
1461 }
1462 break;
1463 case MESSAGE_REFRESH:
1464 refreshSilentMode();
1465 mAdapter.notifyDataSetChanged();
1466 break;
1467 case MESSAGE_SHOW:
1468 handleShow();
1469 break;
Jason Monk361915c2017-03-21 20:33:59 -04001470 }
1471 }
1472 };
1473
1474 private void onAirplaneModeChanged() {
1475 // Let the service state callbacks handle the state.
1476 if (mHasTelephony) return;
1477
1478 boolean airplaneModeOn = Settings.Global.getInt(
1479 mContext.getContentResolver(),
1480 Settings.Global.AIRPLANE_MODE_ON,
1481 0) == 1;
1482 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1483 mAirplaneModeOn.updateState(mAirplaneState);
1484 }
1485
1486 /**
1487 * Change the airplane mode system setting
1488 */
1489 private void changeAirplaneModeSystemSetting(boolean on) {
1490 Settings.Global.putInt(
1491 mContext.getContentResolver(),
1492 Settings.Global.AIRPLANE_MODE_ON,
1493 on ? 1 : 0);
1494 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1495 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1496 intent.putExtra("state", on);
1497 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1498 if (!mHasTelephony) {
1499 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1500 }
1501 }
1502
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001503 private static final class ActionsDialog extends Dialog implements DialogInterface,
1504 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001505
Jason Monk16fbd9d2017-04-27 14:28:49 -04001506 private final Context mContext;
1507 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001508 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001509 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001510 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001511 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001512 private boolean mKeyguardShowing;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001513 private boolean mUseSeparatedList;
Beverly526d2d62018-08-15 12:55:33 -04001514 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001515 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001516
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001517 ActionsDialog(Context context, MyAdapter adapter, boolean separated,
Steve Elliott9b87a442019-03-05 10:24:16 -05001518 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001519 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001520 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001521 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001522 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001523 mUseSeparatedList = separated;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001524
1525 // Window initialization
1526 Window window = getWindow();
1527 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001528 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1529 window.getDecorView();
1530 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1531 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1532 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1533 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1534 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001535 window.addFlags(
1536 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001537 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001538 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001539 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1540 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1541 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001542 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1543
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001544 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001545
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001546 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001547
1548 mPanelController = plugin;
1549 View panelView = initializePanel();
1550 if (panelView == null) {
1551 mBackgroundDrawable = new GradientDrawable(context);
Steve Elliottbfa314a2019-03-06 13:53:19 -05001552 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
Steve Elliott9b87a442019-03-05 10:24:16 -05001553 } else {
1554 mBackgroundDrawable = context.getDrawable(
1555 com.android.systemui.R.drawable.global_action_panel_scrim);
1556 mScrimAlpha = 1f;
1557 addContentView(
1558 panelView,
1559 new ViewGroup.LayoutParams(
1560 ViewGroup.LayoutParams.MATCH_PARENT,
1561 ViewGroup.LayoutParams.MATCH_PARENT));
1562 }
1563 window.setBackgroundDrawable(mBackgroundDrawable);
1564 }
1565
1566 private View initializePanel() {
1567 if (isPanelEnabled(mContext) && mPanelController != null) {
1568 View panelView = mPanelController.getPanelContent();
1569 if (panelView != null) {
1570 FrameLayout panelContainer = new FrameLayout(mContext);
1571 FrameLayout.LayoutParams panelParams =
1572 new FrameLayout.LayoutParams(
1573 FrameLayout.LayoutParams.MATCH_PARENT,
1574 FrameLayout.LayoutParams.WRAP_CONTENT);
Steve Elliott9b87a442019-03-05 10:24:16 -05001575 panelContainer.addView(panelView, panelParams);
1576 return panelContainer;
1577 }
1578 }
1579 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001580 }
1581
1582 private void initializeLayout() {
1583 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001584 mGlobalActionsLayout = (MultiListLayout)
1585 findViewById(com.android.systemui.R.id.global_actions_view);
1586 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001587 mGlobalActionsLayout.setSeparated(mUseSeparatedList);
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);
Jason Monk361915c2017-03-21 20:33:59 -04001599 }
1600
Steve Elliott9b87a442019-03-05 10:24:16 -05001601 private boolean isPanelEnabled(Context context) {
1602 return FeatureFlagUtils.isEnabled(
1603 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1604 }
1605
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001606 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001607 if (isGridEnabled(context)) {
1608 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1609 return com.android.systemui.R.layout.global_actions_grid_seascape;
1610 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001611 return com.android.systemui.R.layout.global_actions_grid;
1612 }
1613 return com.android.systemui.R.layout.global_actions_wrapped;
1614 }
1615
Jason Monk361915c2017-03-21 20:33:59 -04001616 @Override
1617 protected void onStart() {
1618 super.setCanceledOnTouchOutside(true);
1619 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001620 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001621
Steve Elliott9b87a442019-03-05 10:24:16 -05001622 if (mBackgroundDrawable instanceof GradientDrawable) {
1623 Point displaySize = new Point();
1624 mContext.getDisplay().getRealSize(displaySize);
1625 mColorExtractor.addOnColorsChangedListener(this);
1626 ((GradientDrawable) mBackgroundDrawable)
1627 .setScreenSize(displaySize.x, displaySize.y);
1628 GradientColors colors = mColorExtractor.getColors(
1629 mKeyguardShowing
1630 ? WallpaperManager.FLAG_LOCK
1631 : WallpaperManager.FLAG_SYSTEM);
1632 updateColors(colors, false /* animate */);
1633 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001634 }
1635
1636 /**
1637 * Updates background and system bars according to current GradientColors.
1638 * @param colors Colors and hints to use.
1639 * @param animate Interpolates gradient if true, just sets otherwise.
1640 */
1641 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001642 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1643 return;
1644 }
1645 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001646 View decorView = getWindow().getDecorView();
1647 if (colors.supportsDarkText()) {
1648 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001649 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001650 } else {
1651 decorView.setSystemUiVisibility(0);
1652 }
Jason Monk361915c2017-03-21 20:33:59 -04001653 }
1654
1655 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001656 protected void onStop() {
1657 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001658 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001659 }
1660
1661 @Override
1662 public void show() {
1663 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001664 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001665 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001666 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1667 mGlobalActionsLayout.setAlpha(0);
1668 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001669 .alpha(1)
1670 .translationX(0)
1671 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001672 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001673 .setUpdateListener(animation -> {
1674 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001675 * mScrimAlpha * 255);
1676 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001677 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001678 .start();
1679 }
1680
1681 @Override
1682 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001683 if (!mShowing) {
1684 return;
1685 }
1686 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001687 mGlobalActionsLayout.setTranslationX(0);
1688 mGlobalActionsLayout.setAlpha(1);
1689 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001690 .alpha(0)
1691 .translationX(getAnimTranslation())
1692 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001693 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001694 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001695 .setUpdateListener(animation -> {
1696 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001697 * mScrimAlpha * 255);
1698 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001699 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001700 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001701 if (mPanelController != null) {
1702 mPanelController.onDismissed();
1703 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001704 }
1705
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001706 void dismissImmediately() {
1707 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001708 mShowing = false;
Steve Elliott2d206d32019-03-14 16:30:33 -04001709 if (mPanelController != null) {
1710 mPanelController.onDismissed();
1711 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001712 }
1713
Jason Monk16fbd9d2017-04-27 14:28:49 -04001714 private float getAnimTranslation() {
1715 return getContext().getResources().getDimension(
1716 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001717 }
1718
1719 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001720 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001721 if (mKeyguardShowing) {
1722 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001723 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1724 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001725 }
1726 } else {
1727 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001728 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1729 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001730 }
1731 }
1732 }
1733
1734 public void setKeyguardShowing(boolean keyguardShowing) {
1735 mKeyguardShowing = keyguardShowing;
1736 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001737
1738 public void onRotate(int from, int to) {
1739 if (mShowing && isGridEnabled(mContext)) {
1740 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001741 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001742 }
1743 }
Jason Monk361915c2017-03-21 20:33:59 -04001744 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001745
1746 /**
1747 * Determines whether or not the Global Actions Dialog should use the newer grid-style layout.
1748 */
1749 public static boolean isGridEnabled(Context context) {
1750 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1751 }
Jason Monk361915c2017-03-21 20:33:59 -04001752}