blob: e7ce134db8346e8c74b44546174526b835b8e23d [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;
Inseob Kim5e82f732019-11-08 15:08:38 +090055import android.sysprop.TelephonyProperties;
Fan Zhangf9914762019-11-01 15:58:38 -070056import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040057import android.telephony.PhoneStateListener;
58import android.telephony.ServiceState;
59import android.telephony.TelephonyManager;
60import android.text.TextUtils;
61import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080062import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040063import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070064import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.LayoutInflater;
66import android.view.View;
67import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070068import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.view.WindowManager;
70import android.view.WindowManagerGlobal;
71import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050072import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.ImageView;
74import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040075import android.widget.TextView;
76
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.R;
78import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070080import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040083import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040085import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050086import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040087import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.internal.widget.LockPatternUtils;
89import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050091import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050092import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040093import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010094import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040095import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010096import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050097import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050098import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -040099import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700100import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400101import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800102import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500103import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500104import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700105
Jason Monk361915c2017-03-21 20:33:59 -0400106import java.util.ArrayList;
107import java.util.List;
108
109/**
110 * Helper to show the global actions dialog. Each item is an {@link Action} that
111 * may show depending on whether the keyguard is showing, and whether the device
112 * is provisioned.
113 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500114public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
115 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400116
117 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
118 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700119 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400120
121 private static final String TAG = "GlobalActionsDialog";
122
123 private static final boolean SHOW_SILENT_TOGGLE = true;
124
125 /* Valid settings for global actions keys.
126 * see config.xml config_globalActionList */
127 private static final String GLOBAL_ACTION_KEY_POWER = "power";
128 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
129 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
130 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
131 private static final String GLOBAL_ACTION_KEY_USERS = "users";
132 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
133 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
134 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
135 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
136 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000137 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800138 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500139 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400140
141 private final Context mContext;
142 private final GlobalActionsManager mWindowManagerFuncs;
143 private final AudioManager mAudioManager;
144 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000145 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800146 private final LockPatternUtils mLockPatternUtils;
147 private final KeyguardManager mKeyguardManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000148 private final BroadcastDispatcher mBroadcastDispatcher;
Jason Monk361915c2017-03-21 20:33:59 -0400149
150 private ArrayList<Action> mItems;
151 private ActionsDialog mDialog;
152
153 private Action mSilentModeAction;
154 private ToggleAction mAirplaneModeOn;
155
156 private MyAdapter mAdapter;
157
158 private boolean mKeyguardShowing = false;
159 private boolean mDeviceProvisioned = false;
160 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
161 private boolean mIsWaitingForEcmExit = false;
162 private boolean mHasTelephony;
163 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000164 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800165 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400166 private final boolean mShowSilentToggle;
167 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500168 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400169 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400170 private final ActivityStarter mActivityStarter;
171 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500172
Jason Monk361915c2017-03-21 20:33:59 -0400173 /**
174 * @param context everything needs a context :(
175 */
176 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700177 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400178 mWindowManagerFuncs = windowManagerFuncs;
179 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
180 mDreamManager = IDreamManager.Stub.asInterface(
181 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000182 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
183 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800184 mLockPatternUtils = new LockPatternUtils(mContext);
185 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000186 mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class);
Jason Monk361915c2017-03-21 20:33:59 -0400187
188 // receive broadcasts
189 IntentFilter filter = new IntentFilter();
190 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
191 filter.addAction(Intent.ACTION_SCREEN_OFF);
Jayachandran C142eae02019-12-13 19:29:20 -0800192 filter.addAction(TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000193 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400194
195 ConnectivityManager cm = (ConnectivityManager)
196 context.getSystemService(Context.CONNECTIVITY_SERVICE);
197 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
198
199 // get notified of phone state changes
200 TelephonyManager telephonyManager =
201 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
202 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
203 mContext.getContentResolver().registerContentObserver(
204 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
205 mAirplaneModeObserver);
206 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
207 mHasVibrator = vibrator != null && vibrator.hasVibrator();
208
209 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
210 R.bool.config_useFixedVolume);
211
212 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500213 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400214 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700215
216 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500217
Steve Elliott4c868852019-03-14 16:25:41 -0400218 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400219 KeyguardStateController keyguardStateController =
220 Dependency.get(KeyguardStateController.class);
221 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
222 @Override
223 public void onUnlockedChanged() {
224 if (mDialog != null && mDialog.mPanelController != null) {
225 boolean locked = !keyguardStateController.canDismissLockScreen();
226 mDialog.mPanelController.onDeviceLockStateChanged(locked);
227 }
228 }
229 });
Jason Monk361915c2017-03-21 20:33:59 -0400230 }
231
232 /**
233 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400234 *
Jason Monk361915c2017-03-21 20:33:59 -0400235 * @param keyguardShowing True if keyguard is showing
236 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400237 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
238 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400239 mKeyguardShowing = keyguardShowing;
240 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400241 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400242 if (mDialog != null) {
243 mDialog.dismiss();
244 mDialog = null;
245 // Show delayed, so that the dismiss of the previous dialog completes
246 mHandler.sendEmptyMessage(MESSAGE_SHOW);
247 } else {
248 handleShow();
249 }
250 }
251
Charles He9851a8d2017-10-10 17:31:30 +0100252 /**
253 * Dismiss the global actions dialog, if it's currently shown
254 */
255 public void dismissDialog() {
256 mHandler.removeMessages(MESSAGE_DISMISS);
257 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
258 }
259
Jason Monk361915c2017-03-21 20:33:59 -0400260 private void awakenIfNecessary() {
261 if (mDreamManager != null) {
262 try {
263 if (mDreamManager.isDreaming()) {
264 mDreamManager.awaken();
265 }
266 } catch (RemoteException e) {
267 // we tried
268 }
269 }
270 }
271
272 private void handleShow() {
273 awakenIfNecessary();
274 mDialog = createDialog();
275 prepareDialog();
276
277 // If we only have 1 item and it's a simple press action, just do this action.
278 if (mAdapter.getCount() == 1
279 && mAdapter.getItem(0) instanceof SinglePressAction
280 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
281 ((SinglePressAction) mAdapter.getItem(0)).onPress();
282 } else {
283 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
284 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100285 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400286 mDialog.getWindow().setAttributes(attrs);
287 mDialog.show();
288 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400289 }
290 }
291
292 /**
293 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400294 *
Jason Monk361915c2017-03-21 20:33:59 -0400295 * @return A new dialog.
296 */
297 private ActionsDialog createDialog() {
298 // Simple toggle style if there's no vibrator, otherwise use a tri-state
299 if (!mHasVibrator) {
300 mSilentModeAction = new SilentModeToggleAction();
301 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700302 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400303 }
304 mAirplaneModeOn = new ToggleAction(
305 R.drawable.ic_lock_airplane_mode,
306 R.drawable.ic_lock_airplane_mode_off,
307 R.string.global_actions_toggle_airplane_mode,
308 R.string.global_actions_airplane_mode_on_status,
309 R.string.global_actions_airplane_mode_off_status) {
310
311 void onToggle(boolean on) {
Inseob Kim5e82f732019-11-08 15:08:38 +0900312 if (mHasTelephony && TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400313 mIsWaitingForEcmExit = true;
314 // Launch ECM exit dialog
315 Intent ecmDialogIntent =
Jayachandran C142eae02019-12-13 19:29:20 -0800316 new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
Jason Monk361915c2017-03-21 20:33:59 -0400317 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
318 mContext.startActivity(ecmDialogIntent);
319 } else {
320 changeAirplaneModeSystemSetting(on);
321 }
322 }
323
324 @Override
325 protected void changeStateFromPress(boolean buttonOn) {
326 if (!mHasTelephony) return;
327
328 // In ECM mode airplane state cannot be changed
Inseob Kim5e82f732019-11-08 15:08:38 +0900329 if (!TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400330 mState = buttonOn ? State.TurningOn : State.TurningOff;
331 mAirplaneState = mState;
332 }
333 }
334
335 public boolean showDuringKeyguard() {
336 return true;
337 }
338
339 public boolean showBeforeProvisioning() {
340 return false;
341 }
342 };
343 onAirplaneModeChanged();
344
345 mItems = new ArrayList<Action>();
346 String[] defaultActions = mContext.getResources().getStringArray(
347 R.array.config_globalActionsList);
348
349 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000350 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800351 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400352 for (int i = 0; i < defaultActions.length; i++) {
353 String actionKey = defaultActions[i];
354 if (addedKeys.contains(actionKey)) {
355 // If we already have added this, don't add it again.
356 continue;
357 }
358 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
359 mItems.add(new PowerAction());
360 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
361 mItems.add(mAirplaneModeOn);
362 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
363 if (Settings.Global.getInt(mContext.getContentResolver(),
364 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
365 mItems.add(new BugReportAction());
366 }
367 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
368 if (mShowSilentToggle) {
369 mItems.add(mSilentModeAction);
370 }
371 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
372 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
373 addUsersToMenu(mItems);
374 }
375 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
376 mItems.add(getSettingsAction());
377 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700378 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
379 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800380 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700381 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800382 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
385 mItems.add(getVoiceAssistAction());
386 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
387 mItems.add(getAssistAction());
388 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
389 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500390 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
391 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000392 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000393 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000394 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
395 mItems.add(new LogoutAction());
396 mHasLogoutButton = true;
397 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800398 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400399 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800400 mItems.add(new EmergencyDialerAction());
401 }
Jason Monk361915c2017-03-21 20:33:59 -0400402 } else {
403 Log.e(TAG, "Invalid global action key " + actionKey);
404 }
405 // Add here so we don't add more than one.
406 addedKeys.add(actionKey);
407 }
408
409 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400410 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400411 }
412
413 mAdapter = new MyAdapter();
414
Steve Elliott9b87a442019-03-05 10:24:16 -0500415 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400416 mPanelPlugin != null
417 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400418 new GlobalActionsPanelPlugin.Callbacks() {
419 @Override
420 public void dismissGlobalActionsMenu() {
Steve Elliottc3147e22019-11-20 19:34:57 -0500421 dismissDialog();
Steve Elliott4c868852019-03-14 16:25:41 -0400422 }
423
424 @Override
425 public void startPendingIntentDismissingKeyguard(
426 PendingIntent intent) {
427 mActivityStarter
428 .startPendingIntentDismissingKeyguard(intent);
429 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400430 },
431 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500432 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400433
Matt Pietalfd3b6cb2020-01-08 09:43:54 -0500434 ActionsDialog dialog = new ActionsDialog(
435 mContext, mAdapter, panelViewController, isControlsEnabled(mContext));
Jason Monk361915c2017-03-21 20:33:59 -0400436 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700437 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400438
439 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800440 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400441
442 return dialog;
443 }
444
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800445 private boolean shouldDisplayLockdown() {
446 int userId = getCurrentUser().id;
447 // Lockdown is meaningless without a place to go.
448 if (!mKeyguardManager.isDeviceSecure(userId)) {
449 return false;
450 }
451
452 // Only show the lockdown button if the device isn't locked down (for whatever reason).
453 int state = mLockPatternUtils.getStrongAuthForUser(userId);
454 return (state == STRONG_AUTH_NOT_REQUIRED
455 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
456 }
457
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700458 @Override
459 public void onUiModeChanged() {
460 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700461 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400462 mDialog.refreshDialog();
463 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700464 }
465
466 public void destroy() {
467 Dependency.get(ConfigurationController.class).removeCallback(this);
468 }
469
Jason Monk361915c2017-03-21 20:33:59 -0400470 private final class PowerAction extends SinglePressAction implements LongPressAction {
471 private PowerAction() {
472 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400473 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400474 }
475
476 @Override
477 public boolean onLongPress() {
478 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
479 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
480 mWindowManagerFuncs.reboot(true);
481 return true;
482 }
483 return false;
484 }
485
486 @Override
487 public boolean showDuringKeyguard() {
488 return true;
489 }
490
491 @Override
492 public boolean showBeforeProvisioning() {
493 return true;
494 }
495
496 @Override
497 public void onPress() {
498 // shutdown by making sure radio and power are handled accordingly.
499 mWindowManagerFuncs.shutdown();
500 }
501 }
502
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400503 private abstract class EmergencyAction extends SinglePressAction {
504 EmergencyAction(int iconResId, int messageResId) {
505 super(iconResId, messageResId);
506 }
507
508 @Override
509 public boolean shouldBeSeparated() {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -0500510 return !isControlsEnabled(mContext);
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400511 }
512
513 @Override
514 public View create(
515 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
516 View v = super.create(context, convertView, parent, inflater);
517 int textColor;
518 if (shouldBeSeparated()) {
519 textColor = v.getResources().getColor(
520 com.android.systemui.R.color.global_actions_alert_text);
521 } else {
522 textColor = v.getResources().getColor(
523 com.android.systemui.R.color.global_actions_text);
524 }
525 TextView messageView = v.findViewById(R.id.message);
526 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400527 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400528 ImageView icon = (ImageView) v.findViewById(R.id.icon);
529 icon.getDrawable().setTint(textColor);
530 return v;
531 }
532
533 @Override
534 public boolean showDuringKeyguard() {
535 return true;
536 }
537
538 @Override
539 public boolean showBeforeProvisioning() {
540 return true;
541 }
542 }
543
544 private class EmergencyAffordanceAction extends EmergencyAction {
545 EmergencyAffordanceAction() {
546 super(R.drawable.emergency_icon,
547 R.string.global_action_emergency);
548 }
549
550 @Override
551 public void onPress() {
552 mEmergencyAffordanceManager.performEmergencyCall();
553 }
554 }
555
556 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800557 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400558 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800559 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800560 }
561
562 @Override
563 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800564 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Fan Zhangf9914762019-11-01 15:58:38 -0700565 Intent intent = mContext.getSystemService(TelecomManager.class)
566 .createLaunchEmergencyDialerIntent(null /* number */);
Leo Hsue4cc3122019-03-06 15:25:28 +0800567 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
568 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
569 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800570 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
571 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800572 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
573 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800574 }
575
Jason Monk361915c2017-03-21 20:33:59 -0400576 private final class RestartAction extends SinglePressAction implements LongPressAction {
577 private RestartAction() {
578 super(R.drawable.ic_restart, R.string.global_action_restart);
579 }
580
581 @Override
582 public boolean onLongPress() {
583 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
584 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
585 mWindowManagerFuncs.reboot(true);
586 return true;
587 }
588 return false;
589 }
590
591 @Override
592 public boolean showDuringKeyguard() {
593 return true;
594 }
595
596 @Override
597 public boolean showBeforeProvisioning() {
598 return true;
599 }
600
601 @Override
602 public void onPress() {
603 mWindowManagerFuncs.reboot(false);
604 }
605 }
606
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400607 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500608 public ScreenshotAction() {
609 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
610 }
611
612 @Override
613 public void onPress() {
614 // Add a little delay before executing, to give the
615 // dialog a chance to go away before it takes a
616 // screenshot.
617 // TODO: instead, omit global action dialog layer
618 mHandler.postDelayed(new Runnable() {
619 @Override
620 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400621 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500622 MetricsLogger.action(mContext,
623 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
624 }
625 }, 500);
626 }
627
628 @Override
629 public boolean showDuringKeyguard() {
630 return true;
631 }
632
633 @Override
634 public boolean showBeforeProvisioning() {
635 return false;
636 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400637
638 @Override
639 public boolean onLongPress() {
640 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
641 mScreenRecordHelper.launchRecordPrompt();
642 } else {
643 onPress();
644 }
645 return true;
646 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500647 }
648
Jason Monk361915c2017-03-21 20:33:59 -0400649 private class BugReportAction extends SinglePressAction implements LongPressAction {
650
651 public BugReportAction() {
652 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
653 }
654
655 @Override
656 public void onPress() {
657 // don't actually trigger the bugreport if we are running stability
658 // tests via monkey
659 if (ActivityManager.isUserAMonkey()) {
660 return;
661 }
662 // Add a little delay before executing, to give the
663 // dialog a chance to go away before it takes a
664 // screenshot.
665 mHandler.postDelayed(new Runnable() {
666 @Override
667 public void run() {
668 try {
669 // Take an "interactive" bugreport.
670 MetricsLogger.action(mContext,
671 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Paul Changc40f5782019-11-04 18:55:31 +0800672 if (!ActivityManager.getService().launchBugReportHandlerApp()) {
673 Log.w(TAG, "Bugreport handler could not be launched");
674 ActivityManager.getService().requestInteractiveBugReport();
675 }
Jason Monk361915c2017-03-21 20:33:59 -0400676 } catch (RemoteException e) {
677 }
678 }
679 }, 500);
680 }
681
682 @Override
683 public boolean onLongPress() {
684 // don't actually trigger the bugreport if we are running stability
685 // tests via monkey
686 if (ActivityManager.isUserAMonkey()) {
687 return false;
688 }
689 try {
690 // Take a "full" bugreport.
691 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100692 ActivityManager.getService().requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400693 } catch (RemoteException e) {
694 }
695 return false;
696 }
697
698 public boolean showDuringKeyguard() {
699 return true;
700 }
701
702 @Override
703 public boolean showBeforeProvisioning() {
704 return false;
705 }
Jason Monk361915c2017-03-21 20:33:59 -0400706 }
707
Alex Chau04458852017-11-27 18:21:23 +0000708 private final class LogoutAction extends SinglePressAction {
709 private LogoutAction() {
710 super(R.drawable.ic_logout, R.string.global_action_logout);
711 }
712
713 @Override
714 public boolean showDuringKeyguard() {
715 return true;
716 }
717
718 @Override
719 public boolean showBeforeProvisioning() {
720 return false;
721 }
722
723 @Override
724 public void onPress() {
725 // Add a little delay before executing, to give the dialog a chance to go away before
726 // switching user
727 mHandler.postDelayed(() -> {
728 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000729 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000730 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000731 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000732 } catch (RemoteException re) {
733 Log.e(TAG, "Couldn't logout user " + re);
734 }
735 }, 500);
736 }
737 }
738
Jason Monk361915c2017-03-21 20:33:59 -0400739 private Action getSettingsAction() {
740 return new SinglePressAction(R.drawable.ic_settings,
741 R.string.global_action_settings) {
742
743 @Override
744 public void onPress() {
745 Intent intent = new Intent(Settings.ACTION_SETTINGS);
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
Jason Monk361915c2017-03-21 20:33:59 -0400762 private Action getAssistAction() {
763 return new SinglePressAction(R.drawable.ic_action_assist_focused,
764 R.string.global_action_assist) {
765 @Override
766 public void onPress() {
767 Intent intent = new Intent(Intent.ACTION_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 getVoiceAssistAction() {
785 return new SinglePressAction(R.drawable.ic_voice_search,
786 R.string.global_action_voice_assist) {
787 @Override
788 public void onPress() {
789 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
790 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
791 mContext.startActivity(intent);
792 }
793
794 @Override
795 public boolean showDuringKeyguard() {
796 return true;
797 }
798
799 @Override
800 public boolean showBeforeProvisioning() {
801 return true;
802 }
803 };
804 }
805
806 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400807 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400808 R.string.global_action_lockdown) {
809
810 @Override
811 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700812 new LockPatternUtils(mContext)
813 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
814 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400815 try {
816 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100817 // Lock profiles (if any) on the background thread.
818 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
819 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400820 } catch (RemoteException e) {
821 Log.e(TAG, "Error while trying to lock device.", e);
822 }
823 }
824
825 @Override
826 public boolean showDuringKeyguard() {
827 return true;
828 }
829
830 @Override
831 public boolean showBeforeProvisioning() {
832 return false;
833 }
834 };
835 }
836
Pavel Grafov059021b2018-05-02 13:44:46 +0100837 private void lockProfiles() {
838 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
839 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
840 final int currentUserId = getCurrentUser().id;
841 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
842 for (final int id : profileIds) {
843 if (id != currentUserId) {
844 tm.setDeviceLockedForUser(id, true);
845 }
846 }
847 }
848
Jason Monk361915c2017-03-21 20:33:59 -0400849 private UserInfo getCurrentUser() {
850 try {
851 return ActivityManager.getService().getCurrentUser();
852 } catch (RemoteException re) {
853 return null;
854 }
855 }
856
857 private boolean isCurrentUserOwner() {
858 UserInfo currentUser = getCurrentUser();
859 return currentUser == null || currentUser.isPrimary();
860 }
861
862 private void addUsersToMenu(ArrayList<Action> items) {
863 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
864 if (um.isUserSwitcherEnabled()) {
865 List<UserInfo> users = um.getUsers();
866 UserInfo currentUser = getCurrentUser();
867 for (final UserInfo user : users) {
868 if (user.supportsSwitchToByUser()) {
869 boolean isCurrentUser = currentUser == null
870 ? user.id == 0 : (currentUser.id == user.id);
871 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
872 : null;
873 SinglePressAction switchToUser = new SinglePressAction(
874 R.drawable.ic_menu_cc, icon,
875 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400876 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400877 public void onPress() {
878 try {
879 ActivityManager.getService().switchUser(user.id);
880 } catch (RemoteException re) {
881 Log.e(TAG, "Couldn't switch user " + re);
882 }
883 }
884
885 public boolean showDuringKeyguard() {
886 return true;
887 }
888
889 public boolean showBeforeProvisioning() {
890 return false;
891 }
892 };
893 items.add(switchToUser);
894 }
895 }
896 }
897 }
898
899 private void prepareDialog() {
900 refreshSilentMode();
901 mAirplaneModeOn.updateState(mAirplaneState);
902 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400903 if (mShowSilentToggle) {
904 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000905 mBroadcastDispatcher.registerReceiver(mRingerModeReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400906 }
907 }
908
909 private void refreshSilentMode() {
910 if (!mHasVibrator) {
911 final boolean silentModeOn =
912 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400913 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400914 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
915 }
916 }
917
918 /** {@inheritDoc} */
919 public void onDismiss(DialogInterface dialog) {
Steve Elliottc3147e22019-11-20 19:34:57 -0500920 if (mDialog == dialog) {
921 mDialog = null;
922 }
Jason Monk361915c2017-03-21 20:33:59 -0400923 mWindowManagerFuncs.onGlobalActionsHidden();
924 if (mShowSilentToggle) {
925 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000926 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400927 } catch (IllegalArgumentException ie) {
928 // ignore this
929 Log.w(TAG, ie);
930 }
931 }
932 }
933
934 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800935 public void onShow(DialogInterface dialog) {
936 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
937 }
938
Jason Monk361915c2017-03-21 20:33:59 -0400939 /**
940 * The adapter used for the list within the global actions dialog, taking
941 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400942 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
943 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400944 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
945 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500946 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400947 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400948 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400949 for (int i = 0; i < mItems.size(); i++) {
950 final Action action = mItems.get(i);
951
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400952 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
953 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400954 }
Jason Monk361915c2017-03-21 20:33:59 -0400955 }
956 return count;
957 }
958
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400959 private boolean shouldBeShown(Action action) {
960 if (mKeyguardShowing && !action.showDuringKeyguard()) {
961 return false;
962 }
963 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
964 return false;
965 }
966 return true;
967 }
968
969 @Override
970 public int countSeparatedItems() {
971 return countItems(true);
972 }
973
974 @Override
975 public int countListItems() {
976 return countItems(false);
977 }
978
979 @Override
980 public int getCount() {
981 return countSeparatedItems() + countListItems();
982 }
983
Jason Monk361915c2017-03-21 20:33:59 -0400984 @Override
985 public boolean isEnabled(int position) {
986 return getItem(position).isEnabled();
987 }
988
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500989 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400990 public boolean areAllItemsEnabled() {
991 return false;
992 }
993
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500994 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400995 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400996 int filteredPos = 0;
997 for (int i = 0; i < mItems.size(); i++) {
998 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400999 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001000 continue;
1001 }
1002 if (filteredPos == position) {
1003 return action;
1004 }
1005 filteredPos++;
1006 }
1007
1008 throw new IllegalArgumentException("position " + position
1009 + " out of range of showable actions"
1010 + ", filtered count=" + getCount()
1011 + ", keyguardshowing=" + mKeyguardShowing
1012 + ", provisioned=" + mDeviceProvisioned);
1013 }
1014
1015
1016 public long getItemId(int position) {
1017 return position;
1018 }
1019
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001020 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001021 public View getView(int position, View convertView, ViewGroup parent) {
1022 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001023 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001024 view.setOnClickListener(v -> onClickItem(position));
1025 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001026 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001027 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001028
1029 @Override
1030 public boolean onLongClickItem(int position) {
1031 final Action action = mAdapter.getItem(position);
1032 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001033 if (mDialog != null) {
1034 mDialog.dismiss();
1035 } else {
1036 Log.w(TAG, "Action long-clicked while mDialog is null.");
1037 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001038 return ((LongPressAction) action).onLongPress();
1039 }
1040 return false;
1041 }
1042
1043 @Override
1044 public void onClickItem(int position) {
1045 Action item = mAdapter.getItem(position);
1046 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001047 if (mDialog != null) {
1048 mDialog.dismiss();
1049 } else {
1050 Log.w(TAG, "Action clicked while mDialog is null.");
1051 }
1052 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001053 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001054 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001055
1056 @Override
1057 public boolean shouldBeSeparated(int position) {
1058 return getItem(position).shouldBeSeparated();
1059 }
Jason Monk361915c2017-03-21 20:33:59 -04001060 }
1061
1062 // note: the scheme below made more sense when we were planning on having
1063 // 8 different things in the global actions dialog. seems overkill with
1064 // only 3 items now, but may as well keep this flexible approach so it will
1065 // be easy should someone decide at the last minute to include something
1066 // else, such as 'enable wifi', or 'enable bluetooth'
1067
1068 /**
1069 * What each item in the global actions dialog must be able to support.
1070 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001071 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001072 /**
1073 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001074 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001075 */
1076 CharSequence getLabelForAccessibility(Context context);
1077
1078 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1079
1080 void onPress();
1081
1082 /**
1083 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001084 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001085 */
1086 boolean showDuringKeyguard();
1087
1088 /**
1089 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001090 * device is provisioned.onlongpress
1091 *
Jason Monk361915c2017-03-21 20:33:59 -04001092 */
1093 boolean showBeforeProvisioning();
1094
1095 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001096
1097 default boolean shouldBeSeparated() {
1098 return false;
1099 }
Jason Monk361915c2017-03-21 20:33:59 -04001100 }
1101
1102 /**
1103 * An action that also supports long press.
1104 */
1105 private interface LongPressAction extends Action {
1106 boolean onLongPress();
1107 }
1108
1109 /**
1110 * A single press action maintains no state, just responds to a press
1111 * and takes an action.
1112 */
1113 private static abstract class SinglePressAction implements Action {
1114 private final int mIconResId;
1115 private final Drawable mIcon;
1116 private final int mMessageResId;
1117 private final CharSequence mMessage;
1118
1119 protected SinglePressAction(int iconResId, int messageResId) {
1120 mIconResId = iconResId;
1121 mMessageResId = messageResId;
1122 mMessage = null;
1123 mIcon = null;
1124 }
1125
1126 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1127 mIconResId = iconResId;
1128 mMessageResId = 0;
1129 mMessage = message;
1130 mIcon = icon;
1131 }
1132
1133 public boolean isEnabled() {
1134 return true;
1135 }
1136
1137 public String getStatus() {
1138 return null;
1139 }
1140
1141 abstract public void onPress();
1142
1143 public CharSequence getLabelForAccessibility(Context context) {
1144 if (mMessage != null) {
1145 return mMessage;
1146 } else {
1147 return context.getString(mMessageResId);
1148 }
1149 }
1150
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001151 protected int getActionLayoutId(Context context) {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001152 if (isControlsEnabled(context)) {
1153 return com.android.systemui.R.layout.global_actions_grid_item_v2;
1154 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001155 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001156 }
1157
Jason Monk361915c2017-03-21 20:33:59 -04001158 public View create(
1159 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001160 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001161 false);
Jason Monk361915c2017-03-21 20:33:59 -04001162
1163 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1164 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001165 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001166
1167 TextView statusView = (TextView) v.findViewById(R.id.status);
1168 final String status = getStatus();
1169 if (!TextUtils.isEmpty(status)) {
1170 statusView.setText(status);
1171 } else {
1172 statusView.setVisibility(View.GONE);
1173 }
1174 if (mIcon != null) {
1175 icon.setImageDrawable(mIcon);
1176 icon.setScaleType(ScaleType.CENTER_CROP);
1177 } else if (mIconResId != 0) {
1178 icon.setImageDrawable(context.getDrawable(mIconResId));
1179 }
1180 if (mMessage != null) {
1181 messageView.setText(mMessage);
1182 } else {
1183 messageView.setText(mMessageResId);
1184 }
1185
1186 return v;
1187 }
1188 }
1189
1190 /**
1191 * A toggle action knows whether it is on or off, and displays an icon
1192 * and status message accordingly.
1193 */
1194 private static abstract class ToggleAction implements Action {
1195
1196 enum State {
1197 Off(false),
1198 TurningOn(true),
1199 TurningOff(true),
1200 On(false);
1201
1202 private final boolean inTransition;
1203
1204 State(boolean intermediate) {
1205 inTransition = intermediate;
1206 }
1207
1208 public boolean inTransition() {
1209 return inTransition;
1210 }
1211 }
1212
1213 protected State mState = State.Off;
1214
1215 // prefs
1216 protected int mEnabledIconResId;
1217 protected int mDisabledIconResid;
1218 protected int mMessageResId;
1219 protected int mEnabledStatusMessageResId;
1220 protected int mDisabledStatusMessageResId;
1221
1222 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001223 * @param enabledIconResId The icon for when this action is on.
1224 * @param disabledIconResid The icon for when this action is off.
1225 * @param message The general information message, e.g 'Silent Mode'
1226 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001227 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1228 */
1229 public ToggleAction(int enabledIconResId,
1230 int disabledIconResid,
1231 int message,
1232 int enabledStatusMessageResId,
1233 int disabledStatusMessageResId) {
1234 mEnabledIconResId = enabledIconResId;
1235 mDisabledIconResid = disabledIconResid;
1236 mMessageResId = message;
1237 mEnabledStatusMessageResId = enabledStatusMessageResId;
1238 mDisabledStatusMessageResId = disabledStatusMessageResId;
1239 }
1240
1241 /**
1242 * Override to make changes to resource IDs just before creating the
1243 * View.
1244 */
1245 void willCreate() {
1246
1247 }
1248
1249 @Override
1250 public CharSequence getLabelForAccessibility(Context context) {
1251 return context.getString(mMessageResId);
1252 }
1253
1254 public View create(Context context, View convertView, ViewGroup parent,
1255 LayoutInflater inflater) {
1256 willCreate();
1257
1258 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001259 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001260
1261 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1262 TextView messageView = (TextView) v.findViewById(R.id.message);
1263 TextView statusView = (TextView) v.findViewById(R.id.status);
1264 final boolean enabled = isEnabled();
1265
1266 if (messageView != null) {
1267 messageView.setText(mMessageResId);
1268 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001269 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001270 }
1271
1272 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1273 if (icon != null) {
1274 icon.setImageDrawable(context.getDrawable(
1275 (on ? mEnabledIconResId : mDisabledIconResid)));
1276 icon.setEnabled(enabled);
1277 }
1278
1279 if (statusView != null) {
1280 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1281 statusView.setVisibility(View.VISIBLE);
1282 statusView.setEnabled(enabled);
1283 }
1284 v.setEnabled(enabled);
1285
1286 return v;
1287 }
1288
1289 public final void onPress() {
1290 if (mState.inTransition()) {
1291 Log.w(TAG, "shouldn't be able to toggle when in transition");
1292 return;
1293 }
1294
1295 final boolean nowOn = !(mState == State.On);
1296 onToggle(nowOn);
1297 changeStateFromPress(nowOn);
1298 }
1299
1300 public boolean isEnabled() {
1301 return !mState.inTransition();
1302 }
1303
1304 /**
1305 * Implementations may override this if their state can be in on of the intermediate
1306 * states until some notification is received (e.g airplane mode is 'turning off' until
1307 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001308 *
Jason Monk361915c2017-03-21 20:33:59 -04001309 * @param buttonOn Whether the button was turned on or off
1310 */
1311 protected void changeStateFromPress(boolean buttonOn) {
1312 mState = buttonOn ? State.On : State.Off;
1313 }
1314
1315 abstract void onToggle(boolean on);
1316
1317 public void updateState(State state) {
1318 mState = state;
1319 }
1320 }
1321
1322 private class SilentModeToggleAction extends ToggleAction {
1323 public SilentModeToggleAction() {
1324 super(R.drawable.ic_audio_vol_mute,
1325 R.drawable.ic_audio_vol,
1326 R.string.global_action_toggle_silent_mode,
1327 R.string.global_action_silent_mode_on_status,
1328 R.string.global_action_silent_mode_off_status);
1329 }
1330
1331 void onToggle(boolean on) {
1332 if (on) {
1333 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1334 } else {
1335 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1336 }
1337 }
1338
1339 public boolean showDuringKeyguard() {
1340 return true;
1341 }
1342
1343 public boolean showBeforeProvisioning() {
1344 return false;
1345 }
1346 }
1347
1348 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1349
Jason Monk16fbd9d2017-04-27 14:28:49 -04001350 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001351
1352 private final AudioManager mAudioManager;
1353 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001354
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001355 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001356 mAudioManager = audioManager;
1357 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001358 }
1359
1360 private int ringerModeToIndex(int ringerMode) {
1361 // They just happen to coincide
1362 return ringerMode;
1363 }
1364
1365 private int indexToRingerMode(int index) {
1366 // They just happen to coincide
1367 return index;
1368 }
1369
1370 @Override
1371 public CharSequence getLabelForAccessibility(Context context) {
1372 return null;
1373 }
1374
1375 public View create(Context context, View convertView, ViewGroup parent,
1376 LayoutInflater inflater) {
1377 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1378
1379 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1380 for (int i = 0; i < 3; i++) {
1381 View itemView = v.findViewById(ITEM_IDS[i]);
1382 itemView.setSelected(selectedIndex == i);
1383 // Set up click handler
1384 itemView.setTag(i);
1385 itemView.setOnClickListener(this);
1386 }
1387 return v;
1388 }
1389
1390 public void onPress() {
1391 }
1392
1393 public boolean showDuringKeyguard() {
1394 return true;
1395 }
1396
1397 public boolean showBeforeProvisioning() {
1398 return false;
1399 }
1400
1401 public boolean isEnabled() {
1402 return true;
1403 }
1404
1405 void willCreate() {
1406 }
1407
1408 public void onClick(View v) {
1409 if (!(v.getTag() instanceof Integer)) return;
1410
1411 int index = (Integer) v.getTag();
1412 mAudioManager.setRingerMode(indexToRingerMode(index));
1413 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1414 }
1415 }
1416
1417 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1418 public void onReceive(Context context, Intent intent) {
1419 String action = intent.getAction();
1420 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1421 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1422 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1423 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001424 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001425 }
Jayachandran C142eae02019-12-13 19:29:20 -08001426 } else if (TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001427 // Airplane mode can be changed after ECM exits if airplane toggle button
1428 // is pressed during ECM mode
1429 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1430 mIsWaitingForEcmExit) {
1431 mIsWaitingForEcmExit = false;
1432 changeAirplaneModeSystemSetting(true);
1433 }
1434 }
1435 }
1436 };
1437
1438 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1439 @Override
1440 public void onServiceStateChanged(ServiceState serviceState) {
1441 if (!mHasTelephony) return;
1442 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1443 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1444 mAirplaneModeOn.updateState(mAirplaneState);
1445 mAdapter.notifyDataSetChanged();
1446 }
1447 };
1448
1449 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1450 @Override
1451 public void onReceive(Context context, Intent intent) {
1452 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1453 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1454 }
1455 }
1456 };
1457
1458 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1459 @Override
1460 public void onChange(boolean selfChange) {
1461 onAirplaneModeChanged();
1462 }
1463 };
1464
1465 private static final int MESSAGE_DISMISS = 0;
1466 private static final int MESSAGE_REFRESH = 1;
1467 private static final int MESSAGE_SHOW = 2;
1468 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1469
1470 private Handler mHandler = new Handler() {
1471 public void handleMessage(Message msg) {
1472 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001473 case MESSAGE_DISMISS:
1474 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001475 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1476 mDialog.dismissImmediately();
1477 } else {
1478 mDialog.dismiss();
1479 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001480 mDialog = null;
1481 }
1482 break;
1483 case MESSAGE_REFRESH:
1484 refreshSilentMode();
1485 mAdapter.notifyDataSetChanged();
1486 break;
1487 case MESSAGE_SHOW:
1488 handleShow();
1489 break;
Jason Monk361915c2017-03-21 20:33:59 -04001490 }
1491 }
1492 };
1493
1494 private void onAirplaneModeChanged() {
1495 // Let the service state callbacks handle the state.
1496 if (mHasTelephony) return;
1497
1498 boolean airplaneModeOn = Settings.Global.getInt(
1499 mContext.getContentResolver(),
1500 Settings.Global.AIRPLANE_MODE_ON,
1501 0) == 1;
1502 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1503 mAirplaneModeOn.updateState(mAirplaneState);
1504 }
1505
1506 /**
1507 * Change the airplane mode system setting
1508 */
1509 private void changeAirplaneModeSystemSetting(boolean on) {
1510 Settings.Global.putInt(
1511 mContext.getContentResolver(),
1512 Settings.Global.AIRPLANE_MODE_ON,
1513 on ? 1 : 0);
1514 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1515 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1516 intent.putExtra("state", on);
1517 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1518 if (!mHasTelephony) {
1519 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1520 }
1521 }
1522
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001523 private static final class ActionsDialog extends Dialog implements DialogInterface,
1524 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001525
Jason Monk16fbd9d2017-04-27 14:28:49 -04001526 private final Context mContext;
1527 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001528 private final IStatusBarService mStatusBarService;
1529 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001530 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001531 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001532 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001533 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001534 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001535 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001536 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001537 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001538 private boolean mHadTopUi;
1539 private final StatusBarWindowController mStatusBarWindowController;
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001540 private boolean mControlsEnabled;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001541
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001542 ActionsDialog(Context context, MyAdapter adapter,
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001543 GlobalActionsPanelPlugin.PanelViewController plugin,
1544 boolean controlsEnabled) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001545 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001546 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001547 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001548 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001549 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001550 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001551 mControlsEnabled = controlsEnabled;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001552
1553 // Window initialization
1554 Window window = getWindow();
1555 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001556 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1557 window.getDecorView();
1558 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1559 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1560 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1561 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1562 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001563 window.addFlags(
1564 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001565 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001566 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001567 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1568 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1569 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001570 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Tiger Huang4a7835f2019-11-06 00:07:56 +08001571 window.setFitWindowInsetsTypes(0 /* types */);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001572 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001573
1574 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001575 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001576 }
1577
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001578 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001579 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001580 }
1581
1582 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001583 int rotation = RotationUtils.getRotation(mContext);
1584 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1585 if (rotation != RotationUtils.ROTATION_NONE) {
1586 if (rotationLocked) {
1587 if (mResetOrientationData == null) {
1588 mResetOrientationData = new ResetOrientationData();
1589 mResetOrientationData.locked = true;
1590 mResetOrientationData.rotation = rotation;
1591 }
1592
1593 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001594 // This call is posted so that the rotation does not change until post-layout,
1595 // otherwise onConfigurationChanged() may not get invoked.
1596 mGlobalActionsLayout.post(() ->
1597 RotationPolicy.setRotationLockAtAngle(
1598 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001599 }
1600 } else {
1601 if (!rotationLocked) {
1602 if (mResetOrientationData == null) {
1603 mResetOrientationData = new ResetOrientationData();
1604 mResetOrientationData.locked = false;
1605 }
1606
1607 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001608 // This call is posted so that the rotation does not change until post-layout,
1609 // otherwise onConfigurationChanged() may not get invoked.
1610 mGlobalActionsLayout.post(() ->
1611 RotationPolicy.setRotationLockAtAngle(
1612 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001613 }
1614
Steve Elliott53f12ae2019-05-13 17:14:15 -04001615 // Disable rotation suggestions, if enabled
1616 setRotationSuggestionsEnabled(false);
1617
Steve Elliott300b48f2019-05-29 14:13:50 -04001618 FrameLayout panelContainer =
1619 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001620 FrameLayout.LayoutParams panelParams =
1621 new FrameLayout.LayoutParams(
1622 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001623 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001624 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001625 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1626 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001627 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001628 }
1629
1630 private void initializeLayout() {
1631 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001632 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001633 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001634 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001635 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001636 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001637 @Override
1638 public boolean dispatchPopulateAccessibilityEvent(
1639 View host, AccessibilityEvent event) {
1640 // Populate the title here, just as Activity does
1641 event.getText().add(mContext.getString(R.string.global_actions));
1642 return true;
1643 }
1644 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001645 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001646 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001647
Steve Elliott86ef6282019-05-08 14:45:04 -04001648 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001649 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001650 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001651 if (mBackgroundDrawable == null) {
1652 mBackgroundDrawable = new ScrimDrawable();
1653 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1654 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001655 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001656 }
1657
Steve Elliott300b48f2019-05-29 14:13:50 -04001658 private void fixNavBarClipping() {
1659 ViewGroup content = findViewById(android.R.id.content);
1660 content.setClipChildren(false);
1661 content.setClipToPadding(false);
1662 ViewGroup contentParent = (ViewGroup) content.getParent();
1663 contentParent.setClipChildren(false);
1664 contentParent.setClipToPadding(false);
1665 }
1666
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001667 private int getGlobalActionsLayoutId(Context context) {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001668 if (mControlsEnabled) {
1669 return com.android.systemui.R.layout.global_actions_grid_v2;
1670 }
1671
Steve Elliottdc165632019-05-23 14:26:31 -04001672 int rotation = RotationUtils.getRotation(context);
1673 boolean useGridLayout = isForceGridEnabled(context)
1674 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1675 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001676 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001677 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001678 } else {
1679 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001680 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001681 } else {
1682 if (useGridLayout) {
1683 return com.android.systemui.R.layout.global_actions_grid;
1684 } else {
1685 return com.android.systemui.R.layout.global_actions_column;
1686 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001687 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001688 }
1689
Jason Monk361915c2017-03-21 20:33:59 -04001690 @Override
1691 protected void onStart() {
1692 super.setCanceledOnTouchOutside(true);
1693 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001694 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001695
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001696 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001697 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001698 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001699 updateColors(colors, false /* animate */);
1700 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001701 }
1702
1703 /**
1704 * Updates background and system bars according to current GradientColors.
1705 * @param colors Colors and hints to use.
1706 * @param animate Interpolates gradient if true, just sets otherwise.
1707 */
1708 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001709 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001710 return;
1711 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001712 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001713 View decorView = getWindow().getDecorView();
1714 if (colors.supportsDarkText()) {
1715 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001716 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001717 } else {
1718 decorView.setSystemUiVisibility(0);
1719 }
Jason Monk361915c2017-03-21 20:33:59 -04001720 }
1721
1722 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001723 protected void onStop() {
1724 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001725 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001726 }
1727
1728 @Override
1729 public void show() {
1730 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001731 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001732 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1733 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001734 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001735 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1736 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001737 mGlobalActionsLayout.setAlpha(0);
1738 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001739 .alpha(1)
1740 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001741 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001742 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001743 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001744 .setUpdateListener(animation -> {
1745 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001746 * mScrimAlpha * 255);
1747 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001748 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001749 .start();
1750 }
1751
1752 @Override
1753 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001754 if (!mShowing) {
1755 return;
1756 }
1757 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001758 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001759 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001760 mGlobalActionsLayout.setAlpha(1);
1761 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001762 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001763 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1764 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001765 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001766 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001767 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001768 .setUpdateListener(animation -> {
1769 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001770 * mScrimAlpha * 255);
1771 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001772 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001773 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001774 dismissPanel();
1775 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001776 }
1777
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001778 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001779 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001780 dismissPanel();
1781 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001782 completeDismiss();
1783 }
1784
1785 private void completeDismiss() {
1786 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1787 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001788 }
1789
1790 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001791 if (mPanelController != null) {
1792 mPanelController.onDismissed();
1793 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001794 }
1795
Steve Elliott53f12ae2019-05-13 17:14:15 -04001796 private void setRotationSuggestionsEnabled(boolean enabled) {
1797 try {
1798 final int userId = Binder.getCallingUserHandle().getIdentifier();
1799 final int what = enabled
1800 ? StatusBarManager.DISABLE2_NONE
1801 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1802 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1803 } catch (RemoteException ex) {
1804 throw ex.rethrowFromSystemServer();
1805 }
1806 }
1807
Steve Elliott48f75db2019-05-03 15:03:38 -04001808 private void resetOrientation() {
1809 if (mResetOrientationData != null) {
1810 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1811 mResetOrientationData.rotation);
1812 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001813 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001814 }
1815
Jason Monk361915c2017-03-21 20:33:59 -04001816 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001817 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001818 if (mKeyguardShowing) {
1819 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001820 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1821 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001822 }
1823 } else {
1824 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001825 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1826 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001827 }
1828 }
1829 }
1830
1831 public void setKeyguardShowing(boolean keyguardShowing) {
1832 mKeyguardShowing = keyguardShowing;
1833 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001834
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001835 public void refreshDialog() {
1836 initializeLayout();
1837 mGlobalActionsLayout.updateList();
1838 }
1839
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001840 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001841 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001842 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001843 }
1844 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001845
1846 private static class ResetOrientationData {
1847 public boolean locked;
1848 public int rotation;
1849 }
Jason Monk361915c2017-03-21 20:33:59 -04001850 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001851
1852 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001853 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1854 */
1855 private static boolean isPanelDebugModeEnabled(Context context) {
1856 return Settings.Secure.getInt(context.getContentResolver(),
1857 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1858 }
1859
1860 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001861 * Determines whether or not the Global Actions menu should be forced to
1862 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001863 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001864 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001865 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001866 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001867
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001868
1869 /**
1870 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1871 */
1872 private static boolean shouldUseSeparatedView() {
1873 return true;
1874 }
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001875
1876 private static boolean isControlsEnabled(Context context) {
1877 return Settings.Secure.getInt(
1878 context.getContentResolver(), "systemui.controls_available", 0) == 1;
1879 }
Jason Monk361915c2017-03-21 20:33:59 -04001880}