blob: 8a95cc4910adb1daa4d8082f9b0bbb9f47a6ace8 [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;
Jason Monk16fbd9d2017-04-27 14:28:49 -040068import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.widget.BaseAdapter;
Steve Elliott9b87a442019-03-05 10:24:16 -050070import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.ImageView;
72import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.TextView;
74
Charles He9851a8d2017-10-10 17:31:30 +010075import com.android.internal.R;
76import com.android.internal.colorextraction.ColorExtractor;
77import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070078import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010079import com.android.internal.logging.MetricsLogger;
80import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
81import com.android.internal.telephony.TelephonyIntents;
82import com.android.internal.telephony.TelephonyProperties;
83import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040084import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050085import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.internal.widget.LockPatternUtils;
87import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050089import com.android.systemui.MultiListLayout;
Charles He9851a8d2017-10-10 17:31:30 +010090import com.android.systemui.colorextraction.SysuiColorExtractor;
91import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Steve Elliott9b87a442019-03-05 10:24:16 -050092import com.android.systemui.plugins.GlobalActionsPanelPlugin;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070093import com.android.systemui.statusbar.policy.ConfigurationController;
Steve Elliott9b87a442019-03-05 10:24:16 -050094import com.android.systemui.statusbar.policy.ExtensionController;
95import com.android.systemui.statusbar.policy.ExtensionController.Extension;
Shaotang Li5c422632018-07-04 14:18:40 +080096import com.android.systemui.util.EmergencyDialerConstants;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050097import com.android.systemui.util.leak.RotationUtils;
Julia Reynolds42411922017-11-08 11:19:09 -050098import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070099
Jason Monk361915c2017-03-21 20:33:59 -0400100import java.util.ArrayList;
101import java.util.List;
Aaron Heuckrotheeda8be2019-03-04 11:01:05 -0500102import java.util.Locale;
Jason Monk361915c2017-03-21 20:33:59 -0400103
104/**
105 * Helper to show the global actions dialog. Each item is an {@link Action} that
106 * may show depending on whether the keyguard is showing, and whether the device
107 * is provisioned.
108 */
Charles He9851a8d2017-10-10 17:31:30 +0100109class GlobalActionsDialog implements DialogInterface.OnDismissListener,
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700110 DialogInterface.OnClickListener, DialogInterface.OnShowListener,
111 ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400112
113 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
114 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700115 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400116
117 private static final String TAG = "GlobalActionsDialog";
118
119 private static final boolean SHOW_SILENT_TOGGLE = true;
120
121 /* Valid settings for global actions keys.
122 * see config.xml config_globalActionList */
123 private static final String GLOBAL_ACTION_KEY_POWER = "power";
124 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
125 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
126 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
127 private static final String GLOBAL_ACTION_KEY_USERS = "users";
128 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
129 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
130 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
131 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
132 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000133 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800134 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500135 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400136
137 private final Context mContext;
138 private final GlobalActionsManager mWindowManagerFuncs;
139 private final AudioManager mAudioManager;
140 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000141 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800142 private final LockPatternUtils mLockPatternUtils;
143 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400144
145 private ArrayList<Action> mItems;
146 private ActionsDialog mDialog;
147
148 private Action mSilentModeAction;
149 private ToggleAction mAirplaneModeOn;
150
151 private MyAdapter mAdapter;
152
153 private boolean mKeyguardShowing = false;
154 private boolean mDeviceProvisioned = false;
155 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
156 private boolean mIsWaitingForEcmExit = false;
157 private boolean mHasTelephony;
158 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000159 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800160 private boolean mHasLockdownButton;
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800161 private boolean mSeparatedEmergencyButtonEnabled;
Jason Monk361915c2017-03-21 20:33:59 -0400162 private final boolean mShowSilentToggle;
163 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500164 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400165 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400166
Steve Elliott9b87a442019-03-05 10:24:16 -0500167 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
168
Jason Monk361915c2017-03-21 20:33:59 -0400169 /**
170 * @param context everything needs a context :(
171 */
172 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700173 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400174 mWindowManagerFuncs = windowManagerFuncs;
175 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
176 mDreamManager = IDreamManager.Stub.asInterface(
177 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000178 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
179 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800180 mLockPatternUtils = new LockPatternUtils(mContext);
181 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400182
183 // receive broadcasts
184 IntentFilter filter = new IntentFilter();
185 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
186 filter.addAction(Intent.ACTION_SCREEN_OFF);
187 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
188 context.registerReceiver(mBroadcastReceiver, filter);
189
190 ConnectivityManager cm = (ConnectivityManager)
191 context.getSystemService(Context.CONNECTIVITY_SERVICE);
192 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
193
194 // get notified of phone state changes
195 TelephonyManager telephonyManager =
196 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
197 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
198 mContext.getContentResolver().registerContentObserver(
199 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
200 mAirplaneModeObserver);
201 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
202 mHasVibrator = vibrator != null && vibrator.hasVibrator();
203
204 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
205 R.bool.config_useFixedVolume);
206
207 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500208 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400209 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700210
211 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500212
213 mPanelExtension = Dependency.get(ExtensionController.class)
214 .newExtension(GlobalActionsPanelPlugin.class)
215 .withPlugin(GlobalActionsPanelPlugin.class)
216 .build();
Jason Monk361915c2017-03-21 20:33:59 -0400217 }
218
219 /**
220 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400221 *
Jason Monk361915c2017-03-21 20:33:59 -0400222 * @param keyguardShowing True if keyguard is showing
223 */
224 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
225 mKeyguardShowing = keyguardShowing;
226 mDeviceProvisioned = isDeviceProvisioned;
227 if (mDialog != null) {
228 mDialog.dismiss();
229 mDialog = null;
230 // Show delayed, so that the dismiss of the previous dialog completes
231 mHandler.sendEmptyMessage(MESSAGE_SHOW);
232 } else {
233 handleShow();
234 }
235 }
236
Charles He9851a8d2017-10-10 17:31:30 +0100237 /**
238 * Dismiss the global actions dialog, if it's currently shown
239 */
240 public void dismissDialog() {
241 mHandler.removeMessages(MESSAGE_DISMISS);
242 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
243 }
244
Jason Monk361915c2017-03-21 20:33:59 -0400245 private void awakenIfNecessary() {
246 if (mDreamManager != null) {
247 try {
248 if (mDreamManager.isDreaming()) {
249 mDreamManager.awaken();
250 }
251 } catch (RemoteException e) {
252 // we tried
253 }
254 }
255 }
256
257 private void handleShow() {
258 awakenIfNecessary();
259 mDialog = createDialog();
260 prepareDialog();
261
262 // If we only have 1 item and it's a simple press action, just do this action.
263 if (mAdapter.getCount() == 1
264 && mAdapter.getItem(0) instanceof SinglePressAction
265 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
266 ((SinglePressAction) mAdapter.getItem(0)).onPress();
267 } else {
268 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
269 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100270 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400271 mDialog.getWindow().setAttributes(attrs);
272 mDialog.show();
273 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400274 }
275 }
276
277 /**
278 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400279 *
Jason Monk361915c2017-03-21 20:33:59 -0400280 * @return A new dialog.
281 */
282 private ActionsDialog createDialog() {
283 // Simple toggle style if there's no vibrator, otherwise use a tri-state
284 if (!mHasVibrator) {
285 mSilentModeAction = new SilentModeToggleAction();
286 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700287 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400288 }
289 mAirplaneModeOn = new ToggleAction(
290 R.drawable.ic_lock_airplane_mode,
291 R.drawable.ic_lock_airplane_mode_off,
292 R.string.global_actions_toggle_airplane_mode,
293 R.string.global_actions_airplane_mode_on_status,
294 R.string.global_actions_airplane_mode_off_status) {
295
296 void onToggle(boolean on) {
297 if (mHasTelephony && Boolean.parseBoolean(
298 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
299 mIsWaitingForEcmExit = true;
300 // Launch ECM exit dialog
301 Intent ecmDialogIntent =
302 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
303 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
304 mContext.startActivity(ecmDialogIntent);
305 } else {
306 changeAirplaneModeSystemSetting(on);
307 }
308 }
309
310 @Override
311 protected void changeStateFromPress(boolean buttonOn) {
312 if (!mHasTelephony) return;
313
314 // In ECM mode airplane state cannot be changed
315 if (!(Boolean.parseBoolean(
316 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
317 mState = buttonOn ? State.TurningOn : State.TurningOff;
318 mAirplaneState = mState;
319 }
320 }
321
322 public boolean showDuringKeyguard() {
323 return true;
324 }
325
326 public boolean showBeforeProvisioning() {
327 return false;
328 }
329 };
330 onAirplaneModeChanged();
331
332 mItems = new ArrayList<Action>();
333 String[] defaultActions = mContext.getResources().getStringArray(
334 R.array.config_globalActionsList);
335
336 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000337 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800338 mHasLockdownButton = false;
yuanjiahsu2c69b0b2018-11-16 21:37:33 +0800339 mSeparatedEmergencyButtonEnabled = true;
Jason Monk361915c2017-03-21 20:33:59 -0400340 for (int i = 0; i < defaultActions.length; i++) {
341 String actionKey = defaultActions[i];
342 if (addedKeys.contains(actionKey)) {
343 // If we already have added this, don't add it again.
344 continue;
345 }
346 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
347 mItems.add(new PowerAction());
348 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
349 mItems.add(mAirplaneModeOn);
350 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
351 if (Settings.Global.getInt(mContext.getContentResolver(),
352 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
353 mItems.add(new BugReportAction());
354 }
355 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
356 if (mShowSilentToggle) {
357 mItems.add(mSilentModeAction);
358 }
359 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
360 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
361 addUsersToMenu(mItems);
362 }
363 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
364 mItems.add(getSettingsAction());
365 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700366 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
367 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800368 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700369 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800370 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700371 }
Jason Monk361915c2017-03-21 20:33:59 -0400372 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
373 mItems.add(getVoiceAssistAction());
374 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
375 mItems.add(getAssistAction());
376 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
377 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500378 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
379 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000380 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000381 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000382 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
383 mItems.add(new LogoutAction());
384 mHasLogoutButton = true;
385 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800386 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
387 if (mSeparatedEmergencyButtonEnabled
388 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
389 mItems.add(new EmergencyDialerAction());
390 }
Jason Monk361915c2017-03-21 20:33:59 -0400391 } else {
392 Log.e(TAG, "Invalid global action key " + actionKey);
393 }
394 // Add here so we don't add more than one.
395 addedKeys.add(actionKey);
396 }
397
398 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
399 mItems.add(getEmergencyAction());
400 }
401
402 mAdapter = new MyAdapter();
403
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700404 OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> {
405 final Action action = mAdapter.getItem(position);
406 if (action instanceof LongPressAction) {
407 mDialog.dismiss();
408 return ((LongPressAction) action).onLongPress();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400409 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700410 return false;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400411 };
Steve Elliott9b87a442019-03-05 10:24:16 -0500412 GlobalActionsPanelPlugin.PanelViewController panelViewController =
413 mPanelExtension.get() != null
414 ? mPanelExtension.get().onPanelShown(() -> {
415 if (mDialog != null) {
416 mDialog.dismiss();
417 }
418 })
419 : null;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800420 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener,
Steve Elliott9b87a442019-03-05 10:24:16 -0500421 mSeparatedEmergencyButtonEnabled, panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400422 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700423 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400424
425 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800426 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400427
428 return dialog;
429 }
430
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800431 private boolean shouldDisplayLockdown() {
432 int userId = getCurrentUser().id;
433 // Lockdown is meaningless without a place to go.
434 if (!mKeyguardManager.isDeviceSecure(userId)) {
435 return false;
436 }
437
438 // Only show the lockdown button if the device isn't locked down (for whatever reason).
439 int state = mLockPatternUtils.getStrongAuthForUser(userId);
440 return (state == STRONG_AUTH_NOT_REQUIRED
441 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
442 }
443
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700444 @Override
445 public void onUiModeChanged() {
446 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
447 }
448
449 public void destroy() {
450 Dependency.get(ConfigurationController.class).removeCallback(this);
451 }
452
Jason Monk361915c2017-03-21 20:33:59 -0400453 private final class PowerAction extends SinglePressAction implements LongPressAction {
454 private PowerAction() {
455 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400456 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400457 }
458
459 @Override
460 public boolean onLongPress() {
461 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
462 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
463 mWindowManagerFuncs.reboot(true);
464 return true;
465 }
466 return false;
467 }
468
469 @Override
470 public boolean showDuringKeyguard() {
471 return true;
472 }
473
474 @Override
475 public boolean showBeforeProvisioning() {
476 return true;
477 }
478
479 @Override
480 public void onPress() {
481 // shutdown by making sure radio and power are handled accordingly.
482 mWindowManagerFuncs.shutdown();
483 }
484 }
485
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800486 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800487 private EmergencyDialerAction() {
488 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800489 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800490 }
491
492 @Override
493 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800494 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800495 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800496 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800497 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
498 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800499 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
500 }
501
502 @Override
503 public boolean showDuringKeyguard() {
504 return true;
505 }
506
507 @Override
508 public boolean showBeforeProvisioning() {
509 return true;
510 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500511
512 @Override
513 public boolean shouldBeSeparated() {
514 return true;
515 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800516 }
517
Jason Monk361915c2017-03-21 20:33:59 -0400518 private final class RestartAction extends SinglePressAction implements LongPressAction {
519 private RestartAction() {
520 super(R.drawable.ic_restart, R.string.global_action_restart);
521 }
522
523 @Override
524 public boolean onLongPress() {
525 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
526 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
527 mWindowManagerFuncs.reboot(true);
528 return true;
529 }
530 return false;
531 }
532
533 @Override
534 public boolean showDuringKeyguard() {
535 return true;
536 }
537
538 @Override
539 public boolean showBeforeProvisioning() {
540 return true;
541 }
542
543 @Override
544 public void onPress() {
545 mWindowManagerFuncs.reboot(false);
546 }
547 }
548
549
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400550 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500551 public ScreenshotAction() {
552 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
553 }
554
555 @Override
556 public void onPress() {
557 // Add a little delay before executing, to give the
558 // dialog a chance to go away before it takes a
559 // screenshot.
560 // TODO: instead, omit global action dialog layer
561 mHandler.postDelayed(new Runnable() {
562 @Override
563 public void run() {
564 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
565 MetricsLogger.action(mContext,
566 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
567 }
568 }, 500);
569 }
570
571 @Override
572 public boolean showDuringKeyguard() {
573 return true;
574 }
575
576 @Override
577 public boolean showBeforeProvisioning() {
578 return false;
579 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400580
581 @Override
582 public boolean onLongPress() {
583 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
584 mScreenRecordHelper.launchRecordPrompt();
585 } else {
586 onPress();
587 }
588 return true;
589 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500590 }
591
Jason Monk361915c2017-03-21 20:33:59 -0400592 private class BugReportAction extends SinglePressAction implements LongPressAction {
593
594 public BugReportAction() {
595 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
596 }
597
598 @Override
599 public void onPress() {
600 // don't actually trigger the bugreport if we are running stability
601 // tests via monkey
602 if (ActivityManager.isUserAMonkey()) {
603 return;
604 }
605 // Add a little delay before executing, to give the
606 // dialog a chance to go away before it takes a
607 // screenshot.
608 mHandler.postDelayed(new Runnable() {
609 @Override
610 public void run() {
611 try {
612 // Take an "interactive" bugreport.
613 MetricsLogger.action(mContext,
614 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
615 ActivityManager.getService().requestBugReport(
616 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
617 } catch (RemoteException e) {
618 }
619 }
620 }, 500);
621 }
622
623 @Override
624 public boolean onLongPress() {
625 // don't actually trigger the bugreport if we are running stability
626 // tests via monkey
627 if (ActivityManager.isUserAMonkey()) {
628 return false;
629 }
630 try {
631 // Take a "full" bugreport.
632 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
633 ActivityManager.getService().requestBugReport(
634 ActivityManager.BUGREPORT_OPTION_FULL);
635 } catch (RemoteException e) {
636 }
637 return false;
638 }
639
640 public boolean showDuringKeyguard() {
641 return true;
642 }
643
644 @Override
645 public boolean showBeforeProvisioning() {
646 return false;
647 }
648
649 @Override
650 public String getStatus() {
651 return mContext.getString(
652 R.string.bugreport_status,
653 Build.VERSION.RELEASE,
654 Build.ID);
655 }
656 }
657
Alex Chau04458852017-11-27 18:21:23 +0000658 private final class LogoutAction extends SinglePressAction {
659 private LogoutAction() {
660 super(R.drawable.ic_logout, R.string.global_action_logout);
661 }
662
663 @Override
664 public boolean showDuringKeyguard() {
665 return true;
666 }
667
668 @Override
669 public boolean showBeforeProvisioning() {
670 return false;
671 }
672
673 @Override
674 public void onPress() {
675 // Add a little delay before executing, to give the dialog a chance to go away before
676 // switching user
677 mHandler.postDelayed(() -> {
678 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000679 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000680 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000681 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000682 } catch (RemoteException re) {
683 Log.e(TAG, "Couldn't logout user " + re);
684 }
685 }, 500);
686 }
687 }
688
Jason Monk361915c2017-03-21 20:33:59 -0400689 private Action getSettingsAction() {
690 return new SinglePressAction(R.drawable.ic_settings,
691 R.string.global_action_settings) {
692
693 @Override
694 public void onPress() {
695 Intent intent = new Intent(Settings.ACTION_SETTINGS);
696 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
697 mContext.startActivity(intent);
698 }
699
700 @Override
701 public boolean showDuringKeyguard() {
702 return true;
703 }
704
705 @Override
706 public boolean showBeforeProvisioning() {
707 return true;
708 }
709 };
710 }
711
712 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800713 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
714 if(!mSeparatedEmergencyButtonEnabled) {
715 // use un-colored legacy treatment
716 emergencyIcon.setTintList(null);
717 }
718
Jason Monk361915c2017-03-21 20:33:59 -0400719 return new SinglePressAction(R.drawable.emergency_icon,
720 R.string.global_action_emergency) {
721 @Override
722 public void onPress() {
723 mEmergencyAffordanceManager.performEmergencyCall();
724 }
725
726 @Override
727 public boolean showDuringKeyguard() {
728 return true;
729 }
730
731 @Override
732 public boolean showBeforeProvisioning() {
733 return true;
734 }
735 };
736 }
737
738 private Action getAssistAction() {
739 return new SinglePressAction(R.drawable.ic_action_assist_focused,
740 R.string.global_action_assist) {
741 @Override
742 public void onPress() {
743 Intent intent = new Intent(Intent.ACTION_ASSIST);
744 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
745 mContext.startActivity(intent);
746 }
747
748 @Override
749 public boolean showDuringKeyguard() {
750 return true;
751 }
752
753 @Override
754 public boolean showBeforeProvisioning() {
755 return true;
756 }
757 };
758 }
759
760 private Action getVoiceAssistAction() {
761 return new SinglePressAction(R.drawable.ic_voice_search,
762 R.string.global_action_voice_assist) {
763 @Override
764 public void onPress() {
765 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
766 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
767 mContext.startActivity(intent);
768 }
769
770 @Override
771 public boolean showDuringKeyguard() {
772 return true;
773 }
774
775 @Override
776 public boolean showBeforeProvisioning() {
777 return true;
778 }
779 };
780 }
781
782 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400783 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400784 R.string.global_action_lockdown) {
785
786 @Override
787 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700788 new LockPatternUtils(mContext)
789 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
790 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400791 try {
792 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100793 // Lock profiles (if any) on the background thread.
794 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
795 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400796 } catch (RemoteException e) {
797 Log.e(TAG, "Error while trying to lock device.", e);
798 }
799 }
800
801 @Override
802 public boolean showDuringKeyguard() {
803 return true;
804 }
805
806 @Override
807 public boolean showBeforeProvisioning() {
808 return false;
809 }
810 };
811 }
812
Pavel Grafov059021b2018-05-02 13:44:46 +0100813 private void lockProfiles() {
814 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
815 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
816 final int currentUserId = getCurrentUser().id;
817 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
818 for (final int id : profileIds) {
819 if (id != currentUserId) {
820 tm.setDeviceLockedForUser(id, true);
821 }
822 }
823 }
824
Jason Monk361915c2017-03-21 20:33:59 -0400825 private UserInfo getCurrentUser() {
826 try {
827 return ActivityManager.getService().getCurrentUser();
828 } catch (RemoteException re) {
829 return null;
830 }
831 }
832
833 private boolean isCurrentUserOwner() {
834 UserInfo currentUser = getCurrentUser();
835 return currentUser == null || currentUser.isPrimary();
836 }
837
838 private void addUsersToMenu(ArrayList<Action> items) {
839 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
840 if (um.isUserSwitcherEnabled()) {
841 List<UserInfo> users = um.getUsers();
842 UserInfo currentUser = getCurrentUser();
843 for (final UserInfo user : users) {
844 if (user.supportsSwitchToByUser()) {
845 boolean isCurrentUser = currentUser == null
846 ? user.id == 0 : (currentUser.id == user.id);
847 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
848 : null;
849 SinglePressAction switchToUser = new SinglePressAction(
850 R.drawable.ic_menu_cc, icon,
851 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400852 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400853 public void onPress() {
854 try {
855 ActivityManager.getService().switchUser(user.id);
856 } catch (RemoteException re) {
857 Log.e(TAG, "Couldn't switch user " + re);
858 }
859 }
860
861 public boolean showDuringKeyguard() {
862 return true;
863 }
864
865 public boolean showBeforeProvisioning() {
866 return false;
867 }
868 };
869 items.add(switchToUser);
870 }
871 }
872 }
873 }
874
875 private void prepareDialog() {
876 refreshSilentMode();
877 mAirplaneModeOn.updateState(mAirplaneState);
878 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400879 if (mShowSilentToggle) {
880 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
881 mContext.registerReceiver(mRingerModeReceiver, filter);
882 }
883 }
884
885 private void refreshSilentMode() {
886 if (!mHasVibrator) {
887 final boolean silentModeOn =
888 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400889 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400890 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
891 }
892 }
893
894 /** {@inheritDoc} */
895 public void onDismiss(DialogInterface dialog) {
896 mWindowManagerFuncs.onGlobalActionsHidden();
897 if (mShowSilentToggle) {
898 try {
899 mContext.unregisterReceiver(mRingerModeReceiver);
900 } catch (IllegalArgumentException ie) {
901 // ignore this
902 Log.w(TAG, ie);
903 }
904 }
905 }
906
907 /** {@inheritDoc} */
908 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400909 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400910 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400911 dialog.dismiss();
912 }
Jason Monkfd279662017-06-29 19:37:48 -0400913 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400914 }
915
Shaotang Li786da902018-08-02 11:18:00 +0800916 /** {@inheritDoc} */
917 public void onShow(DialogInterface dialog) {
918 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
919 }
920
Jason Monk361915c2017-03-21 20:33:59 -0400921 /**
922 * The adapter used for the list within the global actions dialog, taking
923 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400924 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
925 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400926 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
927 */
928 private class MyAdapter extends BaseAdapter {
929
930 public int getCount() {
931 int count = 0;
932
933 for (int i = 0; i < mItems.size(); i++) {
934 final Action action = mItems.get(i);
935
936 if (mKeyguardShowing && !action.showDuringKeyguard()) {
937 continue;
938 }
939 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
940 continue;
941 }
942 count++;
943 }
944 return count;
945 }
946
947 @Override
948 public boolean isEnabled(int position) {
949 return getItem(position).isEnabled();
950 }
951
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500952 public ArrayList<Action> getSeparatedActions(boolean shouldUseSeparatedView) {
953 ArrayList<Action> separatedActions = new ArrayList<Action>();
954 if (!shouldUseSeparatedView) {
955 return separatedActions;
956 }
957 for (int i = 0; i < mItems.size(); i++) {
958 final Action action = mItems.get(i);
959 if (action.shouldBeSeparated()) {
960 separatedActions.add(action);
961 }
962 }
963 return separatedActions;
964 }
965
966 public ArrayList<Action> getListActions(boolean shouldUseSeparatedView) {
967 if (!shouldUseSeparatedView) {
968 return new ArrayList<Action>(mItems);
969 }
970 ArrayList<Action> listActions = new ArrayList<Action>();
971 for (int i = 0; i < mItems.size(); i++) {
972 final Action action = mItems.get(i);
973 if (!action.shouldBeSeparated()) {
974 listActions.add(action);
975 }
976 }
977 return listActions;
978 }
979
Jason Monk361915c2017-03-21 20:33:59 -0400980 @Override
981 public boolean areAllItemsEnabled() {
982 return false;
983 }
984
985 public Action getItem(int position) {
986
987 int filteredPos = 0;
988 for (int i = 0; i < mItems.size(); i++) {
989 final Action action = mItems.get(i);
990 if (mKeyguardShowing && !action.showDuringKeyguard()) {
991 continue;
992 }
993 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
994 continue;
995 }
996 if (filteredPos == position) {
997 return action;
998 }
999 filteredPos++;
1000 }
1001
1002 throw new IllegalArgumentException("position " + position
1003 + " out of range of showable actions"
1004 + ", filtered count=" + getCount()
1005 + ", keyguardshowing=" + mKeyguardShowing
1006 + ", provisioned=" + mDeviceProvisioned);
1007 }
1008
1009
1010 public long getItemId(int position) {
1011 return position;
1012 }
1013
1014 public View getView(int position, View convertView, ViewGroup parent) {
1015 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001016 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001017 // Everything but screenshot, the last item, gets white background.
1018 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001019 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001020 }
1021 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001022 }
1023 }
1024
1025 // note: the scheme below made more sense when we were planning on having
1026 // 8 different things in the global actions dialog. seems overkill with
1027 // only 3 items now, but may as well keep this flexible approach so it will
1028 // be easy should someone decide at the last minute to include something
1029 // else, such as 'enable wifi', or 'enable bluetooth'
1030
1031 /**
1032 * What each item in the global actions dialog must be able to support.
1033 */
1034 private interface Action {
1035 /**
1036 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001037 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001038 */
1039 CharSequence getLabelForAccessibility(Context context);
1040
1041 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1042
1043 void onPress();
1044
1045 /**
1046 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001047 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001048 */
1049 boolean showDuringKeyguard();
1050
1051 /**
1052 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -04001053 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -04001054 */
1055 boolean showBeforeProvisioning();
1056
1057 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001058
1059 default boolean shouldBeSeparated() {
1060 return false;
1061 }
Jason Monk361915c2017-03-21 20:33:59 -04001062 }
1063
1064 /**
1065 * An action that also supports long press.
1066 */
1067 private interface LongPressAction extends Action {
1068 boolean onLongPress();
1069 }
1070
1071 /**
1072 * A single press action maintains no state, just responds to a press
1073 * and takes an action.
1074 */
1075 private static abstract class SinglePressAction implements Action {
1076 private final int mIconResId;
1077 private final Drawable mIcon;
1078 private final int mMessageResId;
1079 private final CharSequence mMessage;
1080
1081 protected SinglePressAction(int iconResId, int messageResId) {
1082 mIconResId = iconResId;
1083 mMessageResId = messageResId;
1084 mMessage = null;
1085 mIcon = null;
1086 }
1087
1088 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1089 mIconResId = iconResId;
1090 mMessageResId = 0;
1091 mMessage = message;
1092 mIcon = icon;
1093 }
1094
1095 public boolean isEnabled() {
1096 return true;
1097 }
1098
1099 public String getStatus() {
1100 return null;
1101 }
1102
1103 abstract public void onPress();
1104
1105 public CharSequence getLabelForAccessibility(Context context) {
1106 if (mMessage != null) {
1107 return mMessage;
1108 } else {
1109 return context.getString(mMessageResId);
1110 }
1111 }
1112
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001113 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001114 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001115 return com.android.systemui.R.layout.global_actions_grid_item;
1116 }
1117 return com.android.systemui.R.layout.global_actions_item;
1118 }
1119
Jason Monk361915c2017-03-21 20:33:59 -04001120 public View create(
1121 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001122 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001123 false);
Jason Monk361915c2017-03-21 20:33:59 -04001124
1125 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1126 TextView messageView = (TextView) v.findViewById(R.id.message);
1127
1128 TextView statusView = (TextView) v.findViewById(R.id.status);
1129 final String status = getStatus();
1130 if (!TextUtils.isEmpty(status)) {
1131 statusView.setText(status);
1132 } else {
1133 statusView.setVisibility(View.GONE);
1134 }
1135 if (mIcon != null) {
1136 icon.setImageDrawable(mIcon);
1137 icon.setScaleType(ScaleType.CENTER_CROP);
1138 } else if (mIconResId != 0) {
1139 icon.setImageDrawable(context.getDrawable(mIconResId));
1140 }
1141 if (mMessage != null) {
1142 messageView.setText(mMessage);
1143 } else {
1144 messageView.setText(mMessageResId);
1145 }
1146
1147 return v;
1148 }
1149 }
1150
1151 /**
1152 * A toggle action knows whether it is on or off, and displays an icon
1153 * and status message accordingly.
1154 */
1155 private static abstract class ToggleAction implements Action {
1156
1157 enum State {
1158 Off(false),
1159 TurningOn(true),
1160 TurningOff(true),
1161 On(false);
1162
1163 private final boolean inTransition;
1164
1165 State(boolean intermediate) {
1166 inTransition = intermediate;
1167 }
1168
1169 public boolean inTransition() {
1170 return inTransition;
1171 }
1172 }
1173
1174 protected State mState = State.Off;
1175
1176 // prefs
1177 protected int mEnabledIconResId;
1178 protected int mDisabledIconResid;
1179 protected int mMessageResId;
1180 protected int mEnabledStatusMessageResId;
1181 protected int mDisabledStatusMessageResId;
1182
1183 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001184 * @param enabledIconResId The icon for when this action is on.
1185 * @param disabledIconResid The icon for when this action is off.
1186 * @param message The general information message, e.g 'Silent Mode'
1187 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001188 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1189 */
1190 public ToggleAction(int enabledIconResId,
1191 int disabledIconResid,
1192 int message,
1193 int enabledStatusMessageResId,
1194 int disabledStatusMessageResId) {
1195 mEnabledIconResId = enabledIconResId;
1196 mDisabledIconResid = disabledIconResid;
1197 mMessageResId = message;
1198 mEnabledStatusMessageResId = enabledStatusMessageResId;
1199 mDisabledStatusMessageResId = disabledStatusMessageResId;
1200 }
1201
1202 /**
1203 * Override to make changes to resource IDs just before creating the
1204 * View.
1205 */
1206 void willCreate() {
1207
1208 }
1209
1210 @Override
1211 public CharSequence getLabelForAccessibility(Context context) {
1212 return context.getString(mMessageResId);
1213 }
1214
1215 public View create(Context context, View convertView, ViewGroup parent,
1216 LayoutInflater inflater) {
1217 willCreate();
1218
1219 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001220 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001221
1222 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1223 TextView messageView = (TextView) v.findViewById(R.id.message);
1224 TextView statusView = (TextView) v.findViewById(R.id.status);
1225 final boolean enabled = isEnabled();
1226
1227 if (messageView != null) {
1228 messageView.setText(mMessageResId);
1229 messageView.setEnabled(enabled);
1230 }
1231
1232 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1233 if (icon != null) {
1234 icon.setImageDrawable(context.getDrawable(
1235 (on ? mEnabledIconResId : mDisabledIconResid)));
1236 icon.setEnabled(enabled);
1237 }
1238
1239 if (statusView != null) {
1240 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1241 statusView.setVisibility(View.VISIBLE);
1242 statusView.setEnabled(enabled);
1243 }
1244 v.setEnabled(enabled);
1245
1246 return v;
1247 }
1248
1249 public final void onPress() {
1250 if (mState.inTransition()) {
1251 Log.w(TAG, "shouldn't be able to toggle when in transition");
1252 return;
1253 }
1254
1255 final boolean nowOn = !(mState == State.On);
1256 onToggle(nowOn);
1257 changeStateFromPress(nowOn);
1258 }
1259
1260 public boolean isEnabled() {
1261 return !mState.inTransition();
1262 }
1263
1264 /**
1265 * Implementations may override this if their state can be in on of the intermediate
1266 * states until some notification is received (e.g airplane mode is 'turning off' until
1267 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001268 *
Jason Monk361915c2017-03-21 20:33:59 -04001269 * @param buttonOn Whether the button was turned on or off
1270 */
1271 protected void changeStateFromPress(boolean buttonOn) {
1272 mState = buttonOn ? State.On : State.Off;
1273 }
1274
1275 abstract void onToggle(boolean on);
1276
1277 public void updateState(State state) {
1278 mState = state;
1279 }
1280 }
1281
1282 private class SilentModeToggleAction extends ToggleAction {
1283 public SilentModeToggleAction() {
1284 super(R.drawable.ic_audio_vol_mute,
1285 R.drawable.ic_audio_vol,
1286 R.string.global_action_toggle_silent_mode,
1287 R.string.global_action_silent_mode_on_status,
1288 R.string.global_action_silent_mode_off_status);
1289 }
1290
1291 void onToggle(boolean on) {
1292 if (on) {
1293 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1294 } else {
1295 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1296 }
1297 }
1298
1299 public boolean showDuringKeyguard() {
1300 return true;
1301 }
1302
1303 public boolean showBeforeProvisioning() {
1304 return false;
1305 }
1306 }
1307
1308 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1309
Jason Monk16fbd9d2017-04-27 14:28:49 -04001310 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001311
1312 private final AudioManager mAudioManager;
1313 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001314
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001315 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001316 mAudioManager = audioManager;
1317 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001318 }
1319
1320 private int ringerModeToIndex(int ringerMode) {
1321 // They just happen to coincide
1322 return ringerMode;
1323 }
1324
1325 private int indexToRingerMode(int index) {
1326 // They just happen to coincide
1327 return index;
1328 }
1329
1330 @Override
1331 public CharSequence getLabelForAccessibility(Context context) {
1332 return null;
1333 }
1334
1335 public View create(Context context, View convertView, ViewGroup parent,
1336 LayoutInflater inflater) {
1337 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1338
1339 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1340 for (int i = 0; i < 3; i++) {
1341 View itemView = v.findViewById(ITEM_IDS[i]);
1342 itemView.setSelected(selectedIndex == i);
1343 // Set up click handler
1344 itemView.setTag(i);
1345 itemView.setOnClickListener(this);
1346 }
1347 return v;
1348 }
1349
1350 public void onPress() {
1351 }
1352
1353 public boolean showDuringKeyguard() {
1354 return true;
1355 }
1356
1357 public boolean showBeforeProvisioning() {
1358 return false;
1359 }
1360
1361 public boolean isEnabled() {
1362 return true;
1363 }
1364
1365 void willCreate() {
1366 }
1367
1368 public void onClick(View v) {
1369 if (!(v.getTag() instanceof Integer)) return;
1370
1371 int index = (Integer) v.getTag();
1372 mAudioManager.setRingerMode(indexToRingerMode(index));
1373 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1374 }
1375 }
1376
1377 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1378 public void onReceive(Context context, Intent intent) {
1379 String action = intent.getAction();
1380 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1381 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1382 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1383 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001384 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001385 }
1386 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1387 // Airplane mode can be changed after ECM exits if airplane toggle button
1388 // is pressed during ECM mode
1389 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1390 mIsWaitingForEcmExit) {
1391 mIsWaitingForEcmExit = false;
1392 changeAirplaneModeSystemSetting(true);
1393 }
1394 }
1395 }
1396 };
1397
1398 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1399 @Override
1400 public void onServiceStateChanged(ServiceState serviceState) {
1401 if (!mHasTelephony) return;
1402 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1403 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1404 mAirplaneModeOn.updateState(mAirplaneState);
1405 mAdapter.notifyDataSetChanged();
1406 }
1407 };
1408
1409 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1410 @Override
1411 public void onReceive(Context context, Intent intent) {
1412 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1413 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1414 }
1415 }
1416 };
1417
1418 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1419 @Override
1420 public void onChange(boolean selfChange) {
1421 onAirplaneModeChanged();
1422 }
1423 };
1424
1425 private static final int MESSAGE_DISMISS = 0;
1426 private static final int MESSAGE_REFRESH = 1;
1427 private static final int MESSAGE_SHOW = 2;
1428 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1429
1430 private Handler mHandler = new Handler() {
1431 public void handleMessage(Message msg) {
1432 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001433 case MESSAGE_DISMISS:
1434 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001435 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1436 mDialog.dismissImmediately();
1437 } else {
1438 mDialog.dismiss();
1439 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001440 mDialog = null;
1441 }
1442 break;
1443 case MESSAGE_REFRESH:
1444 refreshSilentMode();
1445 mAdapter.notifyDataSetChanged();
1446 break;
1447 case MESSAGE_SHOW:
1448 handleShow();
1449 break;
Jason Monk361915c2017-03-21 20:33:59 -04001450 }
1451 }
1452 };
1453
1454 private void onAirplaneModeChanged() {
1455 // Let the service state callbacks handle the state.
1456 if (mHasTelephony) return;
1457
1458 boolean airplaneModeOn = Settings.Global.getInt(
1459 mContext.getContentResolver(),
1460 Settings.Global.AIRPLANE_MODE_ON,
1461 0) == 1;
1462 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1463 mAirplaneModeOn.updateState(mAirplaneState);
1464 }
1465
1466 /**
1467 * Change the airplane mode system setting
1468 */
1469 private void changeAirplaneModeSystemSetting(boolean on) {
1470 Settings.Global.putInt(
1471 mContext.getContentResolver(),
1472 Settings.Global.AIRPLANE_MODE_ON,
1473 on ? 1 : 0);
1474 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1475 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1476 intent.putExtra("state", on);
1477 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1478 if (!mHasTelephony) {
1479 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1480 }
1481 }
1482
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001483 private static final class ActionsDialog extends Dialog implements DialogInterface,
1484 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001485
Jason Monk16fbd9d2017-04-27 14:28:49 -04001486 private final Context mContext;
1487 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001488 private MultiListLayout mGlobalActionsLayout;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001489 private final OnClickListener mClickListener;
1490 private final OnItemLongClickListener mLongClickListener;
Steve Elliott9b87a442019-03-05 10:24:16 -05001491 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001492 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001493 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001494 private boolean mKeyguardShowing;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001495 private boolean mShouldDisplaySeparatedButton;
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
1499 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Steve Elliott9b87a442019-03-05 10:24:16 -05001500 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton,
1501 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001502 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001503 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001504 mAdapter = adapter;
1505 mClickListener = clickListener;
1506 mLongClickListener = longClickListener;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001507 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001508 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001509
1510 // Window initialization
1511 Window window = getWindow();
1512 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001513 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1514 window.getDecorView();
1515 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1516 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1517 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1518 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1519 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001520 window.addFlags(
1521 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001522 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001523 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001524 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1525 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1526 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001527 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1528
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001529 initializeLayout();
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001530
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001531 setTitle(R.string.global_actions);
Steve Elliott9b87a442019-03-05 10:24:16 -05001532
1533 mPanelController = plugin;
1534 View panelView = initializePanel();
1535 if (panelView == null) {
1536 mBackgroundDrawable = new GradientDrawable(context);
1537 mScrimAlpha = 0.7f;
1538 } else {
1539 mBackgroundDrawable = context.getDrawable(
1540 com.android.systemui.R.drawable.global_action_panel_scrim);
1541 mScrimAlpha = 1f;
1542 addContentView(
1543 panelView,
1544 new ViewGroup.LayoutParams(
1545 ViewGroup.LayoutParams.MATCH_PARENT,
1546 ViewGroup.LayoutParams.MATCH_PARENT));
1547 }
1548 window.setBackgroundDrawable(mBackgroundDrawable);
1549 }
1550
1551 private View initializePanel() {
1552 if (isPanelEnabled(mContext) && mPanelController != null) {
1553 View panelView = mPanelController.getPanelContent();
1554 if (panelView != null) {
1555 FrameLayout panelContainer = new FrameLayout(mContext);
1556 FrameLayout.LayoutParams panelParams =
1557 new FrameLayout.LayoutParams(
1558 FrameLayout.LayoutParams.MATCH_PARENT,
1559 FrameLayout.LayoutParams.WRAP_CONTENT);
1560 panelParams.topMargin = mContext.getResources().getDimensionPixelSize(
1561 com.android.systemui.R.dimen.global_actions_panel_top_margin);
1562 panelContainer.addView(panelView, panelParams);
1563 return panelContainer;
1564 }
1565 }
1566 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001567 }
1568
1569 private void initializeLayout() {
1570 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001571 mGlobalActionsLayout = (MultiListLayout)
1572 findViewById(com.android.systemui.R.id.global_actions_view);
1573 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
1574 mGlobalActionsLayout.setHasSeparatedView(mShouldDisplaySeparatedButton);
1575 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001576 @Override
1577 public boolean dispatchPopulateAccessibilityEvent(
1578 View host, AccessibilityEvent event) {
1579 // Populate the title here, just as Activity does
1580 event.getText().add(mContext.getString(R.string.global_actions));
1581 return true;
1582 }
1583 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001584 mGlobalActionsLayout.setRotationListener(this::onRotate);
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 Monk16fbd9d2017-04-27 14:28:49 -04001602 private void updateList() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001603 mGlobalActionsLayout.removeAllItems();
1604 ArrayList<Action> separatedActions =
1605 mAdapter.getSeparatedActions(mShouldDisplaySeparatedButton);
1606 ArrayList<Action> listActions = mAdapter.getListActions(mShouldDisplaySeparatedButton);
1607 mGlobalActionsLayout.setExpectedListItemCount(listActions.size());
1608 mGlobalActionsLayout.setExpectedSeparatedItemCount(separatedActions.size());
Aaron Heuckrotheeda8be2019-03-04 11:01:05 -05001609 int rotation = RotationUtils.getRotation(mContext);
1610
1611 boolean reverse = false; // should we add items to parents in the reverse order?
1612 if (isGridEnabled(mContext)) {
1613 if (rotation == RotationUtils.ROTATION_NONE
1614 || rotation == RotationUtils.ROTATION_SEASCAPE) {
1615 reverse = !reverse; // if we're in portrait or seascape, reverse items
1616 }
1617 if (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())
1618 == View.LAYOUT_DIRECTION_RTL) {
1619 reverse = !reverse; // if we're in an RTL language, reverse items (again)
1620 }
1621 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001622
Jason Monk16fbd9d2017-04-27 14:28:49 -04001623 for (int i = 0; i < mAdapter.getCount(); i++) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001624 Action action = mAdapter.getItem(i);
1625 int separatedIndex = separatedActions.indexOf(action);
1626 ViewGroup parent;
1627 if (separatedIndex != -1) {
Aaron Heuckrotheeda8be2019-03-04 11:01:05 -05001628 parent = mGlobalActionsLayout.getParentView(true, separatedIndex, rotation);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001629 } else {
1630 int listIndex = listActions.indexOf(action);
Aaron Heuckrotheeda8be2019-03-04 11:01:05 -05001631 parent = mGlobalActionsLayout.getParentView(false, listIndex, rotation);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001632 }
1633 View v = mAdapter.getView(i, null, parent);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001634 final int pos = i;
1635 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1636 v.setOnLongClickListener(view ->
1637 mLongClickListener.onItemLongClick(null, v, pos, 0));
Aaron Heuckrotheeda8be2019-03-04 11:01:05 -05001638 if (reverse) {
1639 parent.addView(v, 0); // reverse order of items
1640 } else {
1641 parent.addView(v);
1642 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001643 }
Jason Monk361915c2017-03-21 20:33:59 -04001644 }
1645
1646 @Override
1647 protected void onStart() {
1648 super.setCanceledOnTouchOutside(true);
1649 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001650 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001651
Steve Elliott9b87a442019-03-05 10:24:16 -05001652 if (mBackgroundDrawable instanceof GradientDrawable) {
1653 Point displaySize = new Point();
1654 mContext.getDisplay().getRealSize(displaySize);
1655 mColorExtractor.addOnColorsChangedListener(this);
1656 ((GradientDrawable) mBackgroundDrawable)
1657 .setScreenSize(displaySize.x, displaySize.y);
1658 GradientColors colors = mColorExtractor.getColors(
1659 mKeyguardShowing
1660 ? WallpaperManager.FLAG_LOCK
1661 : WallpaperManager.FLAG_SYSTEM);
1662 updateColors(colors, false /* animate */);
1663 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001664 }
1665
1666 /**
1667 * Updates background and system bars according to current GradientColors.
1668 * @param colors Colors and hints to use.
1669 * @param animate Interpolates gradient if true, just sets otherwise.
1670 */
1671 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001672 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1673 return;
1674 }
1675 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001676 View decorView = getWindow().getDecorView();
1677 if (colors.supportsDarkText()) {
1678 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001679 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001680 } else {
1681 decorView.setSystemUiVisibility(0);
1682 }
Jason Monk361915c2017-03-21 20:33:59 -04001683 }
1684
1685 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001686 protected void onStop() {
1687 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001688 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001689 }
1690
1691 @Override
1692 public void show() {
1693 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001694 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001695 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001696 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1697 mGlobalActionsLayout.setAlpha(0);
1698 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001699 .alpha(1)
1700 .translationX(0)
1701 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001702 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001703 .setUpdateListener(animation -> {
1704 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001705 * mScrimAlpha * 255);
1706 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001707 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001708 .start();
1709 }
1710
1711 @Override
1712 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001713 if (!mShowing) {
1714 return;
1715 }
1716 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001717 mGlobalActionsLayout.setTranslationX(0);
1718 mGlobalActionsLayout.setAlpha(1);
1719 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001720 .alpha(0)
1721 .translationX(getAnimTranslation())
1722 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001723 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001724 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001725 .setUpdateListener(animation -> {
1726 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001727 * mScrimAlpha * 255);
1728 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001729 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001730 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001731 if (mPanelController != null) {
1732 mPanelController.onDismissed();
1733 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001734 }
1735
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001736 void dismissImmediately() {
1737 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001738 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001739 }
1740
Jason Monk16fbd9d2017-04-27 14:28:49 -04001741 private float getAnimTranslation() {
1742 return getContext().getResources().getDimension(
1743 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001744 }
1745
1746 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001747 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001748 if (mKeyguardShowing) {
1749 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001750 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1751 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001752 }
1753 } else {
1754 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001755 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1756 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001757 }
1758 }
1759 }
1760
1761 public void setKeyguardShowing(boolean keyguardShowing) {
1762 mKeyguardShowing = keyguardShowing;
1763 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001764
1765 public void onRotate(int from, int to) {
1766 if (mShowing && isGridEnabled(mContext)) {
1767 initializeLayout();
1768 updateList();
1769 }
1770 }
Jason Monk361915c2017-03-21 20:33:59 -04001771 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001772
1773 /**
1774 * Determines whether or not the Global Actions Dialog should use the newer grid-style layout.
1775 */
1776 public static boolean isGridEnabled(Context context) {
1777 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1778 }
Jason Monk361915c2017-03-21 20:33:59 -04001779}