blob: 189badfc42fc8fd974372d2d4a66955c2dd10ff9 [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
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070017import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
18
Jason Monk361915c2017-03-21 20:33:59 -040019import com.android.internal.R;
Lucas Dupine2292a92017-07-06 14:35:30 -070020import com.android.internal.colorextraction.ColorExtractor;
21import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Jason Monk361915c2017-03-21 20:33:59 -040022import com.android.internal.logging.MetricsLogger;
23import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
24import com.android.internal.util.EmergencyAffordanceManager;
25import com.android.internal.telephony.TelephonyIntents;
26import com.android.internal.telephony.TelephonyProperties;
27import com.android.internal.widget.LockPatternUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070028import com.android.systemui.Dependency;
Jason Monk16fbd9d2017-04-27 14:28:49 -040029import com.android.systemui.HardwareUiLayout;
Lucas Dupinde9db422017-07-19 17:15:41 -070030import com.android.systemui.Interpolators;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -070031import com.android.systemui.colorextraction.SysuiColorExtractor;
Jason Monk361915c2017-03-21 20:33:59 -040032import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
Lucas Dupinde9db422017-07-19 17:15:41 -070033import com.android.systemui.statusbar.notification.NotificationUtils;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070034import com.android.systemui.statusbar.phone.ScrimController;
Jason Monk16fbd9d2017-04-27 14:28:49 -040035import com.android.systemui.volume.VolumeDialogMotion.LogAccelerateInterpolator;
36import com.android.systemui.volume.VolumeDialogMotion.LogDecelerateInterpolator;
Jason Monk361915c2017-03-21 20:33:59 -040037
Jason Monk4e04e492017-07-11 11:07:08 -040038import android.animation.ValueAnimator;
39import android.animation.ValueAnimator.AnimatorUpdateListener;
Jason Monk361915c2017-03-21 20:33:59 -040040import android.app.ActivityManager;
41import android.app.Dialog;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070042import android.app.WallpaperManager;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.content.BroadcastReceiver;
44import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
47import android.content.IntentFilter;
48import android.content.pm.UserInfo;
49import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070050import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040051import android.graphics.drawable.Drawable;
52import android.media.AudioManager;
53import android.net.ConnectivityManager;
54import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040055import android.os.Handler;
56import android.os.Message;
57import android.os.RemoteException;
58import android.os.ServiceManager;
59import android.os.SystemProperties;
60import android.os.UserHandle;
61import android.os.UserManager;
62import android.os.Vibrator;
63import android.provider.Settings;
64import android.service.dreams.DreamService;
65import android.service.dreams.IDreamManager;
66import android.telephony.PhoneStateListener;
67import android.telephony.ServiceState;
68import android.telephony.TelephonyManager;
69import android.text.TextUtils;
70import android.util.ArraySet;
71import android.util.Log;
Lucas Dupinde9db422017-07-19 17:15:41 -070072import android.util.MathUtils;
Lucas Dupin448786c2017-07-24 17:44:25 -070073import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040074import android.view.LayoutInflater;
75import android.view.View;
76import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070077import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040078import android.view.WindowManager;
79import android.view.WindowManagerGlobal;
80import android.view.accessibility.AccessibilityEvent;
81import android.widget.AdapterView;
Jason Monk16fbd9d2017-04-27 14:28:49 -040082import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040083import android.widget.BaseAdapter;
84import android.widget.ImageView;
85import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040086import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040087import android.widget.TextView;
88
Lucas Dupine2292a92017-07-06 14:35:30 -070089import com.android.internal.colorextraction.drawable.GradientDrawable;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070090
Jason Monk361915c2017-03-21 20:33:59 -040091import java.util.ArrayList;
92import java.util.List;
93
94/**
95 * Helper to show the global actions dialog. Each item is an {@link Action} that
96 * may show depending on whether the keyguard is showing, and whether the device
97 * is provisioned.
98 */
Jason Monk16fbd9d2017-04-27 14:28:49 -040099class GlobalActionsDialog implements DialogInterface.OnDismissListener, DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -0400100
101 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
102 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
103
104 private static final String TAG = "GlobalActionsDialog";
105
106 private static final boolean SHOW_SILENT_TOGGLE = true;
107
108 /* Valid settings for global actions keys.
109 * see config.xml config_globalActionList */
110 private static final String GLOBAL_ACTION_KEY_POWER = "power";
111 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
112 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
113 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
114 private static final String GLOBAL_ACTION_KEY_USERS = "users";
115 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
116 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
117 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
118 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
119 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
120
121 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)) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700315 if (Settings.Secure.getInt(mContext.getContentResolver(),
316 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0) {
317 mItems.add(getLockdownAction());
318 }
Jason Monk361915c2017-03-21 20:33:59 -0400319 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
320 mItems.add(getVoiceAssistAction());
321 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
322 mItems.add(getAssistAction());
323 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
324 mItems.add(new RestartAction());
325 } else {
326 Log.e(TAG, "Invalid global action key " + actionKey);
327 }
328 // Add here so we don't add more than one.
329 addedKeys.add(actionKey);
330 }
331
332 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
333 mItems.add(getEmergencyAction());
334 }
335
336 mAdapter = new MyAdapter();
337
Jason Monk16fbd9d2017-04-27 14:28:49 -0400338 OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener() {
339 @Override
340 public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
341 long id) {
342 final Action action = mAdapter.getItem(position);
343 if (action instanceof LongPressAction) {
Shunta Satobdb0e492017-04-14 16:01:54 +0900344 mDialog.dismiss();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400345 return ((LongPressAction) action).onLongPress();
346 }
347 return false;
348 }
349 };
350 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener);
Jason Monk361915c2017-03-21 20:33:59 -0400351 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700352 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400353
354 dialog.setOnDismissListener(this);
355
356 return dialog;
357 }
358
359 private final class PowerAction extends SinglePressAction implements LongPressAction {
360 private PowerAction() {
361 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400362 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400363 }
364
365 @Override
366 public boolean onLongPress() {
367 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
368 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
369 mWindowManagerFuncs.reboot(true);
370 return true;
371 }
372 return false;
373 }
374
375 @Override
376 public boolean showDuringKeyguard() {
377 return true;
378 }
379
380 @Override
381 public boolean showBeforeProvisioning() {
382 return true;
383 }
384
385 @Override
386 public void onPress() {
387 // shutdown by making sure radio and power are handled accordingly.
388 mWindowManagerFuncs.shutdown();
389 }
390 }
391
392 private final class RestartAction extends SinglePressAction implements LongPressAction {
393 private RestartAction() {
394 super(R.drawable.ic_restart, R.string.global_action_restart);
395 }
396
397 @Override
398 public boolean onLongPress() {
399 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
400 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
401 mWindowManagerFuncs.reboot(true);
402 return true;
403 }
404 return false;
405 }
406
407 @Override
408 public boolean showDuringKeyguard() {
409 return true;
410 }
411
412 @Override
413 public boolean showBeforeProvisioning() {
414 return true;
415 }
416
417 @Override
418 public void onPress() {
419 mWindowManagerFuncs.reboot(false);
420 }
421 }
422
423
424 private class BugReportAction extends SinglePressAction implements LongPressAction {
425
426 public BugReportAction() {
427 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
428 }
429
430 @Override
431 public void onPress() {
432 // don't actually trigger the bugreport if we are running stability
433 // tests via monkey
434 if (ActivityManager.isUserAMonkey()) {
435 return;
436 }
437 // Add a little delay before executing, to give the
438 // dialog a chance to go away before it takes a
439 // screenshot.
440 mHandler.postDelayed(new Runnable() {
441 @Override
442 public void run() {
443 try {
444 // Take an "interactive" bugreport.
445 MetricsLogger.action(mContext,
446 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
447 ActivityManager.getService().requestBugReport(
448 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
449 } catch (RemoteException e) {
450 }
451 }
452 }, 500);
453 }
454
455 @Override
456 public boolean onLongPress() {
457 // don't actually trigger the bugreport if we are running stability
458 // tests via monkey
459 if (ActivityManager.isUserAMonkey()) {
460 return false;
461 }
462 try {
463 // Take a "full" bugreport.
464 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
465 ActivityManager.getService().requestBugReport(
466 ActivityManager.BUGREPORT_OPTION_FULL);
467 } catch (RemoteException e) {
468 }
469 return false;
470 }
471
472 public boolean showDuringKeyguard() {
473 return true;
474 }
475
476 @Override
477 public boolean showBeforeProvisioning() {
478 return false;
479 }
480
481 @Override
482 public String getStatus() {
483 return mContext.getString(
484 R.string.bugreport_status,
485 Build.VERSION.RELEASE,
486 Build.ID);
487 }
488 }
489
490 private Action getSettingsAction() {
491 return new SinglePressAction(R.drawable.ic_settings,
492 R.string.global_action_settings) {
493
494 @Override
495 public void onPress() {
496 Intent intent = new Intent(Settings.ACTION_SETTINGS);
497 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
498 mContext.startActivity(intent);
499 }
500
501 @Override
502 public boolean showDuringKeyguard() {
503 return true;
504 }
505
506 @Override
507 public boolean showBeforeProvisioning() {
508 return true;
509 }
510 };
511 }
512
513 private Action getEmergencyAction() {
514 return new SinglePressAction(R.drawable.emergency_icon,
515 R.string.global_action_emergency) {
516 @Override
517 public void onPress() {
518 mEmergencyAffordanceManager.performEmergencyCall();
519 }
520
521 @Override
522 public boolean showDuringKeyguard() {
523 return true;
524 }
525
526 @Override
527 public boolean showBeforeProvisioning() {
528 return true;
529 }
530 };
531 }
532
533 private Action getAssistAction() {
534 return new SinglePressAction(R.drawable.ic_action_assist_focused,
535 R.string.global_action_assist) {
536 @Override
537 public void onPress() {
538 Intent intent = new Intent(Intent.ACTION_ASSIST);
539 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
540 mContext.startActivity(intent);
541 }
542
543 @Override
544 public boolean showDuringKeyguard() {
545 return true;
546 }
547
548 @Override
549 public boolean showBeforeProvisioning() {
550 return true;
551 }
552 };
553 }
554
555 private Action getVoiceAssistAction() {
556 return new SinglePressAction(R.drawable.ic_voice_search,
557 R.string.global_action_voice_assist) {
558 @Override
559 public void onPress() {
560 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
561 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
562 mContext.startActivity(intent);
563 }
564
565 @Override
566 public boolean showDuringKeyguard() {
567 return true;
568 }
569
570 @Override
571 public boolean showBeforeProvisioning() {
572 return true;
573 }
574 };
575 }
576
577 private Action getLockdownAction() {
578 return new SinglePressAction(R.drawable.ic_lock_lock,
579 R.string.global_action_lockdown) {
580
581 @Override
582 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700583 new LockPatternUtils(mContext)
584 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
585 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400586 try {
587 WindowManagerGlobal.getWindowManagerService().lockNow(null);
588 } catch (RemoteException e) {
589 Log.e(TAG, "Error while trying to lock device.", e);
590 }
591 }
592
593 @Override
594 public boolean showDuringKeyguard() {
595 return true;
596 }
597
598 @Override
599 public boolean showBeforeProvisioning() {
600 return false;
601 }
602 };
603 }
604
605 private UserInfo getCurrentUser() {
606 try {
607 return ActivityManager.getService().getCurrentUser();
608 } catch (RemoteException re) {
609 return null;
610 }
611 }
612
613 private boolean isCurrentUserOwner() {
614 UserInfo currentUser = getCurrentUser();
615 return currentUser == null || currentUser.isPrimary();
616 }
617
618 private void addUsersToMenu(ArrayList<Action> items) {
619 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
620 if (um.isUserSwitcherEnabled()) {
621 List<UserInfo> users = um.getUsers();
622 UserInfo currentUser = getCurrentUser();
623 for (final UserInfo user : users) {
624 if (user.supportsSwitchToByUser()) {
625 boolean isCurrentUser = currentUser == null
626 ? user.id == 0 : (currentUser.id == user.id);
627 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
628 : null;
629 SinglePressAction switchToUser = new SinglePressAction(
630 R.drawable.ic_menu_cc, icon,
631 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400632 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400633 public void onPress() {
634 try {
635 ActivityManager.getService().switchUser(user.id);
636 } catch (RemoteException re) {
637 Log.e(TAG, "Couldn't switch user " + re);
638 }
639 }
640
641 public boolean showDuringKeyguard() {
642 return true;
643 }
644
645 public boolean showBeforeProvisioning() {
646 return false;
647 }
648 };
649 items.add(switchToUser);
650 }
651 }
652 }
653 }
654
655 private void prepareDialog() {
656 refreshSilentMode();
657 mAirplaneModeOn.updateState(mAirplaneState);
658 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400659 if (mShowSilentToggle) {
660 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
661 mContext.registerReceiver(mRingerModeReceiver, filter);
662 }
663 }
664
665 private void refreshSilentMode() {
666 if (!mHasVibrator) {
667 final boolean silentModeOn =
668 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400669 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400670 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
671 }
672 }
673
674 /** {@inheritDoc} */
675 public void onDismiss(DialogInterface dialog) {
676 mWindowManagerFuncs.onGlobalActionsHidden();
677 if (mShowSilentToggle) {
678 try {
679 mContext.unregisterReceiver(mRingerModeReceiver);
680 } catch (IllegalArgumentException ie) {
681 // ignore this
682 Log.w(TAG, ie);
683 }
684 }
685 }
686
687 /** {@inheritDoc} */
688 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400689 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400690 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400691 dialog.dismiss();
692 }
Jason Monkfd279662017-06-29 19:37:48 -0400693 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400694 }
695
696 /**
697 * The adapter used for the list within the global actions dialog, taking
698 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400699 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
700 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400701 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
702 */
703 private class MyAdapter extends BaseAdapter {
704
705 public int getCount() {
706 int count = 0;
707
708 for (int i = 0; i < mItems.size(); i++) {
709 final Action action = mItems.get(i);
710
711 if (mKeyguardShowing && !action.showDuringKeyguard()) {
712 continue;
713 }
714 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
715 continue;
716 }
717 count++;
718 }
719 return count;
720 }
721
722 @Override
723 public boolean isEnabled(int position) {
724 return getItem(position).isEnabled();
725 }
726
727 @Override
728 public boolean areAllItemsEnabled() {
729 return false;
730 }
731
732 public Action getItem(int position) {
733
734 int filteredPos = 0;
735 for (int i = 0; i < mItems.size(); i++) {
736 final Action action = mItems.get(i);
737 if (mKeyguardShowing && !action.showDuringKeyguard()) {
738 continue;
739 }
740 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
741 continue;
742 }
743 if (filteredPos == position) {
744 return action;
745 }
746 filteredPos++;
747 }
748
749 throw new IllegalArgumentException("position " + position
750 + " out of range of showable actions"
751 + ", filtered count=" + getCount()
752 + ", keyguardshowing=" + mKeyguardShowing
753 + ", provisioned=" + mDeviceProvisioned);
754 }
755
756
757 public long getItemId(int position) {
758 return position;
759 }
760
761 public View getView(int position, View convertView, ViewGroup parent) {
762 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400763 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
764 if (position == 2) {
765 HardwareUiLayout.get(parent).setDivisionView(view);
766 }
767 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400768 }
769 }
770
771 // note: the scheme below made more sense when we were planning on having
772 // 8 different things in the global actions dialog. seems overkill with
773 // only 3 items now, but may as well keep this flexible approach so it will
774 // be easy should someone decide at the last minute to include something
775 // else, such as 'enable wifi', or 'enable bluetooth'
776
777 /**
778 * What each item in the global actions dialog must be able to support.
779 */
780 private interface Action {
781 /**
782 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400783 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400784 */
785 CharSequence getLabelForAccessibility(Context context);
786
787 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
788
789 void onPress();
790
791 /**
792 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400793 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400794 */
795 boolean showDuringKeyguard();
796
797 /**
798 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400799 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400800 */
801 boolean showBeforeProvisioning();
802
803 boolean isEnabled();
804 }
805
806 /**
807 * An action that also supports long press.
808 */
809 private interface LongPressAction extends Action {
810 boolean onLongPress();
811 }
812
813 /**
814 * A single press action maintains no state, just responds to a press
815 * and takes an action.
816 */
817 private static abstract class SinglePressAction implements Action {
818 private final int mIconResId;
819 private final Drawable mIcon;
820 private final int mMessageResId;
821 private final CharSequence mMessage;
822
823 protected SinglePressAction(int iconResId, int messageResId) {
824 mIconResId = iconResId;
825 mMessageResId = messageResId;
826 mMessage = null;
827 mIcon = null;
828 }
829
830 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
831 mIconResId = iconResId;
832 mMessageResId = 0;
833 mMessage = message;
834 mIcon = icon;
835 }
836
837 public boolean isEnabled() {
838 return true;
839 }
840
841 public String getStatus() {
842 return null;
843 }
844
845 abstract public void onPress();
846
847 public CharSequence getLabelForAccessibility(Context context) {
848 if (mMessage != null) {
849 return mMessage;
850 } else {
851 return context.getString(mMessageResId);
852 }
853 }
854
855 public View create(
856 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400857 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
858 false);
Jason Monk361915c2017-03-21 20:33:59 -0400859
860 ImageView icon = (ImageView) v.findViewById(R.id.icon);
861 TextView messageView = (TextView) v.findViewById(R.id.message);
862
863 TextView statusView = (TextView) v.findViewById(R.id.status);
864 final String status = getStatus();
865 if (!TextUtils.isEmpty(status)) {
866 statusView.setText(status);
867 } else {
868 statusView.setVisibility(View.GONE);
869 }
870 if (mIcon != null) {
871 icon.setImageDrawable(mIcon);
872 icon.setScaleType(ScaleType.CENTER_CROP);
873 } else if (mIconResId != 0) {
874 icon.setImageDrawable(context.getDrawable(mIconResId));
875 }
876 if (mMessage != null) {
877 messageView.setText(mMessage);
878 } else {
879 messageView.setText(mMessageResId);
880 }
881
882 return v;
883 }
884 }
885
886 /**
887 * A toggle action knows whether it is on or off, and displays an icon
888 * and status message accordingly.
889 */
890 private static abstract class ToggleAction implements Action {
891
892 enum State {
893 Off(false),
894 TurningOn(true),
895 TurningOff(true),
896 On(false);
897
898 private final boolean inTransition;
899
900 State(boolean intermediate) {
901 inTransition = intermediate;
902 }
903
904 public boolean inTransition() {
905 return inTransition;
906 }
907 }
908
909 protected State mState = State.Off;
910
911 // prefs
912 protected int mEnabledIconResId;
913 protected int mDisabledIconResid;
914 protected int mMessageResId;
915 protected int mEnabledStatusMessageResId;
916 protected int mDisabledStatusMessageResId;
917
918 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -0400919 * @param enabledIconResId The icon for when this action is on.
920 * @param disabledIconResid The icon for when this action is off.
921 * @param message The general information message, e.g 'Silent Mode'
922 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -0400923 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
924 */
925 public ToggleAction(int enabledIconResId,
926 int disabledIconResid,
927 int message,
928 int enabledStatusMessageResId,
929 int disabledStatusMessageResId) {
930 mEnabledIconResId = enabledIconResId;
931 mDisabledIconResid = disabledIconResid;
932 mMessageResId = message;
933 mEnabledStatusMessageResId = enabledStatusMessageResId;
934 mDisabledStatusMessageResId = disabledStatusMessageResId;
935 }
936
937 /**
938 * Override to make changes to resource IDs just before creating the
939 * View.
940 */
941 void willCreate() {
942
943 }
944
945 @Override
946 public CharSequence getLabelForAccessibility(Context context) {
947 return context.getString(mMessageResId);
948 }
949
950 public View create(Context context, View convertView, ViewGroup parent,
951 LayoutInflater inflater) {
952 willCreate();
953
954 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -0400955 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -0400956
957 ImageView icon = (ImageView) v.findViewById(R.id.icon);
958 TextView messageView = (TextView) v.findViewById(R.id.message);
959 TextView statusView = (TextView) v.findViewById(R.id.status);
960 final boolean enabled = isEnabled();
961
962 if (messageView != null) {
963 messageView.setText(mMessageResId);
964 messageView.setEnabled(enabled);
965 }
966
967 boolean on = ((mState == State.On) || (mState == State.TurningOn));
968 if (icon != null) {
969 icon.setImageDrawable(context.getDrawable(
970 (on ? mEnabledIconResId : mDisabledIconResid)));
971 icon.setEnabled(enabled);
972 }
973
974 if (statusView != null) {
975 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
976 statusView.setVisibility(View.VISIBLE);
977 statusView.setEnabled(enabled);
978 }
979 v.setEnabled(enabled);
980
981 return v;
982 }
983
984 public final void onPress() {
985 if (mState.inTransition()) {
986 Log.w(TAG, "shouldn't be able to toggle when in transition");
987 return;
988 }
989
990 final boolean nowOn = !(mState == State.On);
991 onToggle(nowOn);
992 changeStateFromPress(nowOn);
993 }
994
995 public boolean isEnabled() {
996 return !mState.inTransition();
997 }
998
999 /**
1000 * Implementations may override this if their state can be in on of the intermediate
1001 * states until some notification is received (e.g airplane mode is 'turning off' until
1002 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001003 *
Jason Monk361915c2017-03-21 20:33:59 -04001004 * @param buttonOn Whether the button was turned on or off
1005 */
1006 protected void changeStateFromPress(boolean buttonOn) {
1007 mState = buttonOn ? State.On : State.Off;
1008 }
1009
1010 abstract void onToggle(boolean on);
1011
1012 public void updateState(State state) {
1013 mState = state;
1014 }
1015 }
1016
1017 private class SilentModeToggleAction extends ToggleAction {
1018 public SilentModeToggleAction() {
1019 super(R.drawable.ic_audio_vol_mute,
1020 R.drawable.ic_audio_vol,
1021 R.string.global_action_toggle_silent_mode,
1022 R.string.global_action_silent_mode_on_status,
1023 R.string.global_action_silent_mode_off_status);
1024 }
1025
1026 void onToggle(boolean on) {
1027 if (on) {
1028 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1029 } else {
1030 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1031 }
1032 }
1033
1034 public boolean showDuringKeyguard() {
1035 return true;
1036 }
1037
1038 public boolean showBeforeProvisioning() {
1039 return false;
1040 }
1041 }
1042
1043 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1044
Jason Monk16fbd9d2017-04-27 14:28:49 -04001045 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001046
1047 private final AudioManager mAudioManager;
1048 private final Handler mHandler;
1049 private final Context mContext;
1050
1051 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1052 mAudioManager = audioManager;
1053 mHandler = handler;
1054 mContext = context;
1055 }
1056
1057 private int ringerModeToIndex(int ringerMode) {
1058 // They just happen to coincide
1059 return ringerMode;
1060 }
1061
1062 private int indexToRingerMode(int index) {
1063 // They just happen to coincide
1064 return index;
1065 }
1066
1067 @Override
1068 public CharSequence getLabelForAccessibility(Context context) {
1069 return null;
1070 }
1071
1072 public View create(Context context, View convertView, ViewGroup parent,
1073 LayoutInflater inflater) {
1074 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1075
1076 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1077 for (int i = 0; i < 3; i++) {
1078 View itemView = v.findViewById(ITEM_IDS[i]);
1079 itemView.setSelected(selectedIndex == i);
1080 // Set up click handler
1081 itemView.setTag(i);
1082 itemView.setOnClickListener(this);
1083 }
1084 return v;
1085 }
1086
1087 public void onPress() {
1088 }
1089
1090 public boolean showDuringKeyguard() {
1091 return true;
1092 }
1093
1094 public boolean showBeforeProvisioning() {
1095 return false;
1096 }
1097
1098 public boolean isEnabled() {
1099 return true;
1100 }
1101
1102 void willCreate() {
1103 }
1104
1105 public void onClick(View v) {
1106 if (!(v.getTag() instanceof Integer)) return;
1107
1108 int index = (Integer) v.getTag();
1109 mAudioManager.setRingerMode(indexToRingerMode(index));
1110 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1111 }
1112 }
1113
1114 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1115 public void onReceive(Context context, Intent intent) {
1116 String action = intent.getAction();
1117 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1118 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1119 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1120 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
1121 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
1122 }
1123 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1124 // Airplane mode can be changed after ECM exits if airplane toggle button
1125 // is pressed during ECM mode
1126 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1127 mIsWaitingForEcmExit) {
1128 mIsWaitingForEcmExit = false;
1129 changeAirplaneModeSystemSetting(true);
1130 }
1131 }
1132 }
1133 };
1134
1135 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1136 @Override
1137 public void onServiceStateChanged(ServiceState serviceState) {
1138 if (!mHasTelephony) return;
1139 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1140 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1141 mAirplaneModeOn.updateState(mAirplaneState);
1142 mAdapter.notifyDataSetChanged();
1143 }
1144 };
1145
1146 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1147 @Override
1148 public void onReceive(Context context, Intent intent) {
1149 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1150 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1151 }
1152 }
1153 };
1154
1155 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1156 @Override
1157 public void onChange(boolean selfChange) {
1158 onAirplaneModeChanged();
1159 }
1160 };
1161
1162 private static final int MESSAGE_DISMISS = 0;
1163 private static final int MESSAGE_REFRESH = 1;
1164 private static final int MESSAGE_SHOW = 2;
1165 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1166
1167 private Handler mHandler = new Handler() {
1168 public void handleMessage(Message msg) {
1169 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001170 case MESSAGE_DISMISS:
1171 if (mDialog != null) {
1172 mDialog.dismiss();
1173 mDialog = null;
1174 }
1175 break;
1176 case MESSAGE_REFRESH:
1177 refreshSilentMode();
1178 mAdapter.notifyDataSetChanged();
1179 break;
1180 case MESSAGE_SHOW:
1181 handleShow();
1182 break;
Jason Monk361915c2017-03-21 20:33:59 -04001183 }
1184 }
1185 };
1186
1187 private void onAirplaneModeChanged() {
1188 // Let the service state callbacks handle the state.
1189 if (mHasTelephony) return;
1190
1191 boolean airplaneModeOn = Settings.Global.getInt(
1192 mContext.getContentResolver(),
1193 Settings.Global.AIRPLANE_MODE_ON,
1194 0) == 1;
1195 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1196 mAirplaneModeOn.updateState(mAirplaneState);
1197 }
1198
1199 /**
1200 * Change the airplane mode system setting
1201 */
1202 private void changeAirplaneModeSystemSetting(boolean on) {
1203 Settings.Global.putInt(
1204 mContext.getContentResolver(),
1205 Settings.Global.AIRPLANE_MODE_ON,
1206 on ? 1 : 0);
1207 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1208 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1209 intent.putExtra("state", on);
1210 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1211 if (!mHasTelephony) {
1212 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1213 }
1214 }
1215
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001216 private static final class ActionsDialog extends Dialog implements DialogInterface,
1217 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001218
Jason Monk16fbd9d2017-04-27 14:28:49 -04001219 private final Context mContext;
1220 private final MyAdapter mAdapter;
1221 private final LinearLayout mListView;
1222 private final HardwareUiLayout mHardwareLayout;
1223 private final OnClickListener mClickListener;
1224 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001225 private final GradientDrawable mGradientDrawable;
1226 private final ColorExtractor mColorExtractor;
1227 private boolean mKeyguardShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001228
1229 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
1230 OnItemLongClickListener longClickListener) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001231 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001232 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001233 mAdapter = adapter;
1234 mClickListener = clickListener;
1235 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001236 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001237 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001238
1239 // Window initialization
1240 Window window = getWindow();
1241 window.requestFeature(Window.FEATURE_NO_TITLE);
1242 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
1243 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001244 window.addFlags(
1245 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001246 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1247 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1248 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1249 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1250 window.setBackgroundDrawable(mGradientDrawable);
1251 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1252
Jason Monk16fbd9d2017-04-27 14:28:49 -04001253 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001254 mListView = findViewById(android.R.id.list);
1255 mHardwareLayout = HardwareUiLayout.get(mListView);
1256 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Jason Monk361915c2017-03-21 20:33:59 -04001257 }
1258
Jason Monk16fbd9d2017-04-27 14:28:49 -04001259 private void updateList() {
1260 mListView.removeAllViews();
1261 for (int i = 0; i < mAdapter.getCount(); i++) {
1262 View v = mAdapter.getView(i, null, mListView);
1263 final int pos = i;
1264 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1265 v.setOnLongClickListener(view ->
1266 mLongClickListener.onItemLongClick(null, v, pos, 0));
1267 mListView.addView(v);
1268 }
Jason Monk361915c2017-03-21 20:33:59 -04001269 }
1270
1271 @Override
1272 protected void onStart() {
1273 super.setCanceledOnTouchOutside(true);
1274 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001275 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001276
1277 Point displaySize = new Point();
1278 mContext.getDisplay().getRealSize(displaySize);
1279 mColorExtractor.addOnColorsChangedListener(this);
1280 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1281 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1282 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001283 updateColors(colors, false /* animate */);
1284 }
1285
1286 /**
1287 * Updates background and system bars according to current GradientColors.
1288 * @param colors Colors and hints to use.
1289 * @param animate Interpolates gradient if true, just sets otherwise.
1290 */
1291 private void updateColors(GradientColors colors, boolean animate) {
1292 mGradientDrawable.setColors(colors, animate);
1293 View decorView = getWindow().getDecorView();
1294 if (colors.supportsDarkText()) {
1295 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1296 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1297 } else {
1298 decorView.setSystemUiVisibility(0);
1299 }
Jason Monk361915c2017-03-21 20:33:59 -04001300 }
1301
1302 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001303 protected void onStop() {
1304 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001305 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001306 }
1307
1308 @Override
1309 public void show() {
1310 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001311 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001312 mHardwareLayout.setTranslationX(getAnimTranslation());
1313 mHardwareLayout.setAlpha(0);
1314 mHardwareLayout.animate()
1315 .alpha(1)
1316 .translationX(0)
1317 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001318 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001319 .setUpdateListener(animation -> {
1320 int alpha = (int) ((Float) animation.getAnimatedValue()
1321 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1322 mGradientDrawable.setAlpha(alpha);
1323 })
Jason Monka7af3b62017-07-07 11:35:13 -04001324 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001325 .start();
1326 }
1327
1328 @Override
1329 public void dismiss() {
1330 mHardwareLayout.setTranslationX(0);
1331 mHardwareLayout.setAlpha(1);
1332 mHardwareLayout.animate()
1333 .alpha(0)
1334 .translationX(getAnimTranslation())
1335 .setDuration(300)
1336 .withEndAction(() -> super.dismiss())
1337 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001338 .setUpdateListener(animation -> {
1339 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1340 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1341 mGradientDrawable.setAlpha(alpha);
1342 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001343 .start();
1344 }
1345
1346 private float getAnimTranslation() {
1347 return getContext().getResources().getDimension(
1348 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001349 }
1350
1351 @Override
1352 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1353 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
1354 for (int i = 0; i < mAdapter.getCount(); ++i) {
1355 CharSequence label =
1356 mAdapter.getItem(i).getLabelForAccessibility(getContext());
1357 if (label != null) {
1358 event.getText().add(label);
1359 }
1360 }
1361 }
1362 return super.dispatchPopulateAccessibilityEvent(event);
1363 }
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001364
1365 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001366 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001367 if (mKeyguardShowing) {
1368 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001369 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1370 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001371 }
1372 } else {
1373 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001374 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1375 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001376 }
1377 }
1378 }
1379
1380 public void setKeyguardShowing(boolean keyguardShowing) {
1381 mKeyguardShowing = keyguardShowing;
1382 }
Jason Monk361915c2017-03-21 20:33:59 -04001383 }
1384}