blob: e128531cf49dddab300c52a6dfd9093e9f553d23 [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Steve Elliott4c868852019-03-14 16:25:41 -040027import android.app.PendingIntent;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070028import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000029import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010030import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040031import android.content.BroadcastReceiver;
32import android.content.Context;
33import android.content.DialogInterface;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.UserInfo;
37import android.database.ContentObserver;
38import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.net.ConnectivityManager;
Jason Monk361915c2017-03-21 20:33:59 -040041import android.os.Handler;
42import android.os.Message;
43import android.os.RemoteException;
44import android.os.ServiceManager;
45import android.os.SystemProperties;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.os.Vibrator;
49import android.provider.Settings;
50import android.service.dreams.DreamService;
51import android.service.dreams.IDreamManager;
52import android.telephony.PhoneStateListener;
53import android.telephony.ServiceState;
54import android.telephony.TelephonyManager;
55import android.text.TextUtils;
56import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080057import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040058import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070059import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040060import android.view.LayoutInflater;
61import android.view.View;
62import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070063import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.view.WindowManager;
65import android.view.WindowManagerGlobal;
66import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050067import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040068import android.widget.ImageView;
69import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.widget.TextView;
71
Charles He9851a8d2017-10-10 17:31:30 +010072import com.android.internal.R;
73import com.android.internal.colorextraction.ColorExtractor;
74import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070075import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010076import com.android.internal.logging.MetricsLogger;
77import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
78import com.android.internal.telephony.TelephonyIntents;
79import com.android.internal.telephony.TelephonyProperties;
80import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040081import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050082import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010083import com.android.internal.widget.LockPatternUtils;
84import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050086import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050087import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040089import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050091import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050092import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070093import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080094import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050095import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050096import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070097
Jason Monk361915c2017-03-21 20:33:59 -040098import java.util.ArrayList;
99import java.util.List;
100
101/**
102 * Helper to show the global actions dialog. Each item is an {@link Action} that
103 * may show depending on whether the keyguard is showing, and whether the device
104 * is provisioned.
105 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500106public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
107 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400108
109 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
110 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700111 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400112
113 private static final String TAG = "GlobalActionsDialog";
114
115 private static final boolean SHOW_SILENT_TOGGLE = true;
116
117 /* Valid settings for global actions keys.
118 * see config.xml config_globalActionList */
119 private static final String GLOBAL_ACTION_KEY_POWER = "power";
120 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
121 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
122 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
123 private static final String GLOBAL_ACTION_KEY_USERS = "users";
124 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
125 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
126 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
127 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
128 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000129 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800130 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500131 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400132
133 private final Context mContext;
134 private final GlobalActionsManager mWindowManagerFuncs;
135 private final AudioManager mAudioManager;
136 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000137 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800138 private final LockPatternUtils mLockPatternUtils;
139 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400140
141 private ArrayList<Action> mItems;
142 private ActionsDialog mDialog;
143
144 private Action mSilentModeAction;
145 private ToggleAction mAirplaneModeOn;
146
147 private MyAdapter mAdapter;
148
149 private boolean mKeyguardShowing = false;
150 private boolean mDeviceProvisioned = false;
151 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
152 private boolean mIsWaitingForEcmExit = false;
153 private boolean mHasTelephony;
154 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000155 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800156 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400157 private final boolean mShowSilentToggle;
158 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500159 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400160 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400161 private final ActivityStarter mActivityStarter;
162 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500163
Jason Monk361915c2017-03-21 20:33:59 -0400164 /**
165 * @param context everything needs a context :(
166 */
167 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700168 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400169 mWindowManagerFuncs = windowManagerFuncs;
170 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
171 mDreamManager = IDreamManager.Stub.asInterface(
172 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000173 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
174 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800175 mLockPatternUtils = new LockPatternUtils(mContext);
176 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400177
178 // receive broadcasts
179 IntentFilter filter = new IntentFilter();
180 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
181 filter.addAction(Intent.ACTION_SCREEN_OFF);
182 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
183 context.registerReceiver(mBroadcastReceiver, filter);
184
185 ConnectivityManager cm = (ConnectivityManager)
186 context.getSystemService(Context.CONNECTIVITY_SERVICE);
187 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
188
189 // get notified of phone state changes
190 TelephonyManager telephonyManager =
191 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
192 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
193 mContext.getContentResolver().registerContentObserver(
194 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
195 mAirplaneModeObserver);
196 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
197 mHasVibrator = vibrator != null && vibrator.hasVibrator();
198
199 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
200 R.bool.config_useFixedVolume);
201
202 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500203 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400204 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700205
206 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500207
Steve Elliott4c868852019-03-14 16:25:41 -0400208 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400209 }
210
211 /**
212 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400213 *
Jason Monk361915c2017-03-21 20:33:59 -0400214 * @param keyguardShowing True if keyguard is showing
215 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400216 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
217 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400218 mKeyguardShowing = keyguardShowing;
219 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400220 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400221 if (mDialog != null) {
222 mDialog.dismiss();
223 mDialog = null;
224 // Show delayed, so that the dismiss of the previous dialog completes
225 mHandler.sendEmptyMessage(MESSAGE_SHOW);
226 } else {
227 handleShow();
228 }
229 }
230
Charles He9851a8d2017-10-10 17:31:30 +0100231 /**
232 * Dismiss the global actions dialog, if it's currently shown
233 */
234 public void dismissDialog() {
235 mHandler.removeMessages(MESSAGE_DISMISS);
236 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
237 }
238
Jason Monk361915c2017-03-21 20:33:59 -0400239 private void awakenIfNecessary() {
240 if (mDreamManager != null) {
241 try {
242 if (mDreamManager.isDreaming()) {
243 mDreamManager.awaken();
244 }
245 } catch (RemoteException e) {
246 // we tried
247 }
248 }
249 }
250
251 private void handleShow() {
252 awakenIfNecessary();
253 mDialog = createDialog();
254 prepareDialog();
255
256 // If we only have 1 item and it's a simple press action, just do this action.
257 if (mAdapter.getCount() == 1
258 && mAdapter.getItem(0) instanceof SinglePressAction
259 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
260 ((SinglePressAction) mAdapter.getItem(0)).onPress();
261 } else {
262 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
263 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100264 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400265 mDialog.getWindow().setAttributes(attrs);
266 mDialog.show();
267 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400268 }
269 }
270
271 /**
272 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400273 *
Jason Monk361915c2017-03-21 20:33:59 -0400274 * @return A new dialog.
275 */
276 private ActionsDialog createDialog() {
277 // Simple toggle style if there's no vibrator, otherwise use a tri-state
278 if (!mHasVibrator) {
279 mSilentModeAction = new SilentModeToggleAction();
280 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700281 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400282 }
283 mAirplaneModeOn = new ToggleAction(
284 R.drawable.ic_lock_airplane_mode,
285 R.drawable.ic_lock_airplane_mode_off,
286 R.string.global_actions_toggle_airplane_mode,
287 R.string.global_actions_airplane_mode_on_status,
288 R.string.global_actions_airplane_mode_off_status) {
289
290 void onToggle(boolean on) {
291 if (mHasTelephony && Boolean.parseBoolean(
292 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
293 mIsWaitingForEcmExit = true;
294 // Launch ECM exit dialog
295 Intent ecmDialogIntent =
296 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
297 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
298 mContext.startActivity(ecmDialogIntent);
299 } else {
300 changeAirplaneModeSystemSetting(on);
301 }
302 }
303
304 @Override
305 protected void changeStateFromPress(boolean buttonOn) {
306 if (!mHasTelephony) return;
307
308 // In ECM mode airplane state cannot be changed
309 if (!(Boolean.parseBoolean(
310 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
311 mState = buttonOn ? State.TurningOn : State.TurningOff;
312 mAirplaneState = mState;
313 }
314 }
315
316 public boolean showDuringKeyguard() {
317 return true;
318 }
319
320 public boolean showBeforeProvisioning() {
321 return false;
322 }
323 };
324 onAirplaneModeChanged();
325
326 mItems = new ArrayList<Action>();
327 String[] defaultActions = mContext.getResources().getStringArray(
328 R.array.config_globalActionsList);
329
330 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000331 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800332 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400333 for (int i = 0; i < defaultActions.length; i++) {
334 String actionKey = defaultActions[i];
335 if (addedKeys.contains(actionKey)) {
336 // If we already have added this, don't add it again.
337 continue;
338 }
339 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
340 mItems.add(new PowerAction());
341 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
342 mItems.add(mAirplaneModeOn);
343 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
344 if (Settings.Global.getInt(mContext.getContentResolver(),
345 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
346 mItems.add(new BugReportAction());
347 }
348 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
349 if (mShowSilentToggle) {
350 mItems.add(mSilentModeAction);
351 }
352 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
353 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
354 addUsersToMenu(mItems);
355 }
356 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
357 mItems.add(getSettingsAction());
358 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700359 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
360 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800361 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700362 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800363 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700364 }
Jason Monk361915c2017-03-21 20:33:59 -0400365 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
366 mItems.add(getVoiceAssistAction());
367 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
368 mItems.add(getAssistAction());
369 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
370 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500371 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
372 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000373 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000374 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000375 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
376 mItems.add(new LogoutAction());
377 mHasLogoutButton = true;
378 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800379 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400380 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800381 mItems.add(new EmergencyDialerAction());
382 }
Jason Monk361915c2017-03-21 20:33:59 -0400383 } else {
384 Log.e(TAG, "Invalid global action key " + actionKey);
385 }
386 // Add here so we don't add more than one.
387 addedKeys.add(actionKey);
388 }
389
390 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400391 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400392 }
393
394 mAdapter = new MyAdapter();
395
Steve Elliott9b87a442019-03-05 10:24:16 -0500396 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400397 mPanelPlugin != null
398 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400399 new GlobalActionsPanelPlugin.Callbacks() {
400 @Override
401 public void dismissGlobalActionsMenu() {
402 if (mDialog != null) {
403 mDialog.dismiss();
404 }
405 }
406
407 @Override
408 public void startPendingIntentDismissingKeyguard(
409 PendingIntent intent) {
410 mActivityStarter
411 .startPendingIntentDismissingKeyguard(intent);
412 }
413 })
Steve Elliott9b87a442019-03-05 10:24:16 -0500414 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400415 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400416 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700417 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400418
419 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800420 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400421
422 return dialog;
423 }
424
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800425 private boolean shouldDisplayLockdown() {
426 int userId = getCurrentUser().id;
427 // Lockdown is meaningless without a place to go.
428 if (!mKeyguardManager.isDeviceSecure(userId)) {
429 return false;
430 }
431
432 // Only show the lockdown button if the device isn't locked down (for whatever reason).
433 int state = mLockPatternUtils.getStrongAuthForUser(userId);
434 return (state == STRONG_AUTH_NOT_REQUIRED
435 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
436 }
437
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700438 @Override
439 public void onUiModeChanged() {
440 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700441 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400442 mDialog.refreshDialog();
443 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700444 }
445
446 public void destroy() {
447 Dependency.get(ConfigurationController.class).removeCallback(this);
448 }
449
Jason Monk361915c2017-03-21 20:33:59 -0400450 private final class PowerAction extends SinglePressAction implements LongPressAction {
451 private PowerAction() {
452 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400453 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400454 }
455
456 @Override
457 public boolean onLongPress() {
458 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
459 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
460 mWindowManagerFuncs.reboot(true);
461 return true;
462 }
463 return false;
464 }
465
466 @Override
467 public boolean showDuringKeyguard() {
468 return true;
469 }
470
471 @Override
472 public boolean showBeforeProvisioning() {
473 return true;
474 }
475
476 @Override
477 public void onPress() {
478 // shutdown by making sure radio and power are handled accordingly.
479 mWindowManagerFuncs.shutdown();
480 }
481 }
482
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400483 private abstract class EmergencyAction extends SinglePressAction {
484 EmergencyAction(int iconResId, int messageResId) {
485 super(iconResId, messageResId);
486 }
487
488 @Override
489 public boolean shouldBeSeparated() {
490 return shouldUseSeparatedView();
491 }
492
493 @Override
494 public View create(
495 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
496 View v = super.create(context, convertView, parent, inflater);
497 int textColor;
498 if (shouldBeSeparated()) {
499 textColor = v.getResources().getColor(
500 com.android.systemui.R.color.global_actions_alert_text);
501 } else {
502 textColor = v.getResources().getColor(
503 com.android.systemui.R.color.global_actions_text);
504 }
505 TextView messageView = v.findViewById(R.id.message);
506 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400507 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400508 ImageView icon = (ImageView) v.findViewById(R.id.icon);
509 icon.getDrawable().setTint(textColor);
510 return v;
511 }
512
513 @Override
514 public boolean showDuringKeyguard() {
515 return true;
516 }
517
518 @Override
519 public boolean showBeforeProvisioning() {
520 return true;
521 }
522 }
523
524 private class EmergencyAffordanceAction extends EmergencyAction {
525 EmergencyAffordanceAction() {
526 super(R.drawable.emergency_icon,
527 R.string.global_action_emergency);
528 }
529
530 @Override
531 public void onPress() {
532 mEmergencyAffordanceManager.performEmergencyCall();
533 }
534 }
535
536 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800537 private EmergencyDialerAction() {
538 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800539 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800540 }
541
542 @Override
543 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800544 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800545 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800546 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
547 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
548 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800549 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
550 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800551 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
552 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800553 }
554
Jason Monk361915c2017-03-21 20:33:59 -0400555 private final class RestartAction extends SinglePressAction implements LongPressAction {
556 private RestartAction() {
557 super(R.drawable.ic_restart, R.string.global_action_restart);
558 }
559
560 @Override
561 public boolean onLongPress() {
562 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
563 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
564 mWindowManagerFuncs.reboot(true);
565 return true;
566 }
567 return false;
568 }
569
570 @Override
571 public boolean showDuringKeyguard() {
572 return true;
573 }
574
575 @Override
576 public boolean showBeforeProvisioning() {
577 return true;
578 }
579
580 @Override
581 public void onPress() {
582 mWindowManagerFuncs.reboot(false);
583 }
584 }
585
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400586 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500587 public ScreenshotAction() {
588 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
589 }
590
591 @Override
592 public void onPress() {
593 // Add a little delay before executing, to give the
594 // dialog a chance to go away before it takes a
595 // screenshot.
596 // TODO: instead, omit global action dialog layer
597 mHandler.postDelayed(new Runnable() {
598 @Override
599 public void run() {
600 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
601 MetricsLogger.action(mContext,
602 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
603 }
604 }, 500);
605 }
606
607 @Override
608 public boolean showDuringKeyguard() {
609 return true;
610 }
611
612 @Override
613 public boolean showBeforeProvisioning() {
614 return false;
615 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400616
617 @Override
618 public boolean onLongPress() {
619 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
620 mScreenRecordHelper.launchRecordPrompt();
621 } else {
622 onPress();
623 }
624 return true;
625 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500626 }
627
Jason Monk361915c2017-03-21 20:33:59 -0400628 private class BugReportAction extends SinglePressAction implements LongPressAction {
629
630 public BugReportAction() {
631 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
632 }
633
634 @Override
635 public void onPress() {
636 // don't actually trigger the bugreport if we are running stability
637 // tests via monkey
638 if (ActivityManager.isUserAMonkey()) {
639 return;
640 }
641 // Add a little delay before executing, to give the
642 // dialog a chance to go away before it takes a
643 // screenshot.
644 mHandler.postDelayed(new Runnable() {
645 @Override
646 public void run() {
647 try {
648 // Take an "interactive" bugreport.
649 MetricsLogger.action(mContext,
650 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
651 ActivityManager.getService().requestBugReport(
652 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
653 } catch (RemoteException e) {
654 }
655 }
656 }, 500);
657 }
658
659 @Override
660 public boolean onLongPress() {
661 // don't actually trigger the bugreport if we are running stability
662 // tests via monkey
663 if (ActivityManager.isUserAMonkey()) {
664 return false;
665 }
666 try {
667 // Take a "full" bugreport.
668 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
669 ActivityManager.getService().requestBugReport(
670 ActivityManager.BUGREPORT_OPTION_FULL);
671 } catch (RemoteException e) {
672 }
673 return false;
674 }
675
676 public boolean showDuringKeyguard() {
677 return true;
678 }
679
680 @Override
681 public boolean showBeforeProvisioning() {
682 return false;
683 }
Jason Monk361915c2017-03-21 20:33:59 -0400684 }
685
Alex Chau04458852017-11-27 18:21:23 +0000686 private final class LogoutAction extends SinglePressAction {
687 private LogoutAction() {
688 super(R.drawable.ic_logout, R.string.global_action_logout);
689 }
690
691 @Override
692 public boolean showDuringKeyguard() {
693 return true;
694 }
695
696 @Override
697 public boolean showBeforeProvisioning() {
698 return false;
699 }
700
701 @Override
702 public void onPress() {
703 // Add a little delay before executing, to give the dialog a chance to go away before
704 // switching user
705 mHandler.postDelayed(() -> {
706 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000707 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000708 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000709 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000710 } catch (RemoteException re) {
711 Log.e(TAG, "Couldn't logout user " + re);
712 }
713 }, 500);
714 }
715 }
716
Jason Monk361915c2017-03-21 20:33:59 -0400717 private Action getSettingsAction() {
718 return new SinglePressAction(R.drawable.ic_settings,
719 R.string.global_action_settings) {
720
721 @Override
722 public void onPress() {
723 Intent intent = new Intent(Settings.ACTION_SETTINGS);
724 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
725 mContext.startActivity(intent);
726 }
727
728 @Override
729 public boolean showDuringKeyguard() {
730 return true;
731 }
732
733 @Override
734 public boolean showBeforeProvisioning() {
735 return true;
736 }
737 };
738 }
739
Jason Monk361915c2017-03-21 20:33:59 -0400740 private Action getAssistAction() {
741 return new SinglePressAction(R.drawable.ic_action_assist_focused,
742 R.string.global_action_assist) {
743 @Override
744 public void onPress() {
745 Intent intent = new Intent(Intent.ACTION_ASSIST);
746 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
747 mContext.startActivity(intent);
748 }
749
750 @Override
751 public boolean showDuringKeyguard() {
752 return true;
753 }
754
755 @Override
756 public boolean showBeforeProvisioning() {
757 return true;
758 }
759 };
760 }
761
762 private Action getVoiceAssistAction() {
763 return new SinglePressAction(R.drawable.ic_voice_search,
764 R.string.global_action_voice_assist) {
765 @Override
766 public void onPress() {
767 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
768 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
769 mContext.startActivity(intent);
770 }
771
772 @Override
773 public boolean showDuringKeyguard() {
774 return true;
775 }
776
777 @Override
778 public boolean showBeforeProvisioning() {
779 return true;
780 }
781 };
782 }
783
784 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400785 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400786 R.string.global_action_lockdown) {
787
788 @Override
789 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700790 new LockPatternUtils(mContext)
791 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
792 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400793 try {
794 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100795 // Lock profiles (if any) on the background thread.
796 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
797 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400798 } catch (RemoteException e) {
799 Log.e(TAG, "Error while trying to lock device.", e);
800 }
801 }
802
803 @Override
804 public boolean showDuringKeyguard() {
805 return true;
806 }
807
808 @Override
809 public boolean showBeforeProvisioning() {
810 return false;
811 }
812 };
813 }
814
Pavel Grafov059021b2018-05-02 13:44:46 +0100815 private void lockProfiles() {
816 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
817 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
818 final int currentUserId = getCurrentUser().id;
819 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
820 for (final int id : profileIds) {
821 if (id != currentUserId) {
822 tm.setDeviceLockedForUser(id, true);
823 }
824 }
825 }
826
Jason Monk361915c2017-03-21 20:33:59 -0400827 private UserInfo getCurrentUser() {
828 try {
829 return ActivityManager.getService().getCurrentUser();
830 } catch (RemoteException re) {
831 return null;
832 }
833 }
834
835 private boolean isCurrentUserOwner() {
836 UserInfo currentUser = getCurrentUser();
837 return currentUser == null || currentUser.isPrimary();
838 }
839
840 private void addUsersToMenu(ArrayList<Action> items) {
841 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
842 if (um.isUserSwitcherEnabled()) {
843 List<UserInfo> users = um.getUsers();
844 UserInfo currentUser = getCurrentUser();
845 for (final UserInfo user : users) {
846 if (user.supportsSwitchToByUser()) {
847 boolean isCurrentUser = currentUser == null
848 ? user.id == 0 : (currentUser.id == user.id);
849 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
850 : null;
851 SinglePressAction switchToUser = new SinglePressAction(
852 R.drawable.ic_menu_cc, icon,
853 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400854 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400855 public void onPress() {
856 try {
857 ActivityManager.getService().switchUser(user.id);
858 } catch (RemoteException re) {
859 Log.e(TAG, "Couldn't switch user " + re);
860 }
861 }
862
863 public boolean showDuringKeyguard() {
864 return true;
865 }
866
867 public boolean showBeforeProvisioning() {
868 return false;
869 }
870 };
871 items.add(switchToUser);
872 }
873 }
874 }
875 }
876
877 private void prepareDialog() {
878 refreshSilentMode();
879 mAirplaneModeOn.updateState(mAirplaneState);
880 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400881 if (mShowSilentToggle) {
882 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
883 mContext.registerReceiver(mRingerModeReceiver, filter);
884 }
885 }
886
887 private void refreshSilentMode() {
888 if (!mHasVibrator) {
889 final boolean silentModeOn =
890 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400891 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400892 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
893 }
894 }
895
896 /** {@inheritDoc} */
897 public void onDismiss(DialogInterface dialog) {
898 mWindowManagerFuncs.onGlobalActionsHidden();
899 if (mShowSilentToggle) {
900 try {
901 mContext.unregisterReceiver(mRingerModeReceiver);
902 } catch (IllegalArgumentException ie) {
903 // ignore this
904 Log.w(TAG, ie);
905 }
906 }
907 }
908
909 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800910 public void onShow(DialogInterface dialog) {
911 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
912 }
913
Jason Monk361915c2017-03-21 20:33:59 -0400914 /**
915 * The adapter used for the list within the global actions dialog, taking
916 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400917 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
918 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400919 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
920 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500921 public class MyAdapter extends MultiListAdapter {
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 Heuckrothc7926612019-04-02 14:21:57 -04001130 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001131 }
1132
Jason Monk361915c2017-03-21 20:33:59 -04001133 public View create(
1134 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001135 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001136 false);
Jason Monk361915c2017-03-21 20:33:59 -04001137
1138 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1139 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001140 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001141
1142 TextView statusView = (TextView) v.findViewById(R.id.status);
1143 final String status = getStatus();
1144 if (!TextUtils.isEmpty(status)) {
1145 statusView.setText(status);
1146 } else {
1147 statusView.setVisibility(View.GONE);
1148 }
1149 if (mIcon != null) {
1150 icon.setImageDrawable(mIcon);
1151 icon.setScaleType(ScaleType.CENTER_CROP);
1152 } else if (mIconResId != 0) {
1153 icon.setImageDrawable(context.getDrawable(mIconResId));
1154 }
1155 if (mMessage != null) {
1156 messageView.setText(mMessage);
1157 } else {
1158 messageView.setText(mMessageResId);
1159 }
1160
1161 return v;
1162 }
1163 }
1164
1165 /**
1166 * A toggle action knows whether it is on or off, and displays an icon
1167 * and status message accordingly.
1168 */
1169 private static abstract class ToggleAction implements Action {
1170
1171 enum State {
1172 Off(false),
1173 TurningOn(true),
1174 TurningOff(true),
1175 On(false);
1176
1177 private final boolean inTransition;
1178
1179 State(boolean intermediate) {
1180 inTransition = intermediate;
1181 }
1182
1183 public boolean inTransition() {
1184 return inTransition;
1185 }
1186 }
1187
1188 protected State mState = State.Off;
1189
1190 // prefs
1191 protected int mEnabledIconResId;
1192 protected int mDisabledIconResid;
1193 protected int mMessageResId;
1194 protected int mEnabledStatusMessageResId;
1195 protected int mDisabledStatusMessageResId;
1196
1197 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001198 * @param enabledIconResId The icon for when this action is on.
1199 * @param disabledIconResid The icon for when this action is off.
1200 * @param message The general information message, e.g 'Silent Mode'
1201 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001202 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1203 */
1204 public ToggleAction(int enabledIconResId,
1205 int disabledIconResid,
1206 int message,
1207 int enabledStatusMessageResId,
1208 int disabledStatusMessageResId) {
1209 mEnabledIconResId = enabledIconResId;
1210 mDisabledIconResid = disabledIconResid;
1211 mMessageResId = message;
1212 mEnabledStatusMessageResId = enabledStatusMessageResId;
1213 mDisabledStatusMessageResId = disabledStatusMessageResId;
1214 }
1215
1216 /**
1217 * Override to make changes to resource IDs just before creating the
1218 * View.
1219 */
1220 void willCreate() {
1221
1222 }
1223
1224 @Override
1225 public CharSequence getLabelForAccessibility(Context context) {
1226 return context.getString(mMessageResId);
1227 }
1228
1229 public View create(Context context, View convertView, ViewGroup parent,
1230 LayoutInflater inflater) {
1231 willCreate();
1232
1233 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001234 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001235
1236 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1237 TextView messageView = (TextView) v.findViewById(R.id.message);
1238 TextView statusView = (TextView) v.findViewById(R.id.status);
1239 final boolean enabled = isEnabled();
1240
1241 if (messageView != null) {
1242 messageView.setText(mMessageResId);
1243 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001244 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001245 }
1246
1247 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1248 if (icon != null) {
1249 icon.setImageDrawable(context.getDrawable(
1250 (on ? mEnabledIconResId : mDisabledIconResid)));
1251 icon.setEnabled(enabled);
1252 }
1253
1254 if (statusView != null) {
1255 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1256 statusView.setVisibility(View.VISIBLE);
1257 statusView.setEnabled(enabled);
1258 }
1259 v.setEnabled(enabled);
1260
1261 return v;
1262 }
1263
1264 public final void onPress() {
1265 if (mState.inTransition()) {
1266 Log.w(TAG, "shouldn't be able to toggle when in transition");
1267 return;
1268 }
1269
1270 final boolean nowOn = !(mState == State.On);
1271 onToggle(nowOn);
1272 changeStateFromPress(nowOn);
1273 }
1274
1275 public boolean isEnabled() {
1276 return !mState.inTransition();
1277 }
1278
1279 /**
1280 * Implementations may override this if their state can be in on of the intermediate
1281 * states until some notification is received (e.g airplane mode is 'turning off' until
1282 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001283 *
Jason Monk361915c2017-03-21 20:33:59 -04001284 * @param buttonOn Whether the button was turned on or off
1285 */
1286 protected void changeStateFromPress(boolean buttonOn) {
1287 mState = buttonOn ? State.On : State.Off;
1288 }
1289
1290 abstract void onToggle(boolean on);
1291
1292 public void updateState(State state) {
1293 mState = state;
1294 }
1295 }
1296
1297 private class SilentModeToggleAction extends ToggleAction {
1298 public SilentModeToggleAction() {
1299 super(R.drawable.ic_audio_vol_mute,
1300 R.drawable.ic_audio_vol,
1301 R.string.global_action_toggle_silent_mode,
1302 R.string.global_action_silent_mode_on_status,
1303 R.string.global_action_silent_mode_off_status);
1304 }
1305
1306 void onToggle(boolean on) {
1307 if (on) {
1308 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1309 } else {
1310 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1311 }
1312 }
1313
1314 public boolean showDuringKeyguard() {
1315 return true;
1316 }
1317
1318 public boolean showBeforeProvisioning() {
1319 return false;
1320 }
1321 }
1322
1323 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1324
Jason Monk16fbd9d2017-04-27 14:28:49 -04001325 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001326
1327 private final AudioManager mAudioManager;
1328 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001329
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001330 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001331 mAudioManager = audioManager;
1332 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001333 }
1334
1335 private int ringerModeToIndex(int ringerMode) {
1336 // They just happen to coincide
1337 return ringerMode;
1338 }
1339
1340 private int indexToRingerMode(int index) {
1341 // They just happen to coincide
1342 return index;
1343 }
1344
1345 @Override
1346 public CharSequence getLabelForAccessibility(Context context) {
1347 return null;
1348 }
1349
1350 public View create(Context context, View convertView, ViewGroup parent,
1351 LayoutInflater inflater) {
1352 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1353
1354 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1355 for (int i = 0; i < 3; i++) {
1356 View itemView = v.findViewById(ITEM_IDS[i]);
1357 itemView.setSelected(selectedIndex == i);
1358 // Set up click handler
1359 itemView.setTag(i);
1360 itemView.setOnClickListener(this);
1361 }
1362 return v;
1363 }
1364
1365 public void onPress() {
1366 }
1367
1368 public boolean showDuringKeyguard() {
1369 return true;
1370 }
1371
1372 public boolean showBeforeProvisioning() {
1373 return false;
1374 }
1375
1376 public boolean isEnabled() {
1377 return true;
1378 }
1379
1380 void willCreate() {
1381 }
1382
1383 public void onClick(View v) {
1384 if (!(v.getTag() instanceof Integer)) return;
1385
1386 int index = (Integer) v.getTag();
1387 mAudioManager.setRingerMode(indexToRingerMode(index));
1388 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1389 }
1390 }
1391
1392 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1393 public void onReceive(Context context, Intent intent) {
1394 String action = intent.getAction();
1395 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1396 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1397 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1398 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001399 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001400 }
1401 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1402 // Airplane mode can be changed after ECM exits if airplane toggle button
1403 // is pressed during ECM mode
1404 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1405 mIsWaitingForEcmExit) {
1406 mIsWaitingForEcmExit = false;
1407 changeAirplaneModeSystemSetting(true);
1408 }
1409 }
1410 }
1411 };
1412
1413 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1414 @Override
1415 public void onServiceStateChanged(ServiceState serviceState) {
1416 if (!mHasTelephony) return;
1417 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1418 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1419 mAirplaneModeOn.updateState(mAirplaneState);
1420 mAdapter.notifyDataSetChanged();
1421 }
1422 };
1423
1424 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1425 @Override
1426 public void onReceive(Context context, Intent intent) {
1427 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1428 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1429 }
1430 }
1431 };
1432
1433 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1434 @Override
1435 public void onChange(boolean selfChange) {
1436 onAirplaneModeChanged();
1437 }
1438 };
1439
1440 private static final int MESSAGE_DISMISS = 0;
1441 private static final int MESSAGE_REFRESH = 1;
1442 private static final int MESSAGE_SHOW = 2;
1443 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1444
1445 private Handler mHandler = new Handler() {
1446 public void handleMessage(Message msg) {
1447 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001448 case MESSAGE_DISMISS:
1449 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001450 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1451 mDialog.dismissImmediately();
1452 } else {
1453 mDialog.dismiss();
1454 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001455 mDialog = null;
1456 }
1457 break;
1458 case MESSAGE_REFRESH:
1459 refreshSilentMode();
1460 mAdapter.notifyDataSetChanged();
1461 break;
1462 case MESSAGE_SHOW:
1463 handleShow();
1464 break;
Jason Monk361915c2017-03-21 20:33:59 -04001465 }
1466 }
1467 };
1468
1469 private void onAirplaneModeChanged() {
1470 // Let the service state callbacks handle the state.
1471 if (mHasTelephony) return;
1472
1473 boolean airplaneModeOn = Settings.Global.getInt(
1474 mContext.getContentResolver(),
1475 Settings.Global.AIRPLANE_MODE_ON,
1476 0) == 1;
1477 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1478 mAirplaneModeOn.updateState(mAirplaneState);
1479 }
1480
1481 /**
1482 * Change the airplane mode system setting
1483 */
1484 private void changeAirplaneModeSystemSetting(boolean on) {
1485 Settings.Global.putInt(
1486 mContext.getContentResolver(),
1487 Settings.Global.AIRPLANE_MODE_ON,
1488 on ? 1 : 0);
1489 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1490 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1491 intent.putExtra("state", on);
1492 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1493 if (!mHasTelephony) {
1494 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1495 }
1496 }
1497
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001498 private static final class ActionsDialog extends Dialog implements DialogInterface,
1499 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001500
Jason Monk16fbd9d2017-04-27 14:28:49 -04001501 private final Context mContext;
1502 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001503 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001504 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001505 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001506 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001507 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001508 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001509 private float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001510
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001511 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001512 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001513 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001514 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001515 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001516 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001517
1518 // Window initialization
1519 Window window = getWindow();
1520 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001521 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1522 window.getDecorView();
1523 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1524 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1525 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1526 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1527 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001528 window.addFlags(
1529 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001530 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001531 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001532 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1533 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1534 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001535 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001536 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001537
1538 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001539 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001540 }
1541
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001542 private boolean shouldUsePanel() {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001543 if (!isPanelEnabled(mContext) || mPanelController == null) {
1544 return false;
Steve Elliott9b87a442019-03-05 10:24:16 -05001545 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001546 if (mPanelController.getPanelContent() == null) {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001547 return false;
1548 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001549 return true;
1550 }
1551
1552 private void initializePanel() {
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001553 FrameLayout panelContainer = new FrameLayout(mContext);
1554 FrameLayout.LayoutParams panelParams =
1555 new FrameLayout.LayoutParams(
1556 FrameLayout.LayoutParams.MATCH_PARENT,
1557 FrameLayout.LayoutParams.WRAP_CONTENT);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001558 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001559 addContentView(
1560 panelContainer,
1561 new ViewGroup.LayoutParams(
1562 ViewGroup.LayoutParams.MATCH_PARENT,
1563 ViewGroup.LayoutParams.MATCH_PARENT));
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001564 }
1565
1566 private void initializeLayout() {
1567 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001568 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001569 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001570 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001571 @Override
1572 public boolean dispatchPopulateAccessibilityEvent(
1573 View host, AccessibilityEvent event) {
1574 // Populate the title here, just as Activity does
1575 event.getText().add(mContext.getString(R.string.global_actions));
1576 return true;
1577 }
1578 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001579 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001580 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001581
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001582 if (!shouldUsePanel()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001583 if (mBackgroundDrawable == null) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001584 mBackgroundDrawable = new ScrimDrawable();
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001585 }
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;
Steve Elliottc59eb342019-04-23 16:59:06 -04001591 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001592 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001593 mGlobalActionsLayout.setSnapToEdge(true);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001594 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001595 }
1596
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001597 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001598 if (isForceGridEnabled(context) || shouldUsePanel()) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001599 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1600 return com.android.systemui.R.layout.global_actions_grid_seascape;
1601 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001602 return com.android.systemui.R.layout.global_actions_grid;
1603 }
1604 return com.android.systemui.R.layout.global_actions_wrapped;
1605 }
1606
Jason Monk361915c2017-03-21 20:33:59 -04001607 @Override
1608 protected void onStart() {
1609 super.setCanceledOnTouchOutside(true);
1610 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001611 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001612
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001613 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001614 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001615 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001616 updateColors(colors, false /* animate */);
1617 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001618 }
1619
1620 /**
1621 * Updates background and system bars according to current GradientColors.
1622 * @param colors Colors and hints to use.
1623 * @param animate Interpolates gradient if true, just sets otherwise.
1624 */
1625 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001626 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001627 return;
1628 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001629 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001630 View decorView = getWindow().getDecorView();
1631 if (colors.supportsDarkText()) {
1632 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001633 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001634 } else {
1635 decorView.setSystemUiVisibility(0);
1636 }
Jason Monk361915c2017-03-21 20:33:59 -04001637 }
1638
1639 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001640 protected void onStop() {
1641 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001642 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001643 }
1644
1645 @Override
1646 public void show() {
1647 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001648 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001649 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001650 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1651 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001652 mGlobalActionsLayout.setAlpha(0);
1653 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001654 .alpha(1)
1655 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001656 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001657 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001658 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001659 .setUpdateListener(animation -> {
1660 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001661 * mScrimAlpha * 255);
1662 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001663 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001664 .start();
1665 }
1666
1667 @Override
1668 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001669 if (!mShowing) {
1670 return;
1671 }
1672 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001673 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001674 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001675 mGlobalActionsLayout.setAlpha(1);
1676 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001677 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001678 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1679 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001680 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001681 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001682 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001683 .setUpdateListener(animation -> {
1684 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001685 * mScrimAlpha * 255);
1686 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001687 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001688 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001689 if (mPanelController != null) {
1690 mPanelController.onDismissed();
1691 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001692 }
1693
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001694 void dismissImmediately() {
1695 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001696 mShowing = false;
Steve Elliott2d206d32019-03-14 16:30:33 -04001697 if (mPanelController != null) {
1698 mPanelController.onDismissed();
1699 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001700 }
1701
Jason Monk361915c2017-03-21 20:33:59 -04001702 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001703 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001704 if (mKeyguardShowing) {
1705 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001706 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1707 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001708 }
1709 } else {
1710 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001711 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1712 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001713 }
1714 }
1715 }
1716
1717 public void setKeyguardShowing(boolean keyguardShowing) {
1718 mKeyguardShowing = keyguardShowing;
1719 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001720
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001721 public void refreshDialog() {
1722 initializeLayout();
1723 mGlobalActionsLayout.updateList();
1724 }
1725
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001726 public void onRotate(int from, int to) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001727 if (mShowing && (shouldUsePanel() || isForceGridEnabled(mContext))) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001728 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001729 }
1730 }
Jason Monk361915c2017-03-21 20:33:59 -04001731 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001732
1733 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001734 * Determines whether or not the Global Actions menu should be forced to
1735 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001736 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001737 private static boolean isForceGridEnabled(Context context) {
1738 return FeatureFlagUtils.isEnabled(context,
1739 FeatureFlagUtils.FORCE_GLOBAL_ACTIONS_GRID_ENABLED);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001740 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001741
1742 /**
1743 * Determines whether or not the Global Actions Panel should appear when the power button
1744 * is held.
1745 */
1746 private static boolean isPanelEnabled(Context context) {
1747 return FeatureFlagUtils.isEnabled(
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001748 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1749 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001750
1751 /**
1752 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1753 */
1754 private static boolean shouldUseSeparatedView() {
1755 return true;
1756 }
Jason Monk361915c2017-03-21 20:33:59 -04001757}