blob: f4cdbac9f2231dba5f6de791ddfbfec3f76802d7 [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;
yuanjiahsu88363e92018-09-06 19:23:52 +080058import android.util.FeatureFlagUtils;
Jason Monk361915c2017-03-21 20:33:59 -040059import android.util.Log;
Lucas Dupin448786c2017-07-24 17:44:25 -070060import android.view.ContextThemeWrapper;
Jason Monk361915c2017-03-21 20:33:59 -040061import android.view.LayoutInflater;
62import android.view.View;
63import android.view.ViewGroup;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070064import android.view.Window;
Jason Monk361915c2017-03-21 20:33:59 -040065import android.view.WindowManager;
66import android.view.WindowManagerGlobal;
67import android.view.accessibility.AccessibilityEvent;
Jason Monk16fbd9d2017-04-27 14:28:49 -040068import android.widget.AdapterView.OnItemLongClickListener;
Jason Monk361915c2017-03-21 20:33:59 -040069import android.widget.BaseAdapter;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +080070import android.widget.FrameLayout;
Jason Monk361915c2017-03-21 20:33:59 -040071import android.widget.ImageView;
72import android.widget.ImageView.ScaleType;
Jason Monk16fbd9d2017-04-27 14:28:49 -040073import android.widget.LinearLayout;
Jason Monk361915c2017-03-21 20:33:59 -040074import android.widget.TextView;
75
Charles He9851a8d2017-10-10 17:31:30 +010076import com.android.internal.R;
77import com.android.internal.colorextraction.ColorExtractor;
78import com.android.internal.colorextraction.ColorExtractor.GradientColors;
Lucas Dupine2292a92017-07-06 14:35:30 -070079import com.android.internal.colorextraction.drawable.GradientDrawable;
Charles He9851a8d2017-10-10 17:31:30 +010080import com.android.internal.logging.MetricsLogger;
81import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
82import com.android.internal.telephony.TelephonyIntents;
83import com.android.internal.telephony.TelephonyProperties;
84import com.android.internal.util.EmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -050085import com.android.internal.util.ScreenshotHelper;
Charles He9851a8d2017-10-10 17:31:30 +010086import com.android.internal.widget.LockPatternUtils;
87import com.android.systemui.Dependency;
88import com.android.systemui.HardwareUiLayout;
89import com.android.systemui.Interpolators;
90import com.android.systemui.colorextraction.SysuiColorExtractor;
91import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
92import com.android.systemui.statusbar.phone.ScrimController;
Shaotang Lif9a69e22018-07-04 14:18:40 +080093import com.android.systemui.util.EmergencyDialerConstants;
Julia Reynolds42411922017-11-08 11:19:09 -050094import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070095
Jason Monk361915c2017-03-21 20:33:59 -040096import java.util.ArrayList;
97import java.util.List;
98
99/**
100 * Helper to show the global actions dialog. Each item is an {@link Action} that
101 * may show depending on whether the keyguard is showing, and whether the device
102 * is provisioned.
103 */
Charles He9851a8d2017-10-10 17:31:30 +0100104class GlobalActionsDialog implements DialogInterface.OnDismissListener,
105 DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -0400106
107 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
108 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700109 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400110
111 private static final String TAG = "GlobalActionsDialog";
112
113 private static final boolean SHOW_SILENT_TOGGLE = true;
114
115 /* Valid settings for global actions keys.
116 * see config.xml config_globalActionList */
117 private static final String GLOBAL_ACTION_KEY_POWER = "power";
118 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
119 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
120 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
121 private static final String GLOBAL_ACTION_KEY_USERS = "users";
122 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
123 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
124 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
125 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
126 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000127 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800128 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500129 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400130
131 private final Context mContext;
132 private final GlobalActionsManager mWindowManagerFuncs;
133 private final AudioManager mAudioManager;
134 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000135 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800136 private final LockPatternUtils mLockPatternUtils;
137 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400138
139 private ArrayList<Action> mItems;
140 private ActionsDialog mDialog;
141
142 private Action mSilentModeAction;
143 private ToggleAction mAirplaneModeOn;
144
145 private MyAdapter mAdapter;
146
147 private boolean mKeyguardShowing = false;
148 private boolean mDeviceProvisioned = false;
149 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
150 private boolean mIsWaitingForEcmExit = false;
151 private boolean mHasTelephony;
152 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000153 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800154 private boolean mHasLockdownButton;
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800155 private boolean mSeparatedEmergencyButtonEnabled;
Jason Monk361915c2017-03-21 20:33:59 -0400156 private final boolean mShowSilentToggle;
157 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500158 private final ScreenshotHelper mScreenshotHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400159
160 /**
161 * @param context everything needs a context :(
162 */
163 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700164 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400165 mWindowManagerFuncs = windowManagerFuncs;
166 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
167 mDreamManager = IDreamManager.Stub.asInterface(
168 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000169 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
170 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800171 mLockPatternUtils = new LockPatternUtils(mContext);
172 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400173
174 // receive broadcasts
175 IntentFilter filter = new IntentFilter();
176 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
177 filter.addAction(Intent.ACTION_SCREEN_OFF);
178 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
179 context.registerReceiver(mBroadcastReceiver, filter);
180
181 ConnectivityManager cm = (ConnectivityManager)
182 context.getSystemService(Context.CONNECTIVITY_SERVICE);
183 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
184
185 // get notified of phone state changes
186 TelephonyManager telephonyManager =
187 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
188 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
189 mContext.getContentResolver().registerContentObserver(
190 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
191 mAirplaneModeObserver);
192 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
193 mHasVibrator = vibrator != null && vibrator.hasVibrator();
194
195 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
196 R.bool.config_useFixedVolume);
197
198 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500199 mScreenshotHelper = new ScreenshotHelper(context);
Jason Monk361915c2017-03-21 20:33:59 -0400200 }
201
202 /**
203 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400204 *
Jason Monk361915c2017-03-21 20:33:59 -0400205 * @param keyguardShowing True if keyguard is showing
206 */
207 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
208 mKeyguardShowing = keyguardShowing;
209 mDeviceProvisioned = isDeviceProvisioned;
210 if (mDialog != null) {
211 mDialog.dismiss();
212 mDialog = null;
213 // Show delayed, so that the dismiss of the previous dialog completes
214 mHandler.sendEmptyMessage(MESSAGE_SHOW);
215 } else {
216 handleShow();
217 }
218 }
219
Charles He9851a8d2017-10-10 17:31:30 +0100220 /**
221 * Dismiss the global actions dialog, if it's currently shown
222 */
223 public void dismissDialog() {
224 mHandler.removeMessages(MESSAGE_DISMISS);
225 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
226 }
227
Jason Monk361915c2017-03-21 20:33:59 -0400228 private void awakenIfNecessary() {
229 if (mDreamManager != null) {
230 try {
231 if (mDreamManager.isDreaming()) {
232 mDreamManager.awaken();
233 }
234 } catch (RemoteException e) {
235 // we tried
236 }
237 }
238 }
239
240 private void handleShow() {
241 awakenIfNecessary();
242 mDialog = createDialog();
243 prepareDialog();
244
245 // If we only have 1 item and it's a simple press action, just do this action.
246 if (mAdapter.getCount() == 1
247 && mAdapter.getItem(0) instanceof SinglePressAction
248 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
249 ((SinglePressAction) mAdapter.getItem(0)).onPress();
250 } else {
251 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
252 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100253 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400254 mDialog.getWindow().setAttributes(attrs);
255 mDialog.show();
256 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400257 }
258 }
259
260 /**
261 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400262 *
Jason Monk361915c2017-03-21 20:33:59 -0400263 * @return A new dialog.
264 */
265 private ActionsDialog createDialog() {
266 // Simple toggle style if there's no vibrator, otherwise use a tri-state
267 if (!mHasVibrator) {
268 mSilentModeAction = new SilentModeToggleAction();
269 } else {
270 mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler);
271 }
272 mAirplaneModeOn = new ToggleAction(
273 R.drawable.ic_lock_airplane_mode,
274 R.drawable.ic_lock_airplane_mode_off,
275 R.string.global_actions_toggle_airplane_mode,
276 R.string.global_actions_airplane_mode_on_status,
277 R.string.global_actions_airplane_mode_off_status) {
278
279 void onToggle(boolean on) {
280 if (mHasTelephony && Boolean.parseBoolean(
281 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
282 mIsWaitingForEcmExit = true;
283 // Launch ECM exit dialog
284 Intent ecmDialogIntent =
285 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
286 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
287 mContext.startActivity(ecmDialogIntent);
288 } else {
289 changeAirplaneModeSystemSetting(on);
290 }
291 }
292
293 @Override
294 protected void changeStateFromPress(boolean buttonOn) {
295 if (!mHasTelephony) return;
296
297 // In ECM mode airplane state cannot be changed
298 if (!(Boolean.parseBoolean(
299 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
300 mState = buttonOn ? State.TurningOn : State.TurningOff;
301 mAirplaneState = mState;
302 }
303 }
304
305 public boolean showDuringKeyguard() {
306 return true;
307 }
308
309 public boolean showBeforeProvisioning() {
310 return false;
311 }
312 };
313 onAirplaneModeChanged();
314
315 mItems = new ArrayList<Action>();
316 String[] defaultActions = mContext.getResources().getStringArray(
317 R.array.config_globalActionsList);
318
319 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000320 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800321 mHasLockdownButton = false;
yuanjiahsu88363e92018-09-06 19:23:52 +0800322 mSeparatedEmergencyButtonEnabled = FeatureFlagUtils
323 .isEnabled(mContext, FeatureFlagUtils.EMERGENCY_DIAL_SHORTCUTS);
Jason Monk361915c2017-03-21 20:33:59 -0400324 for (int i = 0; i < defaultActions.length; i++) {
325 String actionKey = defaultActions[i];
326 if (addedKeys.contains(actionKey)) {
327 // If we already have added this, don't add it again.
328 continue;
329 }
330 if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
331 mItems.add(new PowerAction());
332 } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
333 mItems.add(mAirplaneModeOn);
334 } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
335 if (Settings.Global.getInt(mContext.getContentResolver(),
336 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
337 mItems.add(new BugReportAction());
338 }
339 } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
340 if (mShowSilentToggle) {
341 mItems.add(mSilentModeAction);
342 }
343 } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
344 if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
345 addUsersToMenu(mItems);
346 }
347 } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
348 mItems.add(getSettingsAction());
349 } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
Chad Brubaker02cd6cf2018-05-01 14:59:33 -0700350 if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
351 Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800352 && shouldDisplayLockdown()) {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700353 mItems.add(getLockdownAction());
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800354 mHasLockdownButton = true;
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700355 }
Jason Monk361915c2017-03-21 20:33:59 -0400356 } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
357 mItems.add(getVoiceAssistAction());
358 } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
359 mItems.add(getAssistAction());
360 } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
361 mItems.add(new RestartAction());
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500362 } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
363 mItems.add(new ScreenshotAction());
Alex Chau04458852017-11-27 18:21:23 +0000364 } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
Alex Chaud7958272017-12-08 11:30:52 +0000365 if (mDevicePolicyManager.isLogoutEnabled()
Alex Chau04458852017-11-27 18:21:23 +0000366 && getCurrentUser().id != UserHandle.USER_SYSTEM) {
367 mItems.add(new LogoutAction());
368 mHasLogoutButton = true;
369 }
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800370 } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
371 if (mSeparatedEmergencyButtonEnabled
372 && !mEmergencyAffordanceManager.needsEmergencyAffordance()) {
373 mItems.add(new EmergencyDialerAction());
374 }
Jason Monk361915c2017-03-21 20:33:59 -0400375 } else {
376 Log.e(TAG, "Invalid global action key " + actionKey);
377 }
378 // Add here so we don't add more than one.
379 addedKeys.add(actionKey);
380 }
381
382 if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
383 mItems.add(getEmergencyAction());
384 }
385
386 mAdapter = new MyAdapter();
387
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700388 OnItemLongClickListener onItemLongClickListener = (parent, view, position, id) -> {
389 final Action action = mAdapter.getItem(position);
390 if (action instanceof LongPressAction) {
391 mDialog.dismiss();
392 return ((LongPressAction) action).onLongPress();
Jason Monk16fbd9d2017-04-27 14:28:49 -0400393 }
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700394 return false;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400395 };
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +0800396 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener,
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800397 mSeparatedEmergencyButtonEnabled);
Jason Monk361915c2017-03-21 20:33:59 -0400398 dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
Lucas Dupinc1cc7592017-05-22 15:56:16 -0700399 dialog.setKeyguardShowing(mKeyguardShowing);
Jason Monk361915c2017-03-21 20:33:59 -0400400
401 dialog.setOnDismissListener(this);
402
403 return dialog;
404 }
405
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800406 private boolean shouldDisplayLockdown() {
407 int userId = getCurrentUser().id;
408 // Lockdown is meaningless without a place to go.
409 if (!mKeyguardManager.isDeviceSecure(userId)) {
410 return false;
411 }
412
413 // Only show the lockdown button if the device isn't locked down (for whatever reason).
414 int state = mLockPatternUtils.getStrongAuthForUser(userId);
415 return (state == STRONG_AUTH_NOT_REQUIRED
416 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
417 }
418
Jason Monk361915c2017-03-21 20:33:59 -0400419 private final class PowerAction extends SinglePressAction implements LongPressAction {
420 private PowerAction() {
421 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400422 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400423 }
424
425 @Override
426 public boolean onLongPress() {
427 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
428 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
429 mWindowManagerFuncs.reboot(true);
430 return true;
431 }
432 return false;
433 }
434
435 @Override
436 public boolean showDuringKeyguard() {
437 return true;
438 }
439
440 @Override
441 public boolean showBeforeProvisioning() {
442 return true;
443 }
444
445 @Override
446 public void onPress() {
447 // shutdown by making sure radio and power are handled accordingly.
448 mWindowManagerFuncs.shutdown();
449 }
450 }
451
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800452 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800453 private EmergencyDialerAction() {
454 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +0800455 R.string.global_action_emergency);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800456 }
457
458 @Override
459 public void onPress() {
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
866 /**
867 * The adapter used for the list within the global actions dialog, taking
868 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400869 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
870 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400871 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
872 */
873 private class MyAdapter extends BaseAdapter {
874
875 public int getCount() {
876 int count = 0;
877
878 for (int i = 0; i < mItems.size(); i++) {
879 final Action action = mItems.get(i);
880
881 if (mKeyguardShowing && !action.showDuringKeyguard()) {
882 continue;
883 }
884 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
885 continue;
886 }
887 count++;
888 }
889 return count;
890 }
891
892 @Override
893 public boolean isEnabled(int position) {
894 return getItem(position).isEnabled();
895 }
896
897 @Override
898 public boolean areAllItemsEnabled() {
899 return false;
900 }
901
902 public Action getItem(int position) {
903
904 int filteredPos = 0;
905 for (int i = 0; i < mItems.size(); i++) {
906 final Action action = mItems.get(i);
907 if (mKeyguardShowing && !action.showDuringKeyguard()) {
908 continue;
909 }
910 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
911 continue;
912 }
913 if (filteredPos == position) {
914 return action;
915 }
916 filteredPos++;
917 }
918
919 throw new IllegalArgumentException("position " + position
920 + " out of range of showable actions"
921 + ", filtered count=" + getCount()
922 + ", keyguardshowing=" + mKeyguardShowing
923 + ", provisioned=" + mDeviceProvisioned);
924 }
925
926
927 public long getItemId(int position) {
928 return position;
929 }
930
931 public View getView(int position, View convertView, ViewGroup parent) {
932 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400933 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500934 // Everything but screenshot, the last item, gets white background.
935 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400936 HardwareUiLayout.get(parent).setDivisionView(view);
937 }
938 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400939 }
940 }
941
942 // note: the scheme below made more sense when we were planning on having
943 // 8 different things in the global actions dialog. seems overkill with
944 // only 3 items now, but may as well keep this flexible approach so it will
945 // be easy should someone decide at the last minute to include something
946 // else, such as 'enable wifi', or 'enable bluetooth'
947
948 /**
949 * What each item in the global actions dialog must be able to support.
950 */
951 private interface Action {
952 /**
953 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400954 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400955 */
956 CharSequence getLabelForAccessibility(Context context);
957
958 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
959
960 void onPress();
961
962 /**
963 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400964 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400965 */
966 boolean showDuringKeyguard();
967
968 /**
969 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400970 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400971 */
972 boolean showBeforeProvisioning();
973
974 boolean isEnabled();
975 }
976
977 /**
978 * An action that also supports long press.
979 */
980 private interface LongPressAction extends Action {
981 boolean onLongPress();
982 }
983
984 /**
985 * A single press action maintains no state, just responds to a press
986 * and takes an action.
987 */
988 private static abstract class SinglePressAction implements Action {
989 private final int mIconResId;
990 private final Drawable mIcon;
991 private final int mMessageResId;
992 private final CharSequence mMessage;
993
994 protected SinglePressAction(int iconResId, int messageResId) {
995 mIconResId = iconResId;
996 mMessageResId = messageResId;
997 mMessage = null;
998 mIcon = null;
999 }
1000
1001 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1002 mIconResId = iconResId;
1003 mMessageResId = 0;
1004 mMessage = message;
1005 mIcon = icon;
1006 }
1007
1008 public boolean isEnabled() {
1009 return true;
1010 }
1011
1012 public String getStatus() {
1013 return null;
1014 }
1015
1016 abstract public void onPress();
1017
1018 public CharSequence getLabelForAccessibility(Context context) {
1019 if (mMessage != null) {
1020 return mMessage;
1021 } else {
1022 return context.getString(mMessageResId);
1023 }
1024 }
1025
1026 public View create(
1027 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001028 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1029 false);
Jason Monk361915c2017-03-21 20:33:59 -04001030
1031 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1032 TextView messageView = (TextView) v.findViewById(R.id.message);
1033
1034 TextView statusView = (TextView) v.findViewById(R.id.status);
1035 final String status = getStatus();
1036 if (!TextUtils.isEmpty(status)) {
1037 statusView.setText(status);
1038 } else {
1039 statusView.setVisibility(View.GONE);
1040 }
1041 if (mIcon != null) {
1042 icon.setImageDrawable(mIcon);
1043 icon.setScaleType(ScaleType.CENTER_CROP);
1044 } else if (mIconResId != 0) {
1045 icon.setImageDrawable(context.getDrawable(mIconResId));
1046 }
1047 if (mMessage != null) {
1048 messageView.setText(mMessage);
1049 } else {
1050 messageView.setText(mMessageResId);
1051 }
1052
1053 return v;
1054 }
1055 }
1056
1057 /**
1058 * A toggle action knows whether it is on or off, and displays an icon
1059 * and status message accordingly.
1060 */
1061 private static abstract class ToggleAction implements Action {
1062
1063 enum State {
1064 Off(false),
1065 TurningOn(true),
1066 TurningOff(true),
1067 On(false);
1068
1069 private final boolean inTransition;
1070
1071 State(boolean intermediate) {
1072 inTransition = intermediate;
1073 }
1074
1075 public boolean inTransition() {
1076 return inTransition;
1077 }
1078 }
1079
1080 protected State mState = State.Off;
1081
1082 // prefs
1083 protected int mEnabledIconResId;
1084 protected int mDisabledIconResid;
1085 protected int mMessageResId;
1086 protected int mEnabledStatusMessageResId;
1087 protected int mDisabledStatusMessageResId;
1088
1089 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001090 * @param enabledIconResId The icon for when this action is on.
1091 * @param disabledIconResid The icon for when this action is off.
1092 * @param message The general information message, e.g 'Silent Mode'
1093 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001094 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1095 */
1096 public ToggleAction(int enabledIconResId,
1097 int disabledIconResid,
1098 int message,
1099 int enabledStatusMessageResId,
1100 int disabledStatusMessageResId) {
1101 mEnabledIconResId = enabledIconResId;
1102 mDisabledIconResid = disabledIconResid;
1103 mMessageResId = message;
1104 mEnabledStatusMessageResId = enabledStatusMessageResId;
1105 mDisabledStatusMessageResId = disabledStatusMessageResId;
1106 }
1107
1108 /**
1109 * Override to make changes to resource IDs just before creating the
1110 * View.
1111 */
1112 void willCreate() {
1113
1114 }
1115
1116 @Override
1117 public CharSequence getLabelForAccessibility(Context context) {
1118 return context.getString(mMessageResId);
1119 }
1120
1121 public View create(Context context, View convertView, ViewGroup parent,
1122 LayoutInflater inflater) {
1123 willCreate();
1124
1125 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001126 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001127
1128 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1129 TextView messageView = (TextView) v.findViewById(R.id.message);
1130 TextView statusView = (TextView) v.findViewById(R.id.status);
1131 final boolean enabled = isEnabled();
1132
1133 if (messageView != null) {
1134 messageView.setText(mMessageResId);
1135 messageView.setEnabled(enabled);
1136 }
1137
1138 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1139 if (icon != null) {
1140 icon.setImageDrawable(context.getDrawable(
1141 (on ? mEnabledIconResId : mDisabledIconResid)));
1142 icon.setEnabled(enabled);
1143 }
1144
1145 if (statusView != null) {
1146 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1147 statusView.setVisibility(View.VISIBLE);
1148 statusView.setEnabled(enabled);
1149 }
1150 v.setEnabled(enabled);
1151
1152 return v;
1153 }
1154
1155 public final void onPress() {
1156 if (mState.inTransition()) {
1157 Log.w(TAG, "shouldn't be able to toggle when in transition");
1158 return;
1159 }
1160
1161 final boolean nowOn = !(mState == State.On);
1162 onToggle(nowOn);
1163 changeStateFromPress(nowOn);
1164 }
1165
1166 public boolean isEnabled() {
1167 return !mState.inTransition();
1168 }
1169
1170 /**
1171 * Implementations may override this if their state can be in on of the intermediate
1172 * states until some notification is received (e.g airplane mode is 'turning off' until
1173 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001174 *
Jason Monk361915c2017-03-21 20:33:59 -04001175 * @param buttonOn Whether the button was turned on or off
1176 */
1177 protected void changeStateFromPress(boolean buttonOn) {
1178 mState = buttonOn ? State.On : State.Off;
1179 }
1180
1181 abstract void onToggle(boolean on);
1182
1183 public void updateState(State state) {
1184 mState = state;
1185 }
1186 }
1187
1188 private class SilentModeToggleAction extends ToggleAction {
1189 public SilentModeToggleAction() {
1190 super(R.drawable.ic_audio_vol_mute,
1191 R.drawable.ic_audio_vol,
1192 R.string.global_action_toggle_silent_mode,
1193 R.string.global_action_silent_mode_on_status,
1194 R.string.global_action_silent_mode_off_status);
1195 }
1196
1197 void onToggle(boolean on) {
1198 if (on) {
1199 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1200 } else {
1201 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1202 }
1203 }
1204
1205 public boolean showDuringKeyguard() {
1206 return true;
1207 }
1208
1209 public boolean showBeforeProvisioning() {
1210 return false;
1211 }
1212 }
1213
1214 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1215
Jason Monk16fbd9d2017-04-27 14:28:49 -04001216 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001217
1218 private final AudioManager mAudioManager;
1219 private final Handler mHandler;
1220 private final Context mContext;
1221
1222 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1223 mAudioManager = audioManager;
1224 mHandler = handler;
1225 mContext = context;
1226 }
1227
1228 private int ringerModeToIndex(int ringerMode) {
1229 // They just happen to coincide
1230 return ringerMode;
1231 }
1232
1233 private int indexToRingerMode(int index) {
1234 // They just happen to coincide
1235 return index;
1236 }
1237
1238 @Override
1239 public CharSequence getLabelForAccessibility(Context context) {
1240 return null;
1241 }
1242
1243 public View create(Context context, View convertView, ViewGroup parent,
1244 LayoutInflater inflater) {
1245 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1246
1247 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1248 for (int i = 0; i < 3; i++) {
1249 View itemView = v.findViewById(ITEM_IDS[i]);
1250 itemView.setSelected(selectedIndex == i);
1251 // Set up click handler
1252 itemView.setTag(i);
1253 itemView.setOnClickListener(this);
1254 }
1255 return v;
1256 }
1257
1258 public void onPress() {
1259 }
1260
1261 public boolean showDuringKeyguard() {
1262 return true;
1263 }
1264
1265 public boolean showBeforeProvisioning() {
1266 return false;
1267 }
1268
1269 public boolean isEnabled() {
1270 return true;
1271 }
1272
1273 void willCreate() {
1274 }
1275
1276 public void onClick(View v) {
1277 if (!(v.getTag() instanceof Integer)) return;
1278
1279 int index = (Integer) v.getTag();
1280 mAudioManager.setRingerMode(indexToRingerMode(index));
1281 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1282 }
1283 }
1284
1285 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1286 public void onReceive(Context context, Intent intent) {
1287 String action = intent.getAction();
1288 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1289 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1290 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1291 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001292 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001293 }
1294 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1295 // Airplane mode can be changed after ECM exits if airplane toggle button
1296 // is pressed during ECM mode
1297 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1298 mIsWaitingForEcmExit) {
1299 mIsWaitingForEcmExit = false;
1300 changeAirplaneModeSystemSetting(true);
1301 }
1302 }
1303 }
1304 };
1305
1306 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1307 @Override
1308 public void onServiceStateChanged(ServiceState serviceState) {
1309 if (!mHasTelephony) return;
1310 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1311 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1312 mAirplaneModeOn.updateState(mAirplaneState);
1313 mAdapter.notifyDataSetChanged();
1314 }
1315 };
1316
1317 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1318 @Override
1319 public void onReceive(Context context, Intent intent) {
1320 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1321 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1322 }
1323 }
1324 };
1325
1326 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1327 @Override
1328 public void onChange(boolean selfChange) {
1329 onAirplaneModeChanged();
1330 }
1331 };
1332
1333 private static final int MESSAGE_DISMISS = 0;
1334 private static final int MESSAGE_REFRESH = 1;
1335 private static final int MESSAGE_SHOW = 2;
1336 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1337
1338 private Handler mHandler = new Handler() {
1339 public void handleMessage(Message msg) {
1340 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001341 case MESSAGE_DISMISS:
1342 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001343 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1344 mDialog.dismissImmediately();
1345 } else {
1346 mDialog.dismiss();
1347 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001348 mDialog = null;
1349 }
1350 break;
1351 case MESSAGE_REFRESH:
1352 refreshSilentMode();
1353 mAdapter.notifyDataSetChanged();
1354 break;
1355 case MESSAGE_SHOW:
1356 handleShow();
1357 break;
Jason Monk361915c2017-03-21 20:33:59 -04001358 }
1359 }
1360 };
1361
1362 private void onAirplaneModeChanged() {
1363 // Let the service state callbacks handle the state.
1364 if (mHasTelephony) return;
1365
1366 boolean airplaneModeOn = Settings.Global.getInt(
1367 mContext.getContentResolver(),
1368 Settings.Global.AIRPLANE_MODE_ON,
1369 0) == 1;
1370 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1371 mAirplaneModeOn.updateState(mAirplaneState);
1372 }
1373
1374 /**
1375 * Change the airplane mode system setting
1376 */
1377 private void changeAirplaneModeSystemSetting(boolean on) {
1378 Settings.Global.putInt(
1379 mContext.getContentResolver(),
1380 Settings.Global.AIRPLANE_MODE_ON,
1381 on ? 1 : 0);
1382 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1383 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1384 intent.putExtra("state", on);
1385 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1386 if (!mHasTelephony) {
1387 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1388 }
1389 }
1390
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001391 private static final class ActionsDialog extends Dialog implements DialogInterface,
1392 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001393
Jason Monk16fbd9d2017-04-27 14:28:49 -04001394 private final Context mContext;
1395 private final MyAdapter mAdapter;
1396 private final LinearLayout mListView;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001397 private final FrameLayout mSeparatedView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001398 private final HardwareUiLayout mHardwareLayout;
1399 private final OnClickListener mClickListener;
1400 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001401 private final GradientDrawable mGradientDrawable;
1402 private final ColorExtractor mColorExtractor;
1403 private boolean mKeyguardShowing;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001404 private boolean mShouldDisplaySeparatedButton;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001405
1406 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001407 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001408 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001409 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001410 mAdapter = adapter;
1411 mClickListener = clickListener;
1412 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001413 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001414 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001415 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001416
1417 // Window initialization
1418 Window window = getWindow();
1419 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001420 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1421 window.getDecorView();
1422 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1423 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1424 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1425 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1426 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001427 window.addFlags(
1428 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001429 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001430 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001431 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1432 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1433 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1434 window.setBackgroundDrawable(mGradientDrawable);
1435 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1436
Jason Monk16fbd9d2017-04-27 14:28:49 -04001437 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001438 mListView = findViewById(android.R.id.list);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001439 mSeparatedView = findViewById(com.android.systemui.R.id.separated_button);
1440 if (!mShouldDisplaySeparatedButton) {
1441 mSeparatedView.setVisibility(View.GONE);
1442 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001443 mHardwareLayout = HardwareUiLayout.get(mListView);
1444 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001445 mHardwareLayout.setHasSeparatedButton(mShouldDisplaySeparatedButton);
Phil Weaver8583ae82018-02-13 11:01:24 -08001446 setTitle(R.string.global_actions);
Phil Weaver9054e092018-04-27 16:28:50 -07001447 mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1448 @Override
1449 public boolean dispatchPopulateAccessibilityEvent(
1450 View host, AccessibilityEvent event) {
1451 // Populate the title here, just as Activity does
1452 event.getText().add(mContext.getString(R.string.global_actions));
1453 return true;
1454 }
1455 });
Jason Monk361915c2017-03-21 20:33:59 -04001456 }
1457
Jason Monk16fbd9d2017-04-27 14:28:49 -04001458 private void updateList() {
1459 mListView.removeAllViews();
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001460 mSeparatedView.removeAllViews();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001461 for (int i = 0; i < mAdapter.getCount(); i++) {
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001462 ViewGroup parentView = mShouldDisplaySeparatedButton && i == mAdapter.getCount() - 1
1463 ? mSeparatedView : mListView;
1464 View v = mAdapter.getView(i, null, parentView);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001465 final int pos = i;
1466 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1467 v.setOnLongClickListener(view ->
1468 mLongClickListener.onItemLongClick(null, v, pos, 0));
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001469 parentView.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001470 }
Jason Monk361915c2017-03-21 20:33:59 -04001471 }
1472
1473 @Override
1474 protected void onStart() {
1475 super.setCanceledOnTouchOutside(true);
1476 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001477 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001478
1479 Point displaySize = new Point();
1480 mContext.getDisplay().getRealSize(displaySize);
1481 mColorExtractor.addOnColorsChangedListener(this);
1482 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1483 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1484 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001485 updateColors(colors, false /* animate */);
1486 }
1487
1488 /**
1489 * Updates background and system bars according to current GradientColors.
1490 * @param colors Colors and hints to use.
1491 * @param animate Interpolates gradient if true, just sets otherwise.
1492 */
1493 private void updateColors(GradientColors colors, boolean animate) {
1494 mGradientDrawable.setColors(colors, animate);
1495 View decorView = getWindow().getDecorView();
1496 if (colors.supportsDarkText()) {
1497 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1498 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1499 } else {
1500 decorView.setSystemUiVisibility(0);
1501 }
Jason Monk361915c2017-03-21 20:33:59 -04001502 }
1503
1504 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001505 protected void onStop() {
1506 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001507 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001508 }
1509
1510 @Override
1511 public void show() {
1512 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001513 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001514 mHardwareLayout.setTranslationX(getAnimTranslation());
1515 mHardwareLayout.setAlpha(0);
1516 mHardwareLayout.animate()
1517 .alpha(1)
1518 .translationX(0)
1519 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001520 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001521 .setUpdateListener(animation -> {
1522 int alpha = (int) ((Float) animation.getAnimatedValue()
1523 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1524 mGradientDrawable.setAlpha(alpha);
1525 })
Jason Monka7af3b62017-07-07 11:35:13 -04001526 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001527 .start();
1528 }
1529
1530 @Override
1531 public void dismiss() {
1532 mHardwareLayout.setTranslationX(0);
1533 mHardwareLayout.setAlpha(1);
1534 mHardwareLayout.animate()
1535 .alpha(0)
1536 .translationX(getAnimTranslation())
1537 .setDuration(300)
1538 .withEndAction(() -> super.dismiss())
1539 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001540 .setUpdateListener(animation -> {
1541 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1542 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1543 mGradientDrawable.setAlpha(alpha);
1544 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001545 .start();
1546 }
1547
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001548 void dismissImmediately() {
1549 super.dismiss();
1550 }
1551
Jason Monk16fbd9d2017-04-27 14:28:49 -04001552 private float getAnimTranslation() {
1553 return getContext().getResources().getDimension(
1554 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001555 }
1556
1557 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001558 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001559 if (mKeyguardShowing) {
1560 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001561 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1562 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001563 }
1564 } else {
1565 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001566 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1567 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001568 }
1569 }
1570 }
1571
1572 public void setKeyguardShowing(boolean keyguardShowing) {
1573 mKeyguardShowing = keyguardShowing;
1574 }
Jason Monk361915c2017-03-21 20:33:59 -04001575 }
1576}