blob: b58459470f3f157b0de1873156f64ec70e89900f [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() {
Steve Elliottc3147e22019-11-20 19:34:57 -0500422 dismissDialog();
Steve Elliott4c868852019-03-14 16:25:41 -0400423 }
424
425 @Override
426 public void startPendingIntentDismissingKeyguard(
427 PendingIntent intent) {
428 mActivityStarter
429 .startPendingIntentDismissingKeyguard(intent);
430 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400431 },
432 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500433 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400434
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400435 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400436 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700437 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400438
439 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800440 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400441
442 return dialog;
443 }
444
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800445 private boolean shouldDisplayLockdown() {
446 int userId = getCurrentUser().id;
447 // Lockdown is meaningless without a place to go.
448 if (!mKeyguardManager.isDeviceSecure(userId)) {
449 return false;
450 }
451
452 // Only show the lockdown button if the device isn't locked down (for whatever reason).
453 int state = mLockPatternUtils.getStrongAuthForUser(userId);
454 return (state == STRONG_AUTH_NOT_REQUIRED
455 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
456 }
457
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700458 @Override
459 public void onUiModeChanged() {
460 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700461 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400462 mDialog.refreshDialog();
463 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700464 }
465
466 public void destroy() {
467 Dependency.get(ConfigurationController.class).removeCallback(this);
468 }
469
Jason Monk361915c2017-03-21 20:33:59 -0400470 private final class PowerAction extends SinglePressAction implements LongPressAction {
471 private PowerAction() {
472 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400473 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400474 }
475
476 @Override
477 public boolean onLongPress() {
478 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
479 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
480 mWindowManagerFuncs.reboot(true);
481 return true;
482 }
483 return false;
484 }
485
486 @Override
487 public boolean showDuringKeyguard() {
488 return true;
489 }
490
491 @Override
492 public boolean showBeforeProvisioning() {
493 return true;
494 }
495
496 @Override
497 public void onPress() {
498 // shutdown by making sure radio and power are handled accordingly.
499 mWindowManagerFuncs.shutdown();
500 }
501 }
502
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400503 private abstract class EmergencyAction extends SinglePressAction {
504 EmergencyAction(int iconResId, int messageResId) {
505 super(iconResId, messageResId);
506 }
507
508 @Override
509 public boolean shouldBeSeparated() {
510 return shouldUseSeparatedView();
511 }
512
513 @Override
514 public View create(
515 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
516 View v = super.create(context, convertView, parent, inflater);
517 int textColor;
518 if (shouldBeSeparated()) {
519 textColor = v.getResources().getColor(
520 com.android.systemui.R.color.global_actions_alert_text);
521 } else {
522 textColor = v.getResources().getColor(
523 com.android.systemui.R.color.global_actions_text);
524 }
525 TextView messageView = v.findViewById(R.id.message);
526 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400527 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400528 ImageView icon = (ImageView) v.findViewById(R.id.icon);
529 icon.getDrawable().setTint(textColor);
530 return v;
531 }
532
533 @Override
534 public boolean showDuringKeyguard() {
535 return true;
536 }
537
538 @Override
539 public boolean showBeforeProvisioning() {
540 return true;
541 }
542 }
543
544 private class EmergencyAffordanceAction extends EmergencyAction {
545 EmergencyAffordanceAction() {
546 super(R.drawable.emergency_icon,
547 R.string.global_action_emergency);
548 }
549
550 @Override
551 public void onPress() {
552 mEmergencyAffordanceManager.performEmergencyCall();
553 }
554 }
555
556 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800557 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400558 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800559 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800560 }
561
562 @Override
563 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800564 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Fan Zhangf9914762019-11-01 15:58:38 -0700565 Intent intent = mContext.getSystemService(TelecomManager.class)
566 .createLaunchEmergencyDialerIntent(null /* number */);
Leo Hsue4cc3122019-03-06 15:25:28 +0800567 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
568 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
569 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800570 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
571 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800572 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
573 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800574 }
575
Jason Monk361915c2017-03-21 20:33:59 -0400576 private final class RestartAction extends SinglePressAction implements LongPressAction {
577 private RestartAction() {
578 super(R.drawable.ic_restart, R.string.global_action_restart);
579 }
580
581 @Override
582 public boolean onLongPress() {
583 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
584 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
585 mWindowManagerFuncs.reboot(true);
586 return true;
587 }
588 return false;
589 }
590
591 @Override
592 public boolean showDuringKeyguard() {
593 return true;
594 }
595
596 @Override
597 public boolean showBeforeProvisioning() {
598 return true;
599 }
600
601 @Override
602 public void onPress() {
603 mWindowManagerFuncs.reboot(false);
604 }
605 }
606
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400607 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500608 public ScreenshotAction() {
609 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
610 }
611
612 @Override
613 public void onPress() {
614 // Add a little delay before executing, to give the
615 // dialog a chance to go away before it takes a
616 // screenshot.
617 // TODO: instead, omit global action dialog layer
618 mHandler.postDelayed(new Runnable() {
619 @Override
620 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400621 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500622 MetricsLogger.action(mContext,
623 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
624 }
625 }, 500);
626 }
627
628 @Override
629 public boolean showDuringKeyguard() {
630 return true;
631 }
632
633 @Override
634 public boolean showBeforeProvisioning() {
635 return false;
636 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400637
638 @Override
639 public boolean onLongPress() {
640 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
641 mScreenRecordHelper.launchRecordPrompt();
642 } else {
643 onPress();
644 }
645 return true;
646 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500647 }
648
Jason Monk361915c2017-03-21 20:33:59 -0400649 private class BugReportAction extends SinglePressAction implements LongPressAction {
650
651 public BugReportAction() {
652 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
653 }
654
655 @Override
656 public void onPress() {
657 // don't actually trigger the bugreport if we are running stability
658 // tests via monkey
659 if (ActivityManager.isUserAMonkey()) {
660 return;
661 }
662 // Add a little delay before executing, to give the
663 // dialog a chance to go away before it takes a
664 // screenshot.
665 mHandler.postDelayed(new Runnable() {
666 @Override
667 public void run() {
668 try {
669 // Take an "interactive" bugreport.
670 MetricsLogger.action(mContext,
671 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100672 ActivityManager.getService().requestInteractiveBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400673 } catch (RemoteException e) {
674 }
675 }
676 }, 500);
677 }
678
679 @Override
680 public boolean onLongPress() {
681 // don't actually trigger the bugreport if we are running stability
682 // tests via monkey
683 if (ActivityManager.isUserAMonkey()) {
684 return false;
685 }
686 try {
687 // Take a "full" bugreport.
688 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
Abhijeet Kaur16207e32019-08-22 15:05:38 +0100689 ActivityManager.getService().requestFullBugReport();
Jason Monk361915c2017-03-21 20:33:59 -0400690 } catch (RemoteException e) {
691 }
692 return false;
693 }
694
695 public boolean showDuringKeyguard() {
696 return true;
697 }
698
699 @Override
700 public boolean showBeforeProvisioning() {
701 return false;
702 }
Jason Monk361915c2017-03-21 20:33:59 -0400703 }
704
Alex Chau04458852017-11-27 18:21:23 +0000705 private final class LogoutAction extends SinglePressAction {
706 private LogoutAction() {
707 super(R.drawable.ic_logout, R.string.global_action_logout);
708 }
709
710 @Override
711 public boolean showDuringKeyguard() {
712 return true;
713 }
714
715 @Override
716 public boolean showBeforeProvisioning() {
717 return false;
718 }
719
720 @Override
721 public void onPress() {
722 // Add a little delay before executing, to give the dialog a chance to go away before
723 // switching user
724 mHandler.postDelayed(() -> {
725 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000726 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000727 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000728 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000729 } catch (RemoteException re) {
730 Log.e(TAG, "Couldn't logout user " + re);
731 }
732 }, 500);
733 }
734 }
735
Jason Monk361915c2017-03-21 20:33:59 -0400736 private Action getSettingsAction() {
737 return new SinglePressAction(R.drawable.ic_settings,
738 R.string.global_action_settings) {
739
740 @Override
741 public void onPress() {
742 Intent intent = new Intent(Settings.ACTION_SETTINGS);
743 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
744 mContext.startActivity(intent);
745 }
746
747 @Override
748 public boolean showDuringKeyguard() {
749 return true;
750 }
751
752 @Override
753 public boolean showBeforeProvisioning() {
754 return true;
755 }
756 };
757 }
758
Jason Monk361915c2017-03-21 20:33:59 -0400759 private Action getAssistAction() {
760 return new SinglePressAction(R.drawable.ic_action_assist_focused,
761 R.string.global_action_assist) {
762 @Override
763 public void onPress() {
764 Intent intent = new Intent(Intent.ACTION_ASSIST);
765 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
766 mContext.startActivity(intent);
767 }
768
769 @Override
770 public boolean showDuringKeyguard() {
771 return true;
772 }
773
774 @Override
775 public boolean showBeforeProvisioning() {
776 return true;
777 }
778 };
779 }
780
781 private Action getVoiceAssistAction() {
782 return new SinglePressAction(R.drawable.ic_voice_search,
783 R.string.global_action_voice_assist) {
784 @Override
785 public void onPress() {
786 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
787 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
788 mContext.startActivity(intent);
789 }
790
791 @Override
792 public boolean showDuringKeyguard() {
793 return true;
794 }
795
796 @Override
797 public boolean showBeforeProvisioning() {
798 return true;
799 }
800 };
801 }
802
803 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400804 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400805 R.string.global_action_lockdown) {
806
807 @Override
808 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700809 new LockPatternUtils(mContext)
810 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
811 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400812 try {
813 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100814 // Lock profiles (if any) on the background thread.
815 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
816 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400817 } catch (RemoteException e) {
818 Log.e(TAG, "Error while trying to lock device.", e);
819 }
820 }
821
822 @Override
823 public boolean showDuringKeyguard() {
824 return true;
825 }
826
827 @Override
828 public boolean showBeforeProvisioning() {
829 return false;
830 }
831 };
832 }
833
Pavel Grafov059021b2018-05-02 13:44:46 +0100834 private void lockProfiles() {
835 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
836 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
837 final int currentUserId = getCurrentUser().id;
838 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
839 for (final int id : profileIds) {
840 if (id != currentUserId) {
841 tm.setDeviceLockedForUser(id, true);
842 }
843 }
844 }
845
Jason Monk361915c2017-03-21 20:33:59 -0400846 private UserInfo getCurrentUser() {
847 try {
848 return ActivityManager.getService().getCurrentUser();
849 } catch (RemoteException re) {
850 return null;
851 }
852 }
853
854 private boolean isCurrentUserOwner() {
855 UserInfo currentUser = getCurrentUser();
856 return currentUser == null || currentUser.isPrimary();
857 }
858
859 private void addUsersToMenu(ArrayList<Action> items) {
860 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
861 if (um.isUserSwitcherEnabled()) {
862 List<UserInfo> users = um.getUsers();
863 UserInfo currentUser = getCurrentUser();
864 for (final UserInfo user : users) {
865 if (user.supportsSwitchToByUser()) {
866 boolean isCurrentUser = currentUser == null
867 ? user.id == 0 : (currentUser.id == user.id);
868 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
869 : null;
870 SinglePressAction switchToUser = new SinglePressAction(
871 R.drawable.ic_menu_cc, icon,
872 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400873 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400874 public void onPress() {
875 try {
876 ActivityManager.getService().switchUser(user.id);
877 } catch (RemoteException re) {
878 Log.e(TAG, "Couldn't switch user " + re);
879 }
880 }
881
882 public boolean showDuringKeyguard() {
883 return true;
884 }
885
886 public boolean showBeforeProvisioning() {
887 return false;
888 }
889 };
890 items.add(switchToUser);
891 }
892 }
893 }
894 }
895
896 private void prepareDialog() {
897 refreshSilentMode();
898 mAirplaneModeOn.updateState(mAirplaneState);
899 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400900 if (mShowSilentToggle) {
901 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000902 mBroadcastDispatcher.registerReceiver(mRingerModeReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400903 }
904 }
905
906 private void refreshSilentMode() {
907 if (!mHasVibrator) {
908 final boolean silentModeOn =
909 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400910 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400911 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
912 }
913 }
914
915 /** {@inheritDoc} */
916 public void onDismiss(DialogInterface dialog) {
Steve Elliottc3147e22019-11-20 19:34:57 -0500917 if (mDialog == dialog) {
918 mDialog = null;
919 }
Jason Monk361915c2017-03-21 20:33:59 -0400920 mWindowManagerFuncs.onGlobalActionsHidden();
921 if (mShowSilentToggle) {
922 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000923 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400924 } catch (IllegalArgumentException ie) {
925 // ignore this
926 Log.w(TAG, ie);
927 }
928 }
929 }
930
931 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800932 public void onShow(DialogInterface dialog) {
933 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
934 }
935
Jason Monk361915c2017-03-21 20:33:59 -0400936 /**
937 * The adapter used for the list within the global actions dialog, taking
938 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400939 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
940 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400941 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
942 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500943 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400944 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400945 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400946 for (int i = 0; i < mItems.size(); i++) {
947 final Action action = mItems.get(i);
948
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400949 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
950 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400951 }
Jason Monk361915c2017-03-21 20:33:59 -0400952 }
953 return count;
954 }
955
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400956 private boolean shouldBeShown(Action action) {
957 if (mKeyguardShowing && !action.showDuringKeyguard()) {
958 return false;
959 }
960 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
961 return false;
962 }
963 return true;
964 }
965
966 @Override
967 public int countSeparatedItems() {
968 return countItems(true);
969 }
970
971 @Override
972 public int countListItems() {
973 return countItems(false);
974 }
975
976 @Override
977 public int getCount() {
978 return countSeparatedItems() + countListItems();
979 }
980
Jason Monk361915c2017-03-21 20:33:59 -0400981 @Override
982 public boolean isEnabled(int position) {
983 return getItem(position).isEnabled();
984 }
985
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500986 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400987 public boolean areAllItemsEnabled() {
988 return false;
989 }
990
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500991 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400992 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400993 int filteredPos = 0;
994 for (int i = 0; i < mItems.size(); i++) {
995 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400996 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400997 continue;
998 }
999 if (filteredPos == position) {
1000 return action;
1001 }
1002 filteredPos++;
1003 }
1004
1005 throw new IllegalArgumentException("position " + position
1006 + " out of range of showable actions"
1007 + ", filtered count=" + getCount()
1008 + ", keyguardshowing=" + mKeyguardShowing
1009 + ", provisioned=" + mDeviceProvisioned);
1010 }
1011
1012
1013 public long getItemId(int position) {
1014 return position;
1015 }
1016
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001017 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001018 public View getView(int position, View convertView, ViewGroup parent) {
1019 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001020 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001021 view.setOnClickListener(v -> onClickItem(position));
1022 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001023 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001024 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001025
1026 @Override
1027 public boolean onLongClickItem(int position) {
1028 final Action action = mAdapter.getItem(position);
1029 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001030 if (mDialog != null) {
1031 mDialog.dismiss();
1032 } else {
1033 Log.w(TAG, "Action long-clicked while mDialog is null.");
1034 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001035 return ((LongPressAction) action).onLongPress();
1036 }
1037 return false;
1038 }
1039
1040 @Override
1041 public void onClickItem(int position) {
1042 Action item = mAdapter.getItem(position);
1043 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001044 if (mDialog != null) {
1045 mDialog.dismiss();
1046 } else {
1047 Log.w(TAG, "Action clicked while mDialog is null.");
1048 }
1049 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001050 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001051 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001052
1053 @Override
1054 public boolean shouldBeSeparated(int position) {
1055 return getItem(position).shouldBeSeparated();
1056 }
Jason Monk361915c2017-03-21 20:33:59 -04001057 }
1058
1059 // note: the scheme below made more sense when we were planning on having
1060 // 8 different things in the global actions dialog. seems overkill with
1061 // only 3 items now, but may as well keep this flexible approach so it will
1062 // be easy should someone decide at the last minute to include something
1063 // else, such as 'enable wifi', or 'enable bluetooth'
1064
1065 /**
1066 * What each item in the global actions dialog must be able to support.
1067 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001068 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001069 /**
1070 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001071 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001072 */
1073 CharSequence getLabelForAccessibility(Context context);
1074
1075 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1076
1077 void onPress();
1078
1079 /**
1080 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001081 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001082 */
1083 boolean showDuringKeyguard();
1084
1085 /**
1086 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001087 * device is provisioned.onlongpress
1088 *
Jason Monk361915c2017-03-21 20:33:59 -04001089 */
1090 boolean showBeforeProvisioning();
1091
1092 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001093
1094 default boolean shouldBeSeparated() {
1095 return false;
1096 }
Jason Monk361915c2017-03-21 20:33:59 -04001097 }
1098
1099 /**
1100 * An action that also supports long press.
1101 */
1102 private interface LongPressAction extends Action {
1103 boolean onLongPress();
1104 }
1105
1106 /**
1107 * A single press action maintains no state, just responds to a press
1108 * and takes an action.
1109 */
1110 private static abstract class SinglePressAction implements Action {
1111 private final int mIconResId;
1112 private final Drawable mIcon;
1113 private final int mMessageResId;
1114 private final CharSequence mMessage;
1115
1116 protected SinglePressAction(int iconResId, int messageResId) {
1117 mIconResId = iconResId;
1118 mMessageResId = messageResId;
1119 mMessage = null;
1120 mIcon = null;
1121 }
1122
1123 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1124 mIconResId = iconResId;
1125 mMessageResId = 0;
1126 mMessage = message;
1127 mIcon = icon;
1128 }
1129
1130 public boolean isEnabled() {
1131 return true;
1132 }
1133
1134 public String getStatus() {
1135 return null;
1136 }
1137
1138 abstract public void onPress();
1139
1140 public CharSequence getLabelForAccessibility(Context context) {
1141 if (mMessage != null) {
1142 return mMessage;
1143 } else {
1144 return context.getString(mMessageResId);
1145 }
1146 }
1147
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001148 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001149 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001150 }
1151
Jason Monk361915c2017-03-21 20:33:59 -04001152 public View create(
1153 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001154 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001155 false);
Jason Monk361915c2017-03-21 20:33:59 -04001156
1157 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1158 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001159 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001160
1161 TextView statusView = (TextView) v.findViewById(R.id.status);
1162 final String status = getStatus();
1163 if (!TextUtils.isEmpty(status)) {
1164 statusView.setText(status);
1165 } else {
1166 statusView.setVisibility(View.GONE);
1167 }
1168 if (mIcon != null) {
1169 icon.setImageDrawable(mIcon);
1170 icon.setScaleType(ScaleType.CENTER_CROP);
1171 } else if (mIconResId != 0) {
1172 icon.setImageDrawable(context.getDrawable(mIconResId));
1173 }
1174 if (mMessage != null) {
1175 messageView.setText(mMessage);
1176 } else {
1177 messageView.setText(mMessageResId);
1178 }
1179
1180 return v;
1181 }
1182 }
1183
1184 /**
1185 * A toggle action knows whether it is on or off, and displays an icon
1186 * and status message accordingly.
1187 */
1188 private static abstract class ToggleAction implements Action {
1189
1190 enum State {
1191 Off(false),
1192 TurningOn(true),
1193 TurningOff(true),
1194 On(false);
1195
1196 private final boolean inTransition;
1197
1198 State(boolean intermediate) {
1199 inTransition = intermediate;
1200 }
1201
1202 public boolean inTransition() {
1203 return inTransition;
1204 }
1205 }
1206
1207 protected State mState = State.Off;
1208
1209 // prefs
1210 protected int mEnabledIconResId;
1211 protected int mDisabledIconResid;
1212 protected int mMessageResId;
1213 protected int mEnabledStatusMessageResId;
1214 protected int mDisabledStatusMessageResId;
1215
1216 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001217 * @param enabledIconResId The icon for when this action is on.
1218 * @param disabledIconResid The icon for when this action is off.
1219 * @param message The general information message, e.g 'Silent Mode'
1220 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001221 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1222 */
1223 public ToggleAction(int enabledIconResId,
1224 int disabledIconResid,
1225 int message,
1226 int enabledStatusMessageResId,
1227 int disabledStatusMessageResId) {
1228 mEnabledIconResId = enabledIconResId;
1229 mDisabledIconResid = disabledIconResid;
1230 mMessageResId = message;
1231 mEnabledStatusMessageResId = enabledStatusMessageResId;
1232 mDisabledStatusMessageResId = disabledStatusMessageResId;
1233 }
1234
1235 /**
1236 * Override to make changes to resource IDs just before creating the
1237 * View.
1238 */
1239 void willCreate() {
1240
1241 }
1242
1243 @Override
1244 public CharSequence getLabelForAccessibility(Context context) {
1245 return context.getString(mMessageResId);
1246 }
1247
1248 public View create(Context context, View convertView, ViewGroup parent,
1249 LayoutInflater inflater) {
1250 willCreate();
1251
1252 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001253 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001254
1255 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1256 TextView messageView = (TextView) v.findViewById(R.id.message);
1257 TextView statusView = (TextView) v.findViewById(R.id.status);
1258 final boolean enabled = isEnabled();
1259
1260 if (messageView != null) {
1261 messageView.setText(mMessageResId);
1262 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001263 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001264 }
1265
1266 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1267 if (icon != null) {
1268 icon.setImageDrawable(context.getDrawable(
1269 (on ? mEnabledIconResId : mDisabledIconResid)));
1270 icon.setEnabled(enabled);
1271 }
1272
1273 if (statusView != null) {
1274 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1275 statusView.setVisibility(View.VISIBLE);
1276 statusView.setEnabled(enabled);
1277 }
1278 v.setEnabled(enabled);
1279
1280 return v;
1281 }
1282
1283 public final void onPress() {
1284 if (mState.inTransition()) {
1285 Log.w(TAG, "shouldn't be able to toggle when in transition");
1286 return;
1287 }
1288
1289 final boolean nowOn = !(mState == State.On);
1290 onToggle(nowOn);
1291 changeStateFromPress(nowOn);
1292 }
1293
1294 public boolean isEnabled() {
1295 return !mState.inTransition();
1296 }
1297
1298 /**
1299 * Implementations may override this if their state can be in on of the intermediate
1300 * states until some notification is received (e.g airplane mode is 'turning off' until
1301 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001302 *
Jason Monk361915c2017-03-21 20:33:59 -04001303 * @param buttonOn Whether the button was turned on or off
1304 */
1305 protected void changeStateFromPress(boolean buttonOn) {
1306 mState = buttonOn ? State.On : State.Off;
1307 }
1308
1309 abstract void onToggle(boolean on);
1310
1311 public void updateState(State state) {
1312 mState = state;
1313 }
1314 }
1315
1316 private class SilentModeToggleAction extends ToggleAction {
1317 public SilentModeToggleAction() {
1318 super(R.drawable.ic_audio_vol_mute,
1319 R.drawable.ic_audio_vol,
1320 R.string.global_action_toggle_silent_mode,
1321 R.string.global_action_silent_mode_on_status,
1322 R.string.global_action_silent_mode_off_status);
1323 }
1324
1325 void onToggle(boolean on) {
1326 if (on) {
1327 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1328 } else {
1329 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1330 }
1331 }
1332
1333 public boolean showDuringKeyguard() {
1334 return true;
1335 }
1336
1337 public boolean showBeforeProvisioning() {
1338 return false;
1339 }
1340 }
1341
1342 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1343
Jason Monk16fbd9d2017-04-27 14:28:49 -04001344 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001345
1346 private final AudioManager mAudioManager;
1347 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001348
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001349 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001350 mAudioManager = audioManager;
1351 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001352 }
1353
1354 private int ringerModeToIndex(int ringerMode) {
1355 // They just happen to coincide
1356 return ringerMode;
1357 }
1358
1359 private int indexToRingerMode(int index) {
1360 // They just happen to coincide
1361 return index;
1362 }
1363
1364 @Override
1365 public CharSequence getLabelForAccessibility(Context context) {
1366 return null;
1367 }
1368
1369 public View create(Context context, View convertView, ViewGroup parent,
1370 LayoutInflater inflater) {
1371 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1372
1373 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1374 for (int i = 0; i < 3; i++) {
1375 View itemView = v.findViewById(ITEM_IDS[i]);
1376 itemView.setSelected(selectedIndex == i);
1377 // Set up click handler
1378 itemView.setTag(i);
1379 itemView.setOnClickListener(this);
1380 }
1381 return v;
1382 }
1383
1384 public void onPress() {
1385 }
1386
1387 public boolean showDuringKeyguard() {
1388 return true;
1389 }
1390
1391 public boolean showBeforeProvisioning() {
1392 return false;
1393 }
1394
1395 public boolean isEnabled() {
1396 return true;
1397 }
1398
1399 void willCreate() {
1400 }
1401
1402 public void onClick(View v) {
1403 if (!(v.getTag() instanceof Integer)) return;
1404
1405 int index = (Integer) v.getTag();
1406 mAudioManager.setRingerMode(indexToRingerMode(index));
1407 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1408 }
1409 }
1410
1411 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1412 public void onReceive(Context context, Intent intent) {
1413 String action = intent.getAction();
1414 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1415 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1416 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1417 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001418 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001419 }
1420 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1421 // Airplane mode can be changed after ECM exits if airplane toggle button
1422 // is pressed during ECM mode
1423 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1424 mIsWaitingForEcmExit) {
1425 mIsWaitingForEcmExit = false;
1426 changeAirplaneModeSystemSetting(true);
1427 }
1428 }
1429 }
1430 };
1431
1432 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1433 @Override
1434 public void onServiceStateChanged(ServiceState serviceState) {
1435 if (!mHasTelephony) return;
1436 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1437 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1438 mAirplaneModeOn.updateState(mAirplaneState);
1439 mAdapter.notifyDataSetChanged();
1440 }
1441 };
1442
1443 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1444 @Override
1445 public void onReceive(Context context, Intent intent) {
1446 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1447 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1448 }
1449 }
1450 };
1451
1452 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1453 @Override
1454 public void onChange(boolean selfChange) {
1455 onAirplaneModeChanged();
1456 }
1457 };
1458
1459 private static final int MESSAGE_DISMISS = 0;
1460 private static final int MESSAGE_REFRESH = 1;
1461 private static final int MESSAGE_SHOW = 2;
1462 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1463
1464 private Handler mHandler = new Handler() {
1465 public void handleMessage(Message msg) {
1466 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001467 case MESSAGE_DISMISS:
1468 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001469 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1470 mDialog.dismissImmediately();
1471 } else {
1472 mDialog.dismiss();
1473 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001474 mDialog = null;
1475 }
1476 break;
1477 case MESSAGE_REFRESH:
1478 refreshSilentMode();
1479 mAdapter.notifyDataSetChanged();
1480 break;
1481 case MESSAGE_SHOW:
1482 handleShow();
1483 break;
Jason Monk361915c2017-03-21 20:33:59 -04001484 }
1485 }
1486 };
1487
1488 private void onAirplaneModeChanged() {
1489 // Let the service state callbacks handle the state.
1490 if (mHasTelephony) return;
1491
1492 boolean airplaneModeOn = Settings.Global.getInt(
1493 mContext.getContentResolver(),
1494 Settings.Global.AIRPLANE_MODE_ON,
1495 0) == 1;
1496 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1497 mAirplaneModeOn.updateState(mAirplaneState);
1498 }
1499
1500 /**
1501 * Change the airplane mode system setting
1502 */
1503 private void changeAirplaneModeSystemSetting(boolean on) {
1504 Settings.Global.putInt(
1505 mContext.getContentResolver(),
1506 Settings.Global.AIRPLANE_MODE_ON,
1507 on ? 1 : 0);
1508 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1509 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1510 intent.putExtra("state", on);
1511 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1512 if (!mHasTelephony) {
1513 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1514 }
1515 }
1516
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001517 private static final class ActionsDialog extends Dialog implements DialogInterface,
1518 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001519
Jason Monk16fbd9d2017-04-27 14:28:49 -04001520 private final Context mContext;
1521 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001522 private final IStatusBarService mStatusBarService;
1523 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001524 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001525 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001526 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001527 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001528 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001529 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001530 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001531 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001532 private boolean mHadTopUi;
1533 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001534
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001535 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001536 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001537 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001538 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001539 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001540 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001541 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001542 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001543
1544 // Window initialization
1545 Window window = getWindow();
1546 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001547 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1548 window.getDecorView();
1549 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1550 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1551 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1552 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1553 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001554 window.addFlags(
1555 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001556 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001557 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001558 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1559 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1560 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001561 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001562 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001563
1564 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001565 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001566 }
1567
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001568 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001569 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001570 }
1571
1572 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001573 int rotation = RotationUtils.getRotation(mContext);
1574 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1575 if (rotation != RotationUtils.ROTATION_NONE) {
1576 if (rotationLocked) {
1577 if (mResetOrientationData == null) {
1578 mResetOrientationData = new ResetOrientationData();
1579 mResetOrientationData.locked = true;
1580 mResetOrientationData.rotation = rotation;
1581 }
1582
1583 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001584 // This call is posted so that the rotation does not change until post-layout,
1585 // otherwise onConfigurationChanged() may not get invoked.
1586 mGlobalActionsLayout.post(() ->
1587 RotationPolicy.setRotationLockAtAngle(
1588 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001589 }
1590 } else {
1591 if (!rotationLocked) {
1592 if (mResetOrientationData == null) {
1593 mResetOrientationData = new ResetOrientationData();
1594 mResetOrientationData.locked = false;
1595 }
1596
1597 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001598 // This call is posted so that the rotation does not change until post-layout,
1599 // otherwise onConfigurationChanged() may not get invoked.
1600 mGlobalActionsLayout.post(() ->
1601 RotationPolicy.setRotationLockAtAngle(
1602 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001603 }
1604
Steve Elliott53f12ae2019-05-13 17:14:15 -04001605 // Disable rotation suggestions, if enabled
1606 setRotationSuggestionsEnabled(false);
1607
Steve Elliott300b48f2019-05-29 14:13:50 -04001608 FrameLayout panelContainer =
1609 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001610 FrameLayout.LayoutParams panelParams =
1611 new FrameLayout.LayoutParams(
1612 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001613 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001614 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001615 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1616 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001617 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001618 }
1619
1620 private void initializeLayout() {
1621 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001622 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001623 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001624 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001625 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001626 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001627 @Override
1628 public boolean dispatchPopulateAccessibilityEvent(
1629 View host, AccessibilityEvent event) {
1630 // Populate the title here, just as Activity does
1631 event.getText().add(mContext.getString(R.string.global_actions));
1632 return true;
1633 }
1634 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001635 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001636 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001637
Steve Elliott86ef6282019-05-08 14:45:04 -04001638 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001639 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001640 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001641 if (mBackgroundDrawable == null) {
1642 mBackgroundDrawable = new ScrimDrawable();
1643 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1644 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001645 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001646 }
1647
Steve Elliott300b48f2019-05-29 14:13:50 -04001648 private void fixNavBarClipping() {
1649 ViewGroup content = findViewById(android.R.id.content);
1650 content.setClipChildren(false);
1651 content.setClipToPadding(false);
1652 ViewGroup contentParent = (ViewGroup) content.getParent();
1653 contentParent.setClipChildren(false);
1654 contentParent.setClipToPadding(false);
1655 }
1656
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001657 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001658 int rotation = RotationUtils.getRotation(context);
1659 boolean useGridLayout = isForceGridEnabled(context)
1660 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1661 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001662 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001663 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001664 } else {
1665 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001666 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001667 } else {
1668 if (useGridLayout) {
1669 return com.android.systemui.R.layout.global_actions_grid;
1670 } else {
1671 return com.android.systemui.R.layout.global_actions_column;
1672 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001673 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001674 }
1675
Jason Monk361915c2017-03-21 20:33:59 -04001676 @Override
1677 protected void onStart() {
1678 super.setCanceledOnTouchOutside(true);
1679 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001680 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001681
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001682 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001683 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001684 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001685 updateColors(colors, false /* animate */);
1686 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001687 }
1688
1689 /**
1690 * Updates background and system bars according to current GradientColors.
1691 * @param colors Colors and hints to use.
1692 * @param animate Interpolates gradient if true, just sets otherwise.
1693 */
1694 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001695 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001696 return;
1697 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001698 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001699 View decorView = getWindow().getDecorView();
1700 if (colors.supportsDarkText()) {
1701 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001702 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001703 } else {
1704 decorView.setSystemUiVisibility(0);
1705 }
Jason Monk361915c2017-03-21 20:33:59 -04001706 }
1707
1708 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001709 protected void onStop() {
1710 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001711 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001712 }
1713
1714 @Override
1715 public void show() {
1716 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001717 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001718 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1719 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001720 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001721 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1722 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001723 mGlobalActionsLayout.setAlpha(0);
1724 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001725 .alpha(1)
1726 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001727 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001728 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001729 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001730 .setUpdateListener(animation -> {
1731 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001732 * mScrimAlpha * 255);
1733 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001734 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001735 .start();
1736 }
1737
1738 @Override
1739 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001740 if (!mShowing) {
1741 return;
1742 }
1743 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001744 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001745 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001746 mGlobalActionsLayout.setAlpha(1);
1747 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001748 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001749 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1750 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001751 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001752 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001753 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001754 .setUpdateListener(animation -> {
1755 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001756 * mScrimAlpha * 255);
1757 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001758 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001759 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001760 dismissPanel();
1761 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001762 }
1763
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001764 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001765 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001766 dismissPanel();
1767 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001768 completeDismiss();
1769 }
1770
1771 private void completeDismiss() {
1772 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1773 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001774 }
1775
1776 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001777 if (mPanelController != null) {
1778 mPanelController.onDismissed();
1779 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001780 }
1781
Steve Elliott53f12ae2019-05-13 17:14:15 -04001782 private void setRotationSuggestionsEnabled(boolean enabled) {
1783 try {
1784 final int userId = Binder.getCallingUserHandle().getIdentifier();
1785 final int what = enabled
1786 ? StatusBarManager.DISABLE2_NONE
1787 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1788 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1789 } catch (RemoteException ex) {
1790 throw ex.rethrowFromSystemServer();
1791 }
1792 }
1793
Steve Elliott48f75db2019-05-03 15:03:38 -04001794 private void resetOrientation() {
1795 if (mResetOrientationData != null) {
1796 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1797 mResetOrientationData.rotation);
1798 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001799 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001800 }
1801
Jason Monk361915c2017-03-21 20:33:59 -04001802 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001803 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001804 if (mKeyguardShowing) {
1805 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001806 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1807 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001808 }
1809 } else {
1810 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001811 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1812 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001813 }
1814 }
1815 }
1816
1817 public void setKeyguardShowing(boolean keyguardShowing) {
1818 mKeyguardShowing = keyguardShowing;
1819 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001820
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001821 public void refreshDialog() {
1822 initializeLayout();
1823 mGlobalActionsLayout.updateList();
1824 }
1825
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001826 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001827 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001828 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001829 }
1830 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001831
1832 private static class ResetOrientationData {
1833 public boolean locked;
1834 public int rotation;
1835 }
Jason Monk361915c2017-03-21 20:33:59 -04001836 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001837
1838 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001839 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1840 */
1841 private static boolean isPanelDebugModeEnabled(Context context) {
1842 return Settings.Secure.getInt(context.getContentResolver(),
1843 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1844 }
1845
1846 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001847 * Determines whether or not the Global Actions menu should be forced to
1848 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001849 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001850 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001851 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001852 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001853
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001854
1855 /**
1856 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1857 */
1858 private static boolean shouldUseSeparatedView() {
1859 return true;
1860 }
Jason Monk361915c2017-03-21 20:33:59 -04001861}