blob: f5ac0d39d61f6d11d0f3607a1a495bdc9dcfd1ca [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;
70import android.widget.ImageView;
71import android.widget.ImageView.ScaleType;
Jason Monk361915c2017-03-21 20:33:59 -040072import android.widget.TextView;
73
Charles He9851a8d2017-10-10 17:31:30 +010074import com.android.internal.R;
75import com.android.internal.colorextraction.ColorExtractor;
76import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070077import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010078import com.android.internal.logging.MetricsLogger;
79import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
80import com.android.internal.telephony.TelephonyIntents;
81import com.android.internal.telephony.TelephonyProperties;
82import com.android.internal.util.EmergencyAffordanceManager;
Beth Thibodeau5898ac42018-10-26 13:00:09 -040083import com.android.internal.util.ScreenRecordHelper;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050084import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.internal.widget.LockPatternUtils;
86import com.android.systemui.Dependency;
Charles He9851a8d2017-10-10 17:31:30 +010087import com.android.systemui.Interpolators;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050088import com.android.systemui.MultiListLayout;
Charles He9851a8d2017-10-10 17:31:30 +010089import com.android.systemui.colorextraction.SysuiColorExtractor;
90import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
91import com.android.systemui.statusbar.phone.ScrimController;
Lucas Dupine5b7dc72018-10-02 15:18:05 -070092import com.android.systemui.statusbar.policy.ConfigurationController;
Shaotang Li5c422632018-07-04 14:18:40 +080093import com.android.systemui.util.EmergencyDialerConstants;
Julia Reynolds42411922017-11-08 11:19:09 -050094import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070095
Jason Monk361915c2017-03-21 20:33:59 -040096import java.util.ArrayList;
97import java.util.List;
98
99/**
100 * Helper to show the global actions dialog. Each item is an {@link Action} that
101 * may show depending on whether the keyguard is showing, and whether the device
102 * is provisioned.
103 */
Charles He9851a8d2017-10-10 17:31:30 +0100104class GlobalActionsDialog implements DialogInterface.OnDismissListener,
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700105 DialogInterface.OnClickListener, DialogInterface.OnShowListener,
106 ConfigurationController.ConfigurationListener {
Jason Monk361915c2017-03-21 20:33:59 -0400107
108 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
109 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700110 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400111
112 private static final String TAG = "GlobalActionsDialog";
113
114 private static final boolean SHOW_SILENT_TOGGLE = true;
115
116 /* Valid settings for global actions keys.
117 * see config.xml config_globalActionList */
118 private static final String GLOBAL_ACTION_KEY_POWER = "power";
119 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
120 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
121 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
122 private static final String GLOBAL_ACTION_KEY_USERS = "users";
123 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
124 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
125 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
126 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
127 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000128 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800129 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500130 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400131
132 private final Context mContext;
133 private final GlobalActionsManager mWindowManagerFuncs;
134 private final AudioManager mAudioManager;
135 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000136 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800137 private final LockPatternUtils mLockPatternUtils;
138 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400139
140 private ArrayList<Action> mItems;
141 private ActionsDialog mDialog;
142
143 private Action mSilentModeAction;
144 private ToggleAction mAirplaneModeOn;
145
146 private MyAdapter mAdapter;
147
148 private boolean mKeyguardShowing = false;
149 private boolean mDeviceProvisioned = false;
150 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
151 private boolean mIsWaitingForEcmExit = false;
152 private boolean mHasTelephony;
153 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000154 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800155 private boolean mHasLockdownButton;
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800156 private boolean mSeparatedEmergencyButtonEnabled;
Jason Monk361915c2017-03-21 20:33:59 -0400157 private final boolean mShowSilentToggle;
158 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500159 private final ScreenshotHelper mScreenshotHelper;
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400160 private final ScreenRecordHelper mScreenRecordHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400161
162 /**
163 * @param context everything needs a context :(
164 */
165 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700166 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400167 mWindowManagerFuncs = windowManagerFuncs;
168 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
169 mDreamManager = IDreamManager.Stub.asInterface(
170 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000171 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
172 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800173 mLockPatternUtils = new LockPatternUtils(mContext);
174 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400175
176 // receive broadcasts
177 IntentFilter filter = new IntentFilter();
178 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
179 filter.addAction(Intent.ACTION_SCREEN_OFF);
180 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
181 context.registerReceiver(mBroadcastReceiver, filter);
182
183 ConnectivityManager cm = (ConnectivityManager)
184 context.getSystemService(Context.CONNECTIVITY_SERVICE);
185 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
186
187 // get notified of phone state changes
188 TelephonyManager telephonyManager =
189 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
190 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
191 mContext.getContentResolver().registerContentObserver(
192 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
193 mAirplaneModeObserver);
194 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
195 mHasVibrator = vibrator != null && vibrator.hasVibrator();
196
197 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
198 R.bool.config_useFixedVolume);
199
200 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500201 mScreenshotHelper = new ScreenshotHelper(context);
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400202 mScreenRecordHelper = new ScreenRecordHelper(context);
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700203
204 Dependency.get(ConfigurationController.class).addCallback(this);
Jason Monk361915c2017-03-21 20:33:59 -0400205 }
206
207 /**
208 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400209 *
Jason Monk361915c2017-03-21 20:33:59 -0400210 * @param keyguardShowing True if keyguard is showing
211 */
212 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
213 mKeyguardShowing = keyguardShowing;
214 mDeviceProvisioned = isDeviceProvisioned;
215 if (mDialog != null) {
216 mDialog.dismiss();
217 mDialog = null;
218 // Show delayed, so that the dismiss of the previous dialog completes
219 mHandler.sendEmptyMessage(MESSAGE_SHOW);
220 } else {
221 handleShow();
222 }
223 }
224
Charles He9851a8d2017-10-10 17:31:30 +0100225 /**
226 * Dismiss the global actions dialog, if it's currently shown
227 */
228 public void dismissDialog() {
229 mHandler.removeMessages(MESSAGE_DISMISS);
230 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
231 }
232
Jason Monk361915c2017-03-21 20:33:59 -0400233 private void awakenIfNecessary() {
234 if (mDreamManager != null) {
235 try {
236 if (mDreamManager.isDreaming()) {
237 mDreamManager.awaken();
238 }
239 } catch (RemoteException e) {
240 // we tried
241 }
242 }
243 }
244
245 private void handleShow() {
246 awakenIfNecessary();
247 mDialog = createDialog();
248 prepareDialog();
249
250 // If we only have 1 item and it's a simple press action, just do this action.
251 if (mAdapter.getCount() == 1
252 && mAdapter.getItem(0) instanceof SinglePressAction
253 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
254 ((SinglePressAction) mAdapter.getItem(0)).onPress();
255 } else {
256 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
257 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100258 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400259 mDialog.getWindow().setAttributes(attrs);
260 mDialog.show();
261 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400262 }
263 }
264
265 /**
266 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400267 *
Jason Monk361915c2017-03-21 20:33:59 -0400268 * @return A new dialog.
269 */
270 private ActionsDialog createDialog() {
271 // Simple toggle style if there's no vibrator, otherwise use a tri-state
272 if (!mHasVibrator) {
273 mSilentModeAction = new SilentModeToggleAction();
274 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700275 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400276 }
277 mAirplaneModeOn = new ToggleAction(
278 R.drawable.ic_lock_airplane_mode,
279 R.drawable.ic_lock_airplane_mode_off,
280 R.string.global_actions_toggle_airplane_mode,
281 R.string.global_actions_airplane_mode_on_status,
282 R.string.global_actions_airplane_mode_off_status) {
283
284 void onToggle(boolean on) {
285 if (mHasTelephony && Boolean.parseBoolean(
286 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
287 mIsWaitingForEcmExit = true;
288 // Launch ECM exit dialog
289 Intent ecmDialogIntent =
290 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
291 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
292 mContext.startActivity(ecmDialogIntent);
293 } else {
294 changeAirplaneModeSystemSetting(on);
295 }
296 }
297
298 @Override
299 protected void changeStateFromPress(boolean buttonOn) {
300 if (!mHasTelephony) return;
301
302 // In ECM mode airplane state cannot be changed
303 if (!(Boolean.parseBoolean(
304 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
305 mState = buttonOn ? State.TurningOn : State.TurningOff;
306 mAirplaneState = mState;
307 }
308 }
309
310 public boolean showDuringKeyguard() {
311 return true;
312 }
313
314 public boolean showBeforeProvisioning() {
315 return false;
316 }
317 };
318 onAirplaneModeChanged();
319
320 mItems = new ArrayList<Action>();
321 String[] defaultActions = mContext.getResources().getStringArray(
322 R.array.config_globalActionsList);
323
324 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000325 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800326 mHasLockdownButton = false;
yuanjiahsu2c69b0b2018-11-16 21:37:33 +0800327 mSeparatedEmergencyButtonEnabled = true;
Jason Monk361915c2017-03-21 20:33:59 -0400328 for (int i = 0; i < defaultActions.length; i++) {
329 String actionKey = defaultActions[i];
330 if (addedKeys.contains(actionKey)) {
331 // If we already have added this, don't add it again.
332 continue;
333 }
334 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
335 mItems.add(new PowerAction());
336 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
337 mItems.add(mAirplaneModeOn);
338 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
339 if (Settings.Global.getInt(mContext.getContentResolver(),
340 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
341 mItems.add(new BugReportAction());
342 }
343 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
344 if (mShowSilentToggle) {
345 mItems.add(mSilentModeAction);
346 }
347 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
348 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
349 addUsersToMenu(mItems);
350 }
351 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
352 mItems.add(getSettingsAction());
353 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700354 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
355 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800356 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700357 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800358 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700359 }
Jason Monk361915c2017-03-21 20:33:59 -0400360 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
361 mItems.add(getVoiceAssistAction());
362 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
363 mItems.add(getAssistAction());
364 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
365 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500366 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
367 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000368 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000369 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000370 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
371 mItems.add(new LogoutAction());
372 mHasLogoutButton = true;
373 }
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800374 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
375 if (mSeparatedEmergencyButtonEnabled
376 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
377 mItems.add(new EmergencyDialerAction());
378 }
Jason Monk361915c2017-03-21 20:33:59 -0400379 } else {
380 Log.e(TAG, "Invalid global action key " + actionKey);
381 }
382 // Add here so we don't add more than one.
383 addedKeys.add(actionKey);
384 }
385
386 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
387 mItems.add(getEmergencyAction());
388 }
389
390 mAdapter = new MyAdapter();
391
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700392 OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> {
393 final Action action = mAdapter.getItem(position);
394 if (action instanceof LongPressAction) {
395 mDialog.dismiss();
396 return ((LongPressAction) action).onLongPress();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400397 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700398 return false;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400399 };
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800400 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener,
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800401 mSeparatedEmergencyButtonEnabled);
Jason Monk361915c2017-03-21 20:33:59 -0400402 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700403 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400404
405 dialog.setOnDismissListener(this);
Shaotang Li786da902018-08-02 11:18:00 +0800406 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400407
408 return dialog;
409 }
410
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800411 private boolean shouldDisplayLockdown() {
412 int userId = getCurrentUser().id;
413 // Lockdown is meaningless without a place to go.
414 if (!mKeyguardManager.isDeviceSecure(userId)) {
415 return false;
416 }
417
418 // Only show the lockdown button if the device isn't locked down (for whatever reason).
419 int state = mLockPatternUtils.getStrongAuthForUser(userId);
420 return (state == STRONG_AUTH_NOT_REQUIRED
421 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
422 }
423
Lucas Dupine5b7dc72018-10-02 15:18:05 -0700424 @Override
425 public void onUiModeChanged() {
426 mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
427 }
428
429 public void destroy() {
430 Dependency.get(ConfigurationController.class).removeCallback(this);
431 }
432
Jason Monk361915c2017-03-21 20:33:59 -0400433 private final class PowerAction extends SinglePressAction implements LongPressAction {
434 private PowerAction() {
435 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400436 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400437 }
438
439 @Override
440 public boolean onLongPress() {
441 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
442 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
443 mWindowManagerFuncs.reboot(true);
444 return true;
445 }
446 return false;
447 }
448
449 @Override
450 public boolean showDuringKeyguard() {
451 return true;
452 }
453
454 @Override
455 public boolean showBeforeProvisioning() {
456 return true;
457 }
458
459 @Override
460 public void onPress() {
461 // shutdown by making sure radio and power are handled accordingly.
462 mWindowManagerFuncs.shutdown();
463 }
464 }
465
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800466 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800467 private EmergencyDialerAction() {
468 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800469 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800470 }
471
472 @Override
473 public void onPress() {
Shaotang Li786da902018-08-02 11:18:00 +0800474 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Li5c422632018-07-04 14:18:40 +0800475 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800476 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Li5c422632018-07-04 14:18:40 +0800477 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
478 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800479 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
480 }
481
482 @Override
483 public boolean showDuringKeyguard() {
484 return true;
485 }
486
487 @Override
488 public boolean showBeforeProvisioning() {
489 return true;
490 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500491
492 @Override
493 public boolean shouldBeSeparated() {
494 return true;
495 }
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800496 }
497
Jason Monk361915c2017-03-21 20:33:59 -0400498 private final class RestartAction extends SinglePressAction implements LongPressAction {
499 private RestartAction() {
500 super(R.drawable.ic_restart, R.string.global_action_restart);
501 }
502
503 @Override
504 public boolean onLongPress() {
505 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
506 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
507 mWindowManagerFuncs.reboot(true);
508 return true;
509 }
510 return false;
511 }
512
513 @Override
514 public boolean showDuringKeyguard() {
515 return true;
516 }
517
518 @Override
519 public boolean showBeforeProvisioning() {
520 return true;
521 }
522
523 @Override
524 public void onPress() {
525 mWindowManagerFuncs.reboot(false);
526 }
527 }
528
529
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400530 private class ScreenshotAction extends SinglePressAction implements LongPressAction {
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500531 public ScreenshotAction() {
532 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
533 }
534
535 @Override
536 public void onPress() {
537 // Add a little delay before executing, to give the
538 // dialog a chance to go away before it takes a
539 // screenshot.
540 // TODO: instead, omit global action dialog layer
541 mHandler.postDelayed(new Runnable() {
542 @Override
543 public void run() {
544 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
545 MetricsLogger.action(mContext,
546 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
547 }
548 }, 500);
549 }
550
551 @Override
552 public boolean showDuringKeyguard() {
553 return true;
554 }
555
556 @Override
557 public boolean showBeforeProvisioning() {
558 return false;
559 }
Beth Thibodeau5898ac42018-10-26 13:00:09 -0400560
561 @Override
562 public boolean onLongPress() {
563 if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
564 mScreenRecordHelper.launchRecordPrompt();
565 } else {
566 onPress();
567 }
568 return true;
569 }
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500570 }
571
Jason Monk361915c2017-03-21 20:33:59 -0400572 private class BugReportAction extends SinglePressAction implements LongPressAction {
573
574 public BugReportAction() {
575 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
576 }
577
578 @Override
579 public void onPress() {
580 // don't actually trigger the bugreport if we are running stability
581 // tests via monkey
582 if (ActivityManager.isUserAMonkey()) {
583 return;
584 }
585 // Add a little delay before executing, to give the
586 // dialog a chance to go away before it takes a
587 // screenshot.
588 mHandler.postDelayed(new Runnable() {
589 @Override
590 public void run() {
591 try {
592 // Take an "interactive" bugreport.
593 MetricsLogger.action(mContext,
594 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
595 ActivityManager.getService().requestBugReport(
596 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
597 } catch (RemoteException e) {
598 }
599 }
600 }, 500);
601 }
602
603 @Override
604 public boolean onLongPress() {
605 // don't actually trigger the bugreport if we are running stability
606 // tests via monkey
607 if (ActivityManager.isUserAMonkey()) {
608 return false;
609 }
610 try {
611 // Take a "full" bugreport.
612 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
613 ActivityManager.getService().requestBugReport(
614 ActivityManager.BUGREPORT_OPTION_FULL);
615 } catch (RemoteException e) {
616 }
617 return false;
618 }
619
620 public boolean showDuringKeyguard() {
621 return true;
622 }
623
624 @Override
625 public boolean showBeforeProvisioning() {
626 return false;
627 }
628
629 @Override
630 public String getStatus() {
631 return mContext.getString(
632 R.string.bugreport_status,
633 Build.VERSION.RELEASE,
634 Build.ID);
635 }
636 }
637
Alex Chau04458852017-11-27 18:21:23 +0000638 private final class LogoutAction extends SinglePressAction {
639 private LogoutAction() {
640 super(R.drawable.ic_logout, R.string.global_action_logout);
641 }
642
643 @Override
644 public boolean showDuringKeyguard() {
645 return true;
646 }
647
648 @Override
649 public boolean showBeforeProvisioning() {
650 return false;
651 }
652
653 @Override
654 public void onPress() {
655 // Add a little delay before executing, to give the dialog a chance to go away before
656 // switching user
657 mHandler.postDelayed(() -> {
658 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000659 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000660 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000661 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000662 } catch (RemoteException re) {
663 Log.e(TAG, "Couldn't logout user " + re);
664 }
665 }, 500);
666 }
667 }
668
Jason Monk361915c2017-03-21 20:33:59 -0400669 private Action getSettingsAction() {
670 return new SinglePressAction(R.drawable.ic_settings,
671 R.string.global_action_settings) {
672
673 @Override
674 public void onPress() {
675 Intent intent = new Intent(Settings.ACTION_SETTINGS);
676 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
677 mContext.startActivity(intent);
678 }
679
680 @Override
681 public boolean showDuringKeyguard() {
682 return true;
683 }
684
685 @Override
686 public boolean showBeforeProvisioning() {
687 return true;
688 }
689 };
690 }
691
692 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800693 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
694 if(!mSeparatedEmergencyButtonEnabled) {
695 // use un-colored legacy treatment
696 emergencyIcon.setTintList(null);
697 }
698
Jason Monk361915c2017-03-21 20:33:59 -0400699 return new SinglePressAction(R.drawable.emergency_icon,
700 R.string.global_action_emergency) {
701 @Override
702 public void onPress() {
703 mEmergencyAffordanceManager.performEmergencyCall();
704 }
705
706 @Override
707 public boolean showDuringKeyguard() {
708 return true;
709 }
710
711 @Override
712 public boolean showBeforeProvisioning() {
713 return true;
714 }
715 };
716 }
717
718 private Action getAssistAction() {
719 return new SinglePressAction(R.drawable.ic_action_assist_focused,
720 R.string.global_action_assist) {
721 @Override
722 public void onPress() {
723 Intent intent = new Intent(Intent.ACTION_ASSIST);
724 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
725 mContext.startActivity(intent);
726 }
727
728 @Override
729 public boolean showDuringKeyguard() {
730 return true;
731 }
732
733 @Override
734 public boolean showBeforeProvisioning() {
735 return true;
736 }
737 };
738 }
739
740 private Action getVoiceAssistAction() {
741 return new SinglePressAction(R.drawable.ic_voice_search,
742 R.string.global_action_voice_assist) {
743 @Override
744 public void onPress() {
745 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
746 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
747 mContext.startActivity(intent);
748 }
749
750 @Override
751 public boolean showDuringKeyguard() {
752 return true;
753 }
754
755 @Override
756 public boolean showBeforeProvisioning() {
757 return true;
758 }
759 };
760 }
761
762 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400763 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400764 R.string.global_action_lockdown) {
765
766 @Override
767 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700768 new LockPatternUtils(mContext)
769 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
770 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400771 try {
772 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100773 // Lock profiles (if any) on the background thread.
774 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
775 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400776 } catch (RemoteException e) {
777 Log.e(TAG, "Error while trying to lock device.", e);
778 }
779 }
780
781 @Override
782 public boolean showDuringKeyguard() {
783 return true;
784 }
785
786 @Override
787 public boolean showBeforeProvisioning() {
788 return false;
789 }
790 };
791 }
792
Pavel Grafov059021b2018-05-02 13:44:46 +0100793 private void lockProfiles() {
794 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
795 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
796 final int currentUserId = getCurrentUser().id;
797 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
798 for (final int id : profileIds) {
799 if (id != currentUserId) {
800 tm.setDeviceLockedForUser(id, true);
801 }
802 }
803 }
804
Jason Monk361915c2017-03-21 20:33:59 -0400805 private UserInfo getCurrentUser() {
806 try {
807 return ActivityManager.getService().getCurrentUser();
808 } catch (RemoteException re) {
809 return null;
810 }
811 }
812
813 private boolean isCurrentUserOwner() {
814 UserInfo currentUser = getCurrentUser();
815 return currentUser == null || currentUser.isPrimary();
816 }
817
818 private void addUsersToMenu(ArrayList<Action> items) {
819 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
820 if (um.isUserSwitcherEnabled()) {
821 List<UserInfo> users = um.getUsers();
822 UserInfo currentUser = getCurrentUser();
823 for (final UserInfo user : users) {
824 if (user.supportsSwitchToByUser()) {
825 boolean isCurrentUser = currentUser == null
826 ? user.id == 0 : (currentUser.id == user.id);
827 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
828 : null;
829 SinglePressAction switchToUser = new SinglePressAction(
830 R.drawable.ic_menu_cc, icon,
831 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400832 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400833 public void onPress() {
834 try {
835 ActivityManager.getService().switchUser(user.id);
836 } catch (RemoteException re) {
837 Log.e(TAG, "Couldn't switch user " + re);
838 }
839 }
840
841 public boolean showDuringKeyguard() {
842 return true;
843 }
844
845 public boolean showBeforeProvisioning() {
846 return false;
847 }
848 };
849 items.add(switchToUser);
850 }
851 }
852 }
853 }
854
855 private void prepareDialog() {
856 refreshSilentMode();
857 mAirplaneModeOn.updateState(mAirplaneState);
858 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400859 if (mShowSilentToggle) {
860 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
861 mContext.registerReceiver(mRingerModeReceiver, filter);
862 }
863 }
864
865 private void refreshSilentMode() {
866 if (!mHasVibrator) {
867 final boolean silentModeOn =
868 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400869 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400870 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
871 }
872 }
873
874 /** {@inheritDoc} */
875 public void onDismiss(DialogInterface dialog) {
876 mWindowManagerFuncs.onGlobalActionsHidden();
877 if (mShowSilentToggle) {
878 try {
879 mContext.unregisterReceiver(mRingerModeReceiver);
880 } catch (IllegalArgumentException ie) {
881 // ignore this
882 Log.w(TAG, ie);
883 }
884 }
885 }
886
887 /** {@inheritDoc} */
888 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400889 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400890 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400891 dialog.dismiss();
892 }
Jason Monkfd279662017-06-29 19:37:48 -0400893 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400894 }
895
Shaotang Li786da902018-08-02 11:18:00 +0800896 /** {@inheritDoc} */
897 public void onShow(DialogInterface dialog) {
898 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
899 }
900
Jason Monk361915c2017-03-21 20:33:59 -0400901 /**
902 * The adapter used for the list within the global actions dialog, taking
903 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400904 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
905 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400906 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
907 */
908 private class MyAdapter extends BaseAdapter {
909
910 public int getCount() {
911 int count = 0;
912
913 for (int i = 0; i < mItems.size(); i++) {
914 final Action action = mItems.get(i);
915
916 if (mKeyguardShowing && !action.showDuringKeyguard()) {
917 continue;
918 }
919 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
920 continue;
921 }
922 count++;
923 }
924 return count;
925 }
926
927 @Override
928 public boolean isEnabled(int position) {
929 return getItem(position).isEnabled();
930 }
931
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500932 public ArrayList<Action> getSeparatedActions(boolean shouldUseSeparatedView) {
933 ArrayList<Action> separatedActions = new ArrayList<Action>();
934 if (!shouldUseSeparatedView) {
935 return separatedActions;
936 }
937 for (int i = 0; i < mItems.size(); i++) {
938 final Action action = mItems.get(i);
939 if (action.shouldBeSeparated()) {
940 separatedActions.add(action);
941 }
942 }
943 return separatedActions;
944 }
945
946 public ArrayList<Action> getListActions(boolean shouldUseSeparatedView) {
947 if (!shouldUseSeparatedView) {
948 return new ArrayList<Action>(mItems);
949 }
950 ArrayList<Action> listActions = new ArrayList<Action>();
951 for (int i = 0; i < mItems.size(); i++) {
952 final Action action = mItems.get(i);
953 if (!action.shouldBeSeparated()) {
954 listActions.add(action);
955 }
956 }
957 return listActions;
958 }
959
Jason Monk361915c2017-03-21 20:33:59 -0400960 @Override
961 public boolean areAllItemsEnabled() {
962 return false;
963 }
964
965 public Action getItem(int position) {
966
967 int filteredPos = 0;
968 for (int i = 0; i < mItems.size(); i++) {
969 final Action action = mItems.get(i);
970 if (mKeyguardShowing && !action.showDuringKeyguard()) {
971 continue;
972 }
973 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
974 continue;
975 }
976 if (filteredPos == position) {
977 return action;
978 }
979 filteredPos++;
980 }
981
982 throw new IllegalArgumentException("position " + position
983 + " out of range of showable actions"
984 + ", filtered count=" + getCount()
985 + ", keyguardshowing=" + mKeyguardShowing
986 + ", provisioned=" + mDeviceProvisioned);
987 }
988
989
990 public long getItemId(int position) {
991 return position;
992 }
993
994 public View getView(int position, View convertView, ViewGroup parent) {
995 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400996 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500997 // Everything but screenshot, the last item, gets white background.
998 if (position == getCount() - 1) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500999 MultiListLayout.get(parent).setDivisionView(view);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001000 }
1001 return view;
Jason Monk361915c2017-03-21 20:33:59 -04001002 }
1003 }
1004
1005 // note: the scheme below made more sense when we were planning on having
1006 // 8 different things in the global actions dialog. seems overkill with
1007 // only 3 items now, but may as well keep this flexible approach so it will
1008 // be easy should someone decide at the last minute to include something
1009 // else, such as 'enable wifi', or 'enable bluetooth'
1010
1011 /**
1012 * What each item in the global actions dialog must be able to support.
1013 */
1014 private interface Action {
1015 /**
1016 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -04001017 * for none.
Jason Monk361915c2017-03-21 20:33:59 -04001018 */
1019 CharSequence getLabelForAccessibility(Context context);
1020
1021 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
1022
1023 void onPress();
1024
1025 /**
1026 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -04001027 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -04001028 */
1029 boolean showDuringKeyguard();
1030
1031 /**
1032 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -04001033 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -04001034 */
1035 boolean showBeforeProvisioning();
1036
1037 boolean isEnabled();
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001038
1039 default boolean shouldBeSeparated() {
1040 return false;
1041 }
Jason Monk361915c2017-03-21 20:33:59 -04001042 }
1043
1044 /**
1045 * An action that also supports long press.
1046 */
1047 private interface LongPressAction extends Action {
1048 boolean onLongPress();
1049 }
1050
1051 /**
1052 * A single press action maintains no state, just responds to a press
1053 * and takes an action.
1054 */
1055 private static abstract class SinglePressAction implements Action {
1056 private final int mIconResId;
1057 private final Drawable mIcon;
1058 private final int mMessageResId;
1059 private final CharSequence mMessage;
1060
1061 protected SinglePressAction(int iconResId, int messageResId) {
1062 mIconResId = iconResId;
1063 mMessageResId = messageResId;
1064 mMessage = null;
1065 mIcon = null;
1066 }
1067
1068 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1069 mIconResId = iconResId;
1070 mMessageResId = 0;
1071 mMessage = message;
1072 mIcon = icon;
1073 }
1074
1075 public boolean isEnabled() {
1076 return true;
1077 }
1078
1079 public String getStatus() {
1080 return null;
1081 }
1082
1083 abstract public void onPress();
1084
1085 public CharSequence getLabelForAccessibility(Context context) {
1086 if (mMessage != null) {
1087 return mMessage;
1088 } else {
1089 return context.getString(mMessageResId);
1090 }
1091 }
1092
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001093 protected int getActionLayoutId(Context context) {
1094 if (FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED)) {
1095 return com.android.systemui.R.layout.global_actions_grid_item;
1096 }
1097 return com.android.systemui.R.layout.global_actions_item;
1098 }
1099
Jason Monk361915c2017-03-21 20:33:59 -04001100 public View create(
1101 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001102 View v = inflater.inflate(getActionLayoutId(context), parent,
Jason Monk16fbd9d2017-04-27 14:28:49 -04001103 false);
Jason Monk361915c2017-03-21 20:33:59 -04001104
1105 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1106 TextView messageView = (TextView) v.findViewById(R.id.message);
1107
1108 TextView statusView = (TextView) v.findViewById(R.id.status);
1109 final String status = getStatus();
1110 if (!TextUtils.isEmpty(status)) {
1111 statusView.setText(status);
1112 } else {
1113 statusView.setVisibility(View.GONE);
1114 }
1115 if (mIcon != null) {
1116 icon.setImageDrawable(mIcon);
1117 icon.setScaleType(ScaleType.CENTER_CROP);
1118 } else if (mIconResId != 0) {
1119 icon.setImageDrawable(context.getDrawable(mIconResId));
1120 }
1121 if (mMessage != null) {
1122 messageView.setText(mMessage);
1123 } else {
1124 messageView.setText(mMessageResId);
1125 }
1126
1127 return v;
1128 }
1129 }
1130
1131 /**
1132 * A toggle action knows whether it is on or off, and displays an icon
1133 * and status message accordingly.
1134 */
1135 private static abstract class ToggleAction implements Action {
1136
1137 enum State {
1138 Off(false),
1139 TurningOn(true),
1140 TurningOff(true),
1141 On(false);
1142
1143 private final boolean inTransition;
1144
1145 State(boolean intermediate) {
1146 inTransition = intermediate;
1147 }
1148
1149 public boolean inTransition() {
1150 return inTransition;
1151 }
1152 }
1153
1154 protected State mState = State.Off;
1155
1156 // prefs
1157 protected int mEnabledIconResId;
1158 protected int mDisabledIconResid;
1159 protected int mMessageResId;
1160 protected int mEnabledStatusMessageResId;
1161 protected int mDisabledStatusMessageResId;
1162
1163 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001164 * @param enabledIconResId The icon for when this action is on.
1165 * @param disabledIconResid The icon for when this action is off.
1166 * @param message The general information message, e.g 'Silent Mode'
1167 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001168 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1169 */
1170 public ToggleAction(int enabledIconResId,
1171 int disabledIconResid,
1172 int message,
1173 int enabledStatusMessageResId,
1174 int disabledStatusMessageResId) {
1175 mEnabledIconResId = enabledIconResId;
1176 mDisabledIconResid = disabledIconResid;
1177 mMessageResId = message;
1178 mEnabledStatusMessageResId = enabledStatusMessageResId;
1179 mDisabledStatusMessageResId = disabledStatusMessageResId;
1180 }
1181
1182 /**
1183 * Override to make changes to resource IDs just before creating the
1184 * View.
1185 */
1186 void willCreate() {
1187
1188 }
1189
1190 @Override
1191 public CharSequence getLabelForAccessibility(Context context) {
1192 return context.getString(mMessageResId);
1193 }
1194
1195 public View create(Context context, View convertView, ViewGroup parent,
1196 LayoutInflater inflater) {
1197 willCreate();
1198
1199 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001200 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001201
1202 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1203 TextView messageView = (TextView) v.findViewById(R.id.message);
1204 TextView statusView = (TextView) v.findViewById(R.id.status);
1205 final boolean enabled = isEnabled();
1206
1207 if (messageView != null) {
1208 messageView.setText(mMessageResId);
1209 messageView.setEnabled(enabled);
1210 }
1211
1212 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1213 if (icon != null) {
1214 icon.setImageDrawable(context.getDrawable(
1215 (on ? mEnabledIconResId : mDisabledIconResid)));
1216 icon.setEnabled(enabled);
1217 }
1218
1219 if (statusView != null) {
1220 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1221 statusView.setVisibility(View.VISIBLE);
1222 statusView.setEnabled(enabled);
1223 }
1224 v.setEnabled(enabled);
1225
1226 return v;
1227 }
1228
1229 public final void onPress() {
1230 if (mState.inTransition()) {
1231 Log.w(TAG, "shouldn't be able to toggle when in transition");
1232 return;
1233 }
1234
1235 final boolean nowOn = !(mState == State.On);
1236 onToggle(nowOn);
1237 changeStateFromPress(nowOn);
1238 }
1239
1240 public boolean isEnabled() {
1241 return !mState.inTransition();
1242 }
1243
1244 /**
1245 * Implementations may override this if their state can be in on of the intermediate
1246 * states until some notification is received (e.g airplane mode is 'turning off' until
1247 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001248 *
Jason Monk361915c2017-03-21 20:33:59 -04001249 * @param buttonOn Whether the button was turned on or off
1250 */
1251 protected void changeStateFromPress(boolean buttonOn) {
1252 mState = buttonOn ? State.On : State.Off;
1253 }
1254
1255 abstract void onToggle(boolean on);
1256
1257 public void updateState(State state) {
1258 mState = state;
1259 }
1260 }
1261
1262 private class SilentModeToggleAction extends ToggleAction {
1263 public SilentModeToggleAction() {
1264 super(R.drawable.ic_audio_vol_mute,
1265 R.drawable.ic_audio_vol,
1266 R.string.global_action_toggle_silent_mode,
1267 R.string.global_action_silent_mode_on_status,
1268 R.string.global_action_silent_mode_off_status);
1269 }
1270
1271 void onToggle(boolean on) {
1272 if (on) {
1273 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1274 } else {
1275 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1276 }
1277 }
1278
1279 public boolean showDuringKeyguard() {
1280 return true;
1281 }
1282
1283 public boolean showBeforeProvisioning() {
1284 return false;
1285 }
1286 }
1287
1288 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1289
Jason Monk16fbd9d2017-04-27 14:28:49 -04001290 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001291
1292 private final AudioManager mAudioManager;
1293 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001294
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001295 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001296 mAudioManager = audioManager;
1297 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001298 }
1299
1300 private int ringerModeToIndex(int ringerMode) {
1301 // They just happen to coincide
1302 return ringerMode;
1303 }
1304
1305 private int indexToRingerMode(int index) {
1306 // They just happen to coincide
1307 return index;
1308 }
1309
1310 @Override
1311 public CharSequence getLabelForAccessibility(Context context) {
1312 return null;
1313 }
1314
1315 public View create(Context context, View convertView, ViewGroup parent,
1316 LayoutInflater inflater) {
1317 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1318
1319 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1320 for (int i = 0; i < 3; i++) {
1321 View itemView = v.findViewById(ITEM_IDS[i]);
1322 itemView.setSelected(selectedIndex == i);
1323 // Set up click handler
1324 itemView.setTag(i);
1325 itemView.setOnClickListener(this);
1326 }
1327 return v;
1328 }
1329
1330 public void onPress() {
1331 }
1332
1333 public boolean showDuringKeyguard() {
1334 return true;
1335 }
1336
1337 public boolean showBeforeProvisioning() {
1338 return false;
1339 }
1340
1341 public boolean isEnabled() {
1342 return true;
1343 }
1344
1345 void willCreate() {
1346 }
1347
1348 public void onClick(View v) {
1349 if (!(v.getTag() instanceof Integer)) return;
1350
1351 int index = (Integer) v.getTag();
1352 mAudioManager.setRingerMode(indexToRingerMode(index));
1353 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1354 }
1355 }
1356
1357 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1358 public void onReceive(Context context, Intent intent) {
1359 String action = intent.getAction();
1360 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1361 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1362 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1363 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001364 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001365 }
1366 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1367 // Airplane mode can be changed after ECM exits if airplane toggle button
1368 // is pressed during ECM mode
1369 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1370 mIsWaitingForEcmExit) {
1371 mIsWaitingForEcmExit = false;
1372 changeAirplaneModeSystemSetting(true);
1373 }
1374 }
1375 }
1376 };
1377
1378 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1379 @Override
1380 public void onServiceStateChanged(ServiceState serviceState) {
1381 if (!mHasTelephony) return;
1382 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1383 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1384 mAirplaneModeOn.updateState(mAirplaneState);
1385 mAdapter.notifyDataSetChanged();
1386 }
1387 };
1388
1389 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1390 @Override
1391 public void onReceive(Context context, Intent intent) {
1392 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1393 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1394 }
1395 }
1396 };
1397
1398 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1399 @Override
1400 public void onChange(boolean selfChange) {
1401 onAirplaneModeChanged();
1402 }
1403 };
1404
1405 private static final int MESSAGE_DISMISS = 0;
1406 private static final int MESSAGE_REFRESH = 1;
1407 private static final int MESSAGE_SHOW = 2;
1408 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1409
1410 private Handler mHandler = new Handler() {
1411 public void handleMessage(Message msg) {
1412 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001413 case MESSAGE_DISMISS:
1414 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001415 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1416 mDialog.dismissImmediately();
1417 } else {
1418 mDialog.dismiss();
1419 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001420 mDialog = null;
1421 }
1422 break;
1423 case MESSAGE_REFRESH:
1424 refreshSilentMode();
1425 mAdapter.notifyDataSetChanged();
1426 break;
1427 case MESSAGE_SHOW:
1428 handleShow();
1429 break;
Jason Monk361915c2017-03-21 20:33:59 -04001430 }
1431 }
1432 };
1433
1434 private void onAirplaneModeChanged() {
1435 // Let the service state callbacks handle the state.
1436 if (mHasTelephony) return;
1437
1438 boolean airplaneModeOn = Settings.Global.getInt(
1439 mContext.getContentResolver(),
1440 Settings.Global.AIRPLANE_MODE_ON,
1441 0) == 1;
1442 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1443 mAirplaneModeOn.updateState(mAirplaneState);
1444 }
1445
1446 /**
1447 * Change the airplane mode system setting
1448 */
1449 private void changeAirplaneModeSystemSetting(boolean on) {
1450 Settings.Global.putInt(
1451 mContext.getContentResolver(),
1452 Settings.Global.AIRPLANE_MODE_ON,
1453 on ? 1 : 0);
1454 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1455 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1456 intent.putExtra("state", on);
1457 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1458 if (!mHasTelephony) {
1459 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1460 }
1461 }
1462
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001463 private static final class ActionsDialog extends Dialog implements DialogInterface,
1464 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001465
Jason Monk16fbd9d2017-04-27 14:28:49 -04001466 private final Context mContext;
1467 private final MyAdapter mAdapter;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001468 private final MultiListLayout mGlobalActionsLayout;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001469 private final OnClickListener mClickListener;
1470 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001471 private final GradientDrawable mGradientDrawable;
1472 private final ColorExtractor mColorExtractor;
1473 private boolean mKeyguardShowing;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001474 private boolean mShouldDisplaySeparatedButton;
Beverly526d2d62018-08-15 12:55:33 -04001475 private boolean mShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001476
1477 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001478 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001479 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001480 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001481 mAdapter = adapter;
1482 mClickListener = clickListener;
1483 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001484 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001485 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001486 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001487
1488 // Window initialization
1489 Window window = getWindow();
1490 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001491 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1492 window.getDecorView();
1493 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1494 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1495 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1496 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1497 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001498 window.addFlags(
1499 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001500 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001501 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001502 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1503 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1504 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1505 window.setBackgroundDrawable(mGradientDrawable);
1506 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1507
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001508
1509 setContentView(getGlobalActionsLayoutId(context));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001510 mGlobalActionsLayout = (MultiListLayout)
1511 findViewById(com.android.systemui.R.id.global_actions_view);
1512 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
1513 mGlobalActionsLayout.setHasSeparatedView(mShouldDisplaySeparatedButton);
1514 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001515 @Override
1516 public boolean dispatchPopulateAccessibilityEvent(
1517 View host, AccessibilityEvent event) {
1518 // Populate the title here, just as Activity does
1519 event.getText().add(mContext.getString(R.string.global_actions));
1520 return true;
1521 }
1522 });
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001523 setTitle(R.string.global_actions);
Jason Monk361915c2017-03-21 20:33:59 -04001524 }
1525
Aaron Heuckroth166392f2019-01-17 16:50:59 -05001526 private int getGlobalActionsLayoutId(Context context) {
1527 if (FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.GLOBAL_ACTIONS_GRID_ENABLED)) {
1528 return com.android.systemui.R.layout.global_actions_grid;
1529 }
1530 return com.android.systemui.R.layout.global_actions_wrapped;
1531 }
1532
Jason Monk16fbd9d2017-04-27 14:28:49 -04001533 private void updateList() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001534 mGlobalActionsLayout.removeAllItems();
1535 ArrayList<Action> separatedActions =
1536 mAdapter.getSeparatedActions(mShouldDisplaySeparatedButton);
1537 ArrayList<Action> listActions = mAdapter.getListActions(mShouldDisplaySeparatedButton);
1538 mGlobalActionsLayout.setExpectedListItemCount(listActions.size());
1539 mGlobalActionsLayout.setExpectedSeparatedItemCount(separatedActions.size());
1540
Jason Monk16fbd9d2017-04-27 14:28:49 -04001541 for (int i = 0; i < mAdapter.getCount(); i++) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001542 Action action = mAdapter.getItem(i);
1543 int separatedIndex = separatedActions.indexOf(action);
1544 ViewGroup parent;
1545 if (separatedIndex != -1) {
1546 parent = mGlobalActionsLayout.getParentView(true, separatedIndex);
1547 } else {
1548 int listIndex = listActions.indexOf(action);
1549 parent = mGlobalActionsLayout.getParentView(false, listIndex);
1550 }
1551 View v = mAdapter.getView(i, null, parent);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001552 final int pos = i;
1553 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1554 v.setOnLongClickListener(view ->
1555 mLongClickListener.onItemLongClick(null, v, pos, 0));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001556 parent.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001557 }
Jason Monk361915c2017-03-21 20:33:59 -04001558 }
1559
1560 @Override
1561 protected void onStart() {
1562 super.setCanceledOnTouchOutside(true);
1563 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001564 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001565
1566 Point displaySize = new Point();
1567 mContext.getDisplay().getRealSize(displaySize);
1568 mColorExtractor.addOnColorsChangedListener(this);
1569 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1570 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1571 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001572 updateColors(colors, false /* animate */);
1573 }
1574
1575 /**
1576 * Updates background and system bars according to current GradientColors.
1577 * @param colors Colors and hints to use.
1578 * @param animate Interpolates gradient if true, just sets otherwise.
1579 */
1580 private void updateColors(GradientColors colors, boolean animate) {
1581 mGradientDrawable.setColors(colors, animate);
1582 View decorView = getWindow().getDecorView();
1583 if (colors.supportsDarkText()) {
1584 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1585 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1586 } else {
1587 decorView.setSystemUiVisibility(0);
1588 }
Jason Monk361915c2017-03-21 20:33:59 -04001589 }
1590
1591 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001592 protected void onStop() {
1593 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001594 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001595 }
1596
1597 @Override
1598 public void show() {
1599 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001600 mShowing = true;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001601 mGradientDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001602 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1603 mGlobalActionsLayout.setAlpha(0);
1604 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001605 .alpha(1)
1606 .translationX(0)
1607 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001608 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001609 .setUpdateListener(animation -> {
1610 int alpha = (int) ((Float) animation.getAnimatedValue()
1611 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1612 mGradientDrawable.setAlpha(alpha);
1613 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001614 .start();
1615 }
1616
1617 @Override
1618 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001619 if (!mShowing) {
1620 return;
1621 }
1622 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001623 mGlobalActionsLayout.setTranslationX(0);
1624 mGlobalActionsLayout.setAlpha(1);
1625 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001626 .alpha(0)
1627 .translationX(getAnimTranslation())
1628 .setDuration(300)
1629 .withEndAction(() -> super.dismiss())
1630 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001631 .setUpdateListener(animation -> {
1632 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1633 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1634 mGradientDrawable.setAlpha(alpha);
1635 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001636 .start();
1637 }
1638
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001639 void dismissImmediately() {
1640 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001641 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001642 }
1643
Jason Monk16fbd9d2017-04-27 14:28:49 -04001644 private float getAnimTranslation() {
1645 return getContext().getResources().getDimension(
1646 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001647 }
1648
1649 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001650 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001651 if (mKeyguardShowing) {
1652 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001653 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1654 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001655 }
1656 } else {
1657 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001658 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1659 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001660 }
1661 }
1662 }
1663
1664 public void setKeyguardShowing(boolean keyguardShowing) {
1665 mKeyguardShowing = keyguardShowing;
1666 }
Jason Monk361915c2017-03-21 20:33:59 -04001667 }
1668}