blob: a38838d3d86ead7d3573ba660e2fd2cb57801ac3 [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);
Leo Hsu90e276e2019-03-06 15:25:28 +0800461 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
462 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
463 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Lif9a69e22018-07-04 14:18:40 +0800464 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
465 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800466 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
467 }
468
469 @Override
470 public boolean showDuringKeyguard() {
471 return true;
472 }
473
474 @Override
475 public boolean showBeforeProvisioning() {
476 return true;
477 }
478 }
479
Jason Monk361915c2017-03-21 20:33:59 -0400480 private final class RestartAction extends SinglePressAction implements LongPressAction {
481 private RestartAction() {
482 super(R.drawable.ic_restart, R.string.global_action_restart);
483 }
484
485 @Override
486 public boolean onLongPress() {
487 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
488 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
489 mWindowManagerFuncs.reboot(true);
490 return true;
491 }
492 return false;
493 }
494
495 @Override
496 public boolean showDuringKeyguard() {
497 return true;
498 }
499
500 @Override
501 public boolean showBeforeProvisioning() {
502 return true;
503 }
504
505 @Override
506 public void onPress() {
507 mWindowManagerFuncs.reboot(false);
508 }
509 }
510
511
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500512 private class ScreenshotAction extends SinglePressAction {
513 public ScreenshotAction() {
514 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
515 }
516
517 @Override
518 public void onPress() {
519 // Add a little delay before executing, to give the
520 // dialog a chance to go away before it takes a
521 // screenshot.
522 // TODO: instead, omit global action dialog layer
523 mHandler.postDelayed(new Runnable() {
524 @Override
525 public void run() {
526 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
527 MetricsLogger.action(mContext,
528 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
529 }
530 }, 500);
531 }
532
533 @Override
534 public boolean showDuringKeyguard() {
535 return true;
536 }
537
538 @Override
539 public boolean showBeforeProvisioning() {
540 return false;
541 }
542 }
543
Jason Monk361915c2017-03-21 20:33:59 -0400544 private class BugReportAction extends SinglePressAction implements LongPressAction {
545
546 public BugReportAction() {
547 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
548 }
549
550 @Override
551 public void onPress() {
552 // don't actually trigger the bugreport if we are running stability
553 // tests via monkey
554 if (ActivityManager.isUserAMonkey()) {
555 return;
556 }
557 // Add a little delay before executing, to give the
558 // dialog a chance to go away before it takes a
559 // screenshot.
560 mHandler.postDelayed(new Runnable() {
561 @Override
562 public void run() {
563 try {
564 // Take an "interactive" bugreport.
565 MetricsLogger.action(mContext,
566 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
567 ActivityManager.getService().requestBugReport(
568 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
569 } catch (RemoteException e) {
570 }
571 }
572 }, 500);
573 }
574
575 @Override
576 public boolean onLongPress() {
577 // don't actually trigger the bugreport if we are running stability
578 // tests via monkey
579 if (ActivityManager.isUserAMonkey()) {
580 return false;
581 }
582 try {
583 // Take a "full" bugreport.
584 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
585 ActivityManager.getService().requestBugReport(
586 ActivityManager.BUGREPORT_OPTION_FULL);
587 } catch (RemoteException e) {
588 }
589 return false;
590 }
591
592 public boolean showDuringKeyguard() {
593 return true;
594 }
595
596 @Override
597 public boolean showBeforeProvisioning() {
598 return false;
599 }
600
601 @Override
602 public String getStatus() {
603 return mContext.getString(
604 R.string.bugreport_status,
605 Build.VERSION.RELEASE,
606 Build.ID);
607 }
608 }
609
Alex Chau04458852017-11-27 18:21:23 +0000610 private final class LogoutAction extends SinglePressAction {
611 private LogoutAction() {
612 super(R.drawable.ic_logout, R.string.global_action_logout);
613 }
614
615 @Override
616 public boolean showDuringKeyguard() {
617 return true;
618 }
619
620 @Override
621 public boolean showBeforeProvisioning() {
622 return false;
623 }
624
625 @Override
626 public void onPress() {
627 // Add a little delay before executing, to give the dialog a chance to go away before
628 // switching user
629 mHandler.postDelayed(() -> {
630 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000631 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000632 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000633 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000634 } catch (RemoteException re) {
635 Log.e(TAG, "Couldn't logout user " + re);
636 }
637 }, 500);
638 }
639 }
640
Jason Monk361915c2017-03-21 20:33:59 -0400641 private Action getSettingsAction() {
642 return new SinglePressAction(R.drawable.ic_settings,
643 R.string.global_action_settings) {
644
645 @Override
646 public void onPress() {
647 Intent intent = new Intent(Settings.ACTION_SETTINGS);
648 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
649 mContext.startActivity(intent);
650 }
651
652 @Override
653 public boolean showDuringKeyguard() {
654 return true;
655 }
656
657 @Override
658 public boolean showBeforeProvisioning() {
659 return true;
660 }
661 };
662 }
663
664 private Action getEmergencyAction() {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800665 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
666 if(!mSeparatedEmergencyButtonEnabled) {
667 // use un-colored legacy treatment
668 emergencyIcon.setTintList(null);
669 }
670
Jason Monk361915c2017-03-21 20:33:59 -0400671 return new SinglePressAction(R.drawable.emergency_icon,
672 R.string.global_action_emergency) {
673 @Override
674 public void onPress() {
675 mEmergencyAffordanceManager.performEmergencyCall();
676 }
677
678 @Override
679 public boolean showDuringKeyguard() {
680 return true;
681 }
682
683 @Override
684 public boolean showBeforeProvisioning() {
685 return true;
686 }
687 };
688 }
689
690 private Action getAssistAction() {
691 return new SinglePressAction(R.drawable.ic_action_assist_focused,
692 R.string.global_action_assist) {
693 @Override
694 public void onPress() {
695 Intent intent = new Intent(Intent.ACTION_ASSIST);
696 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
697 mContext.startActivity(intent);
698 }
699
700 @Override
701 public boolean showDuringKeyguard() {
702 return true;
703 }
704
705 @Override
706 public boolean showBeforeProvisioning() {
707 return true;
708 }
709 };
710 }
711
712 private Action getVoiceAssistAction() {
713 return new SinglePressAction(R.drawable.ic_voice_search,
714 R.string.global_action_voice_assist) {
715 @Override
716 public void onPress() {
717 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
718 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
719 mContext.startActivity(intent);
720 }
721
722 @Override
723 public boolean showDuringKeyguard() {
724 return true;
725 }
726
727 @Override
728 public boolean showBeforeProvisioning() {
729 return true;
730 }
731 };
732 }
733
734 private Action getLockdownAction() {
Alison Cichowlasd372cde2018-05-16 15:40:45 -0400735 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400736 R.string.global_action_lockdown) {
737
738 @Override
739 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700740 new LockPatternUtils(mContext)
741 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
742 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400743 try {
744 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100745 // Lock profiles (if any) on the background thread.
746 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
747 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400748 } catch (RemoteException e) {
749 Log.e(TAG, "Error while trying to lock device.", e);
750 }
751 }
752
753 @Override
754 public boolean showDuringKeyguard() {
755 return true;
756 }
757
758 @Override
759 public boolean showBeforeProvisioning() {
760 return false;
761 }
762 };
763 }
764
Pavel Grafov059021b2018-05-02 13:44:46 +0100765 private void lockProfiles() {
766 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
767 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
768 final int currentUserId = getCurrentUser().id;
769 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
770 for (final int id : profileIds) {
771 if (id != currentUserId) {
772 tm.setDeviceLockedForUser(id, true);
773 }
774 }
775 }
776
Jason Monk361915c2017-03-21 20:33:59 -0400777 private UserInfo getCurrentUser() {
778 try {
779 return ActivityManager.getService().getCurrentUser();
780 } catch (RemoteException re) {
781 return null;
782 }
783 }
784
785 private boolean isCurrentUserOwner() {
786 UserInfo currentUser = getCurrentUser();
787 return currentUser == null || currentUser.isPrimary();
788 }
789
790 private void addUsersToMenu(ArrayList<Action> items) {
791 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
792 if (um.isUserSwitcherEnabled()) {
793 List<UserInfo> users = um.getUsers();
794 UserInfo currentUser = getCurrentUser();
795 for (final UserInfo user : users) {
796 if (user.supportsSwitchToByUser()) {
797 boolean isCurrentUser = currentUser == null
798 ? user.id == 0 : (currentUser.id == user.id);
799 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
800 : null;
801 SinglePressAction switchToUser = new SinglePressAction(
802 R.drawable.ic_menu_cc, icon,
803 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400804 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400805 public void onPress() {
806 try {
807 ActivityManager.getService().switchUser(user.id);
808 } catch (RemoteException re) {
809 Log.e(TAG, "Couldn't switch user " + re);
810 }
811 }
812
813 public boolean showDuringKeyguard() {
814 return true;
815 }
816
817 public boolean showBeforeProvisioning() {
818 return false;
819 }
820 };
821 items.add(switchToUser);
822 }
823 }
824 }
825 }
826
827 private void prepareDialog() {
828 refreshSilentMode();
829 mAirplaneModeOn.updateState(mAirplaneState);
830 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400831 if (mShowSilentToggle) {
832 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
833 mContext.registerReceiver(mRingerModeReceiver, filter);
834 }
835 }
836
837 private void refreshSilentMode() {
838 if (!mHasVibrator) {
839 final boolean silentModeOn =
840 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400841 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400842 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
843 }
844 }
845
846 /** {@inheritDoc} */
847 public void onDismiss(DialogInterface dialog) {
848 mWindowManagerFuncs.onGlobalActionsHidden();
849 if (mShowSilentToggle) {
850 try {
851 mContext.unregisterReceiver(mRingerModeReceiver);
852 } catch (IllegalArgumentException ie) {
853 // ignore this
854 Log.w(TAG, ie);
855 }
856 }
857 }
858
859 /** {@inheritDoc} */
860 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400861 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400862 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400863 dialog.dismiss();
864 }
Jason Monkfd279662017-06-29 19:37:48 -0400865 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400866 }
867
Shaotang Li8af3a242018-08-02 11:18:00 +0800868 /** {@inheritDoc} */
869 public void onShow(DialogInterface dialog) {
870 MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
871 }
872
Jason Monk361915c2017-03-21 20:33:59 -0400873 /**
874 * The adapter used for the list within the global actions dialog, taking
875 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400876 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
877 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400878 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
879 */
880 private class MyAdapter extends BaseAdapter {
881
882 public int getCount() {
883 int count = 0;
884
885 for (int i = 0; i < mItems.size(); i++) {
886 final Action action = mItems.get(i);
887
888 if (mKeyguardShowing && !action.showDuringKeyguard()) {
889 continue;
890 }
891 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
892 continue;
893 }
894 count++;
895 }
896 return count;
897 }
898
899 @Override
900 public boolean isEnabled(int position) {
901 return getItem(position).isEnabled();
902 }
903
904 @Override
905 public boolean areAllItemsEnabled() {
906 return false;
907 }
908
909 public Action getItem(int position) {
910
911 int filteredPos = 0;
912 for (int i = 0; i < mItems.size(); i++) {
913 final Action action = mItems.get(i);
914 if (mKeyguardShowing && !action.showDuringKeyguard()) {
915 continue;
916 }
917 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
918 continue;
919 }
920 if (filteredPos == position) {
921 return action;
922 }
923 filteredPos++;
924 }
925
926 throw new IllegalArgumentException("position " + position
927 + " out of range of showable actions"
928 + ", filtered count=" + getCount()
929 + ", keyguardshowing=" + mKeyguardShowing
930 + ", provisioned=" + mDeviceProvisioned);
931 }
932
933
934 public long getItemId(int position) {
935 return position;
936 }
937
938 public View getView(int position, View convertView, ViewGroup parent) {
939 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400940 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500941 // Everything but screenshot, the last item, gets white background.
942 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400943 HardwareUiLayout.get(parent).setDivisionView(view);
944 }
945 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400946 }
947 }
948
949 // note: the scheme below made more sense when we were planning on having
950 // 8 different things in the global actions dialog. seems overkill with
951 // only 3 items now, but may as well keep this flexible approach so it will
952 // be easy should someone decide at the last minute to include something
953 // else, such as 'enable wifi', or 'enable bluetooth'
954
955 /**
956 * What each item in the global actions dialog must be able to support.
957 */
958 private interface Action {
959 /**
960 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400961 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400962 */
963 CharSequence getLabelForAccessibility(Context context);
964
965 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
966
967 void onPress();
968
969 /**
970 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400971 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400972 */
973 boolean showDuringKeyguard();
974
975 /**
976 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400977 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400978 */
979 boolean showBeforeProvisioning();
980
981 boolean isEnabled();
982 }
983
984 /**
985 * An action that also supports long press.
986 */
987 private interface LongPressAction extends Action {
988 boolean onLongPress();
989 }
990
991 /**
992 * A single press action maintains no state, just responds to a press
993 * and takes an action.
994 */
995 private static abstract class SinglePressAction implements Action {
996 private final int mIconResId;
997 private final Drawable mIcon;
998 private final int mMessageResId;
999 private final CharSequence mMessage;
1000
1001 protected SinglePressAction(int iconResId, int messageResId) {
1002 mIconResId = iconResId;
1003 mMessageResId = messageResId;
1004 mMessage = null;
1005 mIcon = null;
1006 }
1007
1008 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1009 mIconResId = iconResId;
1010 mMessageResId = 0;
1011 mMessage = message;
1012 mIcon = icon;
1013 }
1014
1015 public boolean isEnabled() {
1016 return true;
1017 }
1018
1019 public String getStatus() {
1020 return null;
1021 }
1022
1023 abstract public void onPress();
1024
1025 public CharSequence getLabelForAccessibility(Context context) {
1026 if (mMessage != null) {
1027 return mMessage;
1028 } else {
1029 return context.getString(mMessageResId);
1030 }
1031 }
1032
1033 public View create(
1034 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001035 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1036 false);
Jason Monk361915c2017-03-21 20:33:59 -04001037
1038 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1039 TextView messageView = (TextView) v.findViewById(R.id.message);
1040
1041 TextView statusView = (TextView) v.findViewById(R.id.status);
1042 final String status = getStatus();
1043 if (!TextUtils.isEmpty(status)) {
1044 statusView.setText(status);
1045 } else {
1046 statusView.setVisibility(View.GONE);
1047 }
1048 if (mIcon != null) {
1049 icon.setImageDrawable(mIcon);
1050 icon.setScaleType(ScaleType.CENTER_CROP);
1051 } else if (mIconResId != 0) {
1052 icon.setImageDrawable(context.getDrawable(mIconResId));
1053 }
1054 if (mMessage != null) {
1055 messageView.setText(mMessage);
1056 } else {
1057 messageView.setText(mMessageResId);
1058 }
1059
1060 return v;
1061 }
1062 }
1063
1064 /**
1065 * A toggle action knows whether it is on or off, and displays an icon
1066 * and status message accordingly.
1067 */
1068 private static abstract class ToggleAction implements Action {
1069
1070 enum State {
1071 Off(false),
1072 TurningOn(true),
1073 TurningOff(true),
1074 On(false);
1075
1076 private final boolean inTransition;
1077
1078 State(boolean intermediate) {
1079 inTransition = intermediate;
1080 }
1081
1082 public boolean inTransition() {
1083 return inTransition;
1084 }
1085 }
1086
1087 protected State mState = State.Off;
1088
1089 // prefs
1090 protected int mEnabledIconResId;
1091 protected int mDisabledIconResid;
1092 protected int mMessageResId;
1093 protected int mEnabledStatusMessageResId;
1094 protected int mDisabledStatusMessageResId;
1095
1096 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001097 * @param enabledIconResId The icon for when this action is on.
1098 * @param disabledIconResid The icon for when this action is off.
1099 * @param message The general information message, e.g 'Silent Mode'
1100 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001101 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1102 */
1103 public ToggleAction(int enabledIconResId,
1104 int disabledIconResid,
1105 int message,
1106 int enabledStatusMessageResId,
1107 int disabledStatusMessageResId) {
1108 mEnabledIconResId = enabledIconResId;
1109 mDisabledIconResid = disabledIconResid;
1110 mMessageResId = message;
1111 mEnabledStatusMessageResId = enabledStatusMessageResId;
1112 mDisabledStatusMessageResId = disabledStatusMessageResId;
1113 }
1114
1115 /**
1116 * Override to make changes to resource IDs just before creating the
1117 * View.
1118 */
1119 void willCreate() {
1120
1121 }
1122
1123 @Override
1124 public CharSequence getLabelForAccessibility(Context context) {
1125 return context.getString(mMessageResId);
1126 }
1127
1128 public View create(Context context, View convertView, ViewGroup parent,
1129 LayoutInflater inflater) {
1130 willCreate();
1131
1132 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001133 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001134
1135 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1136 TextView messageView = (TextView) v.findViewById(R.id.message);
1137 TextView statusView = (TextView) v.findViewById(R.id.status);
1138 final boolean enabled = isEnabled();
1139
1140 if (messageView != null) {
1141 messageView.setText(mMessageResId);
1142 messageView.setEnabled(enabled);
1143 }
1144
1145 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1146 if (icon != null) {
1147 icon.setImageDrawable(context.getDrawable(
1148 (on ? mEnabledIconResId : mDisabledIconResid)));
1149 icon.setEnabled(enabled);
1150 }
1151
1152 if (statusView != null) {
1153 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1154 statusView.setVisibility(View.VISIBLE);
1155 statusView.setEnabled(enabled);
1156 }
1157 v.setEnabled(enabled);
1158
1159 return v;
1160 }
1161
1162 public final void onPress() {
1163 if (mState.inTransition()) {
1164 Log.w(TAG, "shouldn't be able to toggle when in transition");
1165 return;
1166 }
1167
1168 final boolean nowOn = !(mState == State.On);
1169 onToggle(nowOn);
1170 changeStateFromPress(nowOn);
1171 }
1172
1173 public boolean isEnabled() {
1174 return !mState.inTransition();
1175 }
1176
1177 /**
1178 * Implementations may override this if their state can be in on of the intermediate
1179 * states until some notification is received (e.g airplane mode is 'turning off' until
1180 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001181 *
Jason Monk361915c2017-03-21 20:33:59 -04001182 * @param buttonOn Whether the button was turned on or off
1183 */
1184 protected void changeStateFromPress(boolean buttonOn) {
1185 mState = buttonOn ? State.On : State.Off;
1186 }
1187
1188 abstract void onToggle(boolean on);
1189
1190 public void updateState(State state) {
1191 mState = state;
1192 }
1193 }
1194
1195 private class SilentModeToggleAction extends ToggleAction {
1196 public SilentModeToggleAction() {
1197 super(R.drawable.ic_audio_vol_mute,
1198 R.drawable.ic_audio_vol,
1199 R.string.global_action_toggle_silent_mode,
1200 R.string.global_action_silent_mode_on_status,
1201 R.string.global_action_silent_mode_off_status);
1202 }
1203
1204 void onToggle(boolean on) {
1205 if (on) {
1206 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1207 } else {
1208 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1209 }
1210 }
1211
1212 public boolean showDuringKeyguard() {
1213 return true;
1214 }
1215
1216 public boolean showBeforeProvisioning() {
1217 return false;
1218 }
1219 }
1220
1221 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1222
Jason Monk16fbd9d2017-04-27 14:28:49 -04001223 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001224
1225 private final AudioManager mAudioManager;
1226 private final Handler mHandler;
1227 private final Context mContext;
1228
1229 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1230 mAudioManager = audioManager;
1231 mHandler = handler;
1232 mContext = context;
1233 }
1234
1235 private int ringerModeToIndex(int ringerMode) {
1236 // They just happen to coincide
1237 return ringerMode;
1238 }
1239
1240 private int indexToRingerMode(int index) {
1241 // They just happen to coincide
1242 return index;
1243 }
1244
1245 @Override
1246 public CharSequence getLabelForAccessibility(Context context) {
1247 return null;
1248 }
1249
1250 public View create(Context context, View convertView, ViewGroup parent,
1251 LayoutInflater inflater) {
1252 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1253
1254 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1255 for (int i = 0; i < 3; i++) {
1256 View itemView = v.findViewById(ITEM_IDS[i]);
1257 itemView.setSelected(selectedIndex == i);
1258 // Set up click handler
1259 itemView.setTag(i);
1260 itemView.setOnClickListener(this);
1261 }
1262 return v;
1263 }
1264
1265 public void onPress() {
1266 }
1267
1268 public boolean showDuringKeyguard() {
1269 return true;
1270 }
1271
1272 public boolean showBeforeProvisioning() {
1273 return false;
1274 }
1275
1276 public boolean isEnabled() {
1277 return true;
1278 }
1279
1280 void willCreate() {
1281 }
1282
1283 public void onClick(View v) {
1284 if (!(v.getTag() instanceof Integer)) return;
1285
1286 int index = (Integer) v.getTag();
1287 mAudioManager.setRingerMode(indexToRingerMode(index));
1288 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1289 }
1290 }
1291
1292 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1293 public void onReceive(Context context, Intent intent) {
1294 String action = intent.getAction();
1295 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1296 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1297 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1298 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001299 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001300 }
1301 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1302 // Airplane mode can be changed after ECM exits if airplane toggle button
1303 // is pressed during ECM mode
1304 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1305 mIsWaitingForEcmExit) {
1306 mIsWaitingForEcmExit = false;
1307 changeAirplaneModeSystemSetting(true);
1308 }
1309 }
1310 }
1311 };
1312
1313 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1314 @Override
1315 public void onServiceStateChanged(ServiceState serviceState) {
1316 if (!mHasTelephony) return;
1317 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1318 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1319 mAirplaneModeOn.updateState(mAirplaneState);
1320 mAdapter.notifyDataSetChanged();
1321 }
1322 };
1323
1324 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1325 @Override
1326 public void onReceive(Context context, Intent intent) {
1327 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1328 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1329 }
1330 }
1331 };
1332
1333 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1334 @Override
1335 public void onChange(boolean selfChange) {
1336 onAirplaneModeChanged();
1337 }
1338 };
1339
1340 private static final int MESSAGE_DISMISS = 0;
1341 private static final int MESSAGE_REFRESH = 1;
1342 private static final int MESSAGE_SHOW = 2;
1343 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1344
1345 private Handler mHandler = new Handler() {
1346 public void handleMessage(Message msg) {
1347 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001348 case MESSAGE_DISMISS:
1349 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001350 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1351 mDialog.dismissImmediately();
1352 } else {
1353 mDialog.dismiss();
1354 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001355 mDialog = null;
1356 }
1357 break;
1358 case MESSAGE_REFRESH:
1359 refreshSilentMode();
1360 mAdapter.notifyDataSetChanged();
1361 break;
1362 case MESSAGE_SHOW:
1363 handleShow();
1364 break;
Jason Monk361915c2017-03-21 20:33:59 -04001365 }
1366 }
1367 };
1368
1369 private void onAirplaneModeChanged() {
1370 // Let the service state callbacks handle the state.
1371 if (mHasTelephony) return;
1372
1373 boolean airplaneModeOn = Settings.Global.getInt(
1374 mContext.getContentResolver(),
1375 Settings.Global.AIRPLANE_MODE_ON,
1376 0) == 1;
1377 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1378 mAirplaneModeOn.updateState(mAirplaneState);
1379 }
1380
1381 /**
1382 * Change the airplane mode system setting
1383 */
1384 private void changeAirplaneModeSystemSetting(boolean on) {
1385 Settings.Global.putInt(
1386 mContext.getContentResolver(),
1387 Settings.Global.AIRPLANE_MODE_ON,
1388 on ? 1 : 0);
1389 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1390 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1391 intent.putExtra("state", on);
1392 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1393 if (!mHasTelephony) {
1394 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1395 }
1396 }
1397
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001398 private static final class ActionsDialog extends Dialog implements DialogInterface,
1399 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001400
Jason Monk16fbd9d2017-04-27 14:28:49 -04001401 private final Context mContext;
1402 private final MyAdapter mAdapter;
1403 private final LinearLayout mListView;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001404 private final FrameLayout mSeparatedView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001405 private final HardwareUiLayout mHardwareLayout;
1406 private final OnClickListener mClickListener;
1407 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001408 private final GradientDrawable mGradientDrawable;
1409 private final ColorExtractor mColorExtractor;
1410 private boolean mKeyguardShowing;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001411 private boolean mShouldDisplaySeparatedButton;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001412
1413 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001414 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001415 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001416 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001417 mAdapter = adapter;
1418 mClickListener = clickListener;
1419 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001420 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001421 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001422 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001423
1424 // Window initialization
1425 Window window = getWindow();
1426 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001427 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1428 window.getDecorView();
1429 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1430 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1431 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1432 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1433 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001434 window.addFlags(
1435 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001436 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001437 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001438 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1439 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1440 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1441 window.setBackgroundDrawable(mGradientDrawable);
1442 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1443
Jason Monk16fbd9d2017-04-27 14:28:49 -04001444 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001445 mListView = findViewById(android.R.id.list);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001446 mSeparatedView = findViewById(com.android.systemui.R.id.separated_button);
1447 if (!mShouldDisplaySeparatedButton) {
1448 mSeparatedView.setVisibility(View.GONE);
1449 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001450 mHardwareLayout = HardwareUiLayout.get(mListView);
1451 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001452 mHardwareLayout.setHasSeparatedButton(mShouldDisplaySeparatedButton);
Phil Weaver8583ae82018-02-13 11:01:24 -08001453 setTitle(R.string.global_actions);
Phil Weaver9054e092018-04-27 16:28:50 -07001454 mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1455 @Override
1456 public boolean dispatchPopulateAccessibilityEvent(
1457 View host, AccessibilityEvent event) {
1458 // Populate the title here, just as Activity does
1459 event.getText().add(mContext.getString(R.string.global_actions));
1460 return true;
1461 }
1462 });
Jason Monk361915c2017-03-21 20:33:59 -04001463 }
1464
Jason Monk16fbd9d2017-04-27 14:28:49 -04001465 private void updateList() {
1466 mListView.removeAllViews();
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001467 mSeparatedView.removeAllViews();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001468 for (int i = 0; i < mAdapter.getCount(); i++) {
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001469 ViewGroup parentView = mShouldDisplaySeparatedButton && i == mAdapter.getCount() - 1
1470 ? mSeparatedView : mListView;
1471 View v = mAdapter.getView(i, null, parentView);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001472 final int pos = i;
1473 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1474 v.setOnLongClickListener(view ->
1475 mLongClickListener.onItemLongClick(null, v, pos, 0));
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001476 parentView.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001477 }
Jason Monk361915c2017-03-21 20:33:59 -04001478 }
1479
1480 @Override
1481 protected void onStart() {
1482 super.setCanceledOnTouchOutside(true);
1483 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001484 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001485
1486 Point displaySize = new Point();
1487 mContext.getDisplay().getRealSize(displaySize);
1488 mColorExtractor.addOnColorsChangedListener(this);
1489 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1490 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1491 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001492 updateColors(colors, false /* animate */);
1493 }
1494
1495 /**
1496 * Updates background and system bars according to current GradientColors.
1497 * @param colors Colors and hints to use.
1498 * @param animate Interpolates gradient if true, just sets otherwise.
1499 */
1500 private void updateColors(GradientColors colors, boolean animate) {
1501 mGradientDrawable.setColors(colors, animate);
1502 View decorView = getWindow().getDecorView();
1503 if (colors.supportsDarkText()) {
1504 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1505 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1506 } else {
1507 decorView.setSystemUiVisibility(0);
1508 }
Jason Monk361915c2017-03-21 20:33:59 -04001509 }
1510
1511 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001512 protected void onStop() {
1513 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001514 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001515 }
1516
1517 @Override
1518 public void show() {
1519 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001520 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001521 mHardwareLayout.setTranslationX(getAnimTranslation());
1522 mHardwareLayout.setAlpha(0);
1523 mHardwareLayout.animate()
1524 .alpha(1)
1525 .translationX(0)
1526 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001527 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001528 .setUpdateListener(animation -> {
1529 int alpha = (int) ((Float) animation.getAnimatedValue()
1530 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1531 mGradientDrawable.setAlpha(alpha);
1532 })
Jason Monka7af3b62017-07-07 11:35:13 -04001533 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001534 .start();
1535 }
1536
1537 @Override
1538 public void dismiss() {
1539 mHardwareLayout.setTranslationX(0);
1540 mHardwareLayout.setAlpha(1);
1541 mHardwareLayout.animate()
1542 .alpha(0)
1543 .translationX(getAnimTranslation())
1544 .setDuration(300)
1545 .withEndAction(() -> super.dismiss())
1546 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001547 .setUpdateListener(animation -> {
1548 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1549 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1550 mGradientDrawable.setAlpha(alpha);
1551 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001552 .start();
1553 }
1554
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001555 void dismissImmediately() {
1556 super.dismiss();
1557 }
1558
Jason Monk16fbd9d2017-04-27 14:28:49 -04001559 private float getAnimTranslation() {
1560 return getContext().getResources().getDimension(
1561 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001562 }
1563
1564 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001565 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001566 if (mKeyguardShowing) {
1567 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001568 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1569 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001570 }
1571 } else {
1572 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001573 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1574 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001575 }
1576 }
1577 }
1578
1579 public void setKeyguardShowing(boolean keyguardShowing) {
1580 mKeyguardShowing = keyguardShowing;
1581 }
Jason Monk361915c2017-03-21 20:33:59 -04001582 }
1583}