blob: 327325378e5dd997aae9a34254c140cea3ba521a [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;
41import android.os.Build;
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;
90import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050091import com.android.systemui.plugins.GlobalActionsPanelPlugin;
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;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500158 private boolean mUseSeparatedList;
Jason Monk361915c2017-03-21 20:33:59 -0400159 private final boolean mShowSilentToggle;
160 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500161 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400162 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400163
Steve Elliott9b87a442019-03-05 10:24:16 -0500164 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
165
Jason Monk361915c2017-03-21 20:33:59 -0400166 /**
167 * @param context everything needs a context :(
168 */
169 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700170 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400171 mWindowManagerFuncs = windowManagerFuncs;
172 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
173 mDreamManager = IDreamManager.Stub.asInterface(
174 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000175 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
176 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800177 mLockPatternUtils = new LockPatternUtils(mContext);
178 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400179
180 // receive broadcasts
181 IntentFilter filter = new IntentFilter();
182 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
183 filter.addAction(Intent.ACTION_SCREEN_OFF);
184 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
185 context.registerReceiver(mBroadcastReceiver, filter);
186
187 ConnectivityManager cm = (ConnectivityManager)
188 context.getSystemService(Context.CONNECTIVITY_SERVICE);
189 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
190
191 // get notified of phone state changes
192 TelephonyManager telephonyManager =
193 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
194 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
195 mContext.getContentResolver().registerContentObserver(
196 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
197 mAirplaneModeObserver);
198 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
199 mHasVibrator = vibrator != null && vibrator.hasVibrator();
200
201 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
202 R.bool.config_useFixedVolume);
203
204 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500205 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400206 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700207
208 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500209
210 mPanelExtension = Dependency.get(ExtensionController.class)
211 .newExtension(GlobalActionsPanelPlugin.class)
212 .withPlugin(GlobalActionsPanelPlugin.class)
213 .build();
Jason Monk361915c2017-03-21 20:33:59 -0400214 }
215
216 /**
217 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400218 *
Jason Monk361915c2017-03-21 20:33:59 -0400219 * @param keyguardShowing True if keyguard is showing
220 */
221 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
222 mKeyguardShowing = keyguardShowing;
223 mDeviceProvisioned = isDeviceProvisioned;
224 if (mDialog != null) {
225 mDialog.dismiss();
226 mDialog = null;
227 // Show delayed, so that the dismiss of the previous dialog completes
228 mHandler.sendEmptyMessage(MESSAGE_SHOW);
229 } else {
230 handleShow();
231 }
232 }
233
Charles He9851a8d2017-10-10 17:31:30 +0100234 /**
235 * Dismiss the global actions dialog, if it's currently shown
236 */
237 public void dismissDialog() {
238 mHandler.removeMessages(MESSAGE_DISMISS);
239 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
240 }
241
Jason Monk361915c2017-03-21 20:33:59 -0400242 private void awakenIfNecessary() {
243 if (mDreamManager != null) {
244 try {
245 if (mDreamManager.isDreaming()) {
246 mDreamManager.awaken();
247 }
248 } catch (RemoteException e) {
249 // we tried
250 }
251 }
252 }
253
254 private void handleShow() {
255 awakenIfNecessary();
256 mDialog = createDialog();
257 prepareDialog();
258
259 // If we only have 1 item and it's a simple press action, just do this action.
260 if (mAdapter.getCount() == 1
261 && mAdapter.getItem(0) instanceof SinglePressAction
262 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
263 ((SinglePressAction) mAdapter.getItem(0)).onPress();
264 } else {
265 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
266 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100267 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400268 mDialog.getWindow().setAttributes(attrs);
269 mDialog.show();
270 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400271 }
272 }
273
274 /**
275 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400276 *
Jason Monk361915c2017-03-21 20:33:59 -0400277 * @return A new dialog.
278 */
279 private ActionsDialog createDialog() {
280 // Simple toggle style if there's no vibrator, otherwise use a tri-state
281 if (!mHasVibrator) {
282 mSilentModeAction = new SilentModeToggleAction();
283 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700284 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400285 }
286 mAirplaneModeOn = new ToggleAction(
287 R.drawable.ic_lock_airplane_mode,
288 R.drawable.ic_lock_airplane_mode_off,
289 R.string.global_actions_toggle_airplane_mode,
290 R.string.global_actions_airplane_mode_on_status,
291 R.string.global_actions_airplane_mode_off_status) {
292
293 void onToggle(boolean on) {
294 if (mHasTelephony && Boolean.parseBoolean(
295 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
296 mIsWaitingForEcmExit = true;
297 // Launch ECM exit dialog
298 Intent ecmDialogIntent =
299 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
300 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
301 mContext.startActivity(ecmDialogIntent);
302 } else {
303 changeAirplaneModeSystemSetting(on);
304 }
305 }
306
307 @Override
308 protected void changeStateFromPress(boolean buttonOn) {
309 if (!mHasTelephony) return;
310
311 // In ECM mode airplane state cannot be changed
312 if (!(Boolean.parseBoolean(
313 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
314 mState = buttonOn ? State.TurningOn : State.TurningOff;
315 mAirplaneState = mState;
316 }
317 }
318
319 public boolean showDuringKeyguard() {
320 return true;
321 }
322
323 public boolean showBeforeProvisioning() {
324 return false;
325 }
326 };
327 onAirplaneModeChanged();
328
329 mItems = new ArrayList<Action>();
330 String[] defaultActions = mContext.getResources().getStringArray(
331 R.array.config_globalActionsList);
332
333 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000334 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800335 mHasLockdownButton = false;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500336 mUseSeparatedList = true;
Jason Monk361915c2017-03-21 20:33:59 -0400337 for (int i = 0; i < defaultActions.length; i++) {
338 String actionKey = defaultActions[i];
339 if (addedKeys.contains(actionKey)) {
340 // If we already have added this, don't add it again.
341 continue;
342 }
343 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
344 mItems.add(new PowerAction());
345 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
346 mItems.add(mAirplaneModeOn);
347 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
348 if (Settings.Global.getInt(mContext.getContentResolver(),
349 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
350 mItems.add(new BugReportAction());
351 }
352 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
353 if (mShowSilentToggle) {
354 mItems.add(mSilentModeAction);
355 }
356 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
357 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
358 addUsersToMenu(mItems);
359 }
360 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
361 mItems.add(getSettingsAction());
362 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700363 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
364 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800365 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700366 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800367 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700368 }
Jason Monk361915c2017-03-21 20:33:59 -0400369 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
370 mItems.add(getVoiceAssistAction());
371 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
372 mItems.add(getAssistAction());
373 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
374 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500375 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
376 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000377 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000378 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000379 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
380 mItems.add(new LogoutAction());
381 mHasLogoutButton = true;
382 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800383 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500384 if (mUseSeparatedList
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800385 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
386 mItems.add(new EmergencyDialerAction());
387 }
Jason Monk361915c2017-03-21 20:33:59 -0400388 } else {
389 Log.e(TAG, "Invalid global action key " + actionKey);
390 }
391 // Add here so we don't add more than one.
392 addedKeys.add(actionKey);
393 }
394
395 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
396 mItems.add(getEmergencyAction());
397 }
398
399 mAdapter = new MyAdapter();
400
Steve Elliott9b87a442019-03-05 10:24:16 -0500401 GlobalActionsPanelPlugin.PanelViewController panelViewController =
402 mPanelExtension.get() != null
403 ? mPanelExtension.get().onPanelShown(() -> {
404 if (mDialog != null) {
405 mDialog.dismiss();
406 }
407 })
408 : null;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500409 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, mUseSeparatedList,
410 panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400411 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700412 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400413
414 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800415 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400416
417 return dialog;
418 }
419
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800420 private boolean shouldDisplayLockdown() {
421 int userId = getCurrentUser().id;
422 // Lockdown is meaningless without a place to go.
423 if (!mKeyguardManager.isDeviceSecure(userId)) {
424 return false;
425 }
426
427 // Only show the lockdown button if the device isn't locked down (for whatever reason).
428 int state = mLockPatternUtils.getStrongAuthForUser(userId);
429 return (state == STRONG_AUTH_NOT_REQUIRED
430 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
431 }
432
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700433 @Override
434 public void onUiModeChanged() {
435 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
436 }
437
438 public void destroy() {
439 Dependency.get(ConfigurationController.class).removeCallback(this);
440 }
441
Jason Monk361915c2017-03-21 20:33:59 -0400442 private final class PowerAction extends SinglePressAction implements LongPressAction {
443 private PowerAction() {
444 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400445 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400446 }
447
448 @Override
449 public boolean onLongPress() {
450 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
451 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
452 mWindowManagerFuncs.reboot(true);
453 return true;
454 }
455 return false;
456 }
457
458 @Override
459 public boolean showDuringKeyguard() {
460 return true;
461 }
462
463 @Override
464 public boolean showBeforeProvisioning() {
465 return true;
466 }
467
468 @Override
469 public void onPress() {
470 // shutdown by making sure radio and power are handled accordingly.
471 mWindowManagerFuncs.shutdown();
472 }
473 }
474
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800475 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800476 private EmergencyDialerAction() {
477 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800478 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800479 }
480
481 @Override
482 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800483 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800484 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800485 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
486 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
487 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800488 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
489 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800490 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
491 }
492
493 @Override
494 public boolean showDuringKeyguard() {
495 return true;
496 }
497
498 @Override
499 public boolean showBeforeProvisioning() {
500 return true;
501 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500502
503 @Override
504 public boolean shouldBeSeparated() {
505 return true;
506 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800507 }
508
Jason Monk361915c2017-03-21 20:33:59 -0400509 private final class RestartAction extends SinglePressAction implements LongPressAction {
510 private RestartAction() {
511 super(R.drawable.ic_restart, R.string.global_action_restart);
512 }
513
514 @Override
515 public boolean onLongPress() {
516 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
517 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
518 mWindowManagerFuncs.reboot(true);
519 return true;
520 }
521 return false;
522 }
523
524 @Override
525 public boolean showDuringKeyguard() {
526 return true;
527 }
528
529 @Override
530 public boolean showBeforeProvisioning() {
531 return true;
532 }
533
534 @Override
535 public void onPress() {
536 mWindowManagerFuncs.reboot(false);
537 }
538 }
539
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400540 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500541 public ScreenshotAction() {
542 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
543 }
544
545 @Override
546 public void onPress() {
547 // Add a little delay before executing, to give the
548 // dialog a chance to go away before it takes a
549 // screenshot.
550 // TODO: instead, omit global action dialog layer
551 mHandler.postDelayed(new Runnable() {
552 @Override
553 public void run() {
554 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
555 MetricsLogger.action(mContext,
556 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
557 }
558 }, 500);
559 }
560
561 @Override
562 public boolean showDuringKeyguard() {
563 return true;
564 }
565
566 @Override
567 public boolean showBeforeProvisioning() {
568 return false;
569 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400570
571 @Override
572 public boolean onLongPress() {
573 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
574 mScreenRecordHelper.launchRecordPrompt();
575 } else {
576 onPress();
577 }
578 return true;
579 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500580 }
581
Jason Monk361915c2017-03-21 20:33:59 -0400582 private class BugReportAction extends SinglePressAction implements LongPressAction {
583
584 public BugReportAction() {
585 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
586 }
587
588 @Override
589 public void onPress() {
590 // don't actually trigger the bugreport if we are running stability
591 // tests via monkey
592 if (ActivityManager.isUserAMonkey()) {
593 return;
594 }
595 // Add a little delay before executing, to give the
596 // dialog a chance to go away before it takes a
597 // screenshot.
598 mHandler.postDelayed(new Runnable() {
599 @Override
600 public void run() {
601 try {
602 // Take an "interactive" bugreport.
603 MetricsLogger.action(mContext,
604 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
605 ActivityManager.getService().requestBugReport(
606 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
607 } catch (RemoteException e) {
608 }
609 }
610 }, 500);
611 }
612
613 @Override
614 public boolean onLongPress() {
615 // don't actually trigger the bugreport if we are running stability
616 // tests via monkey
617 if (ActivityManager.isUserAMonkey()) {
618 return false;
619 }
620 try {
621 // Take a "full" bugreport.
622 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
623 ActivityManager.getService().requestBugReport(
624 ActivityManager.BUGREPORT_OPTION_FULL);
625 } catch (RemoteException e) {
626 }
627 return false;
628 }
629
630 public boolean showDuringKeyguard() {
631 return true;
632 }
633
634 @Override
635 public boolean showBeforeProvisioning() {
636 return false;
637 }
638
639 @Override
640 public String getStatus() {
641 return mContext.getString(
642 R.string.bugreport_status,
643 Build.VERSION.RELEASE,
644 Build.ID);
645 }
646 }
647
Alex Chau04458852017-11-27 18:21:23 +0000648 private final class LogoutAction extends SinglePressAction {
649 private LogoutAction() {
650 super(R.drawable.ic_logout, R.string.global_action_logout);
651 }
652
653 @Override
654 public boolean showDuringKeyguard() {
655 return true;
656 }
657
658 @Override
659 public boolean showBeforeProvisioning() {
660 return false;
661 }
662
663 @Override
664 public void onPress() {
665 // Add a little delay before executing, to give the dialog a chance to go away before
666 // switching user
667 mHandler.postDelayed(() -> {
668 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000669 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000670 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000671 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000672 } catch (RemoteException re) {
673 Log.e(TAG, "Couldn't logout user " + re);
674 }
675 }, 500);
676 }
677 }
678
Jason Monk361915c2017-03-21 20:33:59 -0400679 private Action getSettingsAction() {
680 return new SinglePressAction(R.drawable.ic_settings,
681 R.string.global_action_settings) {
682
683 @Override
684 public void onPress() {
685 Intent intent = new Intent(Settings.ACTION_SETTINGS);
686 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
687 mContext.startActivity(intent);
688 }
689
690 @Override
691 public boolean showDuringKeyguard() {
692 return true;
693 }
694
695 @Override
696 public boolean showBeforeProvisioning() {
697 return true;
698 }
699 };
700 }
701
702 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800703 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500704 if (!mUseSeparatedList) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800705 // use un-colored legacy treatment
706 emergencyIcon.setTintList(null);
707 }
708
Jason Monk361915c2017-03-21 20:33:59 -0400709 return new SinglePressAction(R.drawable.emergency_icon,
710 R.string.global_action_emergency) {
711 @Override
712 public void onPress() {
713 mEmergencyAffordanceManager.performEmergencyCall();
714 }
715
716 @Override
717 public boolean showDuringKeyguard() {
718 return true;
719 }
720
721 @Override
722 public boolean showBeforeProvisioning() {
723 return true;
724 }
725 };
726 }
727
728 private Action getAssistAction() {
729 return new SinglePressAction(R.drawable.ic_action_assist_focused,
730 R.string.global_action_assist) {
731 @Override
732 public void onPress() {
733 Intent intent = new Intent(Intent.ACTION_ASSIST);
734 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
735 mContext.startActivity(intent);
736 }
737
738 @Override
739 public boolean showDuringKeyguard() {
740 return true;
741 }
742
743 @Override
744 public boolean showBeforeProvisioning() {
745 return true;
746 }
747 };
748 }
749
750 private Action getVoiceAssistAction() {
751 return new SinglePressAction(R.drawable.ic_voice_search,
752 R.string.global_action_voice_assist) {
753 @Override
754 public void onPress() {
755 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
756 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
757 mContext.startActivity(intent);
758 }
759
760 @Override
761 public boolean showDuringKeyguard() {
762 return true;
763 }
764
765 @Override
766 public boolean showBeforeProvisioning() {
767 return true;
768 }
769 };
770 }
771
772 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400773 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400774 R.string.global_action_lockdown) {
775
776 @Override
777 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700778 new LockPatternUtils(mContext)
779 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
780 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400781 try {
782 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100783 // Lock profiles (if any) on the background thread.
784 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
785 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400786 } catch (RemoteException e) {
787 Log.e(TAG, "Error while trying to lock device.", e);
788 }
789 }
790
791 @Override
792 public boolean showDuringKeyguard() {
793 return true;
794 }
795
796 @Override
797 public boolean showBeforeProvisioning() {
798 return false;
799 }
800 };
801 }
802
Pavel Grafov059021b2018-05-02 13:44:46 +0100803 private void lockProfiles() {
804 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
805 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
806 final int currentUserId = getCurrentUser().id;
807 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
808 for (final int id : profileIds) {
809 if (id != currentUserId) {
810 tm.setDeviceLockedForUser(id, true);
811 }
812 }
813 }
814
Jason Monk361915c2017-03-21 20:33:59 -0400815 private UserInfo getCurrentUser() {
816 try {
817 return ActivityManager.getService().getCurrentUser();
818 } catch (RemoteException re) {
819 return null;
820 }
821 }
822
823 private boolean isCurrentUserOwner() {
824 UserInfo currentUser = getCurrentUser();
825 return currentUser == null || currentUser.isPrimary();
826 }
827
828 private void addUsersToMenu(ArrayList<Action> items) {
829 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
830 if (um.isUserSwitcherEnabled()) {
831 List<UserInfo> users = um.getUsers();
832 UserInfo currentUser = getCurrentUser();
833 for (final UserInfo user : users) {
834 if (user.supportsSwitchToByUser()) {
835 boolean isCurrentUser = currentUser == null
836 ? user.id == 0 : (currentUser.id == user.id);
837 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
838 : null;
839 SinglePressAction switchToUser = new SinglePressAction(
840 R.drawable.ic_menu_cc, icon,
841 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400842 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400843 public void onPress() {
844 try {
845 ActivityManager.getService().switchUser(user.id);
846 } catch (RemoteException re) {
847 Log.e(TAG, "Couldn't switch user " + re);
848 }
849 }
850
851 public boolean showDuringKeyguard() {
852 return true;
853 }
854
855 public boolean showBeforeProvisioning() {
856 return false;
857 }
858 };
859 items.add(switchToUser);
860 }
861 }
862 }
863 }
864
865 private void prepareDialog() {
866 refreshSilentMode();
867 mAirplaneModeOn.updateState(mAirplaneState);
868 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400869 if (mShowSilentToggle) {
870 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
871 mContext.registerReceiver(mRingerModeReceiver, filter);
872 }
873 }
874
875 private void refreshSilentMode() {
876 if (!mHasVibrator) {
877 final boolean silentModeOn =
878 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400879 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400880 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
881 }
882 }
883
884 /** {@inheritDoc} */
885 public void onDismiss(DialogInterface dialog) {
886 mWindowManagerFuncs.onGlobalActionsHidden();
887 if (mShowSilentToggle) {
888 try {
889 mContext.unregisterReceiver(mRingerModeReceiver);
890 } catch (IllegalArgumentException ie) {
891 // ignore this
892 Log.w(TAG, ie);
893 }
894 }
895 }
896
897 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800898 public void onShow(DialogInterface dialog) {
899 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
900 }
901
Jason Monk361915c2017-03-21 20:33:59 -0400902 /**
903 * The adapter used for the list within the global actions dialog, taking
904 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400905 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
906 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400907 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
908 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500909 public class MyAdapter extends MultiListAdapter {
910 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400911 public int getCount() {
912 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400913 for (int i = 0; i < mItems.size(); i++) {
914 final Action action = mItems.get(i);
915
916 if (mKeyguardShowing && !action.showDuringKeyguard()) {
917 continue;
918 }
919 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
920 continue;
921 }
922 count++;
923 }
924 return count;
925 }
926
927 @Override
928 public boolean isEnabled(int position) {
929 return getItem(position).isEnabled();
930 }
931
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500932 @Override
933 public ArrayList<Action> getSeparatedItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500934 ArrayList<Action> separatedActions = new ArrayList<Action>();
935 if (!shouldUseSeparatedView) {
936 return separatedActions;
937 }
938 for (int i = 0; i < mItems.size(); i++) {
939 final Action action = mItems.get(i);
940 if (action.shouldBeSeparated()) {
941 separatedActions.add(action);
942 }
943 }
944 return separatedActions;
945 }
946
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500947 @Override
948 public ArrayList<Action> getListItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500949 if (!shouldUseSeparatedView) {
950 return new ArrayList<Action>(mItems);
951 }
952 ArrayList<Action> listActions = new ArrayList<Action>();
953 for (int i = 0; i < mItems.size(); i++) {
954 final Action action = mItems.get(i);
955 if (!action.shouldBeSeparated()) {
956 listActions.add(action);
957 }
958 }
959 return listActions;
960 }
961
Jason Monk361915c2017-03-21 20:33:59 -0400962 @Override
963 public boolean areAllItemsEnabled() {
964 return false;
965 }
966
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500967 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400968 public Action getItem(int position) {
969
970 int filteredPos = 0;
971 for (int i = 0; i < mItems.size(); i++) {
972 final Action action = mItems.get(i);
973 if (mKeyguardShowing && !action.showDuringKeyguard()) {
974 continue;
975 }
976 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
977 continue;
978 }
979 if (filteredPos == position) {
980 return action;
981 }
982 filteredPos++;
983 }
984
985 throw new IllegalArgumentException("position " + position
986 + " out of range of showable actions"
987 + ", filtered count=" + getCount()
988 + ", keyguardshowing=" + mKeyguardShowing
989 + ", provisioned=" + mDeviceProvisioned);
990 }
991
992
993 public long getItemId(int position) {
994 return position;
995 }
996
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500997 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400998 public View getView(int position, View convertView, ViewGroup parent) {
999 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001000 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001001 // Everything but screenshot, the last item, gets white background.
1002 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001003 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001004 }
1005 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001006 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001007
1008 @Override
1009 public boolean onLongClickItem(int position) {
1010 final Action action = mAdapter.getItem(position);
1011 if (action instanceof LongPressAction) {
1012 mDialog.dismiss();
1013 return ((LongPressAction) action).onLongPress();
1014 }
1015 return false;
1016 }
1017
1018 @Override
1019 public void onClickItem(int position) {
1020 Action item = mAdapter.getItem(position);
1021 if (!(item instanceof SilentModeTriStateAction)) {
1022 mDialog.dismiss();
1023 }
1024 item.onPress();
1025 }
Jason Monk361915c2017-03-21 20:33:59 -04001026 }
1027
1028 // note: the scheme below made more sense when we were planning on having
1029 // 8 different things in the global actions dialog. seems overkill with
1030 // only 3 items now, but may as well keep this flexible approach so it will
1031 // be easy should someone decide at the last minute to include something
1032 // else, such as 'enable wifi', or 'enable bluetooth'
1033
1034 /**
1035 * What each item in the global actions dialog must be able to support.
1036 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001037 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001038 /**
1039 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001040 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001041 */
1042 CharSequence getLabelForAccessibility(Context context);
1043
1044 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1045
1046 void onPress();
1047
1048 /**
1049 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001050 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001051 */
1052 boolean showDuringKeyguard();
1053
1054 /**
1055 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001056 * device is provisioned.onlongpress
1057 *
Jason Monk361915c2017-03-21 20:33:59 -04001058 */
1059 boolean showBeforeProvisioning();
1060
1061 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001062
1063 default boolean shouldBeSeparated() {
1064 return false;
1065 }
Jason Monk361915c2017-03-21 20:33:59 -04001066 }
1067
1068 /**
1069 * An action that also supports long press.
1070 */
1071 private interface LongPressAction extends Action {
1072 boolean onLongPress();
1073 }
1074
1075 /**
1076 * A single press action maintains no state, just responds to a press
1077 * and takes an action.
1078 */
1079 private static abstract class SinglePressAction implements Action {
1080 private final int mIconResId;
1081 private final Drawable mIcon;
1082 private final int mMessageResId;
1083 private final CharSequence mMessage;
1084
1085 protected SinglePressAction(int iconResId, int messageResId) {
1086 mIconResId = iconResId;
1087 mMessageResId = messageResId;
1088 mMessage = null;
1089 mIcon = null;
1090 }
1091
1092 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1093 mIconResId = iconResId;
1094 mMessageResId = 0;
1095 mMessage = message;
1096 mIcon = icon;
1097 }
1098
1099 public boolean isEnabled() {
1100 return true;
1101 }
1102
1103 public String getStatus() {
1104 return null;
1105 }
1106
1107 abstract public void onPress();
1108
1109 public CharSequence getLabelForAccessibility(Context context) {
1110 if (mMessage != null) {
1111 return mMessage;
1112 } else {
1113 return context.getString(mMessageResId);
1114 }
1115 }
1116
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001117 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001118 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001119 return com.android.systemui.R.layout.global_actions_grid_item;
1120 }
1121 return com.android.systemui.R.layout.global_actions_item;
1122 }
1123
Jason Monk361915c2017-03-21 20:33:59 -04001124 public View create(
1125 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001126 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001127 false);
Jason Monk361915c2017-03-21 20:33:59 -04001128
1129 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1130 TextView messageView = (TextView) v.findViewById(R.id.message);
1131
1132 TextView statusView = (TextView) v.findViewById(R.id.status);
1133 final String status = getStatus();
1134 if (!TextUtils.isEmpty(status)) {
1135 statusView.setText(status);
1136 } else {
1137 statusView.setVisibility(View.GONE);
1138 }
1139 if (mIcon != null) {
1140 icon.setImageDrawable(mIcon);
1141 icon.setScaleType(ScaleType.CENTER_CROP);
1142 } else if (mIconResId != 0) {
1143 icon.setImageDrawable(context.getDrawable(mIconResId));
1144 }
1145 if (mMessage != null) {
1146 messageView.setText(mMessage);
1147 } else {
1148 messageView.setText(mMessageResId);
1149 }
1150
1151 return v;
1152 }
1153 }
1154
1155 /**
1156 * A toggle action knows whether it is on or off, and displays an icon
1157 * and status message accordingly.
1158 */
1159 private static abstract class ToggleAction implements Action {
1160
1161 enum State {
1162 Off(false),
1163 TurningOn(true),
1164 TurningOff(true),
1165 On(false);
1166
1167 private final boolean inTransition;
1168
1169 State(boolean intermediate) {
1170 inTransition = intermediate;
1171 }
1172
1173 public boolean inTransition() {
1174 return inTransition;
1175 }
1176 }
1177
1178 protected State mState = State.Off;
1179
1180 // prefs
1181 protected int mEnabledIconResId;
1182 protected int mDisabledIconResid;
1183 protected int mMessageResId;
1184 protected int mEnabledStatusMessageResId;
1185 protected int mDisabledStatusMessageResId;
1186
1187 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001188 * @param enabledIconResId The icon for when this action is on.
1189 * @param disabledIconResid The icon for when this action is off.
1190 * @param message The general information message, e.g 'Silent Mode'
1191 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001192 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1193 */
1194 public ToggleAction(int enabledIconResId,
1195 int disabledIconResid,
1196 int message,
1197 int enabledStatusMessageResId,
1198 int disabledStatusMessageResId) {
1199 mEnabledIconResId = enabledIconResId;
1200 mDisabledIconResid = disabledIconResid;
1201 mMessageResId = message;
1202 mEnabledStatusMessageResId = enabledStatusMessageResId;
1203 mDisabledStatusMessageResId = disabledStatusMessageResId;
1204 }
1205
1206 /**
1207 * Override to make changes to resource IDs just before creating the
1208 * View.
1209 */
1210 void willCreate() {
1211
1212 }
1213
1214 @Override
1215 public CharSequence getLabelForAccessibility(Context context) {
1216 return context.getString(mMessageResId);
1217 }
1218
1219 public View create(Context context, View convertView, ViewGroup parent,
1220 LayoutInflater inflater) {
1221 willCreate();
1222
1223 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001224 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001225
1226 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1227 TextView messageView = (TextView) v.findViewById(R.id.message);
1228 TextView statusView = (TextView) v.findViewById(R.id.status);
1229 final boolean enabled = isEnabled();
1230
1231 if (messageView != null) {
1232 messageView.setText(mMessageResId);
1233 messageView.setEnabled(enabled);
1234 }
1235
1236 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1237 if (icon != null) {
1238 icon.setImageDrawable(context.getDrawable(
1239 (on ? mEnabledIconResId : mDisabledIconResid)));
1240 icon.setEnabled(enabled);
1241 }
1242
1243 if (statusView != null) {
1244 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1245 statusView.setVisibility(View.VISIBLE);
1246 statusView.setEnabled(enabled);
1247 }
1248 v.setEnabled(enabled);
1249
1250 return v;
1251 }
1252
1253 public final void onPress() {
1254 if (mState.inTransition()) {
1255 Log.w(TAG, "shouldn't be able to toggle when in transition");
1256 return;
1257 }
1258
1259 final boolean nowOn = !(mState == State.On);
1260 onToggle(nowOn);
1261 changeStateFromPress(nowOn);
1262 }
1263
1264 public boolean isEnabled() {
1265 return !mState.inTransition();
1266 }
1267
1268 /**
1269 * Implementations may override this if their state can be in on of the intermediate
1270 * states until some notification is received (e.g airplane mode is 'turning off' until
1271 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001272 *
Jason Monk361915c2017-03-21 20:33:59 -04001273 * @param buttonOn Whether the button was turned on or off
1274 */
1275 protected void changeStateFromPress(boolean buttonOn) {
1276 mState = buttonOn ? State.On : State.Off;
1277 }
1278
1279 abstract void onToggle(boolean on);
1280
1281 public void updateState(State state) {
1282 mState = state;
1283 }
1284 }
1285
1286 private class SilentModeToggleAction extends ToggleAction {
1287 public SilentModeToggleAction() {
1288 super(R.drawable.ic_audio_vol_mute,
1289 R.drawable.ic_audio_vol,
1290 R.string.global_action_toggle_silent_mode,
1291 R.string.global_action_silent_mode_on_status,
1292 R.string.global_action_silent_mode_off_status);
1293 }
1294
1295 void onToggle(boolean on) {
1296 if (on) {
1297 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1298 } else {
1299 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1300 }
1301 }
1302
1303 public boolean showDuringKeyguard() {
1304 return true;
1305 }
1306
1307 public boolean showBeforeProvisioning() {
1308 return false;
1309 }
1310 }
1311
1312 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1313
Jason Monk16fbd9d2017-04-27 14:28:49 -04001314 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001315
1316 private final AudioManager mAudioManager;
1317 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001318
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001319 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001320 mAudioManager = audioManager;
1321 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001322 }
1323
1324 private int ringerModeToIndex(int ringerMode) {
1325 // They just happen to coincide
1326 return ringerMode;
1327 }
1328
1329 private int indexToRingerMode(int index) {
1330 // They just happen to coincide
1331 return index;
1332 }
1333
1334 @Override
1335 public CharSequence getLabelForAccessibility(Context context) {
1336 return null;
1337 }
1338
1339 public View create(Context context, View convertView, ViewGroup parent,
1340 LayoutInflater inflater) {
1341 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1342
1343 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1344 for (int i = 0; i < 3; i++) {
1345 View itemView = v.findViewById(ITEM_IDS[i]);
1346 itemView.setSelected(selectedIndex == i);
1347 // Set up click handler
1348 itemView.setTag(i);
1349 itemView.setOnClickListener(this);
1350 }
1351 return v;
1352 }
1353
1354 public void onPress() {
1355 }
1356
1357 public boolean showDuringKeyguard() {
1358 return true;
1359 }
1360
1361 public boolean showBeforeProvisioning() {
1362 return false;
1363 }
1364
1365 public boolean isEnabled() {
1366 return true;
1367 }
1368
1369 void willCreate() {
1370 }
1371
1372 public void onClick(View v) {
1373 if (!(v.getTag() instanceof Integer)) return;
1374
1375 int index = (Integer) v.getTag();
1376 mAudioManager.setRingerMode(indexToRingerMode(index));
1377 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1378 }
1379 }
1380
1381 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1382 public void onReceive(Context context, Intent intent) {
1383 String action = intent.getAction();
1384 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1385 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1386 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1387 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001388 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001389 }
1390 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1391 // Airplane mode can be changed after ECM exits if airplane toggle button
1392 // is pressed during ECM mode
1393 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1394 mIsWaitingForEcmExit) {
1395 mIsWaitingForEcmExit = false;
1396 changeAirplaneModeSystemSetting(true);
1397 }
1398 }
1399 }
1400 };
1401
1402 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1403 @Override
1404 public void onServiceStateChanged(ServiceState serviceState) {
1405 if (!mHasTelephony) return;
1406 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1407 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1408 mAirplaneModeOn.updateState(mAirplaneState);
1409 mAdapter.notifyDataSetChanged();
1410 }
1411 };
1412
1413 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1414 @Override
1415 public void onReceive(Context context, Intent intent) {
1416 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1417 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1418 }
1419 }
1420 };
1421
1422 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1423 @Override
1424 public void onChange(boolean selfChange) {
1425 onAirplaneModeChanged();
1426 }
1427 };
1428
1429 private static final int MESSAGE_DISMISS = 0;
1430 private static final int MESSAGE_REFRESH = 1;
1431 private static final int MESSAGE_SHOW = 2;
1432 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1433
1434 private Handler mHandler = new Handler() {
1435 public void handleMessage(Message msg) {
1436 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001437 case MESSAGE_DISMISS:
1438 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001439 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1440 mDialog.dismissImmediately();
1441 } else {
1442 mDialog.dismiss();
1443 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001444 mDialog = null;
1445 }
1446 break;
1447 case MESSAGE_REFRESH:
1448 refreshSilentMode();
1449 mAdapter.notifyDataSetChanged();
1450 break;
1451 case MESSAGE_SHOW:
1452 handleShow();
1453 break;
Jason Monk361915c2017-03-21 20:33:59 -04001454 }
1455 }
1456 };
1457
1458 private void onAirplaneModeChanged() {
1459 // Let the service state callbacks handle the state.
1460 if (mHasTelephony) return;
1461
1462 boolean airplaneModeOn = Settings.Global.getInt(
1463 mContext.getContentResolver(),
1464 Settings.Global.AIRPLANE_MODE_ON,
1465 0) == 1;
1466 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1467 mAirplaneModeOn.updateState(mAirplaneState);
1468 }
1469
1470 /**
1471 * Change the airplane mode system setting
1472 */
1473 private void changeAirplaneModeSystemSetting(boolean on) {
1474 Settings.Global.putInt(
1475 mContext.getContentResolver(),
1476 Settings.Global.AIRPLANE_MODE_ON,
1477 on ? 1 : 0);
1478 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1479 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1480 intent.putExtra("state", on);
1481 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1482 if (!mHasTelephony) {
1483 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1484 }
1485 }
1486
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001487 private static final class ActionsDialog extends Dialog implements DialogInterface,
1488 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001489
Jason Monk16fbd9d2017-04-27 14:28:49 -04001490 private final Context mContext;
1491 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001492 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001493 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001494 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001495 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001496 private boolean mKeyguardShowing;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001497 private boolean mUseSeparatedList;
Beverly526d2d62018-08-15 12:55:33 -04001498 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001499 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001500
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001501 ActionsDialog(Context context, MyAdapter adapter, boolean separated,
Steve Elliott9b87a442019-03-05 10:24:16 -05001502 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001503 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001504 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001505 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001506 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001507 mUseSeparatedList = separated;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001508
1509 // Window initialization
1510 Window window = getWindow();
1511 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001512 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1513 window.getDecorView();
1514 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1515 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1516 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1517 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1518 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001519 window.addFlags(
1520 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001521 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001522 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001523 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1524 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1525 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001526 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1527
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001528 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001529
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001530 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001531
1532 mPanelController = plugin;
1533 View panelView = initializePanel();
1534 if (panelView == null) {
1535 mBackgroundDrawable = new GradientDrawable(context);
1536 mScrimAlpha = 0.7f;
1537 } else {
1538 mBackgroundDrawable = context.getDrawable(
1539 com.android.systemui.R.drawable.global_action_panel_scrim);
1540 mScrimAlpha = 1f;
1541 addContentView(
1542 panelView,
1543 new ViewGroup.LayoutParams(
1544 ViewGroup.LayoutParams.MATCH_PARENT,
1545 ViewGroup.LayoutParams.MATCH_PARENT));
1546 }
1547 window.setBackgroundDrawable(mBackgroundDrawable);
1548 }
1549
1550 private View initializePanel() {
1551 if (isPanelEnabled(mContext) && mPanelController != null) {
1552 View panelView = mPanelController.getPanelContent();
1553 if (panelView != null) {
1554 FrameLayout panelContainer = new FrameLayout(mContext);
1555 FrameLayout.LayoutParams panelParams =
1556 new FrameLayout.LayoutParams(
1557 FrameLayout.LayoutParams.MATCH_PARENT,
1558 FrameLayout.LayoutParams.WRAP_CONTENT);
1559 panelParams.topMargin = mContext.getResources().getDimensionPixelSize(
1560 com.android.systemui.R.dimen.global_actions_panel_top_margin);
1561 panelContainer.addView(panelView, panelParams);
1562 return panelContainer;
1563 }
1564 }
1565 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001566 }
1567
1568 private void initializeLayout() {
1569 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001570 mGlobalActionsLayout = (MultiListLayout)
1571 findViewById(com.android.systemui.R.id.global_actions_view);
1572 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001573 mGlobalActionsLayout.setSeparated(mUseSeparatedList);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001574 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001575 @Override
1576 public boolean dispatchPopulateAccessibilityEvent(
1577 View host, AccessibilityEvent event) {
1578 // Populate the title here, just as Activity does
1579 event.getText().add(mContext.getString(R.string.global_actions));
1580 return true;
1581 }
1582 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001583 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001584 mGlobalActionsLayout.setAdapter(mAdapter);
Jason Monk361915c2017-03-21 20:33:59 -04001585 }
1586
Steve Elliott9b87a442019-03-05 10:24:16 -05001587 private boolean isPanelEnabled(Context context) {
1588 return FeatureFlagUtils.isEnabled(
1589 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1590 }
1591
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001592 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001593 if (isGridEnabled(context)) {
1594 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1595 return com.android.systemui.R.layout.global_actions_grid_seascape;
1596 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001597 return com.android.systemui.R.layout.global_actions_grid;
1598 }
1599 return com.android.systemui.R.layout.global_actions_wrapped;
1600 }
1601
Jason Monk361915c2017-03-21 20:33:59 -04001602 @Override
1603 protected void onStart() {
1604 super.setCanceledOnTouchOutside(true);
1605 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001606 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001607
Steve Elliott9b87a442019-03-05 10:24:16 -05001608 if (mBackgroundDrawable instanceof GradientDrawable) {
1609 Point displaySize = new Point();
1610 mContext.getDisplay().getRealSize(displaySize);
1611 mColorExtractor.addOnColorsChangedListener(this);
1612 ((GradientDrawable) mBackgroundDrawable)
1613 .setScreenSize(displaySize.x, displaySize.y);
1614 GradientColors colors = mColorExtractor.getColors(
1615 mKeyguardShowing
1616 ? WallpaperManager.FLAG_LOCK
1617 : WallpaperManager.FLAG_SYSTEM);
1618 updateColors(colors, false /* animate */);
1619 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001620 }
1621
1622 /**
1623 * Updates background and system bars according to current GradientColors.
1624 * @param colors Colors and hints to use.
1625 * @param animate Interpolates gradient if true, just sets otherwise.
1626 */
1627 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001628 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1629 return;
1630 }
1631 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001632 View decorView = getWindow().getDecorView();
1633 if (colors.supportsDarkText()) {
1634 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001635 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001636 } else {
1637 decorView.setSystemUiVisibility(0);
1638 }
Jason Monk361915c2017-03-21 20:33:59 -04001639 }
1640
1641 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001642 protected void onStop() {
1643 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001644 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001645 }
1646
1647 @Override
1648 public void show() {
1649 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001650 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001651 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001652 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1653 mGlobalActionsLayout.setAlpha(0);
1654 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001655 .alpha(1)
1656 .translationX(0)
1657 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001658 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001659 .setUpdateListener(animation -> {
1660 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001661 * mScrimAlpha * 255);
1662 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001663 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001664 .start();
1665 }
1666
1667 @Override
1668 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001669 if (!mShowing) {
1670 return;
1671 }
1672 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001673 mGlobalActionsLayout.setTranslationX(0);
1674 mGlobalActionsLayout.setAlpha(1);
1675 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001676 .alpha(0)
1677 .translationX(getAnimTranslation())
1678 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001679 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001680 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001681 .setUpdateListener(animation -> {
1682 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001683 * mScrimAlpha * 255);
1684 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001685 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001686 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001687 if (mPanelController != null) {
1688 mPanelController.onDismissed();
1689 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001690 }
1691
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001692 void dismissImmediately() {
1693 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001694 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001695 }
1696
Jason Monk16fbd9d2017-04-27 14:28:49 -04001697 private float getAnimTranslation() {
1698 return getContext().getResources().getDimension(
1699 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001700 }
1701
1702 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001703 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001704 if (mKeyguardShowing) {
1705 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001706 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1707 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001708 }
1709 } else {
1710 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001711 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1712 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001713 }
1714 }
1715 }
1716
1717 public void setKeyguardShowing(boolean keyguardShowing) {
1718 mKeyguardShowing = keyguardShowing;
1719 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001720
1721 public void onRotate(int from, int to) {
1722 if (mShowing && isGridEnabled(mContext)) {
1723 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001724 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001725 }
1726 }
Jason Monk361915c2017-03-21 20:33:59 -04001727 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001728
1729 /**
1730 * Determines whether or not the Global Actions Dialog should use the newer grid-style layout.
1731 */
1732 public static boolean isGridEnabled(Context context) {
1733 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1734 }
Jason Monk361915c2017-03-21 20:33:59 -04001735}