blob: 7b18fad0e1057b36dba454473450632ef166e0c7 [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
1093 public View create(
1094 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001095 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1096 false);
Jason Monk361915c2017-03-21 20:33:59 -04001097
1098 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1099 TextView messageView = (TextView) v.findViewById(R.id.message);
1100
1101 TextView statusView = (TextView) v.findViewById(R.id.status);
1102 final String status = getStatus();
1103 if (!TextUtils.isEmpty(status)) {
1104 statusView.setText(status);
1105 } else {
1106 statusView.setVisibility(View.GONE);
1107 }
1108 if (mIcon != null) {
1109 icon.setImageDrawable(mIcon);
1110 icon.setScaleType(ScaleType.CENTER_CROP);
1111 } else if (mIconResId != 0) {
1112 icon.setImageDrawable(context.getDrawable(mIconResId));
1113 }
1114 if (mMessage != null) {
1115 messageView.setText(mMessage);
1116 } else {
1117 messageView.setText(mMessageResId);
1118 }
1119
1120 return v;
1121 }
1122 }
1123
1124 /**
1125 * A toggle action knows whether it is on or off, and displays an icon
1126 * and status message accordingly.
1127 */
1128 private static abstract class ToggleAction implements Action {
1129
1130 enum State {
1131 Off(false),
1132 TurningOn(true),
1133 TurningOff(true),
1134 On(false);
1135
1136 private final boolean inTransition;
1137
1138 State(boolean intermediate) {
1139 inTransition = intermediate;
1140 }
1141
1142 public boolean inTransition() {
1143 return inTransition;
1144 }
1145 }
1146
1147 protected State mState = State.Off;
1148
1149 // prefs
1150 protected int mEnabledIconResId;
1151 protected int mDisabledIconResid;
1152 protected int mMessageResId;
1153 protected int mEnabledStatusMessageResId;
1154 protected int mDisabledStatusMessageResId;
1155
1156 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001157 * @param enabledIconResId The icon for when this action is on.
1158 * @param disabledIconResid The icon for when this action is off.
1159 * @param message The general information message, e.g 'Silent Mode'
1160 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001161 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1162 */
1163 public ToggleAction(int enabledIconResId,
1164 int disabledIconResid,
1165 int message,
1166 int enabledStatusMessageResId,
1167 int disabledStatusMessageResId) {
1168 mEnabledIconResId = enabledIconResId;
1169 mDisabledIconResid = disabledIconResid;
1170 mMessageResId = message;
1171 mEnabledStatusMessageResId = enabledStatusMessageResId;
1172 mDisabledStatusMessageResId = disabledStatusMessageResId;
1173 }
1174
1175 /**
1176 * Override to make changes to resource IDs just before creating the
1177 * View.
1178 */
1179 void willCreate() {
1180
1181 }
1182
1183 @Override
1184 public CharSequence getLabelForAccessibility(Context context) {
1185 return context.getString(mMessageResId);
1186 }
1187
1188 public View create(Context context, View convertView, ViewGroup parent,
1189 LayoutInflater inflater) {
1190 willCreate();
1191
1192 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001193 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001194
1195 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1196 TextView messageView = (TextView) v.findViewById(R.id.message);
1197 TextView statusView = (TextView) v.findViewById(R.id.status);
1198 final boolean enabled = isEnabled();
1199
1200 if (messageView != null) {
1201 messageView.setText(mMessageResId);
1202 messageView.setEnabled(enabled);
1203 }
1204
1205 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1206 if (icon != null) {
1207 icon.setImageDrawable(context.getDrawable(
1208 (on ? mEnabledIconResId : mDisabledIconResid)));
1209 icon.setEnabled(enabled);
1210 }
1211
1212 if (statusView != null) {
1213 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1214 statusView.setVisibility(View.VISIBLE);
1215 statusView.setEnabled(enabled);
1216 }
1217 v.setEnabled(enabled);
1218
1219 return v;
1220 }
1221
1222 public final void onPress() {
1223 if (mState.inTransition()) {
1224 Log.w(TAG, "shouldn't be able to toggle when in transition");
1225 return;
1226 }
1227
1228 final boolean nowOn = !(mState == State.On);
1229 onToggle(nowOn);
1230 changeStateFromPress(nowOn);
1231 }
1232
1233 public boolean isEnabled() {
1234 return !mState.inTransition();
1235 }
1236
1237 /**
1238 * Implementations may override this if their state can be in on of the intermediate
1239 * states until some notification is received (e.g airplane mode is 'turning off' until
1240 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001241 *
Jason Monk361915c2017-03-21 20:33:59 -04001242 * @param buttonOn Whether the button was turned on or off
1243 */
1244 protected void changeStateFromPress(boolean buttonOn) {
1245 mState = buttonOn ? State.On : State.Off;
1246 }
1247
1248 abstract void onToggle(boolean on);
1249
1250 public void updateState(State state) {
1251 mState = state;
1252 }
1253 }
1254
1255 private class SilentModeToggleAction extends ToggleAction {
1256 public SilentModeToggleAction() {
1257 super(R.drawable.ic_audio_vol_mute,
1258 R.drawable.ic_audio_vol,
1259 R.string.global_action_toggle_silent_mode,
1260 R.string.global_action_silent_mode_on_status,
1261 R.string.global_action_silent_mode_off_status);
1262 }
1263
1264 void onToggle(boolean on) {
1265 if (on) {
1266 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1267 } else {
1268 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1269 }
1270 }
1271
1272 public boolean showDuringKeyguard() {
1273 return true;
1274 }
1275
1276 public boolean showBeforeProvisioning() {
1277 return false;
1278 }
1279 }
1280
1281 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1282
Jason Monk16fbd9d2017-04-27 14:28:49 -04001283 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001284
1285 private final AudioManager mAudioManager;
1286 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001287
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001288 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001289 mAudioManager = audioManager;
1290 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001291 }
1292
1293 private int ringerModeToIndex(int ringerMode) {
1294 // They just happen to coincide
1295 return ringerMode;
1296 }
1297
1298 private int indexToRingerMode(int index) {
1299 // They just happen to coincide
1300 return index;
1301 }
1302
1303 @Override
1304 public CharSequence getLabelForAccessibility(Context context) {
1305 return null;
1306 }
1307
1308 public View create(Context context, View convertView, ViewGroup parent,
1309 LayoutInflater inflater) {
1310 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1311
1312 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1313 for (int i = 0; i < 3; i++) {
1314 View itemView = v.findViewById(ITEM_IDS[i]);
1315 itemView.setSelected(selectedIndex == i);
1316 // Set up click handler
1317 itemView.setTag(i);
1318 itemView.setOnClickListener(this);
1319 }
1320 return v;
1321 }
1322
1323 public void onPress() {
1324 }
1325
1326 public boolean showDuringKeyguard() {
1327 return true;
1328 }
1329
1330 public boolean showBeforeProvisioning() {
1331 return false;
1332 }
1333
1334 public boolean isEnabled() {
1335 return true;
1336 }
1337
1338 void willCreate() {
1339 }
1340
1341 public void onClick(View v) {
1342 if (!(v.getTag() instanceof Integer)) return;
1343
1344 int index = (Integer) v.getTag();
1345 mAudioManager.setRingerMode(indexToRingerMode(index));
1346 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1347 }
1348 }
1349
1350 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1351 public void onReceive(Context context, Intent intent) {
1352 String action = intent.getAction();
1353 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1354 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1355 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1356 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001357 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001358 }
1359 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1360 // Airplane mode can be changed after ECM exits if airplane toggle button
1361 // is pressed during ECM mode
1362 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1363 mIsWaitingForEcmExit) {
1364 mIsWaitingForEcmExit = false;
1365 changeAirplaneModeSystemSetting(true);
1366 }
1367 }
1368 }
1369 };
1370
1371 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1372 @Override
1373 public void onServiceStateChanged(ServiceState serviceState) {
1374 if (!mHasTelephony) return;
1375 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1376 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1377 mAirplaneModeOn.updateState(mAirplaneState);
1378 mAdapter.notifyDataSetChanged();
1379 }
1380 };
1381
1382 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1383 @Override
1384 public void onReceive(Context context, Intent intent) {
1385 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1386 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1387 }
1388 }
1389 };
1390
1391 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1392 @Override
1393 public void onChange(boolean selfChange) {
1394 onAirplaneModeChanged();
1395 }
1396 };
1397
1398 private static final int MESSAGE_DISMISS = 0;
1399 private static final int MESSAGE_REFRESH = 1;
1400 private static final int MESSAGE_SHOW = 2;
1401 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1402
1403 private Handler mHandler = new Handler() {
1404 public void handleMessage(Message msg) {
1405 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001406 case MESSAGE_DISMISS:
1407 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001408 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1409 mDialog.dismissImmediately();
1410 } else {
1411 mDialog.dismiss();
1412 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001413 mDialog = null;
1414 }
1415 break;
1416 case MESSAGE_REFRESH:
1417 refreshSilentMode();
1418 mAdapter.notifyDataSetChanged();
1419 break;
1420 case MESSAGE_SHOW:
1421 handleShow();
1422 break;
Jason Monk361915c2017-03-21 20:33:59 -04001423 }
1424 }
1425 };
1426
1427 private void onAirplaneModeChanged() {
1428 // Let the service state callbacks handle the state.
1429 if (mHasTelephony) return;
1430
1431 boolean airplaneModeOn = Settings.Global.getInt(
1432 mContext.getContentResolver(),
1433 Settings.Global.AIRPLANE_MODE_ON,
1434 0) == 1;
1435 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1436 mAirplaneModeOn.updateState(mAirplaneState);
1437 }
1438
1439 /**
1440 * Change the airplane mode system setting
1441 */
1442 private void changeAirplaneModeSystemSetting(boolean on) {
1443 Settings.Global.putInt(
1444 mContext.getContentResolver(),
1445 Settings.Global.AIRPLANE_MODE_ON,
1446 on ? 1 : 0);
1447 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1448 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1449 intent.putExtra("state", on);
1450 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1451 if (!mHasTelephony) {
1452 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1453 }
1454 }
1455
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001456 private static final class ActionsDialog extends Dialog implements DialogInterface,
1457 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001458
Jason Monk16fbd9d2017-04-27 14:28:49 -04001459 private final Context mContext;
1460 private final MyAdapter mAdapter;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001461 private final MultiListLayout mGlobalActionsLayout;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001462 private final OnClickListener mClickListener;
1463 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001464 private final GradientDrawable mGradientDrawable;
1465 private final ColorExtractor mColorExtractor;
1466 private boolean mKeyguardShowing;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001467 private boolean mShouldDisplaySeparatedButton;
Beverly526d2d62018-08-15 12:55:33 -04001468 private boolean mShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001469
1470 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001471 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001472 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001473 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001474 mAdapter = adapter;
1475 mClickListener = clickListener;
1476 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001477 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001478 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001479 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001480
1481 // Window initialization
1482 Window window = getWindow();
1483 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001484 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1485 window.getDecorView();
1486 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1487 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1488 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1489 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1490 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001491 window.addFlags(
1492 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001493 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001494 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001495 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1496 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1497 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1498 window.setBackgroundDrawable(mGradientDrawable);
1499 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1500
Jason Monk16fbd9d2017-04-27 14:28:49 -04001501 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001502 mGlobalActionsLayout = (MultiListLayout)
1503 findViewById(com.android.systemui.R.id.global_actions_view);
1504 mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
1505 mGlobalActionsLayout.setHasSeparatedView(mShouldDisplaySeparatedButton);
1506 mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
Phil Weaver9054e092018-04-27 16:28:50 -07001507 @Override
1508 public boolean dispatchPopulateAccessibilityEvent(
1509 View host, AccessibilityEvent event) {
1510 // Populate the title here, just as Activity does
1511 event.getText().add(mContext.getString(R.string.global_actions));
1512 return true;
1513 }
1514 });
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001515 setTitle(R.string.global_actions);
Jason Monk361915c2017-03-21 20:33:59 -04001516 }
1517
Jason Monk16fbd9d2017-04-27 14:28:49 -04001518 private void updateList() {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001519 mGlobalActionsLayout.removeAllItems();
1520 ArrayList<Action> separatedActions =
1521 mAdapter.getSeparatedActions(mShouldDisplaySeparatedButton);
1522 ArrayList<Action> listActions = mAdapter.getListActions(mShouldDisplaySeparatedButton);
1523 mGlobalActionsLayout.setExpectedListItemCount(listActions.size());
1524 mGlobalActionsLayout.setExpectedSeparatedItemCount(separatedActions.size());
1525
Jason Monk16fbd9d2017-04-27 14:28:49 -04001526 for (int i = 0; i < mAdapter.getCount(); i++) {
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001527 Action action = mAdapter.getItem(i);
1528 int separatedIndex = separatedActions.indexOf(action);
1529 ViewGroup parent;
1530 if (separatedIndex != -1) {
1531 parent = mGlobalActionsLayout.getParentView(true, separatedIndex);
1532 } else {
1533 int listIndex = listActions.indexOf(action);
1534 parent = mGlobalActionsLayout.getParentView(false, listIndex);
1535 }
1536 View v = mAdapter.getView(i, null, parent);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001537 final int pos = i;
1538 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1539 v.setOnLongClickListener(view ->
1540 mLongClickListener.onItemLongClick(null, v, pos, 0));
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001541 parent.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001542 }
Jason Monk361915c2017-03-21 20:33:59 -04001543 }
1544
1545 @Override
1546 protected void onStart() {
1547 super.setCanceledOnTouchOutside(true);
1548 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001549 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001550
1551 Point displaySize = new Point();
1552 mContext.getDisplay().getRealSize(displaySize);
1553 mColorExtractor.addOnColorsChangedListener(this);
1554 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1555 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1556 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001557 updateColors(colors, false /* animate */);
1558 }
1559
1560 /**
1561 * Updates background and system bars according to current GradientColors.
1562 * @param colors Colors and hints to use.
1563 * @param animate Interpolates gradient if true, just sets otherwise.
1564 */
1565 private void updateColors(GradientColors colors, boolean animate) {
1566 mGradientDrawable.setColors(colors, animate);
1567 View decorView = getWindow().getDecorView();
1568 if (colors.supportsDarkText()) {
1569 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1570 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1571 } else {
1572 decorView.setSystemUiVisibility(0);
1573 }
Jason Monk361915c2017-03-21 20:33:59 -04001574 }
1575
1576 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001577 protected void onStop() {
1578 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001579 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001580 }
1581
1582 @Override
1583 public void show() {
1584 super.show();
Beverly526d2d62018-08-15 12:55:33 -04001585 mShowing = true;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001586 mGradientDrawable.setAlpha(0);
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001587 mGlobalActionsLayout.setTranslationX(getAnimTranslation());
1588 mGlobalActionsLayout.setAlpha(0);
1589 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001590 .alpha(1)
1591 .translationX(0)
1592 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001593 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001594 .setUpdateListener(animation -> {
1595 int alpha = (int) ((Float) animation.getAnimatedValue()
1596 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1597 mGradientDrawable.setAlpha(alpha);
1598 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001599 .start();
1600 }
1601
1602 @Override
1603 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001604 if (!mShowing) {
1605 return;
1606 }
1607 mShowing = false;
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001608 mGlobalActionsLayout.setTranslationX(0);
1609 mGlobalActionsLayout.setAlpha(1);
1610 mGlobalActionsLayout.animate()
Jason Monk16fbd9d2017-04-27 14:28:49 -04001611 .alpha(0)
1612 .translationX(getAnimTranslation())
1613 .setDuration(300)
1614 .withEndAction(() -> super.dismiss())
1615 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001616 .setUpdateListener(animation -> {
1617 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1618 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1619 mGradientDrawable.setAlpha(alpha);
1620 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001621 .start();
1622 }
1623
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001624 void dismissImmediately() {
1625 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001626 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001627 }
1628
Jason Monk16fbd9d2017-04-27 14:28:49 -04001629 private float getAnimTranslation() {
1630 return getContext().getResources().getDimension(
1631 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001632 }
1633
1634 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001635 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001636 if (mKeyguardShowing) {
1637 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001638 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1639 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001640 }
1641 } else {
1642 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001643 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1644 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001645 }
1646 }
1647 }
1648
1649 public void setKeyguardShowing(boolean keyguardShowing) {
1650 mKeyguardShowing = keyguardShowing;
1651 }
Jason Monk361915c2017-03-21 20:33:59 -04001652 }
1653}