blob: a195fc9e347b4184c937dbce62728c4c0ce9e31f [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 Wang3004fcb2018-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;
Julia Reynolds42411922017-11-08 11:19:09 -050092import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
Lucas Dupinc1cc7592017-05-22 15:56:16 -070093
Jason Monk361915c2017-03-21 20:33:59 -040094import java.util.ArrayList;
95import java.util.List;
96
97/**
98 * Helper to show the global actions dialog. Each item is an {@link Action} that
99 * may show depending on whether the keyguard is showing, and whether the device
100 * is provisioned.
101 */
Charles He9851a8d2017-10-10 17:31:30 +0100102class GlobalActionsDialog implements DialogInterface.OnDismissListener,
103 DialogInterface.OnClickListener {
Jason Monk361915c2017-03-21 20:33:59 -0400104
105 static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
106 static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
Lucas Dupin1d4a5792018-04-02 15:14:59 -0700107 static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
Jason Monk361915c2017-03-21 20:33:59 -0400108
109 private static final String TAG = "GlobalActionsDialog";
110
111 private static final boolean SHOW_SILENT_TOGGLE = true;
112
113 /* Valid settings for global actions keys.
114 * see config.xml config_globalActionList */
115 private static final String GLOBAL_ACTION_KEY_POWER = "power";
116 private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
117 private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
118 private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
119 private static final String GLOBAL_ACTION_KEY_USERS = "users";
120 private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
121 private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
122 private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
123 private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
124 private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
Alex Chau04458852017-11-27 18:21:23 +0000125 private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800126 private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500127 private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
Jason Monk361915c2017-03-21 20:33:59 -0400128
129 private final Context mContext;
130 private final GlobalActionsManager mWindowManagerFuncs;
131 private final AudioManager mAudioManager;
132 private final IDreamManager mDreamManager;
Alex Chau04458852017-11-27 18:21:23 +0000133 private final DevicePolicyManager mDevicePolicyManager;
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800134 private final LockPatternUtils mLockPatternUtils;
135 private final KeyguardManager mKeyguardManager;
Jason Monk361915c2017-03-21 20:33:59 -0400136
137 private ArrayList<Action> mItems;
138 private ActionsDialog mDialog;
139
140 private Action mSilentModeAction;
141 private ToggleAction mAirplaneModeOn;
142
143 private MyAdapter mAdapter;
144
145 private boolean mKeyguardShowing = false;
146 private boolean mDeviceProvisioned = false;
147 private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
148 private boolean mIsWaitingForEcmExit = false;
149 private boolean mHasTelephony;
150 private boolean mHasVibrator;
Alex Chau04458852017-11-27 18:21:23 +0000151 private boolean mHasLogoutButton;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800152 private boolean mHasLockdownButton;
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800153 private boolean mSeparatedEmergencyButtonEnabled;
Jason Monk361915c2017-03-21 20:33:59 -0400154 private final boolean mShowSilentToggle;
155 private final EmergencyAffordanceManager mEmergencyAffordanceManager;
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500156 private final ScreenshotHelper mScreenshotHelper;
Jason Monk361915c2017-03-21 20:33:59 -0400157
158 /**
159 * @param context everything needs a context :(
160 */
161 public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
Lucas Dupin448786c2017-07-24 17:44:25 -0700162 mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
Jason Monk361915c2017-03-21 20:33:59 -0400163 mWindowManagerFuncs = windowManagerFuncs;
164 mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
165 mDreamManager = IDreamManager.Stub.asInterface(
166 ServiceManager.getService(DreamService.DREAM_SERVICE));
Alex Chau04458852017-11-27 18:21:23 +0000167 mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
168 Context.DEVICE_POLICY_SERVICE);
Chad Brubakerf4075fe2018-01-03 13:23:22 -0800169 mLockPatternUtils = new LockPatternUtils(mContext);
170 mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
Jason Monk361915c2017-03-21 20:33:59 -0400171
172 // receive broadcasts
173 IntentFilter filter = new IntentFilter();
174 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
175 filter.addAction(Intent.ACTION_SCREEN_OFF);
176 filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
177 context.registerReceiver(mBroadcastReceiver, filter);
178
179 ConnectivityManager cm = (ConnectivityManager)
180 context.getSystemService(Context.CONNECTIVITY_SERVICE);
181 mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
182
183 // get notified of phone state changes
184 TelephonyManager telephonyManager =
185 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
186 telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
187 mContext.getContentResolver().registerContentObserver(
188 Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
189 mAirplaneModeObserver);
190 Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
191 mHasVibrator = vibrator != null && vibrator.hasVibrator();
192
193 mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
194 R.bool.config_useFixedVolume);
195
196 mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500197 mScreenshotHelper = new ScreenshotHelper(context);
Jason Monk361915c2017-03-21 20:33:59 -0400198 }
199
200 /**
201 * Show the global actions dialog (creating if necessary)
Jason Monk16fbd9d2017-04-27 14:28:49 -0400202 *
Jason Monk361915c2017-03-21 20:33:59 -0400203 * @param keyguardShowing True if keyguard is showing
204 */
205 public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) {
206 mKeyguardShowing = keyguardShowing;
207 mDeviceProvisioned = isDeviceProvisioned;
208 if (mDialog != null) {
209 mDialog.dismiss();
210 mDialog = null;
211 // Show delayed, so that the dismiss of the previous dialog completes
212 mHandler.sendEmptyMessage(MESSAGE_SHOW);
213 } else {
214 handleShow();
215 }
216 }
217
Charles He9851a8d2017-10-10 17:31:30 +0100218 /**
219 * Dismiss the global actions dialog, if it's currently shown
220 */
221 public void dismissDialog() {
222 mHandler.removeMessages(MESSAGE_DISMISS);
223 mHandler.sendEmptyMessage(MESSAGE_DISMISS);
224 }
225
Jason Monk361915c2017-03-21 20:33:59 -0400226 private void awakenIfNecessary() {
227 if (mDreamManager != null) {
228 try {
229 if (mDreamManager.isDreaming()) {
230 mDreamManager.awaken();
231 }
232 } catch (RemoteException e) {
233 // we tried
234 }
235 }
236 }
237
238 private void handleShow() {
239 awakenIfNecessary();
240 mDialog = createDialog();
241 prepareDialog();
242
243 // If we only have 1 item and it's a simple press action, just do this action.
244 if (mAdapter.getCount() == 1
245 && mAdapter.getItem(0) instanceof SinglePressAction
246 && !(mAdapter.getItem(0) instanceof LongPressAction)) {
247 ((SinglePressAction) mAdapter.getItem(0)).onPress();
248 } else {
249 WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
250 attrs.setTitle("ActionsDialog");
Adrian Roos2f05bb32018-02-19 16:42:27 +0100251 attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
Jason Monk361915c2017-03-21 20:33:59 -0400252 mDialog.getWindow().setAttributes(attrs);
253 mDialog.show();
254 mWindowManagerFuncs.onGlobalActionsShown();
Jason Monk361915c2017-03-21 20:33:59 -0400255 }
256 }
257
258 /**
259 * Create the global actions dialog.
Jason Monk16fbd9d2017-04-27 14:28:49 -0400260 *
Jason Monk361915c2017-03-21 20:33:59 -0400261 * @return A new dialog.
262 */
263 private ActionsDialog createDialog() {
264 // Simple toggle style if there's no vibrator, otherwise use a tri-state
265 if (!mHasVibrator) {
266 mSilentModeAction = new SilentModeToggleAction();
267 } else {
Lucas Dupin40ec6b782018-06-05 19:07:16 -0700268 mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
Jason Monk361915c2017-03-21 20:33:59 -0400269 }
270 mAirplaneModeOn = new ToggleAction(
271 R.drawable.ic_lock_airplane_mode,
272 R.drawable.ic_lock_airplane_mode_off,
273 R.string.global_actions_toggle_airplane_mode,
274 R.string.global_actions_airplane_mode_on_status,
275 R.string.global_actions_airplane_mode_off_status) {
276
277 void onToggle(boolean on) {
278 if (mHasTelephony && Boolean.parseBoolean(
279 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
280 mIsWaitingForEcmExit = true;
281 // Launch ECM exit dialog
282 Intent ecmDialogIntent =
283 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
284 ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
285 mContext.startActivity(ecmDialogIntent);
286 } else {
287 changeAirplaneModeSystemSetting(on);
288 }
289 }
290
291 @Override
292 protected void changeStateFromPress(boolean buttonOn) {
293 if (!mHasTelephony) return;
294
295 // In ECM mode airplane state cannot be changed
296 if (!(Boolean.parseBoolean(
297 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
298 mState = buttonOn ? State.TurningOn : State.TurningOff;
299 mAirplaneState = mState;
300 }
301 }
302
303 public boolean showDuringKeyguard() {
304 return true;
305 }
306
307 public boolean showBeforeProvisioning() {
308 return false;
309 }
310 };
311 onAirplaneModeChanged();
312
313 mItems = new ArrayList<Action>();
314 String[] defaultActions = mContext.getResources().getStringArray(
315 R.array.config_globalActionsList);
316
317 ArraySet<String> addedKeys = new ArraySet<String>();
Alex Chau04458852017-11-27 18:21:23 +0000318 mHasLogoutButton = false;
Chad Brubaker72a73ea2018-01-26 15:56:55 -0800319 mHasLockdownButton = false;
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800320 mSeparatedEmergencyButtonEnabled = Settings.Global.getInt(mContext.getContentResolver(),
321 Settings.Global.FASTER_EMERGENCY_PHONE_CALL_ENABLED, 0) != 0;
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 Wanga9de7922018-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 Wang3004fcb2018-06-15 16:24:57 +0800394 ActionsDialog dialog = new ActionsDialog(mContext, this, mAdapter, onItemLongClickListener,
Wesley.CW Wanga9de7922018-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 Wanga9de7922018-07-06 15:26:21 +0800450 private class EmergencyDialerAction extends SinglePressAction {
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800451 private static final String ACTION_EMERGENCY_DIALER_DIAL =
452 "com.android.phone.EmergencyDialer.DIAL";
453
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800454 private EmergencyDialerAction() {
455 super(R.drawable.ic_faster_emergency,
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +0800456 R.string.global_action_emergency);
Wesley.CW Wang8d072762018-05-28 16:39:27 +0800457 }
458
459 @Override
460 public void onPress() {
461 Intent intent = new Intent(ACTION_EMERGENCY_DIALER_DIAL);
462 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
463 mContext.startActivityAsUser(intent, UserHandle.CURRENT);
464 }
465
466 @Override
467 public boolean showDuringKeyguard() {
468 return true;
469 }
470
471 @Override
472 public boolean showBeforeProvisioning() {
473 return true;
474 }
475 }
476
Jason Monk361915c2017-03-21 20:33:59 -0400477 private final class RestartAction extends SinglePressAction implements LongPressAction {
478 private RestartAction() {
479 super(R.drawable.ic_restart, R.string.global_action_restart);
480 }
481
482 @Override
483 public boolean onLongPress() {
484 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
485 if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
486 mWindowManagerFuncs.reboot(true);
487 return true;
488 }
489 return false;
490 }
491
492 @Override
493 public boolean showDuringKeyguard() {
494 return true;
495 }
496
497 @Override
498 public boolean showBeforeProvisioning() {
499 return true;
500 }
501
502 @Override
503 public void onPress() {
504 mWindowManagerFuncs.reboot(false);
505 }
506 }
507
508
Alison Cichowlasa2cd19e2017-12-06 10:51:21 -0500509 private class ScreenshotAction extends SinglePressAction {
510 public ScreenshotAction() {
511 super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
512 }
513
514 @Override
515 public void onPress() {
516 // Add a little delay before executing, to give the
517 // dialog a chance to go away before it takes a
518 // screenshot.
519 // TODO: instead, omit global action dialog layer
520 mHandler.postDelayed(new Runnable() {
521 @Override
522 public void run() {
523 mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
524 MetricsLogger.action(mContext,
525 MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
526 }
527 }, 500);
528 }
529
530 @Override
531 public boolean showDuringKeyguard() {
532 return true;
533 }
534
535 @Override
536 public boolean showBeforeProvisioning() {
537 return false;
538 }
539 }
540
Jason Monk361915c2017-03-21 20:33:59 -0400541 private class BugReportAction extends SinglePressAction implements LongPressAction {
542
543 public BugReportAction() {
544 super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
545 }
546
547 @Override
548 public void onPress() {
549 // don't actually trigger the bugreport if we are running stability
550 // tests via monkey
551 if (ActivityManager.isUserAMonkey()) {
552 return;
553 }
554 // Add a little delay before executing, to give the
555 // dialog a chance to go away before it takes a
556 // screenshot.
557 mHandler.postDelayed(new Runnable() {
558 @Override
559 public void run() {
560 try {
561 // Take an "interactive" bugreport.
562 MetricsLogger.action(mContext,
563 MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
564 ActivityManager.getService().requestBugReport(
565 ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
566 } catch (RemoteException e) {
567 }
568 }
569 }, 500);
570 }
571
572 @Override
573 public boolean onLongPress() {
574 // don't actually trigger the bugreport if we are running stability
575 // tests via monkey
576 if (ActivityManager.isUserAMonkey()) {
577 return false;
578 }
579 try {
580 // Take a "full" bugreport.
581 MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
582 ActivityManager.getService().requestBugReport(
583 ActivityManager.BUGREPORT_OPTION_FULL);
584 } catch (RemoteException e) {
585 }
586 return false;
587 }
588
589 public boolean showDuringKeyguard() {
590 return true;
591 }
592
593 @Override
594 public boolean showBeforeProvisioning() {
595 return false;
596 }
597
598 @Override
599 public String getStatus() {
600 return mContext.getString(
601 R.string.bugreport_status,
602 Build.VERSION.RELEASE,
603 Build.ID);
604 }
605 }
606
Alex Chau04458852017-11-27 18:21:23 +0000607 private final class LogoutAction extends SinglePressAction {
608 private LogoutAction() {
609 super(R.drawable.ic_logout, R.string.global_action_logout);
610 }
611
612 @Override
613 public boolean showDuringKeyguard() {
614 return true;
615 }
616
617 @Override
618 public boolean showBeforeProvisioning() {
619 return false;
620 }
621
622 @Override
623 public void onPress() {
624 // Add a little delay before executing, to give the dialog a chance to go away before
625 // switching user
626 mHandler.postDelayed(() -> {
627 try {
Alex Chauedb6a012018-01-26 12:52:43 +0000628 int currentUserId = getCurrentUser().id;
Alex Chau04458852017-11-27 18:21:23 +0000629 ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
Alex Chauedb6a012018-01-26 12:52:43 +0000630 ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
Alex Chau04458852017-11-27 18:21:23 +0000631 } catch (RemoteException re) {
632 Log.e(TAG, "Couldn't logout user " + re);
633 }
634 }, 500);
635 }
636 }
637
Jason Monk361915c2017-03-21 20:33:59 -0400638 private Action getSettingsAction() {
639 return new SinglePressAction(R.drawable.ic_settings,
640 R.string.global_action_settings) {
641
642 @Override
643 public void onPress() {
644 Intent intent = new Intent(Settings.ACTION_SETTINGS);
645 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
646 mContext.startActivity(intent);
647 }
648
649 @Override
650 public boolean showDuringKeyguard() {
651 return true;
652 }
653
654 @Override
655 public boolean showBeforeProvisioning() {
656 return true;
657 }
658 };
659 }
660
661 private Action getEmergencyAction() {
Wesley.CW Wanga9de7922018-07-06 15:26:21 +0800662 Drawable emergencyIcon = mContext.getDrawable(R.drawable.emergency_icon);
663 if(!mSeparatedEmergencyButtonEnabled) {
664 // use un-colored legacy treatment
665 emergencyIcon.setTintList(null);
666 }
667
Jason Monk361915c2017-03-21 20:33:59 -0400668 return new SinglePressAction(R.drawable.emergency_icon,
669 R.string.global_action_emergency) {
670 @Override
671 public void onPress() {
672 mEmergencyAffordanceManager.performEmergencyCall();
673 }
674
675 @Override
676 public boolean showDuringKeyguard() {
677 return true;
678 }
679
680 @Override
681 public boolean showBeforeProvisioning() {
682 return true;
683 }
684 };
685 }
686
687 private Action getAssistAction() {
688 return new SinglePressAction(R.drawable.ic_action_assist_focused,
689 R.string.global_action_assist) {
690 @Override
691 public void onPress() {
692 Intent intent = new Intent(Intent.ACTION_ASSIST);
693 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
694 mContext.startActivity(intent);
695 }
696
697 @Override
698 public boolean showDuringKeyguard() {
699 return true;
700 }
701
702 @Override
703 public boolean showBeforeProvisioning() {
704 return true;
705 }
706 };
707 }
708
709 private Action getVoiceAssistAction() {
710 return new SinglePressAction(R.drawable.ic_voice_search,
711 R.string.global_action_voice_assist) {
712 @Override
713 public void onPress() {
714 Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
715 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
716 mContext.startActivity(intent);
717 }
718
719 @Override
720 public boolean showDuringKeyguard() {
721 return true;
722 }
723
724 @Override
725 public boolean showBeforeProvisioning() {
726 return true;
727 }
728 };
729 }
730
731 private Action getLockdownAction() {
Alison Cichowlas21125432018-05-16 15:40:45 -0400732 return new SinglePressAction(R.drawable.ic_lock_lockdown,
Jason Monk361915c2017-03-21 20:33:59 -0400733 R.string.global_action_lockdown) {
734
735 @Override
736 public void onPress() {
Chad Brubaker4f28f0d2017-09-07 14:28:13 -0700737 new LockPatternUtils(mContext)
738 .requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
739 UserHandle.USER_ALL);
Jason Monk361915c2017-03-21 20:33:59 -0400740 try {
741 WindowManagerGlobal.getWindowManagerService().lockNow(null);
Pavel Grafov059021b2018-05-02 13:44:46 +0100742 // Lock profiles (if any) on the background thread.
743 final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
744 bgHandler.post(() -> lockProfiles());
Jason Monk361915c2017-03-21 20:33:59 -0400745 } catch (RemoteException e) {
746 Log.e(TAG, "Error while trying to lock device.", e);
747 }
748 }
749
750 @Override
751 public boolean showDuringKeyguard() {
752 return true;
753 }
754
755 @Override
756 public boolean showBeforeProvisioning() {
757 return false;
758 }
759 };
760 }
761
Pavel Grafov059021b2018-05-02 13:44:46 +0100762 private void lockProfiles() {
763 final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
764 final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
765 final int currentUserId = getCurrentUser().id;
766 final int[] profileIds = um.getEnabledProfileIds(currentUserId);
767 for (final int id : profileIds) {
768 if (id != currentUserId) {
769 tm.setDeviceLockedForUser(id, true);
770 }
771 }
772 }
773
Jason Monk361915c2017-03-21 20:33:59 -0400774 private UserInfo getCurrentUser() {
775 try {
776 return ActivityManager.getService().getCurrentUser();
777 } catch (RemoteException re) {
778 return null;
779 }
780 }
781
782 private boolean isCurrentUserOwner() {
783 UserInfo currentUser = getCurrentUser();
784 return currentUser == null || currentUser.isPrimary();
785 }
786
787 private void addUsersToMenu(ArrayList<Action> items) {
788 UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
789 if (um.isUserSwitcherEnabled()) {
790 List<UserInfo> users = um.getUsers();
791 UserInfo currentUser = getCurrentUser();
792 for (final UserInfo user : users) {
793 if (user.supportsSwitchToByUser()) {
794 boolean isCurrentUser = currentUser == null
795 ? user.id == 0 : (currentUser.id == user.id);
796 Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
797 : null;
798 SinglePressAction switchToUser = new SinglePressAction(
799 R.drawable.ic_menu_cc, icon,
800 (user.name != null ? user.name : "Primary")
Jason Monk16fbd9d2017-04-27 14:28:49 -0400801 + (isCurrentUser ? " \u2714" : "")) {
Jason Monk361915c2017-03-21 20:33:59 -0400802 public void onPress() {
803 try {
804 ActivityManager.getService().switchUser(user.id);
805 } catch (RemoteException re) {
806 Log.e(TAG, "Couldn't switch user " + re);
807 }
808 }
809
810 public boolean showDuringKeyguard() {
811 return true;
812 }
813
814 public boolean showBeforeProvisioning() {
815 return false;
816 }
817 };
818 items.add(switchToUser);
819 }
820 }
821 }
822 }
823
824 private void prepareDialog() {
825 refreshSilentMode();
826 mAirplaneModeOn.updateState(mAirplaneState);
827 mAdapter.notifyDataSetChanged();
Jason Monk361915c2017-03-21 20:33:59 -0400828 if (mShowSilentToggle) {
829 IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
830 mContext.registerReceiver(mRingerModeReceiver, filter);
831 }
832 }
833
834 private void refreshSilentMode() {
835 if (!mHasVibrator) {
836 final boolean silentModeOn =
837 mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
Jason Monk16fbd9d2017-04-27 14:28:49 -0400838 ((ToggleAction) mSilentModeAction).updateState(
Jason Monk361915c2017-03-21 20:33:59 -0400839 silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
840 }
841 }
842
843 /** {@inheritDoc} */
844 public void onDismiss(DialogInterface dialog) {
845 mWindowManagerFuncs.onGlobalActionsHidden();
846 if (mShowSilentToggle) {
847 try {
848 mContext.unregisterReceiver(mRingerModeReceiver);
849 } catch (IllegalArgumentException ie) {
850 // ignore this
851 Log.w(TAG, ie);
852 }
853 }
854 }
855
856 /** {@inheritDoc} */
857 public void onClick(DialogInterface dialog, int which) {
Jason Monkfd279662017-06-29 19:37:48 -0400858 Action item = mAdapter.getItem(which);
Jason Monkb4302182017-08-04 13:39:17 -0400859 if (!(item instanceof SilentModeTriStateAction)) {
Jason Monk361915c2017-03-21 20:33:59 -0400860 dialog.dismiss();
861 }
Jason Monkfd279662017-06-29 19:37:48 -0400862 item.onPress();
Jason Monk361915c2017-03-21 20:33:59 -0400863 }
864
865 /**
866 * The adapter used for the list within the global actions dialog, taking
867 * into account whether the keyguard is showing via
Jason Monk16fbd9d2017-04-27 14:28:49 -0400868 * {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
869 * the device is provisioned
Jason Monk361915c2017-03-21 20:33:59 -0400870 * via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
871 */
872 private class MyAdapter extends BaseAdapter {
873
874 public int getCount() {
875 int count = 0;
876
877 for (int i = 0; i < mItems.size(); i++) {
878 final Action action = mItems.get(i);
879
880 if (mKeyguardShowing && !action.showDuringKeyguard()) {
881 continue;
882 }
883 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
884 continue;
885 }
886 count++;
887 }
888 return count;
889 }
890
891 @Override
892 public boolean isEnabled(int position) {
893 return getItem(position).isEnabled();
894 }
895
896 @Override
897 public boolean areAllItemsEnabled() {
898 return false;
899 }
900
901 public Action getItem(int position) {
902
903 int filteredPos = 0;
904 for (int i = 0; i < mItems.size(); i++) {
905 final Action action = mItems.get(i);
906 if (mKeyguardShowing && !action.showDuringKeyguard()) {
907 continue;
908 }
909 if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
910 continue;
911 }
912 if (filteredPos == position) {
913 return action;
914 }
915 filteredPos++;
916 }
917
918 throw new IllegalArgumentException("position " + position
919 + " out of range of showable actions"
920 + ", filtered count=" + getCount()
921 + ", keyguardshowing=" + mKeyguardShowing
922 + ", provisioned=" + mDeviceProvisioned);
923 }
924
925
926 public long getItemId(int position) {
927 return position;
928 }
929
930 public View getView(int position, View convertView, ViewGroup parent) {
931 Action action = getItem(position);
Jason Monk16fbd9d2017-04-27 14:28:49 -0400932 View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
Alison Cichowlas3be52db2018-03-06 19:48:06 -0500933 // Everything but screenshot, the last item, gets white background.
934 if (position == getCount() - 1) {
Jason Monk16fbd9d2017-04-27 14:28:49 -0400935 HardwareUiLayout.get(parent).setDivisionView(view);
936 }
937 return view;
Jason Monk361915c2017-03-21 20:33:59 -0400938 }
939 }
940
941 // note: the scheme below made more sense when we were planning on having
942 // 8 different things in the global actions dialog. seems overkill with
943 // only 3 items now, but may as well keep this flexible approach so it will
944 // be easy should someone decide at the last minute to include something
945 // else, such as 'enable wifi', or 'enable bluetooth'
946
947 /**
948 * What each item in the global actions dialog must be able to support.
949 */
950 private interface Action {
951 /**
952 * @return Text that will be announced when dialog is created. null
Jason Monk16fbd9d2017-04-27 14:28:49 -0400953 * for none.
Jason Monk361915c2017-03-21 20:33:59 -0400954 */
955 CharSequence getLabelForAccessibility(Context context);
956
957 View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
958
959 void onPress();
960
961 /**
962 * @return whether this action should appear in the dialog when the keygaurd
Jason Monk16fbd9d2017-04-27 14:28:49 -0400963 * is showing.
Jason Monk361915c2017-03-21 20:33:59 -0400964 */
965 boolean showDuringKeyguard();
966
967 /**
968 * @return whether this action should appear in the dialog before the
Jason Monk16fbd9d2017-04-27 14:28:49 -0400969 * device is provisioned.
Jason Monk361915c2017-03-21 20:33:59 -0400970 */
971 boolean showBeforeProvisioning();
972
973 boolean isEnabled();
974 }
975
976 /**
977 * An action that also supports long press.
978 */
979 private interface LongPressAction extends Action {
980 boolean onLongPress();
981 }
982
983 /**
984 * A single press action maintains no state, just responds to a press
985 * and takes an action.
986 */
987 private static abstract class SinglePressAction implements Action {
988 private final int mIconResId;
989 private final Drawable mIcon;
990 private final int mMessageResId;
991 private final CharSequence mMessage;
992
993 protected SinglePressAction(int iconResId, int messageResId) {
994 mIconResId = iconResId;
995 mMessageResId = messageResId;
996 mMessage = null;
997 mIcon = null;
998 }
999
1000 protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
1001 mIconResId = iconResId;
1002 mMessageResId = 0;
1003 mMessage = message;
1004 mIcon = icon;
1005 }
1006
1007 public boolean isEnabled() {
1008 return true;
1009 }
1010
1011 public String getStatus() {
1012 return null;
1013 }
1014
1015 abstract public void onPress();
1016
1017 public CharSequence getLabelForAccessibility(Context context) {
1018 if (mMessage != null) {
1019 return mMessage;
1020 } else {
1021 return context.getString(mMessageResId);
1022 }
1023 }
1024
1025 public View create(
1026 Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001027 View v = inflater.inflate(com.android.systemui.R.layout.global_actions_item, parent,
1028 false);
Jason Monk361915c2017-03-21 20:33:59 -04001029
1030 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1031 TextView messageView = (TextView) v.findViewById(R.id.message);
1032
1033 TextView statusView = (TextView) v.findViewById(R.id.status);
1034 final String status = getStatus();
1035 if (!TextUtils.isEmpty(status)) {
1036 statusView.setText(status);
1037 } else {
1038 statusView.setVisibility(View.GONE);
1039 }
1040 if (mIcon != null) {
1041 icon.setImageDrawable(mIcon);
1042 icon.setScaleType(ScaleType.CENTER_CROP);
1043 } else if (mIconResId != 0) {
1044 icon.setImageDrawable(context.getDrawable(mIconResId));
1045 }
1046 if (mMessage != null) {
1047 messageView.setText(mMessage);
1048 } else {
1049 messageView.setText(mMessageResId);
1050 }
1051
1052 return v;
1053 }
1054 }
1055
1056 /**
1057 * A toggle action knows whether it is on or off, and displays an icon
1058 * and status message accordingly.
1059 */
1060 private static abstract class ToggleAction implements Action {
1061
1062 enum State {
1063 Off(false),
1064 TurningOn(true),
1065 TurningOff(true),
1066 On(false);
1067
1068 private final boolean inTransition;
1069
1070 State(boolean intermediate) {
1071 inTransition = intermediate;
1072 }
1073
1074 public boolean inTransition() {
1075 return inTransition;
1076 }
1077 }
1078
1079 protected State mState = State.Off;
1080
1081 // prefs
1082 protected int mEnabledIconResId;
1083 protected int mDisabledIconResid;
1084 protected int mMessageResId;
1085 protected int mEnabledStatusMessageResId;
1086 protected int mDisabledStatusMessageResId;
1087
1088 /**
Jason Monk16fbd9d2017-04-27 14:28:49 -04001089 * @param enabledIconResId The icon for when this action is on.
1090 * @param disabledIconResid The icon for when this action is off.
1091 * @param message The general information message, e.g 'Silent Mode'
1092 * @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
Jason Monk361915c2017-03-21 20:33:59 -04001093 * @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
1094 */
1095 public ToggleAction(int enabledIconResId,
1096 int disabledIconResid,
1097 int message,
1098 int enabledStatusMessageResId,
1099 int disabledStatusMessageResId) {
1100 mEnabledIconResId = enabledIconResId;
1101 mDisabledIconResid = disabledIconResid;
1102 mMessageResId = message;
1103 mEnabledStatusMessageResId = enabledStatusMessageResId;
1104 mDisabledStatusMessageResId = disabledStatusMessageResId;
1105 }
1106
1107 /**
1108 * Override to make changes to resource IDs just before creating the
1109 * View.
1110 */
1111 void willCreate() {
1112
1113 }
1114
1115 @Override
1116 public CharSequence getLabelForAccessibility(Context context) {
1117 return context.getString(mMessageResId);
1118 }
1119
1120 public View create(Context context, View convertView, ViewGroup parent,
1121 LayoutInflater inflater) {
1122 willCreate();
1123
1124 View v = inflater.inflate(R
Jason Monk16fbd9d2017-04-27 14:28:49 -04001125 .layout.global_actions_item, parent, false);
Jason Monk361915c2017-03-21 20:33:59 -04001126
1127 ImageView icon = (ImageView) v.findViewById(R.id.icon);
1128 TextView messageView = (TextView) v.findViewById(R.id.message);
1129 TextView statusView = (TextView) v.findViewById(R.id.status);
1130 final boolean enabled = isEnabled();
1131
1132 if (messageView != null) {
1133 messageView.setText(mMessageResId);
1134 messageView.setEnabled(enabled);
1135 }
1136
1137 boolean on = ((mState == State.On) || (mState == State.TurningOn));
1138 if (icon != null) {
1139 icon.setImageDrawable(context.getDrawable(
1140 (on ? mEnabledIconResId : mDisabledIconResid)));
1141 icon.setEnabled(enabled);
1142 }
1143
1144 if (statusView != null) {
1145 statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
1146 statusView.setVisibility(View.VISIBLE);
1147 statusView.setEnabled(enabled);
1148 }
1149 v.setEnabled(enabled);
1150
1151 return v;
1152 }
1153
1154 public final void onPress() {
1155 if (mState.inTransition()) {
1156 Log.w(TAG, "shouldn't be able to toggle when in transition");
1157 return;
1158 }
1159
1160 final boolean nowOn = !(mState == State.On);
1161 onToggle(nowOn);
1162 changeStateFromPress(nowOn);
1163 }
1164
1165 public boolean isEnabled() {
1166 return !mState.inTransition();
1167 }
1168
1169 /**
1170 * Implementations may override this if their state can be in on of the intermediate
1171 * states until some notification is received (e.g airplane mode is 'turning off' until
1172 * we know the wireless connections are back online
Jason Monk16fbd9d2017-04-27 14:28:49 -04001173 *
Jason Monk361915c2017-03-21 20:33:59 -04001174 * @param buttonOn Whether the button was turned on or off
1175 */
1176 protected void changeStateFromPress(boolean buttonOn) {
1177 mState = buttonOn ? State.On : State.Off;
1178 }
1179
1180 abstract void onToggle(boolean on);
1181
1182 public void updateState(State state) {
1183 mState = state;
1184 }
1185 }
1186
1187 private class SilentModeToggleAction extends ToggleAction {
1188 public SilentModeToggleAction() {
1189 super(R.drawable.ic_audio_vol_mute,
1190 R.drawable.ic_audio_vol,
1191 R.string.global_action_toggle_silent_mode,
1192 R.string.global_action_silent_mode_on_status,
1193 R.string.global_action_silent_mode_off_status);
1194 }
1195
1196 void onToggle(boolean on) {
1197 if (on) {
1198 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
1199 } else {
1200 mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1201 }
1202 }
1203
1204 public boolean showDuringKeyguard() {
1205 return true;
1206 }
1207
1208 public boolean showBeforeProvisioning() {
1209 return false;
1210 }
1211 }
1212
1213 private static class SilentModeTriStateAction implements Action, View.OnClickListener {
1214
Jason Monk16fbd9d2017-04-27 14:28:49 -04001215 private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
Jason Monk361915c2017-03-21 20:33:59 -04001216
1217 private final AudioManager mAudioManager;
1218 private final Handler mHandler;
Jason Monk361915c2017-03-21 20:33:59 -04001219
Lucas Dupin40ec6b782018-06-05 19:07:16 -07001220 SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
Jason Monk361915c2017-03-21 20:33:59 -04001221 mAudioManager = audioManager;
1222 mHandler = handler;
Jason Monk361915c2017-03-21 20:33:59 -04001223 }
1224
1225 private int ringerModeToIndex(int ringerMode) {
1226 // They just happen to coincide
1227 return ringerMode;
1228 }
1229
1230 private int indexToRingerMode(int index) {
1231 // They just happen to coincide
1232 return index;
1233 }
1234
1235 @Override
1236 public CharSequence getLabelForAccessibility(Context context) {
1237 return null;
1238 }
1239
1240 public View create(Context context, View convertView, ViewGroup parent,
1241 LayoutInflater inflater) {
1242 View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
1243
1244 int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
1245 for (int i = 0; i < 3; i++) {
1246 View itemView = v.findViewById(ITEM_IDS[i]);
1247 itemView.setSelected(selectedIndex == i);
1248 // Set up click handler
1249 itemView.setTag(i);
1250 itemView.setOnClickListener(this);
1251 }
1252 return v;
1253 }
1254
1255 public void onPress() {
1256 }
1257
1258 public boolean showDuringKeyguard() {
1259 return true;
1260 }
1261
1262 public boolean showBeforeProvisioning() {
1263 return false;
1264 }
1265
1266 public boolean isEnabled() {
1267 return true;
1268 }
1269
1270 void willCreate() {
1271 }
1272
1273 public void onClick(View v) {
1274 if (!(v.getTag() instanceof Integer)) return;
1275
1276 int index = (Integer) v.getTag();
1277 mAudioManager.setRingerMode(indexToRingerMode(index));
1278 mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
1279 }
1280 }
1281
1282 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
1283 public void onReceive(Context context, Intent intent) {
1284 String action = intent.getAction();
1285 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
1286 || Intent.ACTION_SCREEN_OFF.equals(action)) {
1287 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
1288 if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001289 mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
Jason Monk361915c2017-03-21 20:33:59 -04001290 }
1291 } else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
1292 // Airplane mode can be changed after ECM exits if airplane toggle button
1293 // is pressed during ECM mode
1294 if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
1295 mIsWaitingForEcmExit) {
1296 mIsWaitingForEcmExit = false;
1297 changeAirplaneModeSystemSetting(true);
1298 }
1299 }
1300 }
1301 };
1302
1303 PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
1304 @Override
1305 public void onServiceStateChanged(ServiceState serviceState) {
1306 if (!mHasTelephony) return;
1307 final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
1308 mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
1309 mAirplaneModeOn.updateState(mAirplaneState);
1310 mAdapter.notifyDataSetChanged();
1311 }
1312 };
1313
1314 private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
1315 @Override
1316 public void onReceive(Context context, Intent intent) {
1317 if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
1318 mHandler.sendEmptyMessage(MESSAGE_REFRESH);
1319 }
1320 }
1321 };
1322
1323 private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
1324 @Override
1325 public void onChange(boolean selfChange) {
1326 onAirplaneModeChanged();
1327 }
1328 };
1329
1330 private static final int MESSAGE_DISMISS = 0;
1331 private static final int MESSAGE_REFRESH = 1;
1332 private static final int MESSAGE_SHOW = 2;
1333 private static final int DIALOG_DISMISS_DELAY = 300; // ms
1334
1335 private Handler mHandler = new Handler() {
1336 public void handleMessage(Message msg) {
1337 switch (msg.what) {
Jason Monk16fbd9d2017-04-27 14:28:49 -04001338 case MESSAGE_DISMISS:
1339 if (mDialog != null) {
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001340 if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
1341 mDialog.dismissImmediately();
1342 } else {
1343 mDialog.dismiss();
1344 }
Jason Monk16fbd9d2017-04-27 14:28:49 -04001345 mDialog = null;
1346 }
1347 break;
1348 case MESSAGE_REFRESH:
1349 refreshSilentMode();
1350 mAdapter.notifyDataSetChanged();
1351 break;
1352 case MESSAGE_SHOW:
1353 handleShow();
1354 break;
Jason Monk361915c2017-03-21 20:33:59 -04001355 }
1356 }
1357 };
1358
1359 private void onAirplaneModeChanged() {
1360 // Let the service state callbacks handle the state.
1361 if (mHasTelephony) return;
1362
1363 boolean airplaneModeOn = Settings.Global.getInt(
1364 mContext.getContentResolver(),
1365 Settings.Global.AIRPLANE_MODE_ON,
1366 0) == 1;
1367 mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
1368 mAirplaneModeOn.updateState(mAirplaneState);
1369 }
1370
1371 /**
1372 * Change the airplane mode system setting
1373 */
1374 private void changeAirplaneModeSystemSetting(boolean on) {
1375 Settings.Global.putInt(
1376 mContext.getContentResolver(),
1377 Settings.Global.AIRPLANE_MODE_ON,
1378 on ? 1 : 0);
1379 Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
1380 intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
1381 intent.putExtra("state", on);
1382 mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
1383 if (!mHasTelephony) {
1384 mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
1385 }
1386 }
1387
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001388 private static final class ActionsDialog extends Dialog implements DialogInterface,
1389 ColorExtractor.OnColorsChangedListener {
Jason Monk361915c2017-03-21 20:33:59 -04001390
Jason Monk16fbd9d2017-04-27 14:28:49 -04001391 private final Context mContext;
1392 private final MyAdapter mAdapter;
1393 private final LinearLayout mListView;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001394 private final FrameLayout mSeparatedView;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001395 private final HardwareUiLayout mHardwareLayout;
1396 private final OnClickListener mClickListener;
1397 private final OnItemLongClickListener mLongClickListener;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001398 private final GradientDrawable mGradientDrawable;
1399 private final ColorExtractor mColorExtractor;
1400 private boolean mKeyguardShowing;
Wesley.CW Wang3004fcb2018-06-15 16:24:57 +08001401 private boolean mShouldDisplaySeparatedButton;
Beverly526d2d62018-08-15 12:55:33 -04001402 private boolean mShowing;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001403
1404 public ActionsDialog(Context context, OnClickListener clickListener, MyAdapter adapter,
Wesley.CW Wang3004fcb2018-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 Wang3004fcb2018-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 Wang3004fcb2018-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 Wang3004fcb2018-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 Wang3004fcb2018-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 Wang3004fcb2018-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 Wang3004fcb2018-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();
Beverly526d2d62018-08-15 12:55:33 -04001511 mShowing = true;
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001512 mGradientDrawable.setAlpha(0);
Jason Monk16fbd9d2017-04-27 14:28:49 -04001513 mHardwareLayout.setTranslationX(getAnimTranslation());
1514 mHardwareLayout.setAlpha(0);
1515 mHardwareLayout.animate()
1516 .alpha(1)
1517 .translationX(0)
1518 .setDuration(300)
Lucas Dupinde9db422017-07-19 17:15:41 -07001519 .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001520 .setUpdateListener(animation -> {
1521 int alpha = (int) ((Float) animation.getAnimatedValue()
1522 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1523 mGradientDrawable.setAlpha(alpha);
1524 })
Jason Monka7af3b62017-07-07 11:35:13 -04001525 .withEndAction(() -> getWindow().getDecorView().requestAccessibilityFocus())
Jason Monk16fbd9d2017-04-27 14:28:49 -04001526 .start();
1527 }
1528
1529 @Override
1530 public void dismiss() {
Beverly526d2d62018-08-15 12:55:33 -04001531 if (!mShowing) {
1532 return;
1533 }
1534 mShowing = false;
Jason Monk16fbd9d2017-04-27 14:28:49 -04001535 mHardwareLayout.setTranslationX(0);
1536 mHardwareLayout.setAlpha(1);
1537 mHardwareLayout.animate()
1538 .alpha(0)
1539 .translationX(getAnimTranslation())
1540 .setDuration(300)
1541 .withEndAction(() -> super.dismiss())
1542 .setInterpolator(new LogAccelerateInterpolator())
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001543 .setUpdateListener(animation -> {
1544 int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
1545 * ScrimController.GRADIENT_SCRIM_ALPHA * 255);
1546 mGradientDrawable.setAlpha(alpha);
1547 })
Jason Monk16fbd9d2017-04-27 14:28:49 -04001548 .start();
1549 }
1550
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001551 void dismissImmediately() {
1552 super.dismiss();
Beverly526d2d62018-08-15 12:55:33 -04001553 mShowing = false;
Lucas Dupin1d4a5792018-04-02 15:14:59 -07001554 }
1555
Jason Monk16fbd9d2017-04-27 14:28:49 -04001556 private float getAnimTranslation() {
1557 return getContext().getResources().getDimension(
1558 com.android.systemui.R.dimen.global_actions_panel_width) / 2;
Jason Monk361915c2017-03-21 20:33:59 -04001559 }
1560
1561 @Override
Lucas Dupin7aaa3532017-05-28 08:51:07 -07001562 public void onColorsChanged(ColorExtractor extractor, int which) {
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001563 if (mKeyguardShowing) {
1564 if ((WallpaperManager.FLAG_LOCK & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001565 updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
1566 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001567 }
1568 } else {
1569 if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
Lucas Dupinb5f59fe2017-09-14 17:09:39 -07001570 updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
1571 true /* animate */);
Lucas Dupinc1cc7592017-05-22 15:56:16 -07001572 }
1573 }
1574 }
1575
1576 public void setKeyguardShowing(boolean keyguardShowing) {
1577 mKeyguardShowing = keyguardShowing;
1578 }
Jason Monk361915c2017-03-21 20:33:59 -04001579 }
1580}