blob: 28f86ec286268286b294664c5864a5f03ffa2251 [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,
104 DialogInterface.OnClickListener {
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);
400
401 return dialog;
402 }
403
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800404 private boolean shouldDisplayLockdown() {
405 int userId = getCurrentUser().id;
406 // Lockdown is meaningless without a place to go.
407 if (!mKeyguardManager.isDeviceSecure(userId)) {
408 return false;
409 }
410
411 // Only show the lockdown button if the device isn't locked down (for whatever reason).
412 int state = mLockPatternUtils.getStrongAuthForUser(userId);
413 return (state == STRONG_AUTH_NOT_REQUIRED
414 || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
415 }
416
Jason Monk361915c2017-03-21 20:33:59 -0400417 private final class PowerAction extends SinglePressAction implements LongPressAction {
418 private PowerAction() {
419 super(R.drawable.ic_lock_power_off,
Jason Monk16fbd9d2017-04-27 14:28:49 -0400420 R.string.global_action_power_off);
Jason Monk361915c2017-03-21 20:33:59 -0400421 }
422
423 @Override
424 public boolean onLongPress() {
425 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
426 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
427 mWindowManagerFuncs.reboot(true);
428 return true;
429 }
430 return false;
431 }
432
433 @Override
434 public boolean showDuringKeyguard() {
435 return true;
436 }
437
438 @Override
439 public boolean showBeforeProvisioning() {
440 return true;
441 }
442
443 @Override
444 public void onPress() {
445 // shutdown by making sure radio and power are handled accordingly.
446 mWindowManagerFuncs.shutdown();
447 }
448 }
449
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800450 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800451 private EmergencyDialerAction() {
452 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +0800453 R.string.global_action_emergency);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800454 }
455
456 @Override
457 public void onPress() {
Shaotang Lif9a69e22018-07-04 14:18:40 +0800458 Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800459 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Shaotang Lif9a69e22018-07-04 14:18:40 +0800460 intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
461 EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
Wesley.CW Wang74b95792018-05-28 16:39:27 +0800462 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
463 }
464
465 @Override
466 public boolean showDuringKeyguard() {
467 return true;
468 }
469
470 @Override
471 public boolean showBeforeProvisioning() {
472 return true;
473 }
474 }
475
Jason Monk361915c2017-03-21 20:33:59 -0400476 private final class RestartAction extends SinglePressAction implements LongPressAction {
477 private RestartAction() {
478 super(R.drawable.ic_restart, R.string.global_action_restart);
479 }
480
481 @Override
482 public boolean onLongPress() {
483 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
484 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
485 mWindowManagerFuncs.reboot(true);
486 return true;
487 }
488 return false;
489 }
490
491 @Override
492 public boolean showDuringKeyguard() {
493 return true;
494 }
495
496 @Override
497 public boolean showBeforeProvisioning() {
498 return true;
499 }
500
501 @Override
502 public void onPress() {
503 mWindowManagerFuncs.reboot(false);
504 }
505 }
506
507
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500508 private class ScreenshotAction extends SinglePressAction {
509 public ScreenshotAction() {
510 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
511 }
512
513 @Override
514 public void onPress() {
515 // Add a little delay before executing, to give the
516 // dialog a chance to go away before it takes a
517 // screenshot.
518 // TODO: instead, omit global action dialog layer
519 mHandler.postDelayed(new Runnable() {
520 @Override
521 public void run() {
522 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
523 MetricsLogger.action(mContext,
524 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
525 }
526 }, 500);
527 }
528
529 @Override
530 public boolean showDuringKeyguard() {
531 return true;
532 }
533
534 @Override
535 public boolean showBeforeProvisioning() {
536 return false;
537 }
538 }
539
Jason Monk361915c2017-03-21 20:33:59 -0400540 private class BugReportAction extends SinglePressAction implements LongPressAction {
541
542 public BugReportAction() {
543 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
544 }
545
546 @Override
547 public void onPress() {
548 // don't actually trigger the bugreport if we are running stability
549 // tests via monkey
550 if (ActivityManager.isUserAMonkey()) {
551 return;
552 }
553 // Add a little delay before executing, to give the
554 // dialog a chance to go away before it takes a
555 // screenshot.
556 mHandler.postDelayed(new Runnable() {
557 @Override
558 public void run() {
559 try {
560 // Take an "interactive" bugreport.
561 MetricsLogger.action(mContext,
562 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
563 ActivityManager.getService().requestBugReport(
564 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
565 } catch (RemoteException e) {
566 }
567 }
568 }, 500);
569 }
570
571 @Override
572 public boolean onLongPress() {
573 // don't actually trigger the bugreport if we are running stability
574 // tests via monkey
575 if (ActivityManager.isUserAMonkey()) {
576 return false;
577 }
578 try {
579 // Take a "full" bugreport.
580 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
581 ActivityManager.getService().requestBugReport(
582 ActivityManager.BUGREPORT_OPTION_FULL);
583 } catch (RemoteException e) {
584 }
585 return false;
586 }
587
588 public boolean showDuringKeyguard() {
589 return true;
590 }
591
592 @Override
593 public boolean showBeforeProvisioning() {
594 return false;
595 }
596
597 @Override
598 public String getStatus() {
599 return mContext.getString(
600 R.string.bugreport_status,
601 Build.VERSION.RELEASE,
602 Build.ID);
603 }
604 }
605
Alex Chau04458852017-11-27 18:21:23 +0000606 private final class LogoutAction extends SinglePressAction {
607 private LogoutAction() {
608 super(R.drawable.ic_logout, R.string.global_action_logout);
609 }
610
611 @Override
612 public boolean showDuringKeyguard() {
613 return true;
614 }
615
616 @Override
617 public boolean showBeforeProvisioning() {
618 return false;
619 }
620
621 @Override
622 public void onPress() {
623 // Add a little delay before executing, to give the dialog a chance to go away before
624 // switching user
625 mHandler.postDelayed(() -> {
626 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000627 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000628 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000629 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000630 } catch (RemoteException re) {
631 Log.e(TAG, "Couldn't logout user " + re);
632 }
633 }, 500);
634 }
635 }
636
Jason Monk361915c2017-03-21 20:33:59 -0400637 private Action getSettingsAction() {
638 return new SinglePressAction(R.drawable.ic_settings,
639 R.string.global_action_settings) {
640
641 @Override
642 public void onPress() {
643 Intent intent = new Intent(Settings.ACTION_SETTINGS);
644 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
645 mContext.startActivity(intent);
646 }
647
648 @Override
649 public boolean showDuringKeyguard() {
650 return true;
651 }
652
653 @Override
654 public boolean showBeforeProvisioning() {
655 return true;
656 }
657 };
658 }
659
660 private Action getEmergencyAction() {
Wesley.CW Wangd560e8f2018-07-06 15:26:21 +0800661 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
662 if(!mSeparatedEmergencyButtonEnabled) {
663 // use un-colored legacy treatment
664 emergencyIcon.setTintList(null);
665 }
666
Jason Monk361915c2017-03-21 20:33:59 -0400667 return new SinglePressAction(R.drawable.emergency_icon,
668 R.string.global_action_emergency) {
669 @Override
670 public void onPress() {
671 mEmergencyAffordanceManager.performEmergencyCall();
672 }
673
674 @Override
675 public boolean showDuringKeyguard() {
676 return true;
677 }
678
679 @Override
680 public boolean showBeforeProvisioning() {
681 return true;
682 }
683 };
684 }
685
686 private Action getAssistAction() {
687 return new SinglePressAction(R.drawable.ic_action_assist_focused,
688 R.string.global_action_assist) {
689 @Override
690 public void onPress() {
691 Intent intent = new Intent(Intent.ACTION_ASSIST);
692 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
693 mContext.startActivity(intent);
694 }
695
696 @Override
697 public boolean showDuringKeyguard() {
698 return true;
699 }
700
701 @Override
702 public boolean showBeforeProvisioning() {
703 return true;
704 }
705 };
706 }
707
708 private Action getVoiceAssistAction() {
709 return new SinglePressAction(R.drawable.ic_voice_search,
710 R.string.global_action_voice_assist) {
711 @Override
712 public void onPress() {
713 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
714 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
715 mContext.startActivity(intent);
716 }
717
718 @Override
719 public boolean showDuringKeyguard() {
720 return true;
721 }
722
723 @Override
724 public boolean showBeforeProvisioning() {
725 return true;
726 }
727 };
728 }
729
730 private Action getLockdownAction() {
Alison Cichowlasd372cde2018-05-16 15:40:45 -0400731 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400732 R.string.global_action_lockdown) {
733
734 @Override
735 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700736 new LockPatternUtils(mContext)
737 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
738 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400739 try {
740 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100741 // Lock profiles (if any) on the background thread.
742 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
743 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400744 } catch (RemoteException e) {
745 Log.e(TAG, "Error while trying to lock device.", e);
746 }
747 }
748
749 @Override
750 public boolean showDuringKeyguard() {
751 return true;
752 }
753
754 @Override
755 public boolean showBeforeProvisioning() {
756 return false;
757 }
758 };
759 }
760
Pavel Grafov059021b2018-05-02 13:44:46 +0100761 private void lockProfiles() {
762 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
763 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
764 final int currentUserId = getCurrentUser().id;
765 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
766 for (final int id : profileIds) {
767 if (id != currentUserId) {
768 tm.setDeviceLockedForUser(id, true);
769 }
770 }
771 }
772
Jason Monk361915c2017-03-21 20:33:59 -0400773 private UserInfo getCurrentUser() {
774 try {
775 return ActivityManager.getService().getCurrentUser();
776 } catch (RemoteException re) {
777 return null;
778 }
779 }
780
781 private boolean isCurrentUserOwner() {
782 UserInfo currentUser = getCurrentUser();
783 return currentUser == null || currentUser.isPrimary();
784 }
785
786 private void addUsersToMenu(ArrayList<Action> items) {
787 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
788 if (um.isUserSwitcherEnabled()) {
789 List<UserInfo> users = um.getUsers();
790 UserInfo currentUser = getCurrentUser();
791 for (final UserInfo user : users) {
792 if (user.supportsSwitchToByUser()) {
793 boolean isCurrentUser = currentUser == null
794 ? user.id == 0 : (currentUser.id == user.id);
795 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
796 : null;
797 SinglePressAction switchToUser = new SinglePressAction(
798 R.drawable.ic_menu_cc, icon,
799 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400800 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400801 public void onPress() {
802 try {
803 ActivityManager.getService().switchUser(user.id);
804 } catch (RemoteException re) {
805 Log.e(TAG, "Couldn't switch user " + re);
806 }
807 }
808
809 public boolean showDuringKeyguard() {
810 return true;
811 }
812
813 public boolean showBeforeProvisioning() {
814 return false;
815 }
816 };
817 items.add(switchToUser);
818 }
819 }
820 }
821 }
822
823 private void prepareDialog() {
824 refreshSilentMode();
825 mAirplaneModeOn.updateState(mAirplaneState);
826 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400827 if (mShowSilentToggle) {
828 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
829 mContext.registerReceiver(mRingerModeReceiver, filter);
830 }
831 }
832
833 private void refreshSilentMode() {
834 if (!mHasVibrator) {
835 final boolean silentModeOn =
836 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400837 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400838 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
839 }
840 }
841
842 /** {@inheritDoc} */
843 public void onDismiss(DialogInterface dialog) {
844 mWindowManagerFuncs.onGlobalActionsHidden();
845 if (mShowSilentToggle) {
846 try {
847 mContext.unregisterReceiver(mRingerModeReceiver);
848 } catch (IllegalArgumentException ie) {
849 // ignore this
850 Log.w(TAG, ie);
851 }
852 }
853 }
854
855 /** {@inheritDoc} */
856 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400857 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400858 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400859 dialog.dismiss();
860 }
Jason Monkfd279662017-06-29 19:37:48 -0400861 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400862 }
863
864 /**
865 * The adapter used for the list within the global actions dialog, taking
866 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400867 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
868 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400869 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
870 */
871 private class MyAdapter extends BaseAdapter {
872
873 public int getCount() {
874 int count = 0;
875
876 for (int i = 0; i < mItems.size(); i++) {
877 final Action action = mItems.get(i);
878
879 if (mKeyguardShowing && !action.showDuringKeyguard()) {
880 continue;
881 }
882 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
883 continue;
884 }
885 count++;
886 }
887 return count;
888 }
889
890 @Override
891 public boolean isEnabled(int position) {
892 return getItem(position).isEnabled();
893 }
894
895 @Override
896 public boolean areAllItemsEnabled() {
897 return false;
898 }
899
900 public Action getItem(int position) {
901
902 int filteredPos = 0;
903 for (int i = 0; i < mItems.size(); i++) {
904 final Action action = mItems.get(i);
905 if (mKeyguardShowing && !action.showDuringKeyguard()) {
906 continue;
907 }
908 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
909 continue;
910 }
911 if (filteredPos == position) {
912 return action;
913 }
914 filteredPos++;
915 }
916
917 throw new IllegalArgumentException("position " + position
918 + " out of range of showable actions"
919 + ", filtered count=" + getCount()
920 + ", keyguardshowing=" + mKeyguardShowing
921 + ", provisioned=" + mDeviceProvisioned);
922 }
923
924
925 public long getItemId(int position) {
926 return position;
927 }
928
929 public View getView(int position, View convertView, ViewGroup parent) {
930 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400931 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500932 // Everything but screenshot, the last item, gets white background.
933 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400934 HardwareUiLayout.get(parent).setDivisionView(view);
935 }
936 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400937 }
938 }
939
940 // note: the scheme below made more sense when we were planning on having
941 // 8 different things in the global actions dialog. seems overkill with
942 // only 3 items now, but may as well keep this flexible approach so it will
943 // be easy should someone decide at the last minute to include something
944 // else, such as 'enable wifi', or 'enable bluetooth'
945
946 /**
947 * What each item in the global actions dialog must be able to support.
948 */
949 private interface Action {
950 /**
951 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400952 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400953 */
954 CharSequence getLabelForAccessibility(Context context);
955
956 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
957
958 void onPress();
959
960 /**
961 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400962 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400963 */
964 boolean showDuringKeyguard();
965
966 /**
967 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400968 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400969 */
970 boolean showBeforeProvisioning();
971
972 boolean isEnabled();
973 }
974
975 /**
976 * An action that also supports long press.
977 */
978 private interface LongPressAction extends Action {
979 boolean onLongPress();
980 }
981
982 /**
983 * A single press action maintains no state, just responds to a press
984 * and takes an action.
985 */
986 private static abstract class SinglePressAction implements Action {
987 private final int mIconResId;
988 private final Drawable mIcon;
989 private final int mMessageResId;
990 private final CharSequence mMessage;
991
992 protected SinglePressAction(int iconResId, int messageResId) {
993 mIconResId = iconResId;
994 mMessageResId = messageResId;
995 mMessage = null;
996 mIcon = null;
997 }
998
999 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1000 mIconResId = iconResId;
1001 mMessageResId = 0;
1002 mMessage = message;
1003 mIcon = icon;
1004 }
1005
1006 public boolean isEnabled() {
1007 return true;
1008 }
1009
1010 public String getStatus() {
1011 return null;
1012 }
1013
1014 abstract public void onPress();
1015
1016 public CharSequence getLabelForAccessibility(Context context) {
1017 if (mMessage != null) {
1018 return mMessage;
1019 } else {
1020 return context.getString(mMessageResId);
1021 }
1022 }
1023
1024 public View create(
1025 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001026 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1027 false);
Jason Monk361915c2017-03-21 20:33:59 -04001028
1029 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1030 TextView messageView = (TextView) v.findViewById(R.id.message);
1031
1032 TextView statusView = (TextView) v.findViewById(R.id.status);
1033 final String status = getStatus();
1034 if (!TextUtils.isEmpty(status)) {
1035 statusView.setText(status);
1036 } else {
1037 statusView.setVisibility(View.GONE);
1038 }
1039 if (mIcon != null) {
1040 icon.setImageDrawable(mIcon);
1041 icon.setScaleType(ScaleType.CENTER_CROP);
1042 } else if (mIconResId != 0) {
1043 icon.setImageDrawable(context.getDrawable(mIconResId));
1044 }
1045 if (mMessage != null) {
1046 messageView.setText(mMessage);
1047 } else {
1048 messageView.setText(mMessageResId);
1049 }
1050
1051 return v;
1052 }
1053 }
1054
1055 /**
1056 * A toggle action knows whether it is on or off, and displays an icon
1057 * and status message accordingly.
1058 */
1059 private static abstract class ToggleAction implements Action {
1060
1061 enum State {
1062 Off(false),
1063 TurningOn(true),
1064 TurningOff(true),
1065 On(false);
1066
1067 private final boolean inTransition;
1068
1069 State(boolean intermediate) {
1070 inTransition = intermediate;
1071 }
1072
1073 public boolean inTransition() {
1074 return inTransition;
1075 }
1076 }
1077
1078 protected State mState = State.Off;
1079
1080 // prefs
1081 protected int mEnabledIconResId;
1082 protected int mDisabledIconResid;
1083 protected int mMessageResId;
1084 protected int mEnabledStatusMessageResId;
1085 protected int mDisabledStatusMessageResId;
1086
1087 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001088 * @param enabledIconResId The icon for when this action is on.
1089 * @param disabledIconResid The icon for when this action is off.
1090 * @param message The general information message, e.g 'Silent Mode'
1091 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001092 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1093 */
1094 public ToggleAction(int enabledIconResId,
1095 int disabledIconResid,
1096 int message,
1097 int enabledStatusMessageResId,
1098 int disabledStatusMessageResId) {
1099 mEnabledIconResId = enabledIconResId;
1100 mDisabledIconResid = disabledIconResid;
1101 mMessageResId = message;
1102 mEnabledStatusMessageResId = enabledStatusMessageResId;
1103 mDisabledStatusMessageResId = disabledStatusMessageResId;
1104 }
1105
1106 /**
1107 * Override to make changes to resource IDs just before creating the
1108 * View.
1109 */
1110 void willCreate() {
1111
1112 }
1113
1114 @Override
1115 public CharSequence getLabelForAccessibility(Context context) {
1116 return context.getString(mMessageResId);
1117 }
1118
1119 public View create(Context context, View convertView, ViewGroup parent,
1120 LayoutInflater inflater) {
1121 willCreate();
1122
1123 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001124 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001125
1126 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1127 TextView messageView = (TextView) v.findViewById(R.id.message);
1128 TextView statusView = (TextView) v.findViewById(R.id.status);
1129 final boolean enabled = isEnabled();
1130
1131 if (messageView != null) {
1132 messageView.setText(mMessageResId);
1133 messageView.setEnabled(enabled);
1134 }
1135
1136 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1137 if (icon != null) {
1138 icon.setImageDrawable(context.getDrawable(
1139 (on ? mEnabledIconResId : mDisabledIconResid)));
1140 icon.setEnabled(enabled);
1141 }
1142
1143 if (statusView != null) {
1144 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1145 statusView.setVisibility(View.VISIBLE);
1146 statusView.setEnabled(enabled);
1147 }
1148 v.setEnabled(enabled);
1149
1150 return v;
1151 }
1152
1153 public final void onPress() {
1154 if (mState.inTransition()) {
1155 Log.w(TAG, "shouldn't be able to toggle when in transition");
1156 return;
1157 }
1158
1159 final boolean nowOn = !(mState == State.On);
1160 onToggle(nowOn);
1161 changeStateFromPress(nowOn);
1162 }
1163
1164 public boolean isEnabled() {
1165 return !mState.inTransition();
1166 }
1167
1168 /**
1169 * Implementations may override this if their state can be in on of the intermediate
1170 * states until some notification is received (e.g airplane mode is 'turning off' until
1171 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001172 *
Jason Monk361915c2017-03-21 20:33:59 -04001173 * @param buttonOn Whether the button was turned on or off
1174 */
1175 protected void changeStateFromPress(boolean buttonOn) {
1176 mState = buttonOn ? State.On : State.Off;
1177 }
1178
1179 abstract void onToggle(boolean on);
1180
1181 public void updateState(State state) {
1182 mState = state;
1183 }
1184 }
1185
1186 private class SilentModeToggleAction extends ToggleAction {
1187 public SilentModeToggleAction() {
1188 super(R.drawable.ic_audio_vol_mute,
1189 R.drawable.ic_audio_vol,
1190 R.string.global_action_toggle_silent_mode,
1191 R.string.global_action_silent_mode_on_status,
1192 R.string.global_action_silent_mode_off_status);
1193 }
1194
1195 void onToggle(boolean on) {
1196 if (on) {
1197 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1198 } else {
1199 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1200 }
1201 }
1202
1203 public boolean showDuringKeyguard() {
1204 return true;
1205 }
1206
1207 public boolean showBeforeProvisioning() {
1208 return false;
1209 }
1210 }
1211
1212 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1213
Jason Monk16fbd9d2017-04-27 14:28:49 -04001214 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001215
1216 private final AudioManager mAudioManager;
1217 private final Handler mHandler;
1218 private final Context mContext;
1219
1220 SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) {
1221 mAudioManager = audioManager;
1222 mHandler = handler;
1223 mContext = context;
1224 }
1225
1226 private int ringerModeToIndex(int ringerMode) {
1227 // They just happen to coincide
1228 return ringerMode;
1229 }
1230
1231 private int indexToRingerMode(int index) {
1232 // They just happen to coincide
1233 return index;
1234 }
1235
1236 @Override
1237 public CharSequence getLabelForAccessibility(Context context) {
1238 return null;
1239 }
1240
1241 public View create(Context context, View convertView, ViewGroup parent,
1242 LayoutInflater inflater) {
1243 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1244
1245 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1246 for (int i = 0; i < 3; i++) {
1247 View itemView = v.findViewById(ITEM_IDS[i]);
1248 itemView.setSelected(selectedIndex == i);
1249 // Set up click handler
1250 itemView.setTag(i);
1251 itemView.setOnClickListener(this);
1252 }
1253 return v;
1254 }
1255
1256 public void onPress() {
1257 }
1258
1259 public boolean showDuringKeyguard() {
1260 return true;
1261 }
1262
1263 public boolean showBeforeProvisioning() {
1264 return false;
1265 }
1266
1267 public boolean isEnabled() {
1268 return true;
1269 }
1270
1271 void willCreate() {
1272 }
1273
1274 public void onClick(View v) {
1275 if (!(v.getTag() instanceof Integer)) return;
1276
1277 int index = (Integer) v.getTag();
1278 mAudioManager.setRingerMode(indexToRingerMode(index));
1279 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1280 }
1281 }
1282
1283 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1284 public void onReceive(Context context, Intent intent) {
1285 String action = intent.getAction();
1286 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1287 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1288 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1289 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001290 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001291 }
1292 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1293 // Airplane mode can be changed after ECM exits if airplane toggle button
1294 // is pressed during ECM mode
1295 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1296 mIsWaitingForEcmExit) {
1297 mIsWaitingForEcmExit = false;
1298 changeAirplaneModeSystemSetting(true);
1299 }
1300 }
1301 }
1302 };
1303
1304 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1305 @Override
1306 public void onServiceStateChanged(ServiceState serviceState) {
1307 if (!mHasTelephony) return;
1308 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1309 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1310 mAirplaneModeOn.updateState(mAirplaneState);
1311 mAdapter.notifyDataSetChanged();
1312 }
1313 };
1314
1315 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1316 @Override
1317 public void onReceive(Context context, Intent intent) {
1318 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1319 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1320 }
1321 }
1322 };
1323
1324 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1325 @Override
1326 public void onChange(boolean selfChange) {
1327 onAirplaneModeChanged();
1328 }
1329 };
1330
1331 private static final int MESSAGE_DISMISS = 0;
1332 private static final int MESSAGE_REFRESH = 1;
1333 private static final int MESSAGE_SHOW = 2;
1334 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1335
1336 private Handler mHandler = new Handler() {
1337 public void handleMessage(Message msg) {
1338 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001339 case MESSAGE_DISMISS:
1340 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001341 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1342 mDialog.dismissImmediately();
1343 } else {
1344 mDialog.dismiss();
1345 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001346 mDialog = null;
1347 }
1348 break;
1349 case MESSAGE_REFRESH:
1350 refreshSilentMode();
1351 mAdapter.notifyDataSetChanged();
1352 break;
1353 case MESSAGE_SHOW:
1354 handleShow();
1355 break;
Jason Monk361915c2017-03-21 20:33:59 -04001356 }
1357 }
1358 };
1359
1360 private void onAirplaneModeChanged() {
1361 // Let the service state callbacks handle the state.
1362 if (mHasTelephony) return;
1363
1364 boolean airplaneModeOn = Settings.Global.getInt(
1365 mContext.getContentResolver(),
1366 Settings.Global.AIRPLANE_MODE_ON,
1367 0) == 1;
1368 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1369 mAirplaneModeOn.updateState(mAirplaneState);
1370 }
1371
1372 /**
1373 * Change the airplane mode system setting
1374 */
1375 private void changeAirplaneModeSystemSetting(boolean on) {
1376 Settings.Global.putInt(
1377 mContext.getContentResolver(),
1378 Settings.Global.AIRPLANE_MODE_ON,
1379 on ? 1 : 0);
1380 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1381 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1382 intent.putExtra("state", on);
1383 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1384 if (!mHasTelephony) {
1385 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1386 }
1387 }
1388
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001389 private static final class ActionsDialog extends Dialog implements DialogInterface,
1390 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001391
Jason Monk16fbd9d2017-04-27 14:28:49 -04001392 private final Context mContext;
1393 private final MyAdapter mAdapter;
1394 private final LinearLayout mListView;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001395 private final FrameLayout mSeparatedView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001396 private final HardwareUiLayout mHardwareLayout;
1397 private final OnClickListener mClickListener;
1398 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001399 private final GradientDrawable mGradientDrawable;
1400 private final ColorExtractor mColorExtractor;
1401 private boolean mKeyguardShowing;
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001402 private boolean mShouldDisplaySeparatedButton;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001403
1404 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001405 OnItemLongClickListener longClickListener, boolean shouldDisplaySeparatedButton) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001406 super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
Lucas Dupin448786c2017-07-24 17:44:25 -07001407 mContext = context;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001408 mAdapter = adapter;
1409 mClickListener = clickListener;
1410 mLongClickListener = longClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001411 mGradientDrawable = new GradientDrawable(mContext);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07001412 mColorExtractor = Dependency.get(SysuiColorExtractor.class);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001413 mShouldDisplaySeparatedButton = shouldDisplaySeparatedButton;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001414
1415 // Window initialization
1416 Window window = getWindow();
1417 window.requestFeature(Window.FEATURE_NO_TITLE);
Adrian Roosedfab3b2018-03-08 18:39:20 +01001418 // Inflate the decor view, so the attributes below are not overwritten by the theme.
1419 window.getDecorView();
1420 window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1421 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1422 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
1423 window.setLayout(MATCH_PARENT, MATCH_PARENT);
1424 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Alison Cichowlas4f19f4a2017-07-25 10:56:16 -04001425 window.addFlags(
1426 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001427 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
Adrian Roosedfab3b2018-03-08 18:39:20 +01001428 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001429 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
1430 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
1431 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
1432 window.setBackgroundDrawable(mGradientDrawable);
1433 window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
1434
Jason Monk16fbd9d2017-04-27 14:28:49 -04001435 setContentView(com.android.systemui.R.layout.global_actions_wrapped);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001436 mListView = findViewById(android.R.id.list);
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001437 mSeparatedView = findViewById(com.android.systemui.R.id.separated_button);
1438 if (!mShouldDisplaySeparatedButton) {
1439 mSeparatedView.setVisibility(View.GONE);
1440 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001441 mHardwareLayout = HardwareUiLayout.get(mListView);
1442 mHardwareLayout.setOutsideTouchListener(view -> dismiss());
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001443 mHardwareLayout.setHasSeparatedButton(mShouldDisplaySeparatedButton);
Phil Weaver8583ae82018-02-13 11:01:24 -08001444 setTitle(R.string.global_actions);
Phil Weaver9054e092018-04-27 16:28:50 -07001445 mListView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
1446 @Override
1447 public boolean dispatchPopulateAccessibilityEvent(
1448 View host, AccessibilityEvent event) {
1449 // Populate the title here, just as Activity does
1450 event.getText().add(mContext.getString(R.string.global_actions));
1451 return true;
1452 }
1453 });
Jason Monk361915c2017-03-21 20:33:59 -04001454 }
1455
Jason Monk16fbd9d2017-04-27 14:28:49 -04001456 private void updateList() {
1457 mListView.removeAllViews();
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001458 mSeparatedView.removeAllViews();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001459 for (int i = 0; i < mAdapter.getCount(); i++) {
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001460 ViewGroup parentView = mShouldDisplaySeparatedButton && i == mAdapter.getCount() - 1
1461 ? mSeparatedView : mListView;
1462 View v = mAdapter.getView(i, null, parentView);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001463 final int pos = i;
1464 v.setOnClickListener(view -> mClickListener.onClick(this, pos));
1465 v.setOnLongClickListener(view ->
1466 mLongClickListener.onItemLongClick(null, v, pos, 0));
Wesley.CW Wang00e2fcf2018-06-15 16:24:57 +08001467 parentView.addView(v);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001468 }
Jason Monk361915c2017-03-21 20:33:59 -04001469 }
1470
1471 @Override
1472 protected void onStart() {
1473 super.setCanceledOnTouchOutside(true);
1474 super.onStart();
Jason Monk16fbd9d2017-04-27 14:28:49 -04001475 updateList();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001476
1477 Point displaySize = new Point();
1478 mContext.getDisplay().getRealSize(displaySize);
1479 mColorExtractor.addOnColorsChangedListener(this);
1480 mGradientDrawable.setScreenSize(displaySize.x, displaySize.y);
1481 GradientColors colors = mColorExtractor.getColors(mKeyguardShowing ?
1482 WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM);
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001483 updateColors(colors, false /* animate */);
1484 }
1485
1486 /**
1487 * Updates background and system bars according to current GradientColors.
1488 * @param colors Colors and hints to use.
1489 * @param animate Interpolates gradient if true, just sets otherwise.
1490 */
1491 private void updateColors(GradientColors colors, boolean animate) {
1492 mGradientDrawable.setColors(colors, animate);
1493 View decorView = getWindow().getDecorView();
1494 if (colors.supportsDarkText()) {
1495 decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
1496 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
1497 } else {
1498 decorView.setSystemUiVisibility(0);
1499 }
Jason Monk361915c2017-03-21 20:33:59 -04001500 }
1501
1502 @Override
Jason Monk16fbd9d2017-04-27 14:28:49 -04001503 protected void onStop() {
1504 super.onStop();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001505 mColorExtractor.removeOnColorsChangedListener(this);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001506 }
1507
1508 @Override
1509 public void show() {
1510 super.show();
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001511 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001512 mHardwareLayout.setTranslationX(getAnimTranslation());
1513 mHardwareLayout.setAlpha(0);
1514 mHardwareLayout.animate()
1515 .alpha(1)
1516 .translationX(0)
1517 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001518 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001519 .setUpdateListener(animation -> {
1520 int alpha = (int) ((Float) animation.getAnimatedValue()
1521 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1522 mGradientDrawable.setAlpha(alpha);
1523 })
Jason Monka7af3b62017-07-07 11:35:13 -04001524 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001525 .start();
1526 }
1527
1528 @Override
1529 public void dismiss() {
1530 mHardwareLayout.setTranslationX(0);
1531 mHardwareLayout.setAlpha(1);
1532 mHardwareLayout.animate()
1533 .alpha(0)
1534 .translationX(getAnimTranslation())
1535 .setDuration(300)
1536 .withEndAction(() -> super.dismiss())
1537 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001538 .setUpdateListener(animation -> {
1539 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1540 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1541 mGradientDrawable.setAlpha(alpha);
1542 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001543 .start();
1544 }
1545
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001546 void dismissImmediately() {
1547 super.dismiss();
1548 }
1549
Jason Monk16fbd9d2017-04-27 14:28:49 -04001550 private float getAnimTranslation() {
1551 return getContext().getResources().getDimension(
1552 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001553 }
1554
1555 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001556 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001557 if (mKeyguardShowing) {
1558 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001559 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1560 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001561 }
1562 } else {
1563 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001564 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1565 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001566 }
1567 }
1568 }
1569
1570 public void setKeyguardShowing(boolean keyguardShowing) {
1571 mKeyguardShowing = keyguardShowing;
1572 }
Jason Monk361915c2017-03-21 20:33:59 -04001573 }
1574}