blob: 51c2ddca869caed4e776e886ef50b126351a9fe5 [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Steve Elliott4c868852019-03-14 16:25:41 -040027import android.app.PendingIntent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040028import android.app.StatusBarManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070029import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000030import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010031import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040032import android.content.BroadcastReceiver;
33import android.content.Context;
34import android.content.DialogInterface;
35import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.UserInfo;
38import android.database.ContentObserver;
39import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040042import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040044import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.os.Message;
46import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.os.UserHandle;
50import android.os.UserManager;
51import android.os.Vibrator;
52import android.provider.Settings;
53import android.service.dreams.DreamService;
54import android.service.dreams.IDreamManager;
Inseob Kim5e82f732019-11-08 15:08:38 +090055import android.sysprop.TelephonyProperties;
Fan Zhangf9914762019-11-01 15:58:38 -070056import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040057import android.telephony.PhoneStateListener;
58import android.telephony.ServiceState;
59import android.telephony.TelephonyManager;
60import android.text.TextUtils;
61import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080062import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040063import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070064import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.LayoutInflater;
66import android.view.View;
67import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070068import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.view.WindowManager;
70import android.view.WindowManagerGlobal;
71import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050072import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.ImageView;
74import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040075import android.widget.TextView;
76
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.R;
78import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070080import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040083import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.telephony.TelephonyIntents;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040086import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050087import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040088import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.internal.widget.LockPatternUtils;
90import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050092import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050093import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040094import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010095import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040096import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010097import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050098import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050099import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -0400100import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700101import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400102import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800103import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500104import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500105import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700106
Jason Monk361915c2017-03-21 20:33:59 -0400107import java.util.ArrayList;
108import java.util.List;
109
110/**
111 * Helper to show the global actions dialog. Each item is an {@link Action} that
112 * may show depending on whether the keyguard is showing, and whether the device
113 * is provisioned.
114 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500115public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
116 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400117
118 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
119 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700120 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400121
122 private static final String TAG = "GlobalActionsDialog";
123
124 private static final boolean SHOW_SILENT_TOGGLE = true;
125
126 /* Valid settings for global actions keys.
127 * see config.xml config_globalActionList */
128 private static final String GLOBAL_ACTION_KEY_POWER = "power";
129 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
130 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
131 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
132 private static final String GLOBAL_ACTION_KEY_USERS = "users";
133 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
134 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
135 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
136 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
137 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000138 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800139 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500140 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400141
142 private final Context mContext;
143 private final GlobalActionsManager mWindowManagerFuncs;
144 private final AudioManager mAudioManager;
145 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000146 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800147 private final LockPatternUtils mLockPatternUtils;
148 private final KeyguardManager mKeyguardManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000149 private final BroadcastDispatcher mBroadcastDispatcher;
Jason Monk361915c2017-03-21 20:33:59 -0400150
151 private ArrayList<Action> mItems;
152 private ActionsDialog mDialog;
153
154 private Action mSilentModeAction;
155 private ToggleAction mAirplaneModeOn;
156
157 private MyAdapter mAdapter;
158
159 private boolean mKeyguardShowing = false;
160 private boolean mDeviceProvisioned = false;
161 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
162 private boolean mIsWaitingForEcmExit = false;
163 private boolean mHasTelephony;
164 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000165 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800166 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400167 private final boolean mShowSilentToggle;
168 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500169 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400170 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400171 private final ActivityStarter mActivityStarter;
172 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500173
Jason Monk361915c2017-03-21 20:33:59 -0400174 /**
175 * @param context everything needs a context :(
176 */
177 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700178 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400179 mWindowManagerFuncs = windowManagerFuncs;
180 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
181 mDreamManager = IDreamManager.Stub.asInterface(
182 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000183 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
184 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800185 mLockPatternUtils = new LockPatternUtils(mContext);
186 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000187 mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class);
Jason Monk361915c2017-03-21 20:33:59 -0400188
189 // receive broadcasts
190 IntentFilter filter = new IntentFilter();
191 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
192 filter.addAction(Intent.ACTION_SCREEN_OFF);
193 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000194 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400195
196 ConnectivityManager cm = (ConnectivityManager)
197 context.getSystemService(Context.CONNECTIVITY_SERVICE);
198 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
199
200 // get notified of phone state changes
201 TelephonyManager telephonyManager =
202 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
203 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
204 mContext.getContentResolver().registerContentObserver(
205 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
206 mAirplaneModeObserver);
207 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
208 mHasVibrator = vibrator != null && vibrator.hasVibrator();
209
210 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
211 R.bool.config_useFixedVolume);
212
213 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500214 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400215 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700216
217 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500218
Steve Elliott4c868852019-03-14 16:25:41 -0400219 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400220 KeyguardStateController keyguardStateController =
221 Dependency.get(KeyguardStateController.class);
222 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
223 @Override
224 public void onUnlockedChanged() {
225 if (mDialog != null && mDialog.mPanelController != null) {
226 boolean locked = !keyguardStateController.canDismissLockScreen();
227 mDialog.mPanelController.onDeviceLockStateChanged(locked);
228 }
229 }
230 });
Jason Monk361915c2017-03-21 20:33:59 -0400231 }
232
233 /**
234 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400235 *
Jason Monk361915c2017-03-21 20:33:59 -0400236 * @param keyguardShowing True if keyguard is showing
237 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400238 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
239 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400240 mKeyguardShowing = keyguardShowing;
241 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400242 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400243 if (mDialog != null) {
244 mDialog.dismiss();
245 mDialog = null;
246 // Show delayed, so that the dismiss of the previous dialog completes
247 mHandler.sendEmptyMessage(MESSAGE_SHOW);
248 } else {
249 handleShow();
250 }
251 }
252
Charles He9851a8d2017-10-10 17:31:30 +0100253 /**
254 * Dismiss the global actions dialog, if it's currently shown
255 */
256 public void dismissDialog() {
257 mHandler.removeMessages(MESSAGE_DISMISS);
258 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
259 }
260
Jason Monk361915c2017-03-21 20:33:59 -0400261 private void awakenIfNecessary() {
262 if (mDreamManager != null) {
263 try {
264 if (mDreamManager.isDreaming()) {
265 mDreamManager.awaken();
266 }
267 } catch (RemoteException e) {
268 // we tried
269 }
270 }
271 }
272
273 private void handleShow() {
274 awakenIfNecessary();
275 mDialog = createDialog();
276 prepareDialog();
277
278 // If we only have 1 item and it's a simple press action, just do this action.
279 if (mAdapter.getCount() == 1
280 && mAdapter.getItem(0) instanceof SinglePressAction
281 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
282 ((SinglePressAction) mAdapter.getItem(0)).onPress();
283 } else {
284 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
285 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100286 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400287 mDialog.getWindow().setAttributes(attrs);
288 mDialog.show();
289 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400290 }
291 }
292
293 /**
294 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400295 *
Jason Monk361915c2017-03-21 20:33:59 -0400296 * @return A new dialog.
297 */
298 private ActionsDialog createDialog() {
299 // Simple toggle style if there's no vibrator, otherwise use a tri-state
300 if (!mHasVibrator) {
301 mSilentModeAction = new SilentModeToggleAction();
302 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700303 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400304 }
305 mAirplaneModeOn = new ToggleAction(
306 R.drawable.ic_lock_airplane_mode,
307 R.drawable.ic_lock_airplane_mode_off,
308 R.string.global_actions_toggle_airplane_mode,
309 R.string.global_actions_airplane_mode_on_status,
310 R.string.global_actions_airplane_mode_off_status) {
311
312 void onToggle(boolean on) {
Inseob Kim5e82f732019-11-08 15:08:38 +0900313 if (mHasTelephony && TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400314 mIsWaitingForEcmExit = true;
315 // Launch ECM exit dialog
316 Intent ecmDialogIntent =
317 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
318 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
319 mContext.startActivity(ecmDialogIntent);
320 } else {
321 changeAirplaneModeSystemSetting(on);
322 }
323 }
324
325 @Override
326 protected void changeStateFromPress(boolean buttonOn) {
327 if (!mHasTelephony) return;
328
329 // In ECM mode airplane state cannot be changed
Inseob Kim5e82f732019-11-08 15:08:38 +0900330 if (!TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400331 mState = buttonOn ? State.TurningOn : State.TurningOff;
332 mAirplaneState = mState;
333 }
334 }
335
336 public boolean showDuringKeyguard() {
337 return true;
338 }
339
340 public boolean showBeforeProvisioning() {
341 return false;
342 }
343 };
344 onAirplaneModeChanged();
345
346 mItems = new ArrayList<Action>();
347 String[] defaultActions = mContext.getResources().getStringArray(
348 R.array.config_globalActionsList);
349
350 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000351 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800352 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400353 for (int i = 0; i < defaultActions.length; i++) {
354 String actionKey = defaultActions[i];
355 if (addedKeys.contains(actionKey)) {
356 // If we already have added this, don't add it again.
357 continue;
358 }
359 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
360 mItems.add(new PowerAction());
361 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
362 mItems.add(mAirplaneModeOn);
363 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
364 if (Settings.Global.getInt(mContext.getContentResolver(),
365 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
366 mItems.add(new BugReportAction());
367 }
368 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
369 if (mShowSilentToggle) {
370 mItems.add(mSilentModeAction);
371 }
372 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
373 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
374 addUsersToMenu(mItems);
375 }
376 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
377 mItems.add(getSettingsAction());
378 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700379 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
380 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800381 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700382 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800383 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700384 }
Jason Monk361915c2017-03-21 20:33:59 -0400385 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
386 mItems.add(getVoiceAssistAction());
387 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
388 mItems.add(getAssistAction());
389 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
390 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500391 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
392 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000393 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000394 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000395 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
396 mItems.add(new LogoutAction());
397 mHasLogoutButton = true;
398 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800399 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400400 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800401 mItems.add(new EmergencyDialerAction());
402 }
Jason Monk361915c2017-03-21 20:33:59 -0400403 } else {
404 Log.e(TAG, "Invalid global action key " + actionKey);
405 }
406 // Add here so we don't add more than one.
407 addedKeys.add(actionKey);
408 }
409
410 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400411 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400412 }
413
414 mAdapter = new MyAdapter();
415
Steve Elliott9b87a442019-03-05 10:24:16 -0500416 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400417 mPanelPlugin != null
418 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400419 new GlobalActionsPanelPlugin.Callbacks() {
420 @Override
421 public void dismissGlobalActionsMenu() {
422 if (mDialog != null) {
423 mDialog.dismiss();
424 }
425 }
426
427 @Override
428 public void startPendingIntentDismissingKeyguard(
429 PendingIntent intent) {
430 mActivityStarter
431 .startPendingIntentDismissingKeyguard(intent);
432 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400433 },
434 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500435 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400436
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400437 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400438 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700439 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400440
441 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800442 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400443
444 return dialog;
445 }
446
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800447 private boolean shouldDisplayLockdown() {
448 int userId = getCurrentUser().id;
449 // Lockdown is meaningless without a place to go.
450 if (!mKeyguardManager.isDeviceSecure(userId)) {
451 return false;
452 }
453
454 // Only show the lockdown button if the device isn't locked down (for whatever reason).
455 int state = mLockPatternUtils.getStrongAuthForUser(userId);
456 return (state == STRONG_AUTH_NOT_REQUIRED
457 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
458 }
459
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700460 @Override
461 public void onUiModeChanged() {
462 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700463 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400464 mDialog.refreshDialog();
465 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700466 }
467
468 public void destroy() {
469 Dependency.get(ConfigurationController.class).removeCallback(this);
470 }
471
Jason Monk361915c2017-03-21 20:33:59 -0400472 private final class PowerAction extends SinglePressAction implements LongPressAction {
473 private PowerAction() {
474 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400475 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400476 }
477
478 @Override
479 public boolean onLongPress() {
480 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
481 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
482 mWindowManagerFuncs.reboot(true);
483 return true;
484 }
485 return false;
486 }
487
488 @Override
489 public boolean showDuringKeyguard() {
490 return true;
491 }
492
493 @Override
494 public boolean showBeforeProvisioning() {
495 return true;
496 }
497
498 @Override
499 public void onPress() {
500 // shutdown by making sure radio and power are handled accordingly.
501 mWindowManagerFuncs.shutdown();
502 }
503 }
504
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400505 private abstract class EmergencyAction extends SinglePressAction {
506 EmergencyAction(int iconResId, int messageResId) {
507 super(iconResId, messageResId);
508 }
509
510 @Override
511 public boolean shouldBeSeparated() {
512 return shouldUseSeparatedView();
513 }
514
515 @Override
516 public View create(
517 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
518 View v = super.create(context, convertView, parent, inflater);
519 int textColor;
520 if (shouldBeSeparated()) {
521 textColor = v.getResources().getColor(
522 com.android.systemui.R.color.global_actions_alert_text);
523 } else {
524 textColor = v.getResources().getColor(
525 com.android.systemui.R.color.global_actions_text);
526 }
527 TextView messageView = v.findViewById(R.id.message);
528 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400529 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400530 ImageView icon = (ImageView) v.findViewById(R.id.icon);
531 icon.getDrawable().setTint(textColor);
532 return v;
533 }
534
535 @Override
536 public boolean showDuringKeyguard() {
537 return true;
538 }
539
540 @Override
541 public boolean showBeforeProvisioning() {
542 return true;
543 }
544 }
545
546 private class EmergencyAffordanceAction extends EmergencyAction {
547 EmergencyAffordanceAction() {
548 super(R.drawable.emergency_icon,
549 R.string.global_action_emergency);
550 }
551
552 @Override
553 public void onPress() {
554 mEmergencyAffordanceManager.performEmergencyCall();
555 }
556 }
557
558 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800559 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400560 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800561 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800562 }
563
564 @Override
565 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800566 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Fan Zhangf9914762019-11-01 15:58:38 -0700567 Intent intent = mContext.getSystemService(TelecomManager.class)
568 .createLaunchEmergencyDialerIntent(null /* number */);
Leo Hsue4cc3122019-03-06 15:25:28 +0800569 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
570 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
571 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800572 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
573 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800574 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
575 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800576 }
577
Jason Monk361915c2017-03-21 20:33:59 -0400578 private final class RestartAction extends SinglePressAction implements LongPressAction {
579 private RestartAction() {
580 super(R.drawable.ic_restart, R.string.global_action_restart);
581 }
582
583 @Override
584 public boolean onLongPress() {
585 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
586 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
587 mWindowManagerFuncs.reboot(true);
588 return true;
589 }
590 return false;
591 }
592
593 @Override
594 public boolean showDuringKeyguard() {
595 return true;
596 }
597
598 @Override
599 public boolean showBeforeProvisioning() {
600 return true;
601 }
602
603 @Override
604 public void onPress() {
605 mWindowManagerFuncs.reboot(false);
606 }
607 }
608
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400609 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500610 public ScreenshotAction() {
611 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
612 }
613
614 @Override
615 public void onPress() {
616 // Add a little delay before executing, to give the
617 // dialog a chance to go away before it takes a
618 // screenshot.
619 // TODO: instead, omit global action dialog layer
620 mHandler.postDelayed(new Runnable() {
621 @Override
622 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400623 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500624 MetricsLogger.action(mContext,
625 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
626 }
627 }, 500);
628 }
629
630 @Override
631 public boolean showDuringKeyguard() {
632 return true;
633 }
634
635 @Override
636 public boolean showBeforeProvisioning() {
637 return false;
638 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400639
640 @Override
641 public boolean onLongPress() {
642 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
643 mScreenRecordHelper.launchRecordPrompt();
644 } else {
645 onPress();
646 }
647 return true;
648 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500649 }
650
Jason Monk361915c2017-03-21 20:33:59 -0400651 private class BugReportAction extends SinglePressAction implements LongPressAction {
652
653 public BugReportAction() {
654 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
655 }
656
657 @Override
658 public void onPress() {
659 // don't actually trigger the bugreport if we are running stability
660 // tests via monkey
661 if (ActivityManager.isUserAMonkey()) {
662 return;
663 }
664 // Add a little delay before executing, to give the
665 // dialog a chance to go away before it takes a
666 // screenshot.
667 mHandler.postDelayed(new Runnable() {
668 @Override
669 public void run() {
670 try {
671 // Take an "interactive" bugreport.
672 MetricsLogger.action(mContext,
673 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100674 ActivityManager.getService().requestInteractiveBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400675 } catch (RemoteException e) {
676 }
677 }
678 }, 500);
679 }
680
681 @Override
682 public boolean onLongPress() {
683 // don't actually trigger the bugreport if we are running stability
684 // tests via monkey
685 if (ActivityManager.isUserAMonkey()) {
686 return false;
687 }
688 try {
689 // Take a "full" bugreport.
690 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100691 ActivityManager.getService().requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400692 } catch (RemoteException e) {
693 }
694 return false;
695 }
696
697 public boolean showDuringKeyguard() {
698 return true;
699 }
700
701 @Override
702 public boolean showBeforeProvisioning() {
703 return false;
704 }
Jason Monk361915c2017-03-21 20:33:59 -0400705 }
706
Alex Chau04458852017-11-27 18:21:23 +0000707 private final class LogoutAction extends SinglePressAction {
708 private LogoutAction() {
709 super(R.drawable.ic_logout, R.string.global_action_logout);
710 }
711
712 @Override
713 public boolean showDuringKeyguard() {
714 return true;
715 }
716
717 @Override
718 public boolean showBeforeProvisioning() {
719 return false;
720 }
721
722 @Override
723 public void onPress() {
724 // Add a little delay before executing, to give the dialog a chance to go away before
725 // switching user
726 mHandler.postDelayed(() -> {
727 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000728 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000729 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000730 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000731 } catch (RemoteException re) {
732 Log.e(TAG, "Couldn't logout user " + re);
733 }
734 }, 500);
735 }
736 }
737
Jason Monk361915c2017-03-21 20:33:59 -0400738 private Action getSettingsAction() {
739 return new SinglePressAction(R.drawable.ic_settings,
740 R.string.global_action_settings) {
741
742 @Override
743 public void onPress() {
744 Intent intent = new Intent(Settings.ACTION_SETTINGS);
745 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
746 mContext.startActivity(intent);
747 }
748
749 @Override
750 public boolean showDuringKeyguard() {
751 return true;
752 }
753
754 @Override
755 public boolean showBeforeProvisioning() {
756 return true;
757 }
758 };
759 }
760
Jason Monk361915c2017-03-21 20:33:59 -0400761 private Action getAssistAction() {
762 return new SinglePressAction(R.drawable.ic_action_assist_focused,
763 R.string.global_action_assist) {
764 @Override
765 public void onPress() {
766 Intent intent = new Intent(Intent.ACTION_ASSIST);
767 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
768 mContext.startActivity(intent);
769 }
770
771 @Override
772 public boolean showDuringKeyguard() {
773 return true;
774 }
775
776 @Override
777 public boolean showBeforeProvisioning() {
778 return true;
779 }
780 };
781 }
782
783 private Action getVoiceAssistAction() {
784 return new SinglePressAction(R.drawable.ic_voice_search,
785 R.string.global_action_voice_assist) {
786 @Override
787 public void onPress() {
788 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
789 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
790 mContext.startActivity(intent);
791 }
792
793 @Override
794 public boolean showDuringKeyguard() {
795 return true;
796 }
797
798 @Override
799 public boolean showBeforeProvisioning() {
800 return true;
801 }
802 };
803 }
804
805 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400806 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400807 R.string.global_action_lockdown) {
808
809 @Override
810 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700811 new LockPatternUtils(mContext)
812 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
813 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400814 try {
815 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100816 // Lock profiles (if any) on the background thread.
817 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
818 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400819 } catch (RemoteException e) {
820 Log.e(TAG, "Error while trying to lock device.", e);
821 }
822 }
823
824 @Override
825 public boolean showDuringKeyguard() {
826 return true;
827 }
828
829 @Override
830 public boolean showBeforeProvisioning() {
831 return false;
832 }
833 };
834 }
835
Pavel Grafov059021b2018-05-02 13:44:46 +0100836 private void lockProfiles() {
837 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
838 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
839 final int currentUserId = getCurrentUser().id;
840 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
841 for (final int id : profileIds) {
842 if (id != currentUserId) {
843 tm.setDeviceLockedForUser(id, true);
844 }
845 }
846 }
847
Jason Monk361915c2017-03-21 20:33:59 -0400848 private UserInfo getCurrentUser() {
849 try {
850 return ActivityManager.getService().getCurrentUser();
851 } catch (RemoteException re) {
852 return null;
853 }
854 }
855
856 private boolean isCurrentUserOwner() {
857 UserInfo currentUser = getCurrentUser();
858 return currentUser == null || currentUser.isPrimary();
859 }
860
861 private void addUsersToMenu(ArrayList<Action> items) {
862 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
863 if (um.isUserSwitcherEnabled()) {
864 List<UserInfo> users = um.getUsers();
865 UserInfo currentUser = getCurrentUser();
866 for (final UserInfo user : users) {
867 if (user.supportsSwitchToByUser()) {
868 boolean isCurrentUser = currentUser == null
869 ? user.id == 0 : (currentUser.id == user.id);
870 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
871 : null;
872 SinglePressAction switchToUser = new SinglePressAction(
873 R.drawable.ic_menu_cc, icon,
874 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400875 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400876 public void onPress() {
877 try {
878 ActivityManager.getService().switchUser(user.id);
879 } catch (RemoteException re) {
880 Log.e(TAG, "Couldn't switch user " + re);
881 }
882 }
883
884 public boolean showDuringKeyguard() {
885 return true;
886 }
887
888 public boolean showBeforeProvisioning() {
889 return false;
890 }
891 };
892 items.add(switchToUser);
893 }
894 }
895 }
896 }
897
898 private void prepareDialog() {
899 refreshSilentMode();
900 mAirplaneModeOn.updateState(mAirplaneState);
901 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400902 if (mShowSilentToggle) {
903 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000904 mBroadcastDispatcher.registerReceiver(mRingerModeReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400905 }
906 }
907
908 private void refreshSilentMode() {
909 if (!mHasVibrator) {
910 final boolean silentModeOn =
911 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400912 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400913 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
914 }
915 }
916
917 /** {@inheritDoc} */
918 public void onDismiss(DialogInterface dialog) {
919 mWindowManagerFuncs.onGlobalActionsHidden();
920 if (mShowSilentToggle) {
921 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000922 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400923 } catch (IllegalArgumentException ie) {
924 // ignore this
925 Log.w(TAG, ie);
926 }
927 }
928 }
929
930 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800931 public void onShow(DialogInterface dialog) {
932 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
933 }
934
Jason Monk361915c2017-03-21 20:33:59 -0400935 /**
936 * The adapter used for the list within the global actions dialog, taking
937 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400938 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
939 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400940 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
941 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500942 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400943 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400944 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400945 for (int i = 0; i < mItems.size(); i++) {
946 final Action action = mItems.get(i);
947
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400948 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
949 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400950 }
Jason Monk361915c2017-03-21 20:33:59 -0400951 }
952 return count;
953 }
954
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400955 private boolean shouldBeShown(Action action) {
956 if (mKeyguardShowing && !action.showDuringKeyguard()) {
957 return false;
958 }
959 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
960 return false;
961 }
962 return true;
963 }
964
965 @Override
966 public int countSeparatedItems() {
967 return countItems(true);
968 }
969
970 @Override
971 public int countListItems() {
972 return countItems(false);
973 }
974
975 @Override
976 public int getCount() {
977 return countSeparatedItems() + countListItems();
978 }
979
Jason Monk361915c2017-03-21 20:33:59 -0400980 @Override
981 public boolean isEnabled(int position) {
982 return getItem(position).isEnabled();
983 }
984
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500985 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400986 public boolean areAllItemsEnabled() {
987 return false;
988 }
989
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500990 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400991 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400992 int filteredPos = 0;
993 for (int i = 0; i < mItems.size(); i++) {
994 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400995 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400996 continue;
997 }
998 if (filteredPos == position) {
999 return action;
1000 }
1001 filteredPos++;
1002 }
1003
1004 throw new IllegalArgumentException("position " + position
1005 + " out of range of showable actions"
1006 + ", filtered count=" + getCount()
1007 + ", keyguardshowing=" + mKeyguardShowing
1008 + ", provisioned=" + mDeviceProvisioned);
1009 }
1010
1011
1012 public long getItemId(int position) {
1013 return position;
1014 }
1015
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001016 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001017 public View getView(int position, View convertView, ViewGroup parent) {
1018 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001019 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001020 view.setOnClickListener(v -> onClickItem(position));
1021 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001022 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001023 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001024
1025 @Override
1026 public boolean onLongClickItem(int position) {
1027 final Action action = mAdapter.getItem(position);
1028 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001029 if (mDialog != null) {
1030 mDialog.dismiss();
1031 } else {
1032 Log.w(TAG, "Action long-clicked while mDialog is null.");
1033 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001034 return ((LongPressAction) action).onLongPress();
1035 }
1036 return false;
1037 }
1038
1039 @Override
1040 public void onClickItem(int position) {
1041 Action item = mAdapter.getItem(position);
1042 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001043 if (mDialog != null) {
1044 mDialog.dismiss();
1045 } else {
1046 Log.w(TAG, "Action clicked while mDialog is null.");
1047 }
1048 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001049 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001050 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001051
1052 @Override
1053 public boolean shouldBeSeparated(int position) {
1054 return getItem(position).shouldBeSeparated();
1055 }
Jason Monk361915c2017-03-21 20:33:59 -04001056 }
1057
1058 // note: the scheme below made more sense when we were planning on having
1059 // 8 different things in the global actions dialog. seems overkill with
1060 // only 3 items now, but may as well keep this flexible approach so it will
1061 // be easy should someone decide at the last minute to include something
1062 // else, such as 'enable wifi', or 'enable bluetooth'
1063
1064 /**
1065 * What each item in the global actions dialog must be able to support.
1066 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001067 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001068 /**
1069 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001070 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001071 */
1072 CharSequence getLabelForAccessibility(Context context);
1073
1074 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1075
1076 void onPress();
1077
1078 /**
1079 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001080 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001081 */
1082 boolean showDuringKeyguard();
1083
1084 /**
1085 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001086 * device is provisioned.onlongpress
1087 *
Jason Monk361915c2017-03-21 20:33:59 -04001088 */
1089 boolean showBeforeProvisioning();
1090
1091 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001092
1093 default boolean shouldBeSeparated() {
1094 return false;
1095 }
Jason Monk361915c2017-03-21 20:33:59 -04001096 }
1097
1098 /**
1099 * An action that also supports long press.
1100 */
1101 private interface LongPressAction extends Action {
1102 boolean onLongPress();
1103 }
1104
1105 /**
1106 * A single press action maintains no state, just responds to a press
1107 * and takes an action.
1108 */
1109 private static abstract class SinglePressAction implements Action {
1110 private final int mIconResId;
1111 private final Drawable mIcon;
1112 private final int mMessageResId;
1113 private final CharSequence mMessage;
1114
1115 protected SinglePressAction(int iconResId, int messageResId) {
1116 mIconResId = iconResId;
1117 mMessageResId = messageResId;
1118 mMessage = null;
1119 mIcon = null;
1120 }
1121
1122 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1123 mIconResId = iconResId;
1124 mMessageResId = 0;
1125 mMessage = message;
1126 mIcon = icon;
1127 }
1128
1129 public boolean isEnabled() {
1130 return true;
1131 }
1132
1133 public String getStatus() {
1134 return null;
1135 }
1136
1137 abstract public void onPress();
1138
1139 public CharSequence getLabelForAccessibility(Context context) {
1140 if (mMessage != null) {
1141 return mMessage;
1142 } else {
1143 return context.getString(mMessageResId);
1144 }
1145 }
1146
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001147 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001148 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001149 }
1150
Jason Monk361915c2017-03-21 20:33:59 -04001151 public View create(
1152 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001153 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001154 false);
Jason Monk361915c2017-03-21 20:33:59 -04001155
1156 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1157 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001158 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001159
1160 TextView statusView = (TextView) v.findViewById(R.id.status);
1161 final String status = getStatus();
1162 if (!TextUtils.isEmpty(status)) {
1163 statusView.setText(status);
1164 } else {
1165 statusView.setVisibility(View.GONE);
1166 }
1167 if (mIcon != null) {
1168 icon.setImageDrawable(mIcon);
1169 icon.setScaleType(ScaleType.CENTER_CROP);
1170 } else if (mIconResId != 0) {
1171 icon.setImageDrawable(context.getDrawable(mIconResId));
1172 }
1173 if (mMessage != null) {
1174 messageView.setText(mMessage);
1175 } else {
1176 messageView.setText(mMessageResId);
1177 }
1178
1179 return v;
1180 }
1181 }
1182
1183 /**
1184 * A toggle action knows whether it is on or off, and displays an icon
1185 * and status message accordingly.
1186 */
1187 private static abstract class ToggleAction implements Action {
1188
1189 enum State {
1190 Off(false),
1191 TurningOn(true),
1192 TurningOff(true),
1193 On(false);
1194
1195 private final boolean inTransition;
1196
1197 State(boolean intermediate) {
1198 inTransition = intermediate;
1199 }
1200
1201 public boolean inTransition() {
1202 return inTransition;
1203 }
1204 }
1205
1206 protected State mState = State.Off;
1207
1208 // prefs
1209 protected int mEnabledIconResId;
1210 protected int mDisabledIconResid;
1211 protected int mMessageResId;
1212 protected int mEnabledStatusMessageResId;
1213 protected int mDisabledStatusMessageResId;
1214
1215 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001216 * @param enabledIconResId The icon for when this action is on.
1217 * @param disabledIconResid The icon for when this action is off.
1218 * @param message The general information message, e.g 'Silent Mode'
1219 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001220 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1221 */
1222 public ToggleAction(int enabledIconResId,
1223 int disabledIconResid,
1224 int message,
1225 int enabledStatusMessageResId,
1226 int disabledStatusMessageResId) {
1227 mEnabledIconResId = enabledIconResId;
1228 mDisabledIconResid = disabledIconResid;
1229 mMessageResId = message;
1230 mEnabledStatusMessageResId = enabledStatusMessageResId;
1231 mDisabledStatusMessageResId = disabledStatusMessageResId;
1232 }
1233
1234 /**
1235 * Override to make changes to resource IDs just before creating the
1236 * View.
1237 */
1238 void willCreate() {
1239
1240 }
1241
1242 @Override
1243 public CharSequence getLabelForAccessibility(Context context) {
1244 return context.getString(mMessageResId);
1245 }
1246
1247 public View create(Context context, View convertView, ViewGroup parent,
1248 LayoutInflater inflater) {
1249 willCreate();
1250
1251 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001252 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001253
1254 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1255 TextView messageView = (TextView) v.findViewById(R.id.message);
1256 TextView statusView = (TextView) v.findViewById(R.id.status);
1257 final boolean enabled = isEnabled();
1258
1259 if (messageView != null) {
1260 messageView.setText(mMessageResId);
1261 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001262 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001263 }
1264
1265 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1266 if (icon != null) {
1267 icon.setImageDrawable(context.getDrawable(
1268 (on ? mEnabledIconResId : mDisabledIconResid)));
1269 icon.setEnabled(enabled);
1270 }
1271
1272 if (statusView != null) {
1273 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1274 statusView.setVisibility(View.VISIBLE);
1275 statusView.setEnabled(enabled);
1276 }
1277 v.setEnabled(enabled);
1278
1279 return v;
1280 }
1281
1282 public final void onPress() {
1283 if (mState.inTransition()) {
1284 Log.w(TAG, "shouldn't be able to toggle when in transition");
1285 return;
1286 }
1287
1288 final boolean nowOn = !(mState == State.On);
1289 onToggle(nowOn);
1290 changeStateFromPress(nowOn);
1291 }
1292
1293 public boolean isEnabled() {
1294 return !mState.inTransition();
1295 }
1296
1297 /**
1298 * Implementations may override this if their state can be in on of the intermediate
1299 * states until some notification is received (e.g airplane mode is 'turning off' until
1300 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001301 *
Jason Monk361915c2017-03-21 20:33:59 -04001302 * @param buttonOn Whether the button was turned on or off
1303 */
1304 protected void changeStateFromPress(boolean buttonOn) {
1305 mState = buttonOn ? State.On : State.Off;
1306 }
1307
1308 abstract void onToggle(boolean on);
1309
1310 public void updateState(State state) {
1311 mState = state;
1312 }
1313 }
1314
1315 private class SilentModeToggleAction extends ToggleAction {
1316 public SilentModeToggleAction() {
1317 super(R.drawable.ic_audio_vol_mute,
1318 R.drawable.ic_audio_vol,
1319 R.string.global_action_toggle_silent_mode,
1320 R.string.global_action_silent_mode_on_status,
1321 R.string.global_action_silent_mode_off_status);
1322 }
1323
1324 void onToggle(boolean on) {
1325 if (on) {
1326 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1327 } else {
1328 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1329 }
1330 }
1331
1332 public boolean showDuringKeyguard() {
1333 return true;
1334 }
1335
1336 public boolean showBeforeProvisioning() {
1337 return false;
1338 }
1339 }
1340
1341 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1342
Jason Monk16fbd9d2017-04-27 14:28:49 -04001343 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001344
1345 private final AudioManager mAudioManager;
1346 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001347
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001348 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001349 mAudioManager = audioManager;
1350 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001351 }
1352
1353 private int ringerModeToIndex(int ringerMode) {
1354 // They just happen to coincide
1355 return ringerMode;
1356 }
1357
1358 private int indexToRingerMode(int index) {
1359 // They just happen to coincide
1360 return index;
1361 }
1362
1363 @Override
1364 public CharSequence getLabelForAccessibility(Context context) {
1365 return null;
1366 }
1367
1368 public View create(Context context, View convertView, ViewGroup parent,
1369 LayoutInflater inflater) {
1370 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1371
1372 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1373 for (int i = 0; i < 3; i++) {
1374 View itemView = v.findViewById(ITEM_IDS[i]);
1375 itemView.setSelected(selectedIndex == i);
1376 // Set up click handler
1377 itemView.setTag(i);
1378 itemView.setOnClickListener(this);
1379 }
1380 return v;
1381 }
1382
1383 public void onPress() {
1384 }
1385
1386 public boolean showDuringKeyguard() {
1387 return true;
1388 }
1389
1390 public boolean showBeforeProvisioning() {
1391 return false;
1392 }
1393
1394 public boolean isEnabled() {
1395 return true;
1396 }
1397
1398 void willCreate() {
1399 }
1400
1401 public void onClick(View v) {
1402 if (!(v.getTag() instanceof Integer)) return;
1403
1404 int index = (Integer) v.getTag();
1405 mAudioManager.setRingerMode(indexToRingerMode(index));
1406 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1407 }
1408 }
1409
1410 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1411 public void onReceive(Context context, Intent intent) {
1412 String action = intent.getAction();
1413 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1414 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1415 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1416 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001417 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001418 }
1419 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1420 // Airplane mode can be changed after ECM exits if airplane toggle button
1421 // is pressed during ECM mode
1422 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1423 mIsWaitingForEcmExit) {
1424 mIsWaitingForEcmExit = false;
1425 changeAirplaneModeSystemSetting(true);
1426 }
1427 }
1428 }
1429 };
1430
1431 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1432 @Override
1433 public void onServiceStateChanged(ServiceState serviceState) {
1434 if (!mHasTelephony) return;
1435 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1436 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1437 mAirplaneModeOn.updateState(mAirplaneState);
1438 mAdapter.notifyDataSetChanged();
1439 }
1440 };
1441
1442 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1443 @Override
1444 public void onReceive(Context context, Intent intent) {
1445 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1446 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1447 }
1448 }
1449 };
1450
1451 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1452 @Override
1453 public void onChange(boolean selfChange) {
1454 onAirplaneModeChanged();
1455 }
1456 };
1457
1458 private static final int MESSAGE_DISMISS = 0;
1459 private static final int MESSAGE_REFRESH = 1;
1460 private static final int MESSAGE_SHOW = 2;
1461 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1462
1463 private Handler mHandler = new Handler() {
1464 public void handleMessage(Message msg) {
1465 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001466 case MESSAGE_DISMISS:
1467 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001468 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1469 mDialog.dismissImmediately();
1470 } else {
1471 mDialog.dismiss();
1472 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001473 mDialog = null;
1474 }
1475 break;
1476 case MESSAGE_REFRESH:
1477 refreshSilentMode();
1478 mAdapter.notifyDataSetChanged();
1479 break;
1480 case MESSAGE_SHOW:
1481 handleShow();
1482 break;
Jason Monk361915c2017-03-21 20:33:59 -04001483 }
1484 }
1485 };
1486
1487 private void onAirplaneModeChanged() {
1488 // Let the service state callbacks handle the state.
1489 if (mHasTelephony) return;
1490
1491 boolean airplaneModeOn = Settings.Global.getInt(
1492 mContext.getContentResolver(),
1493 Settings.Global.AIRPLANE_MODE_ON,
1494 0) == 1;
1495 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1496 mAirplaneModeOn.updateState(mAirplaneState);
1497 }
1498
1499 /**
1500 * Change the airplane mode system setting
1501 */
1502 private void changeAirplaneModeSystemSetting(boolean on) {
1503 Settings.Global.putInt(
1504 mContext.getContentResolver(),
1505 Settings.Global.AIRPLANE_MODE_ON,
1506 on ? 1 : 0);
1507 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1508 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1509 intent.putExtra("state", on);
1510 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1511 if (!mHasTelephony) {
1512 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1513 }
1514 }
1515
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001516 private static final class ActionsDialog extends Dialog implements DialogInterface,
1517 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001518
Jason Monk16fbd9d2017-04-27 14:28:49 -04001519 private final Context mContext;
1520 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001521 private final IStatusBarService mStatusBarService;
1522 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001523 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001524 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001525 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001526 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001527 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001528 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001529 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001530 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001531 private boolean mHadTopUi;
1532 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001533
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001534 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001535 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001536 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001537 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001538 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001539 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001540 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001541 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001542
1543 // Window initialization
1544 Window window = getWindow();
1545 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001546 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1547 window.getDecorView();
1548 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1549 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1550 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1551 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1552 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001553 window.addFlags(
1554 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001555 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001556 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001557 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1558 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1559 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001560 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001561 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001562
1563 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001564 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001565 }
1566
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001567 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001568 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001569 }
1570
1571 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001572 int rotation = RotationUtils.getRotation(mContext);
1573 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1574 if (rotation != RotationUtils.ROTATION_NONE) {
1575 if (rotationLocked) {
1576 if (mResetOrientationData == null) {
1577 mResetOrientationData = new ResetOrientationData();
1578 mResetOrientationData.locked = true;
1579 mResetOrientationData.rotation = rotation;
1580 }
1581
1582 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001583 // This call is posted so that the rotation does not change until post-layout,
1584 // otherwise onConfigurationChanged() may not get invoked.
1585 mGlobalActionsLayout.post(() ->
1586 RotationPolicy.setRotationLockAtAngle(
1587 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001588 }
1589 } else {
1590 if (!rotationLocked) {
1591 if (mResetOrientationData == null) {
1592 mResetOrientationData = new ResetOrientationData();
1593 mResetOrientationData.locked = false;
1594 }
1595
1596 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001597 // This call is posted so that the rotation does not change until post-layout,
1598 // otherwise onConfigurationChanged() may not get invoked.
1599 mGlobalActionsLayout.post(() ->
1600 RotationPolicy.setRotationLockAtAngle(
1601 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001602 }
1603
Steve Elliott53f12ae2019-05-13 17:14:15 -04001604 // Disable rotation suggestions, if enabled
1605 setRotationSuggestionsEnabled(false);
1606
Steve Elliott300b48f2019-05-29 14:13:50 -04001607 FrameLayout panelContainer =
1608 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001609 FrameLayout.LayoutParams panelParams =
1610 new FrameLayout.LayoutParams(
1611 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001612 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001613 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001614 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1615 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001616 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001617 }
1618
1619 private void initializeLayout() {
1620 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001621 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001622 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001623 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001624 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001625 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001626 @Override
1627 public boolean dispatchPopulateAccessibilityEvent(
1628 View host, AccessibilityEvent event) {
1629 // Populate the title here, just as Activity does
1630 event.getText().add(mContext.getString(R.string.global_actions));
1631 return true;
1632 }
1633 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001634 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001635 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001636
Steve Elliott86ef6282019-05-08 14:45:04 -04001637 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001638 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001639 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001640 if (mBackgroundDrawable == null) {
1641 mBackgroundDrawable = new ScrimDrawable();
1642 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1643 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001644 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001645 }
1646
Steve Elliott300b48f2019-05-29 14:13:50 -04001647 private void fixNavBarClipping() {
1648 ViewGroup content = findViewById(android.R.id.content);
1649 content.setClipChildren(false);
1650 content.setClipToPadding(false);
1651 ViewGroup contentParent = (ViewGroup) content.getParent();
1652 contentParent.setClipChildren(false);
1653 contentParent.setClipToPadding(false);
1654 }
1655
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001656 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001657 int rotation = RotationUtils.getRotation(context);
1658 boolean useGridLayout = isForceGridEnabled(context)
1659 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1660 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001661 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001662 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001663 } else {
1664 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001665 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001666 } else {
1667 if (useGridLayout) {
1668 return com.android.systemui.R.layout.global_actions_grid;
1669 } else {
1670 return com.android.systemui.R.layout.global_actions_column;
1671 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001672 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001673 }
1674
Jason Monk361915c2017-03-21 20:33:59 -04001675 @Override
1676 protected void onStart() {
1677 super.setCanceledOnTouchOutside(true);
1678 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001679 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001680
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001681 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001682 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001683 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001684 updateColors(colors, false /* animate */);
1685 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001686 }
1687
1688 /**
1689 * Updates background and system bars according to current GradientColors.
1690 * @param colors Colors and hints to use.
1691 * @param animate Interpolates gradient if true, just sets otherwise.
1692 */
1693 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001694 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001695 return;
1696 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001697 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001698 View decorView = getWindow().getDecorView();
1699 if (colors.supportsDarkText()) {
1700 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001701 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001702 } else {
1703 decorView.setSystemUiVisibility(0);
1704 }
Jason Monk361915c2017-03-21 20:33:59 -04001705 }
1706
1707 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001708 protected void onStop() {
1709 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001710 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001711 }
1712
1713 @Override
1714 public void show() {
1715 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001716 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001717 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1718 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001719 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001720 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1721 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001722 mGlobalActionsLayout.setAlpha(0);
1723 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001724 .alpha(1)
1725 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001726 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001727 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001728 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001729 .setUpdateListener(animation -> {
1730 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001731 * mScrimAlpha * 255);
1732 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001733 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001734 .start();
1735 }
1736
1737 @Override
1738 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001739 if (!mShowing) {
1740 return;
1741 }
1742 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001743 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001744 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001745 mGlobalActionsLayout.setAlpha(1);
1746 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001747 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001748 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1749 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001750 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001751 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001752 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001753 .setUpdateListener(animation -> {
1754 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001755 * mScrimAlpha * 255);
1756 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001757 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001758 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001759 dismissPanel();
1760 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001761 }
1762
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001763 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001764 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001765 dismissPanel();
1766 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001767 completeDismiss();
1768 }
1769
1770 private void completeDismiss() {
1771 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1772 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001773 }
1774
1775 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001776 if (mPanelController != null) {
1777 mPanelController.onDismissed();
1778 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001779 }
1780
Steve Elliott53f12ae2019-05-13 17:14:15 -04001781 private void setRotationSuggestionsEnabled(boolean enabled) {
1782 try {
1783 final int userId = Binder.getCallingUserHandle().getIdentifier();
1784 final int what = enabled
1785 ? StatusBarManager.DISABLE2_NONE
1786 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1787 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1788 } catch (RemoteException ex) {
1789 throw ex.rethrowFromSystemServer();
1790 }
1791 }
1792
Steve Elliott48f75db2019-05-03 15:03:38 -04001793 private void resetOrientation() {
1794 if (mResetOrientationData != null) {
1795 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1796 mResetOrientationData.rotation);
1797 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001798 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001799 }
1800
Jason Monk361915c2017-03-21 20:33:59 -04001801 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001802 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001803 if (mKeyguardShowing) {
1804 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001805 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1806 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001807 }
1808 } else {
1809 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001810 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1811 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001812 }
1813 }
1814 }
1815
1816 public void setKeyguardShowing(boolean keyguardShowing) {
1817 mKeyguardShowing = keyguardShowing;
1818 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001819
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001820 public void refreshDialog() {
1821 initializeLayout();
1822 mGlobalActionsLayout.updateList();
1823 }
1824
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001825 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001826 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001827 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001828 }
1829 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001830
1831 private static class ResetOrientationData {
1832 public boolean locked;
1833 public int rotation;
1834 }
Jason Monk361915c2017-03-21 20:33:59 -04001835 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001836
1837 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001838 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1839 */
1840 private static boolean isPanelDebugModeEnabled(Context context) {
1841 return Settings.Secure.getInt(context.getContentResolver(),
1842 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1843 }
1844
1845 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001846 * Determines whether or not the Global Actions menu should be forced to
1847 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001848 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001849 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001850 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001851 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001852
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001853
1854 /**
1855 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1856 */
1857 private static boolean shouldUseSeparatedView() {
1858 return true;
1859 }
Jason Monk361915c2017-03-21 20:33:59 -04001860}