blob: 897ab88215ef672dc473659b2d28ccb7c25b2919 [file] [log] [blame]
Jason Monk361915c2017-03-21 20:33:59 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.globalactions;
16
Adrian Roosedfab3b2018-03-08 18:39:20 +010017import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
Adrian Roos2f05bb32018-02-19 16:42:27 +010018import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
19
Chad Brubakerf4075fe2018-01-03 13:23:22 -080020import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
21import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -070022import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
23
Jason Monk361915c2017-03-21 20:33:59 -040024import android.app.ActivityManager;
25import android.app.Dialog;
Chad Brubakerf4075fe2018-01-03 13:23:22 -080026import android.app.KeyguardManager;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070027import android.app.WallpaperManager;
Alex Chau04458852017-11-27 18:21:23 +000028import android.app.admin.DevicePolicyManager;
Pavel Grafov059021b2018-05-02 13:44:46 +010029import android.app.trust.TrustManager;
Jason Monk361915c2017-03-21 20:33:59 -040030import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.Intent;
34import android.content.IntentFilter;
35import android.content.pm.UserInfo;
36import android.database.ContentObserver;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070037import android.graphics.Point;
Jason Monk361915c2017-03-21 20:33:59 -040038import android.graphics.drawable.Drawable;
39import android.media.AudioManager;
40import android.net.ConnectivityManager;
41import android.os.Build;
Jason Monk361915c2017-03-21 20:33:59 -040042import android.os.Handler;
43import android.os.Message;
44import android.os.RemoteException;
45import android.os.ServiceManager;
46import android.os.SystemProperties;
47import android.os.UserHandle;
48import android.os.UserManager;
49import android.os.Vibrator;
50import android.provider.Settings;
51import android.service.dreams.DreamService;
52import android.service.dreams.IDreamManager;
53import android.telephony.PhoneStateListener;
54import android.telephony.ServiceState;
55import android.telephony.TelephonyManager;
56import android.text.TextUtils;
57import android.util.ArraySet;
58import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070059import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040060import android.view.LayoutInflater;
61import android.view.View;
62import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070063import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040064import android.view.WindowManager;
65import android.view.WindowManagerGlobal;
66import android.view.accessibility.AccessibilityEvent;
Jason Monk16fbd9d2017-04-27 14:28:49 -040067import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040068import android.widget.BaseAdapter;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +080069import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040070import android.widget.ImageView;
71import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040072import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040073import android.widget.TextView;
74
Charles He9851a8d2017-10-10 17:31:30 +010075import com.android.internal.R;
76import com.android.internal.colorextraction.ColorExtractor;
77import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070078import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010079import com.android.internal.logging.MetricsLogger;
80import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
81import com.android.internal.telephony.TelephonyIntents;
82import com.android.internal.telephony.TelephonyProperties;
83import com.android.internal.util.EmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050084import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010085import com.android.internal.widget.LockPatternUtils;
86import com.android.systemui.Dependency;
87import com.android.systemui.HardwareUiLayout;
88import com.android.systemui.Interpolators;
89import com.android.systemui.colorextraction.SysuiColorExtractor;
90import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
91import com.android.systemui.statusbar.phone.ScrimController;
Shaotang Lif9a69e22018-07-04 14:18:40 +080092import com.android.systemui.util.EmergencyDialerConstants;
Julia Reynolds42411922017-11-08 11:19:09 -050093import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070094
Jason Monk361915c2017-03-21 20:33:59 -040095import java.util.ArrayList;
96import java.util.List;
97
98/**
99 * Helper to show the global actions dialog. Each item is an {@link Action} that
100 * may show depending on whether the keyguard is showing, and whether the device
101 * is provisioned.
102 */
Charles He9851a8d2017-10-10 17:31:30 +0100103class GlobalActionsDialog implements DialogInterface.OnDismissListener,
Shaotang Li8af3a242018-08-02 11:18:00 +0800104 DialogInterface.OnClickListener, DialogInterface.OnShowListener {
Jason Monk361915c2017-03-21 20:33:59 -0400105
106 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
107 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700108 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400109
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";
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800127 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500128 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400129
130 private final Context mContext;
131 private final GlobalActionsManager mWindowManagerFuncs;
132 private final AudioManager mAudioManager;
133 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000134 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800135 private final LockPatternUtils mLockPatternUtils;
136 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400137
138 private ArrayList<Action> mItems;
139 private ActionsDialog mDialog;
140
141 private Action mSilentModeAction;
142 private ToggleAction mAirplaneModeOn;
143
144 private MyAdapter mAdapter;
145
146 private boolean mKeyguardShowing = false;
147 private boolean mDeviceProvisioned = false;
148 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
149 private boolean mIsWaitingForEcmExit = false;
150 private boolean mHasTelephony;
151 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000152 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800153 private boolean mHasLockdownButton;
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800154 private boolean mSeparatedEmergencyButtonEnabled;
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;
yuanjiahsu2c69b0b2018-11-16 21:37:33 +0800321 mSeparatedEmergencyButtonEnabled = true;
Jason Monk361915c2017-03-21 20:33:59 -0400322 for (int i = 0; i < defaultActions.length; i++) {
323 String actionKey = defaultActions[i];
324 if (addedKeys.contains(actionKey)) {
325 // If we already have added this, don't add it again.
326 continue;
327 }
328 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
329 mItems.add(new PowerAction());
330 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
331 mItems.add(mAirplaneModeOn);
332 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
333 if (Settings.Global.getInt(mContext.getContentResolver(),
334 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
335 mItems.add(new BugReportAction());
336 }
337 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
338 if (mShowSilentToggle) {
339 mItems.add(mSilentModeAction);
340 }
341 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
342 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
343 addUsersToMenu(mItems);
344 }
345 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
346 mItems.add(getSettingsAction());
347 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700348 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
349 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800350 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700351 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800352 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700353 }
Jason Monk361915c2017-03-21 20:33:59 -0400354 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
355 mItems.add(getVoiceAssistAction());
356 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
357 mItems.add(getAssistAction());
358 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
359 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500360 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
361 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000362 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000363 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000364 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
365 mItems.add(new LogoutAction());
366 mHasLogoutButton = true;
367 }
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800368 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
369 if (mSeparatedEmergencyButtonEnabled
370 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
371 mItems.add(new EmergencyDialerAction());
372 }
Jason Monk361915c2017-03-21 20:33:59 -0400373 } else {
374 Log.e(TAG, "Invalid global action key " + actionKey);
375 }
376 // Add here so we don't add more than one.
377 addedKeys.add(actionKey);
378 }
379
380 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
381 mItems.add(getEmergencyAction());
382 }
383
384 mAdapter = new MyAdapter();
385
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700386 OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> {
387 final Action action = mAdapter.getItem(position);
388 if (action instanceof LongPressAction) {
389 mDialog.dismiss();
390 return ((LongPressAction) action).onLongPress();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400391 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700392 return false;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400393 };
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +0800394 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener,
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800395 mSeparatedEmergencyButtonEnabled);
Jason Monk361915c2017-03-21 20:33:59 -0400396 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700397 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400398
399 dialog.setOnDismissListener(this);
Shaotang Li8af3a242018-08-02 11:18:00 +0800400 dialog.setOnShowListener(this);
Jason Monk361915c2017-03-21 20:33:59 -0400401
402 return dialog;
403 }
404
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800405 private boolean shouldDisplayLockdown() {
406 int userId = getCurrentUser().id;
407 // Lockdown is meaningless without a place to go.
408 if (!mKeyguardManager.isDeviceSecure(userId)) {
409 return false;
410 }
411
412 // Only show the lockdown button if the device isn't locked down (for whatever reason).
413 int state = mLockPatternUtils.getStrongAuthForUser(userId);
414 return (state == STRONG_AUTH_NOT_REQUIRED
415 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
416 }
417
Jason Monk361915c2017-03-21 20:33:59 -0400418 private final class PowerAction extends SinglePressAction implements LongPressAction {
419 private PowerAction() {
420 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400421 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400422 }
423
424 @Override
425 public boolean onLongPress() {
426 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
427 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
428 mWindowManagerFuncs.reboot(true);
429 return true;
430 }
431 return false;
432 }
433
434 @Override
435 public boolean showDuringKeyguard() {
436 return true;
437 }
438
439 @Override
440 public boolean showBeforeProvisioning() {
441 return true;
442 }
443
444 @Override
445 public void onPress() {
446 // shutdown by making sure radio and power are handled accordingly.
447 mWindowManagerFuncs.shutdown();
448 }
449 }
450
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800451 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800452 private EmergencyDialerAction() {
453 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +0800454 R.string.global_action_emergency);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800455 }
456
457 @Override
458 public void onPress() {
Shaotang Li8af3a242018-08-02 11:18:00 +0800459 MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Shaotang Lif9a69e22018-07-04 14:18:40 +0800460 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800461 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Lif9a69e22018-07-04 14:18:40 +0800462 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
463 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800464 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
465 }
466
467 @Override
468 public boolean showDuringKeyguard() {
469 return true;
470 }
471
472 @Override
473 public boolean showBeforeProvisioning() {
474 return true;
475 }
476 }
477
Jason Monk361915c2017-03-21 20:33:59 -0400478 private final class RestartAction extends SinglePressAction implements LongPressAction {
479 private RestartAction() {
480 super(R.drawable.ic_restart, R.string.global_action_restart);
481 }
482
483 @Override
484 public boolean onLongPress() {
485 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
486 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
487 mWindowManagerFuncs.reboot(true);
488 return true;
489 }
490 return false;
491 }
492
493 @Override
494 public boolean showDuringKeyguard() {
495 return true;
496 }
497
498 @Override
499 public boolean showBeforeProvisioning() {
500 return true;
501 }
502
503 @Override
504 public void onPress() {
505 mWindowManagerFuncs.reboot(false);
506 }
507 }
508
509
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500510 private class ScreenshotAction extends SinglePressAction {
511 public ScreenshotAction() {
512 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
513 }
514
515 @Override
516 public void onPress() {
517 // Add a little delay before executing, to give the
518 // dialog a chance to go away before it takes a
519 // screenshot.
520 // TODO: instead, omit global action dialog layer
521 mHandler.postDelayed(new Runnable() {
522 @Override
523 public void run() {
524 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
525 MetricsLogger.action(mContext,
526 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
527 }
528 }, 500);
529 }
530
531 @Override
532 public boolean showDuringKeyguard() {
533 return true;
534 }
535
536 @Override
537 public boolean showBeforeProvisioning() {
538 return false;
539 }
540 }
541
Jason Monk361915c2017-03-21 20:33:59 -0400542 private class BugReportAction extends SinglePressAction implements LongPressAction {
543
544 public BugReportAction() {
545 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
546 }
547
548 @Override
549 public void onPress() {
550 // don't actually trigger the bugreport if we are running stability
551 // tests via monkey
552 if (ActivityManager.isUserAMonkey()) {
553 return;
554 }
555 // Add a little delay before executing, to give the
556 // dialog a chance to go away before it takes a
557 // screenshot.
558 mHandler.postDelayed(new Runnable() {
559 @Override
560 public void run() {
561 try {
562 // Take an "interactive" bugreport.
563 MetricsLogger.action(mContext,
564 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
565 ActivityManager.getService().requestBugReport(
566 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
567 } catch (RemoteException e) {
568 }
569 }
570 }, 500);
571 }
572
573 @Override
574 public boolean onLongPress() {
575 // don't actually trigger the bugreport if we are running stability
576 // tests via monkey
577 if (ActivityManager.isUserAMonkey()) {
578 return false;
579 }
580 try {
581 // Take a "full" bugreport.
582 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
583 ActivityManager.getService().requestBugReport(
584 ActivityManager.BUGREPORT_OPTION_FULL);
585 } catch (RemoteException e) {
586 }
587 return false;
588 }
589
590 public boolean showDuringKeyguard() {
591 return true;
592 }
593
594 @Override
595 public boolean showBeforeProvisioning() {
596 return false;
597 }
598
599 @Override
600 public String getStatus() {
601 return mContext.getString(
602 R.string.bugreport_status,
603 Build.VERSION.RELEASE,
604 Build.ID);
605 }
606 }
607
Alex Chau04458852017-11-27 18:21:23 +0000608 private final class LogoutAction extends SinglePressAction {
609 private LogoutAction() {
610 super(R.drawable.ic_logout, R.string.global_action_logout);
611 }
612
613 @Override
614 public boolean showDuringKeyguard() {
615 return true;
616 }
617
618 @Override
619 public boolean showBeforeProvisioning() {
620 return false;
621 }
622
623 @Override
624 public void onPress() {
625 // Add a little delay before executing, to give the dialog a chance to go away before
626 // switching user
627 mHandler.postDelayed(() -> {
628 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000629 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000630 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000631 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000632 } catch (RemoteException re) {
633 Log.e(TAG, "Couldn't logout user " + re);
634 }
635 }, 500);
636 }
637 }
638
Jason Monk361915c2017-03-21 20:33:59 -0400639 private Action getSettingsAction() {
640 return new SinglePressAction(R.drawable.ic_settings,
641 R.string.global_action_settings) {
642
643 @Override
644 public void onPress() {
645 Intent intent = new Intent(Settings.ACTION_SETTINGS);
646 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
647 mContext.startActivity(intent);
648 }
649
650 @Override
651 public boolean showDuringKeyguard() {
652 return true;
653 }
654
655 @Override
656 public boolean showBeforeProvisioning() {
657 return true;
658 }
659 };
660 }
661
662 private Action getEmergencyAction() {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800663 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
664 if(!mSeparatedEmergencyButtonEnabled) {
665 // use un-colored legacy treatment
666 emergencyIcon.setTintList(null);
667 }
668
Jason Monk361915c2017-03-21 20:33:59 -0400669 return new SinglePressAction(R.drawable.emergency_icon,
670 R.string.global_action_emergency) {
671 @Override
672 public void onPress() {
673 mEmergencyAffordanceManager.performEmergencyCall();
674 }
675
676 @Override
677 public boolean showDuringKeyguard() {
678 return true;
679 }
680
681 @Override
682 public boolean showBeforeProvisioning() {
683 return true;
684 }
685 };
686 }
687
688 private Action getAssistAction() {
689 return new SinglePressAction(R.drawable.ic_action_assist_focused,
690 R.string.global_action_assist) {
691 @Override
692 public void onPress() {
693 Intent intent = new Intent(Intent.ACTION_ASSIST);
694 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
695 mContext.startActivity(intent);
696 }
697
698 @Override
699 public boolean showDuringKeyguard() {
700 return true;
701 }
702
703 @Override
704 public boolean showBeforeProvisioning() {
705 return true;
706 }
707 };
708 }
709
710 private Action getVoiceAssistAction() {
711 return new SinglePressAction(R.drawable.ic_voice_search,
712 R.string.global_action_voice_assist) {
713 @Override
714 public void onPress() {
715 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
716 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
717 mContext.startActivity(intent);
718 }
719
720 @Override
721 public boolean showDuringKeyguard() {
722 return true;
723 }
724
725 @Override
726 public boolean showBeforeProvisioning() {
727 return true;
728 }
729 };
730 }
731
732 private Action getLockdownAction() {
Alison Cichowlasd372cde2018-05-16 15:40:45 -0400733 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400734 R.string.global_action_lockdown) {
735
736 @Override
737 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700738 new LockPatternUtils(mContext)
739 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
740 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400741 try {
742 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100743 // Lock profiles (if any) on the background thread.
744 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
745 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400746 } catch (RemoteException e) {
747 Log.e(TAG, "Error while trying to lock device.", e);
748 }
749 }
750
751 @Override
752 public boolean showDuringKeyguard() {
753 return true;
754 }
755
756 @Override
757 public boolean showBeforeProvisioning() {
758 return false;
759 }
760 };
761 }
762
Pavel Grafov059021b2018-05-02 13:44:46 +0100763 private void lockProfiles() {
764 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
765 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
766 final int currentUserId = getCurrentUser().id;
767 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
768 for (final int id : profileIds) {
769 if (id != currentUserId) {
770 tm.setDeviceLockedForUser(id, true);
771 }
772 }
773 }
774
Jason Monk361915c2017-03-21 20:33:59 -0400775 private UserInfo getCurrentUser() {
776 try {
777 return ActivityManager.getService().getCurrentUser();
778 } catch (RemoteException re) {
779 return null;
780 }
781 }
782
783 private boolean isCurrentUserOwner() {
784 UserInfo currentUser = getCurrentUser();
785 return currentUser == null || currentUser.isPrimary();
786 }
787
788 private void addUsersToMenu(ArrayList<Action> items) {
789 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
790 if (um.isUserSwitcherEnabled()) {
791 List<UserInfo> users = um.getUsers();
792 UserInfo currentUser = getCurrentUser();
793 for (final UserInfo user : users) {
794 if (user.supportsSwitchToByUser()) {
795 boolean isCurrentUser = currentUser == null
796 ? user.id == 0 : (currentUser.id == user.id);
797 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
798 : null;
799 SinglePressAction switchToUser = new SinglePressAction(
800 R.drawable.ic_menu_cc, icon,
801 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400802 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400803 public void onPress() {
804 try {
805 ActivityManager.getService().switchUser(user.id);
806 } catch (RemoteException re) {
807 Log.e(TAG, "Couldn't switch user " + re);
808 }
809 }
810
811 public boolean showDuringKeyguard() {
812 return true;
813 }
814
815 public boolean showBeforeProvisioning() {
816 return false;
817 }
818 };
819 items.add(switchToUser);
820 }
821 }
822 }
823 }
824
825 private void prepareDialog() {
826 refreshSilentMode();
827 mAirplaneModeOn.updateState(mAirplaneState);
828 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400829 if (mShowSilentToggle) {
830 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
831 mContext.registerReceiver(mRingerModeReceiver, filter);
832 }
833 }
834
835 private void refreshSilentMode() {
836 if (!mHasVibrator) {
837 final boolean silentModeOn =
838 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400839 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400840 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
841 }
842 }
843
844 /** {@inheritDoc} */
845 public void onDismiss(DialogInterface dialog) {
846 mWindowManagerFuncs.onGlobalActionsHidden();
847 if (mShowSilentToggle) {
848 try {
849 mContext.unregisterReceiver(mRingerModeReceiver);
850 } catch (IllegalArgumentException ie) {
851 // ignore this
852 Log.w(TAG, ie);
853 }
854 }
855 }
856
857 /** {@inheritDoc} */
858 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400859 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400860 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400861 dialog.dismiss();
862 }
Jason Monkfd279662017-06-29 19:37:48 -0400863 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400864 }
865
Shaotang Li8af3a242018-08-02 11:18:00 +0800866 /** {@inheritDoc} */
867 public void onShow(DialogInterface dialog) {
868 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
869 }
870
Jason Monk361915c2017-03-21 20:33:59 -0400871 /**
872 * The adapter used for the list within the global actions dialog, taking
873 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400874 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
875 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400876 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
877 */
878 private class MyAdapter extends BaseAdapter {
879
880 public int getCount() {
881 int count = 0;
882
883 for (int i = 0; i < mItems.size(); i++) {
884 final Action action = mItems.get(i);
885
886 if (mKeyguardShowing && !action.showDuringKeyguard()) {
887 continue;
888 }
889 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
890 continue;
891 }
892 count++;
893 }
894 return count;
895 }
896
897 @Override
898 public boolean isEnabled(int position) {
899 return getItem(position).isEnabled();
900 }
901
902 @Override
903 public boolean areAllItemsEnabled() {
904 return false;
905 }
906
907 public Action getItem(int position) {
908
909 int filteredPos = 0;
910 for (int i = 0; i < mItems.size(); i++) {
911 final Action action = mItems.get(i);
912 if (mKeyguardShowing && !action.showDuringKeyguard()) {
913 continue;
914 }
915 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
916 continue;
917 }
918 if (filteredPos == position) {
919 return action;
920 }
921 filteredPos++;
922 }
923
924 throw new IllegalArgumentException("position " + position
925 + " out of range of showable actions"
926 + ", filtered count=" + getCount()
927 + ", keyguardshowing=" + mKeyguardShowing
928 + ", provisioned=" + mDeviceProvisioned);
929 }
930
931
932 public long getItemId(int position) {
933 return position;
934 }
935
936 public View getView(int position, View convertView, ViewGroup parent) {
937 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400938 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500939 // Everything but screenshot, the last item, gets white background.
940 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400941 HardwareUiLayout.get(parent).setDivisionView(view);
942 }
943 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400944 }
945 }
946
947 // note: the scheme below made more sense when we were planning on having
948 // 8 different things in the global actions dialog. seems overkill with
949 // only 3 items now, but may as well keep this flexible approach so it will
950 // be easy should someone decide at the last minute to include something
951 // else, such as 'enable wifi', or 'enable bluetooth'
952
953 /**
954 * What each item in the global actions dialog must be able to support.
955 */
956 private interface Action {
957 /**
958 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400959 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400960 */
961 CharSequence getLabelForAccessibility(Context context);
962
963 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
964
965 void onPress();
966
967 /**
968 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400969 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400970 */
971 boolean showDuringKeyguard();
972
973 /**
974 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400975 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400976 */
977 boolean showBeforeProvisioning();
978
979 boolean isEnabled();
980 }
981
982 /**
983 * An action that also supports long press.
984 */
985 private interface LongPressAction extends Action {
986 boolean onLongPress();
987 }
988
989 /**
990 * A single press action maintains no state, just responds to a press
991 * and takes an action.
992 */
993 private static abstract class SinglePressAction implements Action {
994 private final int mIconResId;
995 private final Drawable mIcon;
996 private final int mMessageResId;
997 private final CharSequence mMessage;
998
999 protected SinglePressAction(int iconResId, int messageResId) {
1000 mIconResId = iconResId;
1001 mMessageResId = messageResId;
1002 mMessage = null;
1003 mIcon = null;
1004 }
1005
1006 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1007 mIconResId = iconResId;
1008 mMessageResId = 0;
1009 mMessage = message;
1010 mIcon = icon;
1011 }
1012
1013 public boolean isEnabled() {
1014 return true;
1015 }
1016
1017 public String getStatus() {
1018 return null;
1019 }
1020
1021 abstract public void onPress();
1022
1023 public CharSequence getLabelForAccessibility(Context context) {
1024 if (mMessage != null) {
1025 return mMessage;
1026 } else {
1027 return context.getString(mMessageResId);
1028 }
1029 }
1030
1031 public View create(
1032 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001033 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1034 false);
Jason Monk361915c2017-03-21 20:33:59 -04001035
1036 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1037 TextView messageView = (TextView) v.findViewById(R.id.message);
1038
1039 TextView statusView = (TextView) v.findViewById(R.id.status);
1040 final String status = getStatus();
1041 if (!TextUtils.isEmpty(status)) {
1042 statusView.setText(status);
1043 } else {
1044 statusView.setVisibility(View.GONE);
1045 }
1046 if (mIcon != null) {
1047 icon.setImageDrawable(mIcon);
1048 icon.setScaleType(ScaleType.CENTER_CROP);
1049 } else if (mIconResId != 0) {
1050 icon.setImageDrawable(context.getDrawable(mIconResId));
1051 }
1052 if (mMessage != null) {
1053 messageView.setText(mMessage);
1054 } else {
1055 messageView.setText(mMessageResId);
1056 }
1057
1058 return v;
1059 }
1060 }
1061
1062 /**
1063 * A toggle action knows whether it is on or off, and displays an icon
1064 * and status message accordingly.
1065 */
1066 private static abstract class ToggleAction implements Action {
1067
1068 enum State {
1069 Off(false),
1070 TurningOn(true),
1071 TurningOff(true),
1072 On(false);
1073
1074 private final boolean inTransition;
1075
1076 State(boolean intermediate) {
1077 inTransition = intermediate;
1078 }
1079
1080 public boolean inTransition() {
1081 return inTransition;
1082 }
1083 }
1084
1085 protected State mState = State.Off;
1086
1087 // prefs
1088 protected int mEnabledIconResId;
1089 protected int mDisabledIconResid;
1090 protected int mMessageResId;
1091 protected int mEnabledStatusMessageResId;
1092 protected int mDisabledStatusMessageResId;
1093
1094 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001095 * @param enabledIconResId The icon for when this action is on.
1096 * @param disabledIconResid The icon for when this action is off.
1097 * @param message The general information message, e.g 'Silent Mode'
1098 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001099 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1100 */
1101 public ToggleAction(int enabledIconResId,
1102 int disabledIconResid,
1103 int message,
1104 int enabledStatusMessageResId,
1105 int disabledStatusMessageResId) {
1106 mEnabledIconResId = enabledIconResId;
1107 mDisabledIconResid = disabledIconResid;
1108 mMessageResId = message;
1109 mEnabledStatusMessageResId = enabledStatusMessageResId;
1110 mDisabledStatusMessageResId = disabledStatusMessageResId;
1111 }
1112
1113 /**
1114 * Override to make changes to resource IDs just before creating the
1115 * View.
1116 */
1117 void willCreate() {
1118
1119 }
1120
1121 @Override
1122 public CharSequence getLabelForAccessibility(Context context) {
1123 return context.getString(mMessageResId);
1124 }
1125
1126 public View create(Context context, View convertView, ViewGroup parent,
1127 LayoutInflater inflater) {
1128 willCreate();
1129
1130 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001131 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001132
1133 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1134 TextView messageView = (TextView) v.findViewById(R.id.message);
1135 TextView statusView = (TextView) v.findViewById(R.id.status);
1136 final boolean enabled = isEnabled();
1137
1138 if (messageView != null) {
1139 messageView.setText(mMessageResId);
1140 messageView.setEnabled(enabled);
1141 }
1142
1143 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1144 if (icon != null) {
1145 icon.setImageDrawable(context.getDrawable(
1146 (on ? mEnabledIconResId : mDisabledIconResid)));
1147 icon.setEnabled(enabled);
1148 }
1149
1150 if (statusView != null) {
1151 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1152 statusView.setVisibility(View.VISIBLE);
1153 statusView.setEnabled(enabled);
1154 }
1155 v.setEnabled(enabled);
1156
1157 return v;
1158 }
1159
1160 public final void onPress() {
1161 if (mState.inTransition()) {
1162 Log.w(TAG, "shouldn't be able to toggle when in transition");
1163 return;
1164 }
1165
1166 final boolean nowOn = !(mState == State.On);
1167 onToggle(nowOn);
1168 changeStateFromPress(nowOn);
1169 }
1170
1171 public boolean isEnabled() {
1172 return !mState.inTransition();
1173 }
1174
1175 /**
1176 * Implementations may override this if their state can be in on of the intermediate
1177 * states until some notification is received (e.g airplane mode is 'turning off' until
1178 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001179 *
Jason Monk361915c2017-03-21 20:33:59 -04001180 * @param buttonOn Whether the button was turned on or off
1181 */
1182 protected void changeStateFromPress(boolean buttonOn) {
1183 mState = buttonOn ? State.On : State.Off;
1184 }
1185
1186 abstract void onToggle(boolean on);
1187
1188 public void updateState(State state) {
1189 mState = state;
1190 }
1191 }
1192
1193 private class SilentModeToggleAction extends ToggleAction {
1194 public SilentModeToggleAction() {
1195 super(R.drawable.ic_audio_vol_mute,
1196 R.drawable.ic_audio_vol,
1197 R.string.global_action_toggle_silent_mode,
1198 R.string.global_action_silent_mode_on_status,
1199 R.string.global_action_silent_mode_off_status);
1200 }
1201
1202 void onToggle(boolean on) {
1203 if (on) {
1204 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1205 } else {
1206 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1207 }
1208 }
1209
1210 public boolean showDuringKeyguard() {
1211 return true;
1212 }
1213
1214 public boolean showBeforeProvisioning() {
1215 return false;
1216 }
1217 }
1218
1219 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1220
Jason Monk16fbd9d2017-04-27 14:28:49 -04001221 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001222
1223 private final AudioManager mAudioManager;
1224 private final Handler mHandler;
1225 private final Context mContext;
1226
1227 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1228 mAudioManager = audioManager;
1229 mHandler = handler;
1230 mContext = context;
1231 }
1232
1233 private int ringerModeToIndex(int ringerMode) {
1234 // They just happen to coincide
1235 return ringerMode;
1236 }
1237
1238 private int indexToRingerMode(int index) {
1239 // They just happen to coincide
1240 return index;
1241 }
1242
1243 @Override
1244 public CharSequence getLabelForAccessibility(Context context) {
1245 return null;
1246 }
1247
1248 public View create(Context context, View convertView, ViewGroup parent,
1249 LayoutInflater inflater) {
1250 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1251
1252 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1253 for (int i = 0; i < 3; i++) {
1254 View itemView = v.findViewById(ITEM_IDS[i]);
1255 itemView.setSelected(selectedIndex == i);
1256 // Set up click handler
1257 itemView.setTag(i);
1258 itemView.setOnClickListener(this);
1259 }
1260 return v;
1261 }
1262
1263 public void onPress() {
1264 }
1265
1266 public boolean showDuringKeyguard() {
1267 return true;
1268 }
1269
1270 public boolean showBeforeProvisioning() {
1271 return false;
1272 }
1273
1274 public boolean isEnabled() {
1275 return true;
1276 }
1277
1278 void willCreate() {
1279 }
1280
1281 public void onClick(View v) {
1282 if (!(v.getTag() instanceof Integer)) return;
1283
1284 int index = (Integer) v.getTag();
1285 mAudioManager.setRingerMode(indexToRingerMode(index));
1286 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1287 }
1288 }
1289
1290 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1291 public void onReceive(Context context, Intent intent) {
1292 String action = intent.getAction();
1293 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1294 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1295 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1296 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001297 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001298 }
1299 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1300 // Airplane mode can be changed after ECM exits if airplane toggle button
1301 // is pressed during ECM mode
1302 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1303 mIsWaitingForEcmExit) {
1304 mIsWaitingForEcmExit = false;
1305 changeAirplaneModeSystemSetting(true);
1306 }
1307 }
1308 }
1309 };
1310
1311 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1312 @Override
1313 public void onServiceStateChanged(ServiceState serviceState) {
1314 if (!mHasTelephony) return;
1315 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1316 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1317 mAirplaneModeOn.updateState(mAirplaneState);
1318 mAdapter.notifyDataSetChanged();
1319 }
1320 };
1321
1322 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1323 @Override
1324 public void onReceive(Context context, Intent intent) {
1325 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1326 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1327 }
1328 }
1329 };
1330
1331 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1332 @Override
1333 public void onChange(boolean selfChange) {
1334 onAirplaneModeChanged();
1335 }
1336 };
1337
1338 private static final int MESSAGE_DISMISS = 0;
1339 private static final int MESSAGE_REFRESH = 1;
1340 private static final int MESSAGE_SHOW = 2;
1341 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1342
1343 private Handler mHandler = new Handler() {
1344 public void handleMessage(Message msg) {
1345 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001346 case MESSAGE_DISMISS:
1347 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001348 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1349 mDialog.dismissImmediately();
1350 } else {
1351 mDialog.dismiss();
1352 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001353 mDialog = null;
1354 }
1355 break;
1356 case MESSAGE_REFRESH:
1357 refreshSilentMode();
1358 mAdapter.notifyDataSetChanged();
1359 break;
1360 case MESSAGE_SHOW:
1361 handleShow();
1362 break;
Jason Monk361915c2017-03-21 20:33:59 -04001363 }
1364 }
1365 };
1366
1367 private void onAirplaneModeChanged() {
1368 // Let the service state callbacks handle the state.
1369 if (mHasTelephony) return;
1370
1371 boolean airplaneModeOn = Settings.Global.getInt(
1372 mContext.getContentResolver(),
1373 Settings.Global.AIRPLANE_MODE_ON,
1374 0) == 1;
1375 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1376 mAirplaneModeOn.updateState(mAirplaneState);
1377 }
1378
1379 /**
1380 * Change the airplane mode system setting
1381 */
1382 private void changeAirplaneModeSystemSetting(boolean on) {
1383 Settings.Global.putInt(
1384 mContext.getContentResolver(),
1385 Settings.Global.AIRPLANE_MODE_ON,
1386 on ? 1 : 0);
1387 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1388 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1389 intent.putExtra("state", on);
1390 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1391 if (!mHasTelephony) {
1392 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1393 }
1394 }
1395
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001396 private static final class ActionsDialog extends Dialog implements DialogInterface,
1397 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001398
Jason Monk16fbd9d2017-04-27 14:28:49 -04001399 private final Context mContext;
1400 private final MyAdapter mAdapter;
1401 private final LinearLayout mListView;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001402 private final FrameLayout mSeparatedView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001403 private final HardwareUiLayout mHardwareLayout;
1404 private final OnClickListener mClickListener;
1405 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001406 private final GradientDrawable mGradientDrawable;
1407 private final ColorExtractor mColorExtractor;
1408 private boolean mKeyguardShowing;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001409 private boolean mShouldDisplaySeparatedButton;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001410
1411 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001412 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001413 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001414 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001415 mAdapter = adapter;
1416 mClickListener = clickListener;
1417 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001418 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001419 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001420 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001421
1422 // Window initialization
1423 Window window = getWindow();
1424 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001425 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1426 window.getDecorView();
1427 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1428 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1429 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1430 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1431 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001432 window.addFlags(
1433 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001434 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001435 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001436 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1437 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1438 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1439 window.setBackgroundDrawable(mGradientDrawable);
1440 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1441
Jason Monk16fbd9d2017-04-27 14:28:49 -04001442 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001443 mListView = findViewById(android.R.id.list);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001444 mSeparatedView = findViewById(com.android.systemui.R.id.separated_button);
1445 if (!mShouldDisplaySeparatedButton) {
1446 mSeparatedView.setVisibility(View.GONE);
1447 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001448 mHardwareLayout = HardwareUiLayout.get(mListView);
1449 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001450 mHardwareLayout.setHasSeparatedButton(mShouldDisplaySeparatedButton);
Phil Weaver8583ae82018-02-13 11:01:24 -08001451 setTitle(R.string.global_actions);
Phil Weaver9054e092018-04-27 16:28:50 -07001452 mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1453 @Override
1454 public boolean dispatchPopulateAccessibilityEvent(
1455 View host, AccessibilityEvent event) {
1456 // Populate the title here, just as Activity does
1457 event.getText().add(mContext.getString(R.string.global_actions));
1458 return true;
1459 }
1460 });
Jason Monk361915c2017-03-21 20:33:59 -04001461 }
1462
Jason Monk16fbd9d2017-04-27 14:28:49 -04001463 private void updateList() {
1464 mListView.removeAllViews();
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001465 mSeparatedView.removeAllViews();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001466 for (int i = 0; i < mAdapter.getCount(); i++) {
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001467 ViewGroup parentView = mShouldDisplaySeparatedButton && i == mAdapter.getCount() - 1
1468 ? mSeparatedView : mListView;
1469 View v = mAdapter.getView(i, null, parentView);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001470 final int pos = i;
1471 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1472 v.setOnLongClickListener(view ->
1473 mLongClickListener.onItemLongClick(null, v, pos, 0));
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001474 parentView.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001475 }
Jason Monk361915c2017-03-21 20:33:59 -04001476 }
1477
1478 @Override
1479 protected void onStart() {
1480 super.setCanceledOnTouchOutside(true);
1481 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001482 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001483
1484 Point displaySize = new Point();
1485 mContext.getDisplay().getRealSize(displaySize);
1486 mColorExtractor.addOnColorsChangedListener(this);
1487 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1488 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1489 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001490 updateColors(colors, false /* animate */);
1491 }
1492
1493 /**
1494 * Updates background and system bars according to current GradientColors.
1495 * @param colors Colors and hints to use.
1496 * @param animate Interpolates gradient if true, just sets otherwise.
1497 */
1498 private void updateColors(GradientColors colors, boolean animate) {
1499 mGradientDrawable.setColors(colors, animate);
1500 View decorView = getWindow().getDecorView();
1501 if (colors.supportsDarkText()) {
1502 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1503 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1504 } else {
1505 decorView.setSystemUiVisibility(0);
1506 }
Jason Monk361915c2017-03-21 20:33:59 -04001507 }
1508
1509 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001510 protected void onStop() {
1511 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001512 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001513 }
1514
1515 @Override
1516 public void show() {
1517 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001518 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001519 mHardwareLayout.setTranslationX(getAnimTranslation());
1520 mHardwareLayout.setAlpha(0);
1521 mHardwareLayout.animate()
1522 .alpha(1)
1523 .translationX(0)
1524 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001525 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001526 .setUpdateListener(animation -> {
1527 int alpha = (int) ((Float) animation.getAnimatedValue()
1528 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1529 mGradientDrawable.setAlpha(alpha);
1530 })
Jason Monka7af3b62017-07-07 11:35:13 -04001531 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001532 .start();
1533 }
1534
1535 @Override
1536 public void dismiss() {
1537 mHardwareLayout.setTranslationX(0);
1538 mHardwareLayout.setAlpha(1);
1539 mHardwareLayout.animate()
1540 .alpha(0)
1541 .translationX(getAnimTranslation())
1542 .setDuration(300)
1543 .withEndAction(() -> super.dismiss())
1544 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001545 .setUpdateListener(animation -> {
1546 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1547 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1548 mGradientDrawable.setAlpha(alpha);
1549 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001550 .start();
1551 }
1552
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001553 void dismissImmediately() {
1554 super.dismiss();
1555 }
1556
Jason Monk16fbd9d2017-04-27 14:28:49 -04001557 private float getAnimTranslation() {
1558 return getContext().getResources().getDimension(
1559 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001560 }
1561
1562 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001563 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001564 if (mKeyguardShowing) {
1565 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001566 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1567 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001568 }
1569 } else {
1570 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001571 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1572 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001573 }
1574 }
1575 }
1576
1577 public void setKeyguardShowing(boolean keyguardShowing) {
1578 mKeyguardShowing = keyguardShowing;
1579 }
Jason Monk361915c2017-03-21 20:33:59 -04001580 }
1581}