blob: 7ed3a8065bc51945f66aa18aa0f1a4fae3b5c591 [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;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070028import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000029import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010030import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040031import android.content.BroadcastReceiver;
32import android.content.Context;
33import android.content.DialogInterface;
34import android.content.Intent;
35import android.content.IntentFilter;
36import android.content.pm.UserInfo;
37import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070038import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040039import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
Jason Monk361915c2017-03-21 20:33:59 -040042import android.os.Handler;
43import android.os.Message;
44import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.os.UserHandle;
48import android.os.UserManager;
49import android.os.Vibrator;
50import android.provider.Settings;
51import android.service.dreams.DreamService;
52import android.service.dreams.IDreamManager;
53import android.telephony.PhoneStateListener;
54import android.telephony.ServiceState;
55import android.telephony.TelephonyManager;
56import android.text.TextUtils;
57import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080058import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040059import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070060import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040061import android.view.LayoutInflater;
62import android.view.View;
63import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070064import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.WindowManager;
66import android.view.WindowManagerGlobal;
67import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050068import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.widget.ImageView;
70import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.TextView;
72
Charles He9851a8d2017-10-10 17:31:30 +010073import com.android.internal.R;
74import com.android.internal.colorextraction.ColorExtractor;
75import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070076import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.logging.MetricsLogger;
78import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
79import com.android.internal.telephony.TelephonyIntents;
80import com.android.internal.telephony.TelephonyProperties;
81import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040082import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050083import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010084import com.android.internal.widget.LockPatternUtils;
85import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050087import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050088import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.systemui.colorextraction.SysuiColorExtractor;
Steve Elliott4c868852019-03-14 16:25:41 -040090import com.android.systemui.plugins.ActivityStarter;
Charles He9851a8d2017-10-10 17:31:30 +010091import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050092import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050093import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070094import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080095import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050096import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050097import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070098
Jason Monk361915c2017-03-21 20:33:59 -040099import java.util.ArrayList;
100import java.util.List;
101
102/**
103 * Helper to show the global actions dialog. Each item is an {@link Action} that
104 * may show depending on whether the keyguard is showing, and whether the device
105 * is provisioned.
106 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500107public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
108 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400109
110 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
111 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700112 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400113
114 private static final String TAG = "GlobalActionsDialog";
115
116 private static final boolean SHOW_SILENT_TOGGLE = true;
117
118 /* Valid settings for global actions keys.
119 * see config.xml config_globalActionList */
120 private static final String GLOBAL_ACTION_KEY_POWER = "power";
121 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
122 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
123 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
124 private static final String GLOBAL_ACTION_KEY_USERS = "users";
125 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
126 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
127 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
128 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
129 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000130 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800131 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500132 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400133
134 private final Context mContext;
135 private final GlobalActionsManager mWindowManagerFuncs;
136 private final AudioManager mAudioManager;
137 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000138 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800139 private final LockPatternUtils mLockPatternUtils;
140 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400141
142 private ArrayList<Action> mItems;
143 private ActionsDialog mDialog;
144
145 private Action mSilentModeAction;
146 private ToggleAction mAirplaneModeOn;
147
148 private MyAdapter mAdapter;
149
150 private boolean mKeyguardShowing = false;
151 private boolean mDeviceProvisioned = false;
152 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
153 private boolean mIsWaitingForEcmExit = false;
154 private boolean mHasTelephony;
155 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000156 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800157 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400158 private final boolean mShowSilentToggle;
159 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500160 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400161 private final ScreenRecordHelper mScreenRecordHelper;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400162 private final ActivityStarter mActivityStarter;
163 private GlobalActionsPanelPlugin mPanelPlugin;
Steve Elliott9b87a442019-03-05 10:24:16 -0500164
Jason Monk361915c2017-03-21 20:33:59 -0400165 /**
166 * @param context everything needs a context :(
167 */
168 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700169 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400170 mWindowManagerFuncs = windowManagerFuncs;
171 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
172 mDreamManager = IDreamManager.Stub.asInterface(
173 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000174 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
175 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800176 mLockPatternUtils = new LockPatternUtils(mContext);
177 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400178
179 // receive broadcasts
180 IntentFilter filter = new IntentFilter();
181 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
182 filter.addAction(Intent.ACTION_SCREEN_OFF);
183 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
184 context.registerReceiver(mBroadcastReceiver, filter);
185
186 ConnectivityManager cm = (ConnectivityManager)
187 context.getSystemService(Context.CONNECTIVITY_SERVICE);
188 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
189
190 // get notified of phone state changes
191 TelephonyManager telephonyManager =
192 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
193 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
194 mContext.getContentResolver().registerContentObserver(
195 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
196 mAirplaneModeObserver);
197 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
198 mHasVibrator = vibrator != null && vibrator.hasVibrator();
199
200 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
201 R.bool.config_useFixedVolume);
202
203 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500204 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400205 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700206
207 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500208
Steve Elliott4c868852019-03-14 16:25:41 -0400209 mActivityStarter = Dependency.get(ActivityStarter.class);
Jason Monk361915c2017-03-21 20:33:59 -0400210 }
211
212 /**
213 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400214 *
Jason Monk361915c2017-03-21 20:33:59 -0400215 * @param keyguardShowing True if keyguard is showing
216 */
Steve Elliotta3f5207922019-03-18 13:37:22 -0400217 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
218 GlobalActionsPanelPlugin panelPlugin) {
Jason Monk361915c2017-03-21 20:33:59 -0400219 mKeyguardShowing = keyguardShowing;
220 mDeviceProvisioned = isDeviceProvisioned;
Steve Elliotta3f5207922019-03-18 13:37:22 -0400221 mPanelPlugin = panelPlugin;
Jason Monk361915c2017-03-21 20:33:59 -0400222 if (mDialog != null) {
223 mDialog.dismiss();
224 mDialog = null;
225 // Show delayed, so that the dismiss of the previous dialog completes
226 mHandler.sendEmptyMessage(MESSAGE_SHOW);
227 } else {
228 handleShow();
229 }
230 }
231
Charles He9851a8d2017-10-10 17:31:30 +0100232 /**
233 * Dismiss the global actions dialog, if it's currently shown
234 */
235 public void dismissDialog() {
236 mHandler.removeMessages(MESSAGE_DISMISS);
237 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
238 }
239
Jason Monk361915c2017-03-21 20:33:59 -0400240 private void awakenIfNecessary() {
241 if (mDreamManager != null) {
242 try {
243 if (mDreamManager.isDreaming()) {
244 mDreamManager.awaken();
245 }
246 } catch (RemoteException e) {
247 // we tried
248 }
249 }
250 }
251
252 private void handleShow() {
253 awakenIfNecessary();
254 mDialog = createDialog();
255 prepareDialog();
256
257 // If we only have 1 item and it's a simple press action, just do this action.
258 if (mAdapter.getCount() == 1
259 && mAdapter.getItem(0) instanceof SinglePressAction
260 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
261 ((SinglePressAction) mAdapter.getItem(0)).onPress();
262 } else {
263 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
264 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100265 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400266 mDialog.getWindow().setAttributes(attrs);
267 mDialog.show();
268 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400269 }
270 }
271
272 /**
273 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400274 *
Jason Monk361915c2017-03-21 20:33:59 -0400275 * @return A new dialog.
276 */
277 private ActionsDialog createDialog() {
278 // Simple toggle style if there's no vibrator, otherwise use a tri-state
279 if (!mHasVibrator) {
280 mSilentModeAction = new SilentModeToggleAction();
281 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700282 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400283 }
284 mAirplaneModeOn = new ToggleAction(
285 R.drawable.ic_lock_airplane_mode,
286 R.drawable.ic_lock_airplane_mode_off,
287 R.string.global_actions_toggle_airplane_mode,
288 R.string.global_actions_airplane_mode_on_status,
289 R.string.global_actions_airplane_mode_off_status) {
290
291 void onToggle(boolean on) {
292 if (mHasTelephony && Boolean.parseBoolean(
293 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
294 mIsWaitingForEcmExit = true;
295 // Launch ECM exit dialog
296 Intent ecmDialogIntent =
297 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
298 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
299 mContext.startActivity(ecmDialogIntent);
300 } else {
301 changeAirplaneModeSystemSetting(on);
302 }
303 }
304
305 @Override
306 protected void changeStateFromPress(boolean buttonOn) {
307 if (!mHasTelephony) return;
308
309 // In ECM mode airplane state cannot be changed
310 if (!(Boolean.parseBoolean(
311 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
312 mState = buttonOn ? State.TurningOn : State.TurningOff;
313 mAirplaneState = mState;
314 }
315 }
316
317 public boolean showDuringKeyguard() {
318 return true;
319 }
320
321 public boolean showBeforeProvisioning() {
322 return false;
323 }
324 };
325 onAirplaneModeChanged();
326
327 mItems = new ArrayList<Action>();
328 String[] defaultActions = mContext.getResources().getStringArray(
329 R.array.config_globalActionsList);
330
331 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000332 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800333 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400334 for (int i = 0; i < defaultActions.length; i++) {
335 String actionKey = defaultActions[i];
336 if (addedKeys.contains(actionKey)) {
337 // If we already have added this, don't add it again.
338 continue;
339 }
340 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
341 mItems.add(new PowerAction());
342 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
343 mItems.add(mAirplaneModeOn);
344 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
345 if (Settings.Global.getInt(mContext.getContentResolver(),
346 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
347 mItems.add(new BugReportAction());
348 }
349 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
350 if (mShowSilentToggle) {
351 mItems.add(mSilentModeAction);
352 }
353 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
354 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
355 addUsersToMenu(mItems);
356 }
357 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
358 mItems.add(getSettingsAction());
359 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700360 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
361 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800362 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700363 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800364 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700365 }
Jason Monk361915c2017-03-21 20:33:59 -0400366 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
367 mItems.add(getVoiceAssistAction());
368 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
369 mItems.add(getAssistAction());
370 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
371 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500372 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
373 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000374 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000375 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000376 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
377 mItems.add(new LogoutAction());
378 mHasLogoutButton = true;
379 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800380 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400381 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800382 mItems.add(new EmergencyDialerAction());
383 }
Jason Monk361915c2017-03-21 20:33:59 -0400384 } else {
385 Log.e(TAG, "Invalid global action key " + actionKey);
386 }
387 // Add here so we don't add more than one.
388 addedKeys.add(actionKey);
389 }
390
391 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400392 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400393 }
394
395 mAdapter = new MyAdapter();
396
Steve Elliott9b87a442019-03-05 10:24:16 -0500397 GlobalActionsPanelPlugin.PanelViewController panelViewController =
Steve Elliotta3f5207922019-03-18 13:37:22 -0400398 mPanelPlugin != null
399 ? mPanelPlugin.onPanelShown(
Steve Elliott4c868852019-03-14 16:25:41 -0400400 new GlobalActionsPanelPlugin.Callbacks() {
401 @Override
402 public void dismissGlobalActionsMenu() {
403 if (mDialog != null) {
404 mDialog.dismiss();
405 }
406 }
407
408 @Override
409 public void startPendingIntentDismissingKeyguard(
410 PendingIntent intent) {
411 mActivityStarter
412 .startPendingIntentDismissingKeyguard(intent);
413 }
414 })
Steve Elliott9b87a442019-03-05 10:24:16 -0500415 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400416 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400417 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700418 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400419
420 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800421 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400422
423 return dialog;
424 }
425
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800426 private boolean shouldDisplayLockdown() {
427 int userId = getCurrentUser().id;
428 // Lockdown is meaningless without a place to go.
429 if (!mKeyguardManager.isDeviceSecure(userId)) {
430 return false;
431 }
432
433 // Only show the lockdown button if the device isn't locked down (for whatever reason).
434 int state = mLockPatternUtils.getStrongAuthForUser(userId);
435 return (state == STRONG_AUTH_NOT_REQUIRED
436 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
437 }
438
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700439 @Override
440 public void onUiModeChanged() {
441 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
442 }
443
444 public void destroy() {
445 Dependency.get(ConfigurationController.class).removeCallback(this);
446 }
447
Jason Monk361915c2017-03-21 20:33:59 -0400448 private final class PowerAction extends SinglePressAction implements LongPressAction {
449 private PowerAction() {
450 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400451 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400452 }
453
454 @Override
455 public boolean onLongPress() {
456 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
457 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
458 mWindowManagerFuncs.reboot(true);
459 return true;
460 }
461 return false;
462 }
463
464 @Override
465 public boolean showDuringKeyguard() {
466 return true;
467 }
468
469 @Override
470 public boolean showBeforeProvisioning() {
471 return true;
472 }
473
474 @Override
475 public void onPress() {
476 // shutdown by making sure radio and power are handled accordingly.
477 mWindowManagerFuncs.shutdown();
478 }
479 }
480
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400481 private abstract class EmergencyAction extends SinglePressAction {
482 EmergencyAction(int iconResId, int messageResId) {
483 super(iconResId, messageResId);
484 }
485
486 @Override
487 public boolean shouldBeSeparated() {
488 return shouldUseSeparatedView();
489 }
490
491 @Override
492 public View create(
493 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
494 View v = super.create(context, convertView, parent, inflater);
495 int textColor;
496 if (shouldBeSeparated()) {
497 textColor = v.getResources().getColor(
498 com.android.systemui.R.color.global_actions_alert_text);
499 } else {
500 textColor = v.getResources().getColor(
501 com.android.systemui.R.color.global_actions_text);
502 }
503 TextView messageView = v.findViewById(R.id.message);
504 messageView.setTextColor(textColor);
505 ImageView icon = (ImageView) v.findViewById(R.id.icon);
506 icon.getDrawable().setTint(textColor);
507 return v;
508 }
509
510 @Override
511 public boolean showDuringKeyguard() {
512 return true;
513 }
514
515 @Override
516 public boolean showBeforeProvisioning() {
517 return true;
518 }
519 }
520
521 private class EmergencyAffordanceAction extends EmergencyAction {
522 EmergencyAffordanceAction() {
523 super(R.drawable.emergency_icon,
524 R.string.global_action_emergency);
525 }
526
527 @Override
528 public void onPress() {
529 mEmergencyAffordanceManager.performEmergencyCall();
530 }
531 }
532
533 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800534 private EmergencyDialerAction() {
535 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800536 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800537 }
538
539 @Override
540 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800541 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800542 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800543 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
544 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
545 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800546 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
547 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800548 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
549 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800550 }
551
Jason Monk361915c2017-03-21 20:33:59 -0400552 private final class RestartAction extends SinglePressAction implements LongPressAction {
553 private RestartAction() {
554 super(R.drawable.ic_restart, R.string.global_action_restart);
555 }
556
557 @Override
558 public boolean onLongPress() {
559 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
560 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
561 mWindowManagerFuncs.reboot(true);
562 return true;
563 }
564 return false;
565 }
566
567 @Override
568 public boolean showDuringKeyguard() {
569 return true;
570 }
571
572 @Override
573 public boolean showBeforeProvisioning() {
574 return true;
575 }
576
577 @Override
578 public void onPress() {
579 mWindowManagerFuncs.reboot(false);
580 }
581 }
582
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400583 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500584 public ScreenshotAction() {
585 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
586 }
587
588 @Override
589 public void onPress() {
590 // Add a little delay before executing, to give the
591 // dialog a chance to go away before it takes a
592 // screenshot.
593 // TODO: instead, omit global action dialog layer
594 mHandler.postDelayed(new Runnable() {
595 @Override
596 public void run() {
597 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
598 MetricsLogger.action(mContext,
599 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
600 }
601 }, 500);
602 }
603
604 @Override
605 public boolean showDuringKeyguard() {
606 return true;
607 }
608
609 @Override
610 public boolean showBeforeProvisioning() {
611 return false;
612 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400613
614 @Override
615 public boolean onLongPress() {
616 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
617 mScreenRecordHelper.launchRecordPrompt();
618 } else {
619 onPress();
620 }
621 return true;
622 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500623 }
624
Jason Monk361915c2017-03-21 20:33:59 -0400625 private class BugReportAction extends SinglePressAction implements LongPressAction {
626
627 public BugReportAction() {
628 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
629 }
630
631 @Override
632 public void onPress() {
633 // don't actually trigger the bugreport if we are running stability
634 // tests via monkey
635 if (ActivityManager.isUserAMonkey()) {
636 return;
637 }
638 // Add a little delay before executing, to give the
639 // dialog a chance to go away before it takes a
640 // screenshot.
641 mHandler.postDelayed(new Runnable() {
642 @Override
643 public void run() {
644 try {
645 // Take an "interactive" bugreport.
646 MetricsLogger.action(mContext,
647 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
648 ActivityManager.getService().requestBugReport(
649 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
650 } catch (RemoteException e) {
651 }
652 }
653 }, 500);
654 }
655
656 @Override
657 public boolean onLongPress() {
658 // don't actually trigger the bugreport if we are running stability
659 // tests via monkey
660 if (ActivityManager.isUserAMonkey()) {
661 return false;
662 }
663 try {
664 // Take a "full" bugreport.
665 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
666 ActivityManager.getService().requestBugReport(
667 ActivityManager.BUGREPORT_OPTION_FULL);
668 } catch (RemoteException e) {
669 }
670 return false;
671 }
672
673 public boolean showDuringKeyguard() {
674 return true;
675 }
676
677 @Override
678 public boolean showBeforeProvisioning() {
679 return false;
680 }
Jason Monk361915c2017-03-21 20:33:59 -0400681 }
682
Alex Chau04458852017-11-27 18:21:23 +0000683 private final class LogoutAction extends SinglePressAction {
684 private LogoutAction() {
685 super(R.drawable.ic_logout, R.string.global_action_logout);
686 }
687
688 @Override
689 public boolean showDuringKeyguard() {
690 return true;
691 }
692
693 @Override
694 public boolean showBeforeProvisioning() {
695 return false;
696 }
697
698 @Override
699 public void onPress() {
700 // Add a little delay before executing, to give the dialog a chance to go away before
701 // switching user
702 mHandler.postDelayed(() -> {
703 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000704 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000705 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000706 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000707 } catch (RemoteException re) {
708 Log.e(TAG, "Couldn't logout user " + re);
709 }
710 }, 500);
711 }
712 }
713
Jason Monk361915c2017-03-21 20:33:59 -0400714 private Action getSettingsAction() {
715 return new SinglePressAction(R.drawable.ic_settings,
716 R.string.global_action_settings) {
717
718 @Override
719 public void onPress() {
720 Intent intent = new Intent(Settings.ACTION_SETTINGS);
721 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
722 mContext.startActivity(intent);
723 }
724
725 @Override
726 public boolean showDuringKeyguard() {
727 return true;
728 }
729
730 @Override
731 public boolean showBeforeProvisioning() {
732 return true;
733 }
734 };
735 }
736
Jason Monk361915c2017-03-21 20:33:59 -0400737 private Action getAssistAction() {
738 return new SinglePressAction(R.drawable.ic_action_assist_focused,
739 R.string.global_action_assist) {
740 @Override
741 public void onPress() {
742 Intent intent = new Intent(Intent.ACTION_ASSIST);
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
759 private Action getVoiceAssistAction() {
760 return new SinglePressAction(R.drawable.ic_voice_search,
761 R.string.global_action_voice_assist) {
762 @Override
763 public void onPress() {
764 Intent intent = new Intent(Intent.ACTION_VOICE_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 getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400782 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400783 R.string.global_action_lockdown) {
784
785 @Override
786 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700787 new LockPatternUtils(mContext)
788 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
789 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400790 try {
791 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100792 // Lock profiles (if any) on the background thread.
793 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
794 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400795 } catch (RemoteException e) {
796 Log.e(TAG, "Error while trying to lock device.", e);
797 }
798 }
799
800 @Override
801 public boolean showDuringKeyguard() {
802 return true;
803 }
804
805 @Override
806 public boolean showBeforeProvisioning() {
807 return false;
808 }
809 };
810 }
811
Pavel Grafov059021b2018-05-02 13:44:46 +0100812 private void lockProfiles() {
813 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
814 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
815 final int currentUserId = getCurrentUser().id;
816 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
817 for (final int id : profileIds) {
818 if (id != currentUserId) {
819 tm.setDeviceLockedForUser(id, true);
820 }
821 }
822 }
823
Jason Monk361915c2017-03-21 20:33:59 -0400824 private UserInfo getCurrentUser() {
825 try {
826 return ActivityManager.getService().getCurrentUser();
827 } catch (RemoteException re) {
828 return null;
829 }
830 }
831
832 private boolean isCurrentUserOwner() {
833 UserInfo currentUser = getCurrentUser();
834 return currentUser == null || currentUser.isPrimary();
835 }
836
837 private void addUsersToMenu(ArrayList<Action> items) {
838 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
839 if (um.isUserSwitcherEnabled()) {
840 List<UserInfo> users = um.getUsers();
841 UserInfo currentUser = getCurrentUser();
842 for (final UserInfo user : users) {
843 if (user.supportsSwitchToByUser()) {
844 boolean isCurrentUser = currentUser == null
845 ? user.id == 0 : (currentUser.id == user.id);
846 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
847 : null;
848 SinglePressAction switchToUser = new SinglePressAction(
849 R.drawable.ic_menu_cc, icon,
850 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400851 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400852 public void onPress() {
853 try {
854 ActivityManager.getService().switchUser(user.id);
855 } catch (RemoteException re) {
856 Log.e(TAG, "Couldn't switch user " + re);
857 }
858 }
859
860 public boolean showDuringKeyguard() {
861 return true;
862 }
863
864 public boolean showBeforeProvisioning() {
865 return false;
866 }
867 };
868 items.add(switchToUser);
869 }
870 }
871 }
872 }
873
874 private void prepareDialog() {
875 refreshSilentMode();
876 mAirplaneModeOn.updateState(mAirplaneState);
877 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400878 if (mShowSilentToggle) {
879 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
880 mContext.registerReceiver(mRingerModeReceiver, filter);
881 }
882 }
883
884 private void refreshSilentMode() {
885 if (!mHasVibrator) {
886 final boolean silentModeOn =
887 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400888 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400889 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
890 }
891 }
892
893 /** {@inheritDoc} */
894 public void onDismiss(DialogInterface dialog) {
895 mWindowManagerFuncs.onGlobalActionsHidden();
896 if (mShowSilentToggle) {
897 try {
898 mContext.unregisterReceiver(mRingerModeReceiver);
899 } catch (IllegalArgumentException ie) {
900 // ignore this
901 Log.w(TAG, ie);
902 }
903 }
904 }
905
906 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800907 public void onShow(DialogInterface dialog) {
908 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
909 }
910
Jason Monk361915c2017-03-21 20:33:59 -0400911 /**
912 * The adapter used for the list within the global actions dialog, taking
913 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400914 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
915 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400916 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
917 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500918 public class MyAdapter extends MultiListAdapter {
919 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400920 public int getCount() {
921 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400922 for (int i = 0; i < mItems.size(); i++) {
923 final Action action = mItems.get(i);
924
925 if (mKeyguardShowing && !action.showDuringKeyguard()) {
926 continue;
927 }
928 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
929 continue;
930 }
931 count++;
932 }
933 return count;
934 }
935
936 @Override
937 public boolean isEnabled(int position) {
938 return getItem(position).isEnabled();
939 }
940
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500941 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400942 public ArrayList<Action> getSeparatedItems() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500943 ArrayList<Action> separatedActions = new ArrayList<Action>();
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400944 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500945 return separatedActions;
946 }
947 for (int i = 0; i < mItems.size(); i++) {
948 final Action action = mItems.get(i);
949 if (action.shouldBeSeparated()) {
950 separatedActions.add(action);
951 }
952 }
953 return separatedActions;
954 }
955
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500956 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400957 public ArrayList<Action> getListItems() {
958 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500959 return new ArrayList<Action>(mItems);
960 }
961 ArrayList<Action> listActions = new ArrayList<Action>();
962 for (int i = 0; i < mItems.size(); i++) {
963 final Action action = mItems.get(i);
964 if (!action.shouldBeSeparated()) {
965 listActions.add(action);
966 }
967 }
968 return listActions;
969 }
970
Jason Monk361915c2017-03-21 20:33:59 -0400971 @Override
972 public boolean areAllItemsEnabled() {
973 return false;
974 }
975
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500976 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400977 public Action getItem(int position) {
978
979 int filteredPos = 0;
980 for (int i = 0; i < mItems.size(); i++) {
981 final Action action = mItems.get(i);
982 if (mKeyguardShowing && !action.showDuringKeyguard()) {
983 continue;
984 }
985 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
986 continue;
987 }
988 if (filteredPos == position) {
989 return action;
990 }
991 filteredPos++;
992 }
993
994 throw new IllegalArgumentException("position " + position
995 + " out of range of showable actions"
996 + ", filtered count=" + getCount()
997 + ", keyguardshowing=" + mKeyguardShowing
998 + ", provisioned=" + mDeviceProvisioned);
999 }
1000
1001
1002 public long getItemId(int position) {
1003 return position;
1004 }
1005
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001006 @Override
Jason Monk361915c2017-03-21 20:33:59 -04001007 public View getView(int position, View convertView, ViewGroup parent) {
1008 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001009 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001010 // Everything but screenshot, the last item, gets white background.
1011 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001012 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001013 }
1014 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001015 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001016
1017 @Override
1018 public boolean onLongClickItem(int position) {
1019 final Action action = mAdapter.getItem(position);
1020 if (action instanceof LongPressAction) {
1021 mDialog.dismiss();
1022 return ((LongPressAction) action).onLongPress();
1023 }
1024 return false;
1025 }
1026
1027 @Override
1028 public void onClickItem(int position) {
1029 Action item = mAdapter.getItem(position);
1030 if (!(item instanceof SilentModeTriStateAction)) {
1031 mDialog.dismiss();
1032 }
1033 item.onPress();
1034 }
Jason Monk361915c2017-03-21 20:33:59 -04001035 }
1036
1037 // note: the scheme below made more sense when we were planning on having
1038 // 8 different things in the global actions dialog. seems overkill with
1039 // only 3 items now, but may as well keep this flexible approach so it will
1040 // be easy should someone decide at the last minute to include something
1041 // else, such as 'enable wifi', or 'enable bluetooth'
1042
1043 /**
1044 * What each item in the global actions dialog must be able to support.
1045 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001046 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001047 /**
1048 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001049 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001050 */
1051 CharSequence getLabelForAccessibility(Context context);
1052
1053 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1054
1055 void onPress();
1056
1057 /**
1058 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001059 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001060 */
1061 boolean showDuringKeyguard();
1062
1063 /**
1064 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001065 * device is provisioned.onlongpress
1066 *
Jason Monk361915c2017-03-21 20:33:59 -04001067 */
1068 boolean showBeforeProvisioning();
1069
1070 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001071
1072 default boolean shouldBeSeparated() {
1073 return false;
1074 }
Jason Monk361915c2017-03-21 20:33:59 -04001075 }
1076
1077 /**
1078 * An action that also supports long press.
1079 */
1080 private interface LongPressAction extends Action {
1081 boolean onLongPress();
1082 }
1083
1084 /**
1085 * A single press action maintains no state, just responds to a press
1086 * and takes an action.
1087 */
1088 private static abstract class SinglePressAction implements Action {
1089 private final int mIconResId;
1090 private final Drawable mIcon;
1091 private final int mMessageResId;
1092 private final CharSequence mMessage;
1093
1094 protected SinglePressAction(int iconResId, int messageResId) {
1095 mIconResId = iconResId;
1096 mMessageResId = messageResId;
1097 mMessage = null;
1098 mIcon = null;
1099 }
1100
1101 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1102 mIconResId = iconResId;
1103 mMessageResId = 0;
1104 mMessage = message;
1105 mIcon = icon;
1106 }
1107
1108 public boolean isEnabled() {
1109 return true;
1110 }
1111
1112 public String getStatus() {
1113 return null;
1114 }
1115
1116 abstract public void onPress();
1117
1118 public CharSequence getLabelForAccessibility(Context context) {
1119 if (mMessage != null) {
1120 return mMessage;
1121 } else {
1122 return context.getString(mMessageResId);
1123 }
1124 }
1125
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001126 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001127 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001128 return com.android.systemui.R.layout.global_actions_grid_item;
1129 }
1130 return com.android.systemui.R.layout.global_actions_item;
1131 }
1132
Jason Monk361915c2017-03-21 20:33:59 -04001133 public View create(
1134 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001135 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001136 false);
Jason Monk361915c2017-03-21 20:33:59 -04001137
1138 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1139 TextView messageView = (TextView) v.findViewById(R.id.message);
1140
1141 TextView statusView = (TextView) v.findViewById(R.id.status);
1142 final String status = getStatus();
1143 if (!TextUtils.isEmpty(status)) {
1144 statusView.setText(status);
1145 } else {
1146 statusView.setVisibility(View.GONE);
1147 }
1148 if (mIcon != null) {
1149 icon.setImageDrawable(mIcon);
1150 icon.setScaleType(ScaleType.CENTER_CROP);
1151 } else if (mIconResId != 0) {
1152 icon.setImageDrawable(context.getDrawable(mIconResId));
1153 }
1154 if (mMessage != null) {
1155 messageView.setText(mMessage);
1156 } else {
1157 messageView.setText(mMessageResId);
1158 }
1159
1160 return v;
1161 }
1162 }
1163
1164 /**
1165 * A toggle action knows whether it is on or off, and displays an icon
1166 * and status message accordingly.
1167 */
1168 private static abstract class ToggleAction implements Action {
1169
1170 enum State {
1171 Off(false),
1172 TurningOn(true),
1173 TurningOff(true),
1174 On(false);
1175
1176 private final boolean inTransition;
1177
1178 State(boolean intermediate) {
1179 inTransition = intermediate;
1180 }
1181
1182 public boolean inTransition() {
1183 return inTransition;
1184 }
1185 }
1186
1187 protected State mState = State.Off;
1188
1189 // prefs
1190 protected int mEnabledIconResId;
1191 protected int mDisabledIconResid;
1192 protected int mMessageResId;
1193 protected int mEnabledStatusMessageResId;
1194 protected int mDisabledStatusMessageResId;
1195
1196 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001197 * @param enabledIconResId The icon for when this action is on.
1198 * @param disabledIconResid The icon for when this action is off.
1199 * @param message The general information message, e.g 'Silent Mode'
1200 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001201 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1202 */
1203 public ToggleAction(int enabledIconResId,
1204 int disabledIconResid,
1205 int message,
1206 int enabledStatusMessageResId,
1207 int disabledStatusMessageResId) {
1208 mEnabledIconResId = enabledIconResId;
1209 mDisabledIconResid = disabledIconResid;
1210 mMessageResId = message;
1211 mEnabledStatusMessageResId = enabledStatusMessageResId;
1212 mDisabledStatusMessageResId = disabledStatusMessageResId;
1213 }
1214
1215 /**
1216 * Override to make changes to resource IDs just before creating the
1217 * View.
1218 */
1219 void willCreate() {
1220
1221 }
1222
1223 @Override
1224 public CharSequence getLabelForAccessibility(Context context) {
1225 return context.getString(mMessageResId);
1226 }
1227
1228 public View create(Context context, View convertView, ViewGroup parent,
1229 LayoutInflater inflater) {
1230 willCreate();
1231
1232 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001233 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001234
1235 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1236 TextView messageView = (TextView) v.findViewById(R.id.message);
1237 TextView statusView = (TextView) v.findViewById(R.id.status);
1238 final boolean enabled = isEnabled();
1239
1240 if (messageView != null) {
1241 messageView.setText(mMessageResId);
1242 messageView.setEnabled(enabled);
1243 }
1244
1245 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1246 if (icon != null) {
1247 icon.setImageDrawable(context.getDrawable(
1248 (on ? mEnabledIconResId : mDisabledIconResid)));
1249 icon.setEnabled(enabled);
1250 }
1251
1252 if (statusView != null) {
1253 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1254 statusView.setVisibility(View.VISIBLE);
1255 statusView.setEnabled(enabled);
1256 }
1257 v.setEnabled(enabled);
1258
1259 return v;
1260 }
1261
1262 public final void onPress() {
1263 if (mState.inTransition()) {
1264 Log.w(TAG, "shouldn't be able to toggle when in transition");
1265 return;
1266 }
1267
1268 final boolean nowOn = !(mState == State.On);
1269 onToggle(nowOn);
1270 changeStateFromPress(nowOn);
1271 }
1272
1273 public boolean isEnabled() {
1274 return !mState.inTransition();
1275 }
1276
1277 /**
1278 * Implementations may override this if their state can be in on of the intermediate
1279 * states until some notification is received (e.g airplane mode is 'turning off' until
1280 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001281 *
Jason Monk361915c2017-03-21 20:33:59 -04001282 * @param buttonOn Whether the button was turned on or off
1283 */
1284 protected void changeStateFromPress(boolean buttonOn) {
1285 mState = buttonOn ? State.On : State.Off;
1286 }
1287
1288 abstract void onToggle(boolean on);
1289
1290 public void updateState(State state) {
1291 mState = state;
1292 }
1293 }
1294
1295 private class SilentModeToggleAction extends ToggleAction {
1296 public SilentModeToggleAction() {
1297 super(R.drawable.ic_audio_vol_mute,
1298 R.drawable.ic_audio_vol,
1299 R.string.global_action_toggle_silent_mode,
1300 R.string.global_action_silent_mode_on_status,
1301 R.string.global_action_silent_mode_off_status);
1302 }
1303
1304 void onToggle(boolean on) {
1305 if (on) {
1306 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1307 } else {
1308 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1309 }
1310 }
1311
1312 public boolean showDuringKeyguard() {
1313 return true;
1314 }
1315
1316 public boolean showBeforeProvisioning() {
1317 return false;
1318 }
1319 }
1320
1321 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1322
Jason Monk16fbd9d2017-04-27 14:28:49 -04001323 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001324
1325 private final AudioManager mAudioManager;
1326 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001327
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001328 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001329 mAudioManager = audioManager;
1330 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001331 }
1332
1333 private int ringerModeToIndex(int ringerMode) {
1334 // They just happen to coincide
1335 return ringerMode;
1336 }
1337
1338 private int indexToRingerMode(int index) {
1339 // They just happen to coincide
1340 return index;
1341 }
1342
1343 @Override
1344 public CharSequence getLabelForAccessibility(Context context) {
1345 return null;
1346 }
1347
1348 public View create(Context context, View convertView, ViewGroup parent,
1349 LayoutInflater inflater) {
1350 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1351
1352 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1353 for (int i = 0; i < 3; i++) {
1354 View itemView = v.findViewById(ITEM_IDS[i]);
1355 itemView.setSelected(selectedIndex == i);
1356 // Set up click handler
1357 itemView.setTag(i);
1358 itemView.setOnClickListener(this);
1359 }
1360 return v;
1361 }
1362
1363 public void onPress() {
1364 }
1365
1366 public boolean showDuringKeyguard() {
1367 return true;
1368 }
1369
1370 public boolean showBeforeProvisioning() {
1371 return false;
1372 }
1373
1374 public boolean isEnabled() {
1375 return true;
1376 }
1377
1378 void willCreate() {
1379 }
1380
1381 public void onClick(View v) {
1382 if (!(v.getTag() instanceof Integer)) return;
1383
1384 int index = (Integer) v.getTag();
1385 mAudioManager.setRingerMode(indexToRingerMode(index));
1386 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1387 }
1388 }
1389
1390 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1391 public void onReceive(Context context, Intent intent) {
1392 String action = intent.getAction();
1393 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1394 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1395 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1396 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001397 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001398 }
1399 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1400 // Airplane mode can be changed after ECM exits if airplane toggle button
1401 // is pressed during ECM mode
1402 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1403 mIsWaitingForEcmExit) {
1404 mIsWaitingForEcmExit = false;
1405 changeAirplaneModeSystemSetting(true);
1406 }
1407 }
1408 }
1409 };
1410
1411 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1412 @Override
1413 public void onServiceStateChanged(ServiceState serviceState) {
1414 if (!mHasTelephony) return;
1415 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1416 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1417 mAirplaneModeOn.updateState(mAirplaneState);
1418 mAdapter.notifyDataSetChanged();
1419 }
1420 };
1421
1422 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1423 @Override
1424 public void onReceive(Context context, Intent intent) {
1425 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1426 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1427 }
1428 }
1429 };
1430
1431 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1432 @Override
1433 public void onChange(boolean selfChange) {
1434 onAirplaneModeChanged();
1435 }
1436 };
1437
1438 private static final int MESSAGE_DISMISS = 0;
1439 private static final int MESSAGE_REFRESH = 1;
1440 private static final int MESSAGE_SHOW = 2;
1441 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1442
1443 private Handler mHandler = new Handler() {
1444 public void handleMessage(Message msg) {
1445 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001446 case MESSAGE_DISMISS:
1447 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001448 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1449 mDialog.dismissImmediately();
1450 } else {
1451 mDialog.dismiss();
1452 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001453 mDialog = null;
1454 }
1455 break;
1456 case MESSAGE_REFRESH:
1457 refreshSilentMode();
1458 mAdapter.notifyDataSetChanged();
1459 break;
1460 case MESSAGE_SHOW:
1461 handleShow();
1462 break;
Jason Monk361915c2017-03-21 20:33:59 -04001463 }
1464 }
1465 };
1466
1467 private void onAirplaneModeChanged() {
1468 // Let the service state callbacks handle the state.
1469 if (mHasTelephony) return;
1470
1471 boolean airplaneModeOn = Settings.Global.getInt(
1472 mContext.getContentResolver(),
1473 Settings.Global.AIRPLANE_MODE_ON,
1474 0) == 1;
1475 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1476 mAirplaneModeOn.updateState(mAirplaneState);
1477 }
1478
1479 /**
1480 * Change the airplane mode system setting
1481 */
1482 private void changeAirplaneModeSystemSetting(boolean on) {
1483 Settings.Global.putInt(
1484 mContext.getContentResolver(),
1485 Settings.Global.AIRPLANE_MODE_ON,
1486 on ? 1 : 0);
1487 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1488 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1489 intent.putExtra("state", on);
1490 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1491 if (!mHasTelephony) {
1492 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1493 }
1494 }
1495
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001496 private static final class ActionsDialog extends Dialog implements DialogInterface,
1497 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001498
Jason Monk16fbd9d2017-04-27 14:28:49 -04001499 private final Context mContext;
1500 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001501 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001502 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001503 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001504 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001505 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001506 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001507 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001508
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001509 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001510 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001511 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001512 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001513 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001514 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001515
1516 // Window initialization
1517 Window window = getWindow();
1518 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001519 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1520 window.getDecorView();
1521 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1522 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1523 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1524 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1525 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001526 window.addFlags(
1527 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001528 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001529 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001530 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1531 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1532 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001533 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1534
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001535 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001536
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001537 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001538
1539 mPanelController = plugin;
1540 View panelView = initializePanel();
1541 if (panelView == null) {
1542 mBackgroundDrawable = new GradientDrawable(context);
Steve Elliottbfa314a2019-03-06 13:53:19 -05001543 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
Steve Elliott9b87a442019-03-05 10:24:16 -05001544 } else {
1545 mBackgroundDrawable = context.getDrawable(
1546 com.android.systemui.R.drawable.global_action_panel_scrim);
1547 mScrimAlpha = 1f;
1548 addContentView(
1549 panelView,
1550 new ViewGroup.LayoutParams(
1551 ViewGroup.LayoutParams.MATCH_PARENT,
1552 ViewGroup.LayoutParams.MATCH_PARENT));
1553 }
1554 window.setBackgroundDrawable(mBackgroundDrawable);
1555 }
1556
1557 private View initializePanel() {
1558 if (isPanelEnabled(mContext) && mPanelController != null) {
1559 View panelView = mPanelController.getPanelContent();
1560 if (panelView != null) {
1561 FrameLayout panelContainer = new FrameLayout(mContext);
1562 FrameLayout.LayoutParams panelParams =
1563 new FrameLayout.LayoutParams(
1564 FrameLayout.LayoutParams.MATCH_PARENT,
1565 FrameLayout.LayoutParams.WRAP_CONTENT);
Steve Elliott9b87a442019-03-05 10:24:16 -05001566 panelContainer.addView(panelView, panelParams);
1567 return panelContainer;
1568 }
1569 }
1570 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001571 }
1572
1573 private void initializeLayout() {
1574 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001575 mGlobalActionsLayout = (MultiListLayout)
1576 findViewById(com.android.systemui.R.id.global_actions_view);
1577 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001578 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001579 @Override
1580 public boolean dispatchPopulateAccessibilityEvent(
1581 View host, AccessibilityEvent event) {
1582 // Populate the title here, just as Activity does
1583 event.getText().add(mContext.getString(R.string.global_actions));
1584 return true;
1585 }
1586 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001587 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001588 mGlobalActionsLayout.setAdapter(mAdapter);
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001589 mGlobalActionsLayout.setSnapToEdge(isPanelEnabled(mContext)
1590 && mPanelController != null);
Steve Elliott9b87a442019-03-05 10:24:16 -05001591 }
1592
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001593 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001594 if (isGridEnabled(context)) {
1595 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1596 return com.android.systemui.R.layout.global_actions_grid_seascape;
1597 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001598 return com.android.systemui.R.layout.global_actions_grid;
1599 }
1600 return com.android.systemui.R.layout.global_actions_wrapped;
1601 }
1602
Jason Monk361915c2017-03-21 20:33:59 -04001603 @Override
1604 protected void onStart() {
1605 super.setCanceledOnTouchOutside(true);
1606 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001607 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001608
Steve Elliott9b87a442019-03-05 10:24:16 -05001609 if (mBackgroundDrawable instanceof GradientDrawable) {
1610 Point displaySize = new Point();
1611 mContext.getDisplay().getRealSize(displaySize);
1612 mColorExtractor.addOnColorsChangedListener(this);
1613 ((GradientDrawable) mBackgroundDrawable)
1614 .setScreenSize(displaySize.x, displaySize.y);
1615 GradientColors colors = mColorExtractor.getColors(
1616 mKeyguardShowing
1617 ? WallpaperManager.FLAG_LOCK
1618 : WallpaperManager.FLAG_SYSTEM);
1619 updateColors(colors, false /* animate */);
1620 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001621 }
1622
1623 /**
1624 * Updates background and system bars according to current GradientColors.
1625 * @param colors Colors and hints to use.
1626 * @param animate Interpolates gradient if true, just sets otherwise.
1627 */
1628 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001629 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1630 return;
1631 }
1632 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001633 View decorView = getWindow().getDecorView();
1634 if (colors.supportsDarkText()) {
1635 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001636 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001637 } else {
1638 decorView.setSystemUiVisibility(0);
1639 }
Jason Monk361915c2017-03-21 20:33:59 -04001640 }
1641
1642 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001643 protected void onStop() {
1644 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001645 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001646 }
1647
1648 @Override
1649 public void show() {
1650 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001651 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001652 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001653 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1654 mGlobalActionsLayout.setAlpha(0);
1655 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001656 .alpha(1)
1657 .translationX(0)
1658 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001659 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001660 .setUpdateListener(animation -> {
1661 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001662 * mScrimAlpha * 255);
1663 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001664 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001665 .start();
1666 }
1667
1668 @Override
1669 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001670 if (!mShowing) {
1671 return;
1672 }
1673 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001674 mGlobalActionsLayout.setTranslationX(0);
1675 mGlobalActionsLayout.setAlpha(1);
1676 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001677 .alpha(0)
1678 .translationX(getAnimTranslation())
1679 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001680 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001681 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001682 .setUpdateListener(animation -> {
1683 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001684 * mScrimAlpha * 255);
1685 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001686 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001687 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001688 if (mPanelController != null) {
1689 mPanelController.onDismissed();
1690 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001691 }
1692
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001693 void dismissImmediately() {
1694 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001695 mShowing = false;
Steve Elliott2d206d32019-03-14 16:30:33 -04001696 if (mPanelController != null) {
1697 mPanelController.onDismissed();
1698 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001699 }
1700
Jason Monk16fbd9d2017-04-27 14:28:49 -04001701 private float getAnimTranslation() {
1702 return getContext().getResources().getDimension(
1703 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001704 }
1705
1706 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001707 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001708 if (mKeyguardShowing) {
1709 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001710 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1711 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001712 }
1713 } else {
1714 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001715 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1716 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001717 }
1718 }
1719 }
1720
1721 public void setKeyguardShowing(boolean keyguardShowing) {
1722 mKeyguardShowing = keyguardShowing;
1723 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001724
1725 public void onRotate(int from, int to) {
1726 if (mShowing && isGridEnabled(mContext)) {
1727 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001728 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001729 }
1730 }
Jason Monk361915c2017-03-21 20:33:59 -04001731 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001732
1733 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001734 * Determines whether or not the Global Actions menu should use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001735 */
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001736 private static boolean isGridEnabled(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001737 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1738 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001739
1740 /**
1741 * Determines whether or not the Global Actions Panel should appear when the power button
1742 * is held.
1743 */
1744 private static boolean isPanelEnabled(Context context) {
1745 return FeatureFlagUtils.isEnabled(
1746 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED); }
1747
1748 /**
1749 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1750 */
1751 private static boolean shouldUseSeparatedView() {
1752 return true;
1753 }
Jason Monk361915c2017-03-21 20:33:59 -04001754}