blob: bb2d142c11a1ba1040e11369153a31224236343e [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;
Steve Elliott53f12ae2019-05-13 17:14:15 -040028import android.app.StatusBarManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070029import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000030import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010031import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040032import android.content.BroadcastReceiver;
33import android.content.Context;
34import android.content.DialogInterface;
35import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.UserInfo;
38import android.database.ContentObserver;
39import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040042import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040044import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.os.Message;
46import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.os.UserHandle;
50import android.os.UserManager;
51import android.os.Vibrator;
52import android.provider.Settings;
53import android.service.dreams.DreamService;
54import android.service.dreams.IDreamManager;
Fan Zhangf9914762019-11-01 15:58:38 -070055import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040056import android.telephony.PhoneStateListener;
57import android.telephony.ServiceState;
58import android.telephony.TelephonyManager;
59import android.text.TextUtils;
60import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080061import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040062import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070063import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.view.LayoutInflater;
65import android.view.View;
66import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070067import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040068import android.view.WindowManager;
69import android.view.WindowManagerGlobal;
70import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050071import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040072import android.widget.ImageView;
73import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040074import android.widget.TextView;
75
Charles He9851a8d2017-10-10 17:31:30 +010076import com.android.internal.R;
77import com.android.internal.colorextraction.ColorExtractor;
78import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070079import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010080import com.android.internal.logging.MetricsLogger;
81import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040082import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010083import com.android.internal.telephony.TelephonyIntents;
84import com.android.internal.telephony.TelephonyProperties;
85import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040086import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050087import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040088import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.internal.widget.LockPatternUtils;
90import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050092import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050093import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040094import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010095import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040096import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010097import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050098import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050099import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -0400100import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700101import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400102import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800103import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500104import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500105import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700106
Jason Monk361915c2017-03-21 20:33:59 -0400107import java.util.ArrayList;
108import java.util.List;
109
110/**
111 * Helper to show the global actions dialog. Each item is an {@link Action} that
112 * may show depending on whether the keyguard is showing, and whether the device
113 * is provisioned.
114 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500115public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
116 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400117
118 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
119 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700120 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400121
122 private static final String TAG = "GlobalActionsDialog";
123
124 private static final boolean SHOW_SILENT_TOGGLE = true;
125
126 /* Valid settings for global actions keys.
127 * see config.xml config_globalActionList */
128 private static final String GLOBAL_ACTION_KEY_POWER = "power";
129 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
130 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
131 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
132 private static final String GLOBAL_ACTION_KEY_USERS = "users";
133 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
134 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
135 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
136 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
137 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000138 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800139 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500140 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400141
142 private final Context mContext;
143 private final GlobalActionsManager mWindowManagerFuncs;
144 private final AudioManager mAudioManager;
145 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000146 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800147 private final LockPatternUtils mLockPatternUtils;
148 private final KeyguardManager mKeyguardManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000149 private final BroadcastDispatcher mBroadcastDispatcher;
Jason Monk361915c2017-03-21 20:33:59 -0400150
151 private ArrayList<Action> mItems;
152 private ActionsDialog mDialog;
153
154 private Action mSilentModeAction;
155 private ToggleAction mAirplaneModeOn;
156
157 private MyAdapter mAdapter;
158
159 private boolean mKeyguardShowing = false;
160 private boolean mDeviceProvisioned = false;
161 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
162 private boolean mIsWaitingForEcmExit = false;
163 private boolean mHasTelephony;
164 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000165 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800166 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400167 private final boolean mShowSilentToggle;
168 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500169 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400170 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400171 private final ActivityStarter mActivityStarter;
172 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500173
Jason Monk361915c2017-03-21 20:33:59 -0400174 /**
175 * @param context everything needs a context :(
176 */
177 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700178 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400179 mWindowManagerFuncs = windowManagerFuncs;
180 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
181 mDreamManager = IDreamManager.Stub.asInterface(
182 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000183 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
184 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800185 mLockPatternUtils = new LockPatternUtils(mContext);
186 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000187 mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class);
Jason Monk361915c2017-03-21 20:33:59 -0400188
189 // receive broadcasts
190 IntentFilter filter = new IntentFilter();
191 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
192 filter.addAction(Intent.ACTION_SCREEN_OFF);
193 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000194 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400195
196 ConnectivityManager cm = (ConnectivityManager)
197 context.getSystemService(Context.CONNECTIVITY_SERVICE);
198 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
199
200 // get notified of phone state changes
201 TelephonyManager telephonyManager =
202 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
203 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
204 mContext.getContentResolver().registerContentObserver(
205 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
206 mAirplaneModeObserver);
207 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
208 mHasVibrator = vibrator != null && vibrator.hasVibrator();
209
210 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
211 R.bool.config_useFixedVolume);
212
213 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500214 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400215 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700216
217 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500218
Steve Elliott4c868852019-03-14 16:25:41 -0400219 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400220 KeyguardStateController keyguardStateController =
221 Dependency.get(KeyguardStateController.class);
222 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
223 @Override
224 public void onUnlockedChanged() {
225 if (mDialog != null && mDialog.mPanelController != null) {
226 boolean locked = !keyguardStateController.canDismissLockScreen();
227 mDialog.mPanelController.onDeviceLockStateChanged(locked);
228 }
229 }
230 });
Jason Monk361915c2017-03-21 20:33:59 -0400231 }
232
233 /**
234 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400235 *
Jason Monk361915c2017-03-21 20:33:59 -0400236 * @param keyguardShowing True if keyguard is showing
237 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400238 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
239 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400240 mKeyguardShowing = keyguardShowing;
241 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400242 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400243 if (mDialog != null) {
244 mDialog.dismiss();
245 mDialog = null;
246 // Show delayed, so that the dismiss of the previous dialog completes
247 mHandler.sendEmptyMessage(MESSAGE_SHOW);
248 } else {
249 handleShow();
250 }
251 }
252
Charles He9851a8d2017-10-10 17:31:30 +0100253 /**
254 * Dismiss the global actions dialog, if it's currently shown
255 */
256 public void dismissDialog() {
257 mHandler.removeMessages(MESSAGE_DISMISS);
258 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
259 }
260
Jason Monk361915c2017-03-21 20:33:59 -0400261 private void awakenIfNecessary() {
262 if (mDreamManager != null) {
263 try {
264 if (mDreamManager.isDreaming()) {
265 mDreamManager.awaken();
266 }
267 } catch (RemoteException e) {
268 // we tried
269 }
270 }
271 }
272
273 private void handleShow() {
274 awakenIfNecessary();
275 mDialog = createDialog();
276 prepareDialog();
277
278 // If we only have 1 item and it's a simple press action, just do this action.
279 if (mAdapter.getCount() == 1
280 && mAdapter.getItem(0) instanceof SinglePressAction
281 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
282 ((SinglePressAction) mAdapter.getItem(0)).onPress();
283 } else {
284 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
285 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100286 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400287 mDialog.getWindow().setAttributes(attrs);
288 mDialog.show();
289 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400290 }
291 }
292
293 /**
294 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400295 *
Jason Monk361915c2017-03-21 20:33:59 -0400296 * @return A new dialog.
297 */
298 private ActionsDialog createDialog() {
299 // Simple toggle style if there's no vibrator, otherwise use a tri-state
300 if (!mHasVibrator) {
301 mSilentModeAction = new SilentModeToggleAction();
302 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700303 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400304 }
305 mAirplaneModeOn = new ToggleAction(
306 R.drawable.ic_lock_airplane_mode,
307 R.drawable.ic_lock_airplane_mode_off,
308 R.string.global_actions_toggle_airplane_mode,
309 R.string.global_actions_airplane_mode_on_status,
310 R.string.global_actions_airplane_mode_off_status) {
311
312 void onToggle(boolean on) {
313 if (mHasTelephony && Boolean.parseBoolean(
314 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
315 mIsWaitingForEcmExit = true;
316 // Launch ECM exit dialog
317 Intent ecmDialogIntent =
318 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
319 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
320 mContext.startActivity(ecmDialogIntent);
321 } else {
322 changeAirplaneModeSystemSetting(on);
323 }
324 }
325
326 @Override
327 protected void changeStateFromPress(boolean buttonOn) {
328 if (!mHasTelephony) return;
329
330 // In ECM mode airplane state cannot be changed
331 if (!(Boolean.parseBoolean(
332 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
333 mState = buttonOn ? State.TurningOn : State.TurningOff;
334 mAirplaneState = mState;
335 }
336 }
337
338 public boolean showDuringKeyguard() {
339 return true;
340 }
341
342 public boolean showBeforeProvisioning() {
343 return false;
344 }
345 };
346 onAirplaneModeChanged();
347
348 mItems = new ArrayList<Action>();
349 String[] defaultActions = mContext.getResources().getStringArray(
350 R.array.config_globalActionsList);
351
352 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000353 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800354 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400355 for (int i = 0; i < defaultActions.length; i++) {
356 String actionKey = defaultActions[i];
357 if (addedKeys.contains(actionKey)) {
358 // If we already have added this, don't add it again.
359 continue;
360 }
361 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
362 mItems.add(new PowerAction());
363 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
364 mItems.add(mAirplaneModeOn);
365 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
366 if (Settings.Global.getInt(mContext.getContentResolver(),
367 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
368 mItems.add(new BugReportAction());
369 }
370 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
371 if (mShowSilentToggle) {
372 mItems.add(mSilentModeAction);
373 }
374 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
375 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
376 addUsersToMenu(mItems);
377 }
378 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
379 mItems.add(getSettingsAction());
380 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700381 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
382 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800383 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700384 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800385 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700386 }
Jason Monk361915c2017-03-21 20:33:59 -0400387 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
388 mItems.add(getVoiceAssistAction());
389 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
390 mItems.add(getAssistAction());
391 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
392 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500393 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
394 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000395 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000396 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000397 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
398 mItems.add(new LogoutAction());
399 mHasLogoutButton = true;
400 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800401 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400402 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800403 mItems.add(new EmergencyDialerAction());
404 }
Jason Monk361915c2017-03-21 20:33:59 -0400405 } else {
406 Log.e(TAG, "Invalid global action key " + actionKey);
407 }
408 // Add here so we don't add more than one.
409 addedKeys.add(actionKey);
410 }
411
412 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400413 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400414 }
415
416 mAdapter = new MyAdapter();
417
Steve Elliott9b87a442019-03-05 10:24:16 -0500418 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400419 mPanelPlugin != null
420 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400421 new GlobalActionsPanelPlugin.Callbacks() {
422 @Override
423 public void dismissGlobalActionsMenu() {
424 if (mDialog != null) {
425 mDialog.dismiss();
426 }
427 }
428
429 @Override
430 public void startPendingIntentDismissingKeyguard(
431 PendingIntent intent) {
432 mActivityStarter
433 .startPendingIntentDismissingKeyguard(intent);
434 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400435 },
436 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500437 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400438
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400439 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400440 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700441 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400442
443 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800444 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400445
446 return dialog;
447 }
448
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800449 private boolean shouldDisplayLockdown() {
450 int userId = getCurrentUser().id;
451 // Lockdown is meaningless without a place to go.
452 if (!mKeyguardManager.isDeviceSecure(userId)) {
453 return false;
454 }
455
456 // Only show the lockdown button if the device isn't locked down (for whatever reason).
457 int state = mLockPatternUtils.getStrongAuthForUser(userId);
458 return (state == STRONG_AUTH_NOT_REQUIRED
459 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
460 }
461
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700462 @Override
463 public void onUiModeChanged() {
464 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700465 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400466 mDialog.refreshDialog();
467 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700468 }
469
470 public void destroy() {
471 Dependency.get(ConfigurationController.class).removeCallback(this);
472 }
473
Jason Monk361915c2017-03-21 20:33:59 -0400474 private final class PowerAction extends SinglePressAction implements LongPressAction {
475 private PowerAction() {
476 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400477 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400478 }
479
480 @Override
481 public boolean onLongPress() {
482 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
483 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
484 mWindowManagerFuncs.reboot(true);
485 return true;
486 }
487 return false;
488 }
489
490 @Override
491 public boolean showDuringKeyguard() {
492 return true;
493 }
494
495 @Override
496 public boolean showBeforeProvisioning() {
497 return true;
498 }
499
500 @Override
501 public void onPress() {
502 // shutdown by making sure radio and power are handled accordingly.
503 mWindowManagerFuncs.shutdown();
504 }
505 }
506
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400507 private abstract class EmergencyAction extends SinglePressAction {
508 EmergencyAction(int iconResId, int messageResId) {
509 super(iconResId, messageResId);
510 }
511
512 @Override
513 public boolean shouldBeSeparated() {
514 return shouldUseSeparatedView();
515 }
516
517 @Override
518 public View create(
519 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
520 View v = super.create(context, convertView, parent, inflater);
521 int textColor;
522 if (shouldBeSeparated()) {
523 textColor = v.getResources().getColor(
524 com.android.systemui.R.color.global_actions_alert_text);
525 } else {
526 textColor = v.getResources().getColor(
527 com.android.systemui.R.color.global_actions_text);
528 }
529 TextView messageView = v.findViewById(R.id.message);
530 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400531 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400532 ImageView icon = (ImageView) v.findViewById(R.id.icon);
533 icon.getDrawable().setTint(textColor);
534 return v;
535 }
536
537 @Override
538 public boolean showDuringKeyguard() {
539 return true;
540 }
541
542 @Override
543 public boolean showBeforeProvisioning() {
544 return true;
545 }
546 }
547
548 private class EmergencyAffordanceAction extends EmergencyAction {
549 EmergencyAffordanceAction() {
550 super(R.drawable.emergency_icon,
551 R.string.global_action_emergency);
552 }
553
554 @Override
555 public void onPress() {
556 mEmergencyAffordanceManager.performEmergencyCall();
557 }
558 }
559
560 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800561 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400562 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800563 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800564 }
565
566 @Override
567 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800568 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Fan Zhangf9914762019-11-01 15:58:38 -0700569 Intent intent = mContext.getSystemService(TelecomManager.class)
570 .createLaunchEmergencyDialerIntent(null /* number */);
Leo Hsue4cc3122019-03-06 15:25:28 +0800571 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
572 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
573 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800574 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
575 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800576 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
577 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800578 }
579
Jason Monk361915c2017-03-21 20:33:59 -0400580 private final class RestartAction extends SinglePressAction implements LongPressAction {
581 private RestartAction() {
582 super(R.drawable.ic_restart, R.string.global_action_restart);
583 }
584
585 @Override
586 public boolean onLongPress() {
587 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
588 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
589 mWindowManagerFuncs.reboot(true);
590 return true;
591 }
592 return false;
593 }
594
595 @Override
596 public boolean showDuringKeyguard() {
597 return true;
598 }
599
600 @Override
601 public boolean showBeforeProvisioning() {
602 return true;
603 }
604
605 @Override
606 public void onPress() {
607 mWindowManagerFuncs.reboot(false);
608 }
609 }
610
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400611 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500612 public ScreenshotAction() {
613 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
614 }
615
616 @Override
617 public void onPress() {
618 // Add a little delay before executing, to give the
619 // dialog a chance to go away before it takes a
620 // screenshot.
621 // TODO: instead, omit global action dialog layer
622 mHandler.postDelayed(new Runnable() {
623 @Override
624 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400625 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500626 MetricsLogger.action(mContext,
627 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
628 }
629 }, 500);
630 }
631
632 @Override
633 public boolean showDuringKeyguard() {
634 return true;
635 }
636
637 @Override
638 public boolean showBeforeProvisioning() {
639 return false;
640 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400641
642 @Override
643 public boolean onLongPress() {
644 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
645 mScreenRecordHelper.launchRecordPrompt();
646 } else {
647 onPress();
648 }
649 return true;
650 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500651 }
652
Jason Monk361915c2017-03-21 20:33:59 -0400653 private class BugReportAction extends SinglePressAction implements LongPressAction {
654
655 public BugReportAction() {
656 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
657 }
658
659 @Override
660 public void onPress() {
661 // don't actually trigger the bugreport if we are running stability
662 // tests via monkey
663 if (ActivityManager.isUserAMonkey()) {
664 return;
665 }
666 // Add a little delay before executing, to give the
667 // dialog a chance to go away before it takes a
668 // screenshot.
669 mHandler.postDelayed(new Runnable() {
670 @Override
671 public void run() {
672 try {
673 // Take an "interactive" bugreport.
674 MetricsLogger.action(mContext,
675 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100676 ActivityManager.getService().requestInteractiveBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400677 } catch (RemoteException e) {
678 }
679 }
680 }, 500);
681 }
682
683 @Override
684 public boolean onLongPress() {
685 // don't actually trigger the bugreport if we are running stability
686 // tests via monkey
687 if (ActivityManager.isUserAMonkey()) {
688 return false;
689 }
690 try {
691 // Take a "full" bugreport.
692 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100693 ActivityManager.getService().requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400694 } catch (RemoteException e) {
695 }
696 return false;
697 }
698
699 public boolean showDuringKeyguard() {
700 return true;
701 }
702
703 @Override
704 public boolean showBeforeProvisioning() {
705 return false;
706 }
Jason Monk361915c2017-03-21 20:33:59 -0400707 }
708
Alex Chau04458852017-11-27 18:21:23 +0000709 private final class LogoutAction extends SinglePressAction {
710 private LogoutAction() {
711 super(R.drawable.ic_logout, R.string.global_action_logout);
712 }
713
714 @Override
715 public boolean showDuringKeyguard() {
716 return true;
717 }
718
719 @Override
720 public boolean showBeforeProvisioning() {
721 return false;
722 }
723
724 @Override
725 public void onPress() {
726 // Add a little delay before executing, to give the dialog a chance to go away before
727 // switching user
728 mHandler.postDelayed(() -> {
729 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000730 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000731 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000732 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000733 } catch (RemoteException re) {
734 Log.e(TAG, "Couldn't logout user " + re);
735 }
736 }, 500);
737 }
738 }
739
Jason Monk361915c2017-03-21 20:33:59 -0400740 private Action getSettingsAction() {
741 return new SinglePressAction(R.drawable.ic_settings,
742 R.string.global_action_settings) {
743
744 @Override
745 public void onPress() {
746 Intent intent = new Intent(Settings.ACTION_SETTINGS);
747 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
748 mContext.startActivity(intent);
749 }
750
751 @Override
752 public boolean showDuringKeyguard() {
753 return true;
754 }
755
756 @Override
757 public boolean showBeforeProvisioning() {
758 return true;
759 }
760 };
761 }
762
Jason Monk361915c2017-03-21 20:33:59 -0400763 private Action getAssistAction() {
764 return new SinglePressAction(R.drawable.ic_action_assist_focused,
765 R.string.global_action_assist) {
766 @Override
767 public void onPress() {
768 Intent intent = new Intent(Intent.ACTION_ASSIST);
769 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
770 mContext.startActivity(intent);
771 }
772
773 @Override
774 public boolean showDuringKeyguard() {
775 return true;
776 }
777
778 @Override
779 public boolean showBeforeProvisioning() {
780 return true;
781 }
782 };
783 }
784
785 private Action getVoiceAssistAction() {
786 return new SinglePressAction(R.drawable.ic_voice_search,
787 R.string.global_action_voice_assist) {
788 @Override
789 public void onPress() {
790 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
791 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
792 mContext.startActivity(intent);
793 }
794
795 @Override
796 public boolean showDuringKeyguard() {
797 return true;
798 }
799
800 @Override
801 public boolean showBeforeProvisioning() {
802 return true;
803 }
804 };
805 }
806
807 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400808 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400809 R.string.global_action_lockdown) {
810
811 @Override
812 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700813 new LockPatternUtils(mContext)
814 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
815 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400816 try {
817 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100818 // Lock profiles (if any) on the background thread.
819 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
820 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400821 } catch (RemoteException e) {
822 Log.e(TAG, "Error while trying to lock device.", e);
823 }
824 }
825
826 @Override
827 public boolean showDuringKeyguard() {
828 return true;
829 }
830
831 @Override
832 public boolean showBeforeProvisioning() {
833 return false;
834 }
835 };
836 }
837
Pavel Grafov059021b2018-05-02 13:44:46 +0100838 private void lockProfiles() {
839 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
840 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
841 final int currentUserId = getCurrentUser().id;
842 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
843 for (final int id : profileIds) {
844 if (id != currentUserId) {
845 tm.setDeviceLockedForUser(id, true);
846 }
847 }
848 }
849
Jason Monk361915c2017-03-21 20:33:59 -0400850 private UserInfo getCurrentUser() {
851 try {
852 return ActivityManager.getService().getCurrentUser();
853 } catch (RemoteException re) {
854 return null;
855 }
856 }
857
858 private boolean isCurrentUserOwner() {
859 UserInfo currentUser = getCurrentUser();
860 return currentUser == null || currentUser.isPrimary();
861 }
862
863 private void addUsersToMenu(ArrayList<Action> items) {
864 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
865 if (um.isUserSwitcherEnabled()) {
866 List<UserInfo> users = um.getUsers();
867 UserInfo currentUser = getCurrentUser();
868 for (final UserInfo user : users) {
869 if (user.supportsSwitchToByUser()) {
870 boolean isCurrentUser = currentUser == null
871 ? user.id == 0 : (currentUser.id == user.id);
872 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
873 : null;
874 SinglePressAction switchToUser = new SinglePressAction(
875 R.drawable.ic_menu_cc, icon,
876 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400877 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400878 public void onPress() {
879 try {
880 ActivityManager.getService().switchUser(user.id);
881 } catch (RemoteException re) {
882 Log.e(TAG, "Couldn't switch user " + re);
883 }
884 }
885
886 public boolean showDuringKeyguard() {
887 return true;
888 }
889
890 public boolean showBeforeProvisioning() {
891 return false;
892 }
893 };
894 items.add(switchToUser);
895 }
896 }
897 }
898 }
899
900 private void prepareDialog() {
901 refreshSilentMode();
902 mAirplaneModeOn.updateState(mAirplaneState);
903 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400904 if (mShowSilentToggle) {
905 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000906 mBroadcastDispatcher.registerReceiver(mRingerModeReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400907 }
908 }
909
910 private void refreshSilentMode() {
911 if (!mHasVibrator) {
912 final boolean silentModeOn =
913 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400914 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400915 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
916 }
917 }
918
919 /** {@inheritDoc} */
920 public void onDismiss(DialogInterface dialog) {
921 mWindowManagerFuncs.onGlobalActionsHidden();
922 if (mShowSilentToggle) {
923 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000924 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400925 } catch (IllegalArgumentException ie) {
926 // ignore this
927 Log.w(TAG, ie);
928 }
929 }
930 }
931
932 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800933 public void onShow(DialogInterface dialog) {
934 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
935 }
936
Jason Monk361915c2017-03-21 20:33:59 -0400937 /**
938 * The adapter used for the list within the global actions dialog, taking
939 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400940 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
941 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400942 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
943 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500944 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400945 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400946 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400947 for (int i = 0; i < mItems.size(); i++) {
948 final Action action = mItems.get(i);
949
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400950 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
951 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400952 }
Jason Monk361915c2017-03-21 20:33:59 -0400953 }
954 return count;
955 }
956
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400957 private boolean shouldBeShown(Action action) {
958 if (mKeyguardShowing && !action.showDuringKeyguard()) {
959 return false;
960 }
961 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
962 return false;
963 }
964 return true;
965 }
966
967 @Override
968 public int countSeparatedItems() {
969 return countItems(true);
970 }
971
972 @Override
973 public int countListItems() {
974 return countItems(false);
975 }
976
977 @Override
978 public int getCount() {
979 return countSeparatedItems() + countListItems();
980 }
981
Jason Monk361915c2017-03-21 20:33:59 -0400982 @Override
983 public boolean isEnabled(int position) {
984 return getItem(position).isEnabled();
985 }
986
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500987 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400988 public boolean areAllItemsEnabled() {
989 return false;
990 }
991
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500992 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400993 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400994 int filteredPos = 0;
995 for (int i = 0; i < mItems.size(); i++) {
996 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400997 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400998 continue;
999 }
1000 if (filteredPos == position) {
1001 return action;
1002 }
1003 filteredPos++;
1004 }
1005
1006 throw new IllegalArgumentException("position " + position
1007 + " out of range of showable actions"
1008 + ", filtered count=" + getCount()
1009 + ", keyguardshowing=" + mKeyguardShowing
1010 + ", provisioned=" + mDeviceProvisioned);
1011 }
1012
1013
1014 public long getItemId(int position) {
1015 return position;
1016 }
1017
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001018 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001019 public View getView(int position, View convertView, ViewGroup parent) {
1020 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001021 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001022 view.setOnClickListener(v -> onClickItem(position));
1023 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001024 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001025 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001026
1027 @Override
1028 public boolean onLongClickItem(int position) {
1029 final Action action = mAdapter.getItem(position);
1030 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001031 if (mDialog != null) {
1032 mDialog.dismiss();
1033 } else {
1034 Log.w(TAG, "Action long-clicked while mDialog is null.");
1035 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001036 return ((LongPressAction) action).onLongPress();
1037 }
1038 return false;
1039 }
1040
1041 @Override
1042 public void onClickItem(int position) {
1043 Action item = mAdapter.getItem(position);
1044 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001045 if (mDialog != null) {
1046 mDialog.dismiss();
1047 } else {
1048 Log.w(TAG, "Action clicked while mDialog is null.");
1049 }
1050 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001051 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001052 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001053
1054 @Override
1055 public boolean shouldBeSeparated(int position) {
1056 return getItem(position).shouldBeSeparated();
1057 }
Jason Monk361915c2017-03-21 20:33:59 -04001058 }
1059
1060 // note: the scheme below made more sense when we were planning on having
1061 // 8 different things in the global actions dialog. seems overkill with
1062 // only 3 items now, but may as well keep this flexible approach so it will
1063 // be easy should someone decide at the last minute to include something
1064 // else, such as 'enable wifi', or 'enable bluetooth'
1065
1066 /**
1067 * What each item in the global actions dialog must be able to support.
1068 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001069 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001070 /**
1071 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001072 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001073 */
1074 CharSequence getLabelForAccessibility(Context context);
1075
1076 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1077
1078 void onPress();
1079
1080 /**
1081 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001082 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001083 */
1084 boolean showDuringKeyguard();
1085
1086 /**
1087 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001088 * device is provisioned.onlongpress
1089 *
Jason Monk361915c2017-03-21 20:33:59 -04001090 */
1091 boolean showBeforeProvisioning();
1092
1093 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001094
1095 default boolean shouldBeSeparated() {
1096 return false;
1097 }
Jason Monk361915c2017-03-21 20:33:59 -04001098 }
1099
1100 /**
1101 * An action that also supports long press.
1102 */
1103 private interface LongPressAction extends Action {
1104 boolean onLongPress();
1105 }
1106
1107 /**
1108 * A single press action maintains no state, just responds to a press
1109 * and takes an action.
1110 */
1111 private static abstract class SinglePressAction implements Action {
1112 private final int mIconResId;
1113 private final Drawable mIcon;
1114 private final int mMessageResId;
1115 private final CharSequence mMessage;
1116
1117 protected SinglePressAction(int iconResId, int messageResId) {
1118 mIconResId = iconResId;
1119 mMessageResId = messageResId;
1120 mMessage = null;
1121 mIcon = null;
1122 }
1123
1124 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1125 mIconResId = iconResId;
1126 mMessageResId = 0;
1127 mMessage = message;
1128 mIcon = icon;
1129 }
1130
1131 public boolean isEnabled() {
1132 return true;
1133 }
1134
1135 public String getStatus() {
1136 return null;
1137 }
1138
1139 abstract public void onPress();
1140
1141 public CharSequence getLabelForAccessibility(Context context) {
1142 if (mMessage != null) {
1143 return mMessage;
1144 } else {
1145 return context.getString(mMessageResId);
1146 }
1147 }
1148
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001149 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001150 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001151 }
1152
Jason Monk361915c2017-03-21 20:33:59 -04001153 public View create(
1154 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001155 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001156 false);
Jason Monk361915c2017-03-21 20:33:59 -04001157
1158 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1159 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001160 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001161
1162 TextView statusView = (TextView) v.findViewById(R.id.status);
1163 final String status = getStatus();
1164 if (!TextUtils.isEmpty(status)) {
1165 statusView.setText(status);
1166 } else {
1167 statusView.setVisibility(View.GONE);
1168 }
1169 if (mIcon != null) {
1170 icon.setImageDrawable(mIcon);
1171 icon.setScaleType(ScaleType.CENTER_CROP);
1172 } else if (mIconResId != 0) {
1173 icon.setImageDrawable(context.getDrawable(mIconResId));
1174 }
1175 if (mMessage != null) {
1176 messageView.setText(mMessage);
1177 } else {
1178 messageView.setText(mMessageResId);
1179 }
1180
1181 return v;
1182 }
1183 }
1184
1185 /**
1186 * A toggle action knows whether it is on or off, and displays an icon
1187 * and status message accordingly.
1188 */
1189 private static abstract class ToggleAction implements Action {
1190
1191 enum State {
1192 Off(false),
1193 TurningOn(true),
1194 TurningOff(true),
1195 On(false);
1196
1197 private final boolean inTransition;
1198
1199 State(boolean intermediate) {
1200 inTransition = intermediate;
1201 }
1202
1203 public boolean inTransition() {
1204 return inTransition;
1205 }
1206 }
1207
1208 protected State mState = State.Off;
1209
1210 // prefs
1211 protected int mEnabledIconResId;
1212 protected int mDisabledIconResid;
1213 protected int mMessageResId;
1214 protected int mEnabledStatusMessageResId;
1215 protected int mDisabledStatusMessageResId;
1216
1217 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001218 * @param enabledIconResId The icon for when this action is on.
1219 * @param disabledIconResid The icon for when this action is off.
1220 * @param message The general information message, e.g 'Silent Mode'
1221 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001222 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1223 */
1224 public ToggleAction(int enabledIconResId,
1225 int disabledIconResid,
1226 int message,
1227 int enabledStatusMessageResId,
1228 int disabledStatusMessageResId) {
1229 mEnabledIconResId = enabledIconResId;
1230 mDisabledIconResid = disabledIconResid;
1231 mMessageResId = message;
1232 mEnabledStatusMessageResId = enabledStatusMessageResId;
1233 mDisabledStatusMessageResId = disabledStatusMessageResId;
1234 }
1235
1236 /**
1237 * Override to make changes to resource IDs just before creating the
1238 * View.
1239 */
1240 void willCreate() {
1241
1242 }
1243
1244 @Override
1245 public CharSequence getLabelForAccessibility(Context context) {
1246 return context.getString(mMessageResId);
1247 }
1248
1249 public View create(Context context, View convertView, ViewGroup parent,
1250 LayoutInflater inflater) {
1251 willCreate();
1252
1253 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001254 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001255
1256 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1257 TextView messageView = (TextView) v.findViewById(R.id.message);
1258 TextView statusView = (TextView) v.findViewById(R.id.status);
1259 final boolean enabled = isEnabled();
1260
1261 if (messageView != null) {
1262 messageView.setText(mMessageResId);
1263 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001264 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001265 }
1266
1267 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1268 if (icon != null) {
1269 icon.setImageDrawable(context.getDrawable(
1270 (on ? mEnabledIconResId : mDisabledIconResid)));
1271 icon.setEnabled(enabled);
1272 }
1273
1274 if (statusView != null) {
1275 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1276 statusView.setVisibility(View.VISIBLE);
1277 statusView.setEnabled(enabled);
1278 }
1279 v.setEnabled(enabled);
1280
1281 return v;
1282 }
1283
1284 public final void onPress() {
1285 if (mState.inTransition()) {
1286 Log.w(TAG, "shouldn't be able to toggle when in transition");
1287 return;
1288 }
1289
1290 final boolean nowOn = !(mState == State.On);
1291 onToggle(nowOn);
1292 changeStateFromPress(nowOn);
1293 }
1294
1295 public boolean isEnabled() {
1296 return !mState.inTransition();
1297 }
1298
1299 /**
1300 * Implementations may override this if their state can be in on of the intermediate
1301 * states until some notification is received (e.g airplane mode is 'turning off' until
1302 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001303 *
Jason Monk361915c2017-03-21 20:33:59 -04001304 * @param buttonOn Whether the button was turned on or off
1305 */
1306 protected void changeStateFromPress(boolean buttonOn) {
1307 mState = buttonOn ? State.On : State.Off;
1308 }
1309
1310 abstract void onToggle(boolean on);
1311
1312 public void updateState(State state) {
1313 mState = state;
1314 }
1315 }
1316
1317 private class SilentModeToggleAction extends ToggleAction {
1318 public SilentModeToggleAction() {
1319 super(R.drawable.ic_audio_vol_mute,
1320 R.drawable.ic_audio_vol,
1321 R.string.global_action_toggle_silent_mode,
1322 R.string.global_action_silent_mode_on_status,
1323 R.string.global_action_silent_mode_off_status);
1324 }
1325
1326 void onToggle(boolean on) {
1327 if (on) {
1328 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1329 } else {
1330 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1331 }
1332 }
1333
1334 public boolean showDuringKeyguard() {
1335 return true;
1336 }
1337
1338 public boolean showBeforeProvisioning() {
1339 return false;
1340 }
1341 }
1342
1343 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1344
Jason Monk16fbd9d2017-04-27 14:28:49 -04001345 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001346
1347 private final AudioManager mAudioManager;
1348 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001349
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001350 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001351 mAudioManager = audioManager;
1352 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001353 }
1354
1355 private int ringerModeToIndex(int ringerMode) {
1356 // They just happen to coincide
1357 return ringerMode;
1358 }
1359
1360 private int indexToRingerMode(int index) {
1361 // They just happen to coincide
1362 return index;
1363 }
1364
1365 @Override
1366 public CharSequence getLabelForAccessibility(Context context) {
1367 return null;
1368 }
1369
1370 public View create(Context context, View convertView, ViewGroup parent,
1371 LayoutInflater inflater) {
1372 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1373
1374 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1375 for (int i = 0; i < 3; i++) {
1376 View itemView = v.findViewById(ITEM_IDS[i]);
1377 itemView.setSelected(selectedIndex == i);
1378 // Set up click handler
1379 itemView.setTag(i);
1380 itemView.setOnClickListener(this);
1381 }
1382 return v;
1383 }
1384
1385 public void onPress() {
1386 }
1387
1388 public boolean showDuringKeyguard() {
1389 return true;
1390 }
1391
1392 public boolean showBeforeProvisioning() {
1393 return false;
1394 }
1395
1396 public boolean isEnabled() {
1397 return true;
1398 }
1399
1400 void willCreate() {
1401 }
1402
1403 public void onClick(View v) {
1404 if (!(v.getTag() instanceof Integer)) return;
1405
1406 int index = (Integer) v.getTag();
1407 mAudioManager.setRingerMode(indexToRingerMode(index));
1408 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1409 }
1410 }
1411
1412 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1413 public void onReceive(Context context, Intent intent) {
1414 String action = intent.getAction();
1415 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1416 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1417 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1418 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001419 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001420 }
1421 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1422 // Airplane mode can be changed after ECM exits if airplane toggle button
1423 // is pressed during ECM mode
1424 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1425 mIsWaitingForEcmExit) {
1426 mIsWaitingForEcmExit = false;
1427 changeAirplaneModeSystemSetting(true);
1428 }
1429 }
1430 }
1431 };
1432
1433 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1434 @Override
1435 public void onServiceStateChanged(ServiceState serviceState) {
1436 if (!mHasTelephony) return;
1437 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1438 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1439 mAirplaneModeOn.updateState(mAirplaneState);
1440 mAdapter.notifyDataSetChanged();
1441 }
1442 };
1443
1444 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1445 @Override
1446 public void onReceive(Context context, Intent intent) {
1447 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1448 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1449 }
1450 }
1451 };
1452
1453 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1454 @Override
1455 public void onChange(boolean selfChange) {
1456 onAirplaneModeChanged();
1457 }
1458 };
1459
1460 private static final int MESSAGE_DISMISS = 0;
1461 private static final int MESSAGE_REFRESH = 1;
1462 private static final int MESSAGE_SHOW = 2;
1463 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1464
1465 private Handler mHandler = new Handler() {
1466 public void handleMessage(Message msg) {
1467 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001468 case MESSAGE_DISMISS:
1469 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001470 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1471 mDialog.dismissImmediately();
1472 } else {
1473 mDialog.dismiss();
1474 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001475 mDialog = null;
1476 }
1477 break;
1478 case MESSAGE_REFRESH:
1479 refreshSilentMode();
1480 mAdapter.notifyDataSetChanged();
1481 break;
1482 case MESSAGE_SHOW:
1483 handleShow();
1484 break;
Jason Monk361915c2017-03-21 20:33:59 -04001485 }
1486 }
1487 };
1488
1489 private void onAirplaneModeChanged() {
1490 // Let the service state callbacks handle the state.
1491 if (mHasTelephony) return;
1492
1493 boolean airplaneModeOn = Settings.Global.getInt(
1494 mContext.getContentResolver(),
1495 Settings.Global.AIRPLANE_MODE_ON,
1496 0) == 1;
1497 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1498 mAirplaneModeOn.updateState(mAirplaneState);
1499 }
1500
1501 /**
1502 * Change the airplane mode system setting
1503 */
1504 private void changeAirplaneModeSystemSetting(boolean on) {
1505 Settings.Global.putInt(
1506 mContext.getContentResolver(),
1507 Settings.Global.AIRPLANE_MODE_ON,
1508 on ? 1 : 0);
1509 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1510 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1511 intent.putExtra("state", on);
1512 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1513 if (!mHasTelephony) {
1514 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1515 }
1516 }
1517
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001518 private static final class ActionsDialog extends Dialog implements DialogInterface,
1519 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001520
Jason Monk16fbd9d2017-04-27 14:28:49 -04001521 private final Context mContext;
1522 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001523 private final IStatusBarService mStatusBarService;
1524 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001525 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001526 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001527 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001528 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001529 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001530 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001531 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001532 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001533 private boolean mHadTopUi;
1534 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001535
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001536 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001537 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001538 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001539 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001540 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001541 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001542 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001543 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001544
1545 // Window initialization
1546 Window window = getWindow();
1547 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001548 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1549 window.getDecorView();
1550 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1551 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1552 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1553 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1554 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001555 window.addFlags(
1556 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001557 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001558 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001559 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1560 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1561 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001562 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001563 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001564
1565 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001566 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001567 }
1568
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001569 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001570 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001571 }
1572
1573 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001574 int rotation = RotationUtils.getRotation(mContext);
1575 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1576 if (rotation != RotationUtils.ROTATION_NONE) {
1577 if (rotationLocked) {
1578 if (mResetOrientationData == null) {
1579 mResetOrientationData = new ResetOrientationData();
1580 mResetOrientationData.locked = true;
1581 mResetOrientationData.rotation = rotation;
1582 }
1583
1584 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001585 // This call is posted so that the rotation does not change until post-layout,
1586 // otherwise onConfigurationChanged() may not get invoked.
1587 mGlobalActionsLayout.post(() ->
1588 RotationPolicy.setRotationLockAtAngle(
1589 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001590 }
1591 } else {
1592 if (!rotationLocked) {
1593 if (mResetOrientationData == null) {
1594 mResetOrientationData = new ResetOrientationData();
1595 mResetOrientationData.locked = false;
1596 }
1597
1598 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001599 // This call is posted so that the rotation does not change until post-layout,
1600 // otherwise onConfigurationChanged() may not get invoked.
1601 mGlobalActionsLayout.post(() ->
1602 RotationPolicy.setRotationLockAtAngle(
1603 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001604 }
1605
Steve Elliott53f12ae2019-05-13 17:14:15 -04001606 // Disable rotation suggestions, if enabled
1607 setRotationSuggestionsEnabled(false);
1608
Steve Elliott300b48f2019-05-29 14:13:50 -04001609 FrameLayout panelContainer =
1610 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001611 FrameLayout.LayoutParams panelParams =
1612 new FrameLayout.LayoutParams(
1613 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001614 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001615 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001616 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1617 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001618 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001619 }
1620
1621 private void initializeLayout() {
1622 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001623 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001624 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001625 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001626 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001627 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001628 @Override
1629 public boolean dispatchPopulateAccessibilityEvent(
1630 View host, AccessibilityEvent event) {
1631 // Populate the title here, just as Activity does
1632 event.getText().add(mContext.getString(R.string.global_actions));
1633 return true;
1634 }
1635 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001636 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001637 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001638
Steve Elliott86ef6282019-05-08 14:45:04 -04001639 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001640 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001641 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001642 if (mBackgroundDrawable == null) {
1643 mBackgroundDrawable = new ScrimDrawable();
1644 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1645 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001646 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001647 }
1648
Steve Elliott300b48f2019-05-29 14:13:50 -04001649 private void fixNavBarClipping() {
1650 ViewGroup content = findViewById(android.R.id.content);
1651 content.setClipChildren(false);
1652 content.setClipToPadding(false);
1653 ViewGroup contentParent = (ViewGroup) content.getParent();
1654 contentParent.setClipChildren(false);
1655 contentParent.setClipToPadding(false);
1656 }
1657
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001658 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001659 int rotation = RotationUtils.getRotation(context);
1660 boolean useGridLayout = isForceGridEnabled(context)
1661 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1662 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001663 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001664 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001665 } else {
1666 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001667 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001668 } else {
1669 if (useGridLayout) {
1670 return com.android.systemui.R.layout.global_actions_grid;
1671 } else {
1672 return com.android.systemui.R.layout.global_actions_column;
1673 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001674 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001675 }
1676
Jason Monk361915c2017-03-21 20:33:59 -04001677 @Override
1678 protected void onStart() {
1679 super.setCanceledOnTouchOutside(true);
1680 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001681 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001682
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001683 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001684 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001685 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001686 updateColors(colors, false /* animate */);
1687 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001688 }
1689
1690 /**
1691 * Updates background and system bars according to current GradientColors.
1692 * @param colors Colors and hints to use.
1693 * @param animate Interpolates gradient if true, just sets otherwise.
1694 */
1695 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001696 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001697 return;
1698 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001699 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001700 View decorView = getWindow().getDecorView();
1701 if (colors.supportsDarkText()) {
1702 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001703 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001704 } else {
1705 decorView.setSystemUiVisibility(0);
1706 }
Jason Monk361915c2017-03-21 20:33:59 -04001707 }
1708
1709 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001710 protected void onStop() {
1711 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001712 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001713 }
1714
1715 @Override
1716 public void show() {
1717 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001718 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001719 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1720 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001721 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001722 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1723 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001724 mGlobalActionsLayout.setAlpha(0);
1725 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001726 .alpha(1)
1727 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001728 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001729 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001730 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001731 .setUpdateListener(animation -> {
1732 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001733 * mScrimAlpha * 255);
1734 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001735 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001736 .start();
1737 }
1738
1739 @Override
1740 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001741 if (!mShowing) {
1742 return;
1743 }
1744 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001745 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001746 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001747 mGlobalActionsLayout.setAlpha(1);
1748 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001749 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001750 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1751 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001752 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001753 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001754 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001755 .setUpdateListener(animation -> {
1756 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001757 * mScrimAlpha * 255);
1758 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001759 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001760 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001761 dismissPanel();
1762 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001763 }
1764
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001765 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001766 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001767 dismissPanel();
1768 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001769 completeDismiss();
1770 }
1771
1772 private void completeDismiss() {
1773 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1774 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001775 }
1776
1777 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001778 if (mPanelController != null) {
1779 mPanelController.onDismissed();
1780 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001781 }
1782
Steve Elliott53f12ae2019-05-13 17:14:15 -04001783 private void setRotationSuggestionsEnabled(boolean enabled) {
1784 try {
1785 final int userId = Binder.getCallingUserHandle().getIdentifier();
1786 final int what = enabled
1787 ? StatusBarManager.DISABLE2_NONE
1788 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1789 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1790 } catch (RemoteException ex) {
1791 throw ex.rethrowFromSystemServer();
1792 }
1793 }
1794
Steve Elliott48f75db2019-05-03 15:03:38 -04001795 private void resetOrientation() {
1796 if (mResetOrientationData != null) {
1797 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1798 mResetOrientationData.rotation);
1799 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001800 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001801 }
1802
Jason Monk361915c2017-03-21 20:33:59 -04001803 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001804 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001805 if (mKeyguardShowing) {
1806 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001807 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1808 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001809 }
1810 } else {
1811 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001812 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1813 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001814 }
1815 }
1816 }
1817
1818 public void setKeyguardShowing(boolean keyguardShowing) {
1819 mKeyguardShowing = keyguardShowing;
1820 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001821
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001822 public void refreshDialog() {
1823 initializeLayout();
1824 mGlobalActionsLayout.updateList();
1825 }
1826
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001827 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001828 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001829 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001830 }
1831 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001832
1833 private static class ResetOrientationData {
1834 public boolean locked;
1835 public int rotation;
1836 }
Jason Monk361915c2017-03-21 20:33:59 -04001837 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001838
1839 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001840 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1841 */
1842 private static boolean isPanelDebugModeEnabled(Context context) {
1843 return Settings.Secure.getInt(context.getContentResolver(),
1844 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1845 }
1846
1847 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001848 * Determines whether or not the Global Actions menu should be forced to
1849 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001850 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001851 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001852 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001853 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001854
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001855
1856 /**
1857 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1858 */
1859 private static boolean shouldUseSeparatedView() {
1860 return true;
1861 }
Jason Monk361915c2017-03-21 20:33:59 -04001862}