blob: 73539f9380e6aa8554765275c3e293acf6efd8fa [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
Dave Mankoffc88a2d72020-01-10 15:59:31 -050024import android.annotation.Nullable;
Jason Monk361915c2017-03-21 20:33:59 -040025import android.app.ActivityManager;
26import android.app.Dialog;
Dave Mankoffc88a2d72020-01-10 15:59:31 -050027import android.app.IActivityManager;
Steve Elliott4c868852019-03-14 16:25:41 -040028import android.app.PendingIntent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040029import android.app.StatusBarManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070030import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000031import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010032import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040033import android.content.BroadcastReceiver;
Matt Pietal61266442020-03-17 12:53:44 -040034import android.content.ComponentName;
Dave Mankoffc88a2d72020-01-10 15:59:31 -050035import android.content.ContentResolver;
Jason Monk361915c2017-03-21 20:33:59 -040036import android.content.Context;
37import android.content.DialogInterface;
38import android.content.Intent;
39import android.content.IntentFilter;
Matt Pietal61266442020-03-17 12:53:44 -040040import android.content.SharedPreferences;
Jason Monk361915c2017-03-21 20:33:59 -040041import android.content.pm.UserInfo;
Dave Mankoffc88a2d72020-01-10 15:59:31 -050042import android.content.res.Resources;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.database.ContentObserver;
Matt Pietale0661b62020-01-29 14:35:31 -050044import android.graphics.Color;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.graphics.drawable.Drawable;
46import android.media.AudioManager;
47import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040048import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040049import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040050import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040051import android.os.Message;
52import android.os.RemoteException;
Jason Monk361915c2017-03-21 20:33:59 -040053import android.os.SystemProperties;
54import android.os.UserHandle;
55import android.os.UserManager;
56import android.os.Vibrator;
57import android.provider.Settings;
Jason Monk361915c2017-03-21 20:33:59 -040058import android.service.dreams.IDreamManager;
Inseob Kim5e82f732019-11-08 15:08:38 +090059import android.sysprop.TelephonyProperties;
Fan Zhangf9914762019-11-01 15:58:38 -070060import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040061import android.telephony.PhoneStateListener;
62import android.telephony.ServiceState;
63import android.telephony.TelephonyManager;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080065import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040066import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070067import android.view.ContextThemeWrapper;
Dave Mankoff8df818e2020-02-12 14:22:26 -050068import android.view.IWindowManager;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.view.LayoutInflater;
70import android.view.View;
71import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070072import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.view.WindowManager;
Jason Monk361915c2017-03-21 20:33:59 -040074import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050075import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040076import android.widget.ImageView;
77import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040078import android.widget.TextView;
79
Charles He9851a8d2017-10-10 17:31:30 +010080import com.android.internal.R;
81import com.android.internal.colorextraction.ColorExtractor;
82import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070083import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.logging.MetricsLogger;
85import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040086import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010087import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040088import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050089import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040090import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.internal.widget.LockPatternUtils;
Charles He9851a8d2017-10-10 17:31:30 +010092import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050093import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050094import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040095import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010096import com.android.systemui.colorextraction.SysuiColorExtractor;
Matt Pietal61266442020-03-17 12:53:44 -040097import com.android.systemui.controls.ControlsServiceInfo;
98import com.android.systemui.controls.controller.ControlsController;
Fabian Kozynski0424ab12020-02-21 12:09:17 -050099import com.android.systemui.controls.management.ControlsListingController;
Matt Pietal22231792020-01-23 09:51:09 -0500100import com.android.systemui.controls.ui.ControlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500101import com.android.systemui.dagger.qualifiers.Background;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500102import com.android.systemui.dagger.qualifiers.Main;
Steve Elliott4c868852019-03-14 16:25:41 -0400103import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +0100104import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -0500105import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700106import com.android.systemui.statusbar.BlurUtils;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800107import com.android.systemui.statusbar.NotificationShadeDepthController;
Matt Pietal22231792020-01-23 09:51:09 -0500108import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Lucas Dupin43d01242020-02-03 11:58:33 -0800109import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700110import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400111import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800112import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500113import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500114import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700115
Jason Monk361915c2017-03-21 20:33:59 -0400116import java.util.ArrayList;
117import java.util.List;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500118import java.util.concurrent.Executor;
Jason Monk361915c2017-03-21 20:33:59 -0400119
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500120import javax.inject.Inject;
121
Jason Monk361915c2017-03-21 20:33:59 -0400122/**
Sean Pont9d4fb032020-03-04 18:54:32 -0800123 * Helper to show the global actions dialog. Each item is an {@link Action} that may show depending
124 * on whether the keyguard is showing, and whether the device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400125 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500126public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
Sean Pont9d4fb032020-03-04 18:54:32 -0800127 DialogInterface.OnShowListener,
128 ConfigurationController.ConfigurationListener,
129 GlobalActionsPanelPlugin.Callbacks {
Jason Monk361915c2017-03-21 20:33:59 -0400130
Sean Pont9d4fb032020-03-04 18:54:32 -0800131 public static final String SYSTEM_DIALOG_REASON_KEY = "reason";
132 public static final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
133 public static final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400134
135 private static final String TAG = "GlobalActionsDialog";
136
137 private static final boolean SHOW_SILENT_TOGGLE = true;
138
139 /* Valid settings for global actions keys.
140 * see config.xml config_globalActionList */
141 private static final String GLOBAL_ACTION_KEY_POWER = "power";
142 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
143 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
144 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
145 private static final String GLOBAL_ACTION_KEY_USERS = "users";
146 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
147 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
148 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
149 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
150 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000151 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800152 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500153 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400154
Matt Pietal61266442020-03-17 12:53:44 -0400155 private static final String PREFS_CONTROLS_SEEDING_COMPLETED = "ControlsSeedingCompleted";
156 private static final String PREFS_CONTROLS_FILE = "controls_prefs";
157
Jason Monk361915c2017-03-21 20:33:59 -0400158 private final Context mContext;
159 private final GlobalActionsManager mWindowManagerFuncs;
160 private final AudioManager mAudioManager;
161 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000162 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800163 private final LockPatternUtils mLockPatternUtils;
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500164 private final KeyguardStateController mKeyguardStateController;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000165 private final BroadcastDispatcher mBroadcastDispatcher;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500166 private final ContentResolver mContentResolver;
167 private final Resources mResources;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500168 private final ConfigurationController mConfigurationController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500169 private final UserManager mUserManager;
170 private final TrustManager mTrustManager;
171 private final IActivityManager mIActivityManager;
172 private final TelecomManager mTelecomManager;
173 private final MetricsLogger mMetricsLogger;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800174 private final NotificationShadeDepthController mDepthController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700175 private final BlurUtils mBlurUtils;
Jason Monk361915c2017-03-21 20:33:59 -0400176
177 private ArrayList<Action> mItems;
178 private ActionsDialog mDialog;
179
180 private Action mSilentModeAction;
181 private ToggleAction mAirplaneModeOn;
182
183 private MyAdapter mAdapter;
184
185 private boolean mKeyguardShowing = false;
186 private boolean mDeviceProvisioned = false;
187 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
188 private boolean mIsWaitingForEcmExit = false;
189 private boolean mHasTelephony;
190 private boolean mHasVibrator;
191 private final boolean mShowSilentToggle;
192 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500193 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400194 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400195 private final ActivityStarter mActivityStarter;
Lucas Dupin991415e2019-11-25 17:48:58 -0800196 private final SysuiColorExtractor mSysuiColorExtractor;
197 private final IStatusBarService mStatusBarService;
198 private final NotificationShadeWindowController mNotificationShadeWindowController;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400199 private GlobalActionsPanelPlugin mPanelPlugin;
Matt Pietal22231792020-01-23 09:51:09 -0500200 private ControlsUiController mControlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500201 private final IWindowManager mIWindowManager;
202 private final Executor mBackgroundExecutor;
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500203 private final ControlsListingController mControlsListingController;
204 private boolean mAnyControlsProviders = false;
Steve Elliott9b87a442019-03-05 10:24:16 -0500205
Jason Monk361915c2017-03-21 20:33:59 -0400206 /**
207 * @param context everything needs a context :(
208 */
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500209 @Inject
210 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs,
211 AudioManager audioManager, IDreamManager iDreamManager,
212 DevicePolicyManager devicePolicyManager, LockPatternUtils lockPatternUtils,
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500213 BroadcastDispatcher broadcastDispatcher,
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500214 ConnectivityManager connectivityManager, TelephonyManager telephonyManager,
215 ContentResolver contentResolver, @Nullable Vibrator vibrator, @Main Resources resources,
216 ConfigurationController configurationController, ActivityStarter activityStarter,
217 KeyguardStateController keyguardStateController, UserManager userManager,
218 TrustManager trustManager, IActivityManager iActivityManager,
Dave Mankoff3c6aaab2020-01-10 17:50:34 -0500219 @Nullable TelecomManager telecomManager, MetricsLogger metricsLogger,
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800220 NotificationShadeDepthController depthController, SysuiColorExtractor colorExtractor,
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700221 IStatusBarService statusBarService, BlurUtils blurUtils,
Matt Pietal22231792020-01-23 09:51:09 -0500222 NotificationShadeWindowController notificationShadeWindowController,
Dave Mankoff8df818e2020-02-12 14:22:26 -0500223 ControlsUiController controlsUiController, IWindowManager iWindowManager,
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500224 @Background Executor backgroundExecutor,
Matt Pietal61266442020-03-17 12:53:44 -0400225 ControlsListingController controlsListingController,
226 ControlsController controlsController) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700227 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400228 mWindowManagerFuncs = windowManagerFuncs;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500229 mAudioManager = audioManager;
230 mDreamManager = iDreamManager;
231 mDevicePolicyManager = devicePolicyManager;
232 mLockPatternUtils = lockPatternUtils;
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500233 mKeyguardStateController = keyguardStateController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500234 mBroadcastDispatcher = broadcastDispatcher;
235 mContentResolver = contentResolver;
236 mResources = resources;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500237 mConfigurationController = configurationController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500238 mUserManager = userManager;
239 mTrustManager = trustManager;
240 mIActivityManager = iActivityManager;
241 mTelecomManager = telecomManager;
242 mMetricsLogger = metricsLogger;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800243 mDepthController = depthController;
Lucas Dupin991415e2019-11-25 17:48:58 -0800244 mSysuiColorExtractor = colorExtractor;
245 mStatusBarService = statusBarService;
246 mNotificationShadeWindowController = notificationShadeWindowController;
Matt Pietal22231792020-01-23 09:51:09 -0500247 mControlsUiController = controlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500248 mIWindowManager = iWindowManager;
249 mBackgroundExecutor = backgroundExecutor;
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500250 mControlsListingController = controlsListingController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700251 mBlurUtils = blurUtils;
Jason Monk361915c2017-03-21 20:33:59 -0400252
253 // receive broadcasts
254 IntentFilter filter = new IntentFilter();
255 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
256 filter.addAction(Intent.ACTION_SCREEN_OFF);
Jayachandran C142eae02019-12-13 19:29:20 -0800257 filter.addAction(TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000258 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400259
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500260 mHasTelephony = connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
Jason Monk361915c2017-03-21 20:33:59 -0400261
262 // get notified of phone state changes
Jason Monk361915c2017-03-21 20:33:59 -0400263 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500264 contentResolver.registerContentObserver(
Jason Monk361915c2017-03-21 20:33:59 -0400265 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
266 mAirplaneModeObserver);
Jason Monk361915c2017-03-21 20:33:59 -0400267 mHasVibrator = vibrator != null && vibrator.hasVibrator();
268
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500269 mShowSilentToggle = SHOW_SILENT_TOGGLE && !resources.getBoolean(
Jason Monk361915c2017-03-21 20:33:59 -0400270 R.bool.config_useFixedVolume);
271
272 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500273 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400274 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700275
Dave Mankoff8df818e2020-02-12 14:22:26 -0500276 mConfigurationController.addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500277
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500278 mActivityStarter = activityStarter;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400279 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
280 @Override
281 public void onUnlockedChanged() {
282 if (mDialog != null && mDialog.mPanelController != null) {
Sean Pont9d4fb032020-03-04 18:54:32 -0800283 boolean unlocked = keyguardStateController.isUnlocked()
284 || keyguardStateController.canDismissLockScreen();
285 mDialog.mPanelController.onDeviceLockStateChanged(unlocked);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400286 }
287 }
288 });
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500289
Matt Pietal61266442020-03-17 12:53:44 -0400290 String preferredControlsPackage = mContext.getResources()
291 .getString(com.android.systemui.R.string.config_controlsPreferredPackage);
292 mControlsListingController.addCallback(list -> {
293 mAnyControlsProviders = !list.isEmpty();
294
295 /*
296 * See if any service providers match the preferred component. If they do,
297 * and there are no current favorites, and we haven't successfully loaded favorites to
298 * date, query the preferred component for a limited number of suggested controls.
299 */
300 ComponentName preferredComponent = null;
301 for (ControlsServiceInfo info : list) {
302 if (info.componentName.getPackageName().equals(preferredControlsPackage)) {
303 preferredComponent = info.componentName;
304 break;
305 }
306 }
307
308 if (preferredComponent == null) return;
309
310 SharedPreferences prefs = context.getSharedPreferences(PREFS_CONTROLS_FILE,
311 Context.MODE_PRIVATE);
312 boolean isSeeded = prefs.getBoolean(PREFS_CONTROLS_SEEDING_COMPLETED, false);
313 boolean hasFavorites = controlsController.getFavorites().size() > 0;
314 if (!isSeeded && !hasFavorites) {
315 controlsController.seedFavoritesForComponent(
316 preferredComponent,
317 (accepted) -> {
318 Log.i(TAG, "Controls seeded: " + accepted);
319 prefs.edit().putBoolean(PREFS_CONTROLS_SEEDING_COMPLETED,
320 accepted).apply();
321 }
322 );
323 }
324 });
Jason Monk361915c2017-03-21 20:33:59 -0400325 }
326
Matt Pietal61266442020-03-17 12:53:44 -0400327
328
329
Jason Monk361915c2017-03-21 20:33:59 -0400330 /**
331 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400332 *
Jason Monk361915c2017-03-21 20:33:59 -0400333 * @param keyguardShowing True if keyguard is showing
334 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400335 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
336 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400337 mKeyguardShowing = keyguardShowing;
338 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400339 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400340 if (mDialog != null) {
341 mDialog.dismiss();
342 mDialog = null;
343 // Show delayed, so that the dismiss of the previous dialog completes
344 mHandler.sendEmptyMessage(MESSAGE_SHOW);
345 } else {
346 handleShow();
347 }
348 }
349
Charles He9851a8d2017-10-10 17:31:30 +0100350 /**
351 * Dismiss the global actions dialog, if it's currently shown
352 */
353 public void dismissDialog() {
354 mHandler.removeMessages(MESSAGE_DISMISS);
355 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
356 }
357
Jason Monk361915c2017-03-21 20:33:59 -0400358 private void awakenIfNecessary() {
359 if (mDreamManager != null) {
360 try {
361 if (mDreamManager.isDreaming()) {
362 mDreamManager.awaken();
363 }
364 } catch (RemoteException e) {
365 // we tried
366 }
367 }
368 }
369
370 private void handleShow() {
371 awakenIfNecessary();
372 mDialog = createDialog();
373 prepareDialog();
374
375 // If we only have 1 item and it's a simple press action, just do this action.
376 if (mAdapter.getCount() == 1
377 && mAdapter.getItem(0) instanceof SinglePressAction
378 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
379 ((SinglePressAction) mAdapter.getItem(0)).onPress();
380 } else {
381 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
382 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100383 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400384 mDialog.getWindow().setAttributes(attrs);
385 mDialog.show();
386 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400387 }
388 }
389
390 /**
391 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400392 *
Jason Monk361915c2017-03-21 20:33:59 -0400393 * @return A new dialog.
394 */
395 private ActionsDialog createDialog() {
396 // Simple toggle style if there's no vibrator, otherwise use a tri-state
397 if (!mHasVibrator) {
398 mSilentModeAction = new SilentModeToggleAction();
399 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700400 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400401 }
Aran Ink780d4502020-02-14 10:39:58 -0500402 mAirplaneModeOn = new AirplaneModeAction();
Jason Monk361915c2017-03-21 20:33:59 -0400403 onAirplaneModeChanged();
404
405 mItems = new ArrayList<Action>();
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500406 String[] defaultActions = mResources.getStringArray(R.array.config_globalActionsList);
Jason Monk361915c2017-03-21 20:33:59 -0400407
408 ArraySet<String> addedKeys = new ArraySet<String>();
409 for (int i = 0; i < defaultActions.length; i++) {
410 String actionKey = defaultActions[i];
411 if (addedKeys.contains(actionKey)) {
412 // If we already have added this, don't add it again.
413 continue;
414 }
415 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
416 mItems.add(new PowerAction());
417 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
418 mItems.add(mAirplaneModeOn);
419 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500420 if (Settings.Global.getInt(mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -0400421 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
422 mItems.add(new BugReportAction());
423 }
424 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
425 if (mShowSilentToggle) {
426 mItems.add(mSilentModeAction);
427 }
428 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
429 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
430 addUsersToMenu(mItems);
431 }
432 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
433 mItems.add(getSettingsAction());
434 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500435 if (Settings.Secure.getIntForUser(mContentResolver,
Sean Pont9d4fb032020-03-04 18:54:32 -0800436 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800437 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700438 mItems.add(getLockdownAction());
439 }
Jason Monk361915c2017-03-21 20:33:59 -0400440 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
441 mItems.add(getVoiceAssistAction());
442 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
443 mItems.add(getAssistAction());
444 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
445 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500446 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
447 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000448 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000449 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000450 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
451 mItems.add(new LogoutAction());
Alex Chau04458852017-11-27 18:21:23 +0000452 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800453 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400454 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800455 mItems.add(new EmergencyDialerAction());
456 }
Jason Monk361915c2017-03-21 20:33:59 -0400457 } else {
458 Log.e(TAG, "Invalid global action key " + actionKey);
459 }
460 // Add here so we don't add more than one.
461 addedKeys.add(actionKey);
462 }
463
464 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400465 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400466 }
467
468 mAdapter = new MyAdapter();
469
Sean Pont9d4fb032020-03-04 18:54:32 -0800470 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, getWalletPanelViewController(),
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800471 mDepthController, mSysuiColorExtractor, mStatusBarService,
Matt Pietal22231792020-01-23 09:51:09 -0500472 mNotificationShadeWindowController,
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700473 shouldShowControls() ? mControlsUiController : null, mBlurUtils);
Jason Monk361915c2017-03-21 20:33:59 -0400474 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700475 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400476
477 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800478 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400479
480 return dialog;
481 }
482
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800483 private boolean shouldDisplayLockdown() {
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800484 // Lockdown is meaningless without a place to go.
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500485 if (!mKeyguardStateController.isMethodSecure()) {
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800486 return false;
487 }
488
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500489 int userId = getCurrentUser().id;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800490 // Only show the lockdown button if the device isn't locked down (for whatever reason).
491 int state = mLockPatternUtils.getStrongAuthForUser(userId);
492 return (state == STRONG_AUTH_NOT_REQUIRED
493 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
494 }
495
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700496 @Override
497 public void onUiModeChanged() {
498 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700499 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400500 mDialog.refreshDialog();
501 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700502 }
503
504 public void destroy() {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500505 mConfigurationController.removeCallback(this);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700506 }
507
Sean Pont9d4fb032020-03-04 18:54:32 -0800508 @Nullable
509 private GlobalActionsPanelPlugin.PanelViewController getWalletPanelViewController() {
510 if (mPanelPlugin == null) {
511 return null;
512 }
513 return mPanelPlugin.onPanelShown(this, !mKeyguardStateController.isUnlocked());
514 }
515
516 /**
517 * Implements {@link GlobalActionsPanelPlugin.Callbacks#dismissGlobalActionsMenu()}, which is
518 * called when the quick access wallet requests dismissal.
519 */
520 @Override
521 public void dismissGlobalActionsMenu() {
522 dismissDialog();
523 }
524
525 /**
526 * Implements {@link GlobalActionsPanelPlugin.Callbacks#dismissGlobalActionsMenu()}, which is
527 * called when the quick access wallet requests that an intent be started (with lock screen
528 * shown first if needed).
529 */
530 @Override
531 public void startPendingIntentDismissingKeyguard(PendingIntent pendingIntent) {
532 mActivityStarter.startPendingIntentDismissingKeyguard(pendingIntent);
533 }
534
Jason Monk361915c2017-03-21 20:33:59 -0400535 private final class PowerAction extends SinglePressAction implements LongPressAction {
536 private PowerAction() {
537 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400538 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400539 }
540
541 @Override
542 public boolean onLongPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500543 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
Jason Monk361915c2017-03-21 20:33:59 -0400544 mWindowManagerFuncs.reboot(true);
545 return true;
546 }
547 return false;
548 }
549
550 @Override
551 public boolean showDuringKeyguard() {
552 return true;
553 }
554
555 @Override
556 public boolean showBeforeProvisioning() {
557 return true;
558 }
559
560 @Override
561 public void onPress() {
562 // shutdown by making sure radio and power are handled accordingly.
563 mWindowManagerFuncs.shutdown();
564 }
565 }
566
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400567 private abstract class EmergencyAction extends SinglePressAction {
568 EmergencyAction(int iconResId, int messageResId) {
569 super(iconResId, messageResId);
570 }
571
572 @Override
573 public boolean shouldBeSeparated() {
Aran Ink780d4502020-02-14 10:39:58 -0500574 return true;
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400575 }
576
577 @Override
578 public View create(
579 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
580 View v = super.create(context, convertView, parent, inflater);
581 int textColor;
582 if (shouldBeSeparated()) {
583 textColor = v.getResources().getColor(
584 com.android.systemui.R.color.global_actions_alert_text);
585 } else {
586 textColor = v.getResources().getColor(
587 com.android.systemui.R.color.global_actions_text);
588 }
589 TextView messageView = v.findViewById(R.id.message);
590 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400591 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400592 ImageView icon = (ImageView) v.findViewById(R.id.icon);
593 icon.getDrawable().setTint(textColor);
594 return v;
595 }
596
597 @Override
598 public boolean showDuringKeyguard() {
599 return true;
600 }
601
602 @Override
603 public boolean showBeforeProvisioning() {
604 return true;
605 }
606 }
607
608 private class EmergencyAffordanceAction extends EmergencyAction {
609 EmergencyAffordanceAction() {
610 super(R.drawable.emergency_icon,
611 R.string.global_action_emergency);
612 }
613
614 @Override
615 public void onPress() {
616 mEmergencyAffordanceManager.performEmergencyCall();
617 }
618 }
619
620 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800621 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400622 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800623 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800624 }
625
626 @Override
627 public void onPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500628 mMetricsLogger.action(MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Dave Mankoff3c6aaab2020-01-10 17:50:34 -0500629 if (mTelecomManager != null) {
630 Intent intent = mTelecomManager.createLaunchEmergencyDialerIntent(
631 null /* number */);
632 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
633 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
634 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
635 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
636 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
637 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
638 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800639 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800640 }
641
Jason Monk361915c2017-03-21 20:33:59 -0400642 private final class RestartAction extends SinglePressAction implements LongPressAction {
643 private RestartAction() {
644 super(R.drawable.ic_restart, R.string.global_action_restart);
645 }
646
647 @Override
648 public boolean onLongPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500649 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
Jason Monk361915c2017-03-21 20:33:59 -0400650 mWindowManagerFuncs.reboot(true);
651 return true;
652 }
653 return false;
654 }
655
656 @Override
657 public boolean showDuringKeyguard() {
658 return true;
659 }
660
661 @Override
662 public boolean showBeforeProvisioning() {
663 return true;
664 }
665
666 @Override
667 public void onPress() {
668 mWindowManagerFuncs.reboot(false);
669 }
670 }
671
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400672 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500673 public ScreenshotAction() {
674 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
675 }
676
677 @Override
678 public void onPress() {
679 // Add a little delay before executing, to give the
680 // dialog a chance to go away before it takes a
681 // screenshot.
682 // TODO: instead, omit global action dialog layer
683 mHandler.postDelayed(new Runnable() {
684 @Override
685 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400686 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500687 mMetricsLogger.action(MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500688 }
689 }, 500);
690 }
691
692 @Override
693 public boolean showDuringKeyguard() {
694 return true;
695 }
696
697 @Override
698 public boolean showBeforeProvisioning() {
699 return false;
700 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400701
702 @Override
703 public boolean onLongPress() {
704 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
705 mScreenRecordHelper.launchRecordPrompt();
706 } else {
707 onPress();
708 }
709 return true;
710 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500711 }
712
Jason Monk361915c2017-03-21 20:33:59 -0400713 private class BugReportAction extends SinglePressAction implements LongPressAction {
714
715 public BugReportAction() {
716 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
717 }
718
719 @Override
720 public void onPress() {
721 // don't actually trigger the bugreport if we are running stability
722 // tests via monkey
723 if (ActivityManager.isUserAMonkey()) {
724 return;
725 }
726 // Add a little delay before executing, to give the
727 // dialog a chance to go away before it takes a
728 // screenshot.
729 mHandler.postDelayed(new Runnable() {
730 @Override
731 public void run() {
732 try {
733 // Take an "interactive" bugreport.
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500734 mMetricsLogger.action(
Jason Monk361915c2017-03-21 20:33:59 -0400735 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500736 if (!mIActivityManager.launchBugReportHandlerApp()) {
Paul Changc40f5782019-11-04 18:55:31 +0800737 Log.w(TAG, "Bugreport handler could not be launched");
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500738 mIActivityManager.requestInteractiveBugReport();
Paul Changc40f5782019-11-04 18:55:31 +0800739 }
Jason Monk361915c2017-03-21 20:33:59 -0400740 } catch (RemoteException e) {
741 }
742 }
743 }, 500);
744 }
745
746 @Override
747 public boolean onLongPress() {
748 // don't actually trigger the bugreport if we are running stability
749 // tests via monkey
750 if (ActivityManager.isUserAMonkey()) {
751 return false;
752 }
753 try {
754 // Take a "full" bugreport.
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500755 mMetricsLogger.action(MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
756 mIActivityManager.requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400757 } catch (RemoteException e) {
758 }
759 return false;
760 }
761
762 public boolean showDuringKeyguard() {
763 return true;
764 }
765
766 @Override
767 public boolean showBeforeProvisioning() {
768 return false;
769 }
Jason Monk361915c2017-03-21 20:33:59 -0400770 }
771
Alex Chau04458852017-11-27 18:21:23 +0000772 private final class LogoutAction extends SinglePressAction {
773 private LogoutAction() {
774 super(R.drawable.ic_logout, R.string.global_action_logout);
775 }
776
777 @Override
778 public boolean showDuringKeyguard() {
779 return true;
780 }
781
782 @Override
783 public boolean showBeforeProvisioning() {
784 return false;
785 }
786
787 @Override
788 public void onPress() {
789 // Add a little delay before executing, to give the dialog a chance to go away before
790 // switching user
791 mHandler.postDelayed(() -> {
792 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000793 int currentUserId = getCurrentUser().id;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500794 mIActivityManager.switchUser(UserHandle.USER_SYSTEM);
795 mIActivityManager.stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000796 } catch (RemoteException re) {
797 Log.e(TAG, "Couldn't logout user " + re);
798 }
799 }, 500);
800 }
801 }
802
Jason Monk361915c2017-03-21 20:33:59 -0400803 private Action getSettingsAction() {
804 return new SinglePressAction(R.drawable.ic_settings,
805 R.string.global_action_settings) {
806
807 @Override
808 public void onPress() {
809 Intent intent = new Intent(Settings.ACTION_SETTINGS);
810 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
811 mContext.startActivity(intent);
812 }
813
814 @Override
815 public boolean showDuringKeyguard() {
816 return true;
817 }
818
819 @Override
820 public boolean showBeforeProvisioning() {
821 return true;
822 }
823 };
824 }
825
Jason Monk361915c2017-03-21 20:33:59 -0400826 private Action getAssistAction() {
827 return new SinglePressAction(R.drawable.ic_action_assist_focused,
828 R.string.global_action_assist) {
829 @Override
830 public void onPress() {
831 Intent intent = new Intent(Intent.ACTION_ASSIST);
832 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
833 mContext.startActivity(intent);
834 }
835
836 @Override
837 public boolean showDuringKeyguard() {
838 return true;
839 }
840
841 @Override
842 public boolean showBeforeProvisioning() {
843 return true;
844 }
845 };
846 }
847
848 private Action getVoiceAssistAction() {
849 return new SinglePressAction(R.drawable.ic_voice_search,
850 R.string.global_action_voice_assist) {
851 @Override
852 public void onPress() {
853 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
854 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
855 mContext.startActivity(intent);
856 }
857
858 @Override
859 public boolean showDuringKeyguard() {
860 return true;
861 }
862
863 @Override
864 public boolean showBeforeProvisioning() {
865 return true;
866 }
867 };
868 }
869
870 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400871 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400872 R.string.global_action_lockdown) {
873
874 @Override
875 public void onPress() {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500876 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
877 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400878 try {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500879 mIWindowManager.lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100880 // Lock profiles (if any) on the background thread.
Dave Mankoff8df818e2020-02-12 14:22:26 -0500881 mBackgroundExecutor.execute(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400882 } catch (RemoteException e) {
883 Log.e(TAG, "Error while trying to lock device.", e);
884 }
885 }
886
887 @Override
888 public boolean showDuringKeyguard() {
889 return true;
890 }
891
892 @Override
893 public boolean showBeforeProvisioning() {
894 return false;
895 }
896 };
897 }
898
Pavel Grafov059021b2018-05-02 13:44:46 +0100899 private void lockProfiles() {
Pavel Grafov059021b2018-05-02 13:44:46 +0100900 final int currentUserId = getCurrentUser().id;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500901 final int[] profileIds = mUserManager.getEnabledProfileIds(currentUserId);
Pavel Grafov059021b2018-05-02 13:44:46 +0100902 for (final int id : profileIds) {
903 if (id != currentUserId) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500904 mTrustManager.setDeviceLockedForUser(id, true);
Pavel Grafov059021b2018-05-02 13:44:46 +0100905 }
906 }
907 }
908
Jason Monk361915c2017-03-21 20:33:59 -0400909 private UserInfo getCurrentUser() {
910 try {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500911 return mIActivityManager.getCurrentUser();
Jason Monk361915c2017-03-21 20:33:59 -0400912 } catch (RemoteException re) {
913 return null;
914 }
915 }
916
917 private boolean isCurrentUserOwner() {
918 UserInfo currentUser = getCurrentUser();
919 return currentUser == null || currentUser.isPrimary();
920 }
921
922 private void addUsersToMenu(ArrayList<Action> items) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500923 if (mUserManager.isUserSwitcherEnabled()) {
924 List<UserInfo> users = mUserManager.getUsers();
Jason Monk361915c2017-03-21 20:33:59 -0400925 UserInfo currentUser = getCurrentUser();
926 for (final UserInfo user : users) {
927 if (user.supportsSwitchToByUser()) {
928 boolean isCurrentUser = currentUser == null
929 ? user.id == 0 : (currentUser.id == user.id);
930 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
931 : null;
932 SinglePressAction switchToUser = new SinglePressAction(
933 R.drawable.ic_menu_cc, icon,
934 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400935 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400936 public void onPress() {
937 try {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500938 mIActivityManager.switchUser(user.id);
Jason Monk361915c2017-03-21 20:33:59 -0400939 } catch (RemoteException re) {
940 Log.e(TAG, "Couldn't switch user " + re);
941 }
942 }
943
944 public boolean showDuringKeyguard() {
945 return true;
946 }
947
948 public boolean showBeforeProvisioning() {
949 return false;
950 }
951 };
952 items.add(switchToUser);
953 }
954 }
955 }
956 }
957
958 private void prepareDialog() {
959 refreshSilentMode();
960 mAirplaneModeOn.updateState(mAirplaneState);
961 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400962 if (mShowSilentToggle) {
963 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000964 mBroadcastDispatcher.registerReceiver(mRingerModeReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400965 }
966 }
967
968 private void refreshSilentMode() {
969 if (!mHasVibrator) {
970 final boolean silentModeOn =
971 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400972 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400973 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
974 }
975 }
976
Sean Pont9d4fb032020-03-04 18:54:32 -0800977 /**
978 * {@inheritDoc}
979 */
Jason Monk361915c2017-03-21 20:33:59 -0400980 public void onDismiss(DialogInterface dialog) {
Steve Elliottc3147e22019-11-20 19:34:57 -0500981 if (mDialog == dialog) {
982 mDialog = null;
983 }
Jason Monk361915c2017-03-21 20:33:59 -0400984 mWindowManagerFuncs.onGlobalActionsHidden();
985 if (mShowSilentToggle) {
986 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000987 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400988 } catch (IllegalArgumentException ie) {
989 // ignore this
990 Log.w(TAG, ie);
991 }
992 }
993 }
994
Sean Pont9d4fb032020-03-04 18:54:32 -0800995 /**
996 * {@inheritDoc}
997 */
Shaotang Li786da902018-08-02 11:18:00 +0800998 public void onShow(DialogInterface dialog) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500999 mMetricsLogger.visible(MetricsEvent.POWER_MENU);
Shaotang Li786da902018-08-02 11:18:00 +08001000 }
1001
Jason Monk361915c2017-03-21 20:33:59 -04001002 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001003 * The adapter used for the list within the global actions dialog, taking into account whether
1004 * the keyguard is showing via
1005 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing}
1006 * and whether the device is provisioned via
1007 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
Jason Monk361915c2017-03-21 20:33:59 -04001008 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001009 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001010 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -04001011 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -04001012 for (int i = 0; i < mItems.size(); i++) {
1013 final Action action = mItems.get(i);
1014
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001015 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
1016 count++;
Jason Monk361915c2017-03-21 20:33:59 -04001017 }
Jason Monk361915c2017-03-21 20:33:59 -04001018 }
1019 return count;
1020 }
1021
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001022 private boolean shouldBeShown(Action action) {
1023 if (mKeyguardShowing && !action.showDuringKeyguard()) {
1024 return false;
1025 }
1026 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
1027 return false;
1028 }
1029 return true;
1030 }
1031
1032 @Override
1033 public int countSeparatedItems() {
1034 return countItems(true);
1035 }
1036
1037 @Override
1038 public int countListItems() {
1039 return countItems(false);
1040 }
1041
1042 @Override
1043 public int getCount() {
1044 return countSeparatedItems() + countListItems();
1045 }
1046
Jason Monk361915c2017-03-21 20:33:59 -04001047 @Override
1048 public boolean isEnabled(int position) {
1049 return getItem(position).isEnabled();
1050 }
1051
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001052 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001053 public boolean areAllItemsEnabled() {
1054 return false;
1055 }
1056
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001057 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001058 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -04001059 int filteredPos = 0;
1060 for (int i = 0; i < mItems.size(); i++) {
1061 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001062 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001063 continue;
1064 }
1065 if (filteredPos == position) {
1066 return action;
1067 }
1068 filteredPos++;
1069 }
1070
1071 throw new IllegalArgumentException("position " + position
1072 + " out of range of showable actions"
1073 + ", filtered count=" + getCount()
1074 + ", keyguardshowing=" + mKeyguardShowing
1075 + ", provisioned=" + mDeviceProvisioned);
1076 }
1077
1078
1079 public long getItemId(int position) {
1080 return position;
1081 }
1082
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001083 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001084 public View getView(int position, View convertView, ViewGroup parent) {
1085 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001086 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001087 view.setOnClickListener(v -> onClickItem(position));
Aran Inkb4199d92020-02-07 13:25:21 -05001088 if (action instanceof LongPressAction) {
1089 view.setOnLongClickListener(v -> onLongClickItem(position));
1090 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001091 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001092 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001093
1094 @Override
1095 public boolean onLongClickItem(int position) {
1096 final Action action = mAdapter.getItem(position);
1097 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001098 if (mDialog != null) {
1099 mDialog.dismiss();
1100 } else {
1101 Log.w(TAG, "Action long-clicked while mDialog is null.");
1102 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001103 return ((LongPressAction) action).onLongPress();
1104 }
1105 return false;
1106 }
1107
1108 @Override
1109 public void onClickItem(int position) {
1110 Action item = mAdapter.getItem(position);
1111 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001112 if (mDialog != null) {
1113 mDialog.dismiss();
1114 } else {
1115 Log.w(TAG, "Action clicked while mDialog is null.");
1116 }
1117 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001118 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001119 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001120
1121 @Override
1122 public boolean shouldBeSeparated(int position) {
1123 return getItem(position).shouldBeSeparated();
1124 }
Jason Monk361915c2017-03-21 20:33:59 -04001125 }
1126
1127 // note: the scheme below made more sense when we were planning on having
1128 // 8 different things in the global actions dialog. seems overkill with
1129 // only 3 items now, but may as well keep this flexible approach so it will
1130 // be easy should someone decide at the last minute to include something
1131 // else, such as 'enable wifi', or 'enable bluetooth'
1132
1133 /**
1134 * What each item in the global actions dialog must be able to support.
1135 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001136 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001137 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001138 * @return Text that will be announced when dialog is created. null for none.
Jason Monk361915c2017-03-21 20:33:59 -04001139 */
1140 CharSequence getLabelForAccessibility(Context context);
1141
1142 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1143
1144 void onPress();
1145
1146 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001147 * @return whether this action should appear in the dialog when the keygaurd is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001148 */
1149 boolean showDuringKeyguard();
1150
1151 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001152 * @return whether this action should appear in the dialog before the device is
1153 * provisioned.onlongpress
Jason Monk361915c2017-03-21 20:33:59 -04001154 */
1155 boolean showBeforeProvisioning();
1156
1157 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001158
1159 default boolean shouldBeSeparated() {
1160 return false;
1161 }
Jason Monk361915c2017-03-21 20:33:59 -04001162 }
1163
1164 /**
1165 * An action that also supports long press.
1166 */
1167 private interface LongPressAction extends Action {
1168 boolean onLongPress();
1169 }
1170
1171 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001172 * A single press action maintains no state, just responds to a press and takes an action.
Jason Monk361915c2017-03-21 20:33:59 -04001173 */
Matt Pietale0661b62020-01-29 14:35:31 -05001174
1175 private abstract class SinglePressAction implements Action {
Jason Monk361915c2017-03-21 20:33:59 -04001176 private final int mIconResId;
1177 private final Drawable mIcon;
1178 private final int mMessageResId;
1179 private final CharSequence mMessage;
1180
1181 protected SinglePressAction(int iconResId, int messageResId) {
1182 mIconResId = iconResId;
1183 mMessageResId = messageResId;
1184 mMessage = null;
1185 mIcon = null;
1186 }
1187
1188 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1189 mIconResId = iconResId;
1190 mMessageResId = 0;
1191 mMessage = message;
1192 mIcon = icon;
1193 }
1194
1195 public boolean isEnabled() {
1196 return true;
1197 }
1198
1199 public String getStatus() {
1200 return null;
1201 }
1202
1203 abstract public void onPress();
1204
1205 public CharSequence getLabelForAccessibility(Context context) {
1206 if (mMessage != null) {
1207 return mMessage;
1208 } else {
1209 return context.getString(mMessageResId);
1210 }
1211 }
1212
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001213 protected int getActionLayoutId(Context context) {
Matt Pietale0661b62020-01-29 14:35:31 -05001214 if (shouldShowControls()) {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001215 return com.android.systemui.R.layout.global_actions_grid_item_v2;
1216 }
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001217 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001218 }
1219
Jason Monk361915c2017-03-21 20:33:59 -04001220 public View create(
1221 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001222 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001223 false);
Jason Monk361915c2017-03-21 20:33:59 -04001224
1225 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1226 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001227 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001228
Jason Monk361915c2017-03-21 20:33:59 -04001229 if (mIcon != null) {
1230 icon.setImageDrawable(mIcon);
1231 icon.setScaleType(ScaleType.CENTER_CROP);
1232 } else if (mIconResId != 0) {
1233 icon.setImageDrawable(context.getDrawable(mIconResId));
1234 }
1235 if (mMessage != null) {
1236 messageView.setText(mMessage);
1237 } else {
1238 messageView.setText(mMessageResId);
1239 }
1240
1241 return v;
1242 }
1243 }
1244
1245 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001246 * A toggle action knows whether it is on or off, and displays an icon and status message
1247 * accordingly.
Jason Monk361915c2017-03-21 20:33:59 -04001248 */
1249 private static abstract class ToggleAction implements Action {
1250
1251 enum State {
1252 Off(false),
1253 TurningOn(true),
1254 TurningOff(true),
1255 On(false);
1256
1257 private final boolean inTransition;
1258
1259 State(boolean intermediate) {
1260 inTransition = intermediate;
1261 }
1262
1263 public boolean inTransition() {
1264 return inTransition;
1265 }
1266 }
1267
1268 protected State mState = State.Off;
1269
1270 // prefs
1271 protected int mEnabledIconResId;
1272 protected int mDisabledIconResid;
1273 protected int mMessageResId;
1274 protected int mEnabledStatusMessageResId;
1275 protected int mDisabledStatusMessageResId;
1276
1277 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001278 * @param enabledIconResId The icon for when this action is on.
1279 * @param disabledIconResid The icon for when this action is off.
1280 * @param message The general information message, e.g 'Silent Mode'
1281 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001282 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1283 */
1284 public ToggleAction(int enabledIconResId,
1285 int disabledIconResid,
1286 int message,
1287 int enabledStatusMessageResId,
1288 int disabledStatusMessageResId) {
1289 mEnabledIconResId = enabledIconResId;
1290 mDisabledIconResid = disabledIconResid;
1291 mMessageResId = message;
1292 mEnabledStatusMessageResId = enabledStatusMessageResId;
1293 mDisabledStatusMessageResId = disabledStatusMessageResId;
1294 }
1295
1296 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001297 * Override to make changes to resource IDs just before creating the View.
Jason Monk361915c2017-03-21 20:33:59 -04001298 */
1299 void willCreate() {
1300
1301 }
1302
1303 @Override
1304 public CharSequence getLabelForAccessibility(Context context) {
1305 return context.getString(mMessageResId);
1306 }
1307
1308 public View create(Context context, View convertView, ViewGroup parent,
1309 LayoutInflater inflater) {
1310 willCreate();
1311
Koji Fukui5c010ee2019-12-24 16:12:07 +09001312 View v = inflater.inflate(com.android.systemui.R
1313 .layout.global_actions_grid_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001314
1315 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1316 TextView messageView = (TextView) v.findViewById(R.id.message);
Jason Monk361915c2017-03-21 20:33:59 -04001317 final boolean enabled = isEnabled();
Koji Fukui5c010ee2019-12-24 16:12:07 +09001318 boolean on = ((mState == State.On) || (mState == State.TurningOn));
Jason Monk361915c2017-03-21 20:33:59 -04001319
1320 if (messageView != null) {
Koji Fukui5c010ee2019-12-24 16:12:07 +09001321 messageView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
Jason Monk361915c2017-03-21 20:33:59 -04001322 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001323 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001324 }
1325
Jason Monk361915c2017-03-21 20:33:59 -04001326 if (icon != null) {
1327 icon.setImageDrawable(context.getDrawable(
1328 (on ? mEnabledIconResId : mDisabledIconResid)));
1329 icon.setEnabled(enabled);
1330 }
1331
Jason Monk361915c2017-03-21 20:33:59 -04001332 v.setEnabled(enabled);
1333
1334 return v;
1335 }
1336
1337 public final void onPress() {
1338 if (mState.inTransition()) {
1339 Log.w(TAG, "shouldn't be able to toggle when in transition");
1340 return;
1341 }
1342
1343 final boolean nowOn = !(mState == State.On);
1344 onToggle(nowOn);
1345 changeStateFromPress(nowOn);
1346 }
1347
1348 public boolean isEnabled() {
1349 return !mState.inTransition();
1350 }
1351
1352 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001353 * Implementations may override this if their state can be in on of the intermediate states
1354 * until some notification is received (e.g airplane mode is 'turning off' until we know the
1355 * wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001356 *
Jason Monk361915c2017-03-21 20:33:59 -04001357 * @param buttonOn Whether the button was turned on or off
1358 */
1359 protected void changeStateFromPress(boolean buttonOn) {
1360 mState = buttonOn ? State.On : State.Off;
1361 }
1362
1363 abstract void onToggle(boolean on);
1364
1365 public void updateState(State state) {
1366 mState = state;
1367 }
1368 }
1369
Aran Ink780d4502020-02-14 10:39:58 -05001370 private class AirplaneModeAction extends ToggleAction {
1371 AirplaneModeAction() {
1372 super(
Sean Pont9d4fb032020-03-04 18:54:32 -08001373 R.drawable.ic_lock_airplane_mode,
1374 R.drawable.ic_lock_airplane_mode_off,
1375 R.string.global_actions_toggle_airplane_mode,
1376 R.string.global_actions_airplane_mode_on_status,
1377 R.string.global_actions_airplane_mode_off_status);
Aran Ink780d4502020-02-14 10:39:58 -05001378 }
Sean Pont9d4fb032020-03-04 18:54:32 -08001379
Aran Ink780d4502020-02-14 10:39:58 -05001380 void onToggle(boolean on) {
1381 if (mHasTelephony && TelephonyProperties.in_ecm_mode().orElse(false)) {
1382 mIsWaitingForEcmExit = true;
1383 // Launch ECM exit dialog
1384 Intent ecmDialogIntent =
1385 new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
1386 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1387 mContext.startActivity(ecmDialogIntent);
1388 } else {
1389 changeAirplaneModeSystemSetting(on);
1390 }
1391 }
1392
1393 @Override
1394 protected void changeStateFromPress(boolean buttonOn) {
1395 if (!mHasTelephony) return;
1396
1397 // In ECM mode airplane state cannot be changed
1398 if (!TelephonyProperties.in_ecm_mode().orElse(false)) {
1399 mState = buttonOn ? State.TurningOn : State.TurningOff;
1400 mAirplaneState = mState;
1401 }
1402 }
1403
1404 public boolean showDuringKeyguard() {
1405 return true;
1406 }
1407
1408 public boolean showBeforeProvisioning() {
1409 return false;
1410 }
1411 }
1412
Jason Monk361915c2017-03-21 20:33:59 -04001413 private class SilentModeToggleAction extends ToggleAction {
1414 public SilentModeToggleAction() {
1415 super(R.drawable.ic_audio_vol_mute,
1416 R.drawable.ic_audio_vol,
1417 R.string.global_action_toggle_silent_mode,
1418 R.string.global_action_silent_mode_on_status,
1419 R.string.global_action_silent_mode_off_status);
1420 }
1421
1422 void onToggle(boolean on) {
1423 if (on) {
1424 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1425 } else {
1426 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1427 }
1428 }
1429
1430 public boolean showDuringKeyguard() {
1431 return true;
1432 }
1433
1434 public boolean showBeforeProvisioning() {
1435 return false;
1436 }
1437 }
1438
1439 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1440
Jason Monk16fbd9d2017-04-27 14:28:49 -04001441 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001442
1443 private final AudioManager mAudioManager;
1444 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001445
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001446 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001447 mAudioManager = audioManager;
1448 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001449 }
1450
1451 private int ringerModeToIndex(int ringerMode) {
1452 // They just happen to coincide
1453 return ringerMode;
1454 }
1455
1456 private int indexToRingerMode(int index) {
1457 // They just happen to coincide
1458 return index;
1459 }
1460
1461 @Override
1462 public CharSequence getLabelForAccessibility(Context context) {
1463 return null;
1464 }
1465
1466 public View create(Context context, View convertView, ViewGroup parent,
1467 LayoutInflater inflater) {
1468 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1469
1470 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1471 for (int i = 0; i < 3; i++) {
1472 View itemView = v.findViewById(ITEM_IDS[i]);
1473 itemView.setSelected(selectedIndex == i);
1474 // Set up click handler
1475 itemView.setTag(i);
1476 itemView.setOnClickListener(this);
1477 }
1478 return v;
1479 }
1480
1481 public void onPress() {
1482 }
1483
1484 public boolean showDuringKeyguard() {
1485 return true;
1486 }
1487
1488 public boolean showBeforeProvisioning() {
1489 return false;
1490 }
1491
1492 public boolean isEnabled() {
1493 return true;
1494 }
1495
1496 void willCreate() {
1497 }
1498
1499 public void onClick(View v) {
1500 if (!(v.getTag() instanceof Integer)) return;
1501
1502 int index = (Integer) v.getTag();
1503 mAudioManager.setRingerMode(indexToRingerMode(index));
1504 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1505 }
1506 }
1507
1508 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1509 public void onReceive(Context context, Intent intent) {
1510 String action = intent.getAction();
1511 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1512 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1513 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1514 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001515 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001516 }
Jayachandran C142eae02019-12-13 19:29:20 -08001517 } else if (TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001518 // Airplane mode can be changed after ECM exits if airplane toggle button
1519 // is pressed during ECM mode
Daniel Bright4bcbfce2020-01-09 21:58:16 -08001520 if (!(intent.getBooleanExtra(TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false))
1521 && mIsWaitingForEcmExit) {
Jason Monk361915c2017-03-21 20:33:59 -04001522 mIsWaitingForEcmExit = false;
1523 changeAirplaneModeSystemSetting(true);
1524 }
1525 }
1526 }
1527 };
1528
1529 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1530 @Override
1531 public void onServiceStateChanged(ServiceState serviceState) {
1532 if (!mHasTelephony) return;
1533 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1534 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1535 mAirplaneModeOn.updateState(mAirplaneState);
1536 mAdapter.notifyDataSetChanged();
1537 }
1538 };
1539
1540 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1541 @Override
1542 public void onReceive(Context context, Intent intent) {
1543 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1544 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1545 }
1546 }
1547 };
1548
1549 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1550 @Override
1551 public void onChange(boolean selfChange) {
1552 onAirplaneModeChanged();
1553 }
1554 };
1555
1556 private static final int MESSAGE_DISMISS = 0;
1557 private static final int MESSAGE_REFRESH = 1;
1558 private static final int MESSAGE_SHOW = 2;
1559 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1560
1561 private Handler mHandler = new Handler() {
1562 public void handleMessage(Message msg) {
1563 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001564 case MESSAGE_DISMISS:
1565 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001566 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1567 mDialog.dismissImmediately();
1568 } else {
1569 mDialog.dismiss();
1570 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001571 mDialog = null;
1572 }
1573 break;
1574 case MESSAGE_REFRESH:
1575 refreshSilentMode();
1576 mAdapter.notifyDataSetChanged();
1577 break;
1578 case MESSAGE_SHOW:
1579 handleShow();
1580 break;
Jason Monk361915c2017-03-21 20:33:59 -04001581 }
1582 }
1583 };
1584
1585 private void onAirplaneModeChanged() {
1586 // Let the service state callbacks handle the state.
1587 if (mHasTelephony) return;
1588
1589 boolean airplaneModeOn = Settings.Global.getInt(
Dave Mankoffc88a2d72020-01-10 15:59:31 -05001590 mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -04001591 Settings.Global.AIRPLANE_MODE_ON,
1592 0) == 1;
1593 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1594 mAirplaneModeOn.updateState(mAirplaneState);
1595 }
1596
1597 /**
1598 * Change the airplane mode system setting
1599 */
1600 private void changeAirplaneModeSystemSetting(boolean on) {
1601 Settings.Global.putInt(
Dave Mankoffc88a2d72020-01-10 15:59:31 -05001602 mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -04001603 Settings.Global.AIRPLANE_MODE_ON,
1604 on ? 1 : 0);
1605 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1606 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1607 intent.putExtra("state", on);
1608 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1609 if (!mHasTelephony) {
1610 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1611 }
1612 }
1613
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001614 private static final class ActionsDialog extends Dialog implements DialogInterface,
1615 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001616
Jason Monk16fbd9d2017-04-27 14:28:49 -04001617 private final Context mContext;
1618 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001619 private final IStatusBarService mStatusBarService;
1620 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001621 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001622 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001623 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001624 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001625 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001626 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001627 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001628 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001629 private boolean mHadTopUi;
wilsonshihe8321942019-10-18 18:39:46 +08001630 private final NotificationShadeWindowController mNotificationShadeWindowController;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001631 private final NotificationShadeDepthController mDepthController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001632 private final BlurUtils mBlurUtils;
Matt Pietal22231792020-01-23 09:51:09 -05001633
1634 private ControlsUiController mControlsUiController;
1635 private ViewGroup mControlsView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001636
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001637 ActionsDialog(Context context, MyAdapter adapter,
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001638 GlobalActionsPanelPlugin.PanelViewController plugin,
1639 NotificationShadeDepthController depthController,
Lucas Dupin991415e2019-11-25 17:48:58 -08001640 SysuiColorExtractor sysuiColorExtractor, IStatusBarService statusBarService,
1641 NotificationShadeWindowController notificationShadeWindowController,
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001642 ControlsUiController controlsUiController, BlurUtils blurUtils) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001643 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001644 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001645 mAdapter = adapter;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001646 mDepthController = depthController;
Lucas Dupin991415e2019-11-25 17:48:58 -08001647 mColorExtractor = sysuiColorExtractor;
1648 mStatusBarService = statusBarService;
1649 mNotificationShadeWindowController = notificationShadeWindowController;
Matt Pietal22231792020-01-23 09:51:09 -05001650 mControlsUiController = controlsUiController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001651 mBlurUtils = blurUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001652
1653 // Window initialization
1654 Window window = getWindow();
1655 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001656 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1657 window.getDecorView();
1658 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1659 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1660 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1661 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1662 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001663 window.addFlags(
1664 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Sean Pont9d4fb032020-03-04 18:54:32 -08001665 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1666 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
1667 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1668 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1669 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001670 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Tiger Huang52724442020-01-20 21:38:42 +08001671 window.getAttributes().setFitInsetsTypes(0 /* types */);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001672 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001673
1674 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001675 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001676 }
1677
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001678 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001679 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001680 }
1681
1682 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001683 int rotation = RotationUtils.getRotation(mContext);
1684 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1685 if (rotation != RotationUtils.ROTATION_NONE) {
1686 if (rotationLocked) {
1687 if (mResetOrientationData == null) {
1688 mResetOrientationData = new ResetOrientationData();
1689 mResetOrientationData.locked = true;
1690 mResetOrientationData.rotation = rotation;
1691 }
1692
1693 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001694 // This call is posted so that the rotation does not change until post-layout,
1695 // otherwise onConfigurationChanged() may not get invoked.
1696 mGlobalActionsLayout.post(() ->
1697 RotationPolicy.setRotationLockAtAngle(
1698 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001699 }
1700 } else {
1701 if (!rotationLocked) {
1702 if (mResetOrientationData == null) {
1703 mResetOrientationData = new ResetOrientationData();
1704 mResetOrientationData.locked = false;
1705 }
1706
1707 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001708 // This call is posted so that the rotation does not change until post-layout,
1709 // otherwise onConfigurationChanged() may not get invoked.
1710 mGlobalActionsLayout.post(() ->
1711 RotationPolicy.setRotationLockAtAngle(
1712 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001713 }
1714
Steve Elliott53f12ae2019-05-13 17:14:15 -04001715 // Disable rotation suggestions, if enabled
1716 setRotationSuggestionsEnabled(false);
1717
Steve Elliott300b48f2019-05-29 14:13:50 -04001718 FrameLayout panelContainer =
1719 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001720 FrameLayout.LayoutParams panelParams =
1721 new FrameLayout.LayoutParams(
1722 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001723 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001724 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott48f75db2019-05-03 15:03:38 -04001725 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001726 }
1727
1728 private void initializeLayout() {
1729 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001730 fixNavBarClipping();
Matt Pietal22231792020-01-23 09:51:09 -05001731 mControlsView = findViewById(com.android.systemui.R.id.global_actions_controls);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001732 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001733 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001734 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001735 @Override
1736 public boolean dispatchPopulateAccessibilityEvent(
1737 View host, AccessibilityEvent event) {
1738 // Populate the title here, just as Activity does
1739 event.getText().add(mContext.getString(R.string.global_actions));
1740 return true;
1741 }
1742 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001743 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001744 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001745
Aran Ink06090262020-03-03 13:23:17 -05001746 View globalActionsParent = (View) mGlobalActionsLayout.getParent();
1747 globalActionsParent.setOnClickListener(v -> dismiss());
1748
1749 // add fall-through dismiss handling to root view
1750 View rootView = findViewById(com.android.systemui.R.id.global_actions_grid_root);
1751 if (rootView != null) {
1752 rootView.setOnClickListener(v -> dismiss());
1753 }
1754
Steve Elliott86ef6282019-05-08 14:45:04 -04001755 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001756 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001757 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001758 if (mBackgroundDrawable == null) {
1759 mBackgroundDrawable = new ScrimDrawable();
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001760 mScrimAlpha = mBlurUtils.supportsBlursOnWindows()
1761 ? ScrimController.BLUR_SCRIM_ALPHA : ScrimController.BUSY_SCRIM_ALPHA;
Steve Elliott86ef6282019-05-08 14:45:04 -04001762 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001763 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001764 }
1765
Steve Elliott300b48f2019-05-29 14:13:50 -04001766 private void fixNavBarClipping() {
1767 ViewGroup content = findViewById(android.R.id.content);
1768 content.setClipChildren(false);
1769 content.setClipToPadding(false);
1770 ViewGroup contentParent = (ViewGroup) content.getParent();
1771 contentParent.setClipChildren(false);
1772 contentParent.setClipToPadding(false);
1773 }
1774
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001775 private int getGlobalActionsLayoutId(Context context) {
Matt Pietal22231792020-01-23 09:51:09 -05001776 if (mControlsUiController != null) {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001777 return com.android.systemui.R.layout.global_actions_grid_v2;
1778 }
1779
Steve Elliottdc165632019-05-23 14:26:31 -04001780 int rotation = RotationUtils.getRotation(context);
1781 boolean useGridLayout = isForceGridEnabled(context)
1782 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1783 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001784 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001785 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001786 } else {
1787 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001788 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001789 } else {
1790 if (useGridLayout) {
1791 return com.android.systemui.R.layout.global_actions_grid;
1792 } else {
1793 return com.android.systemui.R.layout.global_actions_column;
1794 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001795 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001796 }
1797
Jason Monk361915c2017-03-21 20:33:59 -04001798 @Override
1799 protected void onStart() {
1800 super.setCanceledOnTouchOutside(true);
1801 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001802 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001803
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001804 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001805 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001806 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001807 updateColors(colors, false /* animate */);
1808 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001809 }
1810
1811 /**
1812 * Updates background and system bars according to current GradientColors.
Sean Pont9d4fb032020-03-04 18:54:32 -08001813 *
1814 * @param colors Colors and hints to use.
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001815 * @param animate Interpolates gradient if true, just sets otherwise.
1816 */
1817 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001818 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001819 return;
1820 }
Lucas Dupinba2b48a2020-02-06 09:28:06 -08001821 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.supportsDarkText() ? Color.WHITE
1822 : Color.BLACK, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001823 View decorView = getWindow().getDecorView();
1824 if (colors.supportsDarkText()) {
1825 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001826 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001827 } else {
1828 decorView.setSystemUiVisibility(0);
1829 }
Jason Monk361915c2017-03-21 20:33:59 -04001830 }
1831
1832 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001833 protected void onStop() {
1834 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001835 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001836 }
1837
1838 @Override
1839 public void show() {
1840 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001841 mShowing = true;
wilsonshihe8321942019-10-18 18:39:46 +08001842 mHadTopUi = mNotificationShadeWindowController.getForceHasTopUi();
1843 mNotificationShadeWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001844 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001845 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1846 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001847 mGlobalActionsLayout.setAlpha(0);
1848 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001849 .alpha(1)
1850 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001851 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001852 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001853 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001854 .setUpdateListener(animation -> {
Lucas Dupin991415e2019-11-25 17:48:58 -08001855 float animatedValue = animation.getAnimatedFraction();
1856 int alpha = (int) (animatedValue * mScrimAlpha * 255);
Steve Elliott9b87a442019-03-05 10:24:16 -05001857 mBackgroundDrawable.setAlpha(alpha);
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001858 mDepthController.updateGlobalDialogVisibility(animatedValue,
1859 mGlobalActionsLayout);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001860 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001861 .start();
Matt Pietal22231792020-01-23 09:51:09 -05001862 if (mControlsUiController != null) {
1863 mControlsUiController.show(mControlsView);
1864 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001865 }
1866
1867 @Override
1868 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001869 if (!mShowing) {
1870 return;
1871 }
1872 mShowing = false;
Matt Pietal22231792020-01-23 09:51:09 -05001873 if (mControlsUiController != null) mControlsUiController.hide();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001874 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001875 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001876 mGlobalActionsLayout.setAlpha(1);
1877 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001878 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001879 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1880 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001881 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001882 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001883 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001884 .setUpdateListener(animation -> {
Lucas Dupin991415e2019-11-25 17:48:58 -08001885 float animatedValue = 1f - animation.getAnimatedFraction();
1886 int alpha = (int) (animatedValue * mScrimAlpha * 255);
Steve Elliott9b87a442019-03-05 10:24:16 -05001887 mBackgroundDrawable.setAlpha(alpha);
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001888 mDepthController.updateGlobalDialogVisibility(animatedValue,
1889 mGlobalActionsLayout);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001890 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001891 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001892 dismissPanel();
1893 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001894 }
1895
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001896 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001897 mShowing = false;
Matt Pietal019feaa2020-01-31 14:51:37 -05001898 if (mControlsUiController != null) mControlsUiController.hide();
Steve Elliott48f75db2019-05-03 15:03:38 -04001899 dismissPanel();
1900 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001901 completeDismiss();
1902 }
1903
1904 private void completeDismiss() {
wilsonshihe8321942019-10-18 18:39:46 +08001905 mNotificationShadeWindowController.setForceHasTopUi(mHadTopUi);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001906 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001907 }
1908
1909 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001910 if (mPanelController != null) {
1911 mPanelController.onDismissed();
1912 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001913 }
1914
Steve Elliott53f12ae2019-05-13 17:14:15 -04001915 private void setRotationSuggestionsEnabled(boolean enabled) {
1916 try {
1917 final int userId = Binder.getCallingUserHandle().getIdentifier();
1918 final int what = enabled
1919 ? StatusBarManager.DISABLE2_NONE
1920 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1921 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1922 } catch (RemoteException ex) {
1923 throw ex.rethrowFromSystemServer();
1924 }
1925 }
1926
Steve Elliott48f75db2019-05-03 15:03:38 -04001927 private void resetOrientation() {
1928 if (mResetOrientationData != null) {
1929 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1930 mResetOrientationData.rotation);
1931 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001932 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001933 }
1934
Jason Monk361915c2017-03-21 20:33:59 -04001935 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001936 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001937 if (mKeyguardShowing) {
1938 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001939 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1940 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001941 }
1942 } else {
1943 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001944 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1945 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001946 }
1947 }
1948 }
1949
1950 public void setKeyguardShowing(boolean keyguardShowing) {
1951 mKeyguardShowing = keyguardShowing;
1952 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001953
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001954 public void refreshDialog() {
1955 initializeLayout();
1956 mGlobalActionsLayout.updateList();
1957 }
1958
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001959 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001960 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001961 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001962 }
1963 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001964
1965 private static class ResetOrientationData {
1966 public boolean locked;
1967 public int rotation;
1968 }
Jason Monk361915c2017-03-21 20:33:59 -04001969 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001970
1971 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001972 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1973 */
1974 private static boolean isPanelDebugModeEnabled(Context context) {
1975 return Settings.Secure.getInt(context.getContentResolver(),
1976 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1977 }
1978
1979 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001980 * Determines whether or not the Global Actions menu should be forced to use the newer
1981 * grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001982 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001983 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001984 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001985 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001986
Matt Pietale0661b62020-01-29 14:35:31 -05001987 private boolean shouldShowControls() {
Fabian Kozynskif6063ae2020-02-24 16:14:35 -05001988 return mKeyguardStateController.isUnlocked()
Fabian Kozynski0424ab12020-02-21 12:09:17 -05001989 && mControlsUiController.getAvailable()
1990 && mAnyControlsProviders;
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001991 }
Jason Monk361915c2017-03-21 20:33:59 -04001992}