Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.car.user; |
| 18 | |
| 19 | import android.annotation.Nullable; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
| 21 | import android.app.IActivityManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 22 | import android.car.settings.CarSettings; |
Ying Zheng | 9fc9940 | 2018-09-19 14:23:59 -0700 | [diff] [blame] | 23 | import android.car.userlib.CarUserManagerHelper; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 28 | import android.location.LocationManager; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Ying Zheng | 8f90edb | 2018-06-13 12:42:31 -0700 | [diff] [blame] | 31 | import android.os.UserManager; |
jovanak | 2447065 | 2018-09-11 17:51:57 -0700 | [diff] [blame] | 32 | import android.provider.Settings; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
| 35 | import com.android.car.CarServiceBase; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 36 | import com.android.internal.annotations.GuardedBy; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 37 | |
| 38 | import java.io.PrintWriter; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 39 | import java.util.ArrayList; |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 40 | import java.util.concurrent.CopyOnWriteArrayList; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * User service for cars. Manages users at boot time. Including: |
| 44 | * |
| 45 | * <ol> |
| 46 | * <li> Creates a secondary admin user on first run. |
Ying Zheng | f4339b8 | 2018-06-19 16:01:05 -0700 | [diff] [blame] | 47 | * <li> Log in to the last active user. |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 48 | * <ol/> |
| 49 | */ |
| 50 | public class CarUserService extends BroadcastReceiver implements CarServiceBase { |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 51 | private static final String TAG = "CarUserService"; |
| 52 | private final Context mContext; |
| 53 | private final CarUserManagerHelper mCarUserManagerHelper; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 54 | private final IActivityManager mAm; |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 55 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 56 | private final Object mLock = new Object(); |
| 57 | @GuardedBy("mLock") |
| 58 | private boolean mUser0Unlocked; |
| 59 | @GuardedBy("mLock") |
| 60 | private final ArrayList<Runnable> mUser0UnlockTasks = new ArrayList<>(); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 61 | @GuardedBy("mLock") |
| 62 | private final ArrayList<Integer> mLastUnlockedUsers = new ArrayList<>(); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 63 | |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 64 | |
| 65 | private final CopyOnWriteArrayList<UserCallback> mUserCallbacks = new CopyOnWriteArrayList<>(); |
| 66 | |
| 67 | /** Interface for callbacks related to user activities. */ |
| 68 | public interface UserCallback { |
| 69 | /** Gets called when user lock status has been changed. */ |
| 70 | void onUserLockChanged(int userId, boolean unlocked); |
| 71 | /** Called when new foreground user started to boot. */ |
| 72 | void onSwitchUser(int userId); |
| 73 | } |
| 74 | |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 75 | public CarUserService( |
| 76 | @Nullable Context context, @Nullable CarUserManagerHelper carUserManagerHelper) { |
| 77 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 78 | Log.d(TAG, "constructed"); |
| 79 | } |
| 80 | mContext = context; |
| 81 | mCarUserManagerHelper = carUserManagerHelper; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 82 | mAm = ActivityManager.getService(); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void init() { |
| 87 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 88 | Log.d(TAG, "init"); |
| 89 | } |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 90 | IntentFilter filter = new IntentFilter(); |
| 91 | filter.addAction(Intent.ACTION_USER_SWITCHED); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 92 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 93 | mContext.registerReceiver(this, filter); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void release() { |
| 98 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 99 | Log.d(TAG, "release"); |
| 100 | } |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 101 | mContext.unregisterReceiver(this); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void dump(PrintWriter writer) { |
| 106 | writer.println(TAG); |
| 107 | writer.println("Context: " + mContext); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 108 | boolean user0Unlocked; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 109 | ArrayList<Integer> lastUnlockedUsers; |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 110 | synchronized (mLock) { |
| 111 | user0Unlocked = mUser0Unlocked; |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 112 | lastUnlockedUsers = new ArrayList<>(mLastUnlockedUsers); |
| 113 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 114 | } |
| 115 | writer.println("User0Unlocked: " + user0Unlocked); |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 116 | writer.println("LastUnlockedUsers:" + lastUnlockedUsers); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 119 | private void updateDefaultUserRestriction() { |
| 120 | // We want to set restrictions on system and guest users only once. These are persisted |
| 121 | // onto disk, so it's sufficient to do it once + we minimize the number of disk writes. |
| 122 | if (Settings.Global.getInt(mContext.getContentResolver(), |
| 123 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, /* default= */ 0) == 0) { |
| 124 | setSystemUserRestrictions(); |
| 125 | mCarUserManagerHelper.initDefaultGuestRestrictions(); |
| 126 | Settings.Global.putInt(mContext.getContentResolver(), |
| 127 | CarSettings.Global.DEFAULT_USER_RESTRICTIONS_SET, 1); |
| 128 | } |
| 129 | } |
| 130 | |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 131 | @Override |
| 132 | public void onReceive(Context context, Intent intent) { |
| 133 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 134 | Log.d(TAG, "onReceive " + intent); |
| 135 | } |
| 136 | |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 137 | if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) { |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 138 | // Update last active user if the switched-to user is a persistent, non-system user. |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 139 | final int currentUser = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
Ying Zheng | cf20f44 | 2018-06-22 16:54:51 -0700 | [diff] [blame] | 140 | if (currentUser > UserHandle.USER_SYSTEM |
| 141 | && mCarUserManagerHelper.isPersistentUser(currentUser)) { |
Ying Zheng | bb1ba2d | 2018-11-08 10:46:16 -0800 | [diff] [blame] | 142 | mCarUserManagerHelper.setLastActiveUser(currentUser); |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 146 | |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 147 | /** Add callback to listen to user activity events. */ |
| 148 | public void addUserCallback(UserCallback callback) { |
| 149 | mUserCallbacks.add(callback); |
| 150 | } |
| 151 | |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Set user lock / unlocking status. This is coming from system server through ICar binder call. |
| 154 | * @param userHandle Handle of user |
| 155 | * @param unlocked unlocked (=true) or locked (=false) |
| 156 | */ |
| 157 | public void setUserLockStatus(int userHandle, boolean unlocked) { |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 158 | for (UserCallback callback : mUserCallbacks) { |
| 159 | callback.onUserLockChanged(userHandle, unlocked); |
| 160 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 161 | ArrayList<Runnable> tasks = null; |
| 162 | synchronized (mLock) { |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 163 | if (userHandle != UserHandle.USER_SYSTEM && unlocked |
| 164 | && mCarUserManagerHelper.isPersistentUser(userHandle)) { |
| 165 | Integer user = userHandle; |
| 166 | mLastUnlockedUsers.remove(user); |
| 167 | mLastUnlockedUsers.add(0, user); |
| 168 | return; |
| 169 | } |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 170 | // Assumes that car service need to do it only once during boot-up |
| 171 | if (unlocked && !mUser0Unlocked) { |
Keun young Park | 13a7a82 | 2019-04-04 15:53:08 -0700 | [diff] [blame^] | 172 | updateDefaultUserRestriction(); |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 173 | tasks = new ArrayList<>(mUser0UnlockTasks); |
| 174 | mUser0UnlockTasks.clear(); |
| 175 | mUser0Unlocked = unlocked; |
| 176 | } |
| 177 | } |
| 178 | if (tasks != null && tasks.size() > 0) { |
| 179 | Log.d(TAG, "User0 unlocked, run queued tasks:" + tasks.size()); |
| 180 | for (Runnable r : tasks) { |
| 181 | r.run(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
Keun young Park | fb65637 | 2019-03-12 18:37:55 -0700 | [diff] [blame] | 187 | * Start all background users that were active in system. |
| 188 | * @return list of background users started successfully. |
| 189 | */ |
| 190 | public ArrayList<Integer> startAllBackgroundUsers() { |
| 191 | ArrayList<Integer> users; |
| 192 | synchronized (mLock) { |
| 193 | users = new ArrayList<>(mLastUnlockedUsers); |
| 194 | } |
| 195 | ArrayList<Integer> startedUsers = new ArrayList<>(); |
| 196 | for (Integer user : users) { |
| 197 | if (user == mCarUserManagerHelper.getCurrentForegroundUserId()) { |
| 198 | continue; |
| 199 | } |
| 200 | try { |
| 201 | if (mAm.startUserInBackground(user)) { |
| 202 | if (mAm.unlockUser(user, null, null, null)) { |
| 203 | startedUsers.add(user); |
| 204 | } |
| 205 | } |
| 206 | } catch (RemoteException e) { |
| 207 | // ignore |
| 208 | } |
| 209 | } |
| 210 | return startedUsers; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Stop all background users that were active in system. |
| 215 | * @return true if stopping succeeds. |
| 216 | */ |
| 217 | public boolean stopBackgroundUser(int userId) { |
| 218 | if (userId == mCarUserManagerHelper.getCurrentForegroundUserId()) { |
| 219 | Log.i(TAG, "stopBackgroundUser, already a fg user:" + userId); |
| 220 | return false; |
| 221 | } |
| 222 | try { |
| 223 | int r = mAm.stopUser(userId, true, null); |
| 224 | if (r != ActivityManager.USER_OP_SUCCESS) { |
| 225 | Log.i(TAG, "stopBackgroundUser failed, user:" + userId + " err:" + r); |
| 226 | return false; |
| 227 | } |
| 228 | } catch (RemoteException e) { |
| 229 | // ignore |
| 230 | } |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | /** |
Pavel Maltsev | 17e8183 | 2019-04-04 14:38:41 -0700 | [diff] [blame] | 235 | * Called when new foreground user started to boot. |
| 236 | * |
| 237 | * @param userHandle user handle of new user |
| 238 | */ |
| 239 | public void onSwitchUser(int userHandle) { |
| 240 | for (UserCallback callback : mUserCallbacks) { |
| 241 | callback.onSwitchUser(userHandle); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /** |
Keun-young Park | d462a91 | 2019-02-11 08:53:42 -0800 | [diff] [blame] | 246 | * Run give runnable when user 0 is unlocked. If user 0 is already unlocked, it is |
| 247 | * run inside this call. |
| 248 | * @param r Runnable to run. |
| 249 | */ |
| 250 | public void runOnUser0Unlock(Runnable r) { |
| 251 | boolean runNow = false; |
| 252 | synchronized (mLock) { |
| 253 | if (mUser0Unlocked) { |
| 254 | runNow = true; |
| 255 | } else { |
| 256 | mUser0UnlockTasks.add(r); |
| 257 | } |
| 258 | } |
| 259 | if (runNow) { |
| 260 | r.run(); |
| 261 | } |
| 262 | } |
| 263 | |
Ying Zheng | 1ab32b6 | 2018-06-26 12:47:26 -0700 | [diff] [blame] | 264 | private void setSystemUserRestrictions() { |
| 265 | // Disable adding accounts for system user. |
| 266 | mCarUserManagerHelper.setUserRestriction(mCarUserManagerHelper.getSystemUserInfo(), |
| 267 | UserManager.DISALLOW_MODIFY_ACCOUNTS, /* enable= */ true); |
| 268 | |
| 269 | // Disable Location service for system user. |
| 270 | LocationManager locationManager = |
| 271 | (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); |
| 272 | locationManager.setLocationEnabledForUser( |
| 273 | /* enabled= */ false, UserHandle.of(UserHandle.USER_SYSTEM)); |
| 274 | } |
Ying Zheng | d3cb98e | 2018-05-11 11:42:48 -0700 | [diff] [blame] | 275 | } |