blob: 087d481a61f2eac98be8cc4dd22922705faf2a72 [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070027import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000028import android.app.admin.DevicePolicyManager;
Jason Monk361915c2017-03-21 20:33:59 -040029import android.content.BroadcastReceiver;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050030import android.content.ComponentName;
Jason Monk361915c2017-03-21 20:33:59 -040031import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050035import android.content.ServiceConnection;
Jason Monk361915c2017-03-21 20:33:59 -040036import android.content.pm.UserInfo;
37import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070038import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040039import android.graphics.drawable.Drawable;
40import android.media.AudioManager;
41import android.net.ConnectivityManager;
42import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040043import android.os.Handler;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050044import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040045import android.os.Message;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050046import android.os.Messenger;
Jason Monk361915c2017-03-21 20:33:59 -040047import android.os.RemoteException;
48import android.os.ServiceManager;
49import android.os.SystemProperties;
50import android.os.UserHandle;
51import android.os.UserManager;
52import android.os.Vibrator;
53import android.provider.Settings;
54import android.service.dreams.DreamService;
55import android.service.dreams.IDreamManager;
56import android.telephony.PhoneStateListener;
57import android.telephony.ServiceState;
58import android.telephony.TelephonyManager;
59import android.text.TextUtils;
60import android.util.ArraySet;
61import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070062import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040063import android.view.LayoutInflater;
64import android.view.View;
65import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070066import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040067import android.view.WindowManager;
68import android.view.WindowManagerGlobal;
69import android.view.accessibility.AccessibilityEvent;
70import android.widget.AdapterView;
Jason Monk16fbd9d2017-04-27 14:28:49 -040071import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040072import android.widget.BaseAdapter;
73import android.widget.ImageView;
74import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040075import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040076import android.widget.TextView;
77
Charles He9851a8d2017-10-10 17:31:30 +010078import com.android.internal.R;
79import com.android.internal.colorextraction.ColorExtractor;
80import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070081import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010082import com.android.internal.logging.MetricsLogger;
83import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
84import com.android.internal.telephony.TelephonyIntents;
85import com.android.internal.telephony.TelephonyProperties;
86import com.android.internal.util.EmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050087import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010088import com.android.internal.widget.LockPatternUtils;
89import com.android.systemui.Dependency;
90import com.android.systemui.HardwareUiLayout;
91import com.android.systemui.Interpolators;
92import com.android.systemui.colorextraction.SysuiColorExtractor;
93import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
94import com.android.systemui.statusbar.phone.ScrimController;
Julia Reynolds42411922017-11-08 11:19:09 -050095import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070096
Jason Monk361915c2017-03-21 20:33:59 -040097import java.util.ArrayList;
98import java.util.List;
99
100/**
101 * Helper to show the global actions dialog. Each item is an {@link Action} that
102 * may show depending on whether the keyguard is showing, and whether the device
103 * is provisioned.
104 */
Charles He9851a8d2017-10-10 17:31:30 +0100105class GlobalActionsDialog implements DialogInterface.OnDismissListener,
106 DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -0400107
108 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
109 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700110 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400111
112 private static final String TAG = "GlobalActionsDialog";
113
114 private static final boolean SHOW_SILENT_TOGGLE = true;
115
116 /* Valid settings for global actions keys.
117 * see config.xml config_globalActionList */
118 private static final String GLOBAL_ACTION_KEY_POWER = "power";
119 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
120 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
121 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
122 private static final String GLOBAL_ACTION_KEY_USERS = "users";
123 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
124 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
125 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
126 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
127 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000128 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500129 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400130
131 private final Context mContext;
132 private final GlobalActionsManager mWindowManagerFuncs;
133 private final AudioManager mAudioManager;
134 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000135 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800136 private final LockPatternUtils mLockPatternUtils;
137 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400138
139 private ArrayList<Action> mItems;
140 private ActionsDialog mDialog;
141
142 private Action mSilentModeAction;
143 private ToggleAction mAirplaneModeOn;
144
145 private MyAdapter mAdapter;
146
147 private boolean mKeyguardShowing = false;
148 private boolean mDeviceProvisioned = false;
149 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
150 private boolean mIsWaitingForEcmExit = false;
151 private boolean mHasTelephony;
152 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000153 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800154 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400155 private final boolean mShowSilentToggle;
156 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500157 private final ScreenshotHelper mScreenshotHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400158
159 /**
160 * @param context everything needs a context :(
161 */
162 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700163 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400164 mWindowManagerFuncs = windowManagerFuncs;
165 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
166 mDreamManager = IDreamManager.Stub.asInterface(
167 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000168 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
169 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800170 mLockPatternUtils = new LockPatternUtils(mContext);
171 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400172
173 // receive broadcasts
174 IntentFilter filter = new IntentFilter();
175 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
176 filter.addAction(Intent.ACTION_SCREEN_OFF);
177 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
178 context.registerReceiver(mBroadcastReceiver, filter);
179
180 ConnectivityManager cm = (ConnectivityManager)
181 context.getSystemService(Context.CONNECTIVITY_SERVICE);
182 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
183
184 // get notified of phone state changes
185 TelephonyManager telephonyManager =
186 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
187 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
188 mContext.getContentResolver().registerContentObserver(
189 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
190 mAirplaneModeObserver);
191 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
192 mHasVibrator = vibrator != null && vibrator.hasVibrator();
193
194 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
195 R.bool.config_useFixedVolume);
196
197 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500198 mScreenshotHelper = new ScreenshotHelper(context);
Jason Monk361915c2017-03-21 20:33:59 -0400199 }
200
201 /**
202 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400203 *
Jason Monk361915c2017-03-21 20:33:59 -0400204 * @param keyguardShowing True if keyguard is showing
205 */
206 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
207 mKeyguardShowing = keyguardShowing;
208 mDeviceProvisioned = isDeviceProvisioned;
209 if (mDialog != null) {
210 mDialog.dismiss();
211 mDialog = null;
212 // Show delayed, so that the dismiss of the previous dialog completes
213 mHandler.sendEmptyMessage(MESSAGE_SHOW);
214 } else {
215 handleShow();
216 }
217 }
218
Charles He9851a8d2017-10-10 17:31:30 +0100219 /**
220 * Dismiss the global actions dialog, if it's currently shown
221 */
222 public void dismissDialog() {
223 mHandler.removeMessages(MESSAGE_DISMISS);
224 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
225 }
226
Jason Monk361915c2017-03-21 20:33:59 -0400227 private void awakenIfNecessary() {
228 if (mDreamManager != null) {
229 try {
230 if (mDreamManager.isDreaming()) {
231 mDreamManager.awaken();
232 }
233 } catch (RemoteException e) {
234 // we tried
235 }
236 }
237 }
238
239 private void handleShow() {
240 awakenIfNecessary();
241 mDialog = createDialog();
242 prepareDialog();
243
244 // If we only have 1 item and it's a simple press action, just do this action.
245 if (mAdapter.getCount() == 1
246 && mAdapter.getItem(0) instanceof SinglePressAction
247 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
248 ((SinglePressAction) mAdapter.getItem(0)).onPress();
249 } else {
250 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
251 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100252 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400253 mDialog.getWindow().setAttributes(attrs);
254 mDialog.show();
255 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400256 }
257 }
258
259 /**
260 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400261 *
Jason Monk361915c2017-03-21 20:33:59 -0400262 * @return A new dialog.
263 */
264 private ActionsDialog createDialog() {
265 // Simple toggle style if there's no vibrator, otherwise use a tri-state
266 if (!mHasVibrator) {
267 mSilentModeAction = new SilentModeToggleAction();
268 } else {
269 mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler);
270 }
271 mAirplaneModeOn = new ToggleAction(
272 R.drawable.ic_lock_airplane_mode,
273 R.drawable.ic_lock_airplane_mode_off,
274 R.string.global_actions_toggle_airplane_mode,
275 R.string.global_actions_airplane_mode_on_status,
276 R.string.global_actions_airplane_mode_off_status) {
277
278 void onToggle(boolean on) {
279 if (mHasTelephony && Boolean.parseBoolean(
280 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
281 mIsWaitingForEcmExit = true;
282 // Launch ECM exit dialog
283 Intent ecmDialogIntent =
284 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
285 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
286 mContext.startActivity(ecmDialogIntent);
287 } else {
288 changeAirplaneModeSystemSetting(on);
289 }
290 }
291
292 @Override
293 protected void changeStateFromPress(boolean buttonOn) {
294 if (!mHasTelephony) return;
295
296 // In ECM mode airplane state cannot be changed
297 if (!(Boolean.parseBoolean(
298 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
299 mState = buttonOn ? State.TurningOn : State.TurningOff;
300 mAirplaneState = mState;
301 }
302 }
303
304 public boolean showDuringKeyguard() {
305 return true;
306 }
307
308 public boolean showBeforeProvisioning() {
309 return false;
310 }
311 };
312 onAirplaneModeChanged();
313
314 mItems = new ArrayList<Action>();
315 String[] defaultActions = mContext.getResources().getStringArray(
316 R.array.config_globalActionsList);
317
318 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000319 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800320 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400321 for (int i = 0; i < defaultActions.length; i++) {
322 String actionKey = defaultActions[i];
323 if (addedKeys.contains(actionKey)) {
324 // If we already have added this, don't add it again.
325 continue;
326 }
327 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
328 mItems.add(new PowerAction());
329 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
330 mItems.add(mAirplaneModeOn);
331 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
332 if (Settings.Global.getInt(mContext.getContentResolver(),
333 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
334 mItems.add(new BugReportAction());
335 }
336 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
337 if (mShowSilentToggle) {
338 mItems.add(mSilentModeAction);
339 }
340 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
341 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
342 addUsersToMenu(mItems);
343 }
344 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
345 mItems.add(getSettingsAction());
346 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700347 if (Settings.Secure.getInt(mContext.getContentResolver(),
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800348 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0
349 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700350 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800351 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700352 }
Jason Monk361915c2017-03-21 20:33:59 -0400353 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
354 mItems.add(getVoiceAssistAction());
355 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
356 mItems.add(getAssistAction());
357 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
358 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500359 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
360 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000361 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000362 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000363 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
364 mItems.add(new LogoutAction());
365 mHasLogoutButton = true;
366 }
Jason Monk361915c2017-03-21 20:33:59 -0400367 } else {
368 Log.e(TAG, "Invalid global action key " + actionKey);
369 }
370 // Add here so we don't add more than one.
371 addedKeys.add(actionKey);
372 }
373
374 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
375 mItems.add(getEmergencyAction());
376 }
377
378 mAdapter = new MyAdapter();
379
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700380 OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> {
381 final Action action = mAdapter.getItem(position);
382 if (action instanceof LongPressAction) {
383 mDialog.dismiss();
384 return ((LongPressAction) action).onLongPress();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400385 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700386 return false;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400387 };
388 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener);
Jason Monk361915c2017-03-21 20:33:59 -0400389 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700390 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400391
392 dialog.setOnDismissListener(this);
393
394 return dialog;
395 }
396
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800397 private boolean shouldDisplayLockdown() {
398 int userId = getCurrentUser().id;
399 // Lockdown is meaningless without a place to go.
400 if (!mKeyguardManager.isDeviceSecure(userId)) {
401 return false;
402 }
403
404 // Only show the lockdown button if the device isn't locked down (for whatever reason).
405 int state = mLockPatternUtils.getStrongAuthForUser(userId);
406 return (state == STRONG_AUTH_NOT_REQUIRED
407 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
408 }
409
Jason Monk361915c2017-03-21 20:33:59 -0400410 private final class PowerAction extends SinglePressAction implements LongPressAction {
411 private PowerAction() {
412 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400413 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400414 }
415
416 @Override
417 public boolean onLongPress() {
418 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
419 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
420 mWindowManagerFuncs.reboot(true);
421 return true;
422 }
423 return false;
424 }
425
426 @Override
427 public boolean showDuringKeyguard() {
428 return true;
429 }
430
431 @Override
432 public boolean showBeforeProvisioning() {
433 return true;
434 }
435
436 @Override
437 public void onPress() {
438 // shutdown by making sure radio and power are handled accordingly.
439 mWindowManagerFuncs.shutdown();
440 }
441 }
442
443 private final class RestartAction extends SinglePressAction implements LongPressAction {
444 private RestartAction() {
445 super(R.drawable.ic_restart, R.string.global_action_restart);
446 }
447
448 @Override
449 public boolean onLongPress() {
450 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
451 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
452 mWindowManagerFuncs.reboot(true);
453 return true;
454 }
455 return false;
456 }
457
458 @Override
459 public boolean showDuringKeyguard() {
460 return true;
461 }
462
463 @Override
464 public boolean showBeforeProvisioning() {
465 return true;
466 }
467
468 @Override
469 public void onPress() {
470 mWindowManagerFuncs.reboot(false);
471 }
472 }
473
474
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500475 private class ScreenshotAction extends SinglePressAction {
476 public ScreenshotAction() {
477 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
478 }
479
480 @Override
481 public void onPress() {
482 // Add a little delay before executing, to give the
483 // dialog a chance to go away before it takes a
484 // screenshot.
485 // TODO: instead, omit global action dialog layer
486 mHandler.postDelayed(new Runnable() {
487 @Override
488 public void run() {
489 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
490 MetricsLogger.action(mContext,
491 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
492 }
493 }, 500);
494 }
495
496 @Override
497 public boolean showDuringKeyguard() {
498 return true;
499 }
500
501 @Override
502 public boolean showBeforeProvisioning() {
503 return false;
504 }
505 }
506
Jason Monk361915c2017-03-21 20:33:59 -0400507 private class BugReportAction extends SinglePressAction implements LongPressAction {
508
509 public BugReportAction() {
510 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
511 }
512
513 @Override
514 public void onPress() {
515 // don't actually trigger the bugreport if we are running stability
516 // tests via monkey
517 if (ActivityManager.isUserAMonkey()) {
518 return;
519 }
520 // Add a little delay before executing, to give the
521 // dialog a chance to go away before it takes a
522 // screenshot.
523 mHandler.postDelayed(new Runnable() {
524 @Override
525 public void run() {
526 try {
527 // Take an "interactive" bugreport.
528 MetricsLogger.action(mContext,
529 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
530 ActivityManager.getService().requestBugReport(
531 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
532 } catch (RemoteException e) {
533 }
534 }
535 }, 500);
536 }
537
538 @Override
539 public boolean onLongPress() {
540 // don't actually trigger the bugreport if we are running stability
541 // tests via monkey
542 if (ActivityManager.isUserAMonkey()) {
543 return false;
544 }
545 try {
546 // Take a "full" bugreport.
547 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
548 ActivityManager.getService().requestBugReport(
549 ActivityManager.BUGREPORT_OPTION_FULL);
550 } catch (RemoteException e) {
551 }
552 return false;
553 }
554
555 public boolean showDuringKeyguard() {
556 return true;
557 }
558
559 @Override
560 public boolean showBeforeProvisioning() {
561 return false;
562 }
563
564 @Override
565 public String getStatus() {
566 return mContext.getString(
567 R.string.bugreport_status,
568 Build.VERSION.RELEASE,
569 Build.ID);
570 }
571 }
572
Alex Chau04458852017-11-27 18:21:23 +0000573 private final class LogoutAction extends SinglePressAction {
574 private LogoutAction() {
575 super(R.drawable.ic_logout, R.string.global_action_logout);
576 }
577
578 @Override
579 public boolean showDuringKeyguard() {
580 return true;
581 }
582
583 @Override
584 public boolean showBeforeProvisioning() {
585 return false;
586 }
587
588 @Override
589 public void onPress() {
590 // Add a little delay before executing, to give the dialog a chance to go away before
591 // switching user
592 mHandler.postDelayed(() -> {
593 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000594 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000595 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000596 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000597 } catch (RemoteException re) {
598 Log.e(TAG, "Couldn't logout user " + re);
599 }
600 }, 500);
601 }
602 }
603
Jason Monk361915c2017-03-21 20:33:59 -0400604 private Action getSettingsAction() {
605 return new SinglePressAction(R.drawable.ic_settings,
606 R.string.global_action_settings) {
607
608 @Override
609 public void onPress() {
610 Intent intent = new Intent(Settings.ACTION_SETTINGS);
611 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
612 mContext.startActivity(intent);
613 }
614
615 @Override
616 public boolean showDuringKeyguard() {
617 return true;
618 }
619
620 @Override
621 public boolean showBeforeProvisioning() {
622 return true;
623 }
624 };
625 }
626
627 private Action getEmergencyAction() {
628 return new SinglePressAction(R.drawable.emergency_icon,
629 R.string.global_action_emergency) {
630 @Override
631 public void onPress() {
632 mEmergencyAffordanceManager.performEmergencyCall();
633 }
634
635 @Override
636 public boolean showDuringKeyguard() {
637 return true;
638 }
639
640 @Override
641 public boolean showBeforeProvisioning() {
642 return true;
643 }
644 };
645 }
646
647 private Action getAssistAction() {
648 return new SinglePressAction(R.drawable.ic_action_assist_focused,
649 R.string.global_action_assist) {
650 @Override
651 public void onPress() {
652 Intent intent = new Intent(Intent.ACTION_ASSIST);
653 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
654 mContext.startActivity(intent);
655 }
656
657 @Override
658 public boolean showDuringKeyguard() {
659 return true;
660 }
661
662 @Override
663 public boolean showBeforeProvisioning() {
664 return true;
665 }
666 };
667 }
668
669 private Action getVoiceAssistAction() {
670 return new SinglePressAction(R.drawable.ic_voice_search,
671 R.string.global_action_voice_assist) {
672 @Override
673 public void onPress() {
674 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
675 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
676 mContext.startActivity(intent);
677 }
678
679 @Override
680 public boolean showDuringKeyguard() {
681 return true;
682 }
683
684 @Override
685 public boolean showBeforeProvisioning() {
686 return true;
687 }
688 };
689 }
690
691 private Action getLockdownAction() {
Chad Brubaker684d9232018-02-08 15:36:00 -0800692 return new SinglePressAction(com.android.systemui.R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400693 R.string.global_action_lockdown) {
694
695 @Override
696 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700697 new LockPatternUtils(mContext)
698 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
699 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400700 try {
701 WindowManagerGlobal.getWindowManagerService().lockNow(null);
702 } catch (RemoteException e) {
703 Log.e(TAG, "Error while trying to lock device.", e);
704 }
705 }
706
707 @Override
708 public boolean showDuringKeyguard() {
709 return true;
710 }
711
712 @Override
713 public boolean showBeforeProvisioning() {
714 return false;
715 }
716 };
717 }
718
719 private UserInfo getCurrentUser() {
720 try {
721 return ActivityManager.getService().getCurrentUser();
722 } catch (RemoteException re) {
723 return null;
724 }
725 }
726
727 private boolean isCurrentUserOwner() {
728 UserInfo currentUser = getCurrentUser();
729 return currentUser == null || currentUser.isPrimary();
730 }
731
732 private void addUsersToMenu(ArrayList<Action> items) {
733 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
734 if (um.isUserSwitcherEnabled()) {
735 List<UserInfo> users = um.getUsers();
736 UserInfo currentUser = getCurrentUser();
737 for (final UserInfo user : users) {
738 if (user.supportsSwitchToByUser()) {
739 boolean isCurrentUser = currentUser == null
740 ? user.id == 0 : (currentUser.id == user.id);
741 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
742 : null;
743 SinglePressAction switchToUser = new SinglePressAction(
744 R.drawable.ic_menu_cc, icon,
745 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400746 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400747 public void onPress() {
748 try {
749 ActivityManager.getService().switchUser(user.id);
750 } catch (RemoteException re) {
751 Log.e(TAG, "Couldn't switch user " + re);
752 }
753 }
754
755 public boolean showDuringKeyguard() {
756 return true;
757 }
758
759 public boolean showBeforeProvisioning() {
760 return false;
761 }
762 };
763 items.add(switchToUser);
764 }
765 }
766 }
767 }
768
769 private void prepareDialog() {
770 refreshSilentMode();
771 mAirplaneModeOn.updateState(mAirplaneState);
772 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400773 if (mShowSilentToggle) {
774 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
775 mContext.registerReceiver(mRingerModeReceiver, filter);
776 }
777 }
778
779 private void refreshSilentMode() {
780 if (!mHasVibrator) {
781 final boolean silentModeOn =
782 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400783 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400784 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
785 }
786 }
787
788 /** {@inheritDoc} */
789 public void onDismiss(DialogInterface dialog) {
790 mWindowManagerFuncs.onGlobalActionsHidden();
791 if (mShowSilentToggle) {
792 try {
793 mContext.unregisterReceiver(mRingerModeReceiver);
794 } catch (IllegalArgumentException ie) {
795 // ignore this
796 Log.w(TAG, ie);
797 }
798 }
799 }
800
801 /** {@inheritDoc} */
802 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400803 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400804 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400805 dialog.dismiss();
806 }
Jason Monkfd279662017-06-29 19:37:48 -0400807 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400808 }
809
810 /**
811 * The adapter used for the list within the global actions dialog, taking
812 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400813 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
814 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400815 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
816 */
817 private class MyAdapter extends BaseAdapter {
818
819 public int getCount() {
820 int count = 0;
821
822 for (int i = 0; i < mItems.size(); i++) {
823 final Action action = mItems.get(i);
824
825 if (mKeyguardShowing && !action.showDuringKeyguard()) {
826 continue;
827 }
828 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
829 continue;
830 }
831 count++;
832 }
833 return count;
834 }
835
836 @Override
837 public boolean isEnabled(int position) {
838 return getItem(position).isEnabled();
839 }
840
841 @Override
842 public boolean areAllItemsEnabled() {
843 return false;
844 }
845
846 public Action getItem(int position) {
847
848 int filteredPos = 0;
849 for (int i = 0; i < mItems.size(); i++) {
850 final Action action = mItems.get(i);
851 if (mKeyguardShowing && !action.showDuringKeyguard()) {
852 continue;
853 }
854 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
855 continue;
856 }
857 if (filteredPos == position) {
858 return action;
859 }
860 filteredPos++;
861 }
862
863 throw new IllegalArgumentException("position " + position
864 + " out of range of showable actions"
865 + ", filtered count=" + getCount()
866 + ", keyguardshowing=" + mKeyguardShowing
867 + ", provisioned=" + mDeviceProvisioned);
868 }
869
870
871 public long getItemId(int position) {
872 return position;
873 }
874
875 public View getView(int position, View convertView, ViewGroup parent) {
876 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400877 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500878 // Everything but screenshot, the last item, gets white background.
879 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400880 HardwareUiLayout.get(parent).setDivisionView(view);
881 }
882 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400883 }
884 }
885
886 // note: the scheme below made more sense when we were planning on having
887 // 8 different things in the global actions dialog. seems overkill with
888 // only 3 items now, but may as well keep this flexible approach so it will
889 // be easy should someone decide at the last minute to include something
890 // else, such as 'enable wifi', or 'enable bluetooth'
891
892 /**
893 * What each item in the global actions dialog must be able to support.
894 */
895 private interface Action {
896 /**
897 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400898 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400899 */
900 CharSequence getLabelForAccessibility(Context context);
901
902 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
903
904 void onPress();
905
906 /**
907 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400908 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400909 */
910 boolean showDuringKeyguard();
911
912 /**
913 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400914 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400915 */
916 boolean showBeforeProvisioning();
917
918 boolean isEnabled();
919 }
920
921 /**
922 * An action that also supports long press.
923 */
924 private interface LongPressAction extends Action {
925 boolean onLongPress();
926 }
927
928 /**
929 * A single press action maintains no state, just responds to a press
930 * and takes an action.
931 */
932 private static abstract class SinglePressAction implements Action {
933 private final int mIconResId;
934 private final Drawable mIcon;
935 private final int mMessageResId;
936 private final CharSequence mMessage;
937
938 protected SinglePressAction(int iconResId, int messageResId) {
939 mIconResId = iconResId;
940 mMessageResId = messageResId;
941 mMessage = null;
942 mIcon = null;
943 }
944
945 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
946 mIconResId = iconResId;
947 mMessageResId = 0;
948 mMessage = message;
949 mIcon = icon;
950 }
951
952 public boolean isEnabled() {
953 return true;
954 }
955
956 public String getStatus() {
957 return null;
958 }
959
960 abstract public void onPress();
961
962 public CharSequence getLabelForAccessibility(Context context) {
963 if (mMessage != null) {
964 return mMessage;
965 } else {
966 return context.getString(mMessageResId);
967 }
968 }
969
970 public View create(
971 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400972 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
973 false);
Jason Monk361915c2017-03-21 20:33:59 -0400974
975 ImageView icon = (ImageView) v.findViewById(R.id.icon);
976 TextView messageView = (TextView) v.findViewById(R.id.message);
977
978 TextView statusView = (TextView) v.findViewById(R.id.status);
979 final String status = getStatus();
980 if (!TextUtils.isEmpty(status)) {
981 statusView.setText(status);
982 } else {
983 statusView.setVisibility(View.GONE);
984 }
985 if (mIcon != null) {
986 icon.setImageDrawable(mIcon);
987 icon.setScaleType(ScaleType.CENTER_CROP);
988 } else if (mIconResId != 0) {
989 icon.setImageDrawable(context.getDrawable(mIconResId));
990 }
991 if (mMessage != null) {
992 messageView.setText(mMessage);
993 } else {
994 messageView.setText(mMessageResId);
995 }
996
997 return v;
998 }
999 }
1000
1001 /**
1002 * A toggle action knows whether it is on or off, and displays an icon
1003 * and status message accordingly.
1004 */
1005 private static abstract class ToggleAction implements Action {
1006
1007 enum State {
1008 Off(false),
1009 TurningOn(true),
1010 TurningOff(true),
1011 On(false);
1012
1013 private final boolean inTransition;
1014
1015 State(boolean intermediate) {
1016 inTransition = intermediate;
1017 }
1018
1019 public boolean inTransition() {
1020 return inTransition;
1021 }
1022 }
1023
1024 protected State mState = State.Off;
1025
1026 // prefs
1027 protected int mEnabledIconResId;
1028 protected int mDisabledIconResid;
1029 protected int mMessageResId;
1030 protected int mEnabledStatusMessageResId;
1031 protected int mDisabledStatusMessageResId;
1032
1033 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001034 * @param enabledIconResId The icon for when this action is on.
1035 * @param disabledIconResid The icon for when this action is off.
1036 * @param message The general information message, e.g 'Silent Mode'
1037 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001038 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1039 */
1040 public ToggleAction(int enabledIconResId,
1041 int disabledIconResid,
1042 int message,
1043 int enabledStatusMessageResId,
1044 int disabledStatusMessageResId) {
1045 mEnabledIconResId = enabledIconResId;
1046 mDisabledIconResid = disabledIconResid;
1047 mMessageResId = message;
1048 mEnabledStatusMessageResId = enabledStatusMessageResId;
1049 mDisabledStatusMessageResId = disabledStatusMessageResId;
1050 }
1051
1052 /**
1053 * Override to make changes to resource IDs just before creating the
1054 * View.
1055 */
1056 void willCreate() {
1057
1058 }
1059
1060 @Override
1061 public CharSequence getLabelForAccessibility(Context context) {
1062 return context.getString(mMessageResId);
1063 }
1064
1065 public View create(Context context, View convertView, ViewGroup parent,
1066 LayoutInflater inflater) {
1067 willCreate();
1068
1069 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001070 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001071
1072 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1073 TextView messageView = (TextView) v.findViewById(R.id.message);
1074 TextView statusView = (TextView) v.findViewById(R.id.status);
1075 final boolean enabled = isEnabled();
1076
1077 if (messageView != null) {
1078 messageView.setText(mMessageResId);
1079 messageView.setEnabled(enabled);
1080 }
1081
1082 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1083 if (icon != null) {
1084 icon.setImageDrawable(context.getDrawable(
1085 (on ? mEnabledIconResId : mDisabledIconResid)));
1086 icon.setEnabled(enabled);
1087 }
1088
1089 if (statusView != null) {
1090 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1091 statusView.setVisibility(View.VISIBLE);
1092 statusView.setEnabled(enabled);
1093 }
1094 v.setEnabled(enabled);
1095
1096 return v;
1097 }
1098
1099 public final void onPress() {
1100 if (mState.inTransition()) {
1101 Log.w(TAG, "shouldn't be able to toggle when in transition");
1102 return;
1103 }
1104
1105 final boolean nowOn = !(mState == State.On);
1106 onToggle(nowOn);
1107 changeStateFromPress(nowOn);
1108 }
1109
1110 public boolean isEnabled() {
1111 return !mState.inTransition();
1112 }
1113
1114 /**
1115 * Implementations may override this if their state can be in on of the intermediate
1116 * states until some notification is received (e.g airplane mode is 'turning off' until
1117 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001118 *
Jason Monk361915c2017-03-21 20:33:59 -04001119 * @param buttonOn Whether the button was turned on or off
1120 */
1121 protected void changeStateFromPress(boolean buttonOn) {
1122 mState = buttonOn ? State.On : State.Off;
1123 }
1124
1125 abstract void onToggle(boolean on);
1126
1127 public void updateState(State state) {
1128 mState = state;
1129 }
1130 }
1131
1132 private class SilentModeToggleAction extends ToggleAction {
1133 public SilentModeToggleAction() {
1134 super(R.drawable.ic_audio_vol_mute,
1135 R.drawable.ic_audio_vol,
1136 R.string.global_action_toggle_silent_mode,
1137 R.string.global_action_silent_mode_on_status,
1138 R.string.global_action_silent_mode_off_status);
1139 }
1140
1141 void onToggle(boolean on) {
1142 if (on) {
1143 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1144 } else {
1145 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1146 }
1147 }
1148
1149 public boolean showDuringKeyguard() {
1150 return true;
1151 }
1152
1153 public boolean showBeforeProvisioning() {
1154 return false;
1155 }
1156 }
1157
1158 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1159
Jason Monk16fbd9d2017-04-27 14:28:49 -04001160 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001161
1162 private final AudioManager mAudioManager;
1163 private final Handler mHandler;
1164 private final Context mContext;
1165
1166 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1167 mAudioManager = audioManager;
1168 mHandler = handler;
1169 mContext = context;
1170 }
1171
1172 private int ringerModeToIndex(int ringerMode) {
1173 // They just happen to coincide
1174 return ringerMode;
1175 }
1176
1177 private int indexToRingerMode(int index) {
1178 // They just happen to coincide
1179 return index;
1180 }
1181
1182 @Override
1183 public CharSequence getLabelForAccessibility(Context context) {
1184 return null;
1185 }
1186
1187 public View create(Context context, View convertView, ViewGroup parent,
1188 LayoutInflater inflater) {
1189 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1190
1191 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1192 for (int i = 0; i < 3; i++) {
1193 View itemView = v.findViewById(ITEM_IDS[i]);
1194 itemView.setSelected(selectedIndex == i);
1195 // Set up click handler
1196 itemView.setTag(i);
1197 itemView.setOnClickListener(this);
1198 }
1199 return v;
1200 }
1201
1202 public void onPress() {
1203 }
1204
1205 public boolean showDuringKeyguard() {
1206 return true;
1207 }
1208
1209 public boolean showBeforeProvisioning() {
1210 return false;
1211 }
1212
1213 public boolean isEnabled() {
1214 return true;
1215 }
1216
1217 void willCreate() {
1218 }
1219
1220 public void onClick(View v) {
1221 if (!(v.getTag() instanceof Integer)) return;
1222
1223 int index = (Integer) v.getTag();
1224 mAudioManager.setRingerMode(indexToRingerMode(index));
1225 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1226 }
1227 }
1228
1229 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1230 public void onReceive(Context context, Intent intent) {
1231 String action = intent.getAction();
1232 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1233 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1234 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1235 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001236 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001237 }
1238 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1239 // Airplane mode can be changed after ECM exits if airplane toggle button
1240 // is pressed during ECM mode
1241 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1242 mIsWaitingForEcmExit) {
1243 mIsWaitingForEcmExit = false;
1244 changeAirplaneModeSystemSetting(true);
1245 }
1246 }
1247 }
1248 };
1249
1250 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1251 @Override
1252 public void onServiceStateChanged(ServiceState serviceState) {
1253 if (!mHasTelephony) return;
1254 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1255 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1256 mAirplaneModeOn.updateState(mAirplaneState);
1257 mAdapter.notifyDataSetChanged();
1258 }
1259 };
1260
1261 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1262 @Override
1263 public void onReceive(Context context, Intent intent) {
1264 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1265 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1266 }
1267 }
1268 };
1269
1270 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1271 @Override
1272 public void onChange(boolean selfChange) {
1273 onAirplaneModeChanged();
1274 }
1275 };
1276
1277 private static final int MESSAGE_DISMISS = 0;
1278 private static final int MESSAGE_REFRESH = 1;
1279 private static final int MESSAGE_SHOW = 2;
1280 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1281
1282 private Handler mHandler = new Handler() {
1283 public void handleMessage(Message msg) {
1284 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001285 case MESSAGE_DISMISS:
1286 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001287 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1288 mDialog.dismissImmediately();
1289 } else {
1290 mDialog.dismiss();
1291 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001292 mDialog = null;
1293 }
1294 break;
1295 case MESSAGE_REFRESH:
1296 refreshSilentMode();
1297 mAdapter.notifyDataSetChanged();
1298 break;
1299 case MESSAGE_SHOW:
1300 handleShow();
1301 break;
Jason Monk361915c2017-03-21 20:33:59 -04001302 }
1303 }
1304 };
1305
1306 private void onAirplaneModeChanged() {
1307 // Let the service state callbacks handle the state.
1308 if (mHasTelephony) return;
1309
1310 boolean airplaneModeOn = Settings.Global.getInt(
1311 mContext.getContentResolver(),
1312 Settings.Global.AIRPLANE_MODE_ON,
1313 0) == 1;
1314 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1315 mAirplaneModeOn.updateState(mAirplaneState);
1316 }
1317
1318 /**
1319 * Change the airplane mode system setting
1320 */
1321 private void changeAirplaneModeSystemSetting(boolean on) {
1322 Settings.Global.putInt(
1323 mContext.getContentResolver(),
1324 Settings.Global.AIRPLANE_MODE_ON,
1325 on ? 1 : 0);
1326 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1327 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1328 intent.putExtra("state", on);
1329 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1330 if (!mHasTelephony) {
1331 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1332 }
1333 }
1334
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001335 private static final class ActionsDialog extends Dialog implements DialogInterface,
1336 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001337
Jason Monk16fbd9d2017-04-27 14:28:49 -04001338 private final Context mContext;
1339 private final MyAdapter mAdapter;
1340 private final LinearLayout mListView;
1341 private final HardwareUiLayout mHardwareLayout;
1342 private final OnClickListener mClickListener;
1343 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001344 private final GradientDrawable mGradientDrawable;
1345 private final ColorExtractor mColorExtractor;
1346 private boolean mKeyguardShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001347
1348 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
1349 OnItemLongClickListener longClickListener) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001350 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001351 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001352 mAdapter = adapter;
1353 mClickListener = clickListener;
1354 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001355 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001356 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001357
1358 // Window initialization
1359 Window window = getWindow();
1360 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001361 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1362 window.getDecorView();
1363 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1364 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1365 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1366 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1367 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001368 window.addFlags(
1369 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001370 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001371 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001372 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1373 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1374 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1375 window.setBackgroundDrawable(mGradientDrawable);
1376 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1377
Jason Monk16fbd9d2017-04-27 14:28:49 -04001378 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001379 mListView = findViewById(android.R.id.list);
1380 mHardwareLayout = HardwareUiLayout.get(mListView);
1381 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Phil Weaver8583ae82018-02-13 11:01:24 -08001382 setTitle(R.string.global_actions);
Phil Weaver9054e092018-04-27 16:28:50 -07001383 mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1384 @Override
1385 public boolean dispatchPopulateAccessibilityEvent(
1386 View host, AccessibilityEvent event) {
1387 // Populate the title here, just as Activity does
1388 event.getText().add(mContext.getString(R.string.global_actions));
1389 return true;
1390 }
1391 });
Jason Monk361915c2017-03-21 20:33:59 -04001392 }
1393
Jason Monk16fbd9d2017-04-27 14:28:49 -04001394 private void updateList() {
1395 mListView.removeAllViews();
1396 for (int i = 0; i < mAdapter.getCount(); i++) {
1397 View v = mAdapter.getView(i, null, mListView);
1398 final int pos = i;
1399 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1400 v.setOnLongClickListener(view ->
1401 mLongClickListener.onItemLongClick(null, v, pos, 0));
1402 mListView.addView(v);
1403 }
Jason Monk361915c2017-03-21 20:33:59 -04001404 }
1405
1406 @Override
1407 protected void onStart() {
1408 super.setCanceledOnTouchOutside(true);
1409 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001410 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001411
1412 Point displaySize = new Point();
1413 mContext.getDisplay().getRealSize(displaySize);
1414 mColorExtractor.addOnColorsChangedListener(this);
1415 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1416 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1417 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001418 updateColors(colors, false /* animate */);
1419 }
1420
1421 /**
1422 * Updates background and system bars according to current GradientColors.
1423 * @param colors Colors and hints to use.
1424 * @param animate Interpolates gradient if true, just sets otherwise.
1425 */
1426 private void updateColors(GradientColors colors, boolean animate) {
1427 mGradientDrawable.setColors(colors, animate);
1428 View decorView = getWindow().getDecorView();
1429 if (colors.supportsDarkText()) {
1430 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1431 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1432 } else {
1433 decorView.setSystemUiVisibility(0);
1434 }
Jason Monk361915c2017-03-21 20:33:59 -04001435 }
1436
1437 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001438 protected void onStop() {
1439 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001440 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001441 }
1442
1443 @Override
1444 public void show() {
1445 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001446 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001447 mHardwareLayout.setTranslationX(getAnimTranslation());
1448 mHardwareLayout.setAlpha(0);
1449 mHardwareLayout.animate()
1450 .alpha(1)
1451 .translationX(0)
1452 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001453 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001454 .setUpdateListener(animation -> {
1455 int alpha = (int) ((Float) animation.getAnimatedValue()
1456 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1457 mGradientDrawable.setAlpha(alpha);
1458 })
Jason Monka7af3b62017-07-07 11:35:13 -04001459 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001460 .start();
1461 }
1462
1463 @Override
1464 public void dismiss() {
1465 mHardwareLayout.setTranslationX(0);
1466 mHardwareLayout.setAlpha(1);
1467 mHardwareLayout.animate()
1468 .alpha(0)
1469 .translationX(getAnimTranslation())
1470 .setDuration(300)
1471 .withEndAction(() -> super.dismiss())
1472 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001473 .setUpdateListener(animation -> {
1474 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1475 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1476 mGradientDrawable.setAlpha(alpha);
1477 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001478 .start();
1479 }
1480
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001481 void dismissImmediately() {
1482 super.dismiss();
1483 }
1484
Jason Monk16fbd9d2017-04-27 14:28:49 -04001485 private float getAnimTranslation() {
1486 return getContext().getResources().getDimension(
1487 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001488 }
1489
1490 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001491 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001492 if (mKeyguardShowing) {
1493 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001494 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1495 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001496 }
1497 } else {
1498 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001499 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1500 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001501 }
1502 }
1503 }
1504
1505 public void setKeyguardShowing(boolean keyguardShowing) {
1506 mKeyguardShowing = keyguardShowing;
1507 }
Jason Monk361915c2017-03-21 20:33:59 -04001508 }
1509}