blob: dcacd0fbc644786b4943413f781cda1ece383a9b [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;
Jason Monk361915c2017-03-21 20:33:59 -040042import android.os.Handler;
43import android.os.Message;
44import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.os.UserHandle;
48import android.os.UserManager;
49import android.os.Vibrator;
50import android.provider.Settings;
51import android.service.dreams.DreamService;
52import android.service.dreams.IDreamManager;
53import android.telephony.PhoneStateListener;
54import android.telephony.ServiceState;
55import android.telephony.TelephonyManager;
56import android.text.TextUtils;
57import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080058import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040059import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070060import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040061import android.view.LayoutInflater;
62import android.view.View;
63import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070064import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.WindowManager;
66import android.view.WindowManagerGlobal;
67import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050068import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.widget.ImageView;
70import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.TextView;
72
Charles He9851a8d2017-10-10 17:31:30 +010073import com.android.internal.R;
74import com.android.internal.colorextraction.ColorExtractor;
75import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070076import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.logging.MetricsLogger;
78import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
79import com.android.internal.telephony.TelephonyIntents;
80import com.android.internal.telephony.TelephonyProperties;
81import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040082import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050083import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.widget.LockPatternUtils;
85import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050087import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050088import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040090import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050092import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050093import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070094import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080095import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050096import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050097import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070098
Jason Monk361915c2017-03-21 20:33:59 -040099import java.util.ArrayList;
100import java.util.List;
101
102/**
103 * Helper to show the global actions dialog. Each item is an {@link Action} that
104 * may show depending on whether the keyguard is showing, and whether the device
105 * is provisioned.
106 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500107public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
108 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400109
110 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
111 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700112 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400113
114 private static final String TAG = "GlobalActionsDialog";
115
116 private static final boolean SHOW_SILENT_TOGGLE = true;
117
118 /* Valid settings for global actions keys.
119 * see config.xml config_globalActionList */
120 private static final String GLOBAL_ACTION_KEY_POWER = "power";
121 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
122 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
123 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
124 private static final String GLOBAL_ACTION_KEY_USERS = "users";
125 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
126 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
127 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
128 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
129 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000130 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800131 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500132 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400133
134 private final Context mContext;
135 private final GlobalActionsManager mWindowManagerFuncs;
136 private final AudioManager mAudioManager;
137 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000138 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800139 private final LockPatternUtils mLockPatternUtils;
140 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400141
142 private ArrayList<Action> mItems;
143 private ActionsDialog mDialog;
144
145 private Action mSilentModeAction;
146 private ToggleAction mAirplaneModeOn;
147
148 private MyAdapter mAdapter;
149
150 private boolean mKeyguardShowing = false;
151 private boolean mDeviceProvisioned = false;
152 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
153 private boolean mIsWaitingForEcmExit = false;
154 private boolean mHasTelephony;
155 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000156 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800157 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400158 private final boolean mShowSilentToggle;
159 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500160 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400161 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400162 private final ActivityStarter mActivityStarter;
163 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500164
Jason Monk361915c2017-03-21 20:33:59 -0400165 /**
166 * @param context everything needs a context :(
167 */
168 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700169 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400170 mWindowManagerFuncs = windowManagerFuncs;
171 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
172 mDreamManager = IDreamManager.Stub.asInterface(
173 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000174 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
175 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800176 mLockPatternUtils = new LockPatternUtils(mContext);
177 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400178
179 // receive broadcasts
180 IntentFilter filter = new IntentFilter();
181 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
182 filter.addAction(Intent.ACTION_SCREEN_OFF);
183 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
184 context.registerReceiver(mBroadcastReceiver, filter);
185
186 ConnectivityManager cm = (ConnectivityManager)
187 context.getSystemService(Context.CONNECTIVITY_SERVICE);
188 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
189
190 // get notified of phone state changes
191 TelephonyManager telephonyManager =
192 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
193 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
194 mContext.getContentResolver().registerContentObserver(
195 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
196 mAirplaneModeObserver);
197 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
198 mHasVibrator = vibrator != null && vibrator.hasVibrator();
199
200 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
201 R.bool.config_useFixedVolume);
202
203 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500204 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400205 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700206
207 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500208
Steve Elliott4c868852019-03-14 16:25:41 -0400209 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400210 }
211
212 /**
213 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400214 *
Jason Monk361915c2017-03-21 20:33:59 -0400215 * @param keyguardShowing True if keyguard is showing
216 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400217 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
218 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400219 mKeyguardShowing = keyguardShowing;
220 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400221 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400222 if (mDialog != null) {
223 mDialog.dismiss();
224 mDialog = null;
225 // Show delayed, so that the dismiss of the previous dialog completes
226 mHandler.sendEmptyMessage(MESSAGE_SHOW);
227 } else {
228 handleShow();
229 }
230 }
231
Charles He9851a8d2017-10-10 17:31:30 +0100232 /**
233 * Dismiss the global actions dialog, if it's currently shown
234 */
235 public void dismissDialog() {
236 mHandler.removeMessages(MESSAGE_DISMISS);
237 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
238 }
239
Jason Monk361915c2017-03-21 20:33:59 -0400240 private void awakenIfNecessary() {
241 if (mDreamManager != null) {
242 try {
243 if (mDreamManager.isDreaming()) {
244 mDreamManager.awaken();
245 }
246 } catch (RemoteException e) {
247 // we tried
248 }
249 }
250 }
251
252 private void handleShow() {
253 awakenIfNecessary();
254 mDialog = createDialog();
255 prepareDialog();
256
257 // If we only have 1 item and it's a simple press action, just do this action.
258 if (mAdapter.getCount() == 1
259 && mAdapter.getItem(0) instanceof SinglePressAction
260 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
261 ((SinglePressAction) mAdapter.getItem(0)).onPress();
262 } else {
263 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
264 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100265 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400266 mDialog.getWindow().setAttributes(attrs);
267 mDialog.show();
268 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400269 }
270 }
271
272 /**
273 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400274 *
Jason Monk361915c2017-03-21 20:33:59 -0400275 * @return A new dialog.
276 */
277 private ActionsDialog createDialog() {
278 // Simple toggle style if there's no vibrator, otherwise use a tri-state
279 if (!mHasVibrator) {
280 mSilentModeAction = new SilentModeToggleAction();
281 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700282 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400283 }
284 mAirplaneModeOn = new ToggleAction(
285 R.drawable.ic_lock_airplane_mode,
286 R.drawable.ic_lock_airplane_mode_off,
287 R.string.global_actions_toggle_airplane_mode,
288 R.string.global_actions_airplane_mode_on_status,
289 R.string.global_actions_airplane_mode_off_status) {
290
291 void onToggle(boolean on) {
292 if (mHasTelephony && Boolean.parseBoolean(
293 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
294 mIsWaitingForEcmExit = true;
295 // Launch ECM exit dialog
296 Intent ecmDialogIntent =
297 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
298 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
299 mContext.startActivity(ecmDialogIntent);
300 } else {
301 changeAirplaneModeSystemSetting(on);
302 }
303 }
304
305 @Override
306 protected void changeStateFromPress(boolean buttonOn) {
307 if (!mHasTelephony) return;
308
309 // In ECM mode airplane state cannot be changed
310 if (!(Boolean.parseBoolean(
311 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
312 mState = buttonOn ? State.TurningOn : State.TurningOff;
313 mAirplaneState = mState;
314 }
315 }
316
317 public boolean showDuringKeyguard() {
318 return true;
319 }
320
321 public boolean showBeforeProvisioning() {
322 return false;
323 }
324 };
325 onAirplaneModeChanged();
326
327 mItems = new ArrayList<Action>();
328 String[] defaultActions = mContext.getResources().getStringArray(
329 R.array.config_globalActionsList);
330
331 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000332 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800333 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400334 for (int i = 0; i < defaultActions.length; i++) {
335 String actionKey = defaultActions[i];
336 if (addedKeys.contains(actionKey)) {
337 // If we already have added this, don't add it again.
338 continue;
339 }
340 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
341 mItems.add(new PowerAction());
342 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
343 mItems.add(mAirplaneModeOn);
344 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
345 if (Settings.Global.getInt(mContext.getContentResolver(),
346 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
347 mItems.add(new BugReportAction());
348 }
349 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
350 if (mShowSilentToggle) {
351 mItems.add(mSilentModeAction);
352 }
353 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
354 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
355 addUsersToMenu(mItems);
356 }
357 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
358 mItems.add(getSettingsAction());
359 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700360 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
361 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800362 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700363 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800364 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700365 }
Jason Monk361915c2017-03-21 20:33:59 -0400366 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
367 mItems.add(getVoiceAssistAction());
368 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
369 mItems.add(getAssistAction());
370 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
371 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500372 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
373 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000374 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000375 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000376 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
377 mItems.add(new LogoutAction());
378 mHasLogoutButton = true;
379 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800380 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400381 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800382 mItems.add(new EmergencyDialerAction());
383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else {
385 Log.e(TAG, "Invalid global action key " + actionKey);
386 }
387 // Add here so we don't add more than one.
388 addedKeys.add(actionKey);
389 }
390
391 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400392 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400393 }
394
395 mAdapter = new MyAdapter();
396
Steve Elliott9b87a442019-03-05 10:24:16 -0500397 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400398 mPanelPlugin != null
399 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400400 new GlobalActionsPanelPlugin.Callbacks() {
401 @Override
402 public void dismissGlobalActionsMenu() {
403 if (mDialog != null) {
404 mDialog.dismiss();
405 }
406 }
407
408 @Override
409 public void startPendingIntentDismissingKeyguard(
410 PendingIntent intent) {
411 mActivityStarter
412 .startPendingIntentDismissingKeyguard(intent);
413 }
414 })
Steve Elliott9b87a442019-03-05 10:24:16 -0500415 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400416 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400417 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700418 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400419
420 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800421 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400422
423 return dialog;
424 }
425
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800426 private boolean shouldDisplayLockdown() {
427 int userId = getCurrentUser().id;
428 // Lockdown is meaningless without a place to go.
429 if (!mKeyguardManager.isDeviceSecure(userId)) {
430 return false;
431 }
432
433 // Only show the lockdown button if the device isn't locked down (for whatever reason).
434 int state = mLockPatternUtils.getStrongAuthForUser(userId);
435 return (state == STRONG_AUTH_NOT_REQUIRED
436 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
437 }
438
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700439 @Override
440 public void onUiModeChanged() {
441 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400442 if (mDialog.isShowing()) {
443 mDialog.refreshDialog();
444 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700445 }
446
447 public void destroy() {
448 Dependency.get(ConfigurationController.class).removeCallback(this);
449 }
450
Jason Monk361915c2017-03-21 20:33:59 -0400451 private final class PowerAction extends SinglePressAction implements LongPressAction {
452 private PowerAction() {
453 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400454 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400455 }
456
457 @Override
458 public boolean onLongPress() {
459 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
460 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
461 mWindowManagerFuncs.reboot(true);
462 return true;
463 }
464 return false;
465 }
466
467 @Override
468 public boolean showDuringKeyguard() {
469 return true;
470 }
471
472 @Override
473 public boolean showBeforeProvisioning() {
474 return true;
475 }
476
477 @Override
478 public void onPress() {
479 // shutdown by making sure radio and power are handled accordingly.
480 mWindowManagerFuncs.shutdown();
481 }
482 }
483
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400484 private abstract class EmergencyAction extends SinglePressAction {
485 EmergencyAction(int iconResId, int messageResId) {
486 super(iconResId, messageResId);
487 }
488
489 @Override
490 public boolean shouldBeSeparated() {
491 return shouldUseSeparatedView();
492 }
493
494 @Override
495 public View create(
496 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
497 View v = super.create(context, convertView, parent, inflater);
498 int textColor;
499 if (shouldBeSeparated()) {
500 textColor = v.getResources().getColor(
501 com.android.systemui.R.color.global_actions_alert_text);
502 } else {
503 textColor = v.getResources().getColor(
504 com.android.systemui.R.color.global_actions_text);
505 }
506 TextView messageView = v.findViewById(R.id.message);
507 messageView.setTextColor(textColor);
508 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 {
922 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400923 public int getCount() {
924 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400925 for (int i = 0; i < mItems.size(); i++) {
926 final Action action = mItems.get(i);
927
928 if (mKeyguardShowing && !action.showDuringKeyguard()) {
929 continue;
930 }
931 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
932 continue;
933 }
934 count++;
935 }
936 return count;
937 }
938
939 @Override
940 public boolean isEnabled(int position) {
941 return getItem(position).isEnabled();
942 }
943
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500944 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400945 public ArrayList<Action> getSeparatedItems() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500946 ArrayList<Action> separatedActions = new ArrayList<Action>();
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400947 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500948 return separatedActions;
949 }
950 for (int i = 0; i < mItems.size(); i++) {
951 final Action action = mItems.get(i);
952 if (action.shouldBeSeparated()) {
953 separatedActions.add(action);
954 }
955 }
956 return separatedActions;
957 }
958
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500959 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400960 public ArrayList<Action> getListItems() {
961 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500962 return new ArrayList<Action>(mItems);
963 }
964 ArrayList<Action> listActions = new ArrayList<Action>();
965 for (int i = 0; i < mItems.size(); i++) {
966 final Action action = mItems.get(i);
967 if (!action.shouldBeSeparated()) {
968 listActions.add(action);
969 }
970 }
971 return listActions;
972 }
973
Jason Monk361915c2017-03-21 20:33:59 -0400974 @Override
975 public boolean areAllItemsEnabled() {
976 return false;
977 }
978
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500979 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400980 public Action getItem(int position) {
981
982 int filteredPos = 0;
983 for (int i = 0; i < mItems.size(); i++) {
984 final Action action = mItems.get(i);
985 if (mKeyguardShowing && !action.showDuringKeyguard()) {
986 continue;
987 }
988 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
989 continue;
990 }
991 if (filteredPos == position) {
992 return action;
993 }
994 filteredPos++;
995 }
996
997 throw new IllegalArgumentException("position " + position
998 + " out of range of showable actions"
999 + ", filtered count=" + getCount()
1000 + ", keyguardshowing=" + mKeyguardShowing
1001 + ", provisioned=" + mDeviceProvisioned);
1002 }
1003
1004
1005 public long getItemId(int position) {
1006 return position;
1007 }
1008
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001009 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001010 public View getView(int position, View convertView, ViewGroup parent) {
1011 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001012 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001013 // Everything but screenshot, the last item, gets white background.
1014 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001015 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001016 }
1017 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001018 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001019
1020 @Override
1021 public boolean onLongClickItem(int position) {
1022 final Action action = mAdapter.getItem(position);
1023 if (action instanceof LongPressAction) {
1024 mDialog.dismiss();
1025 return ((LongPressAction) action).onLongPress();
1026 }
1027 return false;
1028 }
1029
1030 @Override
1031 public void onClickItem(int position) {
1032 Action item = mAdapter.getItem(position);
1033 if (!(item instanceof SilentModeTriStateAction)) {
1034 mDialog.dismiss();
1035 }
1036 item.onPress();
1037 }
Jason Monk361915c2017-03-21 20:33:59 -04001038 }
1039
1040 // note: the scheme below made more sense when we were planning on having
1041 // 8 different things in the global actions dialog. seems overkill with
1042 // only 3 items now, but may as well keep this flexible approach so it will
1043 // be easy should someone decide at the last minute to include something
1044 // else, such as 'enable wifi', or 'enable bluetooth'
1045
1046 /**
1047 * What each item in the global actions dialog must be able to support.
1048 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001049 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001050 /**
1051 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001052 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001053 */
1054 CharSequence getLabelForAccessibility(Context context);
1055
1056 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1057
1058 void onPress();
1059
1060 /**
1061 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001062 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001063 */
1064 boolean showDuringKeyguard();
1065
1066 /**
1067 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001068 * device is provisioned.onlongpress
1069 *
Jason Monk361915c2017-03-21 20:33:59 -04001070 */
1071 boolean showBeforeProvisioning();
1072
1073 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001074
1075 default boolean shouldBeSeparated() {
1076 return false;
1077 }
Jason Monk361915c2017-03-21 20:33:59 -04001078 }
1079
1080 /**
1081 * An action that also supports long press.
1082 */
1083 private interface LongPressAction extends Action {
1084 boolean onLongPress();
1085 }
1086
1087 /**
1088 * A single press action maintains no state, just responds to a press
1089 * and takes an action.
1090 */
1091 private static abstract class SinglePressAction implements Action {
1092 private final int mIconResId;
1093 private final Drawable mIcon;
1094 private final int mMessageResId;
1095 private final CharSequence mMessage;
1096
1097 protected SinglePressAction(int iconResId, int messageResId) {
1098 mIconResId = iconResId;
1099 mMessageResId = messageResId;
1100 mMessage = null;
1101 mIcon = null;
1102 }
1103
1104 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1105 mIconResId = iconResId;
1106 mMessageResId = 0;
1107 mMessage = message;
1108 mIcon = icon;
1109 }
1110
1111 public boolean isEnabled() {
1112 return true;
1113 }
1114
1115 public String getStatus() {
1116 return null;
1117 }
1118
1119 abstract public void onPress();
1120
1121 public CharSequence getLabelForAccessibility(Context context) {
1122 if (mMessage != null) {
1123 return mMessage;
1124 } else {
1125 return context.getString(mMessageResId);
1126 }
1127 }
1128
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001129 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001130 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001131 return com.android.systemui.R.layout.global_actions_grid_item;
1132 }
1133 return com.android.systemui.R.layout.global_actions_item;
1134 }
1135
Jason Monk361915c2017-03-21 20:33:59 -04001136 public View create(
1137 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001138 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001139 false);
Jason Monk361915c2017-03-21 20:33:59 -04001140
1141 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1142 TextView messageView = (TextView) v.findViewById(R.id.message);
1143
1144 TextView statusView = (TextView) v.findViewById(R.id.status);
1145 final String status = getStatus();
1146 if (!TextUtils.isEmpty(status)) {
1147 statusView.setText(status);
1148 } else {
1149 statusView.setVisibility(View.GONE);
1150 }
1151 if (mIcon != null) {
1152 icon.setImageDrawable(mIcon);
1153 icon.setScaleType(ScaleType.CENTER_CROP);
1154 } else if (mIconResId != 0) {
1155 icon.setImageDrawable(context.getDrawable(mIconResId));
1156 }
1157 if (mMessage != null) {
1158 messageView.setText(mMessage);
1159 } else {
1160 messageView.setText(mMessageResId);
1161 }
1162
1163 return v;
1164 }
1165 }
1166
1167 /**
1168 * A toggle action knows whether it is on or off, and displays an icon
1169 * and status message accordingly.
1170 */
1171 private static abstract class ToggleAction implements Action {
1172
1173 enum State {
1174 Off(false),
1175 TurningOn(true),
1176 TurningOff(true),
1177 On(false);
1178
1179 private final boolean inTransition;
1180
1181 State(boolean intermediate) {
1182 inTransition = intermediate;
1183 }
1184
1185 public boolean inTransition() {
1186 return inTransition;
1187 }
1188 }
1189
1190 protected State mState = State.Off;
1191
1192 // prefs
1193 protected int mEnabledIconResId;
1194 protected int mDisabledIconResid;
1195 protected int mMessageResId;
1196 protected int mEnabledStatusMessageResId;
1197 protected int mDisabledStatusMessageResId;
1198
1199 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001200 * @param enabledIconResId The icon for when this action is on.
1201 * @param disabledIconResid The icon for when this action is off.
1202 * @param message The general information message, e.g 'Silent Mode'
1203 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001204 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1205 */
1206 public ToggleAction(int enabledIconResId,
1207 int disabledIconResid,
1208 int message,
1209 int enabledStatusMessageResId,
1210 int disabledStatusMessageResId) {
1211 mEnabledIconResId = enabledIconResId;
1212 mDisabledIconResid = disabledIconResid;
1213 mMessageResId = message;
1214 mEnabledStatusMessageResId = enabledStatusMessageResId;
1215 mDisabledStatusMessageResId = disabledStatusMessageResId;
1216 }
1217
1218 /**
1219 * Override to make changes to resource IDs just before creating the
1220 * View.
1221 */
1222 void willCreate() {
1223
1224 }
1225
1226 @Override
1227 public CharSequence getLabelForAccessibility(Context context) {
1228 return context.getString(mMessageResId);
1229 }
1230
1231 public View create(Context context, View convertView, ViewGroup parent,
1232 LayoutInflater inflater) {
1233 willCreate();
1234
1235 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001236 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001237
1238 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1239 TextView messageView = (TextView) v.findViewById(R.id.message);
1240 TextView statusView = (TextView) v.findViewById(R.id.status);
1241 final boolean enabled = isEnabled();
1242
1243 if (messageView != null) {
1244 messageView.setText(mMessageResId);
1245 messageView.setEnabled(enabled);
1246 }
1247
1248 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1249 if (icon != null) {
1250 icon.setImageDrawable(context.getDrawable(
1251 (on ? mEnabledIconResId : mDisabledIconResid)));
1252 icon.setEnabled(enabled);
1253 }
1254
1255 if (statusView != null) {
1256 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1257 statusView.setVisibility(View.VISIBLE);
1258 statusView.setEnabled(enabled);
1259 }
1260 v.setEnabled(enabled);
1261
1262 return v;
1263 }
1264
1265 public final void onPress() {
1266 if (mState.inTransition()) {
1267 Log.w(TAG, "shouldn't be able to toggle when in transition");
1268 return;
1269 }
1270
1271 final boolean nowOn = !(mState == State.On);
1272 onToggle(nowOn);
1273 changeStateFromPress(nowOn);
1274 }
1275
1276 public boolean isEnabled() {
1277 return !mState.inTransition();
1278 }
1279
1280 /**
1281 * Implementations may override this if their state can be in on of the intermediate
1282 * states until some notification is received (e.g airplane mode is 'turning off' until
1283 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001284 *
Jason Monk361915c2017-03-21 20:33:59 -04001285 * @param buttonOn Whether the button was turned on or off
1286 */
1287 protected void changeStateFromPress(boolean buttonOn) {
1288 mState = buttonOn ? State.On : State.Off;
1289 }
1290
1291 abstract void onToggle(boolean on);
1292
1293 public void updateState(State state) {
1294 mState = state;
1295 }
1296 }
1297
1298 private class SilentModeToggleAction extends ToggleAction {
1299 public SilentModeToggleAction() {
1300 super(R.drawable.ic_audio_vol_mute,
1301 R.drawable.ic_audio_vol,
1302 R.string.global_action_toggle_silent_mode,
1303 R.string.global_action_silent_mode_on_status,
1304 R.string.global_action_silent_mode_off_status);
1305 }
1306
1307 void onToggle(boolean on) {
1308 if (on) {
1309 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1310 } else {
1311 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1312 }
1313 }
1314
1315 public boolean showDuringKeyguard() {
1316 return true;
1317 }
1318
1319 public boolean showBeforeProvisioning() {
1320 return false;
1321 }
1322 }
1323
1324 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1325
Jason Monk16fbd9d2017-04-27 14:28:49 -04001326 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001327
1328 private final AudioManager mAudioManager;
1329 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001330
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001331 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001332 mAudioManager = audioManager;
1333 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001334 }
1335
1336 private int ringerModeToIndex(int ringerMode) {
1337 // They just happen to coincide
1338 return ringerMode;
1339 }
1340
1341 private int indexToRingerMode(int index) {
1342 // They just happen to coincide
1343 return index;
1344 }
1345
1346 @Override
1347 public CharSequence getLabelForAccessibility(Context context) {
1348 return null;
1349 }
1350
1351 public View create(Context context, View convertView, ViewGroup parent,
1352 LayoutInflater inflater) {
1353 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1354
1355 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1356 for (int i = 0; i < 3; i++) {
1357 View itemView = v.findViewById(ITEM_IDS[i]);
1358 itemView.setSelected(selectedIndex == i);
1359 // Set up click handler
1360 itemView.setTag(i);
1361 itemView.setOnClickListener(this);
1362 }
1363 return v;
1364 }
1365
1366 public void onPress() {
1367 }
1368
1369 public boolean showDuringKeyguard() {
1370 return true;
1371 }
1372
1373 public boolean showBeforeProvisioning() {
1374 return false;
1375 }
1376
1377 public boolean isEnabled() {
1378 return true;
1379 }
1380
1381 void willCreate() {
1382 }
1383
1384 public void onClick(View v) {
1385 if (!(v.getTag() instanceof Integer)) return;
1386
1387 int index = (Integer) v.getTag();
1388 mAudioManager.setRingerMode(indexToRingerMode(index));
1389 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1390 }
1391 }
1392
1393 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1394 public void onReceive(Context context, Intent intent) {
1395 String action = intent.getAction();
1396 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1397 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1398 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1399 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001400 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001401 }
1402 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1403 // Airplane mode can be changed after ECM exits if airplane toggle button
1404 // is pressed during ECM mode
1405 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1406 mIsWaitingForEcmExit) {
1407 mIsWaitingForEcmExit = false;
1408 changeAirplaneModeSystemSetting(true);
1409 }
1410 }
1411 }
1412 };
1413
1414 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1415 @Override
1416 public void onServiceStateChanged(ServiceState serviceState) {
1417 if (!mHasTelephony) return;
1418 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1419 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1420 mAirplaneModeOn.updateState(mAirplaneState);
1421 mAdapter.notifyDataSetChanged();
1422 }
1423 };
1424
1425 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1426 @Override
1427 public void onReceive(Context context, Intent intent) {
1428 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1429 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1430 }
1431 }
1432 };
1433
1434 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1435 @Override
1436 public void onChange(boolean selfChange) {
1437 onAirplaneModeChanged();
1438 }
1439 };
1440
1441 private static final int MESSAGE_DISMISS = 0;
1442 private static final int MESSAGE_REFRESH = 1;
1443 private static final int MESSAGE_SHOW = 2;
1444 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1445
1446 private Handler mHandler = new Handler() {
1447 public void handleMessage(Message msg) {
1448 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001449 case MESSAGE_DISMISS:
1450 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001451 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1452 mDialog.dismissImmediately();
1453 } else {
1454 mDialog.dismiss();
1455 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001456 mDialog = null;
1457 }
1458 break;
1459 case MESSAGE_REFRESH:
1460 refreshSilentMode();
1461 mAdapter.notifyDataSetChanged();
1462 break;
1463 case MESSAGE_SHOW:
1464 handleShow();
1465 break;
Jason Monk361915c2017-03-21 20:33:59 -04001466 }
1467 }
1468 };
1469
1470 private void onAirplaneModeChanged() {
1471 // Let the service state callbacks handle the state.
1472 if (mHasTelephony) return;
1473
1474 boolean airplaneModeOn = Settings.Global.getInt(
1475 mContext.getContentResolver(),
1476 Settings.Global.AIRPLANE_MODE_ON,
1477 0) == 1;
1478 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1479 mAirplaneModeOn.updateState(mAirplaneState);
1480 }
1481
1482 /**
1483 * Change the airplane mode system setting
1484 */
1485 private void changeAirplaneModeSystemSetting(boolean on) {
1486 Settings.Global.putInt(
1487 mContext.getContentResolver(),
1488 Settings.Global.AIRPLANE_MODE_ON,
1489 on ? 1 : 0);
1490 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1491 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1492 intent.putExtra("state", on);
1493 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1494 if (!mHasTelephony) {
1495 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1496 }
1497 }
1498
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001499 private static final class ActionsDialog extends Dialog implements DialogInterface,
1500 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001501
Jason Monk16fbd9d2017-04-27 14:28:49 -04001502 private final Context mContext;
1503 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001504 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001505 private Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001506 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001507 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001508 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001509 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001510 private float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001511
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001512 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001513 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001514 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001515 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001516 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001517 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001518
1519 // Window initialization
1520 Window window = getWindow();
1521 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001522 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1523 window.getDecorView();
1524 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1525 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1526 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1527 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1528 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001529 window.addFlags(
1530 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001531 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001532 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001533 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1534 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1535 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001536 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001537 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001538
1539 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001540 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001541 }
1542
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001543 private boolean initializePanel() {
1544 if (!isPanelEnabled(mContext) || mPanelController == null) {
1545 return false;
Steve Elliott9b87a442019-03-05 10:24:16 -05001546 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001547 View panelView = mPanelController.getPanelContent();
1548 if (panelView == null) {
1549 return false;
1550 }
1551 FrameLayout panelContainer = new FrameLayout(mContext);
1552 FrameLayout.LayoutParams panelParams =
1553 new FrameLayout.LayoutParams(
1554 FrameLayout.LayoutParams.MATCH_PARENT,
1555 FrameLayout.LayoutParams.WRAP_CONTENT);
1556 panelContainer.addView(panelView, panelParams);
1557 addContentView(
1558 panelContainer,
1559 new ViewGroup.LayoutParams(
1560 ViewGroup.LayoutParams.MATCH_PARENT,
1561 ViewGroup.LayoutParams.MATCH_PARENT));
1562 return true;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001563 }
1564
1565 private void initializeLayout() {
1566 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001567 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001568 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001569 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001570 @Override
1571 public boolean dispatchPopulateAccessibilityEvent(
1572 View host, AccessibilityEvent event) {
1573 // Populate the title here, just as Activity does
1574 event.getText().add(mContext.getString(R.string.global_actions));
1575 return true;
1576 }
1577 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001578 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001579 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001580
1581 boolean panelEnabled = initializePanel();
1582 if (!panelEnabled) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001583 if (mBackgroundDrawable == null) {
1584 mBackgroundDrawable = new GradientDrawable(mContext);
1585 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001586 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1587 } else {
1588 mBackgroundDrawable = mContext.getDrawable(
1589 com.android.systemui.R.drawable.global_action_panel_scrim);
1590 mScrimAlpha = 1f;
1591 }
1592 mGlobalActionsLayout.setSnapToEdge(panelEnabled);
1593 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001594 }
1595
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001596 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001597 if (isGridEnabled(context)) {
1598 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1599 return com.android.systemui.R.layout.global_actions_grid_seascape;
1600 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001601 return com.android.systemui.R.layout.global_actions_grid;
1602 }
1603 return com.android.systemui.R.layout.global_actions_wrapped;
1604 }
1605
Jason Monk361915c2017-03-21 20:33:59 -04001606 @Override
1607 protected void onStart() {
1608 super.setCanceledOnTouchOutside(true);
1609 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001610 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001611
Steve Elliott9b87a442019-03-05 10:24:16 -05001612 if (mBackgroundDrawable instanceof GradientDrawable) {
1613 Point displaySize = new Point();
1614 mContext.getDisplay().getRealSize(displaySize);
1615 mColorExtractor.addOnColorsChangedListener(this);
1616 ((GradientDrawable) mBackgroundDrawable)
1617 .setScreenSize(displaySize.x, displaySize.y);
1618 GradientColors colors = mColorExtractor.getColors(
1619 mKeyguardShowing
1620 ? WallpaperManager.FLAG_LOCK
1621 : WallpaperManager.FLAG_SYSTEM);
1622 updateColors(colors, false /* animate */);
1623 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001624 }
1625
1626 /**
1627 * Updates background and system bars according to current GradientColors.
1628 * @param colors Colors and hints to use.
1629 * @param animate Interpolates gradient if true, just sets otherwise.
1630 */
1631 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001632 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1633 return;
1634 }
1635 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001636 View decorView = getWindow().getDecorView();
1637 if (colors.supportsDarkText()) {
1638 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001639 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001640 } else {
1641 decorView.setSystemUiVisibility(0);
1642 }
Jason Monk361915c2017-03-21 20:33:59 -04001643 }
1644
1645 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001646 protected void onStop() {
1647 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001648 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001649 }
1650
1651 @Override
1652 public void show() {
1653 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001654 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001655 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001656 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1657 mGlobalActionsLayout.setAlpha(0);
1658 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001659 .alpha(1)
1660 .translationX(0)
1661 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001662 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001663 .setUpdateListener(animation -> {
1664 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001665 * mScrimAlpha * 255);
1666 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001667 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001668 .start();
1669 }
1670
1671 @Override
1672 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001673 if (!mShowing) {
1674 return;
1675 }
1676 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001677 mGlobalActionsLayout.setTranslationX(0);
1678 mGlobalActionsLayout.setAlpha(1);
1679 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001680 .alpha(0)
1681 .translationX(getAnimTranslation())
1682 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001683 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001684 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001685 .setUpdateListener(animation -> {
1686 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001687 * mScrimAlpha * 255);
1688 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001689 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001690 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001691 if (mPanelController != null) {
1692 mPanelController.onDismissed();
1693 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001694 }
1695
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001696 void dismissImmediately() {
1697 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001698 mShowing = false;
Steve Elliott2d206d32019-03-14 16:30:33 -04001699 if (mPanelController != null) {
1700 mPanelController.onDismissed();
1701 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001702 }
1703
Jason Monk16fbd9d2017-04-27 14:28:49 -04001704 private float getAnimTranslation() {
1705 return getContext().getResources().getDimension(
1706 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001707 }
1708
1709 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001710 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001711 if (mKeyguardShowing) {
1712 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001713 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1714 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001715 }
1716 } else {
1717 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001718 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1719 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001720 }
1721 }
1722 }
1723
1724 public void setKeyguardShowing(boolean keyguardShowing) {
1725 mKeyguardShowing = keyguardShowing;
1726 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001727
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001728 public void refreshDialog() {
1729 initializeLayout();
1730 mGlobalActionsLayout.updateList();
1731 }
1732
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001733 public void onRotate(int from, int to) {
1734 if (mShowing && isGridEnabled(mContext)) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001735 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001736 }
1737 }
Jason Monk361915c2017-03-21 20:33:59 -04001738 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001739
1740 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001741 * Determines whether or not the Global Actions menu should use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001742 */
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001743 private static boolean isGridEnabled(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001744 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1745 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001746
1747 /**
1748 * Determines whether or not the Global Actions Panel should appear when the power button
1749 * is held.
1750 */
1751 private static boolean isPanelEnabled(Context context) {
1752 return FeatureFlagUtils.isEnabled(
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001753 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1754 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001755
1756 /**
1757 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1758 */
1759 private static boolean shouldUseSeparatedView() {
1760 return true;
1761 }
Jason Monk361915c2017-03-21 20:33:59 -04001762}