Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.pm; |
| 18 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 19 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 20 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 23 | import android.app.ActivityManagerNative; |
| 24 | import android.app.IStopUserCallback; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 28 | import android.content.RestrictionEntry; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 31 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 32 | import android.graphics.Bitmap; |
| 33 | import android.graphics.BitmapFactory; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 34 | import android.os.Binder; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 35 | import android.os.Bundle; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 36 | import android.os.Environment; |
| 37 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 38 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 39 | import android.os.IUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 40 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 42 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 43 | import android.os.UserManager; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 44 | import android.util.AtomicFile; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 45 | import android.util.Slog; |
| 46 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 47 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 48 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 49 | import android.util.Xml; |
| 50 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 51 | import com.android.internal.util.ArrayUtils; |
| 52 | import com.android.internal.util.FastXmlSerializer; |
| 53 | |
| 54 | import org.xmlpull.v1.XmlPullParser; |
| 55 | import org.xmlpull.v1.XmlPullParserException; |
| 56 | import org.xmlpull.v1.XmlSerializer; |
| 57 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 58 | import java.io.BufferedOutputStream; |
| 59 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 60 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 61 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 62 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 63 | import java.io.FileOutputStream; |
| 64 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 65 | import java.io.PrintWriter; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 66 | import java.util.ArrayList; |
| 67 | import java.util.List; |
| 68 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 69 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 70 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 71 | private static final String LOG_TAG = "UserManagerService"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 72 | |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 73 | private static final boolean DBG = false; |
| 74 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 75 | private static final String TAG_NAME = "name"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 76 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 77 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 78 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 79 | private static final String ATTR_CREATION_TIME = "created"; |
| 80 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 81 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 82 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 83 | private static final String ATTR_PARTIAL = "partial"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 84 | private static final String ATTR_USER_VERSION = "version"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 85 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 86 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 87 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 88 | private static final String TAG_ENTRY = "entry"; |
| 89 | private static final String TAG_VALUE = "value"; |
| 90 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 91 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 92 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 93 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 94 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 95 | private static final String ATTR_TYPE_STRING = "s"; |
| 96 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
| 97 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 98 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 99 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 100 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 101 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 102 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
| 103 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 104 | private static final int MIN_USER_ID = 10; |
| 105 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 106 | private static final int USER_VERSION = 2; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 107 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 108 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 109 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 110 | private final Context mContext; |
| 111 | private final PackageManagerService mPm; |
| 112 | private final Object mInstallLock; |
| 113 | private final Object mPackagesLock; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 114 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 115 | private final Handler mHandler; |
| 116 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 117 | private final File mUsersDir; |
| 118 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 119 | private final File mBaseUserPath; |
| 120 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 121 | private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 122 | private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 126 | * for several seconds to work around a VFS caching issue. |
| 127 | */ |
| 128 | // @GuardedBy("mPackagesLock") |
| 129 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 130 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 131 | private int[] mUserIds; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 132 | private boolean mGuestEnabled; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 133 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 134 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 135 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 136 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 137 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 138 | public static UserManagerService getInstance() { |
| 139 | synchronized (UserManagerService.class) { |
| 140 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 141 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 142 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Available for testing purposes. |
| 146 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 147 | UserManagerService(File dataDir, File baseUserPath) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 148 | this(null, null, new Object(), new Object(), dataDir, baseUserPath); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Called by package manager to create the service. This is closely |
| 153 | * associated with the package manager, and the given lock is the |
| 154 | * package manager's own lock. |
| 155 | */ |
| 156 | UserManagerService(Context context, PackageManagerService pm, |
| 157 | Object installLock, Object packagesLock) { |
| 158 | this(context, pm, installLock, packagesLock, |
| 159 | Environment.getDataDirectory(), |
| 160 | new File(Environment.getDataDirectory(), "user")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 163 | /** |
| 164 | * Available for testing purposes. |
| 165 | */ |
| 166 | private UserManagerService(Context context, PackageManagerService pm, |
| 167 | Object installLock, Object packagesLock, |
| 168 | File dataDir, File baseUserPath) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 169 | mContext = context; |
| 170 | mPm = pm; |
| 171 | mInstallLock = installLock; |
| 172 | mPackagesLock = packagesLock; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 173 | mHandler = new Handler(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 174 | synchronized (mInstallLock) { |
| 175 | synchronized (mPackagesLock) { |
| 176 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 177 | mUsersDir.mkdirs(); |
| 178 | // Make zeroth user directory, for services to migrate their files to that location |
| 179 | File userZeroDir = new File(mUsersDir, "0"); |
| 180 | userZeroDir.mkdirs(); |
| 181 | mBaseUserPath = baseUserPath; |
| 182 | FileUtils.setPermissions(mUsersDir.toString(), |
| 183 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 184 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 185 | -1, -1); |
| 186 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 187 | readUserListLocked(); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 188 | // Prune out any partially created/partially removed users. |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 189 | ArrayList<UserInfo> partials = new ArrayList<UserInfo>(); |
| 190 | for (int i = 0; i < mUsers.size(); i++) { |
| 191 | UserInfo ui = mUsers.valueAt(i); |
| 192 | if (ui.partial && i != 0) { |
| 193 | partials.add(ui); |
| 194 | } |
| 195 | } |
| 196 | for (int i = 0; i < partials.size(); i++) { |
| 197 | UserInfo ui = partials.get(i); |
| 198 | Slog.w(LOG_TAG, "Removing partially created user #" + i |
| 199 | + " (name=" + ui.name + ")"); |
| 200 | removeUserStateLocked(ui.id); |
| 201 | } |
| 202 | sInstance = this; |
| 203 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 204 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 208 | public List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 209 | checkManageUsersPermission("query users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 210 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 211 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 212 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 213 | UserInfo ui = mUsers.valueAt(i); |
| 214 | if (ui.partial) { |
| 215 | continue; |
| 216 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 217 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 218 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 219 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 220 | } |
| 221 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 222 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 225 | @Override |
| 226 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 227 | checkManageUsersPermission("query user"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 228 | synchronized (mPackagesLock) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 229 | return getUserInfoLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 233 | @Override |
| 234 | public boolean isRestricted() { |
| 235 | synchronized (mPackagesLock) { |
| 236 | return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted(); |
| 237 | } |
| 238 | } |
| 239 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 240 | /* |
| 241 | * Should be locked on mUsers before calling this. |
| 242 | */ |
| 243 | private UserInfo getUserInfoLocked(int userId) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 244 | UserInfo ui = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 245 | // If it is partial and not in the process of being removed, return as unknown user. |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 246 | if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 247 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 248 | return null; |
| 249 | } |
| 250 | return ui; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 253 | public boolean exists(int userId) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 254 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 255 | return ArrayUtils.contains(mUserIds, userId); |
| 256 | } |
| 257 | } |
| 258 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 259 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 260 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 261 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 262 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 263 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 264 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 265 | if (info == null || info.partial) { |
| 266 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 267 | return; |
| 268 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 269 | if (name != null && !name.equals(info.name)) { |
| 270 | info.name = name; |
| 271 | writeUserLocked(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 272 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 275 | if (changed) { |
| 276 | sendUserInfoChangedBroadcast(userId); |
| 277 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 280 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 281 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 282 | checkManageUsersPermission("update users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 283 | synchronized (mPackagesLock) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 284 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 285 | if (info == null || info.partial) { |
| 286 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 287 | return; |
| 288 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 289 | writeBitmapLocked(info, bitmap); |
| 290 | writeUserLocked(info); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 291 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 292 | sendUserInfoChangedBroadcast(userId); |
| 293 | } |
| 294 | |
| 295 | private void sendUserInfoChangedBroadcast(int userId) { |
| 296 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 297 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 298 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 299 | mContext.sendBroadcastAsUser(changedIntent, new UserHandle(userId)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 302 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 303 | public Bitmap getUserIcon(int userId) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 304 | checkManageUsersPermission("read users"); |
| 305 | synchronized (mPackagesLock) { |
| 306 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 307 | if (info == null || info.partial) { |
| 308 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 309 | return null; |
| 310 | } |
| 311 | if (info.iconPath == null) { |
| 312 | return null; |
| 313 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 314 | return BitmapFactory.decodeFile(info.iconPath); |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 319 | public void setGuestEnabled(boolean enable) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 320 | checkManageUsersPermission("enable guest users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 321 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 322 | if (mGuestEnabled != enable) { |
| 323 | mGuestEnabled = enable; |
| 324 | // Erase any guest user that currently exists |
| 325 | for (int i = 0; i < mUsers.size(); i++) { |
| 326 | UserInfo user = mUsers.valueAt(i); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 327 | if (!user.partial && user.isGuest()) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 328 | if (!enable) { |
| 329 | removeUser(user.id); |
| 330 | } |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | // No guest was found |
| 335 | if (enable) { |
| 336 | createUser("Guest", UserInfo.FLAG_GUEST); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public boolean isGuestEnabled() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 344 | synchronized (mPackagesLock) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 345 | return mGuestEnabled; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public void wipeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 351 | checkManageUsersPermission("wipe user"); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 352 | // TODO: |
| 353 | } |
| 354 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 355 | public void makeInitialized(int userId) { |
| 356 | checkManageUsersPermission("makeInitialized"); |
| 357 | synchronized (mPackagesLock) { |
| 358 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 359 | if (info == null || info.partial) { |
| 360 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
| 361 | } |
| 362 | if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 363 | info.flags |= UserInfo.FLAG_INITIALIZED; |
| 364 | writeUserLocked(info); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 369 | @Override |
| 370 | public Bundle getUserRestrictions(int userId) { |
| 371 | // checkManageUsersPermission("getUserRestrictions"); |
| 372 | |
| 373 | synchronized (mPackagesLock) { |
| 374 | Bundle restrictions = mUserRestrictions.get(userId); |
| 375 | return restrictions != null ? restrictions : Bundle.EMPTY; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public void setUserRestrictions(Bundle restrictions, int userId) { |
| 381 | checkManageUsersPermission("setUserRestrictions"); |
| 382 | |
| 383 | synchronized (mPackagesLock) { |
| 384 | mUserRestrictions.get(userId).putAll(restrictions); |
| 385 | writeUserLocked(mUsers.get(userId)); |
| 386 | } |
| 387 | } |
| 388 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 389 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 390 | * Check if we've hit the limit of how many users can be created. |
| 391 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 392 | private boolean isUserLimitReachedLocked() { |
| 393 | int nUsers = mUsers.size(); |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 394 | return nUsers >= UserManager.getMaxSupportedUsers(); |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 398 | * Enforces that only the system UID or root's UID or apps that have the |
| 399 | * {@link android.Manifest.permission.MANAGE_USERS MANAGE_USERS} |
| 400 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 401 | * |
| 402 | * @param message used as message if SecurityException is thrown |
| 403 | * @throws SecurityException if the caller is not system or root |
| 404 | */ |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 405 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 406 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 407 | if (uid != Process.SYSTEM_UID && uid != 0 |
| 408 | && ActivityManager.checkComponentPermission( |
| 409 | android.Manifest.permission.MANAGE_USERS, |
| 410 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 411 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 415 | private void writeBitmapLocked(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 416 | try { |
| 417 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 418 | File file = new File(dir, USER_PHOTO_FILENAME); |
| 419 | if (!dir.exists()) { |
| 420 | dir.mkdir(); |
| 421 | FileUtils.setPermissions( |
| 422 | dir.getPath(), |
| 423 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 424 | -1, -1); |
| 425 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 426 | FileOutputStream os; |
| 427 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 428 | info.iconPath = file.getAbsolutePath(); |
| 429 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 430 | try { |
| 431 | os.close(); |
| 432 | } catch (IOException ioe) { |
| 433 | // What the ... ! |
| 434 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 435 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 436 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 437 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 440 | /** |
| 441 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 442 | * cache it elsewhere. |
| 443 | * @return the array of user ids. |
| 444 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 445 | public int[] getUserIds() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 446 | synchronized (mPackagesLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 447 | return mUserIds; |
| 448 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 451 | int[] getUserIdsLPr() { |
| 452 | return mUserIds; |
| 453 | } |
| 454 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 455 | private void readUserList() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 456 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 457 | readUserListLocked(); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | private void readUserListLocked() { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 462 | mGuestEnabled = false; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 463 | if (!mUserListFile.exists()) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 464 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 468 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 469 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 470 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 471 | XmlPullParser parser = Xml.newPullParser(); |
| 472 | parser.setInput(fis, null); |
| 473 | int type; |
| 474 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 475 | && type != XmlPullParser.END_DOCUMENT) { |
| 476 | ; |
| 477 | } |
| 478 | |
| 479 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 480 | Slog.e(LOG_TAG, "Unable to read user list"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 481 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 482 | return; |
| 483 | } |
| 484 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 485 | mNextSerialNumber = -1; |
| 486 | if (parser.getName().equals(TAG_USERS)) { |
| 487 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 488 | if (lastSerialNumber != null) { |
| 489 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 490 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 491 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 492 | if (versionNumber != null) { |
| 493 | mUserVersion = Integer.parseInt(versionNumber); |
| 494 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 497 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 498 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
| 499 | String id = parser.getAttributeValue(null, ATTR_ID); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 500 | UserInfo user = readUserLocked(Integer.parseInt(id)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 501 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 502 | if (user != null) { |
| 503 | mUsers.put(user.id, user); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 504 | if (user.isGuest()) { |
| 505 | mGuestEnabled = true; |
| 506 | } |
| 507 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 508 | mNextSerialNumber = user.id + 1; |
| 509 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 510 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 513 | updateUserIdsLocked(); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 514 | upgradeIfNecessary(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 515 | } catch (IOException ioe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 516 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 517 | } catch (XmlPullParserException pe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 518 | fallbackToSingleUserLocked(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 519 | } finally { |
| 520 | if (fis != null) { |
| 521 | try { |
| 522 | fis.close(); |
| 523 | } catch (IOException e) { |
| 524 | } |
| 525 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 529 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 530 | * Upgrade steps between versions, either for fixing bugs or changing the data format. |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 531 | */ |
| 532 | private void upgradeIfNecessary() { |
| 533 | int userVersion = mUserVersion; |
| 534 | if (userVersion < 1) { |
| 535 | // Assign a proper name for the owner, if not initialized correctly before |
| 536 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 537 | if ("Primary".equals(user.name)) { |
| 538 | user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name); |
| 539 | writeUserLocked(user); |
| 540 | } |
| 541 | userVersion = 1; |
| 542 | } |
| 543 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 544 | if (userVersion < 2) { |
| 545 | // Owner should be marked as initialized |
| 546 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 547 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 548 | user.flags |= UserInfo.FLAG_INITIALIZED; |
| 549 | writeUserLocked(user); |
| 550 | } |
| 551 | userVersion = 2; |
| 552 | } |
| 553 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 554 | if (userVersion < USER_VERSION) { |
| 555 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 556 | + USER_VERSION); |
| 557 | } else { |
| 558 | mUserVersion = userVersion; |
| 559 | writeUserListLocked(); |
| 560 | } |
| 561 | } |
| 562 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 563 | private void fallbackToSingleUserLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 564 | // Create the primary user |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 565 | UserInfo primary = new UserInfo(UserHandle.USER_OWNER, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 566 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 567 | UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 568 | mUsers.put(0, primary); |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 569 | mNextSerialNumber = MIN_USER_ID; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 570 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 571 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 572 | mUserRestrictions.append(UserHandle.USER_OWNER, restrictions); |
| 573 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 574 | updateUserIdsLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 575 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 576 | writeUserListLocked(); |
| 577 | writeUserLocked(primary); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* |
| 581 | * Writes the user file in this format: |
| 582 | * |
| 583 | * <user flags="20039023" id="0"> |
| 584 | * <name>Primary</name> |
| 585 | * </user> |
| 586 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 587 | private void writeUserLocked(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 588 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 589 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + ".xml")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 590 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 591 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 592 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 593 | |
| 594 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 595 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 596 | serializer.setOutput(bos, "utf-8"); |
| 597 | serializer.startDocument(null, true); |
| 598 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 599 | |
| 600 | serializer.startTag(null, TAG_USER); |
| 601 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 602 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 603 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 604 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 605 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 606 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 607 | if (userInfo.iconPath != null) { |
| 608 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 609 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 610 | if (userInfo.partial) { |
| 611 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 612 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 613 | |
| 614 | serializer.startTag(null, TAG_NAME); |
| 615 | serializer.text(userInfo.name); |
| 616 | serializer.endTag(null, TAG_NAME); |
| 617 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 618 | Bundle restrictions = mUserRestrictions.get(userInfo.id); |
| 619 | if (restrictions != null) { |
| 620 | serializer.startTag(null, TAG_RESTRICTIONS); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 621 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 622 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 623 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 624 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 625 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 626 | writeBoolean(serializer, restrictions, |
| 627 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 628 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 629 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
Emily Bernier | b223f73 | 2013-04-11 15:46:36 -0400 | [diff] [blame] | 630 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 631 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 632 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 633 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 634 | serializer.endTag(null, TAG_USER); |
| 635 | |
| 636 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 637 | userFile.finishWrite(fos); |
| 638 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 639 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 640 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Writes the user list file in this format: |
| 646 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 647 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 648 | * <user id="0"></user> |
| 649 | * <user id="2"></user> |
| 650 | * </users> |
| 651 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 652 | private void writeUserListLocked() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 653 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 654 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 655 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 656 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 657 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 658 | |
| 659 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 660 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 661 | serializer.setOutput(bos, "utf-8"); |
| 662 | serializer.startDocument(null, true); |
| 663 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 664 | |
| 665 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 666 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 667 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 668 | |
| 669 | for (int i = 0; i < mUsers.size(); i++) { |
| 670 | UserInfo user = mUsers.valueAt(i); |
| 671 | serializer.startTag(null, TAG_USER); |
| 672 | serializer.attribute(null, ATTR_ID, Integer.toString(user.id)); |
| 673 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | serializer.endTag(null, TAG_USERS); |
| 677 | |
| 678 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 679 | userListFile.finishWrite(fos); |
| 680 | } catch (Exception e) { |
| 681 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 682 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 686 | private UserInfo readUserLocked(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 687 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 688 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 689 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 690 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 691 | long creationTime = 0L; |
| 692 | long lastLoggedInTime = 0L; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 693 | boolean partial = false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 694 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 695 | |
| 696 | FileInputStream fis = null; |
| 697 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 698 | AtomicFile userFile = |
| 699 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + ".xml")); |
| 700 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 701 | XmlPullParser parser = Xml.newPullParser(); |
| 702 | parser.setInput(fis, null); |
| 703 | int type; |
| 704 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 705 | && type != XmlPullParser.END_DOCUMENT) { |
| 706 | ; |
| 707 | } |
| 708 | |
| 709 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 710 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 711 | return null; |
| 712 | } |
| 713 | |
| 714 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 715 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 716 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 717 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 718 | return null; |
| 719 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 720 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 721 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 722 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 723 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 724 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 725 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 726 | if ("true".equals(valueString)) { |
| 727 | partial = true; |
| 728 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 729 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 730 | int outerDepth = parser.getDepth(); |
| 731 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 732 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 733 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 734 | continue; |
| 735 | } |
| 736 | String tag = parser.getName(); |
| 737 | if (TAG_NAME.equals(tag)) { |
| 738 | type = parser.next(); |
| 739 | if (type == XmlPullParser.TEXT) { |
| 740 | name = parser.getText(); |
| 741 | } |
| 742 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 743 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 744 | readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 745 | readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 746 | readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 747 | readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
Maggie Benthall | a12fccf | 2013-03-14 18:02:12 -0400 | [diff] [blame] | 748 | readBoolean(parser, restrictions, |
| 749 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 750 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 751 | readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
Emily Bernier | b223f73 | 2013-04-11 15:46:36 -0400 | [diff] [blame] | 752 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 753 | readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 757 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 758 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 759 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 760 | userInfo.creationTime = creationTime; |
| 761 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 762 | userInfo.partial = partial; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 763 | mUserRestrictions.append(id, restrictions); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 764 | return userInfo; |
| 765 | |
| 766 | } catch (IOException ioe) { |
| 767 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 768 | } finally { |
| 769 | if (fis != null) { |
| 770 | try { |
| 771 | fis.close(); |
| 772 | } catch (IOException e) { |
| 773 | } |
| 774 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 775 | } |
| 776 | return null; |
| 777 | } |
| 778 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 779 | private void readBoolean(XmlPullParser parser, Bundle restrictions, |
| 780 | String restrictionKey) { |
| 781 | String value = parser.getAttributeValue(null, restrictionKey); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 782 | if (value != null) { |
| 783 | restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value)); |
| 784 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey) |
| 788 | throws IOException { |
| 789 | if (restrictions.containsKey(restrictionKey)) { |
| 790 | xml.attribute(null, restrictionKey, |
| 791 | Boolean.toString(restrictions.getBoolean(restrictionKey))); |
| 792 | } |
| 793 | } |
| 794 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 795 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 796 | String valueString = parser.getAttributeValue(null, attr); |
| 797 | if (valueString == null) return defaultValue; |
| 798 | try { |
| 799 | return Integer.parseInt(valueString); |
| 800 | } catch (NumberFormatException nfe) { |
| 801 | return defaultValue; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 806 | String valueString = parser.getAttributeValue(null, attr); |
| 807 | if (valueString == null) return defaultValue; |
| 808 | try { |
| 809 | return Long.parseLong(valueString); |
| 810 | } catch (NumberFormatException nfe) { |
| 811 | return defaultValue; |
| 812 | } |
| 813 | } |
| 814 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 815 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 816 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 817 | checkManageUsersPermission("Only the system can create users"); |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 818 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 819 | final long ident = Binder.clearCallingIdentity(); |
| 820 | final UserInfo userInfo; |
| 821 | try { |
| 822 | synchronized (mInstallLock) { |
| 823 | synchronized (mPackagesLock) { |
| 824 | if (isUserLimitReachedLocked()) return null; |
| 825 | int userId = getNextAvailableIdLocked(); |
| 826 | userInfo = new UserInfo(userId, name, null, flags); |
| 827 | File userPath = new File(mBaseUserPath, Integer.toString(userId)); |
| 828 | userInfo.serialNumber = mNextSerialNumber++; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 829 | long now = System.currentTimeMillis(); |
| 830 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 831 | userInfo.partial = true; |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 832 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 833 | mUsers.put(userId, userInfo); |
| 834 | writeUserListLocked(); |
| 835 | writeUserLocked(userInfo); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 836 | mPm.createNewUserLILPw(userId, userPath); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 837 | userInfo.partial = false; |
| 838 | writeUserLocked(userInfo); |
| 839 | updateUserIdsLocked(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 840 | Bundle restrictions = new Bundle(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 841 | mUserRestrictions.append(userId, restrictions); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 842 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 843 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 844 | if (userInfo != null) { |
| 845 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 846 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id); |
| 847 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 848 | android.Manifest.permission.MANAGE_USERS); |
| 849 | } |
| 850 | } finally { |
| 851 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 852 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 853 | return userInfo; |
| 854 | } |
| 855 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 856 | /** |
| 857 | * Removes a user and all data directories created for that user. This method should be called |
| 858 | * after the user's processes have been terminated. |
| 859 | * @param id the user's id |
| 860 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 861 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 862 | checkManageUsersPermission("Only the system can remove users"); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 863 | final UserInfo user; |
| 864 | synchronized (mPackagesLock) { |
| 865 | user = mUsers.get(userHandle); |
| 866 | if (userHandle == 0 || user == null) { |
| 867 | return false; |
| 868 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 869 | mRemovingUserIds.put(userHandle, true); |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 870 | // Set this to a partially created user, so that the user will be purged |
| 871 | // on next startup, in case the runtime stops now before stopping and |
| 872 | // removing the user completely. |
| 873 | user.partial = true; |
| 874 | writeUserLocked(user); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 875 | } |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 876 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 877 | int res; |
| 878 | try { |
| 879 | res = ActivityManagerNative.getDefault().stopUser(userHandle, |
| 880 | new IStopUserCallback.Stub() { |
| 881 | @Override |
| 882 | public void userStopped(int userId) { |
| 883 | finishRemoveUser(userId); |
| 884 | } |
| 885 | @Override |
| 886 | public void userStopAborted(int userId) { |
| 887 | } |
| 888 | }); |
| 889 | } catch (RemoteException e) { |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | return res == ActivityManager.USER_OP_SUCCESS; |
| 894 | } |
| 895 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 896 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 897 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 898 | // Let other services shutdown any activity and clean up their state before completely |
| 899 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 900 | long ident = Binder.clearCallingIdentity(); |
| 901 | try { |
| 902 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 903 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 904 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 905 | android.Manifest.permission.MANAGE_USERS, |
| 906 | |
| 907 | new BroadcastReceiver() { |
| 908 | @Override |
| 909 | public void onReceive(Context context, Intent intent) { |
| 910 | if (DBG) { |
| 911 | Slog.i(LOG_TAG, |
| 912 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 913 | + userHandle); |
| 914 | } |
| 915 | new Thread() { |
| 916 | public void run() { |
| 917 | synchronized (mInstallLock) { |
| 918 | synchronized (mPackagesLock) { |
| 919 | removeUserStateLocked(userHandle); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | }.start(); |
| 924 | } |
| 925 | }, |
| 926 | |
| 927 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 928 | } finally { |
| 929 | Binder.restoreCallingIdentity(ident); |
| 930 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 933 | private void removeUserStateLocked(final int userHandle) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 934 | // Cleanup package manager settings |
| 935 | mPm.cleanUpUserLILPw(userHandle); |
| 936 | |
| 937 | // Remove this user from the list |
| 938 | mUsers.remove(userHandle); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 939 | |
| 940 | // Have user ID linger for several seconds to let external storage VFS |
| 941 | // cache entries expire. This must be greater than the 'entry_valid' |
| 942 | // timeout used by the FUSE daemon. |
| 943 | mHandler.postDelayed(new Runnable() { |
| 944 | @Override |
| 945 | public void run() { |
| 946 | synchronized (mPackagesLock) { |
| 947 | mRemovingUserIds.delete(userHandle); |
| 948 | } |
| 949 | } |
| 950 | }, MINUTE_IN_MILLIS); |
| 951 | |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 952 | // Remove user file |
| 953 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + ".xml")); |
| 954 | userFile.delete(); |
| 955 | // Update the user list |
| 956 | writeUserListLocked(); |
| 957 | updateUserIdsLocked(); |
| 958 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
| 959 | } |
| 960 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 961 | private void removeDirectoryRecursive(File parent) { |
| 962 | if (parent.isDirectory()) { |
| 963 | String[] files = parent.list(); |
| 964 | for (String filename : files) { |
| 965 | File child = new File(parent, filename); |
| 966 | removeDirectoryRecursive(child); |
| 967 | } |
| 968 | } |
| 969 | parent.delete(); |
| 970 | } |
| 971 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 972 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 973 | public Bundle getApplicationRestrictions(String packageName) { |
| 974 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 975 | } |
| 976 | |
| 977 | @Override |
| 978 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 979 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 980 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 981 | checkManageUsersPermission("Only system can get restrictions for other users/apps"); |
| 982 | } |
| 983 | synchronized (mPackagesLock) { |
| 984 | // Read the restrictions from XML |
| 985 | return readApplicationRestrictionsLocked(packageName, userId); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 990 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 991 | int userId) { |
| 992 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 993 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 994 | checkManageUsersPermission("Only system can set restrictions for other users/apps"); |
| 995 | } |
| 996 | synchronized (mPackagesLock) { |
| 997 | // Write the restrictions to XML |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 998 | writeApplicationRestrictionsLocked(packageName, restrictions, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1003 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1004 | try { |
| 1005 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 1006 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 1007 | } catch (NameNotFoundException nnfe) { |
| 1008 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1009 | } finally { |
| 1010 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1014 | private Bundle readApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1015 | int userId) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1016 | final Bundle restrictions = new Bundle(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1017 | final ArrayList<String> values = new ArrayList<String>(); |
| 1018 | |
| 1019 | FileInputStream fis = null; |
| 1020 | try { |
| 1021 | AtomicFile restrictionsFile = |
| 1022 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 1023 | RESTRICTIONS_FILE_PREFIX + packageName + ".xml")); |
| 1024 | fis = restrictionsFile.openRead(); |
| 1025 | XmlPullParser parser = Xml.newPullParser(); |
| 1026 | parser.setInput(fis, null); |
| 1027 | int type; |
| 1028 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1029 | && type != XmlPullParser.END_DOCUMENT) { |
| 1030 | ; |
| 1031 | } |
| 1032 | |
| 1033 | if (type != XmlPullParser.START_TAG) { |
| 1034 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 1035 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1036 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1040 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 1041 | String key = parser.getAttributeValue(null, ATTR_KEY); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1042 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1043 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 1044 | if (multiple != null) { |
| 1045 | int count = Integer.parseInt(multiple); |
| 1046 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1047 | if (type == XmlPullParser.START_TAG |
| 1048 | && parser.getName().equals(TAG_VALUE)) { |
| 1049 | values.add(parser.nextText().trim()); |
| 1050 | count--; |
| 1051 | } |
| 1052 | } |
| 1053 | String [] valueStrings = new String[values.size()]; |
| 1054 | values.toArray(valueStrings); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1055 | restrictions.putStringArray(key, valueStrings); |
| 1056 | } else if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 1057 | restrictions.putBoolean(key, Boolean.parseBoolean( |
| 1058 | parser.nextText().trim())); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1059 | } else { |
| 1060 | String value = parser.nextText().trim(); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1061 | restrictions.putString(key, value); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | } catch (IOException ioe) { |
| 1067 | } catch (XmlPullParserException pe) { |
| 1068 | } finally { |
| 1069 | if (fis != null) { |
| 1070 | try { |
| 1071 | fis.close(); |
| 1072 | } catch (IOException e) { |
| 1073 | } |
| 1074 | } |
| 1075 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1076 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | private void writeApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1080 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1081 | FileOutputStream fos = null; |
| 1082 | AtomicFile restrictionsFile = new AtomicFile( |
| 1083 | new File(Environment.getUserSystemDirectory(userId), |
| 1084 | RESTRICTIONS_FILE_PREFIX + packageName + ".xml")); |
| 1085 | try { |
| 1086 | fos = restrictionsFile.startWrite(); |
| 1087 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1088 | |
| 1089 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1090 | final XmlSerializer serializer = new FastXmlSerializer(); |
| 1091 | serializer.setOutput(bos, "utf-8"); |
| 1092 | serializer.startDocument(null, true); |
| 1093 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1094 | |
| 1095 | serializer.startTag(null, TAG_RESTRICTIONS); |
| 1096 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1097 | for (String key : restrictions.keySet()) { |
| 1098 | Object value = restrictions.get(key); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1099 | serializer.startTag(null, TAG_ENTRY); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1100 | serializer.attribute(null, ATTR_KEY, key); |
| 1101 | |
| 1102 | if (value instanceof Boolean) { |
| 1103 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 1104 | serializer.text(value.toString()); |
| 1105 | } else if (value == null || value instanceof String) { |
| 1106 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 1107 | serializer.text(value != null ? (String) value : ""); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1108 | } else { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1109 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 1110 | String[] values = (String[]) value; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1111 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1112 | for (String choice : values) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1113 | serializer.startTag(null, TAG_VALUE); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame^] | 1114 | serializer.text(choice != null ? choice : ""); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1115 | serializer.endTag(null, TAG_VALUE); |
| 1116 | } |
| 1117 | } |
| 1118 | serializer.endTag(null, TAG_ENTRY); |
| 1119 | } |
| 1120 | |
| 1121 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 1122 | |
| 1123 | serializer.endDocument(); |
| 1124 | restrictionsFile.finishWrite(fos); |
| 1125 | } catch (Exception e) { |
| 1126 | restrictionsFile.failWrite(fos); |
| 1127 | Slog.e(LOG_TAG, "Error writing application restrictions list"); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1132 | public int getUserSerialNumber(int userHandle) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1133 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1134 | if (!exists(userHandle)) return -1; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1135 | return getUserInfoLocked(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | @Override |
| 1140 | public int getUserHandle(int userSerialNumber) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1141 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1142 | for (int userId : mUserIds) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1143 | if (getUserInfoLocked(userId).serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1144 | } |
| 1145 | // Not found |
| 1146 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1150 | /** |
| 1151 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 1152 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1153 | private void updateUserIdsLocked() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1154 | int num = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1155 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1156 | if (!mUsers.valueAt(i).partial) { |
| 1157 | num++; |
| 1158 | } |
| 1159 | } |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1160 | final int[] newUsers = new int[num]; |
| 1161 | int n = 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1162 | for (int i = 0; i < mUsers.size(); i++) { |
| 1163 | if (!mUsers.valueAt(i).partial) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1164 | newUsers[n++] = mUsers.keyAt(i); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1165 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1166 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1167 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | /** |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1171 | * Make a note of the last started time of a user. |
| 1172 | * @param userId the user that was just foregrounded |
| 1173 | */ |
| 1174 | public void userForeground(int userId) { |
| 1175 | synchronized (mPackagesLock) { |
| 1176 | UserInfo user = mUsers.get(userId); |
| 1177 | long now = System.currentTimeMillis(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1178 | if (user == null || user.partial) { |
| 1179 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 1180 | return; |
| 1181 | } |
| 1182 | if (now > EPOCH_PLUS_30_YEARS) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1183 | user.lastLoggedInTime = now; |
| 1184 | writeUserLocked(user); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1190 | * Returns the next available user id, filling in any holes in the ids. |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1191 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 1192 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1193 | * @return |
| 1194 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1195 | private int getNextAvailableIdLocked() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1196 | synchronized (mPackagesLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1197 | int i = MIN_USER_ID; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1198 | while (i < Integer.MAX_VALUE) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1199 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1200 | break; |
| 1201 | } |
| 1202 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1203 | } |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1204 | return i; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1205 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1206 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1207 | |
| 1208 | @Override |
| 1209 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1210 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1211 | != PackageManager.PERMISSION_GRANTED) { |
| 1212 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 1213 | + Binder.getCallingPid() |
| 1214 | + ", uid=" + Binder.getCallingUid() |
| 1215 | + " without permission " |
| 1216 | + android.Manifest.permission.DUMP); |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | long now = System.currentTimeMillis(); |
| 1221 | StringBuilder sb = new StringBuilder(); |
| 1222 | synchronized (mPackagesLock) { |
| 1223 | pw.println("Users:"); |
| 1224 | for (int i = 0; i < mUsers.size(); i++) { |
| 1225 | UserInfo user = mUsers.valueAt(i); |
| 1226 | if (user == null) continue; |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1227 | pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1228 | if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> "); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1229 | if (user.partial) pw.print(" <partial>"); |
| 1230 | pw.println(); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1231 | pw.print(" Created: "); |
| 1232 | if (user.creationTime == 0) { |
| 1233 | pw.println("<unknown>"); |
| 1234 | } else { |
| 1235 | sb.setLength(0); |
| 1236 | TimeUtils.formatDuration(now - user.creationTime, sb); |
| 1237 | sb.append(" ago"); |
| 1238 | pw.println(sb); |
| 1239 | } |
| 1240 | pw.print(" Last logged in: "); |
| 1241 | if (user.lastLoggedInTime == 0) { |
| 1242 | pw.println("<unknown>"); |
| 1243 | } else { |
| 1244 | sb.setLength(0); |
| 1245 | TimeUtils.formatDuration(now - user.lastLoggedInTime, sb); |
| 1246 | sb.append(" ago"); |
| 1247 | pw.println(sb); |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1252 | } |