Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.am; |
| 18 | |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 19 | import android.app.AlertDialog; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 21 | import android.content.pm.UserInfo; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 25 | import android.os.UserHandle; |
| 26 | import android.os.UserManager; |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 29 | import android.view.ViewTreeObserver; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 30 | import android.view.WindowManager; |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 31 | import android.widget.TextView; |
| 32 | |
| 33 | import com.android.internal.R; |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 34 | import com.android.internal.annotations.GuardedBy; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Dialog to show when a user switch it about to happen. The intent is to snapshot the screen |
| 38 | * immediately after the dialog shows so that the user is informed that something is happening |
| 39 | * in the background rather than just freeze the screen and not know if the user-switch affordance |
| 40 | * was being handled. |
| 41 | */ |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 42 | class UserSwitchingDialog extends AlertDialog |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 43 | implements ViewTreeObserver.OnWindowShownListener { |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 44 | private static final String TAG = "ActivityManagerUserSwitchingDialog"; |
| 45 | |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 46 | // Time to wait for the onWindowShown() callback before continuing the user switch |
| 47 | private static final int WINDOW_SHOWN_TIMEOUT_MS = 3000; |
| 48 | |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 49 | private final ActivityManagerService mService; |
| 50 | private final int mUserId; |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 51 | private static final int MSG_START_USER = 1; |
| 52 | @GuardedBy("this") |
| 53 | private boolean mStartedUser; |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 54 | final protected UserInfo mOldUser; |
| 55 | final protected UserInfo mNewUser; |
| 56 | final private String mSwitchingFromSystemUserMessage; |
| 57 | final private String mSwitchingToSystemUserMessage; |
| 58 | final protected Context mContext; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 59 | |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 60 | public UserSwitchingDialog(ActivityManagerService service, Context context, UserInfo oldUser, |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 61 | UserInfo newUser, boolean aboveSystem, String switchingFromSystemUserMessage, |
| 62 | String switchingToSystemUserMessage) { |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 63 | super(context); |
| 64 | |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 65 | mContext = context; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 66 | mService = service; |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 67 | mUserId = newUser.id; |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 68 | mOldUser = oldUser; |
| 69 | mNewUser = newUser; |
| 70 | mSwitchingFromSystemUserMessage = switchingFromSystemUserMessage; |
| 71 | mSwitchingToSystemUserMessage = switchingToSystemUserMessage; |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 72 | |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 73 | inflateContent(); |
| 74 | |
| 75 | if (aboveSystem) { |
| 76 | getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR); |
| 77 | } |
| 78 | |
| 79 | WindowManager.LayoutParams attrs = getWindow().getAttributes(); |
| 80 | attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR | |
| 81 | WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; |
| 82 | getWindow().setAttributes(attrs); |
| 83 | } |
| 84 | |
| 85 | void inflateContent() { |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 86 | // Set up the dialog contents |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 87 | setCancelable(false); |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 88 | Resources res = getContext().getResources(); |
| 89 | // Custom view due to alignment and font size requirements |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 90 | View view = LayoutInflater.from(getContext()).inflate(R.layout.user_switching_dialog, |
| 91 | null); |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 92 | |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 93 | String viewMessage = null; |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 94 | if (UserManager.isSplitSystemUser() && mNewUser.id == UserHandle.USER_SYSTEM) { |
| 95 | viewMessage = res.getString(R.string.user_logging_out_message, mOldUser.name); |
| 96 | } else if (UserManager.isDeviceInDemoMode(mContext)) { |
| 97 | if (mOldUser.isDemo()) { |
Suprabh Shukla | ff0939b | 2016-06-01 23:19:06 -0700 | [diff] [blame] | 98 | viewMessage = res.getString(R.string.demo_restarting_message); |
| 99 | } else { |
| 100 | viewMessage = res.getString(R.string.demo_starting_message); |
| 101 | } |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 102 | } else { |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 103 | if (mOldUser.id == UserHandle.USER_SYSTEM) { |
| 104 | viewMessage = mSwitchingFromSystemUserMessage; |
| 105 | } else if (mNewUser.id == UserHandle.USER_SYSTEM) { |
| 106 | viewMessage = mSwitchingToSystemUserMessage; |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // If switchingFromSystemUserMessage or switchingToSystemUserMessage is null, fallback |
| 110 | // to system message. |
| 111 | if (viewMessage == null) { |
Aarthi Balachander | cf6ca0c | 2018-04-10 19:26:45 -0700 | [diff] [blame] | 112 | viewMessage = res.getString(R.string.user_switching_message, mNewUser.name); |
Alex Chau | 93ae42b | 2018-01-11 15:10:12 +0000 | [diff] [blame] | 113 | } |
Suprabh Shukla | 4fe508b | 2015-11-20 18:22:57 -0800 | [diff] [blame] | 114 | } |
| 115 | ((TextView) view.findViewById(R.id.message)).setText(viewMessage); |
Amith Yamasani | c2a8d15 | 2014-08-25 14:15:25 -0700 | [diff] [blame] | 116 | setView(view); |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void show() { |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 121 | // Slog.v(TAG, "show called"); |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 122 | super.show(); |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 123 | final View decorView = getWindow().getDecorView(); |
| 124 | if (decorView != null) { |
| 125 | decorView.getViewTreeObserver().addOnWindowShownListener(this); |
| 126 | } |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 127 | // Add a timeout as a safeguard, in case a race in screen on/off causes the window |
| 128 | // callback to never come. |
| 129 | mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_START_USER), |
| 130 | WINDOW_SHOWN_TIMEOUT_MS); |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 133 | @Override |
| 134 | public void onWindowShown() { |
| 135 | // Slog.v(TAG, "onWindowShown called"); |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 136 | startUser(); |
| 137 | } |
| 138 | |
| 139 | void startUser() { |
| 140 | synchronized (this) { |
| 141 | if (!mStartedUser) { |
Evan Rosky | 1839645 | 2016-07-27 15:19:37 -0700 | [diff] [blame] | 142 | mService.mUserController.startUserInForeground(mUserId); |
| 143 | dismiss(); |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 144 | mStartedUser = true; |
| 145 | final View decorView = getWindow().getDecorView(); |
| 146 | if (decorView != null) { |
| 147 | decorView.getViewTreeObserver().removeOnWindowShownListener(this); |
| 148 | } |
| 149 | mHandler.removeMessages(MSG_START_USER); |
| 150 | } |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 151 | } |
Craig Mautner | 9c79504 | 2014-10-28 19:59:59 -0700 | [diff] [blame] | 152 | } |
Amith Yamasani | 5e5cb46 | 2015-02-09 15:45:26 -0800 | [diff] [blame] | 153 | |
| 154 | private final Handler mHandler = new Handler() { |
| 155 | @Override |
| 156 | public void handleMessage(Message msg) { |
| 157 | switch (msg.what) { |
| 158 | case MSG_START_USER: |
| 159 | startUser(); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | }; |
Amith Yamasani | 7805a10 | 2014-08-22 09:27:25 -0700 | [diff] [blame] | 164 | } |