blob: c3f51f5155bacdf772f65c51e070189486bcfd75 [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
17import com.android.internal.R;
Lucas Dupine2292a92017-07-06 14:35:30 -070018import com.android.internal.colorextraction.ColorExtractor;
19import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Jason Monk361915c2017-03-21 20:33:59 -040020import com.android.internal.logging.MetricsLogger;
21import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
22import com.android.internal.util.EmergencyAffordanceManager;
23import com.android.internal.telephony.TelephonyIntents;
24import com.android.internal.telephony.TelephonyProperties;
25import com.android.internal.widget.LockPatternUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070026import com.android.systemui.Dependency;
Jason Monk16fbd9d2017-04-27 14:28:49 -040027import com.android.systemui.HardwareUiLayout;
Lucas Dupinde9db422017-07-19 17:15:41 -070028import com.android.systemui.Interpolators;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070029import com.android.systemui.colorextraction.SysuiColorExtractor;
Jason Monk361915c2017-03-21 20:33:59 -040030import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Lucas Dupinde9db422017-07-19 17:15:41 -070031import com.android.systemui.statusbar.notification.NotificationUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070032import com.android.systemui.statusbar.phone.ScrimController;
Jason Monk16fbd9d2017-04-27 14:28:49 -040033import com.android.systemui.volume.VolumeDialogMotion.LogAccelerateInterpolator;
34import com.android.systemui.volume.VolumeDialogMotion.LogDecelerateInterpolator;
Jason Monk361915c2017-03-21 20:33:59 -040035
Jason Monk4e04e492017-07-11 11:07:08 -040036import android.animation.ValueAnimator;
37import android.animation.ValueAnimator.AnimatorUpdateListener;
Jason Monk361915c2017-03-21 20:33:59 -040038import android.app.ActivityManager;
39import android.app.Dialog;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070040import android.app.WallpaperManager;
Jason Monk361915c2017-03-21 20:33:59 -040041import android.content.BroadcastReceiver;
42import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
45import android.content.IntentFilter;
46import android.content.pm.UserInfo;
47import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070048import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040049import android.graphics.drawable.Drawable;
50import android.media.AudioManager;
51import android.net.ConnectivityManager;
52import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040053import android.os.Handler;
54import android.os.Message;
55import android.os.RemoteException;
56import android.os.ServiceManager;
57import android.os.SystemProperties;
58import android.os.UserHandle;
59import android.os.UserManager;
60import android.os.Vibrator;
61import android.provider.Settings;
62import android.service.dreams.DreamService;
63import android.service.dreams.IDreamManager;
64import android.telephony.PhoneStateListener;
65import android.telephony.ServiceState;
66import android.telephony.TelephonyManager;
67import android.text.TextUtils;
68import android.util.ArraySet;
69import android.util.Log;
Lucas Dupinde9db422017-07-19 17:15:41 -070070import android.util.MathUtils;
Lucas Dupin448786c2017-07-24 17:44:25 -070071import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040072import android.view.LayoutInflater;
73import android.view.View;
74import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070075import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040076import android.view.WindowManager;
77import android.view.WindowManagerGlobal;
78import android.view.accessibility.AccessibilityEvent;
79import android.widget.AdapterView;
Jason Monk16fbd9d2017-04-27 14:28:49 -040080import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040081import android.widget.BaseAdapter;
82import android.widget.ImageView;
83import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040084import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040085import android.widget.TextView;
86
Lucas Dupine2292a92017-07-06 14:35:30 -070087import com.android.internal.colorextraction.drawable.GradientDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070088
Jason Monk361915c2017-03-21 20:33:59 -040089import java.util.ArrayList;
90import java.util.List;
91
92/**
93 * Helper to show the global actions dialog. Each item is an {@link Action} that
94 * may show depending on whether the keyguard is showing, and whether the device
95 * is provisioned.
96 */
Jason Monk16fbd9d2017-04-27 14:28:49 -040097class GlobalActionsDialog implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -040098
99 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
100 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
101
102 private static final String TAG = "GlobalActionsDialog";
103
104 private static final boolean SHOW_SILENT_TOGGLE = true;
105
106 /* Valid settings for global actions keys.
107 * see config.xml config_globalActionList */
108 private static final String GLOBAL_ACTION_KEY_POWER = "power";
109 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
110 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
111 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
112 private static final String GLOBAL_ACTION_KEY_USERS = "users";
113 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
114 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
115 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
116 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
117 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
118
Lucas Dupinde9db422017-07-19 17:15:41 -0700119 private static final float SHUTDOWN_SCRIM_ALPHA = 0.95f;
120
Jason Monk361915c2017-03-21 20:33:59 -0400121 private final Context mContext;
122 private final GlobalActionsManager mWindowManagerFuncs;
123 private final AudioManager mAudioManager;
124 private final IDreamManager mDreamManager;
125
126 private ArrayList<Action> mItems;
127 private ActionsDialog mDialog;
128
129 private Action mSilentModeAction;
130 private ToggleAction mAirplaneModeOn;
131
132 private MyAdapter mAdapter;
133
134 private boolean mKeyguardShowing = false;
135 private boolean mDeviceProvisioned = false;
136 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
137 private boolean mIsWaitingForEcmExit = false;
138 private boolean mHasTelephony;
139 private boolean mHasVibrator;
140 private final boolean mShowSilentToggle;
141 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
142
143 /**
144 * @param context everything needs a context :(
145 */
146 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700147 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400148 mWindowManagerFuncs = windowManagerFuncs;
149 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
150 mDreamManager = IDreamManager.Stub.asInterface(
151 ServiceManager.getService(DreamService.DREAM_SERVICE));
152
153 // receive broadcasts
154 IntentFilter filter = new IntentFilter();
155 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
156 filter.addAction(Intent.ACTION_SCREEN_OFF);
157 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
158 context.registerReceiver(mBroadcastReceiver, filter);
159
160 ConnectivityManager cm = (ConnectivityManager)
161 context.getSystemService(Context.CONNECTIVITY_SERVICE);
162 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
163
164 // get notified of phone state changes
165 TelephonyManager telephonyManager =
166 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
167 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
168 mContext.getContentResolver().registerContentObserver(
169 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
170 mAirplaneModeObserver);
171 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
172 mHasVibrator = vibrator != null && vibrator.hasVibrator();
173
174 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
175 R.bool.config_useFixedVolume);
176
177 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
178 }
179
180 /**
181 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400182 *
Jason Monk361915c2017-03-21 20:33:59 -0400183 * @param keyguardShowing True if keyguard is showing
184 */
185 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
186 mKeyguardShowing = keyguardShowing;
187 mDeviceProvisioned = isDeviceProvisioned;
188 if (mDialog != null) {
189 mDialog.dismiss();
190 mDialog = null;
191 // Show delayed, so that the dismiss of the previous dialog completes
192 mHandler.sendEmptyMessage(MESSAGE_SHOW);
193 } else {
194 handleShow();
195 }
196 }
197
198 private void awakenIfNecessary() {
199 if (mDreamManager != null) {
200 try {
201 if (mDreamManager.isDreaming()) {
202 mDreamManager.awaken();
203 }
204 } catch (RemoteException e) {
205 // we tried
206 }
207 }
208 }
209
210 private void handleShow() {
211 awakenIfNecessary();
212 mDialog = createDialog();
213 prepareDialog();
214
215 // If we only have 1 item and it's a simple press action, just do this action.
216 if (mAdapter.getCount() == 1
217 && mAdapter.getItem(0) instanceof SinglePressAction
218 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
219 ((SinglePressAction) mAdapter.getItem(0)).onPress();
220 } else {
221 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
222 attrs.setTitle("ActionsDialog");
223 mDialog.getWindow().setAttributes(attrs);
224 mDialog.show();
225 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400226 }
227 }
228
229 /**
230 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400231 *
Jason Monk361915c2017-03-21 20:33:59 -0400232 * @return A new dialog.
233 */
234 private ActionsDialog createDialog() {
235 // Simple toggle style if there's no vibrator, otherwise use a tri-state
236 if (!mHasVibrator) {
237 mSilentModeAction = new SilentModeToggleAction();
238 } else {
239 mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler);
240 }
241 mAirplaneModeOn = new ToggleAction(
242 R.drawable.ic_lock_airplane_mode,
243 R.drawable.ic_lock_airplane_mode_off,
244 R.string.global_actions_toggle_airplane_mode,
245 R.string.global_actions_airplane_mode_on_status,
246 R.string.global_actions_airplane_mode_off_status) {
247
248 void onToggle(boolean on) {
249 if (mHasTelephony && Boolean.parseBoolean(
250 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
251 mIsWaitingForEcmExit = true;
252 // Launch ECM exit dialog
253 Intent ecmDialogIntent =
254 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
255 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
256 mContext.startActivity(ecmDialogIntent);
257 } else {
258 changeAirplaneModeSystemSetting(on);
259 }
260 }
261
262 @Override
263 protected void changeStateFromPress(boolean buttonOn) {
264 if (!mHasTelephony) return;
265
266 // In ECM mode airplane state cannot be changed
267 if (!(Boolean.parseBoolean(
268 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
269 mState = buttonOn ? State.TurningOn : State.TurningOff;
270 mAirplaneState = mState;
271 }
272 }
273
274 public boolean showDuringKeyguard() {
275 return true;
276 }
277
278 public boolean showBeforeProvisioning() {
279 return false;
280 }
281 };
282 onAirplaneModeChanged();
283
284 mItems = new ArrayList<Action>();
285 String[] defaultActions = mContext.getResources().getStringArray(
286 R.array.config_globalActionsList);
287
288 ArraySet<String> addedKeys = new ArraySet<String>();
289 for (int i = 0; i < defaultActions.length; i++) {
290 String actionKey = defaultActions[i];
291 if (addedKeys.contains(actionKey)) {
292 // If we already have added this, don't add it again.
293 continue;
294 }
295 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
296 mItems.add(new PowerAction());
297 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
298 mItems.add(mAirplaneModeOn);
299 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
300 if (Settings.Global.getInt(mContext.getContentResolver(),
301 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
302 mItems.add(new BugReportAction());
303 }
304 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
305 if (mShowSilentToggle) {
306 mItems.add(mSilentModeAction);
307 }
308 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
309 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
310 addUsersToMenu(mItems);
311 }
312 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
313 mItems.add(getSettingsAction());
314 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
315 mItems.add(getLockdownAction());
316 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
317 mItems.add(getVoiceAssistAction());
318 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
319 mItems.add(getAssistAction());
320 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
321 mItems.add(new RestartAction());
322 } else {
323 Log.e(TAG, "Invalid global action key " + actionKey);
324 }
325 // Add here so we don't add more than one.
326 addedKeys.add(actionKey);
327 }
328
329 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
330 mItems.add(getEmergencyAction());
331 }
332
333 mAdapter = new MyAdapter();
334
Jason Monk16fbd9d2017-04-27 14:28:49 -0400335 OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener() {
336 @Override
337 public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
338 long id) {
339 final Action action = mAdapter.getItem(position);
340 if (action instanceof LongPressAction) {
Shunta Satobdb0e492017-04-14 16:01:54 +0900341 mDialog.dismiss();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400342 return ((LongPressAction) action).onLongPress();
343 }
344 return false;
345 }
346 };
347 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener);
Jason Monk361915c2017-03-21 20:33:59 -0400348 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700349 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400350
351 dialog.setOnDismissListener(this);
352
353 return dialog;
354 }
355
356 private final class PowerAction extends SinglePressAction implements LongPressAction {
357 private PowerAction() {
358 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400359 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400360 }
361
362 @Override
363 public boolean onLongPress() {
364 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
365 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
366 mWindowManagerFuncs.reboot(true);
367 return true;
368 }
369 return false;
370 }
371
372 @Override
373 public boolean showDuringKeyguard() {
374 return true;
375 }
376
377 @Override
378 public boolean showBeforeProvisioning() {
379 return true;
380 }
381
382 @Override
383 public void onPress() {
384 // shutdown by making sure radio and power are handled accordingly.
385 mWindowManagerFuncs.shutdown();
386 }
387 }
388
389 private final class RestartAction extends SinglePressAction implements LongPressAction {
390 private RestartAction() {
391 super(R.drawable.ic_restart, R.string.global_action_restart);
392 }
393
394 @Override
395 public boolean onLongPress() {
396 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
397 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
398 mWindowManagerFuncs.reboot(true);
399 return true;
400 }
401 return false;
402 }
403
404 @Override
405 public boolean showDuringKeyguard() {
406 return true;
407 }
408
409 @Override
410 public boolean showBeforeProvisioning() {
411 return true;
412 }
413
414 @Override
415 public void onPress() {
416 mWindowManagerFuncs.reboot(false);
417 }
418 }
419
420
421 private class BugReportAction extends SinglePressAction implements LongPressAction {
422
423 public BugReportAction() {
424 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
425 }
426
427 @Override
428 public void onPress() {
429 // don't actually trigger the bugreport if we are running stability
430 // tests via monkey
431 if (ActivityManager.isUserAMonkey()) {
432 return;
433 }
434 // Add a little delay before executing, to give the
435 // dialog a chance to go away before it takes a
436 // screenshot.
437 mHandler.postDelayed(new Runnable() {
438 @Override
439 public void run() {
440 try {
441 // Take an "interactive" bugreport.
442 MetricsLogger.action(mContext,
443 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
444 ActivityManager.getService().requestBugReport(
445 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
446 } catch (RemoteException e) {
447 }
448 }
449 }, 500);
450 }
451
452 @Override
453 public boolean onLongPress() {
454 // don't actually trigger the bugreport if we are running stability
455 // tests via monkey
456 if (ActivityManager.isUserAMonkey()) {
457 return false;
458 }
459 try {
460 // Take a "full" bugreport.
461 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
462 ActivityManager.getService().requestBugReport(
463 ActivityManager.BUGREPORT_OPTION_FULL);
464 } catch (RemoteException e) {
465 }
466 return false;
467 }
468
469 public boolean showDuringKeyguard() {
470 return true;
471 }
472
473 @Override
474 public boolean showBeforeProvisioning() {
475 return false;
476 }
477
478 @Override
479 public String getStatus() {
480 return mContext.getString(
481 R.string.bugreport_status,
482 Build.VERSION.RELEASE,
483 Build.ID);
484 }
485 }
486
487 private Action getSettingsAction() {
488 return new SinglePressAction(R.drawable.ic_settings,
489 R.string.global_action_settings) {
490
491 @Override
492 public void onPress() {
493 Intent intent = new Intent(Settings.ACTION_SETTINGS);
494 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
495 mContext.startActivity(intent);
496 }
497
498 @Override
499 public boolean showDuringKeyguard() {
500 return true;
501 }
502
503 @Override
504 public boolean showBeforeProvisioning() {
505 return true;
506 }
507 };
508 }
509
510 private Action getEmergencyAction() {
511 return new SinglePressAction(R.drawable.emergency_icon,
512 R.string.global_action_emergency) {
513 @Override
514 public void onPress() {
515 mEmergencyAffordanceManager.performEmergencyCall();
516 }
517
518 @Override
519 public boolean showDuringKeyguard() {
520 return true;
521 }
522
523 @Override
524 public boolean showBeforeProvisioning() {
525 return true;
526 }
527 };
528 }
529
530 private Action getAssistAction() {
531 return new SinglePressAction(R.drawable.ic_action_assist_focused,
532 R.string.global_action_assist) {
533 @Override
534 public void onPress() {
535 Intent intent = new Intent(Intent.ACTION_ASSIST);
536 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
537 mContext.startActivity(intent);
538 }
539
540 @Override
541 public boolean showDuringKeyguard() {
542 return true;
543 }
544
545 @Override
546 public boolean showBeforeProvisioning() {
547 return true;
548 }
549 };
550 }
551
552 private Action getVoiceAssistAction() {
553 return new SinglePressAction(R.drawable.ic_voice_search,
554 R.string.global_action_voice_assist) {
555 @Override
556 public void onPress() {
557 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
558 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
559 mContext.startActivity(intent);
560 }
561
562 @Override
563 public boolean showDuringKeyguard() {
564 return true;
565 }
566
567 @Override
568 public boolean showBeforeProvisioning() {
569 return true;
570 }
571 };
572 }
573
574 private Action getLockdownAction() {
575 return new SinglePressAction(R.drawable.ic_lock_lock,
576 R.string.global_action_lockdown) {
577
578 @Override
579 public void onPress() {
580 new LockPatternUtils(mContext).requireCredentialEntry(UserHandle.USER_ALL);
581 try {
582 WindowManagerGlobal.getWindowManagerService().lockNow(null);
583 } catch (RemoteException e) {
584 Log.e(TAG, "Error while trying to lock device.", e);
585 }
586 }
587
588 @Override
589 public boolean showDuringKeyguard() {
590 return true;
591 }
592
593 @Override
594 public boolean showBeforeProvisioning() {
595 return false;
596 }
597 };
598 }
599
600 private UserInfo getCurrentUser() {
601 try {
602 return ActivityManager.getService().getCurrentUser();
603 } catch (RemoteException re) {
604 return null;
605 }
606 }
607
608 private boolean isCurrentUserOwner() {
609 UserInfo currentUser = getCurrentUser();
610 return currentUser == null || currentUser.isPrimary();
611 }
612
613 private void addUsersToMenu(ArrayList<Action> items) {
614 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
615 if (um.isUserSwitcherEnabled()) {
616 List<UserInfo> users = um.getUsers();
617 UserInfo currentUser = getCurrentUser();
618 for (final UserInfo user : users) {
619 if (user.supportsSwitchToByUser()) {
620 boolean isCurrentUser = currentUser == null
621 ? user.id == 0 : (currentUser.id == user.id);
622 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
623 : null;
624 SinglePressAction switchToUser = new SinglePressAction(
625 R.drawable.ic_menu_cc, icon,
626 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400627 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400628 public void onPress() {
629 try {
630 ActivityManager.getService().switchUser(user.id);
631 } catch (RemoteException re) {
632 Log.e(TAG, "Couldn't switch user " + re);
633 }
634 }
635
636 public boolean showDuringKeyguard() {
637 return true;
638 }
639
640 public boolean showBeforeProvisioning() {
641 return false;
642 }
643 };
644 items.add(switchToUser);
645 }
646 }
647 }
648 }
649
650 private void prepareDialog() {
651 refreshSilentMode();
652 mAirplaneModeOn.updateState(mAirplaneState);
653 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400654 if (mShowSilentToggle) {
655 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
656 mContext.registerReceiver(mRingerModeReceiver, filter);
657 }
658 }
659
660 private void refreshSilentMode() {
661 if (!mHasVibrator) {
662 final boolean silentModeOn =
663 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400664 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400665 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
666 }
667 }
668
669 /** {@inheritDoc} */
670 public void onDismiss(DialogInterface dialog) {
671 mWindowManagerFuncs.onGlobalActionsHidden();
672 if (mShowSilentToggle) {
673 try {
674 mContext.unregisterReceiver(mRingerModeReceiver);
675 } catch (IllegalArgumentException ie) {
676 // ignore this
677 Log.w(TAG, ie);
678 }
679 }
680 }
681
682 /** {@inheritDoc} */
683 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400684 Action item = mAdapter.getItem(which);
685 if ((item instanceof PowerAction)
686 || (item instanceof RestartAction)) {
687 if (mDialog != null) mDialog.fadeOut();
688 } else if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400689 dialog.dismiss();
690 }
Jason Monkfd279662017-06-29 19:37:48 -0400691 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400692 }
693
694 /**
695 * The adapter used for the list within the global actions dialog, taking
696 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400697 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
698 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400699 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
700 */
701 private class MyAdapter extends BaseAdapter {
702
703 public int getCount() {
704 int count = 0;
705
706 for (int i = 0; i < mItems.size(); i++) {
707 final Action action = mItems.get(i);
708
709 if (mKeyguardShowing && !action.showDuringKeyguard()) {
710 continue;
711 }
712 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
713 continue;
714 }
715 count++;
716 }
717 return count;
718 }
719
720 @Override
721 public boolean isEnabled(int position) {
722 return getItem(position).isEnabled();
723 }
724
725 @Override
726 public boolean areAllItemsEnabled() {
727 return false;
728 }
729
730 public Action getItem(int position) {
731
732 int filteredPos = 0;
733 for (int i = 0; i < mItems.size(); i++) {
734 final Action action = mItems.get(i);
735 if (mKeyguardShowing && !action.showDuringKeyguard()) {
736 continue;
737 }
738 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
739 continue;
740 }
741 if (filteredPos == position) {
742 return action;
743 }
744 filteredPos++;
745 }
746
747 throw new IllegalArgumentException("position " + position
748 + " out of range of showable actions"
749 + ", filtered count=" + getCount()
750 + ", keyguardshowing=" + mKeyguardShowing
751 + ", provisioned=" + mDeviceProvisioned);
752 }
753
754
755 public long getItemId(int position) {
756 return position;
757 }
758
759 public View getView(int position, View convertView, ViewGroup parent) {
760 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400761 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
762 if (position == 2) {
763 HardwareUiLayout.get(parent).setDivisionView(view);
764 }
765 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400766 }
767 }
768
769 // note: the scheme below made more sense when we were planning on having
770 // 8 different things in the global actions dialog. seems overkill with
771 // only 3 items now, but may as well keep this flexible approach so it will
772 // be easy should someone decide at the last minute to include something
773 // else, such as 'enable wifi', or 'enable bluetooth'
774
775 /**
776 * What each item in the global actions dialog must be able to support.
777 */
778 private interface Action {
779 /**
780 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400781 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400782 */
783 CharSequence getLabelForAccessibility(Context context);
784
785 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
786
787 void onPress();
788
789 /**
790 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400791 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400792 */
793 boolean showDuringKeyguard();
794
795 /**
796 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400797 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400798 */
799 boolean showBeforeProvisioning();
800
801 boolean isEnabled();
802 }
803
804 /**
805 * An action that also supports long press.
806 */
807 private interface LongPressAction extends Action {
808 boolean onLongPress();
809 }
810
811 /**
812 * A single press action maintains no state, just responds to a press
813 * and takes an action.
814 */
815 private static abstract class SinglePressAction implements Action {
816 private final int mIconResId;
817 private final Drawable mIcon;
818 private final int mMessageResId;
819 private final CharSequence mMessage;
820
821 protected SinglePressAction(int iconResId, int messageResId) {
822 mIconResId = iconResId;
823 mMessageResId = messageResId;
824 mMessage = null;
825 mIcon = null;
826 }
827
828 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
829 mIconResId = iconResId;
830 mMessageResId = 0;
831 mMessage = message;
832 mIcon = icon;
833 }
834
835 public boolean isEnabled() {
836 return true;
837 }
838
839 public String getStatus() {
840 return null;
841 }
842
843 abstract public void onPress();
844
845 public CharSequence getLabelForAccessibility(Context context) {
846 if (mMessage != null) {
847 return mMessage;
848 } else {
849 return context.getString(mMessageResId);
850 }
851 }
852
853 public View create(
854 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400855 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
856 false);
Jason Monk361915c2017-03-21 20:33:59 -0400857
858 ImageView icon = (ImageView) v.findViewById(R.id.icon);
859 TextView messageView = (TextView) v.findViewById(R.id.message);
860
861 TextView statusView = (TextView) v.findViewById(R.id.status);
862 final String status = getStatus();
863 if (!TextUtils.isEmpty(status)) {
864 statusView.setText(status);
865 } else {
866 statusView.setVisibility(View.GONE);
867 }
868 if (mIcon != null) {
869 icon.setImageDrawable(mIcon);
870 icon.setScaleType(ScaleType.CENTER_CROP);
871 } else if (mIconResId != 0) {
872 icon.setImageDrawable(context.getDrawable(mIconResId));
873 }
874 if (mMessage != null) {
875 messageView.setText(mMessage);
876 } else {
877 messageView.setText(mMessageResId);
878 }
879
880 return v;
881 }
882 }
883
884 /**
885 * A toggle action knows whether it is on or off, and displays an icon
886 * and status message accordingly.
887 */
888 private static abstract class ToggleAction implements Action {
889
890 enum State {
891 Off(false),
892 TurningOn(true),
893 TurningOff(true),
894 On(false);
895
896 private final boolean inTransition;
897
898 State(boolean intermediate) {
899 inTransition = intermediate;
900 }
901
902 public boolean inTransition() {
903 return inTransition;
904 }
905 }
906
907 protected State mState = State.Off;
908
909 // prefs
910 protected int mEnabledIconResId;
911 protected int mDisabledIconResid;
912 protected int mMessageResId;
913 protected int mEnabledStatusMessageResId;
914 protected int mDisabledStatusMessageResId;
915
916 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -0400917 * @param enabledIconResId The icon for when this action is on.
918 * @param disabledIconResid The icon for when this action is off.
919 * @param message The general information message, e.g 'Silent Mode'
920 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -0400921 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
922 */
923 public ToggleAction(int enabledIconResId,
924 int disabledIconResid,
925 int message,
926 int enabledStatusMessageResId,
927 int disabledStatusMessageResId) {
928 mEnabledIconResId = enabledIconResId;
929 mDisabledIconResid = disabledIconResid;
930 mMessageResId = message;
931 mEnabledStatusMessageResId = enabledStatusMessageResId;
932 mDisabledStatusMessageResId = disabledStatusMessageResId;
933 }
934
935 /**
936 * Override to make changes to resource IDs just before creating the
937 * View.
938 */
939 void willCreate() {
940
941 }
942
943 @Override
944 public CharSequence getLabelForAccessibility(Context context) {
945 return context.getString(mMessageResId);
946 }
947
948 public View create(Context context, View convertView, ViewGroup parent,
949 LayoutInflater inflater) {
950 willCreate();
951
952 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -0400953 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -0400954
955 ImageView icon = (ImageView) v.findViewById(R.id.icon);
956 TextView messageView = (TextView) v.findViewById(R.id.message);
957 TextView statusView = (TextView) v.findViewById(R.id.status);
958 final boolean enabled = isEnabled();
959
960 if (messageView != null) {
961 messageView.setText(mMessageResId);
962 messageView.setEnabled(enabled);
963 }
964
965 boolean on = ((mState == State.On) || (mState == State.TurningOn));
966 if (icon != null) {
967 icon.setImageDrawable(context.getDrawable(
968 (on ? mEnabledIconResId : mDisabledIconResid)));
969 icon.setEnabled(enabled);
970 }
971
972 if (statusView != null) {
973 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
974 statusView.setVisibility(View.VISIBLE);
975 statusView.setEnabled(enabled);
976 }
977 v.setEnabled(enabled);
978
979 return v;
980 }
981
982 public final void onPress() {
983 if (mState.inTransition()) {
984 Log.w(TAG, "shouldn't be able to toggle when in transition");
985 return;
986 }
987
988 final boolean nowOn = !(mState == State.On);
989 onToggle(nowOn);
990 changeStateFromPress(nowOn);
991 }
992
993 public boolean isEnabled() {
994 return !mState.inTransition();
995 }
996
997 /**
998 * Implementations may override this if their state can be in on of the intermediate
999 * states until some notification is received (e.g airplane mode is 'turning off' until
1000 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001001 *
Jason Monk361915c2017-03-21 20:33:59 -04001002 * @param buttonOn Whether the button was turned on or off
1003 */
1004 protected void changeStateFromPress(boolean buttonOn) {
1005 mState = buttonOn ? State.On : State.Off;
1006 }
1007
1008 abstract void onToggle(boolean on);
1009
1010 public void updateState(State state) {
1011 mState = state;
1012 }
1013 }
1014
1015 private class SilentModeToggleAction extends ToggleAction {
1016 public SilentModeToggleAction() {
1017 super(R.drawable.ic_audio_vol_mute,
1018 R.drawable.ic_audio_vol,
1019 R.string.global_action_toggle_silent_mode,
1020 R.string.global_action_silent_mode_on_status,
1021 R.string.global_action_silent_mode_off_status);
1022 }
1023
1024 void onToggle(boolean on) {
1025 if (on) {
1026 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1027 } else {
1028 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1029 }
1030 }
1031
1032 public boolean showDuringKeyguard() {
1033 return true;
1034 }
1035
1036 public boolean showBeforeProvisioning() {
1037 return false;
1038 }
1039 }
1040
1041 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1042
Jason Monk16fbd9d2017-04-27 14:28:49 -04001043 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001044
1045 private final AudioManager mAudioManager;
1046 private final Handler mHandler;
1047 private final Context mContext;
1048
1049 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1050 mAudioManager = audioManager;
1051 mHandler = handler;
1052 mContext = context;
1053 }
1054
1055 private int ringerModeToIndex(int ringerMode) {
1056 // They just happen to coincide
1057 return ringerMode;
1058 }
1059
1060 private int indexToRingerMode(int index) {
1061 // They just happen to coincide
1062 return index;
1063 }
1064
1065 @Override
1066 public CharSequence getLabelForAccessibility(Context context) {
1067 return null;
1068 }
1069
1070 public View create(Context context, View convertView, ViewGroup parent,
1071 LayoutInflater inflater) {
1072 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1073
1074 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1075 for (int i = 0; i < 3; i++) {
1076 View itemView = v.findViewById(ITEM_IDS[i]);
1077 itemView.setSelected(selectedIndex == i);
1078 // Set up click handler
1079 itemView.setTag(i);
1080 itemView.setOnClickListener(this);
1081 }
1082 return v;
1083 }
1084
1085 public void onPress() {
1086 }
1087
1088 public boolean showDuringKeyguard() {
1089 return true;
1090 }
1091
1092 public boolean showBeforeProvisioning() {
1093 return false;
1094 }
1095
1096 public boolean isEnabled() {
1097 return true;
1098 }
1099
1100 void willCreate() {
1101 }
1102
1103 public void onClick(View v) {
1104 if (!(v.getTag() instanceof Integer)) return;
1105
1106 int index = (Integer) v.getTag();
1107 mAudioManager.setRingerMode(indexToRingerMode(index));
1108 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1109 }
1110 }
1111
1112 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1113 public void onReceive(Context context, Intent intent) {
1114 String action = intent.getAction();
1115 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1116 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1117 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1118 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
1119 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
1120 }
1121 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1122 // Airplane mode can be changed after ECM exits if airplane toggle button
1123 // is pressed during ECM mode
1124 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1125 mIsWaitingForEcmExit) {
1126 mIsWaitingForEcmExit = false;
1127 changeAirplaneModeSystemSetting(true);
1128 }
1129 }
1130 }
1131 };
1132
1133 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1134 @Override
1135 public void onServiceStateChanged(ServiceState serviceState) {
1136 if (!mHasTelephony) return;
1137 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1138 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1139 mAirplaneModeOn.updateState(mAirplaneState);
1140 mAdapter.notifyDataSetChanged();
1141 }
1142 };
1143
1144 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1145 @Override
1146 public void onReceive(Context context, Intent intent) {
1147 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1148 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1149 }
1150 }
1151 };
1152
1153 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1154 @Override
1155 public void onChange(boolean selfChange) {
1156 onAirplaneModeChanged();
1157 }
1158 };
1159
1160 private static final int MESSAGE_DISMISS = 0;
1161 private static final int MESSAGE_REFRESH = 1;
1162 private static final int MESSAGE_SHOW = 2;
1163 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1164
1165 private Handler mHandler = new Handler() {
1166 public void handleMessage(Message msg) {
1167 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001168 case MESSAGE_DISMISS:
1169 if (mDialog != null) {
1170 mDialog.dismiss();
1171 mDialog = null;
1172 }
1173 break;
1174 case MESSAGE_REFRESH:
1175 refreshSilentMode();
1176 mAdapter.notifyDataSetChanged();
1177 break;
1178 case MESSAGE_SHOW:
1179 handleShow();
1180 break;
Jason Monk361915c2017-03-21 20:33:59 -04001181 }
1182 }
1183 };
1184
1185 private void onAirplaneModeChanged() {
1186 // Let the service state callbacks handle the state.
1187 if (mHasTelephony) return;
1188
1189 boolean airplaneModeOn = Settings.Global.getInt(
1190 mContext.getContentResolver(),
1191 Settings.Global.AIRPLANE_MODE_ON,
1192 0) == 1;
1193 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1194 mAirplaneModeOn.updateState(mAirplaneState);
1195 }
1196
1197 /**
1198 * Change the airplane mode system setting
1199 */
1200 private void changeAirplaneModeSystemSetting(boolean on) {
1201 Settings.Global.putInt(
1202 mContext.getContentResolver(),
1203 Settings.Global.AIRPLANE_MODE_ON,
1204 on ? 1 : 0);
1205 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1206 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1207 intent.putExtra("state", on);
1208 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1209 if (!mHasTelephony) {
1210 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1211 }
1212 }
1213
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001214 private static final class ActionsDialog extends Dialog implements DialogInterface,
1215 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001216
Jason Monk16fbd9d2017-04-27 14:28:49 -04001217 private final Context mContext;
1218 private final MyAdapter mAdapter;
1219 private final LinearLayout mListView;
1220 private final HardwareUiLayout mHardwareLayout;
1221 private final OnClickListener mClickListener;
1222 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001223 private final GradientDrawable mGradientDrawable;
1224 private final ColorExtractor mColorExtractor;
1225 private boolean mKeyguardShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001226
1227 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
1228 OnItemLongClickListener longClickListener) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001229 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001230 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001231 mAdapter = adapter;
1232 mClickListener = clickListener;
1233 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001234 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001235 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001236
1237 // Window initialization
1238 Window window = getWindow();
1239 window.requestFeature(Window.FEATURE_NO_TITLE);
1240 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
1241 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
1242 window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
1243 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
1244 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1245 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1246 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1247 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1248 window.setBackgroundDrawable(mGradientDrawable);
1249 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1250
Jason Monk16fbd9d2017-04-27 14:28:49 -04001251 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001252 mListView = findViewById(android.R.id.list);
1253 mHardwareLayout = HardwareUiLayout.get(mListView);
1254 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Jason Monk361915c2017-03-21 20:33:59 -04001255 }
1256
Jason Monk16fbd9d2017-04-27 14:28:49 -04001257 private void updateList() {
1258 mListView.removeAllViews();
1259 for (int i = 0; i < mAdapter.getCount(); i++) {
1260 View v = mAdapter.getView(i, null, mListView);
1261 final int pos = i;
1262 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1263 v.setOnLongClickListener(view ->
1264 mLongClickListener.onItemLongClick(null, v, pos, 0));
1265 mListView.addView(v);
1266 }
Jason Monk361915c2017-03-21 20:33:59 -04001267 }
1268
1269 @Override
1270 protected void onStart() {
1271 super.setCanceledOnTouchOutside(true);
1272 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001273 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001274
1275 Point displaySize = new Point();
1276 mContext.getDisplay().getRealSize(displaySize);
1277 mColorExtractor.addOnColorsChangedListener(this);
1278 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1279 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1280 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
1281 mGradientDrawable.setColors(colors, false);
Jason Monk361915c2017-03-21 20:33:59 -04001282 }
1283
1284 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001285 protected void onStop() {
1286 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001287 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001288 }
1289
1290 @Override
1291 public void show() {
1292 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001293 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001294 mHardwareLayout.setTranslationX(getAnimTranslation());
1295 mHardwareLayout.setAlpha(0);
1296 mHardwareLayout.animate()
1297 .alpha(1)
1298 .translationX(0)
1299 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001300 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001301 .setUpdateListener(animation -> {
1302 int alpha = (int) ((Float) animation.getAnimatedValue()
1303 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1304 mGradientDrawable.setAlpha(alpha);
1305 })
Jason Monka7af3b62017-07-07 11:35:13 -04001306 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001307 .start();
1308 }
1309
1310 @Override
1311 public void dismiss() {
1312 mHardwareLayout.setTranslationX(0);
1313 mHardwareLayout.setAlpha(1);
1314 mHardwareLayout.animate()
1315 .alpha(0)
1316 .translationX(getAnimTranslation())
1317 .setDuration(300)
1318 .withEndAction(() -> super.dismiss())
1319 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001320 .setUpdateListener(animation -> {
1321 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1322 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1323 mGradientDrawable.setAlpha(alpha);
1324 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001325 .start();
1326 }
1327
Jason Monkfd279662017-06-29 19:37:48 -04001328 public void fadeOut() {
1329 mHardwareLayout.setTranslationX(0);
1330 mHardwareLayout.setAlpha(1);
1331 mListView.animate()
1332 .alpha(0)
1333 .translationX(getAnimTranslation())
1334 .setDuration(300)
1335 .setInterpolator(new LogAccelerateInterpolator())
Jason Monk4e04e492017-07-11 11:07:08 -04001336 .setUpdateListener(animation -> {
1337 float frac = animation.getAnimatedFraction();
Lucas Dupinde9db422017-07-19 17:15:41 -07001338 float alpha = NotificationUtils.interpolate(
1339 ScrimController.GRADIENT_SCRIM_ALPHA, SHUTDOWN_SCRIM_ALPHA, frac);
Jason Monk4e04e492017-07-11 11:07:08 -04001340 mGradientDrawable.setAlpha((int) (alpha * 255));
1341 })
Jason Monkfd279662017-06-29 19:37:48 -04001342 .start();
1343 }
1344
Jason Monk16fbd9d2017-04-27 14:28:49 -04001345 private float getAnimTranslation() {
1346 return getContext().getResources().getDimension(
1347 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001348 }
1349
1350 @Override
1351 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1352 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
1353 for (int i = 0; i < mAdapter.getCount(); ++i) {
1354 CharSequence label =
1355 mAdapter.getItem(i).getLabelForAccessibility(getContext());
1356 if (label != null) {
1357 event.getText().add(label);
1358 }
1359 }
1360 }
1361 return super.dispatchPopulateAccessibilityEvent(event);
1362 }
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001363
1364 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001365 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001366 if (mKeyguardShowing) {
1367 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001368 mGradientDrawable.setColors(extractor.getColors(WallpaperManager.FLAG_LOCK));
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001369 }
1370 } else {
1371 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001372 mGradientDrawable.setColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM));
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001373 }
1374 }
1375 }
1376
1377 public void setKeyguardShowing(boolean keyguardShowing) {
1378 mKeyguardShowing = keyguardShowing;
1379 }
Jason Monk361915c2017-03-21 20:33:59 -04001380 }
1381}