blob: 3ccad64d76dc108b5eb4a7ce931d273965a68afa [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Steve Elliott4c868852019-03-14 16:25:41 -040027import android.app.PendingIntent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040028import android.app.StatusBarManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070029import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000030import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010031import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040032import android.content.BroadcastReceiver;
33import android.content.Context;
34import android.content.DialogInterface;
35import android.content.Intent;
36import android.content.IntentFilter;
37import android.content.pm.UserInfo;
38import android.database.ContentObserver;
39import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
Steve Elliott53f12ae2019-05-13 17:14:15 -040042import android.os.Binder;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
Steve Elliott53f12ae2019-05-13 17:14:15 -040044import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.os.Message;
46import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.os.UserHandle;
50import android.os.UserManager;
51import android.os.Vibrator;
52import android.provider.Settings;
53import android.service.dreams.DreamService;
54import android.service.dreams.IDreamManager;
Inseob Kim5e82f732019-11-08 15:08:38 +090055import android.sysprop.TelephonyProperties;
Fan Zhangf9914762019-11-01 15:58:38 -070056import android.telecom.TelecomManager;
Jason Monk361915c2017-03-21 20:33:59 -040057import android.telephony.PhoneStateListener;
58import android.telephony.ServiceState;
59import android.telephony.TelephonyManager;
60import android.text.TextUtils;
61import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080062import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040063import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070064import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.LayoutInflater;
66import android.view.View;
67import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070068import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.view.WindowManager;
70import android.view.WindowManagerGlobal;
71import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050072import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.ImageView;
74import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040075import android.widget.TextView;
76
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.R;
78import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupin2bd3af62019-03-25 17:44:28 -070080import com.android.internal.colorextraction.drawable.ScrimDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Steve Elliott53f12ae2019-05-13 17:14:15 -040083import com.android.internal.statusbar.IStatusBarService;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040085import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050086import com.android.internal.util.ScreenshotHelper;
Steve Elliott48f75db2019-05-03 15:03:38 -040087import com.android.internal.view.RotationPolicy;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.internal.widget.LockPatternUtils;
89import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050091import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050092import com.android.systemui.MultiListLayout.MultiListAdapter;
Fabian Kozynskiff5e91f2019-09-24 15:38:08 -040093import com.android.systemui.broadcast.BroadcastDispatcher;
Charles He9851a8d2017-10-10 17:31:30 +010094import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040095import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010096import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050097import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050098import com.android.systemui.statusbar.phone.ScrimController;
Steve Elliottff2c0d92019-07-30 15:09:54 -040099import com.android.systemui.statusbar.phone.StatusBarWindowController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700100import com.android.systemui.statusbar.policy.ConfigurationController;
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400101import com.android.systemui.statusbar.policy.KeyguardStateController;
Shaotang Li5c422632018-07-04 14:18:40 +0800102import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500103import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -0500104import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700105
Jason Monk361915c2017-03-21 20:33:59 -0400106import java.util.ArrayList;
107import java.util.List;
108
109/**
110 * Helper to show the global actions dialog. Each item is an {@link Action} that
111 * may show depending on whether the keyguard is showing, and whether the device
112 * is provisioned.
113 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500114public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
115 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400116
117 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
118 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700119 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400120
121 private static final String TAG = "GlobalActionsDialog";
122
123 private static final boolean SHOW_SILENT_TOGGLE = true;
124
125 /* Valid settings for global actions keys.
126 * see config.xml config_globalActionList */
127 private static final String GLOBAL_ACTION_KEY_POWER = "power";
128 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
129 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
130 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
131 private static final String GLOBAL_ACTION_KEY_USERS = "users";
132 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
133 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
134 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
135 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
136 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000137 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800138 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500139 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400140
141 private final Context mContext;
142 private final GlobalActionsManager mWindowManagerFuncs;
143 private final AudioManager mAudioManager;
144 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000145 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800146 private final LockPatternUtils mLockPatternUtils;
147 private final KeyguardManager mKeyguardManager;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000148 private final BroadcastDispatcher mBroadcastDispatcher;
Jason Monk361915c2017-03-21 20:33:59 -0400149
150 private ArrayList<Action> mItems;
151 private ActionsDialog mDialog;
152
153 private Action mSilentModeAction;
154 private ToggleAction mAirplaneModeOn;
155
156 private MyAdapter mAdapter;
157
158 private boolean mKeyguardShowing = false;
159 private boolean mDeviceProvisioned = false;
160 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
161 private boolean mIsWaitingForEcmExit = false;
162 private boolean mHasTelephony;
163 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000164 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800165 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400166 private final boolean mShowSilentToggle;
167 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500168 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400169 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400170 private final ActivityStarter mActivityStarter;
171 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500172
Jason Monk361915c2017-03-21 20:33:59 -0400173 /**
174 * @param context everything needs a context :(
175 */
176 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700177 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400178 mWindowManagerFuncs = windowManagerFuncs;
179 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
180 mDreamManager = IDreamManager.Stub.asInterface(
181 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000182 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
183 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800184 mLockPatternUtils = new LockPatternUtils(mContext);
185 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000186 mBroadcastDispatcher = Dependency.get(BroadcastDispatcher.class);
Jason Monk361915c2017-03-21 20:33:59 -0400187
188 // receive broadcasts
189 IntentFilter filter = new IntentFilter();
190 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
191 filter.addAction(Intent.ACTION_SCREEN_OFF);
Jayachandran C142eae02019-12-13 19:29:20 -0800192 filter.addAction(TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000193 mBroadcastDispatcher.registerReceiver(mBroadcastReceiver, filter);
Jason Monk361915c2017-03-21 20:33:59 -0400194
195 ConnectivityManager cm = (ConnectivityManager)
196 context.getSystemService(Context.CONNECTIVITY_SERVICE);
197 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
198
199 // get notified of phone state changes
200 TelephonyManager telephonyManager =
201 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
202 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
203 mContext.getContentResolver().registerContentObserver(
204 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
205 mAirplaneModeObserver);
206 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
207 mHasVibrator = vibrator != null && vibrator.hasVibrator();
208
209 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
210 R.bool.config_useFixedVolume);
211
212 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500213 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400214 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700215
216 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500217
Steve Elliott4c868852019-03-14 16:25:41 -0400218 mActivityStarter = Dependency.get(ActivityStarter.class);
Lucas Dupinc8f16e82019-09-17 18:24:50 -0400219 KeyguardStateController keyguardStateController =
220 Dependency.get(KeyguardStateController.class);
221 keyguardStateController.addCallback(new KeyguardStateController.Callback() {
222 @Override
223 public void onUnlockedChanged() {
224 if (mDialog != null && mDialog.mPanelController != null) {
225 boolean locked = !keyguardStateController.canDismissLockScreen();
226 mDialog.mPanelController.onDeviceLockStateChanged(locked);
227 }
228 }
229 });
Jason Monk361915c2017-03-21 20:33:59 -0400230 }
231
232 /**
233 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400234 *
Jason Monk361915c2017-03-21 20:33:59 -0400235 * @param keyguardShowing True if keyguard is showing
236 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400237 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
238 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400239 mKeyguardShowing = keyguardShowing;
240 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400241 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400242 if (mDialog != null) {
243 mDialog.dismiss();
244 mDialog = null;
245 // Show delayed, so that the dismiss of the previous dialog completes
246 mHandler.sendEmptyMessage(MESSAGE_SHOW);
247 } else {
248 handleShow();
249 }
250 }
251
Charles He9851a8d2017-10-10 17:31:30 +0100252 /**
253 * Dismiss the global actions dialog, if it's currently shown
254 */
255 public void dismissDialog() {
256 mHandler.removeMessages(MESSAGE_DISMISS);
257 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
258 }
259
Jason Monk361915c2017-03-21 20:33:59 -0400260 private void awakenIfNecessary() {
261 if (mDreamManager != null) {
262 try {
263 if (mDreamManager.isDreaming()) {
264 mDreamManager.awaken();
265 }
266 } catch (RemoteException e) {
267 // we tried
268 }
269 }
270 }
271
272 private void handleShow() {
273 awakenIfNecessary();
274 mDialog = createDialog();
275 prepareDialog();
276
277 // If we only have 1 item and it's a simple press action, just do this action.
278 if (mAdapter.getCount() == 1
279 && mAdapter.getItem(0) instanceof SinglePressAction
280 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
281 ((SinglePressAction) mAdapter.getItem(0)).onPress();
282 } else {
283 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
284 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100285 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400286 mDialog.getWindow().setAttributes(attrs);
287 mDialog.show();
288 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400289 }
290 }
291
292 /**
293 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400294 *
Jason Monk361915c2017-03-21 20:33:59 -0400295 * @return A new dialog.
296 */
297 private ActionsDialog createDialog() {
298 // Simple toggle style if there's no vibrator, otherwise use a tri-state
299 if (!mHasVibrator) {
300 mSilentModeAction = new SilentModeToggleAction();
301 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700302 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400303 }
304 mAirplaneModeOn = new ToggleAction(
305 R.drawable.ic_lock_airplane_mode,
306 R.drawable.ic_lock_airplane_mode_off,
307 R.string.global_actions_toggle_airplane_mode,
308 R.string.global_actions_airplane_mode_on_status,
309 R.string.global_actions_airplane_mode_off_status) {
310
311 void onToggle(boolean on) {
Inseob Kim5e82f732019-11-08 15:08:38 +0900312 if (mHasTelephony && TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400313 mIsWaitingForEcmExit = true;
314 // Launch ECM exit dialog
315 Intent ecmDialogIntent =
Jayachandran C142eae02019-12-13 19:29:20 -0800316 new Intent(TelephonyManager.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
Jason Monk361915c2017-03-21 20:33:59 -0400317 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
318 mContext.startActivity(ecmDialogIntent);
319 } else {
320 changeAirplaneModeSystemSetting(on);
321 }
322 }
323
324 @Override
325 protected void changeStateFromPress(boolean buttonOn) {
326 if (!mHasTelephony) return;
327
328 // In ECM mode airplane state cannot be changed
Inseob Kim5e82f732019-11-08 15:08:38 +0900329 if (!TelephonyProperties.in_ecm_mode().orElse(false)) {
Jason Monk361915c2017-03-21 20:33:59 -0400330 mState = buttonOn ? State.TurningOn : State.TurningOff;
331 mAirplaneState = mState;
332 }
333 }
334
335 public boolean showDuringKeyguard() {
336 return true;
337 }
338
339 public boolean showBeforeProvisioning() {
340 return false;
341 }
342 };
343 onAirplaneModeChanged();
344
345 mItems = new ArrayList<Action>();
346 String[] defaultActions = mContext.getResources().getStringArray(
347 R.array.config_globalActionsList);
348
349 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000350 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800351 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400352 for (int i = 0; i < defaultActions.length; i++) {
353 String actionKey = defaultActions[i];
354 if (addedKeys.contains(actionKey)) {
355 // If we already have added this, don't add it again.
356 continue;
357 }
358 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
359 mItems.add(new PowerAction());
360 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
361 mItems.add(mAirplaneModeOn);
362 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
363 if (Settings.Global.getInt(mContext.getContentResolver(),
364 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
365 mItems.add(new BugReportAction());
366 }
367 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
368 if (mShowSilentToggle) {
369 mItems.add(mSilentModeAction);
370 }
371 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
372 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
373 addUsersToMenu(mItems);
374 }
375 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
376 mItems.add(getSettingsAction());
377 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700378 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
379 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800380 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700381 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800382 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
385 mItems.add(getVoiceAssistAction());
386 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
387 mItems.add(getAssistAction());
388 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
389 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500390 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
391 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000392 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000393 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000394 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
395 mItems.add(new LogoutAction());
396 mHasLogoutButton = true;
397 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800398 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400399 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800400 mItems.add(new EmergencyDialerAction());
401 }
Jason Monk361915c2017-03-21 20:33:59 -0400402 } else {
403 Log.e(TAG, "Invalid global action key " + actionKey);
404 }
405 // Add here so we don't add more than one.
406 addedKeys.add(actionKey);
407 }
408
409 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400410 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400411 }
412
413 mAdapter = new MyAdapter();
414
Steve Elliott9b87a442019-03-05 10:24:16 -0500415 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400416 mPanelPlugin != null
417 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400418 new GlobalActionsPanelPlugin.Callbacks() {
419 @Override
420 public void dismissGlobalActionsMenu() {
Steve Elliottc3147e22019-11-20 19:34:57 -0500421 dismissDialog();
Steve Elliott4c868852019-03-14 16:25:41 -0400422 }
423
424 @Override
425 public void startPendingIntentDismissingKeyguard(
426 PendingIntent intent) {
427 mActivityStarter
428 .startPendingIntentDismissingKeyguard(intent);
429 }
Steve Elliotte2a2e9b2019-04-30 21:31:03 -0400430 },
431 mKeyguardManager.isDeviceLocked())
Steve Elliott9b87a442019-03-05 10:24:16 -0500432 : null;
Steve Elliott02bbaaa2019-05-01 16:55:33 -0400433
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400434 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400435 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700436 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400437
438 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800439 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400440
441 return dialog;
442 }
443
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800444 private boolean shouldDisplayLockdown() {
445 int userId = getCurrentUser().id;
446 // Lockdown is meaningless without a place to go.
447 if (!mKeyguardManager.isDeviceSecure(userId)) {
448 return false;
449 }
450
451 // Only show the lockdown button if the device isn't locked down (for whatever reason).
452 int state = mLockPatternUtils.getStrongAuthForUser(userId);
453 return (state == STRONG_AUTH_NOT_REQUIRED
454 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
455 }
456
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700457 @Override
458 public void onUiModeChanged() {
459 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
Lucas Dupina4e12ba2019-04-18 11:56:28 -0700460 if (mDialog != null && mDialog.isShowing()) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -0400461 mDialog.refreshDialog();
462 }
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700463 }
464
465 public void destroy() {
466 Dependency.get(ConfigurationController.class).removeCallback(this);
467 }
468
Jason Monk361915c2017-03-21 20:33:59 -0400469 private final class PowerAction extends SinglePressAction implements LongPressAction {
470 private PowerAction() {
471 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400472 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400473 }
474
475 @Override
476 public boolean onLongPress() {
477 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
478 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
479 mWindowManagerFuncs.reboot(true);
480 return true;
481 }
482 return false;
483 }
484
485 @Override
486 public boolean showDuringKeyguard() {
487 return true;
488 }
489
490 @Override
491 public boolean showBeforeProvisioning() {
492 return true;
493 }
494
495 @Override
496 public void onPress() {
497 // shutdown by making sure radio and power are handled accordingly.
498 mWindowManagerFuncs.shutdown();
499 }
500 }
501
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400502 private abstract class EmergencyAction extends SinglePressAction {
503 EmergencyAction(int iconResId, int messageResId) {
504 super(iconResId, messageResId);
505 }
506
507 @Override
508 public boolean shouldBeSeparated() {
509 return shouldUseSeparatedView();
510 }
511
512 @Override
513 public View create(
514 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
515 View v = super.create(context, convertView, parent, inflater);
516 int textColor;
517 if (shouldBeSeparated()) {
518 textColor = v.getResources().getColor(
519 com.android.systemui.R.color.global_actions_alert_text);
520 } else {
521 textColor = v.getResources().getColor(
522 com.android.systemui.R.color.global_actions_text);
523 }
524 TextView messageView = v.findViewById(R.id.message);
525 messageView.setTextColor(textColor);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -0400526 messageView.setSelected(true); // necessary for marquee to work
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400527 ImageView icon = (ImageView) v.findViewById(R.id.icon);
528 icon.getDrawable().setTint(textColor);
529 return v;
530 }
531
532 @Override
533 public boolean showDuringKeyguard() {
534 return true;
535 }
536
537 @Override
538 public boolean showBeforeProvisioning() {
539 return true;
540 }
541 }
542
543 private class EmergencyAffordanceAction extends EmergencyAction {
544 EmergencyAffordanceAction() {
545 super(R.drawable.emergency_icon,
546 R.string.global_action_emergency);
547 }
548
549 @Override
550 public void onPress() {
551 mEmergencyAffordanceManager.performEmergencyCall();
552 }
553 }
554
555 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800556 private EmergencyDialerAction() {
Aran Ink50195262019-05-30 10:55:37 -0400557 super(com.android.systemui.R.drawable.ic_emergency_star,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800558 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800559 }
560
561 @Override
562 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800563 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Fan Zhangf9914762019-11-01 15:58:38 -0700564 Intent intent = mContext.getSystemService(TelecomManager.class)
565 .createLaunchEmergencyDialerIntent(null /* number */);
Leo Hsue4cc3122019-03-06 15:25:28 +0800566 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
567 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
568 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800569 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
570 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800571 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
572 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800573 }
574
Jason Monk361915c2017-03-21 20:33:59 -0400575 private final class RestartAction extends SinglePressAction implements LongPressAction {
576 private RestartAction() {
577 super(R.drawable.ic_restart, R.string.global_action_restart);
578 }
579
580 @Override
581 public boolean onLongPress() {
582 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
583 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
584 mWindowManagerFuncs.reboot(true);
585 return true;
586 }
587 return false;
588 }
589
590 @Override
591 public boolean showDuringKeyguard() {
592 return true;
593 }
594
595 @Override
596 public boolean showBeforeProvisioning() {
597 return true;
598 }
599
600 @Override
601 public void onPress() {
602 mWindowManagerFuncs.reboot(false);
603 }
604 }
605
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400606 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500607 public ScreenshotAction() {
608 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
609 }
610
611 @Override
612 public void onPress() {
613 // Add a little delay before executing, to give the
614 // dialog a chance to go away before it takes a
615 // screenshot.
616 // TODO: instead, omit global action dialog layer
617 mHandler.postDelayed(new Runnable() {
618 @Override
619 public void run() {
James O'Learyfa5bb7a2019-09-05 13:43:29 -0400620 mScreenshotHelper.takeScreenshot(1, true, true, mHandler, null);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500621 MetricsLogger.action(mContext,
622 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
623 }
624 }, 500);
625 }
626
627 @Override
628 public boolean showDuringKeyguard() {
629 return true;
630 }
631
632 @Override
633 public boolean showBeforeProvisioning() {
634 return false;
635 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400636
637 @Override
638 public boolean onLongPress() {
639 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
640 mScreenRecordHelper.launchRecordPrompt();
641 } else {
642 onPress();
643 }
644 return true;
645 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500646 }
647
Jason Monk361915c2017-03-21 20:33:59 -0400648 private class BugReportAction extends SinglePressAction implements LongPressAction {
649
650 public BugReportAction() {
651 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
652 }
653
654 @Override
655 public void onPress() {
656 // don't actually trigger the bugreport if we are running stability
657 // tests via monkey
658 if (ActivityManager.isUserAMonkey()) {
659 return;
660 }
661 // Add a little delay before executing, to give the
662 // dialog a chance to go away before it takes a
663 // screenshot.
664 mHandler.postDelayed(new Runnable() {
665 @Override
666 public void run() {
667 try {
668 // Take an "interactive" bugreport.
669 MetricsLogger.action(mContext,
670 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
Paul Changc40f5782019-11-04 18:55:31 +0800671 if (!ActivityManager.getService().launchBugReportHandlerApp()) {
672 Log.w(TAG, "Bugreport handler could not be launched");
673 ActivityManager.getService().requestInteractiveBugReport();
674 }
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) {
Steve Elliottc3147e22019-11-20 19:34:57 -0500919 if (mDialog == dialog) {
920 mDialog = null;
921 }
Jason Monk361915c2017-03-21 20:33:59 -0400922 mWindowManagerFuncs.onGlobalActionsHidden();
923 if (mShowSilentToggle) {
924 try {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +0000925 mBroadcastDispatcher.unregisterReceiver(mRingerModeReceiver);
Jason Monk361915c2017-03-21 20:33:59 -0400926 } catch (IllegalArgumentException ie) {
927 // ignore this
928 Log.w(TAG, ie);
929 }
930 }
931 }
932
933 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800934 public void onShow(DialogInterface dialog) {
935 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
936 }
937
Jason Monk361915c2017-03-21 20:33:59 -0400938 /**
939 * The adapter used for the list within the global actions dialog, taking
940 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400941 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
942 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400943 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
944 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500945 public class MyAdapter extends MultiListAdapter {
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400946 private int countItems(boolean separated) {
Jason Monk361915c2017-03-21 20:33:59 -0400947 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400948 for (int i = 0; i < mItems.size(); i++) {
949 final Action action = mItems.get(i);
950
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400951 if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
952 count++;
Jason Monk361915c2017-03-21 20:33:59 -0400953 }
Jason Monk361915c2017-03-21 20:33:59 -0400954 }
955 return count;
956 }
957
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400958 private boolean shouldBeShown(Action action) {
959 if (mKeyguardShowing && !action.showDuringKeyguard()) {
960 return false;
961 }
962 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
963 return false;
964 }
965 return true;
966 }
967
968 @Override
969 public int countSeparatedItems() {
970 return countItems(true);
971 }
972
973 @Override
974 public int countListItems() {
975 return countItems(false);
976 }
977
978 @Override
979 public int getCount() {
980 return countSeparatedItems() + countListItems();
981 }
982
Jason Monk361915c2017-03-21 20:33:59 -0400983 @Override
984 public boolean isEnabled(int position) {
985 return getItem(position).isEnabled();
986 }
987
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500988 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400989 public boolean areAllItemsEnabled() {
990 return false;
991 }
992
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500993 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400994 public Action getItem(int position) {
Jason Monk361915c2017-03-21 20:33:59 -0400995 int filteredPos = 0;
996 for (int i = 0; i < mItems.size(); i++) {
997 final Action action = mItems.get(i);
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -0400998 if (!shouldBeShown(action)) {
Jason Monk361915c2017-03-21 20:33:59 -0400999 continue;
1000 }
1001 if (filteredPos == position) {
1002 return action;
1003 }
1004 filteredPos++;
1005 }
1006
1007 throw new IllegalArgumentException("position " + position
1008 + " out of range of showable actions"
1009 + ", filtered count=" + getCount()
1010 + ", keyguardshowing=" + mKeyguardShowing
1011 + ", provisioned=" + mDeviceProvisioned);
1012 }
1013
1014
1015 public long getItemId(int position) {
1016 return position;
1017 }
1018
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001019 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001020 public View getView(int position, View convertView, ViewGroup parent) {
1021 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001022 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001023 view.setOnClickListener(v -> onClickItem(position));
1024 view.setOnLongClickListener(v -> onLongClickItem(position));
Jason Monk16fbd9d2017-04-27 14:28:49 -04001025 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001026 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001027
1028 @Override
1029 public boolean onLongClickItem(int position) {
1030 final Action action = mAdapter.getItem(position);
1031 if (action instanceof LongPressAction) {
Aran Inkbaa25862019-06-11 15:48:55 -04001032 if (mDialog != null) {
1033 mDialog.dismiss();
1034 } else {
1035 Log.w(TAG, "Action long-clicked while mDialog is null.");
1036 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001037 return ((LongPressAction) action).onLongPress();
1038 }
1039 return false;
1040 }
1041
1042 @Override
1043 public void onClickItem(int position) {
1044 Action item = mAdapter.getItem(position);
1045 if (!(item instanceof SilentModeTriStateAction)) {
Aran Inkbaa25862019-06-11 15:48:55 -04001046 if (mDialog != null) {
1047 mDialog.dismiss();
1048 } else {
1049 Log.w(TAG, "Action clicked while mDialog is null.");
1050 }
1051 item.onPress();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001052 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001053 }
Aaron Heuckroth5e28f8c82019-04-24 13:26:12 -04001054
1055 @Override
1056 public boolean shouldBeSeparated(int position) {
1057 return getItem(position).shouldBeSeparated();
1058 }
Jason Monk361915c2017-03-21 20:33:59 -04001059 }
1060
1061 // note: the scheme below made more sense when we were planning on having
1062 // 8 different things in the global actions dialog. seems overkill with
1063 // only 3 items now, but may as well keep this flexible approach so it will
1064 // be easy should someone decide at the last minute to include something
1065 // else, such as 'enable wifi', or 'enable bluetooth'
1066
1067 /**
1068 * What each item in the global actions dialog must be able to support.
1069 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001070 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001071 /**
1072 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001073 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001074 */
1075 CharSequence getLabelForAccessibility(Context context);
1076
1077 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1078
1079 void onPress();
1080
1081 /**
1082 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001083 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001084 */
1085 boolean showDuringKeyguard();
1086
1087 /**
1088 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001089 * device is provisioned.onlongpress
1090 *
Jason Monk361915c2017-03-21 20:33:59 -04001091 */
1092 boolean showBeforeProvisioning();
1093
1094 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001095
1096 default boolean shouldBeSeparated() {
1097 return false;
1098 }
Jason Monk361915c2017-03-21 20:33:59 -04001099 }
1100
1101 /**
1102 * An action that also supports long press.
1103 */
1104 private interface LongPressAction extends Action {
1105 boolean onLongPress();
1106 }
1107
1108 /**
1109 * A single press action maintains no state, just responds to a press
1110 * and takes an action.
1111 */
1112 private static abstract class SinglePressAction implements Action {
1113 private final int mIconResId;
1114 private final Drawable mIcon;
1115 private final int mMessageResId;
1116 private final CharSequence mMessage;
1117
1118 protected SinglePressAction(int iconResId, int messageResId) {
1119 mIconResId = iconResId;
1120 mMessageResId = messageResId;
1121 mMessage = null;
1122 mIcon = null;
1123 }
1124
1125 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1126 mIconResId = iconResId;
1127 mMessageResId = 0;
1128 mMessage = message;
1129 mIcon = icon;
1130 }
1131
1132 public boolean isEnabled() {
1133 return true;
1134 }
1135
1136 public String getStatus() {
1137 return null;
1138 }
1139
1140 abstract public void onPress();
1141
1142 public CharSequence getLabelForAccessibility(Context context) {
1143 if (mMessage != null) {
1144 return mMessage;
1145 } else {
1146 return context.getString(mMessageResId);
1147 }
1148 }
1149
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001150 protected int getActionLayoutId(Context context) {
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001151 return com.android.systemui.R.layout.global_actions_grid_item;
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001152 }
1153
Jason Monk361915c2017-03-21 20:33:59 -04001154 public View create(
1155 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001156 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001157 false);
Jason Monk361915c2017-03-21 20:33:59 -04001158
1159 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1160 TextView messageView = (TextView) v.findViewById(R.id.message);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001161 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001162
1163 TextView statusView = (TextView) v.findViewById(R.id.status);
1164 final String status = getStatus();
1165 if (!TextUtils.isEmpty(status)) {
1166 statusView.setText(status);
1167 } else {
1168 statusView.setVisibility(View.GONE);
1169 }
1170 if (mIcon != null) {
1171 icon.setImageDrawable(mIcon);
1172 icon.setScaleType(ScaleType.CENTER_CROP);
1173 } else if (mIconResId != 0) {
1174 icon.setImageDrawable(context.getDrawable(mIconResId));
1175 }
1176 if (mMessage != null) {
1177 messageView.setText(mMessage);
1178 } else {
1179 messageView.setText(mMessageResId);
1180 }
1181
1182 return v;
1183 }
1184 }
1185
1186 /**
1187 * A toggle action knows whether it is on or off, and displays an icon
1188 * and status message accordingly.
1189 */
1190 private static abstract class ToggleAction implements Action {
1191
1192 enum State {
1193 Off(false),
1194 TurningOn(true),
1195 TurningOff(true),
1196 On(false);
1197
1198 private final boolean inTransition;
1199
1200 State(boolean intermediate) {
1201 inTransition = intermediate;
1202 }
1203
1204 public boolean inTransition() {
1205 return inTransition;
1206 }
1207 }
1208
1209 protected State mState = State.Off;
1210
1211 // prefs
1212 protected int mEnabledIconResId;
1213 protected int mDisabledIconResid;
1214 protected int mMessageResId;
1215 protected int mEnabledStatusMessageResId;
1216 protected int mDisabledStatusMessageResId;
1217
1218 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001219 * @param enabledIconResId The icon for when this action is on.
1220 * @param disabledIconResid The icon for when this action is off.
1221 * @param message The general information message, e.g 'Silent Mode'
1222 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001223 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1224 */
1225 public ToggleAction(int enabledIconResId,
1226 int disabledIconResid,
1227 int message,
1228 int enabledStatusMessageResId,
1229 int disabledStatusMessageResId) {
1230 mEnabledIconResId = enabledIconResId;
1231 mDisabledIconResid = disabledIconResid;
1232 mMessageResId = message;
1233 mEnabledStatusMessageResId = enabledStatusMessageResId;
1234 mDisabledStatusMessageResId = disabledStatusMessageResId;
1235 }
1236
1237 /**
1238 * Override to make changes to resource IDs just before creating the
1239 * View.
1240 */
1241 void willCreate() {
1242
1243 }
1244
1245 @Override
1246 public CharSequence getLabelForAccessibility(Context context) {
1247 return context.getString(mMessageResId);
1248 }
1249
1250 public View create(Context context, View convertView, ViewGroup parent,
1251 LayoutInflater inflater) {
1252 willCreate();
1253
1254 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001255 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001256
1257 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1258 TextView messageView = (TextView) v.findViewById(R.id.message);
1259 TextView statusView = (TextView) v.findViewById(R.id.status);
1260 final boolean enabled = isEnabled();
1261
1262 if (messageView != null) {
1263 messageView.setText(mMessageResId);
1264 messageView.setEnabled(enabled);
Aaron Heuckroth3f2d8b52019-04-05 13:27:51 -04001265 messageView.setSelected(true); // necessary for marquee to work
Jason Monk361915c2017-03-21 20:33:59 -04001266 }
1267
1268 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1269 if (icon != null) {
1270 icon.setImageDrawable(context.getDrawable(
1271 (on ? mEnabledIconResId : mDisabledIconResid)));
1272 icon.setEnabled(enabled);
1273 }
1274
1275 if (statusView != null) {
1276 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1277 statusView.setVisibility(View.VISIBLE);
1278 statusView.setEnabled(enabled);
1279 }
1280 v.setEnabled(enabled);
1281
1282 return v;
1283 }
1284
1285 public final void onPress() {
1286 if (mState.inTransition()) {
1287 Log.w(TAG, "shouldn't be able to toggle when in transition");
1288 return;
1289 }
1290
1291 final boolean nowOn = !(mState == State.On);
1292 onToggle(nowOn);
1293 changeStateFromPress(nowOn);
1294 }
1295
1296 public boolean isEnabled() {
1297 return !mState.inTransition();
1298 }
1299
1300 /**
1301 * Implementations may override this if their state can be in on of the intermediate
1302 * states until some notification is received (e.g airplane mode is 'turning off' until
1303 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001304 *
Jason Monk361915c2017-03-21 20:33:59 -04001305 * @param buttonOn Whether the button was turned on or off
1306 */
1307 protected void changeStateFromPress(boolean buttonOn) {
1308 mState = buttonOn ? State.On : State.Off;
1309 }
1310
1311 abstract void onToggle(boolean on);
1312
1313 public void updateState(State state) {
1314 mState = state;
1315 }
1316 }
1317
1318 private class SilentModeToggleAction extends ToggleAction {
1319 public SilentModeToggleAction() {
1320 super(R.drawable.ic_audio_vol_mute,
1321 R.drawable.ic_audio_vol,
1322 R.string.global_action_toggle_silent_mode,
1323 R.string.global_action_silent_mode_on_status,
1324 R.string.global_action_silent_mode_off_status);
1325 }
1326
1327 void onToggle(boolean on) {
1328 if (on) {
1329 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1330 } else {
1331 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1332 }
1333 }
1334
1335 public boolean showDuringKeyguard() {
1336 return true;
1337 }
1338
1339 public boolean showBeforeProvisioning() {
1340 return false;
1341 }
1342 }
1343
1344 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1345
Jason Monk16fbd9d2017-04-27 14:28:49 -04001346 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001347
1348 private final AudioManager mAudioManager;
1349 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001350
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001351 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001352 mAudioManager = audioManager;
1353 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001354 }
1355
1356 private int ringerModeToIndex(int ringerMode) {
1357 // They just happen to coincide
1358 return ringerMode;
1359 }
1360
1361 private int indexToRingerMode(int index) {
1362 // They just happen to coincide
1363 return index;
1364 }
1365
1366 @Override
1367 public CharSequence getLabelForAccessibility(Context context) {
1368 return null;
1369 }
1370
1371 public View create(Context context, View convertView, ViewGroup parent,
1372 LayoutInflater inflater) {
1373 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1374
1375 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1376 for (int i = 0; i < 3; i++) {
1377 View itemView = v.findViewById(ITEM_IDS[i]);
1378 itemView.setSelected(selectedIndex == i);
1379 // Set up click handler
1380 itemView.setTag(i);
1381 itemView.setOnClickListener(this);
1382 }
1383 return v;
1384 }
1385
1386 public void onPress() {
1387 }
1388
1389 public boolean showDuringKeyguard() {
1390 return true;
1391 }
1392
1393 public boolean showBeforeProvisioning() {
1394 return false;
1395 }
1396
1397 public boolean isEnabled() {
1398 return true;
1399 }
1400
1401 void willCreate() {
1402 }
1403
1404 public void onClick(View v) {
1405 if (!(v.getTag() instanceof Integer)) return;
1406
1407 int index = (Integer) v.getTag();
1408 mAudioManager.setRingerMode(indexToRingerMode(index));
1409 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1410 }
1411 }
1412
1413 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1414 public void onReceive(Context context, Intent intent) {
1415 String action = intent.getAction();
1416 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1417 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1418 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1419 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001420 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001421 }
Jayachandran C142eae02019-12-13 19:29:20 -08001422 } else if (TelephonyManager.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
Jason Monk361915c2017-03-21 20:33:59 -04001423 // Airplane mode can be changed after ECM exits if airplane toggle button
1424 // is pressed during ECM mode
1425 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1426 mIsWaitingForEcmExit) {
1427 mIsWaitingForEcmExit = false;
1428 changeAirplaneModeSystemSetting(true);
1429 }
1430 }
1431 }
1432 };
1433
1434 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1435 @Override
1436 public void onServiceStateChanged(ServiceState serviceState) {
1437 if (!mHasTelephony) return;
1438 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1439 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1440 mAirplaneModeOn.updateState(mAirplaneState);
1441 mAdapter.notifyDataSetChanged();
1442 }
1443 };
1444
1445 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1446 @Override
1447 public void onReceive(Context context, Intent intent) {
1448 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1449 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1450 }
1451 }
1452 };
1453
1454 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1455 @Override
1456 public void onChange(boolean selfChange) {
1457 onAirplaneModeChanged();
1458 }
1459 };
1460
1461 private static final int MESSAGE_DISMISS = 0;
1462 private static final int MESSAGE_REFRESH = 1;
1463 private static final int MESSAGE_SHOW = 2;
1464 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1465
1466 private Handler mHandler = new Handler() {
1467 public void handleMessage(Message msg) {
1468 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001469 case MESSAGE_DISMISS:
1470 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001471 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1472 mDialog.dismissImmediately();
1473 } else {
1474 mDialog.dismiss();
1475 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001476 mDialog = null;
1477 }
1478 break;
1479 case MESSAGE_REFRESH:
1480 refreshSilentMode();
1481 mAdapter.notifyDataSetChanged();
1482 break;
1483 case MESSAGE_SHOW:
1484 handleShow();
1485 break;
Jason Monk361915c2017-03-21 20:33:59 -04001486 }
1487 }
1488 };
1489
1490 private void onAirplaneModeChanged() {
1491 // Let the service state callbacks handle the state.
1492 if (mHasTelephony) return;
1493
1494 boolean airplaneModeOn = Settings.Global.getInt(
1495 mContext.getContentResolver(),
1496 Settings.Global.AIRPLANE_MODE_ON,
1497 0) == 1;
1498 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1499 mAirplaneModeOn.updateState(mAirplaneState);
1500 }
1501
1502 /**
1503 * Change the airplane mode system setting
1504 */
1505 private void changeAirplaneModeSystemSetting(boolean on) {
1506 Settings.Global.putInt(
1507 mContext.getContentResolver(),
1508 Settings.Global.AIRPLANE_MODE_ON,
1509 on ? 1 : 0);
1510 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1511 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1512 intent.putExtra("state", on);
1513 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1514 if (!mHasTelephony) {
1515 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1516 }
1517 }
1518
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001519 private static final class ActionsDialog extends Dialog implements DialogInterface,
1520 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001521
Jason Monk16fbd9d2017-04-27 14:28:49 -04001522 private final Context mContext;
1523 private final MyAdapter mAdapter;
Steve Elliott53f12ae2019-05-13 17:14:15 -04001524 private final IStatusBarService mStatusBarService;
1525 private final IBinder mToken = new Binder();
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001526 private MultiListLayout mGlobalActionsLayout;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001527 private Drawable mBackgroundDrawable;
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001528 private final SysuiColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001529 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001530 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001531 private boolean mShowing;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001532 private float mScrimAlpha;
Steve Elliott48f75db2019-05-03 15:03:38 -04001533 private ResetOrientationData mResetOrientationData;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001534 private boolean mHadTopUi;
1535 private final StatusBarWindowController mStatusBarWindowController;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001536
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001537 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001538 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001539 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001540 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001541 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001542 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Steve Elliott53f12ae2019-05-13 17:14:15 -04001543 mStatusBarService = Dependency.get(IStatusBarService.class);
Steve Elliottff2c0d92019-07-30 15:09:54 -04001544 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001545
1546 // Window initialization
1547 Window window = getWindow();
1548 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001549 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1550 window.getDecorView();
1551 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1552 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1553 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1554 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1555 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001556 window.addFlags(
1557 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001558 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001559 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001560 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1561 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1562 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001563 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
Tiger Huang4a7835f2019-11-06 00:07:56 +08001564 window.setFitWindowInsetsTypes(0 /* types */);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001565 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001566
1567 mPanelController = plugin;
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001568 initializeLayout();
Steve Elliott9b87a442019-03-05 10:24:16 -05001569 }
1570
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001571 private boolean shouldUsePanel() {
Aran Ink3f594552019-05-13 14:29:35 -04001572 return mPanelController != null && mPanelController.getPanelContent() != null;
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001573 }
1574
1575 private void initializePanel() {
Steve Elliott48f75db2019-05-03 15:03:38 -04001576 int rotation = RotationUtils.getRotation(mContext);
1577 boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
1578 if (rotation != RotationUtils.ROTATION_NONE) {
1579 if (rotationLocked) {
1580 if (mResetOrientationData == null) {
1581 mResetOrientationData = new ResetOrientationData();
1582 mResetOrientationData.locked = true;
1583 mResetOrientationData.rotation = rotation;
1584 }
1585
1586 // Unlock rotation, so user can choose to rotate to portrait to see the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001587 // This call is posted so that the rotation does not change until post-layout,
1588 // otherwise onConfigurationChanged() may not get invoked.
1589 mGlobalActionsLayout.post(() ->
1590 RotationPolicy.setRotationLockAtAngle(
1591 mContext, false, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001592 }
1593 } else {
1594 if (!rotationLocked) {
1595 if (mResetOrientationData == null) {
1596 mResetOrientationData = new ResetOrientationData();
1597 mResetOrientationData.locked = false;
1598 }
1599
1600 // Lock to portrait, so the user doesn't accidentally hide the panel.
Steve Elliotte05817f2019-05-17 14:39:53 -04001601 // This call is posted so that the rotation does not change until post-layout,
1602 // otherwise onConfigurationChanged() may not get invoked.
1603 mGlobalActionsLayout.post(() ->
1604 RotationPolicy.setRotationLockAtAngle(
1605 mContext, true, RotationUtils.ROTATION_NONE));
Steve Elliott48f75db2019-05-03 15:03:38 -04001606 }
1607
Steve Elliott53f12ae2019-05-13 17:14:15 -04001608 // Disable rotation suggestions, if enabled
1609 setRotationSuggestionsEnabled(false);
1610
Steve Elliott300b48f2019-05-29 14:13:50 -04001611 FrameLayout panelContainer =
1612 findViewById(com.android.systemui.R.id.global_actions_panel_container);
Steve Elliott48f75db2019-05-03 15:03:38 -04001613 FrameLayout.LayoutParams panelParams =
1614 new FrameLayout.LayoutParams(
1615 FrameLayout.LayoutParams.MATCH_PARENT,
Steve Elliott300b48f2019-05-29 14:13:50 -04001616 FrameLayout.LayoutParams.MATCH_PARENT);
Steve Elliott48f75db2019-05-03 15:03:38 -04001617 panelContainer.addView(mPanelController.getPanelContent(), panelParams);
Steve Elliott86ef6282019-05-08 14:45:04 -04001618 mBackgroundDrawable = mPanelController.getBackgroundDrawable();
1619 mScrimAlpha = 1f;
Steve Elliott48f75db2019-05-03 15:03:38 -04001620 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001621 }
1622
1623 private void initializeLayout() {
1624 setContentView(getGlobalActionsLayoutId(mContext));
Steve Elliott300b48f2019-05-29 14:13:50 -04001625 fixNavBarClipping();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001626 mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001627 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Steve Elliott300b48f2019-05-29 14:13:50 -04001628 ((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001629 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001630 @Override
1631 public boolean dispatchPopulateAccessibilityEvent(
1632 View host, AccessibilityEvent event) {
1633 // Populate the title here, just as Activity does
1634 event.getText().add(mContext.getString(R.string.global_actions));
1635 return true;
1636 }
1637 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001638 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001639 mGlobalActionsLayout.setAdapter(mAdapter);
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001640
Steve Elliott86ef6282019-05-08 14:45:04 -04001641 if (shouldUsePanel()) {
Steve Elliottc59eb342019-04-23 16:59:06 -04001642 initializePanel();
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001643 }
Steve Elliott86ef6282019-05-08 14:45:04 -04001644 if (mBackgroundDrawable == null) {
1645 mBackgroundDrawable = new ScrimDrawable();
1646 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
1647 }
Steve Elliotte9e7e1f2019-03-18 15:54:59 -04001648 getWindow().setBackgroundDrawable(mBackgroundDrawable);
Steve Elliott9b87a442019-03-05 10:24:16 -05001649 }
1650
Steve Elliott300b48f2019-05-29 14:13:50 -04001651 private void fixNavBarClipping() {
1652 ViewGroup content = findViewById(android.R.id.content);
1653 content.setClipChildren(false);
1654 content.setClipToPadding(false);
1655 ViewGroup contentParent = (ViewGroup) content.getParent();
1656 contentParent.setClipChildren(false);
1657 contentParent.setClipToPadding(false);
1658 }
1659
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001660 private int getGlobalActionsLayoutId(Context context) {
Steve Elliottdc165632019-05-23 14:26:31 -04001661 int rotation = RotationUtils.getRotation(context);
1662 boolean useGridLayout = isForceGridEnabled(context)
1663 || (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
1664 if (rotation == RotationUtils.ROTATION_SEASCAPE) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001665 if (useGridLayout) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001666 return com.android.systemui.R.layout.global_actions_grid_seascape;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001667 } else {
1668 return com.android.systemui.R.layout.global_actions_column_seascape;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001669 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001670 } else {
1671 if (useGridLayout) {
1672 return com.android.systemui.R.layout.global_actions_grid;
1673 } else {
1674 return com.android.systemui.R.layout.global_actions_column;
1675 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001676 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001677 }
1678
Jason Monk361915c2017-03-21 20:33:59 -04001679 @Override
1680 protected void onStart() {
1681 super.setCanceledOnTouchOutside(true);
1682 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001683 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001684
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001685 if (mBackgroundDrawable instanceof ScrimDrawable) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001686 mColorExtractor.addOnColorsChangedListener(this);
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001687 GradientColors colors = mColorExtractor.getNeutralColors();
Steve Elliott9b87a442019-03-05 10:24:16 -05001688 updateColors(colors, false /* animate */);
1689 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001690 }
1691
1692 /**
1693 * Updates background and system bars according to current GradientColors.
1694 * @param colors Colors and hints to use.
1695 * @param animate Interpolates gradient if true, just sets otherwise.
1696 */
1697 private void updateColors(GradientColors colors, boolean animate) {
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001698 if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001699 return;
1700 }
Lucas Dupin2bd3af62019-03-25 17:44:28 -07001701 ((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001702 View decorView = getWindow().getDecorView();
1703 if (colors.supportsDarkText()) {
1704 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001705 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001706 } else {
1707 decorView.setSystemUiVisibility(0);
1708 }
Jason Monk361915c2017-03-21 20:33:59 -04001709 }
1710
1711 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001712 protected void onStop() {
1713 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001714 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001715 }
1716
1717 @Override
1718 public void show() {
1719 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001720 mShowing = true;
Steve Elliottff2c0d92019-07-30 15:09:54 -04001721 mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
1722 mStatusBarWindowController.setForceHasTopUi(true);
Steve Elliott9b87a442019-03-05 10:24:16 -05001723 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001724 mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
1725 mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001726 mGlobalActionsLayout.setAlpha(0);
1727 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001728 .alpha(1)
1729 .translationX(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001730 .translationY(0)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001731 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001732 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001733 .setUpdateListener(animation -> {
1734 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001735 * mScrimAlpha * 255);
1736 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001737 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001738 .start();
1739 }
1740
1741 @Override
1742 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001743 if (!mShowing) {
1744 return;
1745 }
1746 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001747 mGlobalActionsLayout.setTranslationX(0);
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001748 mGlobalActionsLayout.setTranslationY(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001749 mGlobalActionsLayout.setAlpha(1);
1750 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001751 .alpha(0)
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001752 .translationX(mGlobalActionsLayout.getAnimationOffsetX())
1753 .translationY(mGlobalActionsLayout.getAnimationOffsetY())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001754 .setDuration(300)
Steve Elliottff2c0d92019-07-30 15:09:54 -04001755 .withEndAction(this::completeDismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001756 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001757 .setUpdateListener(animation -> {
1758 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001759 * mScrimAlpha * 255);
1760 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001761 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001762 .start();
Steve Elliott48f75db2019-05-03 15:03:38 -04001763 dismissPanel();
1764 resetOrientation();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001765 }
1766
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001767 void dismissImmediately() {
Beverly526d2d62018-08-15 12:55:33 -04001768 mShowing = false;
Steve Elliott48f75db2019-05-03 15:03:38 -04001769 dismissPanel();
1770 resetOrientation();
Steve Elliottff2c0d92019-07-30 15:09:54 -04001771 completeDismiss();
1772 }
1773
1774 private void completeDismiss() {
1775 mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
1776 super.dismiss();
Steve Elliott48f75db2019-05-03 15:03:38 -04001777 }
1778
1779 private void dismissPanel() {
Steve Elliott2d206d32019-03-14 16:30:33 -04001780 if (mPanelController != null) {
1781 mPanelController.onDismissed();
1782 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001783 }
1784
Steve Elliott53f12ae2019-05-13 17:14:15 -04001785 private void setRotationSuggestionsEnabled(boolean enabled) {
1786 try {
1787 final int userId = Binder.getCallingUserHandle().getIdentifier();
1788 final int what = enabled
1789 ? StatusBarManager.DISABLE2_NONE
1790 : StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
1791 mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
1792 } catch (RemoteException ex) {
1793 throw ex.rethrowFromSystemServer();
1794 }
1795 }
1796
Steve Elliott48f75db2019-05-03 15:03:38 -04001797 private void resetOrientation() {
1798 if (mResetOrientationData != null) {
1799 RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
1800 mResetOrientationData.rotation);
1801 }
Steve Elliott53f12ae2019-05-13 17:14:15 -04001802 setRotationSuggestionsEnabled(true);
Steve Elliott48f75db2019-05-03 15:03:38 -04001803 }
1804
Jason Monk361915c2017-03-21 20:33:59 -04001805 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001806 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001807 if (mKeyguardShowing) {
1808 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001809 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1810 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001811 }
1812 } else {
1813 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001814 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1815 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001816 }
1817 }
1818 }
1819
1820 public void setKeyguardShowing(boolean keyguardShowing) {
1821 mKeyguardShowing = keyguardShowing;
1822 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001823
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001824 public void refreshDialog() {
1825 initializeLayout();
1826 mGlobalActionsLayout.updateList();
1827 }
1828
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001829 public void onRotate(int from, int to) {
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -04001830 if (mShowing) {
Aaron Heuckroth4ef3a542019-04-01 14:17:23 -04001831 refreshDialog();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001832 }
1833 }
Steve Elliott48f75db2019-05-03 15:03:38 -04001834
1835 private static class ResetOrientationData {
1836 public boolean locked;
1837 public int rotation;
1838 }
Jason Monk361915c2017-03-21 20:33:59 -04001839 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001840
1841 /**
Aran Ink93dd25c2019-05-09 14:42:14 -04001842 * Determines whether or not debug mode has been activated for the Global Actions Panel.
1843 */
1844 private static boolean isPanelDebugModeEnabled(Context context) {
1845 return Settings.Secure.getInt(context.getContentResolver(),
1846 Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
1847 }
1848
1849 /**
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001850 * Determines whether or not the Global Actions menu should be forced to
1851 * use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001852 */
Aaron Heuckrothc7926612019-04-02 14:21:57 -04001853 private static boolean isForceGridEnabled(Context context) {
Aran Ink93dd25c2019-05-09 14:42:14 -04001854 return isPanelDebugModeEnabled(context);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001855 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001856
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001857
1858 /**
1859 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1860 */
1861 private static boolean shouldUseSeparatedView() {
1862 return true;
1863 }
Jason Monk361915c2017-03-21 20:33:59 -04001864}