blob: 50530ff46e8d7ec632a395a6dbf51d48d7223bad [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;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070027import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000028import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010029import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040030import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
35import android.content.pm.UserInfo;
36import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070037import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040038import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.net.ConnectivityManager;
Jason Monk361915c2017-03-21 20:33:59 -040041import android.os.Handler;
42import android.os.Message;
43import android.os.RemoteException;
44import android.os.ServiceManager;
45import android.os.SystemProperties;
46import android.os.UserHandle;
47import android.os.UserManager;
48import android.os.Vibrator;
49import android.provider.Settings;
50import android.service.dreams.DreamService;
51import android.service.dreams.IDreamManager;
52import android.telephony.PhoneStateListener;
53import android.telephony.ServiceState;
54import android.telephony.TelephonyManager;
55import android.text.TextUtils;
56import android.util.ArraySet;
yuanjiahsu88363e92018-09-06 19:23:52 +080057import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040058import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070059import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040060import android.view.LayoutInflater;
61import android.view.View;
62import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070063import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.view.WindowManager;
65import android.view.WindowManagerGlobal;
66import android.view.accessibility.AccessibilityEvent;
Steve Elliott9b87a442019-03-05 10:24:16 -050067import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040068import android.widget.ImageView;
69import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.widget.TextView;
71
Charles He9851a8d2017-10-10 17:31:30 +010072import com.android.internal.R;
73import com.android.internal.colorextraction.ColorExtractor;
74import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070075import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010076import com.android.internal.logging.MetricsLogger;
77import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
78import com.android.internal.telephony.TelephonyIntents;
79import com.android.internal.telephony.TelephonyProperties;
80import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040081import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050082import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010083import com.android.internal.widget.LockPatternUtils;
84import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050086import com.android.systemui.MultiListLayout;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050087import com.android.systemui.MultiListLayout.MultiListAdapter;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.systemui.colorextraction.SysuiColorExtractor;
89import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050090import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Steve Elliottbfa314a2019-03-06 13:53:19 -050091import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070092import com.android.systemui.statusbar.policy.ConfigurationController;
Steve Elliott9b87a442019-03-05 10:24:16 -050093import com.android.systemui.statusbar.policy.ExtensionController;
94import com.android.systemui.statusbar.policy.ExtensionController.Extension;
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;
Jason Monk361915c2017-03-21 20:33:59 -0400162
Steve Elliott9b87a442019-03-05 10:24:16 -0500163 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
164
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
209 mPanelExtension = Dependency.get(ExtensionController.class)
210 .newExtension(GlobalActionsPanelPlugin.class)
211 .withPlugin(GlobalActionsPanelPlugin.class)
212 .build();
Jason Monk361915c2017-03-21 20:33:59 -0400213 }
214
215 /**
216 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400217 *
Jason Monk361915c2017-03-21 20:33:59 -0400218 * @param keyguardShowing True if keyguard is showing
219 */
220 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
221 mKeyguardShowing = keyguardShowing;
222 mDeviceProvisioned = isDeviceProvisioned;
223 if (mDialog != null) {
224 mDialog.dismiss();
225 mDialog = null;
226 // Show delayed, so that the dismiss of the previous dialog completes
227 mHandler.sendEmptyMessage(MESSAGE_SHOW);
228 } else {
229 handleShow();
230 }
231 }
232
Charles He9851a8d2017-10-10 17:31:30 +0100233 /**
234 * Dismiss the global actions dialog, if it's currently shown
235 */
236 public void dismissDialog() {
237 mHandler.removeMessages(MESSAGE_DISMISS);
238 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
239 }
240
Jason Monk361915c2017-03-21 20:33:59 -0400241 private void awakenIfNecessary() {
242 if (mDreamManager != null) {
243 try {
244 if (mDreamManager.isDreaming()) {
245 mDreamManager.awaken();
246 }
247 } catch (RemoteException e) {
248 // we tried
249 }
250 }
251 }
252
253 private void handleShow() {
254 awakenIfNecessary();
255 mDialog = createDialog();
256 prepareDialog();
257
258 // If we only have 1 item and it's a simple press action, just do this action.
259 if (mAdapter.getCount() == 1
260 && mAdapter.getItem(0) instanceof SinglePressAction
261 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
262 ((SinglePressAction) mAdapter.getItem(0)).onPress();
263 } else {
264 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
265 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100266 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400267 mDialog.getWindow().setAttributes(attrs);
268 mDialog.show();
269 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400270 }
271 }
272
273 /**
274 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400275 *
Jason Monk361915c2017-03-21 20:33:59 -0400276 * @return A new dialog.
277 */
278 private ActionsDialog createDialog() {
279 // Simple toggle style if there's no vibrator, otherwise use a tri-state
280 if (!mHasVibrator) {
281 mSilentModeAction = new SilentModeToggleAction();
282 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700283 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400284 }
285 mAirplaneModeOn = new ToggleAction(
286 R.drawable.ic_lock_airplane_mode,
287 R.drawable.ic_lock_airplane_mode_off,
288 R.string.global_actions_toggle_airplane_mode,
289 R.string.global_actions_airplane_mode_on_status,
290 R.string.global_actions_airplane_mode_off_status) {
291
292 void onToggle(boolean on) {
293 if (mHasTelephony && Boolean.parseBoolean(
294 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
295 mIsWaitingForEcmExit = true;
296 // Launch ECM exit dialog
297 Intent ecmDialogIntent =
298 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
299 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
300 mContext.startActivity(ecmDialogIntent);
301 } else {
302 changeAirplaneModeSystemSetting(on);
303 }
304 }
305
306 @Override
307 protected void changeStateFromPress(boolean buttonOn) {
308 if (!mHasTelephony) return;
309
310 // In ECM mode airplane state cannot be changed
311 if (!(Boolean.parseBoolean(
312 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
313 mState = buttonOn ? State.TurningOn : State.TurningOff;
314 mAirplaneState = mState;
315 }
316 }
317
318 public boolean showDuringKeyguard() {
319 return true;
320 }
321
322 public boolean showBeforeProvisioning() {
323 return false;
324 }
325 };
326 onAirplaneModeChanged();
327
328 mItems = new ArrayList<Action>();
329 String[] defaultActions = mContext.getResources().getStringArray(
330 R.array.config_globalActionsList);
331
332 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000333 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800334 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400335 for (int i = 0; i < defaultActions.length; i++) {
336 String actionKey = defaultActions[i];
337 if (addedKeys.contains(actionKey)) {
338 // If we already have added this, don't add it again.
339 continue;
340 }
341 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
342 mItems.add(new PowerAction());
343 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
344 mItems.add(mAirplaneModeOn);
345 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
346 if (Settings.Global.getInt(mContext.getContentResolver(),
347 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
348 mItems.add(new BugReportAction());
349 }
350 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
351 if (mShowSilentToggle) {
352 mItems.add(mSilentModeAction);
353 }
354 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
355 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
356 addUsersToMenu(mItems);
357 }
358 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
359 mItems.add(getSettingsAction());
360 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700361 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
362 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800363 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700364 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800365 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700366 }
Jason Monk361915c2017-03-21 20:33:59 -0400367 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
368 mItems.add(getVoiceAssistAction());
369 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
370 mItems.add(getAssistAction());
371 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
372 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500373 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
374 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000375 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000376 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000377 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
378 mItems.add(new LogoutAction());
379 mHasLogoutButton = true;
380 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800381 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400382 if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800383 mItems.add(new EmergencyDialerAction());
384 }
Jason Monk361915c2017-03-21 20:33:59 -0400385 } else {
386 Log.e(TAG, "Invalid global action key " + actionKey);
387 }
388 // Add here so we don't add more than one.
389 addedKeys.add(actionKey);
390 }
391
392 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400393 mItems.add(new EmergencyAffordanceAction());
Jason Monk361915c2017-03-21 20:33:59 -0400394 }
395
396 mAdapter = new MyAdapter();
397
Steve Elliott9b87a442019-03-05 10:24:16 -0500398 GlobalActionsPanelPlugin.PanelViewController panelViewController =
399 mPanelExtension.get() != null
400 ? mPanelExtension.get().onPanelShown(() -> {
401 if (mDialog != null) {
402 mDialog.dismiss();
403 }
404 })
405 : null;
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400406 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400407 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700408 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400409
410 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800411 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400412
413 return dialog;
414 }
415
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800416 private boolean shouldDisplayLockdown() {
417 int userId = getCurrentUser().id;
418 // Lockdown is meaningless without a place to go.
419 if (!mKeyguardManager.isDeviceSecure(userId)) {
420 return false;
421 }
422
423 // Only show the lockdown button if the device isn't locked down (for whatever reason).
424 int state = mLockPatternUtils.getStrongAuthForUser(userId);
425 return (state == STRONG_AUTH_NOT_REQUIRED
426 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
427 }
428
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700429 @Override
430 public void onUiModeChanged() {
431 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
432 }
433
434 public void destroy() {
435 Dependency.get(ConfigurationController.class).removeCallback(this);
436 }
437
Jason Monk361915c2017-03-21 20:33:59 -0400438 private final class PowerAction extends SinglePressAction implements LongPressAction {
439 private PowerAction() {
440 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400441 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400442 }
443
444 @Override
445 public boolean onLongPress() {
446 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
447 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
448 mWindowManagerFuncs.reboot(true);
449 return true;
450 }
451 return false;
452 }
453
454 @Override
455 public boolean showDuringKeyguard() {
456 return true;
457 }
458
459 @Override
460 public boolean showBeforeProvisioning() {
461 return true;
462 }
463
464 @Override
465 public void onPress() {
466 // shutdown by making sure radio and power are handled accordingly.
467 mWindowManagerFuncs.shutdown();
468 }
469 }
470
Aaron Heuckrothc8343402019-03-13 16:27:15 -0400471 private abstract class EmergencyAction extends SinglePressAction {
472 EmergencyAction(int iconResId, int messageResId) {
473 super(iconResId, messageResId);
474 }
475
476 @Override
477 public boolean shouldBeSeparated() {
478 return shouldUseSeparatedView();
479 }
480
481 @Override
482 public View create(
483 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
484 View v = super.create(context, convertView, parent, inflater);
485 int textColor;
486 if (shouldBeSeparated()) {
487 textColor = v.getResources().getColor(
488 com.android.systemui.R.color.global_actions_alert_text);
489 } else {
490 textColor = v.getResources().getColor(
491 com.android.systemui.R.color.global_actions_text);
492 }
493 TextView messageView = v.findViewById(R.id.message);
494 messageView.setTextColor(textColor);
495 ImageView icon = (ImageView) v.findViewById(R.id.icon);
496 icon.getDrawable().setTint(textColor);
497 return v;
498 }
499
500 @Override
501 public boolean showDuringKeyguard() {
502 return true;
503 }
504
505 @Override
506 public boolean showBeforeProvisioning() {
507 return true;
508 }
509 }
510
511 private class EmergencyAffordanceAction extends EmergencyAction {
512 EmergencyAffordanceAction() {
513 super(R.drawable.emergency_icon,
514 R.string.global_action_emergency);
515 }
516
517 @Override
518 public void onPress() {
519 mEmergencyAffordanceManager.performEmergencyCall();
520 }
521 }
522
523 private class EmergencyDialerAction extends EmergencyAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800524 private EmergencyDialerAction() {
525 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800526 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800527 }
528
529 @Override
530 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800531 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800532 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800533 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
534 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
535 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800536 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
537 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800538 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
539 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800540 }
541
Jason Monk361915c2017-03-21 20:33:59 -0400542 private final class RestartAction extends SinglePressAction implements LongPressAction {
543 private RestartAction() {
544 super(R.drawable.ic_restart, R.string.global_action_restart);
545 }
546
547 @Override
548 public boolean onLongPress() {
549 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
550 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
551 mWindowManagerFuncs.reboot(true);
552 return true;
553 }
554 return false;
555 }
556
557 @Override
558 public boolean showDuringKeyguard() {
559 return true;
560 }
561
562 @Override
563 public boolean showBeforeProvisioning() {
564 return true;
565 }
566
567 @Override
568 public void onPress() {
569 mWindowManagerFuncs.reboot(false);
570 }
571 }
572
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400573 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500574 public ScreenshotAction() {
575 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
576 }
577
578 @Override
579 public void onPress() {
580 // Add a little delay before executing, to give the
581 // dialog a chance to go away before it takes a
582 // screenshot.
583 // TODO: instead, omit global action dialog layer
584 mHandler.postDelayed(new Runnable() {
585 @Override
586 public void run() {
587 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
588 MetricsLogger.action(mContext,
589 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
590 }
591 }, 500);
592 }
593
594 @Override
595 public boolean showDuringKeyguard() {
596 return true;
597 }
598
599 @Override
600 public boolean showBeforeProvisioning() {
601 return false;
602 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400603
604 @Override
605 public boolean onLongPress() {
606 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
607 mScreenRecordHelper.launchRecordPrompt();
608 } else {
609 onPress();
610 }
611 return true;
612 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500613 }
614
Jason Monk361915c2017-03-21 20:33:59 -0400615 private class BugReportAction extends SinglePressAction implements LongPressAction {
616
617 public BugReportAction() {
618 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
619 }
620
621 @Override
622 public void onPress() {
623 // don't actually trigger the bugreport if we are running stability
624 // tests via monkey
625 if (ActivityManager.isUserAMonkey()) {
626 return;
627 }
628 // Add a little delay before executing, to give the
629 // dialog a chance to go away before it takes a
630 // screenshot.
631 mHandler.postDelayed(new Runnable() {
632 @Override
633 public void run() {
634 try {
635 // Take an "interactive" bugreport.
636 MetricsLogger.action(mContext,
637 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
638 ActivityManager.getService().requestBugReport(
639 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
640 } catch (RemoteException e) {
641 }
642 }
643 }, 500);
644 }
645
646 @Override
647 public boolean onLongPress() {
648 // don't actually trigger the bugreport if we are running stability
649 // tests via monkey
650 if (ActivityManager.isUserAMonkey()) {
651 return false;
652 }
653 try {
654 // Take a "full" bugreport.
655 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
656 ActivityManager.getService().requestBugReport(
657 ActivityManager.BUGREPORT_OPTION_FULL);
658 } catch (RemoteException e) {
659 }
660 return false;
661 }
662
663 public boolean showDuringKeyguard() {
664 return true;
665 }
666
667 @Override
668 public boolean showBeforeProvisioning() {
669 return false;
670 }
Jason Monk361915c2017-03-21 20:33:59 -0400671 }
672
Alex Chau04458852017-11-27 18:21:23 +0000673 private final class LogoutAction extends SinglePressAction {
674 private LogoutAction() {
675 super(R.drawable.ic_logout, R.string.global_action_logout);
676 }
677
678 @Override
679 public boolean showDuringKeyguard() {
680 return true;
681 }
682
683 @Override
684 public boolean showBeforeProvisioning() {
685 return false;
686 }
687
688 @Override
689 public void onPress() {
690 // Add a little delay before executing, to give the dialog a chance to go away before
691 // switching user
692 mHandler.postDelayed(() -> {
693 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000694 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000695 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000696 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000697 } catch (RemoteException re) {
698 Log.e(TAG, "Couldn't logout user " + re);
699 }
700 }, 500);
701 }
702 }
703
Jason Monk361915c2017-03-21 20:33:59 -0400704 private Action getSettingsAction() {
705 return new SinglePressAction(R.drawable.ic_settings,
706 R.string.global_action_settings) {
707
708 @Override
709 public void onPress() {
710 Intent intent = new Intent(Settings.ACTION_SETTINGS);
711 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
712 mContext.startActivity(intent);
713 }
714
715 @Override
716 public boolean showDuringKeyguard() {
717 return true;
718 }
719
720 @Override
721 public boolean showBeforeProvisioning() {
722 return true;
723 }
724 };
725 }
726
Jason Monk361915c2017-03-21 20:33:59 -0400727 private Action getAssistAction() {
728 return new SinglePressAction(R.drawable.ic_action_assist_focused,
729 R.string.global_action_assist) {
730 @Override
731 public void onPress() {
732 Intent intent = new Intent(Intent.ACTION_ASSIST);
733 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
734 mContext.startActivity(intent);
735 }
736
737 @Override
738 public boolean showDuringKeyguard() {
739 return true;
740 }
741
742 @Override
743 public boolean showBeforeProvisioning() {
744 return true;
745 }
746 };
747 }
748
749 private Action getVoiceAssistAction() {
750 return new SinglePressAction(R.drawable.ic_voice_search,
751 R.string.global_action_voice_assist) {
752 @Override
753 public void onPress() {
754 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
755 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
756 mContext.startActivity(intent);
757 }
758
759 @Override
760 public boolean showDuringKeyguard() {
761 return true;
762 }
763
764 @Override
765 public boolean showBeforeProvisioning() {
766 return true;
767 }
768 };
769 }
770
771 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400772 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400773 R.string.global_action_lockdown) {
774
775 @Override
776 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700777 new LockPatternUtils(mContext)
778 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
779 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400780 try {
781 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100782 // Lock profiles (if any) on the background thread.
783 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
784 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400785 } catch (RemoteException e) {
786 Log.e(TAG, "Error while trying to lock device.", e);
787 }
788 }
789
790 @Override
791 public boolean showDuringKeyguard() {
792 return true;
793 }
794
795 @Override
796 public boolean showBeforeProvisioning() {
797 return false;
798 }
799 };
800 }
801
Pavel Grafov059021b2018-05-02 13:44:46 +0100802 private void lockProfiles() {
803 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
804 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
805 final int currentUserId = getCurrentUser().id;
806 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
807 for (final int id : profileIds) {
808 if (id != currentUserId) {
809 tm.setDeviceLockedForUser(id, true);
810 }
811 }
812 }
813
Jason Monk361915c2017-03-21 20:33:59 -0400814 private UserInfo getCurrentUser() {
815 try {
816 return ActivityManager.getService().getCurrentUser();
817 } catch (RemoteException re) {
818 return null;
819 }
820 }
821
822 private boolean isCurrentUserOwner() {
823 UserInfo currentUser = getCurrentUser();
824 return currentUser == null || currentUser.isPrimary();
825 }
826
827 private void addUsersToMenu(ArrayList<Action> items) {
828 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
829 if (um.isUserSwitcherEnabled()) {
830 List<UserInfo> users = um.getUsers();
831 UserInfo currentUser = getCurrentUser();
832 for (final UserInfo user : users) {
833 if (user.supportsSwitchToByUser()) {
834 boolean isCurrentUser = currentUser == null
835 ? user.id == 0 : (currentUser.id == user.id);
836 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
837 : null;
838 SinglePressAction switchToUser = new SinglePressAction(
839 R.drawable.ic_menu_cc, icon,
840 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400841 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400842 public void onPress() {
843 try {
844 ActivityManager.getService().switchUser(user.id);
845 } catch (RemoteException re) {
846 Log.e(TAG, "Couldn't switch user " + re);
847 }
848 }
849
850 public boolean showDuringKeyguard() {
851 return true;
852 }
853
854 public boolean showBeforeProvisioning() {
855 return false;
856 }
857 };
858 items.add(switchToUser);
859 }
860 }
861 }
862 }
863
864 private void prepareDialog() {
865 refreshSilentMode();
866 mAirplaneModeOn.updateState(mAirplaneState);
867 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400868 if (mShowSilentToggle) {
869 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
870 mContext.registerReceiver(mRingerModeReceiver, filter);
871 }
872 }
873
874 private void refreshSilentMode() {
875 if (!mHasVibrator) {
876 final boolean silentModeOn =
877 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400878 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400879 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
880 }
881 }
882
883 /** {@inheritDoc} */
884 public void onDismiss(DialogInterface dialog) {
885 mWindowManagerFuncs.onGlobalActionsHidden();
886 if (mShowSilentToggle) {
887 try {
888 mContext.unregisterReceiver(mRingerModeReceiver);
889 } catch (IllegalArgumentException ie) {
890 // ignore this
891 Log.w(TAG, ie);
892 }
893 }
894 }
895
896 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800897 public void onShow(DialogInterface dialog) {
898 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
899 }
900
Jason Monk361915c2017-03-21 20:33:59 -0400901 /**
902 * The adapter used for the list within the global actions dialog, taking
903 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400904 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
905 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400906 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
907 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500908 public class MyAdapter extends MultiListAdapter {
909 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400910 public int getCount() {
911 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400912 for (int i = 0; i < mItems.size(); i++) {
913 final Action action = mItems.get(i);
914
915 if (mKeyguardShowing && !action.showDuringKeyguard()) {
916 continue;
917 }
918 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
919 continue;
920 }
921 count++;
922 }
923 return count;
924 }
925
926 @Override
927 public boolean isEnabled(int position) {
928 return getItem(position).isEnabled();
929 }
930
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500931 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400932 public ArrayList<Action> getSeparatedItems() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500933 ArrayList<Action> separatedActions = new ArrayList<Action>();
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400934 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500935 return separatedActions;
936 }
937 for (int i = 0; i < mItems.size(); i++) {
938 final Action action = mItems.get(i);
939 if (action.shouldBeSeparated()) {
940 separatedActions.add(action);
941 }
942 }
943 return separatedActions;
944 }
945
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500946 @Override
Aaron Heuckrothf19d2722019-03-11 17:06:02 -0400947 public ArrayList<Action> getListItems() {
948 if (!shouldUseSeparatedView()) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500949 return new ArrayList<Action>(mItems);
950 }
951 ArrayList<Action> listActions = new ArrayList<Action>();
952 for (int i = 0; i < mItems.size(); i++) {
953 final Action action = mItems.get(i);
954 if (!action.shouldBeSeparated()) {
955 listActions.add(action);
956 }
957 }
958 return listActions;
959 }
960
Jason Monk361915c2017-03-21 20:33:59 -0400961 @Override
962 public boolean areAllItemsEnabled() {
963 return false;
964 }
965
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500966 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400967 public Action getItem(int position) {
968
969 int filteredPos = 0;
970 for (int i = 0; i < mItems.size(); i++) {
971 final Action action = mItems.get(i);
972 if (mKeyguardShowing && !action.showDuringKeyguard()) {
973 continue;
974 }
975 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
976 continue;
977 }
978 if (filteredPos == position) {
979 return action;
980 }
981 filteredPos++;
982 }
983
984 throw new IllegalArgumentException("position " + position
985 + " out of range of showable actions"
986 + ", filtered count=" + getCount()
987 + ", keyguardshowing=" + mKeyguardShowing
988 + ", provisioned=" + mDeviceProvisioned);
989 }
990
991
992 public long getItemId(int position) {
993 return position;
994 }
995
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500996 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400997 public View getView(int position, View convertView, ViewGroup parent) {
998 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400999 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001000 // Everything but screenshot, the last item, gets white background.
1001 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001002 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001003 }
1004 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001005 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001006
1007 @Override
1008 public boolean onLongClickItem(int position) {
1009 final Action action = mAdapter.getItem(position);
1010 if (action instanceof LongPressAction) {
1011 mDialog.dismiss();
1012 return ((LongPressAction) action).onLongPress();
1013 }
1014 return false;
1015 }
1016
1017 @Override
1018 public void onClickItem(int position) {
1019 Action item = mAdapter.getItem(position);
1020 if (!(item instanceof SilentModeTriStateAction)) {
1021 mDialog.dismiss();
1022 }
1023 item.onPress();
1024 }
Jason Monk361915c2017-03-21 20:33:59 -04001025 }
1026
1027 // note: the scheme below made more sense when we were planning on having
1028 // 8 different things in the global actions dialog. seems overkill with
1029 // only 3 items now, but may as well keep this flexible approach so it will
1030 // be easy should someone decide at the last minute to include something
1031 // else, such as 'enable wifi', or 'enable bluetooth'
1032
1033 /**
1034 * What each item in the global actions dialog must be able to support.
1035 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001036 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001037 /**
1038 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001039 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001040 */
1041 CharSequence getLabelForAccessibility(Context context);
1042
1043 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1044
1045 void onPress();
1046
1047 /**
1048 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001049 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001050 */
1051 boolean showDuringKeyguard();
1052
1053 /**
1054 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001055 * device is provisioned.onlongpress
1056 *
Jason Monk361915c2017-03-21 20:33:59 -04001057 */
1058 boolean showBeforeProvisioning();
1059
1060 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001061
1062 default boolean shouldBeSeparated() {
1063 return false;
1064 }
Jason Monk361915c2017-03-21 20:33:59 -04001065 }
1066
1067 /**
1068 * An action that also supports long press.
1069 */
1070 private interface LongPressAction extends Action {
1071 boolean onLongPress();
1072 }
1073
1074 /**
1075 * A single press action maintains no state, just responds to a press
1076 * and takes an action.
1077 */
1078 private static abstract class SinglePressAction implements Action {
1079 private final int mIconResId;
1080 private final Drawable mIcon;
1081 private final int mMessageResId;
1082 private final CharSequence mMessage;
1083
1084 protected SinglePressAction(int iconResId, int messageResId) {
1085 mIconResId = iconResId;
1086 mMessageResId = messageResId;
1087 mMessage = null;
1088 mIcon = null;
1089 }
1090
1091 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1092 mIconResId = iconResId;
1093 mMessageResId = 0;
1094 mMessage = message;
1095 mIcon = icon;
1096 }
1097
1098 public boolean isEnabled() {
1099 return true;
1100 }
1101
1102 public String getStatus() {
1103 return null;
1104 }
1105
1106 abstract public void onPress();
1107
1108 public CharSequence getLabelForAccessibility(Context context) {
1109 if (mMessage != null) {
1110 return mMessage;
1111 } else {
1112 return context.getString(mMessageResId);
1113 }
1114 }
1115
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001116 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001117 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001118 return com.android.systemui.R.layout.global_actions_grid_item;
1119 }
1120 return com.android.systemui.R.layout.global_actions_item;
1121 }
1122
Jason Monk361915c2017-03-21 20:33:59 -04001123 public View create(
1124 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001125 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001126 false);
Jason Monk361915c2017-03-21 20:33:59 -04001127
1128 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1129 TextView messageView = (TextView) v.findViewById(R.id.message);
1130
1131 TextView statusView = (TextView) v.findViewById(R.id.status);
1132 final String status = getStatus();
1133 if (!TextUtils.isEmpty(status)) {
1134 statusView.setText(status);
1135 } else {
1136 statusView.setVisibility(View.GONE);
1137 }
1138 if (mIcon != null) {
1139 icon.setImageDrawable(mIcon);
1140 icon.setScaleType(ScaleType.CENTER_CROP);
1141 } else if (mIconResId != 0) {
1142 icon.setImageDrawable(context.getDrawable(mIconResId));
1143 }
1144 if (mMessage != null) {
1145 messageView.setText(mMessage);
1146 } else {
1147 messageView.setText(mMessageResId);
1148 }
1149
1150 return v;
1151 }
1152 }
1153
1154 /**
1155 * A toggle action knows whether it is on or off, and displays an icon
1156 * and status message accordingly.
1157 */
1158 private static abstract class ToggleAction implements Action {
1159
1160 enum State {
1161 Off(false),
1162 TurningOn(true),
1163 TurningOff(true),
1164 On(false);
1165
1166 private final boolean inTransition;
1167
1168 State(boolean intermediate) {
1169 inTransition = intermediate;
1170 }
1171
1172 public boolean inTransition() {
1173 return inTransition;
1174 }
1175 }
1176
1177 protected State mState = State.Off;
1178
1179 // prefs
1180 protected int mEnabledIconResId;
1181 protected int mDisabledIconResid;
1182 protected int mMessageResId;
1183 protected int mEnabledStatusMessageResId;
1184 protected int mDisabledStatusMessageResId;
1185
1186 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001187 * @param enabledIconResId The icon for when this action is on.
1188 * @param disabledIconResid The icon for when this action is off.
1189 * @param message The general information message, e.g 'Silent Mode'
1190 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001191 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1192 */
1193 public ToggleAction(int enabledIconResId,
1194 int disabledIconResid,
1195 int message,
1196 int enabledStatusMessageResId,
1197 int disabledStatusMessageResId) {
1198 mEnabledIconResId = enabledIconResId;
1199 mDisabledIconResid = disabledIconResid;
1200 mMessageResId = message;
1201 mEnabledStatusMessageResId = enabledStatusMessageResId;
1202 mDisabledStatusMessageResId = disabledStatusMessageResId;
1203 }
1204
1205 /**
1206 * Override to make changes to resource IDs just before creating the
1207 * View.
1208 */
1209 void willCreate() {
1210
1211 }
1212
1213 @Override
1214 public CharSequence getLabelForAccessibility(Context context) {
1215 return context.getString(mMessageResId);
1216 }
1217
1218 public View create(Context context, View convertView, ViewGroup parent,
1219 LayoutInflater inflater) {
1220 willCreate();
1221
1222 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001223 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001224
1225 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1226 TextView messageView = (TextView) v.findViewById(R.id.message);
1227 TextView statusView = (TextView) v.findViewById(R.id.status);
1228 final boolean enabled = isEnabled();
1229
1230 if (messageView != null) {
1231 messageView.setText(mMessageResId);
1232 messageView.setEnabled(enabled);
1233 }
1234
1235 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1236 if (icon != null) {
1237 icon.setImageDrawable(context.getDrawable(
1238 (on ? mEnabledIconResId : mDisabledIconResid)));
1239 icon.setEnabled(enabled);
1240 }
1241
1242 if (statusView != null) {
1243 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1244 statusView.setVisibility(View.VISIBLE);
1245 statusView.setEnabled(enabled);
1246 }
1247 v.setEnabled(enabled);
1248
1249 return v;
1250 }
1251
1252 public final void onPress() {
1253 if (mState.inTransition()) {
1254 Log.w(TAG, "shouldn't be able to toggle when in transition");
1255 return;
1256 }
1257
1258 final boolean nowOn = !(mState == State.On);
1259 onToggle(nowOn);
1260 changeStateFromPress(nowOn);
1261 }
1262
1263 public boolean isEnabled() {
1264 return !mState.inTransition();
1265 }
1266
1267 /**
1268 * Implementations may override this if their state can be in on of the intermediate
1269 * states until some notification is received (e.g airplane mode is 'turning off' until
1270 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001271 *
Jason Monk361915c2017-03-21 20:33:59 -04001272 * @param buttonOn Whether the button was turned on or off
1273 */
1274 protected void changeStateFromPress(boolean buttonOn) {
1275 mState = buttonOn ? State.On : State.Off;
1276 }
1277
1278 abstract void onToggle(boolean on);
1279
1280 public void updateState(State state) {
1281 mState = state;
1282 }
1283 }
1284
1285 private class SilentModeToggleAction extends ToggleAction {
1286 public SilentModeToggleAction() {
1287 super(R.drawable.ic_audio_vol_mute,
1288 R.drawable.ic_audio_vol,
1289 R.string.global_action_toggle_silent_mode,
1290 R.string.global_action_silent_mode_on_status,
1291 R.string.global_action_silent_mode_off_status);
1292 }
1293
1294 void onToggle(boolean on) {
1295 if (on) {
1296 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1297 } else {
1298 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1299 }
1300 }
1301
1302 public boolean showDuringKeyguard() {
1303 return true;
1304 }
1305
1306 public boolean showBeforeProvisioning() {
1307 return false;
1308 }
1309 }
1310
1311 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1312
Jason Monk16fbd9d2017-04-27 14:28:49 -04001313 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001314
1315 private final AudioManager mAudioManager;
1316 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001317
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001318 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001319 mAudioManager = audioManager;
1320 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001321 }
1322
1323 private int ringerModeToIndex(int ringerMode) {
1324 // They just happen to coincide
1325 return ringerMode;
1326 }
1327
1328 private int indexToRingerMode(int index) {
1329 // They just happen to coincide
1330 return index;
1331 }
1332
1333 @Override
1334 public CharSequence getLabelForAccessibility(Context context) {
1335 return null;
1336 }
1337
1338 public View create(Context context, View convertView, ViewGroup parent,
1339 LayoutInflater inflater) {
1340 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1341
1342 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1343 for (int i = 0; i < 3; i++) {
1344 View itemView = v.findViewById(ITEM_IDS[i]);
1345 itemView.setSelected(selectedIndex == i);
1346 // Set up click handler
1347 itemView.setTag(i);
1348 itemView.setOnClickListener(this);
1349 }
1350 return v;
1351 }
1352
1353 public void onPress() {
1354 }
1355
1356 public boolean showDuringKeyguard() {
1357 return true;
1358 }
1359
1360 public boolean showBeforeProvisioning() {
1361 return false;
1362 }
1363
1364 public boolean isEnabled() {
1365 return true;
1366 }
1367
1368 void willCreate() {
1369 }
1370
1371 public void onClick(View v) {
1372 if (!(v.getTag() instanceof Integer)) return;
1373
1374 int index = (Integer) v.getTag();
1375 mAudioManager.setRingerMode(indexToRingerMode(index));
1376 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1377 }
1378 }
1379
1380 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1381 public void onReceive(Context context, Intent intent) {
1382 String action = intent.getAction();
1383 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1384 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1385 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1386 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001387 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001388 }
1389 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1390 // Airplane mode can be changed after ECM exits if airplane toggle button
1391 // is pressed during ECM mode
1392 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1393 mIsWaitingForEcmExit) {
1394 mIsWaitingForEcmExit = false;
1395 changeAirplaneModeSystemSetting(true);
1396 }
1397 }
1398 }
1399 };
1400
1401 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1402 @Override
1403 public void onServiceStateChanged(ServiceState serviceState) {
1404 if (!mHasTelephony) return;
1405 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1406 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1407 mAirplaneModeOn.updateState(mAirplaneState);
1408 mAdapter.notifyDataSetChanged();
1409 }
1410 };
1411
1412 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1413 @Override
1414 public void onReceive(Context context, Intent intent) {
1415 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1416 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1417 }
1418 }
1419 };
1420
1421 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1422 @Override
1423 public void onChange(boolean selfChange) {
1424 onAirplaneModeChanged();
1425 }
1426 };
1427
1428 private static final int MESSAGE_DISMISS = 0;
1429 private static final int MESSAGE_REFRESH = 1;
1430 private static final int MESSAGE_SHOW = 2;
1431 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1432
1433 private Handler mHandler = new Handler() {
1434 public void handleMessage(Message msg) {
1435 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001436 case MESSAGE_DISMISS:
1437 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001438 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1439 mDialog.dismissImmediately();
1440 } else {
1441 mDialog.dismiss();
1442 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001443 mDialog = null;
1444 }
1445 break;
1446 case MESSAGE_REFRESH:
1447 refreshSilentMode();
1448 mAdapter.notifyDataSetChanged();
1449 break;
1450 case MESSAGE_SHOW:
1451 handleShow();
1452 break;
Jason Monk361915c2017-03-21 20:33:59 -04001453 }
1454 }
1455 };
1456
1457 private void onAirplaneModeChanged() {
1458 // Let the service state callbacks handle the state.
1459 if (mHasTelephony) return;
1460
1461 boolean airplaneModeOn = Settings.Global.getInt(
1462 mContext.getContentResolver(),
1463 Settings.Global.AIRPLANE_MODE_ON,
1464 0) == 1;
1465 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1466 mAirplaneModeOn.updateState(mAirplaneState);
1467 }
1468
1469 /**
1470 * Change the airplane mode system setting
1471 */
1472 private void changeAirplaneModeSystemSetting(boolean on) {
1473 Settings.Global.putInt(
1474 mContext.getContentResolver(),
1475 Settings.Global.AIRPLANE_MODE_ON,
1476 on ? 1 : 0);
1477 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1478 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1479 intent.putExtra("state", on);
1480 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1481 if (!mHasTelephony) {
1482 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1483 }
1484 }
1485
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001486 private static final class ActionsDialog extends Dialog implements DialogInterface,
1487 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001488
Jason Monk16fbd9d2017-04-27 14:28:49 -04001489 private final Context mContext;
1490 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001491 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001492 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001493 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001494 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001495 private boolean mKeyguardShowing;
Beverly526d2d62018-08-15 12:55:33 -04001496 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001497 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001498
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001499 ActionsDialog(Context context, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001500 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001501 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001502 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001503 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001504 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001505
1506 // Window initialization
1507 Window window = getWindow();
1508 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001509 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1510 window.getDecorView();
1511 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1512 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1513 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1514 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1515 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001516 window.addFlags(
1517 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001518 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001519 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001520 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1521 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1522 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001523 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1524
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001525 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001526
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001527 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001528
1529 mPanelController = plugin;
1530 View panelView = initializePanel();
1531 if (panelView == null) {
1532 mBackgroundDrawable = new GradientDrawable(context);
Steve Elliottbfa314a2019-03-06 13:53:19 -05001533 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
Steve Elliott9b87a442019-03-05 10:24:16 -05001534 } else {
1535 mBackgroundDrawable = context.getDrawable(
1536 com.android.systemui.R.drawable.global_action_panel_scrim);
1537 mScrimAlpha = 1f;
1538 addContentView(
1539 panelView,
1540 new ViewGroup.LayoutParams(
1541 ViewGroup.LayoutParams.MATCH_PARENT,
1542 ViewGroup.LayoutParams.MATCH_PARENT));
1543 }
1544 window.setBackgroundDrawable(mBackgroundDrawable);
1545 }
1546
1547 private View initializePanel() {
1548 if (isPanelEnabled(mContext) && mPanelController != null) {
1549 View panelView = mPanelController.getPanelContent();
1550 if (panelView != null) {
1551 FrameLayout panelContainer = new FrameLayout(mContext);
1552 FrameLayout.LayoutParams panelParams =
1553 new FrameLayout.LayoutParams(
1554 FrameLayout.LayoutParams.MATCH_PARENT,
1555 FrameLayout.LayoutParams.WRAP_CONTENT);
Steve Elliott9b87a442019-03-05 10:24:16 -05001556 panelContainer.addView(panelView, panelParams);
1557 return panelContainer;
1558 }
1559 }
1560 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001561 }
1562
1563 private void initializeLayout() {
1564 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001565 mGlobalActionsLayout = (MultiListLayout)
1566 findViewById(com.android.systemui.R.id.global_actions_view);
1567 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001568 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001569 @Override
1570 public boolean dispatchPopulateAccessibilityEvent(
1571 View host, AccessibilityEvent event) {
1572 // Populate the title here, just as Activity does
1573 event.getText().add(mContext.getString(R.string.global_actions));
1574 return true;
1575 }
1576 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001577 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001578 mGlobalActionsLayout.setAdapter(mAdapter);
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001579 mGlobalActionsLayout.setSnapToEdge(isPanelEnabled(mContext)
1580 && mPanelController != null);
Steve Elliott9b87a442019-03-05 10:24:16 -05001581 }
1582
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001583 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001584 if (isGridEnabled(context)) {
1585 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1586 return com.android.systemui.R.layout.global_actions_grid_seascape;
1587 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001588 return com.android.systemui.R.layout.global_actions_grid;
1589 }
1590 return com.android.systemui.R.layout.global_actions_wrapped;
1591 }
1592
Jason Monk361915c2017-03-21 20:33:59 -04001593 @Override
1594 protected void onStart() {
1595 super.setCanceledOnTouchOutside(true);
1596 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001597 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001598
Steve Elliott9b87a442019-03-05 10:24:16 -05001599 if (mBackgroundDrawable instanceof GradientDrawable) {
1600 Point displaySize = new Point();
1601 mContext.getDisplay().getRealSize(displaySize);
1602 mColorExtractor.addOnColorsChangedListener(this);
1603 ((GradientDrawable) mBackgroundDrawable)
1604 .setScreenSize(displaySize.x, displaySize.y);
1605 GradientColors colors = mColorExtractor.getColors(
1606 mKeyguardShowing
1607 ? WallpaperManager.FLAG_LOCK
1608 : WallpaperManager.FLAG_SYSTEM);
1609 updateColors(colors, false /* animate */);
1610 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001611 }
1612
1613 /**
1614 * Updates background and system bars according to current GradientColors.
1615 * @param colors Colors and hints to use.
1616 * @param animate Interpolates gradient if true, just sets otherwise.
1617 */
1618 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001619 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1620 return;
1621 }
1622 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001623 View decorView = getWindow().getDecorView();
1624 if (colors.supportsDarkText()) {
1625 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001626 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001627 } else {
1628 decorView.setSystemUiVisibility(0);
1629 }
Jason Monk361915c2017-03-21 20:33:59 -04001630 }
1631
1632 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001633 protected void onStop() {
1634 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001635 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001636 }
1637
1638 @Override
1639 public void show() {
1640 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001641 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001642 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001643 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1644 mGlobalActionsLayout.setAlpha(0);
1645 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001646 .alpha(1)
1647 .translationX(0)
1648 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001649 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001650 .setUpdateListener(animation -> {
1651 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001652 * mScrimAlpha * 255);
1653 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001654 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001655 .start();
1656 }
1657
1658 @Override
1659 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001660 if (!mShowing) {
1661 return;
1662 }
1663 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001664 mGlobalActionsLayout.setTranslationX(0);
1665 mGlobalActionsLayout.setAlpha(1);
1666 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001667 .alpha(0)
1668 .translationX(getAnimTranslation())
1669 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001670 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001671 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001672 .setUpdateListener(animation -> {
1673 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001674 * mScrimAlpha * 255);
1675 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001676 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001677 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001678 if (mPanelController != null) {
1679 mPanelController.onDismissed();
1680 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001681 }
1682
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001683 void dismissImmediately() {
1684 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001685 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001686 }
1687
Jason Monk16fbd9d2017-04-27 14:28:49 -04001688 private float getAnimTranslation() {
1689 return getContext().getResources().getDimension(
1690 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001691 }
1692
1693 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001694 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001695 if (mKeyguardShowing) {
1696 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001697 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1698 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001699 }
1700 } else {
1701 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001702 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1703 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001704 }
1705 }
1706 }
1707
1708 public void setKeyguardShowing(boolean keyguardShowing) {
1709 mKeyguardShowing = keyguardShowing;
1710 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001711
1712 public void onRotate(int from, int to) {
1713 if (mShowing && isGridEnabled(mContext)) {
1714 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001715 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001716 }
1717 }
Jason Monk361915c2017-03-21 20:33:59 -04001718 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001719
1720 /**
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001721 * Determines whether or not the Global Actions menu should use the newer grid-style layout.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001722 */
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001723 private static boolean isGridEnabled(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001724 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1725 }
Aaron Heuckrothf19d2722019-03-11 17:06:02 -04001726
1727 /**
1728 * Determines whether or not the Global Actions Panel should appear when the power button
1729 * is held.
1730 */
1731 private static boolean isPanelEnabled(Context context) {
1732 return FeatureFlagUtils.isEnabled(
1733 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED); }
1734
1735 /**
1736 * Determines whether the Global Actions menu should use a separated view for emergency actions.
1737 */
1738 private static boolean shouldUseSeparatedView() {
1739 return true;
1740 }
Jason Monk361915c2017-03-21 20:33:59 -04001741}