blob: b07f90970cbef35b6ba63069fd6b7c8b20655e9d [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;
Steve Elliottbfa314a2019-03-06 13:53:19 -050092import com.android.systemui.statusbar.phone.ScrimController;
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;
102
103/**
104 * Helper to show the global actions dialog. Each item is an {@link Action} that
105 * may show depending on whether the keyguard is showing, and whether the device
106 * is provisioned.
107 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500108public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
109 DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400110
111 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
112 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700113 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400114
115 private static final String TAG = "GlobalActionsDialog";
116
117 private static final boolean SHOW_SILENT_TOGGLE = true;
118
119 /* Valid settings for global actions keys.
120 * see config.xml config_globalActionList */
121 private static final String GLOBAL_ACTION_KEY_POWER = "power";
122 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
123 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
124 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
125 private static final String GLOBAL_ACTION_KEY_USERS = "users";
126 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
127 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
128 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
129 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
130 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000131 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800132 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500133 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400134
135 private final Context mContext;
136 private final GlobalActionsManager mWindowManagerFuncs;
137 private final AudioManager mAudioManager;
138 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000139 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800140 private final LockPatternUtils mLockPatternUtils;
141 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400142
143 private ArrayList<Action> mItems;
144 private ActionsDialog mDialog;
145
146 private Action mSilentModeAction;
147 private ToggleAction mAirplaneModeOn;
148
149 private MyAdapter mAdapter;
150
151 private boolean mKeyguardShowing = false;
152 private boolean mDeviceProvisioned = false;
153 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
154 private boolean mIsWaitingForEcmExit = false;
155 private boolean mHasTelephony;
156 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000157 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800158 private boolean mHasLockdownButton;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500159 private boolean mUseSeparatedList;
Jason Monk361915c2017-03-21 20:33:59 -0400160 private final boolean mShowSilentToggle;
161 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500162 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400163 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400164
Steve Elliott9b87a442019-03-05 10:24:16 -0500165 private final Extension<GlobalActionsPanelPlugin> mPanelExtension;
166
Jason Monk361915c2017-03-21 20:33:59 -0400167 /**
168 * @param context everything needs a context :(
169 */
170 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700171 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400172 mWindowManagerFuncs = windowManagerFuncs;
173 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
174 mDreamManager = IDreamManager.Stub.asInterface(
175 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000176 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
177 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800178 mLockPatternUtils = new LockPatternUtils(mContext);
179 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400180
181 // receive broadcasts
182 IntentFilter filter = new IntentFilter();
183 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
184 filter.addAction(Intent.ACTION_SCREEN_OFF);
185 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
186 context.registerReceiver(mBroadcastReceiver, filter);
187
188 ConnectivityManager cm = (ConnectivityManager)
189 context.getSystemService(Context.CONNECTIVITY_SERVICE);
190 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
191
192 // get notified of phone state changes
193 TelephonyManager telephonyManager =
194 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
195 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
196 mContext.getContentResolver().registerContentObserver(
197 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
198 mAirplaneModeObserver);
199 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
200 mHasVibrator = vibrator != null && vibrator.hasVibrator();
201
202 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
203 R.bool.config_useFixedVolume);
204
205 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500206 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400207 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700208
209 Dependency.get(ConfigurationController.class).addCallback(this);
Steve Elliott9b87a442019-03-05 10:24:16 -0500210
211 mPanelExtension = Dependency.get(ExtensionController.class)
212 .newExtension(GlobalActionsPanelPlugin.class)
213 .withPlugin(GlobalActionsPanelPlugin.class)
214 .build();
Jason Monk361915c2017-03-21 20:33:59 -0400215 }
216
217 /**
218 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400219 *
Jason Monk361915c2017-03-21 20:33:59 -0400220 * @param keyguardShowing True if keyguard is showing
221 */
222 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
223 mKeyguardShowing = keyguardShowing;
224 mDeviceProvisioned = isDeviceProvisioned;
225 if (mDialog != null) {
226 mDialog.dismiss();
227 mDialog = null;
228 // Show delayed, so that the dismiss of the previous dialog completes
229 mHandler.sendEmptyMessage(MESSAGE_SHOW);
230 } else {
231 handleShow();
232 }
233 }
234
Charles He9851a8d2017-10-10 17:31:30 +0100235 /**
236 * Dismiss the global actions dialog, if it's currently shown
237 */
238 public void dismissDialog() {
239 mHandler.removeMessages(MESSAGE_DISMISS);
240 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
241 }
242
Jason Monk361915c2017-03-21 20:33:59 -0400243 private void awakenIfNecessary() {
244 if (mDreamManager != null) {
245 try {
246 if (mDreamManager.isDreaming()) {
247 mDreamManager.awaken();
248 }
249 } catch (RemoteException e) {
250 // we tried
251 }
252 }
253 }
254
255 private void handleShow() {
256 awakenIfNecessary();
257 mDialog = createDialog();
258 prepareDialog();
259
260 // If we only have 1 item and it's a simple press action, just do this action.
261 if (mAdapter.getCount() == 1
262 && mAdapter.getItem(0) instanceof SinglePressAction
263 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
264 ((SinglePressAction) mAdapter.getItem(0)).onPress();
265 } else {
266 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
267 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100268 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400269 mDialog.getWindow().setAttributes(attrs);
270 mDialog.show();
271 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400272 }
273 }
274
275 /**
276 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400277 *
Jason Monk361915c2017-03-21 20:33:59 -0400278 * @return A new dialog.
279 */
280 private ActionsDialog createDialog() {
281 // Simple toggle style if there's no vibrator, otherwise use a tri-state
282 if (!mHasVibrator) {
283 mSilentModeAction = new SilentModeToggleAction();
284 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700285 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400286 }
287 mAirplaneModeOn = new ToggleAction(
288 R.drawable.ic_lock_airplane_mode,
289 R.drawable.ic_lock_airplane_mode_off,
290 R.string.global_actions_toggle_airplane_mode,
291 R.string.global_actions_airplane_mode_on_status,
292 R.string.global_actions_airplane_mode_off_status) {
293
294 void onToggle(boolean on) {
295 if (mHasTelephony && Boolean.parseBoolean(
296 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
297 mIsWaitingForEcmExit = true;
298 // Launch ECM exit dialog
299 Intent ecmDialogIntent =
300 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
301 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
302 mContext.startActivity(ecmDialogIntent);
303 } else {
304 changeAirplaneModeSystemSetting(on);
305 }
306 }
307
308 @Override
309 protected void changeStateFromPress(boolean buttonOn) {
310 if (!mHasTelephony) return;
311
312 // In ECM mode airplane state cannot be changed
313 if (!(Boolean.parseBoolean(
314 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
315 mState = buttonOn ? State.TurningOn : State.TurningOff;
316 mAirplaneState = mState;
317 }
318 }
319
320 public boolean showDuringKeyguard() {
321 return true;
322 }
323
324 public boolean showBeforeProvisioning() {
325 return false;
326 }
327 };
328 onAirplaneModeChanged();
329
330 mItems = new ArrayList<Action>();
331 String[] defaultActions = mContext.getResources().getStringArray(
332 R.array.config_globalActionsList);
333
334 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000335 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800336 mHasLockdownButton = false;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500337 mUseSeparatedList = true;
Jason Monk361915c2017-03-21 20:33:59 -0400338 for (int i = 0; i < defaultActions.length; i++) {
339 String actionKey = defaultActions[i];
340 if (addedKeys.contains(actionKey)) {
341 // If we already have added this, don't add it again.
342 continue;
343 }
344 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
345 mItems.add(new PowerAction());
346 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
347 mItems.add(mAirplaneModeOn);
348 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
349 if (Settings.Global.getInt(mContext.getContentResolver(),
350 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
351 mItems.add(new BugReportAction());
352 }
353 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
354 if (mShowSilentToggle) {
355 mItems.add(mSilentModeAction);
356 }
357 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
358 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
359 addUsersToMenu(mItems);
360 }
361 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
362 mItems.add(getSettingsAction());
363 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700364 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
365 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800366 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700367 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800368 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700369 }
Jason Monk361915c2017-03-21 20:33:59 -0400370 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
371 mItems.add(getVoiceAssistAction());
372 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
373 mItems.add(getAssistAction());
374 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
375 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500376 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
377 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000378 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000379 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000380 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
381 mItems.add(new LogoutAction());
382 mHasLogoutButton = true;
383 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800384 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500385 if (mUseSeparatedList
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800386 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
387 mItems.add(new EmergencyDialerAction());
388 }
Jason Monk361915c2017-03-21 20:33:59 -0400389 } else {
390 Log.e(TAG, "Invalid global action key " + actionKey);
391 }
392 // Add here so we don't add more than one.
393 addedKeys.add(actionKey);
394 }
395
396 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
397 mItems.add(getEmergencyAction());
398 }
399
400 mAdapter = new MyAdapter();
401
Steve Elliott9b87a442019-03-05 10:24:16 -0500402 GlobalActionsPanelPlugin.PanelViewController panelViewController =
403 mPanelExtension.get() != null
404 ? mPanelExtension.get().onPanelShown(() -> {
405 if (mDialog != null) {
406 mDialog.dismiss();
407 }
408 })
409 : null;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500410 ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, mUseSeparatedList,
411 panelViewController);
Jason Monk361915c2017-03-21 20:33:59 -0400412 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700413 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400414
415 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800416 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400417
418 return dialog;
419 }
420
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800421 private boolean shouldDisplayLockdown() {
422 int userId = getCurrentUser().id;
423 // Lockdown is meaningless without a place to go.
424 if (!mKeyguardManager.isDeviceSecure(userId)) {
425 return false;
426 }
427
428 // Only show the lockdown button if the device isn't locked down (for whatever reason).
429 int state = mLockPatternUtils.getStrongAuthForUser(userId);
430 return (state == STRONG_AUTH_NOT_REQUIRED
431 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
432 }
433
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700434 @Override
435 public void onUiModeChanged() {
436 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
437 }
438
439 public void destroy() {
440 Dependency.get(ConfigurationController.class).removeCallback(this);
441 }
442
Jason Monk361915c2017-03-21 20:33:59 -0400443 private final class PowerAction extends SinglePressAction implements LongPressAction {
444 private PowerAction() {
445 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400446 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400447 }
448
449 @Override
450 public boolean onLongPress() {
451 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
452 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
453 mWindowManagerFuncs.reboot(true);
454 return true;
455 }
456 return false;
457 }
458
459 @Override
460 public boolean showDuringKeyguard() {
461 return true;
462 }
463
464 @Override
465 public boolean showBeforeProvisioning() {
466 return true;
467 }
468
469 @Override
470 public void onPress() {
471 // shutdown by making sure radio and power are handled accordingly.
472 mWindowManagerFuncs.shutdown();
473 }
474 }
475
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800476 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800477 private EmergencyDialerAction() {
478 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800479 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800480 }
481
482 @Override
483 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800484 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800485 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Leo Hsue4cc3122019-03-06 15:25:28 +0800486 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
487 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
488 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800489 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
490 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800491 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
492 }
493
494 @Override
495 public boolean showDuringKeyguard() {
496 return true;
497 }
498
499 @Override
500 public boolean showBeforeProvisioning() {
501 return true;
502 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500503
504 @Override
505 public boolean shouldBeSeparated() {
506 return true;
507 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800508 }
509
Jason Monk361915c2017-03-21 20:33:59 -0400510 private final class RestartAction extends SinglePressAction implements LongPressAction {
511 private RestartAction() {
512 super(R.drawable.ic_restart, R.string.global_action_restart);
513 }
514
515 @Override
516 public boolean onLongPress() {
517 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
518 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
519 mWindowManagerFuncs.reboot(true);
520 return true;
521 }
522 return false;
523 }
524
525 @Override
526 public boolean showDuringKeyguard() {
527 return true;
528 }
529
530 @Override
531 public boolean showBeforeProvisioning() {
532 return true;
533 }
534
535 @Override
536 public void onPress() {
537 mWindowManagerFuncs.reboot(false);
538 }
539 }
540
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400541 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500542 public ScreenshotAction() {
543 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
544 }
545
546 @Override
547 public void onPress() {
548 // Add a little delay before executing, to give the
549 // dialog a chance to go away before it takes a
550 // screenshot.
551 // TODO: instead, omit global action dialog layer
552 mHandler.postDelayed(new Runnable() {
553 @Override
554 public void run() {
555 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
556 MetricsLogger.action(mContext,
557 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
558 }
559 }, 500);
560 }
561
562 @Override
563 public boolean showDuringKeyguard() {
564 return true;
565 }
566
567 @Override
568 public boolean showBeforeProvisioning() {
569 return false;
570 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400571
572 @Override
573 public boolean onLongPress() {
574 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
575 mScreenRecordHelper.launchRecordPrompt();
576 } else {
577 onPress();
578 }
579 return true;
580 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500581 }
582
Jason Monk361915c2017-03-21 20:33:59 -0400583 private class BugReportAction extends SinglePressAction implements LongPressAction {
584
585 public BugReportAction() {
586 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
587 }
588
589 @Override
590 public void onPress() {
591 // don't actually trigger the bugreport if we are running stability
592 // tests via monkey
593 if (ActivityManager.isUserAMonkey()) {
594 return;
595 }
596 // Add a little delay before executing, to give the
597 // dialog a chance to go away before it takes a
598 // screenshot.
599 mHandler.postDelayed(new Runnable() {
600 @Override
601 public void run() {
602 try {
603 // Take an "interactive" bugreport.
604 MetricsLogger.action(mContext,
605 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
606 ActivityManager.getService().requestBugReport(
607 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
608 } catch (RemoteException e) {
609 }
610 }
611 }, 500);
612 }
613
614 @Override
615 public boolean onLongPress() {
616 // don't actually trigger the bugreport if we are running stability
617 // tests via monkey
618 if (ActivityManager.isUserAMonkey()) {
619 return false;
620 }
621 try {
622 // Take a "full" bugreport.
623 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
624 ActivityManager.getService().requestBugReport(
625 ActivityManager.BUGREPORT_OPTION_FULL);
626 } catch (RemoteException e) {
627 }
628 return false;
629 }
630
631 public boolean showDuringKeyguard() {
632 return true;
633 }
634
635 @Override
636 public boolean showBeforeProvisioning() {
637 return false;
638 }
639
640 @Override
641 public String getStatus() {
642 return mContext.getString(
643 R.string.bugreport_status,
644 Build.VERSION.RELEASE,
645 Build.ID);
646 }
647 }
648
Alex Chau04458852017-11-27 18:21:23 +0000649 private final class LogoutAction extends SinglePressAction {
650 private LogoutAction() {
651 super(R.drawable.ic_logout, R.string.global_action_logout);
652 }
653
654 @Override
655 public boolean showDuringKeyguard() {
656 return true;
657 }
658
659 @Override
660 public boolean showBeforeProvisioning() {
661 return false;
662 }
663
664 @Override
665 public void onPress() {
666 // Add a little delay before executing, to give the dialog a chance to go away before
667 // switching user
668 mHandler.postDelayed(() -> {
669 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000670 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000671 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000672 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000673 } catch (RemoteException re) {
674 Log.e(TAG, "Couldn't logout user " + re);
675 }
676 }, 500);
677 }
678 }
679
Jason Monk361915c2017-03-21 20:33:59 -0400680 private Action getSettingsAction() {
681 return new SinglePressAction(R.drawable.ic_settings,
682 R.string.global_action_settings) {
683
684 @Override
685 public void onPress() {
686 Intent intent = new Intent(Settings.ACTION_SETTINGS);
687 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
688 mContext.startActivity(intent);
689 }
690
691 @Override
692 public boolean showDuringKeyguard() {
693 return true;
694 }
695
696 @Override
697 public boolean showBeforeProvisioning() {
698 return true;
699 }
700 };
701 }
702
703 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800704 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500705 if (!mUseSeparatedList) {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800706 // use un-colored legacy treatment
707 emergencyIcon.setTintList(null);
708 }
709
Jason Monk361915c2017-03-21 20:33:59 -0400710 return new SinglePressAction(R.drawable.emergency_icon,
711 R.string.global_action_emergency) {
712 @Override
713 public void onPress() {
714 mEmergencyAffordanceManager.performEmergencyCall();
715 }
716
717 @Override
718 public boolean showDuringKeyguard() {
719 return true;
720 }
721
722 @Override
723 public boolean showBeforeProvisioning() {
724 return true;
725 }
726 };
727 }
728
729 private Action getAssistAction() {
730 return new SinglePressAction(R.drawable.ic_action_assist_focused,
731 R.string.global_action_assist) {
732 @Override
733 public void onPress() {
734 Intent intent = new Intent(Intent.ACTION_ASSIST);
735 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
736 mContext.startActivity(intent);
737 }
738
739 @Override
740 public boolean showDuringKeyguard() {
741 return true;
742 }
743
744 @Override
745 public boolean showBeforeProvisioning() {
746 return true;
747 }
748 };
749 }
750
751 private Action getVoiceAssistAction() {
752 return new SinglePressAction(R.drawable.ic_voice_search,
753 R.string.global_action_voice_assist) {
754 @Override
755 public void onPress() {
756 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
757 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
758 mContext.startActivity(intent);
759 }
760
761 @Override
762 public boolean showDuringKeyguard() {
763 return true;
764 }
765
766 @Override
767 public boolean showBeforeProvisioning() {
768 return true;
769 }
770 };
771 }
772
773 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400774 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400775 R.string.global_action_lockdown) {
776
777 @Override
778 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700779 new LockPatternUtils(mContext)
780 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
781 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400782 try {
783 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100784 // Lock profiles (if any) on the background thread.
785 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
786 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400787 } catch (RemoteException e) {
788 Log.e(TAG, "Error while trying to lock device.", e);
789 }
790 }
791
792 @Override
793 public boolean showDuringKeyguard() {
794 return true;
795 }
796
797 @Override
798 public boolean showBeforeProvisioning() {
799 return false;
800 }
801 };
802 }
803
Pavel Grafov059021b2018-05-02 13:44:46 +0100804 private void lockProfiles() {
805 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
806 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
807 final int currentUserId = getCurrentUser().id;
808 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
809 for (final int id : profileIds) {
810 if (id != currentUserId) {
811 tm.setDeviceLockedForUser(id, true);
812 }
813 }
814 }
815
Jason Monk361915c2017-03-21 20:33:59 -0400816 private UserInfo getCurrentUser() {
817 try {
818 return ActivityManager.getService().getCurrentUser();
819 } catch (RemoteException re) {
820 return null;
821 }
822 }
823
824 private boolean isCurrentUserOwner() {
825 UserInfo currentUser = getCurrentUser();
826 return currentUser == null || currentUser.isPrimary();
827 }
828
829 private void addUsersToMenu(ArrayList<Action> items) {
830 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
831 if (um.isUserSwitcherEnabled()) {
832 List<UserInfo> users = um.getUsers();
833 UserInfo currentUser = getCurrentUser();
834 for (final UserInfo user : users) {
835 if (user.supportsSwitchToByUser()) {
836 boolean isCurrentUser = currentUser == null
837 ? user.id == 0 : (currentUser.id == user.id);
838 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
839 : null;
840 SinglePressAction switchToUser = new SinglePressAction(
841 R.drawable.ic_menu_cc, icon,
842 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400843 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400844 public void onPress() {
845 try {
846 ActivityManager.getService().switchUser(user.id);
847 } catch (RemoteException re) {
848 Log.e(TAG, "Couldn't switch user " + re);
849 }
850 }
851
852 public boolean showDuringKeyguard() {
853 return true;
854 }
855
856 public boolean showBeforeProvisioning() {
857 return false;
858 }
859 };
860 items.add(switchToUser);
861 }
862 }
863 }
864 }
865
866 private void prepareDialog() {
867 refreshSilentMode();
868 mAirplaneModeOn.updateState(mAirplaneState);
869 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400870 if (mShowSilentToggle) {
871 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
872 mContext.registerReceiver(mRingerModeReceiver, filter);
873 }
874 }
875
876 private void refreshSilentMode() {
877 if (!mHasVibrator) {
878 final boolean silentModeOn =
879 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400880 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400881 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
882 }
883 }
884
885 /** {@inheritDoc} */
886 public void onDismiss(DialogInterface dialog) {
887 mWindowManagerFuncs.onGlobalActionsHidden();
888 if (mShowSilentToggle) {
889 try {
890 mContext.unregisterReceiver(mRingerModeReceiver);
891 } catch (IllegalArgumentException ie) {
892 // ignore this
893 Log.w(TAG, ie);
894 }
895 }
896 }
897
898 /** {@inheritDoc} */
Shaotang Li786da902018-08-02 11:18:00 +0800899 public void onShow(DialogInterface dialog) {
900 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
901 }
902
Jason Monk361915c2017-03-21 20:33:59 -0400903 /**
904 * The adapter used for the list within the global actions dialog, taking
905 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400906 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
907 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400908 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
909 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500910 public class MyAdapter extends MultiListAdapter {
911 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400912 public int getCount() {
913 int count = 0;
Jason Monk361915c2017-03-21 20:33:59 -0400914 for (int i = 0; i < mItems.size(); i++) {
915 final Action action = mItems.get(i);
916
917 if (mKeyguardShowing && !action.showDuringKeyguard()) {
918 continue;
919 }
920 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
921 continue;
922 }
923 count++;
924 }
925 return count;
926 }
927
928 @Override
929 public boolean isEnabled(int position) {
930 return getItem(position).isEnabled();
931 }
932
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500933 @Override
934 public ArrayList<Action> getSeparatedItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500935 ArrayList<Action> separatedActions = new ArrayList<Action>();
936 if (!shouldUseSeparatedView) {
937 return separatedActions;
938 }
939 for (int i = 0; i < mItems.size(); i++) {
940 final Action action = mItems.get(i);
941 if (action.shouldBeSeparated()) {
942 separatedActions.add(action);
943 }
944 }
945 return separatedActions;
946 }
947
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500948 @Override
949 public ArrayList<Action> getListItems(boolean shouldUseSeparatedView) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500950 if (!shouldUseSeparatedView) {
951 return new ArrayList<Action>(mItems);
952 }
953 ArrayList<Action> listActions = new ArrayList<Action>();
954 for (int i = 0; i < mItems.size(); i++) {
955 final Action action = mItems.get(i);
956 if (!action.shouldBeSeparated()) {
957 listActions.add(action);
958 }
959 }
960 return listActions;
961 }
962
Jason Monk361915c2017-03-21 20:33:59 -0400963 @Override
964 public boolean areAllItemsEnabled() {
965 return false;
966 }
967
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500968 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400969 public Action getItem(int position) {
970
971 int filteredPos = 0;
972 for (int i = 0; i < mItems.size(); i++) {
973 final Action action = mItems.get(i);
974 if (mKeyguardShowing && !action.showDuringKeyguard()) {
975 continue;
976 }
977 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
978 continue;
979 }
980 if (filteredPos == position) {
981 return action;
982 }
983 filteredPos++;
984 }
985
986 throw new IllegalArgumentException("position " + position
987 + " out of range of showable actions"
988 + ", filtered count=" + getCount()
989 + ", keyguardshowing=" + mKeyguardShowing
990 + ", provisioned=" + mDeviceProvisioned);
991 }
992
993
994 public long getItemId(int position) {
995 return position;
996 }
997
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500998 @Override
Jason Monk361915c2017-03-21 20:33:59 -0400999 public View getView(int position, View convertView, ViewGroup parent) {
1000 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001001 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -05001002 // Everything but screenshot, the last item, gets white background.
1003 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001004 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001005 }
1006 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001007 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001008
1009 @Override
1010 public boolean onLongClickItem(int position) {
1011 final Action action = mAdapter.getItem(position);
1012 if (action instanceof LongPressAction) {
1013 mDialog.dismiss();
1014 return ((LongPressAction) action).onLongPress();
1015 }
1016 return false;
1017 }
1018
1019 @Override
1020 public void onClickItem(int position) {
1021 Action item = mAdapter.getItem(position);
1022 if (!(item instanceof SilentModeTriStateAction)) {
1023 mDialog.dismiss();
1024 }
1025 item.onPress();
1026 }
Jason Monk361915c2017-03-21 20:33:59 -04001027 }
1028
1029 // note: the scheme below made more sense when we were planning on having
1030 // 8 different things in the global actions dialog. seems overkill with
1031 // only 3 items now, but may as well keep this flexible approach so it will
1032 // be easy should someone decide at the last minute to include something
1033 // else, such as 'enable wifi', or 'enable bluetooth'
1034
1035 /**
1036 * What each item in the global actions dialog must be able to support.
1037 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001038 public interface Action {
Jason Monk361915c2017-03-21 20:33:59 -04001039 /**
1040 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001041 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001042 */
1043 CharSequence getLabelForAccessibility(Context context);
1044
1045 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1046
1047 void onPress();
1048
1049 /**
1050 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001051 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001052 */
1053 boolean showDuringKeyguard();
1054
1055 /**
1056 * @return whether this action should appear in the dialog before the
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001057 * device is provisioned.onlongpress
1058 *
Jason Monk361915c2017-03-21 20:33:59 -04001059 */
1060 boolean showBeforeProvisioning();
1061
1062 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001063
1064 default boolean shouldBeSeparated() {
1065 return false;
1066 }
Jason Monk361915c2017-03-21 20:33:59 -04001067 }
1068
1069 /**
1070 * An action that also supports long press.
1071 */
1072 private interface LongPressAction extends Action {
1073 boolean onLongPress();
1074 }
1075
1076 /**
1077 * A single press action maintains no state, just responds to a press
1078 * and takes an action.
1079 */
1080 private static abstract class SinglePressAction implements Action {
1081 private final int mIconResId;
1082 private final Drawable mIcon;
1083 private final int mMessageResId;
1084 private final CharSequence mMessage;
1085
1086 protected SinglePressAction(int iconResId, int messageResId) {
1087 mIconResId = iconResId;
1088 mMessageResId = messageResId;
1089 mMessage = null;
1090 mIcon = null;
1091 }
1092
1093 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1094 mIconResId = iconResId;
1095 mMessageResId = 0;
1096 mMessage = message;
1097 mIcon = icon;
1098 }
1099
1100 public boolean isEnabled() {
1101 return true;
1102 }
1103
1104 public String getStatus() {
1105 return null;
1106 }
1107
1108 abstract public void onPress();
1109
1110 public CharSequence getLabelForAccessibility(Context context) {
1111 if (mMessage != null) {
1112 return mMessage;
1113 } else {
1114 return context.getString(mMessageResId);
1115 }
1116 }
1117
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001118 protected int getActionLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001119 if (isGridEnabled(context)) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001120 return com.android.systemui.R.layout.global_actions_grid_item;
1121 }
1122 return com.android.systemui.R.layout.global_actions_item;
1123 }
1124
Jason Monk361915c2017-03-21 20:33:59 -04001125 public View create(
1126 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001127 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001128 false);
Jason Monk361915c2017-03-21 20:33:59 -04001129
1130 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1131 TextView messageView = (TextView) v.findViewById(R.id.message);
1132
1133 TextView statusView = (TextView) v.findViewById(R.id.status);
1134 final String status = getStatus();
1135 if (!TextUtils.isEmpty(status)) {
1136 statusView.setText(status);
1137 } else {
1138 statusView.setVisibility(View.GONE);
1139 }
1140 if (mIcon != null) {
1141 icon.setImageDrawable(mIcon);
1142 icon.setScaleType(ScaleType.CENTER_CROP);
1143 } else if (mIconResId != 0) {
1144 icon.setImageDrawable(context.getDrawable(mIconResId));
1145 }
1146 if (mMessage != null) {
1147 messageView.setText(mMessage);
1148 } else {
1149 messageView.setText(mMessageResId);
1150 }
1151
1152 return v;
1153 }
1154 }
1155
1156 /**
1157 * A toggle action knows whether it is on or off, and displays an icon
1158 * and status message accordingly.
1159 */
1160 private static abstract class ToggleAction implements Action {
1161
1162 enum State {
1163 Off(false),
1164 TurningOn(true),
1165 TurningOff(true),
1166 On(false);
1167
1168 private final boolean inTransition;
1169
1170 State(boolean intermediate) {
1171 inTransition = intermediate;
1172 }
1173
1174 public boolean inTransition() {
1175 return inTransition;
1176 }
1177 }
1178
1179 protected State mState = State.Off;
1180
1181 // prefs
1182 protected int mEnabledIconResId;
1183 protected int mDisabledIconResid;
1184 protected int mMessageResId;
1185 protected int mEnabledStatusMessageResId;
1186 protected int mDisabledStatusMessageResId;
1187
1188 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001189 * @param enabledIconResId The icon for when this action is on.
1190 * @param disabledIconResid The icon for when this action is off.
1191 * @param message The general information message, e.g 'Silent Mode'
1192 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001193 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1194 */
1195 public ToggleAction(int enabledIconResId,
1196 int disabledIconResid,
1197 int message,
1198 int enabledStatusMessageResId,
1199 int disabledStatusMessageResId) {
1200 mEnabledIconResId = enabledIconResId;
1201 mDisabledIconResid = disabledIconResid;
1202 mMessageResId = message;
1203 mEnabledStatusMessageResId = enabledStatusMessageResId;
1204 mDisabledStatusMessageResId = disabledStatusMessageResId;
1205 }
1206
1207 /**
1208 * Override to make changes to resource IDs just before creating the
1209 * View.
1210 */
1211 void willCreate() {
1212
1213 }
1214
1215 @Override
1216 public CharSequence getLabelForAccessibility(Context context) {
1217 return context.getString(mMessageResId);
1218 }
1219
1220 public View create(Context context, View convertView, ViewGroup parent,
1221 LayoutInflater inflater) {
1222 willCreate();
1223
1224 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001225 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001226
1227 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1228 TextView messageView = (TextView) v.findViewById(R.id.message);
1229 TextView statusView = (TextView) v.findViewById(R.id.status);
1230 final boolean enabled = isEnabled();
1231
1232 if (messageView != null) {
1233 messageView.setText(mMessageResId);
1234 messageView.setEnabled(enabled);
1235 }
1236
1237 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1238 if (icon != null) {
1239 icon.setImageDrawable(context.getDrawable(
1240 (on ? mEnabledIconResId : mDisabledIconResid)));
1241 icon.setEnabled(enabled);
1242 }
1243
1244 if (statusView != null) {
1245 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1246 statusView.setVisibility(View.VISIBLE);
1247 statusView.setEnabled(enabled);
1248 }
1249 v.setEnabled(enabled);
1250
1251 return v;
1252 }
1253
1254 public final void onPress() {
1255 if (mState.inTransition()) {
1256 Log.w(TAG, "shouldn't be able to toggle when in transition");
1257 return;
1258 }
1259
1260 final boolean nowOn = !(mState == State.On);
1261 onToggle(nowOn);
1262 changeStateFromPress(nowOn);
1263 }
1264
1265 public boolean isEnabled() {
1266 return !mState.inTransition();
1267 }
1268
1269 /**
1270 * Implementations may override this if their state can be in on of the intermediate
1271 * states until some notification is received (e.g airplane mode is 'turning off' until
1272 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001273 *
Jason Monk361915c2017-03-21 20:33:59 -04001274 * @param buttonOn Whether the button was turned on or off
1275 */
1276 protected void changeStateFromPress(boolean buttonOn) {
1277 mState = buttonOn ? State.On : State.Off;
1278 }
1279
1280 abstract void onToggle(boolean on);
1281
1282 public void updateState(State state) {
1283 mState = state;
1284 }
1285 }
1286
1287 private class SilentModeToggleAction extends ToggleAction {
1288 public SilentModeToggleAction() {
1289 super(R.drawable.ic_audio_vol_mute,
1290 R.drawable.ic_audio_vol,
1291 R.string.global_action_toggle_silent_mode,
1292 R.string.global_action_silent_mode_on_status,
1293 R.string.global_action_silent_mode_off_status);
1294 }
1295
1296 void onToggle(boolean on) {
1297 if (on) {
1298 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1299 } else {
1300 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1301 }
1302 }
1303
1304 public boolean showDuringKeyguard() {
1305 return true;
1306 }
1307
1308 public boolean showBeforeProvisioning() {
1309 return false;
1310 }
1311 }
1312
1313 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1314
Jason Monk16fbd9d2017-04-27 14:28:49 -04001315 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001316
1317 private final AudioManager mAudioManager;
1318 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001319
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001320 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001321 mAudioManager = audioManager;
1322 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001323 }
1324
1325 private int ringerModeToIndex(int ringerMode) {
1326 // They just happen to coincide
1327 return ringerMode;
1328 }
1329
1330 private int indexToRingerMode(int index) {
1331 // They just happen to coincide
1332 return index;
1333 }
1334
1335 @Override
1336 public CharSequence getLabelForAccessibility(Context context) {
1337 return null;
1338 }
1339
1340 public View create(Context context, View convertView, ViewGroup parent,
1341 LayoutInflater inflater) {
1342 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1343
1344 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1345 for (int i = 0; i < 3; i++) {
1346 View itemView = v.findViewById(ITEM_IDS[i]);
1347 itemView.setSelected(selectedIndex == i);
1348 // Set up click handler
1349 itemView.setTag(i);
1350 itemView.setOnClickListener(this);
1351 }
1352 return v;
1353 }
1354
1355 public void onPress() {
1356 }
1357
1358 public boolean showDuringKeyguard() {
1359 return true;
1360 }
1361
1362 public boolean showBeforeProvisioning() {
1363 return false;
1364 }
1365
1366 public boolean isEnabled() {
1367 return true;
1368 }
1369
1370 void willCreate() {
1371 }
1372
1373 public void onClick(View v) {
1374 if (!(v.getTag() instanceof Integer)) return;
1375
1376 int index = (Integer) v.getTag();
1377 mAudioManager.setRingerMode(indexToRingerMode(index));
1378 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1379 }
1380 }
1381
1382 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1383 public void onReceive(Context context, Intent intent) {
1384 String action = intent.getAction();
1385 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1386 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1387 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1388 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001389 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001390 }
1391 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1392 // Airplane mode can be changed after ECM exits if airplane toggle button
1393 // is pressed during ECM mode
1394 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1395 mIsWaitingForEcmExit) {
1396 mIsWaitingForEcmExit = false;
1397 changeAirplaneModeSystemSetting(true);
1398 }
1399 }
1400 }
1401 };
1402
1403 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1404 @Override
1405 public void onServiceStateChanged(ServiceState serviceState) {
1406 if (!mHasTelephony) return;
1407 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1408 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1409 mAirplaneModeOn.updateState(mAirplaneState);
1410 mAdapter.notifyDataSetChanged();
1411 }
1412 };
1413
1414 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1415 @Override
1416 public void onReceive(Context context, Intent intent) {
1417 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1418 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1419 }
1420 }
1421 };
1422
1423 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1424 @Override
1425 public void onChange(boolean selfChange) {
1426 onAirplaneModeChanged();
1427 }
1428 };
1429
1430 private static final int MESSAGE_DISMISS = 0;
1431 private static final int MESSAGE_REFRESH = 1;
1432 private static final int MESSAGE_SHOW = 2;
1433 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1434
1435 private Handler mHandler = new Handler() {
1436 public void handleMessage(Message msg) {
1437 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001438 case MESSAGE_DISMISS:
1439 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001440 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1441 mDialog.dismissImmediately();
1442 } else {
1443 mDialog.dismiss();
1444 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001445 mDialog = null;
1446 }
1447 break;
1448 case MESSAGE_REFRESH:
1449 refreshSilentMode();
1450 mAdapter.notifyDataSetChanged();
1451 break;
1452 case MESSAGE_SHOW:
1453 handleShow();
1454 break;
Jason Monk361915c2017-03-21 20:33:59 -04001455 }
1456 }
1457 };
1458
1459 private void onAirplaneModeChanged() {
1460 // Let the service state callbacks handle the state.
1461 if (mHasTelephony) return;
1462
1463 boolean airplaneModeOn = Settings.Global.getInt(
1464 mContext.getContentResolver(),
1465 Settings.Global.AIRPLANE_MODE_ON,
1466 0) == 1;
1467 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1468 mAirplaneModeOn.updateState(mAirplaneState);
1469 }
1470
1471 /**
1472 * Change the airplane mode system setting
1473 */
1474 private void changeAirplaneModeSystemSetting(boolean on) {
1475 Settings.Global.putInt(
1476 mContext.getContentResolver(),
1477 Settings.Global.AIRPLANE_MODE_ON,
1478 on ? 1 : 0);
1479 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1480 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1481 intent.putExtra("state", on);
1482 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1483 if (!mHasTelephony) {
1484 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1485 }
1486 }
1487
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001488 private static final class ActionsDialog extends Dialog implements DialogInterface,
1489 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001490
Jason Monk16fbd9d2017-04-27 14:28:49 -04001491 private final Context mContext;
1492 private final MyAdapter mAdapter;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001493 private MultiListLayout mGlobalActionsLayout;
Steve Elliott9b87a442019-03-05 10:24:16 -05001494 private final Drawable mBackgroundDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001495 private final ColorExtractor mColorExtractor;
Steve Elliott9b87a442019-03-05 10:24:16 -05001496 private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001497 private boolean mKeyguardShowing;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001498 private boolean mUseSeparatedList;
Beverly526d2d62018-08-15 12:55:33 -04001499 private boolean mShowing;
Steve Elliott9b87a442019-03-05 10:24:16 -05001500 private final float mScrimAlpha;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001501
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001502 ActionsDialog(Context context, MyAdapter adapter, boolean separated,
Steve Elliott9b87a442019-03-05 10:24:16 -05001503 GlobalActionsPanelPlugin.PanelViewController plugin) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001504 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001505 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001506 mAdapter = adapter;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001507 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001508 mUseSeparatedList = separated;
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);
Steve Elliottbfa314a2019-03-06 13:53:19 -05001537 mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
Steve Elliott9b87a442019-03-05 10:24:16 -05001538 } 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);
Steve Elliott9b87a442019-03-05 10:24:16 -05001560 panelContainer.addView(panelView, panelParams);
1561 return panelContainer;
1562 }
1563 }
1564 return null;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001565 }
1566
1567 private void initializeLayout() {
1568 setContentView(getGlobalActionsLayoutId(mContext));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001569 mGlobalActionsLayout = (MultiListLayout)
1570 findViewById(com.android.systemui.R.id.global_actions_view);
1571 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001572 mGlobalActionsLayout.setSeparated(mUseSeparatedList);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001573 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001574 @Override
1575 public boolean dispatchPopulateAccessibilityEvent(
1576 View host, AccessibilityEvent event) {
1577 // Populate the title here, just as Activity does
1578 event.getText().add(mContext.getString(R.string.global_actions));
1579 return true;
1580 }
1581 });
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001582 mGlobalActionsLayout.setRotationListener(this::onRotate);
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001583 mGlobalActionsLayout.setAdapter(mAdapter);
Jason Monk361915c2017-03-21 20:33:59 -04001584 }
1585
Steve Elliott9b87a442019-03-05 10:24:16 -05001586 private boolean isPanelEnabled(Context context) {
1587 return FeatureFlagUtils.isEnabled(
1588 context, FeatureFlagUtils.GLOBAL_ACTIONS_PANEL_ENABLED);
1589 }
1590
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001591 private int getGlobalActionsLayoutId(Context context) {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001592 if (isGridEnabled(context)) {
1593 if (RotationUtils.getRotation(context) == RotationUtils.ROTATION_SEASCAPE) {
1594 return com.android.systemui.R.layout.global_actions_grid_seascape;
1595 }
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001596 return com.android.systemui.R.layout.global_actions_grid;
1597 }
1598 return com.android.systemui.R.layout.global_actions_wrapped;
1599 }
1600
Jason Monk361915c2017-03-21 20:33:59 -04001601 @Override
1602 protected void onStart() {
1603 super.setCanceledOnTouchOutside(true);
1604 super.onStart();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001605 mGlobalActionsLayout.updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001606
Steve Elliott9b87a442019-03-05 10:24:16 -05001607 if (mBackgroundDrawable instanceof GradientDrawable) {
1608 Point displaySize = new Point();
1609 mContext.getDisplay().getRealSize(displaySize);
1610 mColorExtractor.addOnColorsChangedListener(this);
1611 ((GradientDrawable) mBackgroundDrawable)
1612 .setScreenSize(displaySize.x, displaySize.y);
1613 GradientColors colors = mColorExtractor.getColors(
1614 mKeyguardShowing
1615 ? WallpaperManager.FLAG_LOCK
1616 : WallpaperManager.FLAG_SYSTEM);
1617 updateColors(colors, false /* animate */);
1618 }
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001619 }
1620
1621 /**
1622 * Updates background and system bars according to current GradientColors.
1623 * @param colors Colors and hints to use.
1624 * @param animate Interpolates gradient if true, just sets otherwise.
1625 */
1626 private void updateColors(GradientColors colors, boolean animate) {
Steve Elliott9b87a442019-03-05 10:24:16 -05001627 if (!(mBackgroundDrawable instanceof GradientDrawable)) {
1628 return;
1629 }
1630 ((GradientDrawable) mBackgroundDrawable).setColors(colors, animate);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001631 View decorView = getWindow().getDecorView();
1632 if (colors.supportsDarkText()) {
1633 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
Steve Elliott9b87a442019-03-05 10:24:16 -05001634 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001635 } else {
1636 decorView.setSystemUiVisibility(0);
1637 }
Jason Monk361915c2017-03-21 20:33:59 -04001638 }
1639
1640 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001641 protected void onStop() {
1642 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001643 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001644 }
1645
1646 @Override
1647 public void show() {
1648 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001649 mShowing = true;
Steve Elliott9b87a442019-03-05 10:24:16 -05001650 mBackgroundDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001651 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1652 mGlobalActionsLayout.setAlpha(0);
1653 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001654 .alpha(1)
1655 .translationX(0)
1656 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001657 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001658 .setUpdateListener(animation -> {
1659 int alpha = (int) ((Float) animation.getAnimatedValue()
Steve Elliott9b87a442019-03-05 10:24:16 -05001660 * mScrimAlpha * 255);
1661 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001662 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001663 .start();
1664 }
1665
1666 @Override
1667 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001668 if (!mShowing) {
1669 return;
1670 }
1671 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001672 mGlobalActionsLayout.setTranslationX(0);
1673 mGlobalActionsLayout.setAlpha(1);
1674 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001675 .alpha(0)
1676 .translationX(getAnimTranslation())
1677 .setDuration(300)
Steve Elliott9b87a442019-03-05 10:24:16 -05001678 .withEndAction(super::dismiss)
Jason Monk16fbd9d2017-04-27 14:28:49 -04001679 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001680 .setUpdateListener(animation -> {
1681 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
Steve Elliott9b87a442019-03-05 10:24:16 -05001682 * mScrimAlpha * 255);
1683 mBackgroundDrawable.setAlpha(alpha);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001684 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001685 .start();
Steve Elliott9b87a442019-03-05 10:24:16 -05001686 if (mPanelController != null) {
1687 mPanelController.onDismissed();
1688 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001689 }
1690
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001691 void dismissImmediately() {
1692 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001693 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001694 }
1695
Jason Monk16fbd9d2017-04-27 14:28:49 -04001696 private float getAnimTranslation() {
1697 return getContext().getResources().getDimension(
1698 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001699 }
1700
1701 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001702 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001703 if (mKeyguardShowing) {
1704 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001705 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1706 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001707 }
1708 } else {
1709 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001710 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1711 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001712 }
1713 }
1714 }
1715
1716 public void setKeyguardShowing(boolean keyguardShowing) {
1717 mKeyguardShowing = keyguardShowing;
1718 }
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001719
1720 public void onRotate(int from, int to) {
1721 if (mShowing && isGridEnabled(mContext)) {
1722 initializeLayout();
Aaron Heuckroth57d60d22019-03-05 14:00:12 -05001723 mGlobalActionsLayout.updateList();
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -05001724 }
1725 }
Jason Monk361915c2017-03-21 20:33:59 -04001726 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -05001727
1728 /**
1729 * Determines whether or not the Global Actions Dialog should use the newer grid-style layout.
1730 */
1731 public static boolean isGridEnabled(Context context) {
1732 return FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED);
1733 }
Jason Monk361915c2017-03-21 20:33:59 -04001734}