blob: c4c1ba703a995fed4ff43be018d4b39034908a27 [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 Roos2f05bb32018-02-19 16:42:27 +010017import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
18
Chad Brubakerf4075fe2018-01-03 13:23:22 -080019import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
20import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070021import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
22
Jason Monk361915c2017-03-21 20:33:59 -040023import android.app.ActivityManager;
24import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080025import android.app.KeyguardManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070026import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000027import android.app.admin.DevicePolicyManager;
Jason Monk361915c2017-03-21 20:33:59 -040028import android.content.BroadcastReceiver;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050029import android.content.ComponentName;
Jason Monk361915c2017-03-21 20:33:59 -040030import android.content.Context;
31import android.content.DialogInterface;
32import android.content.Intent;
33import android.content.IntentFilter;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050034import android.content.ServiceConnection;
Jason Monk361915c2017-03-21 20:33:59 -040035import android.content.pm.UserInfo;
36import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070037import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040038import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.net.ConnectivityManager;
41import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040042import android.os.Handler;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050043import android.os.IBinder;
Jason Monk361915c2017-03-21 20:33:59 -040044import android.os.Message;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050045import android.os.Messenger;
Jason Monk361915c2017-03-21 20:33:59 -040046import android.os.RemoteException;
47import android.os.ServiceManager;
48import android.os.SystemProperties;
49import android.os.UserHandle;
50import android.os.UserManager;
51import android.os.Vibrator;
52import android.provider.Settings;
53import android.service.dreams.DreamService;
54import android.service.dreams.IDreamManager;
55import android.telephony.PhoneStateListener;
56import android.telephony.ServiceState;
57import android.telephony.TelephonyManager;
58import android.text.TextUtils;
59import android.util.ArraySet;
60import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070061import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040062import android.view.LayoutInflater;
63import android.view.View;
64import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070065import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040066import android.view.WindowManager;
67import android.view.WindowManagerGlobal;
68import android.view.accessibility.AccessibilityEvent;
69import android.widget.AdapterView;
Jason Monk16fbd9d2017-04-27 14:28:49 -040070import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.BaseAdapter;
72import android.widget.ImageView;
73import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040074import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040075import android.widget.TextView;
76
Charles He9851a8d2017-10-10 17:31:30 +010077import com.android.internal.R;
78import com.android.internal.colorextraction.ColorExtractor;
79import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070080import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010081import com.android.internal.logging.MetricsLogger;
82import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
83import com.android.internal.telephony.TelephonyIntents;
84import com.android.internal.telephony.TelephonyProperties;
85import com.android.internal.util.EmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050086import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010087import com.android.internal.widget.LockPatternUtils;
88import com.android.systemui.Dependency;
89import com.android.systemui.HardwareUiLayout;
90import com.android.systemui.Interpolators;
91import com.android.systemui.colorextraction.SysuiColorExtractor;
92import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
93import com.android.systemui.statusbar.phone.ScrimController;
Julia Reynolds42411922017-11-08 11:19:09 -050094import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070095
Jason Monk361915c2017-03-21 20:33:59 -040096import java.util.ArrayList;
97import java.util.List;
98
99/**
100 * Helper to show the global actions dialog. Each item is an {@link Action} that
101 * may show depending on whether the keyguard is showing, and whether the device
102 * is provisioned.
103 */
Charles He9851a8d2017-10-10 17:31:30 +0100104class GlobalActionsDialog implements DialogInterface.OnDismissListener,
105 DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -0400106
107 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
108 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
109
110 private static final String TAG = "GlobalActionsDialog";
111
112 private static final boolean SHOW_SILENT_TOGGLE = true;
113
114 /* Valid settings for global actions keys.
115 * see config.xml config_globalActionList */
116 private static final String GLOBAL_ACTION_KEY_POWER = "power";
117 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
118 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
119 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
120 private static final String GLOBAL_ACTION_KEY_USERS = "users";
121 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
122 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
123 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
124 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
125 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000126 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500127 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400128
129 private final Context mContext;
130 private final GlobalActionsManager mWindowManagerFuncs;
131 private final AudioManager mAudioManager;
132 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000133 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800134 private final LockPatternUtils mLockPatternUtils;
135 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400136
137 private ArrayList<Action> mItems;
138 private ActionsDialog mDialog;
139
140 private Action mSilentModeAction;
141 private ToggleAction mAirplaneModeOn;
142
143 private MyAdapter mAdapter;
144
145 private boolean mKeyguardShowing = false;
146 private boolean mDeviceProvisioned = false;
147 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
148 private boolean mIsWaitingForEcmExit = false;
149 private boolean mHasTelephony;
150 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000151 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800152 private boolean mHasLockdownButton;
Jason Monk361915c2017-03-21 20:33:59 -0400153 private final boolean mShowSilentToggle;
154 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500155 private final ScreenshotHelper mScreenshotHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400156
157 /**
158 * @param context everything needs a context :(
159 */
160 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700161 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400162 mWindowManagerFuncs = windowManagerFuncs;
163 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
164 mDreamManager = IDreamManager.Stub.asInterface(
165 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000166 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
167 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800168 mLockPatternUtils = new LockPatternUtils(mContext);
169 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400170
171 // receive broadcasts
172 IntentFilter filter = new IntentFilter();
173 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
174 filter.addAction(Intent.ACTION_SCREEN_OFF);
175 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
176 context.registerReceiver(mBroadcastReceiver, filter);
177
178 ConnectivityManager cm = (ConnectivityManager)
179 context.getSystemService(Context.CONNECTIVITY_SERVICE);
180 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
181
182 // get notified of phone state changes
183 TelephonyManager telephonyManager =
184 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
185 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
186 mContext.getContentResolver().registerContentObserver(
187 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
188 mAirplaneModeObserver);
189 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
190 mHasVibrator = vibrator != null && vibrator.hasVibrator();
191
192 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
193 R.bool.config_useFixedVolume);
194
195 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500196 mScreenshotHelper = new ScreenshotHelper(context);
Jason Monk361915c2017-03-21 20:33:59 -0400197 }
198
199 /**
200 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400201 *
Jason Monk361915c2017-03-21 20:33:59 -0400202 * @param keyguardShowing True if keyguard is showing
203 */
204 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
205 mKeyguardShowing = keyguardShowing;
206 mDeviceProvisioned = isDeviceProvisioned;
207 if (mDialog != null) {
208 mDialog.dismiss();
209 mDialog = null;
210 // Show delayed, so that the dismiss of the previous dialog completes
211 mHandler.sendEmptyMessage(MESSAGE_SHOW);
212 } else {
213 handleShow();
214 }
215 }
216
Charles He9851a8d2017-10-10 17:31:30 +0100217 /**
218 * Dismiss the global actions dialog, if it's currently shown
219 */
220 public void dismissDialog() {
221 mHandler.removeMessages(MESSAGE_DISMISS);
222 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
223 }
224
Jason Monk361915c2017-03-21 20:33:59 -0400225 private void awakenIfNecessary() {
226 if (mDreamManager != null) {
227 try {
228 if (mDreamManager.isDreaming()) {
229 mDreamManager.awaken();
230 }
231 } catch (RemoteException e) {
232 // we tried
233 }
234 }
235 }
236
237 private void handleShow() {
238 awakenIfNecessary();
239 mDialog = createDialog();
240 prepareDialog();
241
242 // If we only have 1 item and it's a simple press action, just do this action.
243 if (mAdapter.getCount() == 1
244 && mAdapter.getItem(0) instanceof SinglePressAction
245 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
246 ((SinglePressAction) mAdapter.getItem(0)).onPress();
247 } else {
248 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
249 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100250 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400251 mDialog.getWindow().setAttributes(attrs);
252 mDialog.show();
253 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400254 }
255 }
256
257 /**
258 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400259 *
Jason Monk361915c2017-03-21 20:33:59 -0400260 * @return A new dialog.
261 */
262 private ActionsDialog createDialog() {
263 // Simple toggle style if there's no vibrator, otherwise use a tri-state
264 if (!mHasVibrator) {
265 mSilentModeAction = new SilentModeToggleAction();
266 } else {
267 mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler);
268 }
269 mAirplaneModeOn = new ToggleAction(
270 R.drawable.ic_lock_airplane_mode,
271 R.drawable.ic_lock_airplane_mode_off,
272 R.string.global_actions_toggle_airplane_mode,
273 R.string.global_actions_airplane_mode_on_status,
274 R.string.global_actions_airplane_mode_off_status) {
275
276 void onToggle(boolean on) {
277 if (mHasTelephony && Boolean.parseBoolean(
278 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
279 mIsWaitingForEcmExit = true;
280 // Launch ECM exit dialog
281 Intent ecmDialogIntent =
282 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
283 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
284 mContext.startActivity(ecmDialogIntent);
285 } else {
286 changeAirplaneModeSystemSetting(on);
287 }
288 }
289
290 @Override
291 protected void changeStateFromPress(boolean buttonOn) {
292 if (!mHasTelephony) return;
293
294 // In ECM mode airplane state cannot be changed
295 if (!(Boolean.parseBoolean(
296 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
297 mState = buttonOn ? State.TurningOn : State.TurningOff;
298 mAirplaneState = mState;
299 }
300 }
301
302 public boolean showDuringKeyguard() {
303 return true;
304 }
305
306 public boolean showBeforeProvisioning() {
307 return false;
308 }
309 };
310 onAirplaneModeChanged();
311
312 mItems = new ArrayList<Action>();
313 String[] defaultActions = mContext.getResources().getStringArray(
314 R.array.config_globalActionsList);
315
316 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000317 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800318 mHasLockdownButton = false;
Jason Monk361915c2017-03-21 20:33:59 -0400319 for (int i = 0; i < defaultActions.length; i++) {
320 String actionKey = defaultActions[i];
321 if (addedKeys.contains(actionKey)) {
322 // If we already have added this, don't add it again.
323 continue;
324 }
325 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
326 mItems.add(new PowerAction());
327 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
328 mItems.add(mAirplaneModeOn);
329 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
330 if (Settings.Global.getInt(mContext.getContentResolver(),
331 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
332 mItems.add(new BugReportAction());
333 }
334 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
335 if (mShowSilentToggle) {
336 mItems.add(mSilentModeAction);
337 }
338 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
339 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
340 addUsersToMenu(mItems);
341 }
342 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
343 mItems.add(getSettingsAction());
344 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700345 if (Settings.Secure.getInt(mContext.getContentResolver(),
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800346 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0) != 0
347 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700348 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800349 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700350 }
Jason Monk361915c2017-03-21 20:33:59 -0400351 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
352 mItems.add(getVoiceAssistAction());
353 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
354 mItems.add(getAssistAction());
355 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
356 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500357 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
358 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000359 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000360 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000361 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
362 mItems.add(new LogoutAction());
363 mHasLogoutButton = true;
364 }
Jason Monk361915c2017-03-21 20:33:59 -0400365 } else {
366 Log.e(TAG, "Invalid global action key " + actionKey);
367 }
368 // Add here so we don't add more than one.
369 addedKeys.add(actionKey);
370 }
371
372 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
373 mItems.add(getEmergencyAction());
374 }
375
376 mAdapter = new MyAdapter();
377
Jason Monk16fbd9d2017-04-27 14:28:49 -0400378 OnItemLongClickListener onItemLongClickListener = new OnItemLongClickListener() {
379 @Override
380 public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
381 long id) {
382 final Action action = mAdapter.getItem(position);
383 if (action instanceof LongPressAction) {
Shunta Satobdb0e492017-04-14 16:01:54 +0900384 mDialog.dismiss();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400385 return ((LongPressAction) action).onLongPress();
386 }
387 return false;
388 }
389 };
390 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener);
Jason Monk361915c2017-03-21 20:33:59 -0400391 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700392 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400393
394 dialog.setOnDismissListener(this);
395
396 return dialog;
397 }
398
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800399 private boolean shouldDisplayLockdown() {
400 int userId = getCurrentUser().id;
401 // Lockdown is meaningless without a place to go.
402 if (!mKeyguardManager.isDeviceSecure(userId)) {
403 return false;
404 }
405
406 // Only show the lockdown button if the device isn't locked down (for whatever reason).
407 int state = mLockPatternUtils.getStrongAuthForUser(userId);
408 return (state == STRONG_AUTH_NOT_REQUIRED
409 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
410 }
411
Jason Monk361915c2017-03-21 20:33:59 -0400412 private final class PowerAction extends SinglePressAction implements LongPressAction {
413 private PowerAction() {
414 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400415 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400416 }
417
418 @Override
419 public boolean onLongPress() {
420 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
421 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
422 mWindowManagerFuncs.reboot(true);
423 return true;
424 }
425 return false;
426 }
427
428 @Override
429 public boolean showDuringKeyguard() {
430 return true;
431 }
432
433 @Override
434 public boolean showBeforeProvisioning() {
435 return true;
436 }
437
438 @Override
439 public void onPress() {
440 // shutdown by making sure radio and power are handled accordingly.
441 mWindowManagerFuncs.shutdown();
442 }
443 }
444
445 private final class RestartAction extends SinglePressAction implements LongPressAction {
446 private RestartAction() {
447 super(R.drawable.ic_restart, R.string.global_action_restart);
448 }
449
450 @Override
451 public boolean onLongPress() {
452 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
453 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
454 mWindowManagerFuncs.reboot(true);
455 return true;
456 }
457 return false;
458 }
459
460 @Override
461 public boolean showDuringKeyguard() {
462 return true;
463 }
464
465 @Override
466 public boolean showBeforeProvisioning() {
467 return true;
468 }
469
470 @Override
471 public void onPress() {
472 mWindowManagerFuncs.reboot(false);
473 }
474 }
475
476
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500477 private class ScreenshotAction extends SinglePressAction {
478 public ScreenshotAction() {
479 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
480 }
481
482 @Override
483 public void onPress() {
484 // Add a little delay before executing, to give the
485 // dialog a chance to go away before it takes a
486 // screenshot.
487 // TODO: instead, omit global action dialog layer
488 mHandler.postDelayed(new Runnable() {
489 @Override
490 public void run() {
491 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
492 MetricsLogger.action(mContext,
493 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
494 }
495 }, 500);
496 }
497
498 @Override
499 public boolean showDuringKeyguard() {
500 return true;
501 }
502
503 @Override
504 public boolean showBeforeProvisioning() {
505 return false;
506 }
507 }
508
Jason Monk361915c2017-03-21 20:33:59 -0400509 private class BugReportAction extends SinglePressAction implements LongPressAction {
510
511 public BugReportAction() {
512 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
513 }
514
515 @Override
516 public void onPress() {
517 // don't actually trigger the bugreport if we are running stability
518 // tests via monkey
519 if (ActivityManager.isUserAMonkey()) {
520 return;
521 }
522 // Add a little delay before executing, to give the
523 // dialog a chance to go away before it takes a
524 // screenshot.
525 mHandler.postDelayed(new Runnable() {
526 @Override
527 public void run() {
528 try {
529 // Take an "interactive" bugreport.
530 MetricsLogger.action(mContext,
531 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
532 ActivityManager.getService().requestBugReport(
533 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
534 } catch (RemoteException e) {
535 }
536 }
537 }, 500);
538 }
539
540 @Override
541 public boolean onLongPress() {
542 // don't actually trigger the bugreport if we are running stability
543 // tests via monkey
544 if (ActivityManager.isUserAMonkey()) {
545 return false;
546 }
547 try {
548 // Take a "full" bugreport.
549 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
550 ActivityManager.getService().requestBugReport(
551 ActivityManager.BUGREPORT_OPTION_FULL);
552 } catch (RemoteException e) {
553 }
554 return false;
555 }
556
557 public boolean showDuringKeyguard() {
558 return true;
559 }
560
561 @Override
562 public boolean showBeforeProvisioning() {
563 return false;
564 }
565
566 @Override
567 public String getStatus() {
568 return mContext.getString(
569 R.string.bugreport_status,
570 Build.VERSION.RELEASE,
571 Build.ID);
572 }
573 }
574
Alex Chau04458852017-11-27 18:21:23 +0000575 private final class LogoutAction extends SinglePressAction {
576 private LogoutAction() {
577 super(R.drawable.ic_logout, R.string.global_action_logout);
578 }
579
580 @Override
581 public boolean showDuringKeyguard() {
582 return true;
583 }
584
585 @Override
586 public boolean showBeforeProvisioning() {
587 return false;
588 }
589
590 @Override
591 public void onPress() {
592 // Add a little delay before executing, to give the dialog a chance to go away before
593 // switching user
594 mHandler.postDelayed(() -> {
595 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000596 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000597 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000598 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000599 } catch (RemoteException re) {
600 Log.e(TAG, "Couldn't logout user " + re);
601 }
602 }, 500);
603 }
604 }
605
Jason Monk361915c2017-03-21 20:33:59 -0400606 private Action getSettingsAction() {
607 return new SinglePressAction(R.drawable.ic_settings,
608 R.string.global_action_settings) {
609
610 @Override
611 public void onPress() {
612 Intent intent = new Intent(Settings.ACTION_SETTINGS);
613 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
614 mContext.startActivity(intent);
615 }
616
617 @Override
618 public boolean showDuringKeyguard() {
619 return true;
620 }
621
622 @Override
623 public boolean showBeforeProvisioning() {
624 return true;
625 }
626 };
627 }
628
629 private Action getEmergencyAction() {
630 return new SinglePressAction(R.drawable.emergency_icon,
631 R.string.global_action_emergency) {
632 @Override
633 public void onPress() {
634 mEmergencyAffordanceManager.performEmergencyCall();
635 }
636
637 @Override
638 public boolean showDuringKeyguard() {
639 return true;
640 }
641
642 @Override
643 public boolean showBeforeProvisioning() {
644 return true;
645 }
646 };
647 }
648
649 private Action getAssistAction() {
650 return new SinglePressAction(R.drawable.ic_action_assist_focused,
651 R.string.global_action_assist) {
652 @Override
653 public void onPress() {
654 Intent intent = new Intent(Intent.ACTION_ASSIST);
655 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
656 mContext.startActivity(intent);
657 }
658
659 @Override
660 public boolean showDuringKeyguard() {
661 return true;
662 }
663
664 @Override
665 public boolean showBeforeProvisioning() {
666 return true;
667 }
668 };
669 }
670
671 private Action getVoiceAssistAction() {
672 return new SinglePressAction(R.drawable.ic_voice_search,
673 R.string.global_action_voice_assist) {
674 @Override
675 public void onPress() {
676 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
677 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
678 mContext.startActivity(intent);
679 }
680
681 @Override
682 public boolean showDuringKeyguard() {
683 return true;
684 }
685
686 @Override
687 public boolean showBeforeProvisioning() {
688 return true;
689 }
690 };
691 }
692
693 private Action getLockdownAction() {
Chad Brubaker684d9232018-02-08 15:36:00 -0800694 return new SinglePressAction(com.android.systemui.R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400695 R.string.global_action_lockdown) {
696
697 @Override
698 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700699 new LockPatternUtils(mContext)
700 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
701 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400702 try {
703 WindowManagerGlobal.getWindowManagerService().lockNow(null);
704 } catch (RemoteException e) {
705 Log.e(TAG, "Error while trying to lock device.", e);
706 }
707 }
708
709 @Override
710 public boolean showDuringKeyguard() {
711 return true;
712 }
713
714 @Override
715 public boolean showBeforeProvisioning() {
716 return false;
717 }
718 };
719 }
720
721 private UserInfo getCurrentUser() {
722 try {
723 return ActivityManager.getService().getCurrentUser();
724 } catch (RemoteException re) {
725 return null;
726 }
727 }
728
729 private boolean isCurrentUserOwner() {
730 UserInfo currentUser = getCurrentUser();
731 return currentUser == null || currentUser.isPrimary();
732 }
733
734 private void addUsersToMenu(ArrayList<Action> items) {
735 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
736 if (um.isUserSwitcherEnabled()) {
737 List<UserInfo> users = um.getUsers();
738 UserInfo currentUser = getCurrentUser();
739 for (final UserInfo user : users) {
740 if (user.supportsSwitchToByUser()) {
741 boolean isCurrentUser = currentUser == null
742 ? user.id == 0 : (currentUser.id == user.id);
743 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
744 : null;
745 SinglePressAction switchToUser = new SinglePressAction(
746 R.drawable.ic_menu_cc, icon,
747 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400748 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400749 public void onPress() {
750 try {
751 ActivityManager.getService().switchUser(user.id);
752 } catch (RemoteException re) {
753 Log.e(TAG, "Couldn't switch user " + re);
754 }
755 }
756
757 public boolean showDuringKeyguard() {
758 return true;
759 }
760
761 public boolean showBeforeProvisioning() {
762 return false;
763 }
764 };
765 items.add(switchToUser);
766 }
767 }
768 }
769 }
770
771 private void prepareDialog() {
772 refreshSilentMode();
773 mAirplaneModeOn.updateState(mAirplaneState);
774 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400775 if (mShowSilentToggle) {
776 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
777 mContext.registerReceiver(mRingerModeReceiver, filter);
778 }
779 }
780
781 private void refreshSilentMode() {
782 if (!mHasVibrator) {
783 final boolean silentModeOn =
784 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400785 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400786 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
787 }
788 }
789
790 /** {@inheritDoc} */
791 public void onDismiss(DialogInterface dialog) {
792 mWindowManagerFuncs.onGlobalActionsHidden();
793 if (mShowSilentToggle) {
794 try {
795 mContext.unregisterReceiver(mRingerModeReceiver);
796 } catch (IllegalArgumentException ie) {
797 // ignore this
798 Log.w(TAG, ie);
799 }
800 }
801 }
802
803 /** {@inheritDoc} */
804 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400805 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400806 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400807 dialog.dismiss();
808 }
Jason Monkfd279662017-06-29 19:37:48 -0400809 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400810 }
811
812 /**
813 * The adapter used for the list within the global actions dialog, taking
814 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400815 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
816 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400817 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
818 */
819 private class MyAdapter extends BaseAdapter {
820
821 public int getCount() {
822 int count = 0;
823
824 for (int i = 0; i < mItems.size(); i++) {
825 final Action action = mItems.get(i);
826
827 if (mKeyguardShowing && !action.showDuringKeyguard()) {
828 continue;
829 }
830 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
831 continue;
832 }
833 count++;
834 }
835 return count;
836 }
837
838 @Override
839 public boolean isEnabled(int position) {
840 return getItem(position).isEnabled();
841 }
842
843 @Override
844 public boolean areAllItemsEnabled() {
845 return false;
846 }
847
848 public Action getItem(int position) {
849
850 int filteredPos = 0;
851 for (int i = 0; i < mItems.size(); i++) {
852 final Action action = mItems.get(i);
853 if (mKeyguardShowing && !action.showDuringKeyguard()) {
854 continue;
855 }
856 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
857 continue;
858 }
859 if (filteredPos == position) {
860 return action;
861 }
862 filteredPos++;
863 }
864
865 throw new IllegalArgumentException("position " + position
866 + " out of range of showable actions"
867 + ", filtered count=" + getCount()
868 + ", keyguardshowing=" + mKeyguardShowing
869 + ", provisioned=" + mDeviceProvisioned);
870 }
871
872
873 public long getItemId(int position) {
874 return position;
875 }
876
877 public View getView(int position, View convertView, ViewGroup parent) {
878 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400879 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800880 // Power off, restart, logout (if present) and lockdown (if present) should be in white
881 // background. Set the division based on which buttons are currently being displayed.
882 if (position == 2 + (mHasLogoutButton ? 1 : 0) + (mHasLockdownButton ? 1 : 0)) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400883 HardwareUiLayout.get(parent).setDivisionView(view);
884 }
885 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400886 }
887 }
888
889 // note: the scheme below made more sense when we were planning on having
890 // 8 different things in the global actions dialog. seems overkill with
891 // only 3 items now, but may as well keep this flexible approach so it will
892 // be easy should someone decide at the last minute to include something
893 // else, such as 'enable wifi', or 'enable bluetooth'
894
895 /**
896 * What each item in the global actions dialog must be able to support.
897 */
898 private interface Action {
899 /**
900 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400901 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400902 */
903 CharSequence getLabelForAccessibility(Context context);
904
905 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
906
907 void onPress();
908
909 /**
910 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400911 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400912 */
913 boolean showDuringKeyguard();
914
915 /**
916 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400917 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400918 */
919 boolean showBeforeProvisioning();
920
921 boolean isEnabled();
922 }
923
924 /**
925 * An action that also supports long press.
926 */
927 private interface LongPressAction extends Action {
928 boolean onLongPress();
929 }
930
931 /**
932 * A single press action maintains no state, just responds to a press
933 * and takes an action.
934 */
935 private static abstract class SinglePressAction implements Action {
936 private final int mIconResId;
937 private final Drawable mIcon;
938 private final int mMessageResId;
939 private final CharSequence mMessage;
940
941 protected SinglePressAction(int iconResId, int messageResId) {
942 mIconResId = iconResId;
943 mMessageResId = messageResId;
944 mMessage = null;
945 mIcon = null;
946 }
947
948 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
949 mIconResId = iconResId;
950 mMessageResId = 0;
951 mMessage = message;
952 mIcon = icon;
953 }
954
955 public boolean isEnabled() {
956 return true;
957 }
958
959 public String getStatus() {
960 return null;
961 }
962
963 abstract public void onPress();
964
965 public CharSequence getLabelForAccessibility(Context context) {
966 if (mMessage != null) {
967 return mMessage;
968 } else {
969 return context.getString(mMessageResId);
970 }
971 }
972
973 public View create(
974 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400975 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
976 false);
Jason Monk361915c2017-03-21 20:33:59 -0400977
978 ImageView icon = (ImageView) v.findViewById(R.id.icon);
979 TextView messageView = (TextView) v.findViewById(R.id.message);
980
981 TextView statusView = (TextView) v.findViewById(R.id.status);
982 final String status = getStatus();
983 if (!TextUtils.isEmpty(status)) {
984 statusView.setText(status);
985 } else {
986 statusView.setVisibility(View.GONE);
987 }
988 if (mIcon != null) {
989 icon.setImageDrawable(mIcon);
990 icon.setScaleType(ScaleType.CENTER_CROP);
991 } else if (mIconResId != 0) {
992 icon.setImageDrawable(context.getDrawable(mIconResId));
993 }
994 if (mMessage != null) {
995 messageView.setText(mMessage);
996 } else {
997 messageView.setText(mMessageResId);
998 }
999
1000 return v;
1001 }
1002 }
1003
1004 /**
1005 * A toggle action knows whether it is on or off, and displays an icon
1006 * and status message accordingly.
1007 */
1008 private static abstract class ToggleAction implements Action {
1009
1010 enum State {
1011 Off(false),
1012 TurningOn(true),
1013 TurningOff(true),
1014 On(false);
1015
1016 private final boolean inTransition;
1017
1018 State(boolean intermediate) {
1019 inTransition = intermediate;
1020 }
1021
1022 public boolean inTransition() {
1023 return inTransition;
1024 }
1025 }
1026
1027 protected State mState = State.Off;
1028
1029 // prefs
1030 protected int mEnabledIconResId;
1031 protected int mDisabledIconResid;
1032 protected int mMessageResId;
1033 protected int mEnabledStatusMessageResId;
1034 protected int mDisabledStatusMessageResId;
1035
1036 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001037 * @param enabledIconResId The icon for when this action is on.
1038 * @param disabledIconResid The icon for when this action is off.
1039 * @param message The general information message, e.g 'Silent Mode'
1040 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001041 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1042 */
1043 public ToggleAction(int enabledIconResId,
1044 int disabledIconResid,
1045 int message,
1046 int enabledStatusMessageResId,
1047 int disabledStatusMessageResId) {
1048 mEnabledIconResId = enabledIconResId;
1049 mDisabledIconResid = disabledIconResid;
1050 mMessageResId = message;
1051 mEnabledStatusMessageResId = enabledStatusMessageResId;
1052 mDisabledStatusMessageResId = disabledStatusMessageResId;
1053 }
1054
1055 /**
1056 * Override to make changes to resource IDs just before creating the
1057 * View.
1058 */
1059 void willCreate() {
1060
1061 }
1062
1063 @Override
1064 public CharSequence getLabelForAccessibility(Context context) {
1065 return context.getString(mMessageResId);
1066 }
1067
1068 public View create(Context context, View convertView, ViewGroup parent,
1069 LayoutInflater inflater) {
1070 willCreate();
1071
1072 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001073 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001074
1075 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1076 TextView messageView = (TextView) v.findViewById(R.id.message);
1077 TextView statusView = (TextView) v.findViewById(R.id.status);
1078 final boolean enabled = isEnabled();
1079
1080 if (messageView != null) {
1081 messageView.setText(mMessageResId);
1082 messageView.setEnabled(enabled);
1083 }
1084
1085 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1086 if (icon != null) {
1087 icon.setImageDrawable(context.getDrawable(
1088 (on ? mEnabledIconResId : mDisabledIconResid)));
1089 icon.setEnabled(enabled);
1090 }
1091
1092 if (statusView != null) {
1093 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1094 statusView.setVisibility(View.VISIBLE);
1095 statusView.setEnabled(enabled);
1096 }
1097 v.setEnabled(enabled);
1098
1099 return v;
1100 }
1101
1102 public final void onPress() {
1103 if (mState.inTransition()) {
1104 Log.w(TAG, "shouldn't be able to toggle when in transition");
1105 return;
1106 }
1107
1108 final boolean nowOn = !(mState == State.On);
1109 onToggle(nowOn);
1110 changeStateFromPress(nowOn);
1111 }
1112
1113 public boolean isEnabled() {
1114 return !mState.inTransition();
1115 }
1116
1117 /**
1118 * Implementations may override this if their state can be in on of the intermediate
1119 * states until some notification is received (e.g airplane mode is 'turning off' until
1120 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001121 *
Jason Monk361915c2017-03-21 20:33:59 -04001122 * @param buttonOn Whether the button was turned on or off
1123 */
1124 protected void changeStateFromPress(boolean buttonOn) {
1125 mState = buttonOn ? State.On : State.Off;
1126 }
1127
1128 abstract void onToggle(boolean on);
1129
1130 public void updateState(State state) {
1131 mState = state;
1132 }
1133 }
1134
1135 private class SilentModeToggleAction extends ToggleAction {
1136 public SilentModeToggleAction() {
1137 super(R.drawable.ic_audio_vol_mute,
1138 R.drawable.ic_audio_vol,
1139 R.string.global_action_toggle_silent_mode,
1140 R.string.global_action_silent_mode_on_status,
1141 R.string.global_action_silent_mode_off_status);
1142 }
1143
1144 void onToggle(boolean on) {
1145 if (on) {
1146 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1147 } else {
1148 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1149 }
1150 }
1151
1152 public boolean showDuringKeyguard() {
1153 return true;
1154 }
1155
1156 public boolean showBeforeProvisioning() {
1157 return false;
1158 }
1159 }
1160
1161 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1162
Jason Monk16fbd9d2017-04-27 14:28:49 -04001163 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001164
1165 private final AudioManager mAudioManager;
1166 private final Handler mHandler;
1167 private final Context mContext;
1168
1169 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1170 mAudioManager = audioManager;
1171 mHandler = handler;
1172 mContext = context;
1173 }
1174
1175 private int ringerModeToIndex(int ringerMode) {
1176 // They just happen to coincide
1177 return ringerMode;
1178 }
1179
1180 private int indexToRingerMode(int index) {
1181 // They just happen to coincide
1182 return index;
1183 }
1184
1185 @Override
1186 public CharSequence getLabelForAccessibility(Context context) {
1187 return null;
1188 }
1189
1190 public View create(Context context, View convertView, ViewGroup parent,
1191 LayoutInflater inflater) {
1192 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1193
1194 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1195 for (int i = 0; i < 3; i++) {
1196 View itemView = v.findViewById(ITEM_IDS[i]);
1197 itemView.setSelected(selectedIndex == i);
1198 // Set up click handler
1199 itemView.setTag(i);
1200 itemView.setOnClickListener(this);
1201 }
1202 return v;
1203 }
1204
1205 public void onPress() {
1206 }
1207
1208 public boolean showDuringKeyguard() {
1209 return true;
1210 }
1211
1212 public boolean showBeforeProvisioning() {
1213 return false;
1214 }
1215
1216 public boolean isEnabled() {
1217 return true;
1218 }
1219
1220 void willCreate() {
1221 }
1222
1223 public void onClick(View v) {
1224 if (!(v.getTag() instanceof Integer)) return;
1225
1226 int index = (Integer) v.getTag();
1227 mAudioManager.setRingerMode(indexToRingerMode(index));
1228 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1229 }
1230 }
1231
1232 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1233 public void onReceive(Context context, Intent intent) {
1234 String action = intent.getAction();
1235 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1236 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1237 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1238 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
1239 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
1240 }
1241 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1242 // Airplane mode can be changed after ECM exits if airplane toggle button
1243 // is pressed during ECM mode
1244 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1245 mIsWaitingForEcmExit) {
1246 mIsWaitingForEcmExit = false;
1247 changeAirplaneModeSystemSetting(true);
1248 }
1249 }
1250 }
1251 };
1252
1253 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1254 @Override
1255 public void onServiceStateChanged(ServiceState serviceState) {
1256 if (!mHasTelephony) return;
1257 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1258 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1259 mAirplaneModeOn.updateState(mAirplaneState);
1260 mAdapter.notifyDataSetChanged();
1261 }
1262 };
1263
1264 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1265 @Override
1266 public void onReceive(Context context, Intent intent) {
1267 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1268 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1269 }
1270 }
1271 };
1272
1273 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1274 @Override
1275 public void onChange(boolean selfChange) {
1276 onAirplaneModeChanged();
1277 }
1278 };
1279
1280 private static final int MESSAGE_DISMISS = 0;
1281 private static final int MESSAGE_REFRESH = 1;
1282 private static final int MESSAGE_SHOW = 2;
1283 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1284
1285 private Handler mHandler = new Handler() {
1286 public void handleMessage(Message msg) {
1287 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001288 case MESSAGE_DISMISS:
1289 if (mDialog != null) {
1290 mDialog.dismiss();
1291 mDialog = null;
1292 }
1293 break;
1294 case MESSAGE_REFRESH:
1295 refreshSilentMode();
1296 mAdapter.notifyDataSetChanged();
1297 break;
1298 case MESSAGE_SHOW:
1299 handleShow();
1300 break;
Jason Monk361915c2017-03-21 20:33:59 -04001301 }
1302 }
1303 };
1304
1305 private void onAirplaneModeChanged() {
1306 // Let the service state callbacks handle the state.
1307 if (mHasTelephony) return;
1308
1309 boolean airplaneModeOn = Settings.Global.getInt(
1310 mContext.getContentResolver(),
1311 Settings.Global.AIRPLANE_MODE_ON,
1312 0) == 1;
1313 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1314 mAirplaneModeOn.updateState(mAirplaneState);
1315 }
1316
1317 /**
1318 * Change the airplane mode system setting
1319 */
1320 private void changeAirplaneModeSystemSetting(boolean on) {
1321 Settings.Global.putInt(
1322 mContext.getContentResolver(),
1323 Settings.Global.AIRPLANE_MODE_ON,
1324 on ? 1 : 0);
1325 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1326 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1327 intent.putExtra("state", on);
1328 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1329 if (!mHasTelephony) {
1330 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1331 }
1332 }
1333
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001334 private static final class ActionsDialog extends Dialog implements DialogInterface,
1335 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001336
Jason Monk16fbd9d2017-04-27 14:28:49 -04001337 private final Context mContext;
1338 private final MyAdapter mAdapter;
1339 private final LinearLayout mListView;
1340 private final HardwareUiLayout mHardwareLayout;
1341 private final OnClickListener mClickListener;
1342 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001343 private final GradientDrawable mGradientDrawable;
1344 private final ColorExtractor mColorExtractor;
1345 private boolean mKeyguardShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001346
1347 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
1348 OnItemLongClickListener longClickListener) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001349 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001350 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001351 mAdapter = adapter;
1352 mClickListener = clickListener;
1353 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001354 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001355 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001356
1357 // Window initialization
1358 Window window = getWindow();
1359 window.requestFeature(Window.FEATURE_NO_TITLE);
1360 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND
1361 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001362 window.addFlags(
1363 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001364 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
1365 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1366 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1367 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1368 window.setBackgroundDrawable(mGradientDrawable);
1369 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1370
Jason Monk16fbd9d2017-04-27 14:28:49 -04001371 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001372 mListView = findViewById(android.R.id.list);
1373 mHardwareLayout = HardwareUiLayout.get(mListView);
1374 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Phil Weaver8583ae82018-02-13 11:01:24 -08001375 setTitle(R.string.global_actions);
Jason Monk361915c2017-03-21 20:33:59 -04001376 }
1377
Jason Monk16fbd9d2017-04-27 14:28:49 -04001378 private void updateList() {
1379 mListView.removeAllViews();
1380 for (int i = 0; i < mAdapter.getCount(); i++) {
1381 View v = mAdapter.getView(i, null, mListView);
1382 final int pos = i;
1383 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1384 v.setOnLongClickListener(view ->
1385 mLongClickListener.onItemLongClick(null, v, pos, 0));
1386 mListView.addView(v);
1387 }
Jason Monk361915c2017-03-21 20:33:59 -04001388 }
1389
1390 @Override
1391 protected void onStart() {
1392 super.setCanceledOnTouchOutside(true);
1393 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001394 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001395
1396 Point displaySize = new Point();
1397 mContext.getDisplay().getRealSize(displaySize);
1398 mColorExtractor.addOnColorsChangedListener(this);
1399 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1400 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1401 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001402 updateColors(colors, false /* animate */);
1403 }
1404
1405 /**
1406 * Updates background and system bars according to current GradientColors.
1407 * @param colors Colors and hints to use.
1408 * @param animate Interpolates gradient if true, just sets otherwise.
1409 */
1410 private void updateColors(GradientColors colors, boolean animate) {
1411 mGradientDrawable.setColors(colors, animate);
1412 View decorView = getWindow().getDecorView();
1413 if (colors.supportsDarkText()) {
1414 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1415 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1416 } else {
1417 decorView.setSystemUiVisibility(0);
1418 }
Jason Monk361915c2017-03-21 20:33:59 -04001419 }
1420
1421 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001422 protected void onStop() {
1423 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001424 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001425 }
1426
1427 @Override
1428 public void show() {
1429 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001430 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001431 mHardwareLayout.setTranslationX(getAnimTranslation());
1432 mHardwareLayout.setAlpha(0);
1433 mHardwareLayout.animate()
1434 .alpha(1)
1435 .translationX(0)
1436 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001437 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001438 .setUpdateListener(animation -> {
1439 int alpha = (int) ((Float) animation.getAnimatedValue()
1440 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1441 mGradientDrawable.setAlpha(alpha);
1442 })
Jason Monka7af3b62017-07-07 11:35:13 -04001443 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001444 .start();
1445 }
1446
1447 @Override
1448 public void dismiss() {
1449 mHardwareLayout.setTranslationX(0);
1450 mHardwareLayout.setAlpha(1);
1451 mHardwareLayout.animate()
1452 .alpha(0)
1453 .translationX(getAnimTranslation())
1454 .setDuration(300)
1455 .withEndAction(() -> super.dismiss())
1456 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001457 .setUpdateListener(animation -> {
1458 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1459 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1460 mGradientDrawable.setAlpha(alpha);
1461 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001462 .start();
1463 }
1464
1465 private float getAnimTranslation() {
1466 return getContext().getResources().getDimension(
1467 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001468 }
1469
1470 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001471 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001472 if (mKeyguardShowing) {
1473 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001474 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1475 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001476 }
1477 } else {
1478 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001479 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1480 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001481 }
1482 }
1483 }
1484
1485 public void setKeyguardShowing(boolean keyguardShowing) {
1486 mKeyguardShowing = keyguardShowing;
1487 }
Jason Monk361915c2017-03-21 20:33:59 -04001488 }
1489}