blob: 5aed647937114ba2fbc95605d2347c728c56b221 [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;
Lucas Dupincb6726f2020-04-07 19:32:38 -070042import android.content.res.ColorStateList;
Dave Mankoffc88a2d72020-01-10 15:59:31 -050043import android.content.res.Resources;
Jason Monk361915c2017-03-21 20:33:59 -040044import android.database.ContentObserver;
Matt Pietale0661b62020-01-29 14:35:31 -050045import android.graphics.Color;
Jason Monk361915c2017-03-21 20:33:59 -040046import android.graphics.drawable.Drawable;
47import android.media.AudioManager;
48import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040049import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040050import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040051import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040052import android.os.Message;
53import android.os.RemoteException;
Jason Monk361915c2017-03-21 20:33:59 -040054import android.os.SystemProperties;
55import android.os.UserHandle;
56import android.os.UserManager;
57import android.os.Vibrator;
58import android.provider.Settings;
Jason Monk361915c2017-03-21 20:33:59 -040059import android.service.dreams.IDreamManager;
Inseob Kim5e82f732019-11-08 15:08:38 +090060import android.sysprop.TelephonyProperties;
Fan Zhangf9914762019-11-01 15:58:38 -070061import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040062import android.telephony.PhoneStateListener;
63import android.telephony.ServiceState;
64import android.telephony.TelephonyManager;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080066import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040067import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070068import android.view.ContextThemeWrapper;
Dave Mankoff8df818e2020-02-12 14:22:26 -050069import android.view.IWindowManager;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.view.LayoutInflater;
71import android.view.View;
72import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070073import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040074import android.view.WindowManager;
Jason Monk361915c2017-03-21 20:33:59 -040075import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050076import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040077import android.widget.ImageView;
78import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040079import android.widget.TextView;
80
Fabian Kozynskib6a20372020-04-01 09:36:43 -040081import androidx.annotation.NonNull;
82import androidx.lifecycle.Lifecycle;
83import androidx.lifecycle.LifecycleOwner;
84import androidx.lifecycle.LifecycleRegistry;
85
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.internal.R;
Chris Wren65a70c92020-04-02 10:43:46 -040087import com.android.internal.annotations.VisibleForTesting;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.internal.colorextraction.ColorExtractor;
89import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070090import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.internal.logging.MetricsLogger;
Chris Wren65a70c92020-04-02 10:43:46 -040092import com.android.internal.logging.UiEvent;
93import com.android.internal.logging.UiEventLogger;
Charles He9851a8d2017-10-10 17:31:30 +010094import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040095import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010096import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040097import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050098import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040099import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +0100100import com.android.internal.widget.LockPatternUtils;
Charles He9851a8d2017-10-10 17:31:30 +0100101import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500102import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500103import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -0400104import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +0100105import com.android.systemui.colorextraction.SysuiColorExtractor;
Matt Pietal61266442020-03-17 12:53:44 -0400106import com.android.systemui.controls.ControlsServiceInfo;
107import com.android.systemui.controls.controller.ControlsController;
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500108import com.android.systemui.controls.management.ControlsListingController;
Matt Pietal22231792020-01-23 09:51:09 -0500109import com.android.systemui.controls.ui.ControlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500110import com.android.systemui.dagger.qualifiers.Background;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500111import com.android.systemui.dagger.qualifiers.Main;
Steve Elliott4c868852019-03-14 16:25:41 -0400112import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +0100113import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -0500114import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700115import com.android.systemui.statusbar.BlurUtils;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800116import com.android.systemui.statusbar.NotificationShadeDepthController;
Matt Pietal22231792020-01-23 09:51:09 -0500117import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
Lucas Dupin43d01242020-02-03 11:58:33 -0800118import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700119import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400120import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800121import com.android.systemui.util.EmergencyDialerConstants;
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400122import com.android.systemui.util.RingerModeTracker;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500123import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500124import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700125
Jason Monk361915c2017-03-21 20:33:59 -0400126import java.util.ArrayList;
127import java.util.List;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500128import java.util.concurrent.Executor;
Jason Monk361915c2017-03-21 20:33:59 -0400129
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500130import javax.inject.Inject;
131
Jason Monk361915c2017-03-21 20:33:59 -0400132/**
Sean Pont9d4fb032020-03-04 18:54:32 -0800133 * Helper to show the global actions dialog. Each item is an {@link Action} that may show depending
134 * on whether the keyguard is showing, and whether the device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400135 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500136public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
Sean Pont9d4fb032020-03-04 18:54:32 -0800137 DialogInterface.OnShowListener,
138 ConfigurationController.ConfigurationListener,
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400139 GlobalActionsPanelPlugin.Callbacks,
140 LifecycleOwner {
Jason Monk361915c2017-03-21 20:33:59 -0400141
Sean Pont9d4fb032020-03-04 18:54:32 -0800142 public static final String SYSTEM_DIALOG_REASON_KEY = "reason";
143 public static final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
144 public static final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400145
146 private static final String TAG = "GlobalActionsDialog";
147
148 private static final boolean SHOW_SILENT_TOGGLE = true;
149
150 /* Valid settings for global actions keys.
151 * see config.xml config_globalActionList */
152 private static final String GLOBAL_ACTION_KEY_POWER = "power";
153 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
154 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
155 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
156 private static final String GLOBAL_ACTION_KEY_USERS = "users";
157 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
158 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
159 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
160 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
161 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000162 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800163 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500164 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400165
Matt Pietal61266442020-03-17 12:53:44 -0400166 private static final String PREFS_CONTROLS_SEEDING_COMPLETED = "ControlsSeedingCompleted";
167 private static final String PREFS_CONTROLS_FILE = "controls_prefs";
168
Jason Monk361915c2017-03-21 20:33:59 -0400169 private final Context mContext;
170 private final GlobalActionsManager mWindowManagerFuncs;
171 private final AudioManager mAudioManager;
172 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000173 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800174 private final LockPatternUtils mLockPatternUtils;
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500175 private final KeyguardStateController mKeyguardStateController;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000176 private final BroadcastDispatcher mBroadcastDispatcher;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500177 private final ContentResolver mContentResolver;
178 private final Resources mResources;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500179 private final ConfigurationController mConfigurationController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500180 private final UserManager mUserManager;
181 private final TrustManager mTrustManager;
182 private final IActivityManager mIActivityManager;
183 private final TelecomManager mTelecomManager;
184 private final MetricsLogger mMetricsLogger;
Chris Wren65a70c92020-04-02 10:43:46 -0400185 private final UiEventLogger mUiEventLogger;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800186 private final NotificationShadeDepthController mDepthController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700187 private final BlurUtils mBlurUtils;
Jason Monk361915c2017-03-21 20:33:59 -0400188
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400189 // Used for RingerModeTracker
190 private final LifecycleRegistry mLifecycle = new LifecycleRegistry(this);
191
Jason Monk361915c2017-03-21 20:33:59 -0400192 private ArrayList<Action> mItems;
193 private ActionsDialog mDialog;
194
195 private Action mSilentModeAction;
196 private ToggleAction mAirplaneModeOn;
197
198 private MyAdapter mAdapter;
199
200 private boolean mKeyguardShowing = false;
201 private boolean mDeviceProvisioned = false;
Lucas Dupincb6726f2020-04-07 19:32:38 -0700202 private ToggleState mAirplaneState = ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -0400203 private boolean mIsWaitingForEcmExit = false;
204 private boolean mHasTelephony;
205 private boolean mHasVibrator;
206 private final boolean mShowSilentToggle;
207 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500208 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400209 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400210 private final ActivityStarter mActivityStarter;
Lucas Dupin991415e2019-11-25 17:48:58 -0800211 private final SysuiColorExtractor mSysuiColorExtractor;
212 private final IStatusBarService mStatusBarService;
213 private final NotificationShadeWindowController mNotificationShadeWindowController;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400214 private GlobalActionsPanelPlugin mPanelPlugin;
Matt Pietal22231792020-01-23 09:51:09 -0500215 private ControlsUiController mControlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500216 private final IWindowManager mIWindowManager;
217 private final Executor mBackgroundExecutor;
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500218 private final ControlsListingController mControlsListingController;
Matt Pietalcd757c82020-04-08 10:20:48 -0400219 private List<ControlsServiceInfo> mControlsServiceInfos = new ArrayList<>();
220 private ControlsController mControlsController;
221 private SharedPreferences mControlsPreferences;
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400222 private final RingerModeTracker mRingerModeTracker;
Steve Elliott9b87a442019-03-05 10:24:16 -0500223
Chris Wren65a70c92020-04-02 10:43:46 -0400224 @VisibleForTesting
225 public enum GlobalActionsEvent implements UiEventLogger.UiEventEnum {
226 @UiEvent(doc = "The global actions / power menu surface became visible on the screen.")
227 GA_POWER_MENU_OPEN(337);
228
229 private final int mId;
230
231 GlobalActionsEvent(int id) {
232 mId = id;
233 }
234
235 @Override
236 public int getId() {
237 return mId;
238 }
239 }
240
Jason Monk361915c2017-03-21 20:33:59 -0400241 /**
242 * @param context everything needs a context :(
243 */
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500244 @Inject
245 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs,
246 AudioManager audioManager, IDreamManager iDreamManager,
247 DevicePolicyManager devicePolicyManager, LockPatternUtils lockPatternUtils,
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500248 BroadcastDispatcher broadcastDispatcher,
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500249 ConnectivityManager connectivityManager, TelephonyManager telephonyManager,
250 ContentResolver contentResolver, @Nullable Vibrator vibrator, @Main Resources resources,
251 ConfigurationController configurationController, ActivityStarter activityStarter,
252 KeyguardStateController keyguardStateController, UserManager userManager,
253 TrustManager trustManager, IActivityManager iActivityManager,
Dave Mankoff3c6aaab2020-01-10 17:50:34 -0500254 @Nullable TelecomManager telecomManager, MetricsLogger metricsLogger,
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800255 NotificationShadeDepthController depthController, SysuiColorExtractor colorExtractor,
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700256 IStatusBarService statusBarService, BlurUtils blurUtils,
Matt Pietal22231792020-01-23 09:51:09 -0500257 NotificationShadeWindowController notificationShadeWindowController,
Dave Mankoff8df818e2020-02-12 14:22:26 -0500258 ControlsUiController controlsUiController, IWindowManager iWindowManager,
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500259 @Background Executor backgroundExecutor,
Matt Pietal61266442020-03-17 12:53:44 -0400260 ControlsListingController controlsListingController,
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400261 ControlsController controlsController, UiEventLogger uiEventLogger,
262 RingerModeTracker ringerModeTracker) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700263 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400264 mWindowManagerFuncs = windowManagerFuncs;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500265 mAudioManager = audioManager;
266 mDreamManager = iDreamManager;
267 mDevicePolicyManager = devicePolicyManager;
268 mLockPatternUtils = lockPatternUtils;
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500269 mKeyguardStateController = keyguardStateController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500270 mBroadcastDispatcher = broadcastDispatcher;
271 mContentResolver = contentResolver;
272 mResources = resources;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500273 mConfigurationController = configurationController;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500274 mUserManager = userManager;
275 mTrustManager = trustManager;
276 mIActivityManager = iActivityManager;
277 mTelecomManager = telecomManager;
278 mMetricsLogger = metricsLogger;
Chris Wren65a70c92020-04-02 10:43:46 -0400279 mUiEventLogger = uiEventLogger;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800280 mDepthController = depthController;
Lucas Dupin991415e2019-11-25 17:48:58 -0800281 mSysuiColorExtractor = colorExtractor;
282 mStatusBarService = statusBarService;
283 mNotificationShadeWindowController = notificationShadeWindowController;
Matt Pietal22231792020-01-23 09:51:09 -0500284 mControlsUiController = controlsUiController;
Dave Mankoff8df818e2020-02-12 14:22:26 -0500285 mIWindowManager = iWindowManager;
286 mBackgroundExecutor = backgroundExecutor;
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500287 mControlsListingController = controlsListingController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700288 mBlurUtils = blurUtils;
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400289 mRingerModeTracker = ringerModeTracker;
Matt Pietalcd757c82020-04-08 10:20:48 -0400290 mControlsController = controlsController;
Jason Monk361915c2017-03-21 20:33:59 -0400291
292 // receive broadcasts
293 IntentFilter filter = new IntentFilter();
294 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
295 filter.addAction(Intent.ACTION_SCREEN_OFF);
Jayachandran C142eae02019-12-13 19:29:20 -0800296 filter.addAction(TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000297 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400298
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500299 mHasTelephony = connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
Jason Monk361915c2017-03-21 20:33:59 -0400300
301 // get notified of phone state changes
Jason Monk361915c2017-03-21 20:33:59 -0400302 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500303 contentResolver.registerContentObserver(
Jason Monk361915c2017-03-21 20:33:59 -0400304 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
305 mAirplaneModeObserver);
Jason Monk361915c2017-03-21 20:33:59 -0400306 mHasVibrator = vibrator != null && vibrator.hasVibrator();
307
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500308 mShowSilentToggle = SHOW_SILENT_TOGGLE && !resources.getBoolean(
Jason Monk361915c2017-03-21 20:33:59 -0400309 R.bool.config_useFixedVolume);
Fabian Kozynskib6a20372020-04-01 09:36:43 -0400310 if (mShowSilentToggle) {
311 mRingerModeTracker.getRingerMode().observe(this, ringer ->
312 mHandler.sendEmptyMessage(MESSAGE_REFRESH)
313 );
314 }
Jason Monk361915c2017-03-21 20:33:59 -0400315
316 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500317 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400318 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700319
Dave Mankoff8df818e2020-02-12 14:22:26 -0500320 mConfigurationController.addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500321
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500322 mActivityStarter = activityStarter;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400323 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
324 @Override
325 public void onUnlockedChanged() {
326 if (mDialog != null && mDialog.mPanelController != null) {
Sean Pont9d4fb032020-03-04 18:54:32 -0800327 boolean unlocked = keyguardStateController.isUnlocked()
328 || keyguardStateController.canDismissLockScreen();
329 mDialog.mPanelController.onDeviceLockStateChanged(unlocked);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400330 }
331 }
332 });
Fabian Kozynski0424ab12020-02-21 12:09:17 -0500333
Matt Pietal61266442020-03-17 12:53:44 -0400334 mControlsListingController.addCallback(list -> {
Matt Pietalcd757c82020-04-08 10:20:48 -0400335 mControlsServiceInfos = list;
Matt Pietal61266442020-03-17 12:53:44 -0400336 });
Matt Pietalcd757c82020-04-08 10:20:48 -0400337
338 // Need to be user-specific with the context to make sure we read the correct prefs
339 Context userContext = context.createContextAsUser(
340 new UserHandle(mUserManager.getUserHandle()), 0);
341 mControlsPreferences = userContext.getSharedPreferences(PREFS_CONTROLS_FILE,
342 Context.MODE_PRIVATE);
343
Jason Monk361915c2017-03-21 20:33:59 -0400344 }
345
Matt Pietalcd757c82020-04-08 10:20:48 -0400346 private void seedFavorites() {
347 if (mControlsServiceInfos.isEmpty()
348 || mControlsController.getFavorites().size() > 0
349 || mControlsPreferences.getBoolean(PREFS_CONTROLS_SEEDING_COMPLETED, false)) {
350 return;
351 }
Matt Pietal61266442020-03-17 12:53:44 -0400352
Matt Pietalcd757c82020-04-08 10:20:48 -0400353 /*
354 * See if any service providers match the preferred component. If they do,
355 * and there are no current favorites, and we haven't successfully loaded favorites to
356 * date, query the preferred component for a limited number of suggested controls.
357 */
358 String preferredControlsPackage = mContext.getResources()
359 .getString(com.android.systemui.R.string.config_controlsPreferredPackage);
Matt Pietal61266442020-03-17 12:53:44 -0400360
Matt Pietalcd757c82020-04-08 10:20:48 -0400361 ComponentName preferredComponent = null;
362 for (ControlsServiceInfo info : mControlsServiceInfos) {
363 if (info.componentName.getPackageName().equals(preferredControlsPackage)) {
364 preferredComponent = info.componentName;
365 break;
366 }
367 }
368
369 if (preferredComponent == null) {
370 Log.i(TAG, "Controls seeding: No preferred component has been set, will not seed");
371 mControlsPreferences.edit().putBoolean(PREFS_CONTROLS_SEEDING_COMPLETED, true).apply();
372 }
373
374 mControlsController.seedFavoritesForComponent(
375 preferredComponent,
376 (accepted) -> {
377 Log.i(TAG, "Controls seeded: " + accepted);
378 mControlsPreferences.edit().putBoolean(PREFS_CONTROLS_SEEDING_COMPLETED,
379 accepted).apply();
380 });
381 }
Matt Pietal61266442020-03-17 12:53:44 -0400382
Jason Monk361915c2017-03-21 20:33:59 -0400383 /**
384 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400385 *
Jason Monk361915c2017-03-21 20:33:59 -0400386 * @param keyguardShowing True if keyguard is showing
387 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400388 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
389 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400390 mKeyguardShowing = keyguardShowing;
391 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400392 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400393 if (mDialog != null) {
394 mDialog.dismiss();
395 mDialog = null;
396 // Show delayed, so that the dismiss of the previous dialog completes
397 mHandler.sendEmptyMessage(MESSAGE_SHOW);
398 } else {
399 handleShow();
400 }
401 }
402
Charles He9851a8d2017-10-10 17:31:30 +0100403 /**
404 * Dismiss the global actions dialog, if it's currently shown
405 */
406 public void dismissDialog() {
407 mHandler.removeMessages(MESSAGE_DISMISS);
408 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
409 }
410
Jason Monk361915c2017-03-21 20:33:59 -0400411 private void awakenIfNecessary() {
412 if (mDreamManager != null) {
413 try {
414 if (mDreamManager.isDreaming()) {
415 mDreamManager.awaken();
416 }
417 } catch (RemoteException e) {
418 // we tried
419 }
420 }
421 }
422
423 private void handleShow() {
424 awakenIfNecessary();
425 mDialog = createDialog();
426 prepareDialog();
Matt Pietalcd757c82020-04-08 10:20:48 -0400427 seedFavorites();
Jason Monk361915c2017-03-21 20:33:59 -0400428
429 // If we only have 1 item and it's a simple press action, just do this action.
430 if (mAdapter.getCount() == 1
431 && mAdapter.getItem(0) instanceof SinglePressAction
432 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
433 ((SinglePressAction) mAdapter.getItem(0)).onPress();
434 } else {
435 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
436 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100437 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400438 mDialog.getWindow().setAttributes(attrs);
439 mDialog.show();
440 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400441 }
442 }
443
444 /**
445 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400446 *
Jason Monk361915c2017-03-21 20:33:59 -0400447 * @return A new dialog.
448 */
449 private ActionsDialog createDialog() {
450 // Simple toggle style if there's no vibrator, otherwise use a tri-state
451 if (!mHasVibrator) {
452 mSilentModeAction = new SilentModeToggleAction();
453 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700454 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400455 }
Aran Ink780d4502020-02-14 10:39:58 -0500456 mAirplaneModeOn = new AirplaneModeAction();
Jason Monk361915c2017-03-21 20:33:59 -0400457 onAirplaneModeChanged();
458
459 mItems = new ArrayList<Action>();
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500460 String[] defaultActions = mResources.getStringArray(R.array.config_globalActionsList);
Jason Monk361915c2017-03-21 20:33:59 -0400461
462 ArraySet<String> addedKeys = new ArraySet<String>();
463 for (int i = 0; i < defaultActions.length; i++) {
464 String actionKey = defaultActions[i];
465 if (addedKeys.contains(actionKey)) {
466 // If we already have added this, don't add it again.
467 continue;
468 }
469 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
470 mItems.add(new PowerAction());
471 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
472 mItems.add(mAirplaneModeOn);
473 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500474 if (Settings.Global.getInt(mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -0400475 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
476 mItems.add(new BugReportAction());
477 }
478 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
479 if (mShowSilentToggle) {
480 mItems.add(mSilentModeAction);
481 }
482 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
483 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
484 addUsersToMenu(mItems);
485 }
486 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
487 mItems.add(getSettingsAction());
488 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500489 if (Settings.Secure.getIntForUser(mContentResolver,
Sean Pont9d4fb032020-03-04 18:54:32 -0800490 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800491 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700492 mItems.add(getLockdownAction());
493 }
Jason Monk361915c2017-03-21 20:33:59 -0400494 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
495 mItems.add(getVoiceAssistAction());
496 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
497 mItems.add(getAssistAction());
498 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
499 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500500 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
501 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000502 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000503 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000504 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
505 mItems.add(new LogoutAction());
Alex Chau04458852017-11-27 18:21:23 +0000506 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800507 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400508 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800509 mItems.add(new EmergencyDialerAction());
510 }
Jason Monk361915c2017-03-21 20:33:59 -0400511 } else {
512 Log.e(TAG, "Invalid global action key " + actionKey);
513 }
514 // Add here so we don't add more than one.
515 addedKeys.add(actionKey);
516 }
517
518 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400519 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400520 }
521
522 mAdapter = new MyAdapter();
523
Lucas Dupin3a8531e2020-04-06 17:18:09 -0700524 mDepthController.setShowingHomeControls(shouldShowControls());
Sean Pont9d4fb032020-03-04 18:54:32 -0800525 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, getWalletPanelViewController(),
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -0800526 mDepthController, mSysuiColorExtractor, mStatusBarService,
Matt Pietal22231792020-01-23 09:51:09 -0500527 mNotificationShadeWindowController,
Lucas Dupin99f5fba2020-03-12 12:59:18 -0700528 shouldShowControls() ? mControlsUiController : null, mBlurUtils);
Jason Monk361915c2017-03-21 20:33:59 -0400529 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700530 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400531
532 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800533 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400534
535 return dialog;
536 }
537
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800538 private boolean shouldDisplayLockdown() {
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800539 // Lockdown is meaningless without a place to go.
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500540 if (!mKeyguardStateController.isMethodSecure()) {
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800541 return false;
542 }
543
Fabian Kozynskif6063ae2020-02-24 16:14:35 -0500544 int userId = getCurrentUser().id;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800545 // Only show the lockdown button if the device isn't locked down (for whatever reason).
546 int state = mLockPatternUtils.getStrongAuthForUser(userId);
547 return (state == STRONG_AUTH_NOT_REQUIRED
548 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
549 }
550
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700551 @Override
552 public void onUiModeChanged() {
553 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700554 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400555 mDialog.refreshDialog();
556 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700557 }
558
559 public void destroy() {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500560 mConfigurationController.removeCallback(this);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700561 }
562
Sean Pont9d4fb032020-03-04 18:54:32 -0800563 @Nullable
564 private GlobalActionsPanelPlugin.PanelViewController getWalletPanelViewController() {
565 if (mPanelPlugin == null) {
566 return null;
567 }
568 return mPanelPlugin.onPanelShown(this, !mKeyguardStateController.isUnlocked());
569 }
570
571 /**
572 * Implements {@link GlobalActionsPanelPlugin.Callbacks#dismissGlobalActionsMenu()}, which is
573 * called when the quick access wallet requests dismissal.
574 */
575 @Override
576 public void dismissGlobalActionsMenu() {
577 dismissDialog();
578 }
579
580 /**
581 * Implements {@link GlobalActionsPanelPlugin.Callbacks#dismissGlobalActionsMenu()}, which is
582 * called when the quick access wallet requests that an intent be started (with lock screen
583 * shown first if needed).
584 */
585 @Override
586 public void startPendingIntentDismissingKeyguard(PendingIntent pendingIntent) {
587 mActivityStarter.startPendingIntentDismissingKeyguard(pendingIntent);
588 }
589
Jason Monk361915c2017-03-21 20:33:59 -0400590 private final class PowerAction extends SinglePressAction implements LongPressAction {
591 private PowerAction() {
592 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400593 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400594 }
595
596 @Override
597 public boolean onLongPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500598 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
Jason Monk361915c2017-03-21 20:33:59 -0400599 mWindowManagerFuncs.reboot(true);
600 return true;
601 }
602 return false;
603 }
604
605 @Override
606 public boolean showDuringKeyguard() {
607 return true;
608 }
609
610 @Override
611 public boolean showBeforeProvisioning() {
612 return true;
613 }
614
615 @Override
616 public void onPress() {
617 // shutdown by making sure radio and power are handled accordingly.
618 mWindowManagerFuncs.shutdown();
619 }
620 }
621
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400622 private abstract class EmergencyAction extends SinglePressAction {
623 EmergencyAction(int iconResId, int messageResId) {
624 super(iconResId, messageResId);
625 }
626
627 @Override
628 public boolean shouldBeSeparated() {
Lucas Dupincb6726f2020-04-07 19:32:38 -0700629 return !shouldShowControls();
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400630 }
631
632 @Override
633 public View create(
634 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
635 View v = super.create(context, convertView, parent, inflater);
636 int textColor;
Lucas Dupincb6726f2020-04-07 19:32:38 -0700637 if (shouldShowControls()) {
638 v.setBackgroundTintList(ColorStateList.valueOf(v.getResources().getColor(
639 com.android.systemui.R.color.global_actions_emergency_background)));
640 textColor = v.getResources().getColor(
641 com.android.systemui.R.color.global_actions_emergency_text);
642 } else if (shouldBeSeparated()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400643 textColor = v.getResources().getColor(
644 com.android.systemui.R.color.global_actions_alert_text);
645 } else {
646 textColor = v.getResources().getColor(
647 com.android.systemui.R.color.global_actions_text);
648 }
649 TextView messageView = v.findViewById(R.id.message);
650 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400651 messageView.setSelected(true); // necessary for marquee to work
Lucas Dupincb6726f2020-04-07 19:32:38 -0700652 ImageView icon = v.findViewById(R.id.icon);
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400653 icon.getDrawable().setTint(textColor);
654 return v;
655 }
656
657 @Override
658 public boolean showDuringKeyguard() {
659 return true;
660 }
661
662 @Override
663 public boolean showBeforeProvisioning() {
664 return true;
665 }
666 }
667
668 private class EmergencyAffordanceAction extends EmergencyAction {
669 EmergencyAffordanceAction() {
670 super(R.drawable.emergency_icon,
671 R.string.global_action_emergency);
672 }
673
674 @Override
675 public void onPress() {
676 mEmergencyAffordanceManager.performEmergencyCall();
677 }
678 }
679
680 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800681 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400682 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800683 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800684 }
685
686 @Override
687 public void onPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500688 mMetricsLogger.action(MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Dave Mankoff3c6aaab2020-01-10 17:50:34 -0500689 if (mTelecomManager != null) {
690 Intent intent = mTelecomManager.createLaunchEmergencyDialerIntent(
691 null /* number */);
692 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
693 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
694 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
695 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
696 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
697 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
698 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800699 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800700 }
701
Jason Monk361915c2017-03-21 20:33:59 -0400702 private final class RestartAction extends SinglePressAction implements LongPressAction {
703 private RestartAction() {
704 super(R.drawable.ic_restart, R.string.global_action_restart);
705 }
706
707 @Override
708 public boolean onLongPress() {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500709 if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
Jason Monk361915c2017-03-21 20:33:59 -0400710 mWindowManagerFuncs.reboot(true);
711 return true;
712 }
713 return false;
714 }
715
716 @Override
717 public boolean showDuringKeyguard() {
718 return true;
719 }
720
721 @Override
722 public boolean showBeforeProvisioning() {
723 return true;
724 }
725
726 @Override
727 public void onPress() {
728 mWindowManagerFuncs.reboot(false);
729 }
730 }
731
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400732 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500733 public ScreenshotAction() {
734 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
735 }
736
737 @Override
738 public void onPress() {
739 // Add a little delay before executing, to give the
740 // dialog a chance to go away before it takes a
741 // screenshot.
742 // TODO: instead, omit global action dialog layer
743 mHandler.postDelayed(new Runnable() {
744 @Override
745 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400746 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500747 mMetricsLogger.action(MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500748 }
749 }, 500);
750 }
751
752 @Override
753 public boolean showDuringKeyguard() {
754 return true;
755 }
756
757 @Override
758 public boolean showBeforeProvisioning() {
759 return false;
760 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400761
762 @Override
763 public boolean onLongPress() {
764 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
765 mScreenRecordHelper.launchRecordPrompt();
766 } else {
767 onPress();
768 }
769 return true;
770 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500771 }
772
Jason Monk361915c2017-03-21 20:33:59 -0400773 private class BugReportAction extends SinglePressAction implements LongPressAction {
774
775 public BugReportAction() {
776 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
777 }
778
779 @Override
780 public void onPress() {
781 // don't actually trigger the bugreport if we are running stability
782 // tests via monkey
783 if (ActivityManager.isUserAMonkey()) {
784 return;
785 }
786 // Add a little delay before executing, to give the
787 // dialog a chance to go away before it takes a
788 // screenshot.
789 mHandler.postDelayed(new Runnable() {
790 @Override
791 public void run() {
792 try {
793 // Take an "interactive" bugreport.
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500794 mMetricsLogger.action(
Jason Monk361915c2017-03-21 20:33:59 -0400795 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500796 if (!mIActivityManager.launchBugReportHandlerApp()) {
Paul Changc40f5782019-11-04 18:55:31 +0800797 Log.w(TAG, "Bugreport handler could not be launched");
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500798 mIActivityManager.requestInteractiveBugReport();
Paul Changc40f5782019-11-04 18:55:31 +0800799 }
Jason Monk361915c2017-03-21 20:33:59 -0400800 } catch (RemoteException e) {
801 }
802 }
803 }, 500);
804 }
805
806 @Override
807 public boolean onLongPress() {
808 // don't actually trigger the bugreport if we are running stability
809 // tests via monkey
810 if (ActivityManager.isUserAMonkey()) {
811 return false;
812 }
813 try {
814 // Take a "full" bugreport.
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500815 mMetricsLogger.action(MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
816 mIActivityManager.requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400817 } catch (RemoteException e) {
818 }
819 return false;
820 }
821
822 public boolean showDuringKeyguard() {
823 return true;
824 }
825
826 @Override
827 public boolean showBeforeProvisioning() {
828 return false;
829 }
Jason Monk361915c2017-03-21 20:33:59 -0400830 }
831
Alex Chau04458852017-11-27 18:21:23 +0000832 private final class LogoutAction extends SinglePressAction {
833 private LogoutAction() {
834 super(R.drawable.ic_logout, R.string.global_action_logout);
835 }
836
837 @Override
838 public boolean showDuringKeyguard() {
839 return true;
840 }
841
842 @Override
843 public boolean showBeforeProvisioning() {
844 return false;
845 }
846
847 @Override
848 public void onPress() {
849 // Add a little delay before executing, to give the dialog a chance to go away before
850 // switching user
851 mHandler.postDelayed(() -> {
852 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000853 int currentUserId = getCurrentUser().id;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500854 mIActivityManager.switchUser(UserHandle.USER_SYSTEM);
855 mIActivityManager.stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000856 } catch (RemoteException re) {
857 Log.e(TAG, "Couldn't logout user " + re);
858 }
859 }, 500);
860 }
861 }
862
Jason Monk361915c2017-03-21 20:33:59 -0400863 private Action getSettingsAction() {
864 return new SinglePressAction(R.drawable.ic_settings,
865 R.string.global_action_settings) {
866
867 @Override
868 public void onPress() {
869 Intent intent = new Intent(Settings.ACTION_SETTINGS);
870 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
871 mContext.startActivity(intent);
872 }
873
874 @Override
875 public boolean showDuringKeyguard() {
876 return true;
877 }
878
879 @Override
880 public boolean showBeforeProvisioning() {
881 return true;
882 }
883 };
884 }
885
Jason Monk361915c2017-03-21 20:33:59 -0400886 private Action getAssistAction() {
887 return new SinglePressAction(R.drawable.ic_action_assist_focused,
888 R.string.global_action_assist) {
889 @Override
890 public void onPress() {
891 Intent intent = new Intent(Intent.ACTION_ASSIST);
892 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
893 mContext.startActivity(intent);
894 }
895
896 @Override
897 public boolean showDuringKeyguard() {
898 return true;
899 }
900
901 @Override
902 public boolean showBeforeProvisioning() {
903 return true;
904 }
905 };
906 }
907
908 private Action getVoiceAssistAction() {
909 return new SinglePressAction(R.drawable.ic_voice_search,
910 R.string.global_action_voice_assist) {
911 @Override
912 public void onPress() {
913 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
914 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
915 mContext.startActivity(intent);
916 }
917
918 @Override
919 public boolean showDuringKeyguard() {
920 return true;
921 }
922
923 @Override
924 public boolean showBeforeProvisioning() {
925 return true;
926 }
927 };
928 }
929
930 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400931 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400932 R.string.global_action_lockdown) {
933
934 @Override
935 public void onPress() {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500936 mLockPatternUtils.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
937 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400938 try {
Dave Mankoff8df818e2020-02-12 14:22:26 -0500939 mIWindowManager.lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100940 // Lock profiles (if any) on the background thread.
Dave Mankoff8df818e2020-02-12 14:22:26 -0500941 mBackgroundExecutor.execute(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400942 } catch (RemoteException e) {
943 Log.e(TAG, "Error while trying to lock device.", e);
944 }
945 }
946
947 @Override
948 public boolean showDuringKeyguard() {
949 return true;
950 }
951
952 @Override
953 public boolean showBeforeProvisioning() {
954 return false;
955 }
956 };
957 }
958
Pavel Grafov059021b2018-05-02 13:44:46 +0100959 private void lockProfiles() {
Pavel Grafov059021b2018-05-02 13:44:46 +0100960 final int currentUserId = getCurrentUser().id;
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500961 final int[] profileIds = mUserManager.getEnabledProfileIds(currentUserId);
Pavel Grafov059021b2018-05-02 13:44:46 +0100962 for (final int id : profileIds) {
963 if (id != currentUserId) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500964 mTrustManager.setDeviceLockedForUser(id, true);
Pavel Grafov059021b2018-05-02 13:44:46 +0100965 }
966 }
967 }
968
Jason Monk361915c2017-03-21 20:33:59 -0400969 private UserInfo getCurrentUser() {
970 try {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500971 return mIActivityManager.getCurrentUser();
Jason Monk361915c2017-03-21 20:33:59 -0400972 } catch (RemoteException re) {
973 return null;
974 }
975 }
976
977 private boolean isCurrentUserOwner() {
978 UserInfo currentUser = getCurrentUser();
979 return currentUser == null || currentUser.isPrimary();
980 }
981
982 private void addUsersToMenu(ArrayList<Action> items) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500983 if (mUserManager.isUserSwitcherEnabled()) {
984 List<UserInfo> users = mUserManager.getUsers();
Jason Monk361915c2017-03-21 20:33:59 -0400985 UserInfo currentUser = getCurrentUser();
986 for (final UserInfo user : users) {
987 if (user.supportsSwitchToByUser()) {
988 boolean isCurrentUser = currentUser == null
989 ? user.id == 0 : (currentUser.id == user.id);
990 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
991 : null;
992 SinglePressAction switchToUser = new SinglePressAction(
993 R.drawable.ic_menu_cc, icon,
994 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400995 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400996 public void onPress() {
997 try {
Dave Mankoffc88a2d72020-01-10 15:59:31 -0500998 mIActivityManager.switchUser(user.id);
Jason Monk361915c2017-03-21 20:33:59 -0400999 } catch (RemoteException re) {
1000 Log.e(TAG, "Couldn't switch user " + re);
1001 }
1002 }
1003
1004 public boolean showDuringKeyguard() {
1005 return true;
1006 }
1007
1008 public boolean showBeforeProvisioning() {
1009 return false;
1010 }
1011 };
1012 items.add(switchToUser);
1013 }
1014 }
1015 }
1016 }
1017
1018 private void prepareDialog() {
1019 refreshSilentMode();
1020 mAirplaneModeOn.updateState(mAirplaneState);
1021 mAdapter.notifyDataSetChanged();
Fabian Kozynskib6a20372020-04-01 09:36:43 -04001022 mLifecycle.setCurrentState(Lifecycle.State.RESUMED);
Jason Monk361915c2017-03-21 20:33:59 -04001023 }
1024
1025 private void refreshSilentMode() {
1026 if (!mHasVibrator) {
Fabian Kozynskib6a20372020-04-01 09:36:43 -04001027 Integer value = mRingerModeTracker.getRingerMode().getValue();
1028 final boolean silentModeOn = value != null && value != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001029 ((ToggleAction) mSilentModeAction).updateState(
Lucas Dupincb6726f2020-04-07 19:32:38 -07001030 silentModeOn ? ToggleState.On : ToggleState.Off);
Jason Monk361915c2017-03-21 20:33:59 -04001031 }
1032 }
1033
Sean Pont9d4fb032020-03-04 18:54:32 -08001034 /**
1035 * {@inheritDoc}
1036 */
Jason Monk361915c2017-03-21 20:33:59 -04001037 public void onDismiss(DialogInterface dialog) {
Steve Elliottc3147e22019-11-20 19:34:57 -05001038 if (mDialog == dialog) {
1039 mDialog = null;
1040 }
Jason Monk361915c2017-03-21 20:33:59 -04001041 mWindowManagerFuncs.onGlobalActionsHidden();
Fabian Kozynskib6a20372020-04-01 09:36:43 -04001042 mLifecycle.setCurrentState(Lifecycle.State.DESTROYED);
Jason Monk361915c2017-03-21 20:33:59 -04001043 }
1044
Sean Pont9d4fb032020-03-04 18:54:32 -08001045 /**
1046 * {@inheritDoc}
1047 */
Shaotang Li786da902018-08-02 11:18:00 +08001048 public void onShow(DialogInterface dialog) {
Dave Mankoffc88a2d72020-01-10 15:59:31 -05001049 mMetricsLogger.visible(MetricsEvent.POWER_MENU);
Chris Wren65a70c92020-04-02 10:43:46 -04001050 mUiEventLogger.log(GlobalActionsEvent.GA_POWER_MENU_OPEN);
Shaotang Li786da902018-08-02 11:18:00 +08001051 }
1052
Lucas Dupincb6726f2020-04-07 19:32:38 -07001053 private int getActionLayoutId() {
1054 if (shouldShowControls()) {
1055 return com.android.systemui.R.layout.global_actions_grid_item_v2;
1056 }
1057 return com.android.systemui.R.layout.global_actions_grid_item;
1058 }
1059
Jason Monk361915c2017-03-21 20:33:59 -04001060 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001061 * The adapter used for the list within the global actions dialog, taking into account whether
1062 * the keyguard is showing via
1063 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing}
1064 * and whether the device is provisioned via
1065 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
Jason Monk361915c2017-03-21 20:33:59 -04001066 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001067 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001068 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -04001069 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -04001070 for (int i = 0; i < mItems.size(); i++) {
1071 final Action action = mItems.get(i);
1072
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001073 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
1074 count++;
Jason Monk361915c2017-03-21 20:33:59 -04001075 }
Jason Monk361915c2017-03-21 20:33:59 -04001076 }
1077 return count;
1078 }
1079
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001080 private boolean shouldBeShown(Action action) {
1081 if (mKeyguardShowing && !action.showDuringKeyguard()) {
1082 return false;
1083 }
1084 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
1085 return false;
1086 }
1087 return true;
1088 }
1089
1090 @Override
1091 public int countSeparatedItems() {
1092 return countItems(true);
1093 }
1094
1095 @Override
1096 public int countListItems() {
1097 return countItems(false);
1098 }
1099
1100 @Override
1101 public int getCount() {
1102 return countSeparatedItems() + countListItems();
1103 }
1104
Jason Monk361915c2017-03-21 20:33:59 -04001105 @Override
1106 public boolean isEnabled(int position) {
1107 return getItem(position).isEnabled();
1108 }
1109
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001110 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001111 public boolean areAllItemsEnabled() {
1112 return false;
1113 }
1114
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001115 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001116 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -04001117 int filteredPos = 0;
1118 for (int i = 0; i < mItems.size(); i++) {
1119 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001120 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001121 continue;
1122 }
1123 if (filteredPos == position) {
1124 return action;
1125 }
1126 filteredPos++;
1127 }
1128
1129 throw new IllegalArgumentException("position " + position
1130 + " out of range of showable actions"
1131 + ", filtered count=" + getCount()
1132 + ", keyguardshowing=" + mKeyguardShowing
1133 + ", provisioned=" + mDeviceProvisioned);
1134 }
1135
1136
1137 public long getItemId(int position) {
1138 return position;
1139 }
1140
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001141 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001142 public View getView(int position, View convertView, ViewGroup parent) {
1143 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001144 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001145 view.setOnClickListener(v -> onClickItem(position));
Aran Inkb4199d92020-02-07 13:25:21 -05001146 if (action instanceof LongPressAction) {
1147 view.setOnLongClickListener(v -> onLongClickItem(position));
1148 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001149 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001150 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001151
1152 @Override
1153 public boolean onLongClickItem(int position) {
1154 final Action action = mAdapter.getItem(position);
1155 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001156 if (mDialog != null) {
1157 mDialog.dismiss();
1158 } else {
1159 Log.w(TAG, "Action long-clicked while mDialog is null.");
1160 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001161 return ((LongPressAction) action).onLongPress();
1162 }
1163 return false;
1164 }
1165
1166 @Override
1167 public void onClickItem(int position) {
1168 Action item = mAdapter.getItem(position);
1169 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001170 if (mDialog != null) {
1171 mDialog.dismiss();
1172 } else {
1173 Log.w(TAG, "Action clicked while mDialog is null.");
1174 }
1175 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001176 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001177 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001178
1179 @Override
1180 public boolean shouldBeSeparated(int position) {
1181 return getItem(position).shouldBeSeparated();
1182 }
Jason Monk361915c2017-03-21 20:33:59 -04001183 }
1184
1185 // note: the scheme below made more sense when we were planning on having
1186 // 8 different things in the global actions dialog. seems overkill with
1187 // only 3 items now, but may as well keep this flexible approach so it will
1188 // be easy should someone decide at the last minute to include something
1189 // else, such as 'enable wifi', or 'enable bluetooth'
1190
1191 /**
1192 * What each item in the global actions dialog must be able to support.
1193 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001194 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001195 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001196 * @return Text that will be announced when dialog is created. null for none.
Jason Monk361915c2017-03-21 20:33:59 -04001197 */
1198 CharSequence getLabelForAccessibility(Context context);
1199
1200 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1201
1202 void onPress();
1203
1204 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001205 * @return whether this action should appear in the dialog when the keygaurd is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001206 */
1207 boolean showDuringKeyguard();
1208
1209 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001210 * @return whether this action should appear in the dialog before the device is
1211 * provisioned.onlongpress
Jason Monk361915c2017-03-21 20:33:59 -04001212 */
1213 boolean showBeforeProvisioning();
1214
1215 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001216
1217 default boolean shouldBeSeparated() {
1218 return false;
1219 }
Jason Monk361915c2017-03-21 20:33:59 -04001220 }
1221
1222 /**
1223 * An action that also supports long press.
1224 */
1225 private interface LongPressAction extends Action {
1226 boolean onLongPress();
1227 }
1228
1229 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001230 * A single press action maintains no state, just responds to a press and takes an action.
Jason Monk361915c2017-03-21 20:33:59 -04001231 */
Matt Pietale0661b62020-01-29 14:35:31 -05001232
1233 private abstract class SinglePressAction implements Action {
Jason Monk361915c2017-03-21 20:33:59 -04001234 private final int mIconResId;
1235 private final Drawable mIcon;
1236 private final int mMessageResId;
1237 private final CharSequence mMessage;
1238
1239 protected SinglePressAction(int iconResId, int messageResId) {
1240 mIconResId = iconResId;
1241 mMessageResId = messageResId;
1242 mMessage = null;
1243 mIcon = null;
1244 }
1245
1246 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1247 mIconResId = iconResId;
1248 mMessageResId = 0;
1249 mMessage = message;
1250 mIcon = icon;
1251 }
1252
1253 public boolean isEnabled() {
1254 return true;
1255 }
1256
1257 public String getStatus() {
1258 return null;
1259 }
1260
1261 abstract public void onPress();
1262
1263 public CharSequence getLabelForAccessibility(Context context) {
1264 if (mMessage != null) {
1265 return mMessage;
1266 } else {
1267 return context.getString(mMessageResId);
1268 }
1269 }
1270
1271 public View create(
1272 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Lucas Dupincb6726f2020-04-07 19:32:38 -07001273 View v = inflater.inflate(getActionLayoutId(), parent, false /* attach */);
Jason Monk361915c2017-03-21 20:33:59 -04001274
Lucas Dupincb6726f2020-04-07 19:32:38 -07001275 ImageView icon = v.findViewById(R.id.icon);
1276 TextView messageView = v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001277 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001278
Jason Monk361915c2017-03-21 20:33:59 -04001279 if (mIcon != null) {
1280 icon.setImageDrawable(mIcon);
1281 icon.setScaleType(ScaleType.CENTER_CROP);
1282 } else if (mIconResId != 0) {
1283 icon.setImageDrawable(context.getDrawable(mIconResId));
1284 }
1285 if (mMessage != null) {
1286 messageView.setText(mMessage);
1287 } else {
1288 messageView.setText(mMessageResId);
1289 }
1290
1291 return v;
1292 }
1293 }
1294
Lucas Dupincb6726f2020-04-07 19:32:38 -07001295 private enum ToggleState {
1296 Off(false),
1297 TurningOn(true),
1298 TurningOff(true),
1299 On(false);
1300
1301 private final boolean mInTransition;
1302
1303 ToggleState(boolean intermediate) {
1304 mInTransition = intermediate;
1305 }
1306
1307 public boolean inTransition() {
1308 return mInTransition;
1309 }
1310 }
1311
Jason Monk361915c2017-03-21 20:33:59 -04001312 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001313 * A toggle action knows whether it is on or off, and displays an icon and status message
1314 * accordingly.
Jason Monk361915c2017-03-21 20:33:59 -04001315 */
Lucas Dupincb6726f2020-04-07 19:32:38 -07001316 private abstract class ToggleAction implements Action {
Jason Monk361915c2017-03-21 20:33:59 -04001317
Lucas Dupincb6726f2020-04-07 19:32:38 -07001318 protected ToggleState mState = ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -04001319
1320 // prefs
1321 protected int mEnabledIconResId;
1322 protected int mDisabledIconResid;
1323 protected int mMessageResId;
1324 protected int mEnabledStatusMessageResId;
1325 protected int mDisabledStatusMessageResId;
1326
1327 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001328 * @param enabledIconResId The icon for when this action is on.
1329 * @param disabledIconResid The icon for when this action is off.
1330 * @param message The general information message, e.g 'Silent Mode'
1331 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001332 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1333 */
1334 public ToggleAction(int enabledIconResId,
1335 int disabledIconResid,
1336 int message,
1337 int enabledStatusMessageResId,
1338 int disabledStatusMessageResId) {
1339 mEnabledIconResId = enabledIconResId;
1340 mDisabledIconResid = disabledIconResid;
1341 mMessageResId = message;
1342 mEnabledStatusMessageResId = enabledStatusMessageResId;
1343 mDisabledStatusMessageResId = disabledStatusMessageResId;
1344 }
1345
1346 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001347 * Override to make changes to resource IDs just before creating the View.
Jason Monk361915c2017-03-21 20:33:59 -04001348 */
1349 void willCreate() {
1350
1351 }
1352
1353 @Override
1354 public CharSequence getLabelForAccessibility(Context context) {
1355 return context.getString(mMessageResId);
1356 }
1357
1358 public View create(Context context, View convertView, ViewGroup parent,
1359 LayoutInflater inflater) {
1360 willCreate();
1361
Lucas Dupincb6726f2020-04-07 19:32:38 -07001362 View v = inflater.inflate(getActionLayoutId(), parent, false /* attach */);
Jason Monk361915c2017-03-21 20:33:59 -04001363
1364 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1365 TextView messageView = (TextView) v.findViewById(R.id.message);
Jason Monk361915c2017-03-21 20:33:59 -04001366 final boolean enabled = isEnabled();
Lucas Dupincb6726f2020-04-07 19:32:38 -07001367 boolean on = ((mState == ToggleState.On) || (mState == ToggleState.TurningOn));
Jason Monk361915c2017-03-21 20:33:59 -04001368
1369 if (messageView != null) {
Koji Fukui5c010ee2019-12-24 16:12:07 +09001370 messageView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
Jason Monk361915c2017-03-21 20:33:59 -04001371 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001372 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001373 }
1374
Jason Monk361915c2017-03-21 20:33:59 -04001375 if (icon != null) {
1376 icon.setImageDrawable(context.getDrawable(
1377 (on ? mEnabledIconResId : mDisabledIconResid)));
1378 icon.setEnabled(enabled);
1379 }
1380
Jason Monk361915c2017-03-21 20:33:59 -04001381 v.setEnabled(enabled);
1382
1383 return v;
1384 }
1385
1386 public final void onPress() {
1387 if (mState.inTransition()) {
1388 Log.w(TAG, "shouldn't be able to toggle when in transition");
1389 return;
1390 }
1391
Lucas Dupincb6726f2020-04-07 19:32:38 -07001392 final boolean nowOn = !(mState == ToggleState.On);
Jason Monk361915c2017-03-21 20:33:59 -04001393 onToggle(nowOn);
1394 changeStateFromPress(nowOn);
1395 }
1396
1397 public boolean isEnabled() {
1398 return !mState.inTransition();
1399 }
1400
1401 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08001402 * Implementations may override this if their state can be in on of the intermediate states
1403 * until some notification is received (e.g airplane mode is 'turning off' until we know the
1404 * wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001405 *
Jason Monk361915c2017-03-21 20:33:59 -04001406 * @param buttonOn Whether the button was turned on or off
1407 */
1408 protected void changeStateFromPress(boolean buttonOn) {
Lucas Dupincb6726f2020-04-07 19:32:38 -07001409 mState = buttonOn ? ToggleState.On : ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -04001410 }
1411
1412 abstract void onToggle(boolean on);
1413
Lucas Dupincb6726f2020-04-07 19:32:38 -07001414 public void updateState(ToggleState state) {
Jason Monk361915c2017-03-21 20:33:59 -04001415 mState = state;
1416 }
1417 }
1418
Aran Ink780d4502020-02-14 10:39:58 -05001419 private class AirplaneModeAction extends ToggleAction {
1420 AirplaneModeAction() {
1421 super(
Sean Pont9d4fb032020-03-04 18:54:32 -08001422 R.drawable.ic_lock_airplane_mode,
1423 R.drawable.ic_lock_airplane_mode_off,
1424 R.string.global_actions_toggle_airplane_mode,
1425 R.string.global_actions_airplane_mode_on_status,
1426 R.string.global_actions_airplane_mode_off_status);
Aran Ink780d4502020-02-14 10:39:58 -05001427 }
Sean Pont9d4fb032020-03-04 18:54:32 -08001428
Aran Ink780d4502020-02-14 10:39:58 -05001429 void onToggle(boolean on) {
1430 if (mHasTelephony && TelephonyProperties.in_ecm_mode().orElse(false)) {
1431 mIsWaitingForEcmExit = true;
1432 // Launch ECM exit dialog
1433 Intent ecmDialogIntent =
1434 new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
1435 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1436 mContext.startActivity(ecmDialogIntent);
1437 } else {
1438 changeAirplaneModeSystemSetting(on);
1439 }
1440 }
1441
1442 @Override
1443 protected void changeStateFromPress(boolean buttonOn) {
1444 if (!mHasTelephony) return;
1445
1446 // In ECM mode airplane state cannot be changed
1447 if (!TelephonyProperties.in_ecm_mode().orElse(false)) {
Lucas Dupincb6726f2020-04-07 19:32:38 -07001448 mState = buttonOn ? ToggleState.TurningOn : ToggleState.TurningOff;
Aran Ink780d4502020-02-14 10:39:58 -05001449 mAirplaneState = mState;
1450 }
1451 }
1452
1453 public boolean showDuringKeyguard() {
1454 return true;
1455 }
1456
1457 public boolean showBeforeProvisioning() {
1458 return false;
1459 }
1460 }
1461
Jason Monk361915c2017-03-21 20:33:59 -04001462 private class SilentModeToggleAction extends ToggleAction {
1463 public SilentModeToggleAction() {
1464 super(R.drawable.ic_audio_vol_mute,
1465 R.drawable.ic_audio_vol,
1466 R.string.global_action_toggle_silent_mode,
1467 R.string.global_action_silent_mode_on_status,
1468 R.string.global_action_silent_mode_off_status);
1469 }
1470
1471 void onToggle(boolean on) {
1472 if (on) {
1473 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1474 } else {
1475 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1476 }
1477 }
1478
1479 public boolean showDuringKeyguard() {
1480 return true;
1481 }
1482
1483 public boolean showBeforeProvisioning() {
1484 return false;
1485 }
1486 }
1487
1488 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1489
Jason Monk16fbd9d2017-04-27 14:28:49 -04001490 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001491
1492 private final AudioManager mAudioManager;
1493 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001494
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001495 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001496 mAudioManager = audioManager;
1497 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001498 }
1499
1500 private int ringerModeToIndex(int ringerMode) {
1501 // They just happen to coincide
1502 return ringerMode;
1503 }
1504
1505 private int indexToRingerMode(int index) {
1506 // They just happen to coincide
1507 return index;
1508 }
1509
1510 @Override
1511 public CharSequence getLabelForAccessibility(Context context) {
1512 return null;
1513 }
1514
1515 public View create(Context context, View convertView, ViewGroup parent,
1516 LayoutInflater inflater) {
1517 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1518
1519 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1520 for (int i = 0; i < 3; i++) {
1521 View itemView = v.findViewById(ITEM_IDS[i]);
1522 itemView.setSelected(selectedIndex == i);
1523 // Set up click handler
1524 itemView.setTag(i);
1525 itemView.setOnClickListener(this);
1526 }
1527 return v;
1528 }
1529
1530 public void onPress() {
1531 }
1532
1533 public boolean showDuringKeyguard() {
1534 return true;
1535 }
1536
1537 public boolean showBeforeProvisioning() {
1538 return false;
1539 }
1540
1541 public boolean isEnabled() {
1542 return true;
1543 }
1544
1545 void willCreate() {
1546 }
1547
1548 public void onClick(View v) {
1549 if (!(v.getTag() instanceof Integer)) return;
1550
1551 int index = (Integer) v.getTag();
1552 mAudioManager.setRingerMode(indexToRingerMode(index));
1553 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1554 }
1555 }
1556
1557 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1558 public void onReceive(Context context, Intent intent) {
1559 String action = intent.getAction();
1560 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1561 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1562 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1563 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001564 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001565 }
Jayachandran C142eae02019-12-13 19:29:20 -08001566 } else if (TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001567 // Airplane mode can be changed after ECM exits if airplane toggle button
1568 // is pressed during ECM mode
Daniel Bright4bcbfce2020-01-09 21:58:16 -08001569 if (!(intent.getBooleanExtra(TelephonyManager.EXTRA_PHONE_IN_ECM_STATE, false))
1570 && mIsWaitingForEcmExit) {
Jason Monk361915c2017-03-21 20:33:59 -04001571 mIsWaitingForEcmExit = false;
1572 changeAirplaneModeSystemSetting(true);
1573 }
1574 }
1575 }
1576 };
1577
1578 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1579 @Override
1580 public void onServiceStateChanged(ServiceState serviceState) {
1581 if (!mHasTelephony) return;
1582 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
Lucas Dupincb6726f2020-04-07 19:32:38 -07001583 mAirplaneState = inAirplaneMode ? ToggleState.On : ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -04001584 mAirplaneModeOn.updateState(mAirplaneState);
1585 mAdapter.notifyDataSetChanged();
1586 }
1587 };
1588
Jason Monk361915c2017-03-21 20:33:59 -04001589 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1590 @Override
1591 public void onChange(boolean selfChange) {
1592 onAirplaneModeChanged();
1593 }
1594 };
1595
1596 private static final int MESSAGE_DISMISS = 0;
1597 private static final int MESSAGE_REFRESH = 1;
1598 private static final int MESSAGE_SHOW = 2;
1599 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1600
1601 private Handler mHandler = new Handler() {
1602 public void handleMessage(Message msg) {
1603 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001604 case MESSAGE_DISMISS:
1605 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001606 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1607 mDialog.dismissImmediately();
1608 } else {
1609 mDialog.dismiss();
1610 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001611 mDialog = null;
1612 }
1613 break;
1614 case MESSAGE_REFRESH:
1615 refreshSilentMode();
1616 mAdapter.notifyDataSetChanged();
1617 break;
1618 case MESSAGE_SHOW:
1619 handleShow();
1620 break;
Jason Monk361915c2017-03-21 20:33:59 -04001621 }
1622 }
1623 };
1624
1625 private void onAirplaneModeChanged() {
1626 // Let the service state callbacks handle the state.
1627 if (mHasTelephony) return;
1628
1629 boolean airplaneModeOn = Settings.Global.getInt(
Dave Mankoffc88a2d72020-01-10 15:59:31 -05001630 mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -04001631 Settings.Global.AIRPLANE_MODE_ON,
1632 0) == 1;
Lucas Dupincb6726f2020-04-07 19:32:38 -07001633 mAirplaneState = airplaneModeOn ? ToggleState.On : ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -04001634 mAirplaneModeOn.updateState(mAirplaneState);
1635 }
1636
1637 /**
1638 * Change the airplane mode system setting
1639 */
1640 private void changeAirplaneModeSystemSetting(boolean on) {
1641 Settings.Global.putInt(
Dave Mankoffc88a2d72020-01-10 15:59:31 -05001642 mContentResolver,
Jason Monk361915c2017-03-21 20:33:59 -04001643 Settings.Global.AIRPLANE_MODE_ON,
1644 on ? 1 : 0);
1645 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1646 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1647 intent.putExtra("state", on);
1648 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1649 if (!mHasTelephony) {
Lucas Dupincb6726f2020-04-07 19:32:38 -07001650 mAirplaneState = on ? ToggleState.On : ToggleState.Off;
Jason Monk361915c2017-03-21 20:33:59 -04001651 }
1652 }
1653
Fabian Kozynskib6a20372020-04-01 09:36:43 -04001654 @NonNull
1655 @Override
1656 public Lifecycle getLifecycle() {
1657 return mLifecycle;
1658 }
1659
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001660 private static final class ActionsDialog extends Dialog implements DialogInterface,
1661 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001662
Jason Monk16fbd9d2017-04-27 14:28:49 -04001663 private final Context mContext;
1664 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001665 private final IStatusBarService mStatusBarService;
1666 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001667 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001668 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001669 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001670 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001671 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001672 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001673 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001674 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001675 private boolean mHadTopUi;
wilsonshihe8321942019-10-18 18:39:46 +08001676 private final NotificationShadeWindowController mNotificationShadeWindowController;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001677 private final NotificationShadeDepthController mDepthController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001678 private final BlurUtils mBlurUtils;
Matt Pietal22231792020-01-23 09:51:09 -05001679
1680 private ControlsUiController mControlsUiController;
1681 private ViewGroup mControlsView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001682
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001683 ActionsDialog(Context context, MyAdapter adapter,
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001684 GlobalActionsPanelPlugin.PanelViewController plugin,
1685 NotificationShadeDepthController depthController,
Lucas Dupin991415e2019-11-25 17:48:58 -08001686 SysuiColorExtractor sysuiColorExtractor, IStatusBarService statusBarService,
1687 NotificationShadeWindowController notificationShadeWindowController,
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001688 ControlsUiController controlsUiController, BlurUtils blurUtils) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001689 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001690 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001691 mAdapter = adapter;
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001692 mDepthController = depthController;
Lucas Dupin991415e2019-11-25 17:48:58 -08001693 mColorExtractor = sysuiColorExtractor;
1694 mStatusBarService = statusBarService;
1695 mNotificationShadeWindowController = notificationShadeWindowController;
Matt Pietal22231792020-01-23 09:51:09 -05001696 mControlsUiController = controlsUiController;
Lucas Dupin99f5fba2020-03-12 12:59:18 -07001697 mBlurUtils = blurUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001698
1699 // Window initialization
1700 Window window = getWindow();
1701 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001702 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1703 window.getDecorView();
1704 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1705 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1706 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1707 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1708 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001709 window.addFlags(
1710 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Sean Pont9d4fb032020-03-04 18:54:32 -08001711 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1712 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
1713 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1714 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1715 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001716 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Tiger Huang52724442020-01-20 21:38:42 +08001717 window.getAttributes().setFitInsetsTypes(0 /* types */);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001718 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001719
1720 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001721 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001722 }
1723
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001724 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001725 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001726 }
1727
1728 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001729 int rotation = RotationUtils.getRotation(mContext);
1730 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1731 if (rotation != RotationUtils.ROTATION_NONE) {
1732 if (rotationLocked) {
1733 if (mResetOrientationData == null) {
1734 mResetOrientationData = new ResetOrientationData();
1735 mResetOrientationData.locked = true;
1736 mResetOrientationData.rotation = rotation;
1737 }
1738
1739 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001740 // This call is posted so that the rotation does not change until post-layout,
1741 // otherwise onConfigurationChanged() may not get invoked.
1742 mGlobalActionsLayout.post(() ->
1743 RotationPolicy.setRotationLockAtAngle(
1744 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001745 }
1746 } else {
1747 if (!rotationLocked) {
1748 if (mResetOrientationData == null) {
1749 mResetOrientationData = new ResetOrientationData();
1750 mResetOrientationData.locked = false;
1751 }
1752
1753 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001754 // This call is posted so that the rotation does not change until post-layout,
1755 // otherwise onConfigurationChanged() may not get invoked.
1756 mGlobalActionsLayout.post(() ->
1757 RotationPolicy.setRotationLockAtAngle(
1758 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001759 }
1760
Steve Elliott53f12ae2019-05-13 17:14:15 -04001761 // Disable rotation suggestions, if enabled
1762 setRotationSuggestionsEnabled(false);
1763
Steve Elliott300b48f2019-05-29 14:13:50 -04001764 FrameLayout panelContainer =
1765 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001766 FrameLayout.LayoutParams panelParams =
1767 new FrameLayout.LayoutParams(
1768 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001769 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001770 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott48f75db2019-05-03 15:03:38 -04001771 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001772 }
1773
1774 private void initializeLayout() {
1775 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001776 fixNavBarClipping();
Matt Pietal22231792020-01-23 09:51:09 -05001777 mControlsView = findViewById(com.android.systemui.R.id.global_actions_controls);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001778 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001779 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001780 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001781 @Override
1782 public boolean dispatchPopulateAccessibilityEvent(
1783 View host, AccessibilityEvent event) {
1784 // Populate the title here, just as Activity does
1785 event.getText().add(mContext.getString(R.string.global_actions));
1786 return true;
1787 }
1788 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001789 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001790 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001791
Aran Ink06090262020-03-03 13:23:17 -05001792 View globalActionsParent = (View) mGlobalActionsLayout.getParent();
1793 globalActionsParent.setOnClickListener(v -> dismiss());
1794
1795 // add fall-through dismiss handling to root view
1796 View rootView = findViewById(com.android.systemui.R.id.global_actions_grid_root);
1797 if (rootView != null) {
1798 rootView.setOnClickListener(v -> dismiss());
1799 }
1800
Steve Elliott86ef6282019-05-08 14:45:04 -04001801 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001802 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001803 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001804 if (mBackgroundDrawable == null) {
1805 mBackgroundDrawable = new ScrimDrawable();
Lucas Dupin3a8531e2020-04-06 17:18:09 -07001806 if (mControlsUiController != null) {
1807 mScrimAlpha = 1.0f;
1808 } else {
1809 mScrimAlpha = mBlurUtils.supportsBlursOnWindows()
1810 ? ScrimController.BLUR_SCRIM_ALPHA : ScrimController.BUSY_SCRIM_ALPHA;
1811 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001812 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001813 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001814 }
1815
Steve Elliott300b48f2019-05-29 14:13:50 -04001816 private void fixNavBarClipping() {
1817 ViewGroup content = findViewById(android.R.id.content);
1818 content.setClipChildren(false);
1819 content.setClipToPadding(false);
1820 ViewGroup contentParent = (ViewGroup) content.getParent();
1821 contentParent.setClipChildren(false);
1822 contentParent.setClipToPadding(false);
1823 }
1824
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001825 private int getGlobalActionsLayoutId(Context context) {
Matt Pietal22231792020-01-23 09:51:09 -05001826 if (mControlsUiController != null) {
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05001827 return com.android.systemui.R.layout.global_actions_grid_v2;
1828 }
1829
Steve Elliottdc165632019-05-23 14:26:31 -04001830 int rotation = RotationUtils.getRotation(context);
1831 boolean useGridLayout = isForceGridEnabled(context)
1832 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1833 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001834 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001835 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001836 } else {
1837 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001838 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001839 } else {
1840 if (useGridLayout) {
1841 return com.android.systemui.R.layout.global_actions_grid;
1842 } else {
1843 return com.android.systemui.R.layout.global_actions_column;
1844 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001845 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001846 }
1847
Jason Monk361915c2017-03-21 20:33:59 -04001848 @Override
1849 protected void onStart() {
1850 super.setCanceledOnTouchOutside(true);
1851 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001852 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001853
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001854 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001855 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001856 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001857 updateColors(colors, false /* animate */);
1858 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001859 }
1860
1861 /**
1862 * Updates background and system bars according to current GradientColors.
Sean Pont9d4fb032020-03-04 18:54:32 -08001863 *
1864 * @param colors Colors and hints to use.
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001865 * @param animate Interpolates gradient if true, just sets otherwise.
1866 */
1867 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001868 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001869 return;
1870 }
Lucas Dupin3a8531e2020-04-06 17:18:09 -07001871 boolean hasControls = mControlsUiController != null;
1872 ((ScrimDrawable) mBackgroundDrawable).setColor(
1873 !hasControls && colors.supportsDarkText() ? Color.WHITE : Color.BLACK, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001874 View decorView = getWindow().getDecorView();
1875 if (colors.supportsDarkText()) {
1876 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001877 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001878 } else {
1879 decorView.setSystemUiVisibility(0);
1880 }
Jason Monk361915c2017-03-21 20:33:59 -04001881 }
1882
1883 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001884 protected void onStop() {
1885 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001886 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001887 }
1888
1889 @Override
1890 public void show() {
1891 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001892 mShowing = true;
wilsonshihe8321942019-10-18 18:39:46 +08001893 mHadTopUi = mNotificationShadeWindowController.getForceHasTopUi();
1894 mNotificationShadeWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001895 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001896 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1897 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001898 mGlobalActionsLayout.setAlpha(0);
1899 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001900 .alpha(1)
1901 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001902 .translationY(0)
Lucas Dupinb079daa2020-03-24 15:56:23 -07001903 .setDuration(450)
Lucas Dupinde9db422017-07-19 17:15:41 -07001904 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001905 .setUpdateListener(animation -> {
Lucas Dupin991415e2019-11-25 17:48:58 -08001906 float animatedValue = animation.getAnimatedFraction();
1907 int alpha = (int) (animatedValue * mScrimAlpha * 255);
Steve Elliott9b87a442019-03-05 10:24:16 -05001908 mBackgroundDrawable.setAlpha(alpha);
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001909 mDepthController.updateGlobalDialogVisibility(animatedValue,
1910 mGlobalActionsLayout);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001911 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001912 .start();
Matt Pietal22231792020-01-23 09:51:09 -05001913 if (mControlsUiController != null) {
1914 mControlsUiController.show(mControlsView);
1915 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001916 }
1917
1918 @Override
1919 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001920 if (!mShowing) {
1921 return;
1922 }
1923 mShowing = false;
Matt Pietal22231792020-01-23 09:51:09 -05001924 if (mControlsUiController != null) mControlsUiController.hide();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001925 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001926 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001927 mGlobalActionsLayout.setAlpha(1);
1928 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001929 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001930 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1931 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Lucas Dupinb079daa2020-03-24 15:56:23 -07001932 .setDuration(550)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001933 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001934 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001935 .setUpdateListener(animation -> {
Lucas Dupin991415e2019-11-25 17:48:58 -08001936 float animatedValue = 1f - animation.getAnimatedFraction();
1937 int alpha = (int) (animatedValue * mScrimAlpha * 255);
Steve Elliott9b87a442019-03-05 10:24:16 -05001938 mBackgroundDrawable.setAlpha(alpha);
Santiago Etchebehere68eb53e2020-02-25 14:25:34 -08001939 mDepthController.updateGlobalDialogVisibility(animatedValue,
1940 mGlobalActionsLayout);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001941 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001942 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001943 dismissPanel();
1944 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001945 }
1946
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001947 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001948 mShowing = false;
Matt Pietal019feaa2020-01-31 14:51:37 -05001949 if (mControlsUiController != null) mControlsUiController.hide();
Steve Elliott48f75db2019-05-03 15:03:38 -04001950 dismissPanel();
1951 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001952 completeDismiss();
1953 }
1954
1955 private void completeDismiss() {
wilsonshihe8321942019-10-18 18:39:46 +08001956 mNotificationShadeWindowController.setForceHasTopUi(mHadTopUi);
Lucas Dupin77198562020-03-31 14:16:16 -07001957 mDepthController.updateGlobalDialogVisibility(0, null /* view */);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001958 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001959 }
1960
1961 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001962 if (mPanelController != null) {
1963 mPanelController.onDismissed();
1964 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001965 }
1966
Steve Elliott53f12ae2019-05-13 17:14:15 -04001967 private void setRotationSuggestionsEnabled(boolean enabled) {
1968 try {
1969 final int userId = Binder.getCallingUserHandle().getIdentifier();
1970 final int what = enabled
1971 ? StatusBarManager.DISABLE2_NONE
1972 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1973 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1974 } catch (RemoteException ex) {
1975 throw ex.rethrowFromSystemServer();
1976 }
1977 }
1978
Steve Elliott48f75db2019-05-03 15:03:38 -04001979 private void resetOrientation() {
1980 if (mResetOrientationData != null) {
1981 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1982 mResetOrientationData.rotation);
1983 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001984 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001985 }
1986
Jason Monk361915c2017-03-21 20:33:59 -04001987 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001988 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001989 if (mKeyguardShowing) {
1990 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001991 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1992 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001993 }
1994 } else {
1995 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001996 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1997 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001998 }
1999 }
2000 }
2001
2002 public void setKeyguardShowing(boolean keyguardShowing) {
2003 mKeyguardShowing = keyguardShowing;
2004 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05002005
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04002006 public void refreshDialog() {
2007 initializeLayout();
2008 mGlobalActionsLayout.updateList();
2009 }
2010
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05002011 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04002012 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04002013 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05002014 }
2015 }
Steve Elliott48f75db2019-05-03 15:03:38 -04002016
2017 private static class ResetOrientationData {
2018 public boolean locked;
2019 public int rotation;
2020 }
Jason Monk361915c2017-03-21 20:33:59 -04002021 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05002022
2023 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04002024 * Determines whether or not debug mode has been activated for the Global Actions Panel.
2025 */
2026 private static boolean isPanelDebugModeEnabled(Context context) {
2027 return Settings.Secure.getInt(context.getContentResolver(),
2028 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
2029 }
2030
2031 /**
Sean Pont9d4fb032020-03-04 18:54:32 -08002032 * Determines whether or not the Global Actions menu should be forced to use the newer
2033 * grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05002034 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04002035 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04002036 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05002037 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04002038
Matt Pietale0661b62020-01-29 14:35:31 -05002039 private boolean shouldShowControls() {
Fabian Kozynskif6063ae2020-02-24 16:14:35 -05002040 return mKeyguardStateController.isUnlocked()
Fabian Kozynski0424ab12020-02-21 12:09:17 -05002041 && mControlsUiController.getAvailable()
Matt Pietalcd757c82020-04-08 10:20:48 -04002042 && !mControlsServiceInfos.isEmpty();
Matt Pietalfd3b6cb2020-01-08 09:43:54 -05002043 }
Jason Monk361915c2017-03-21 20:33:59 -04002044}