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 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
| 22 | import android.app.IStopUserCallback; |
Xiaohui Chen | 203243a | 2015-07-16 11:55:47 -0700 | [diff] [blame] | 23 | import android.app.admin.DevicePolicyManager; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 29 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 30 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 32 | import android.os.Binder; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 34 | import android.os.Environment; |
| 35 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 36 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 37 | import android.os.IUserManager; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 38 | import android.os.Message; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 39 | import android.os.ParcelFileDescriptor; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 40 | import android.os.Parcelable; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 41 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 42 | import android.os.RemoteException; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 43 | import android.os.ServiceManager; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 44 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 45 | import android.os.UserManager; |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 46 | import android.os.storage.StorageManager; |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 47 | import android.os.storage.VolumeInfo; |
| 48 | import android.system.ErrnoException; |
| 49 | import android.system.Os; |
| 50 | import android.system.OsConstants; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 51 | import android.util.AtomicFile; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 52 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 53 | import android.util.Slog; |
| 54 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 55 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 56 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 57 | import android.util.Xml; |
| 58 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 59 | import com.google.android.collect.Sets; |
| 60 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 61 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 62 | import com.android.internal.app.IAppOpsService; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 63 | import com.android.internal.util.ArrayUtils; |
| 64 | import com.android.internal.util.FastXmlSerializer; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 65 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 66 | |
| 67 | import org.xmlpull.v1.XmlPullParser; |
| 68 | import org.xmlpull.v1.XmlPullParserException; |
| 69 | import org.xmlpull.v1.XmlSerializer; |
| 70 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 71 | import java.io.BufferedOutputStream; |
| 72 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 73 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 74 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 75 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 76 | import java.io.FileOutputStream; |
| 77 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 78 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 79 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 80 | import java.util.ArrayList; |
| 81 | import java.util.List; |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 82 | import java.util.Set; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 83 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 84 | import libcore.io.IoUtils; |
| 85 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 86 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 87 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 88 | private static final String LOG_TAG = "UserManagerService"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 89 | |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 90 | private static final boolean DBG = false; |
| 91 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 92 | private static final String TAG_NAME = "name"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 93 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 94 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 95 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 96 | private static final String ATTR_CREATION_TIME = "created"; |
| 97 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 98 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 99 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 100 | private static final String ATTR_PARTIAL = "partial"; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 101 | private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 102 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 103 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 104 | private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 105 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 106 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 107 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 108 | private static final String TAG_ENTRY = "entry"; |
| 109 | private static final String TAG_VALUE = "value"; |
| 110 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 111 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 112 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 113 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 114 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 115 | private static final String ATTR_TYPE_STRING = "s"; |
| 116 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
Amith Yamasani | 5b5aa40 | 2014-06-01 20:10:14 -0700 | [diff] [blame] | 117 | private static final String ATTR_TYPE_INTEGER = "i"; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 118 | private static final String ATTR_TYPE_BUNDLE = "B"; |
| 119 | private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 120 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 121 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 122 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 123 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 124 | private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 125 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 126 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 127 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 128 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 129 | private static final int MIN_USER_ID = 10; |
| 130 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 131 | private static final int USER_VERSION = 5; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 132 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 133 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 134 | |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame^] | 135 | // Maximum number of managed profiles permitted per user is 1. This cannot be increased |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 136 | // without first making sure that the rest of the framework is prepared for it. |
| 137 | private static final int MAX_MANAGED_PROFILES = 1; |
| 138 | |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 139 | /** |
| 140 | * Flag indicating whether device credentials are shared among same-user profiles. |
| 141 | */ |
| 142 | private static final boolean CONFIG_PROFILES_SHARE_CREDENTIAL = true; |
| 143 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 144 | // Set of user restrictions, which can only be enforced by the system |
| 145 | private static final Set<String> SYSTEM_CONTROLLED_RESTRICTIONS = Sets.newArraySet( |
| 146 | UserManager.DISALLOW_RECORD_AUDIO); |
| 147 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 148 | static final int WRITE_USER_MSG = 1; |
| 149 | static final int WRITE_USER_DELAY = 2*1000; // 2 seconds |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 150 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 151 | private static final String XATTR_SERIAL = "user.serial"; |
| 152 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 153 | private final Context mContext; |
| 154 | private final PackageManagerService mPm; |
| 155 | private final Object mInstallLock; |
| 156 | private final Object mPackagesLock; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 157 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 158 | private final Handler mHandler; |
| 159 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 160 | private final File mUsersDir; |
| 161 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 163 | private final SparseArray<UserInfo> mUsers = new SparseArray<UserInfo>(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 164 | private final SparseArray<Bundle> mUserRestrictions = new SparseArray<Bundle>(); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 165 | private final Bundle mGuestRestrictions = new Bundle(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 169 | * for several seconds to work around a VFS caching issue. |
| 170 | */ |
| 171 | // @GuardedBy("mPackagesLock") |
| 172 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 173 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 174 | private int[] mUserIds; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 175 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 176 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 177 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 178 | private IAppOpsService mAppOpsService; |
| 179 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 180 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 181 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 182 | public static UserManagerService getInstance() { |
| 183 | synchronized (UserManagerService.class) { |
| 184 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 185 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 186 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 187 | |
| 188 | /** |
| 189 | * Available for testing purposes. |
| 190 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 191 | UserManagerService(File dataDir, File baseUserPath) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 192 | this(null, null, new Object(), new Object(), dataDir, baseUserPath); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Called by package manager to create the service. This is closely |
| 197 | * associated with the package manager, and the given lock is the |
| 198 | * package manager's own lock. |
| 199 | */ |
| 200 | UserManagerService(Context context, PackageManagerService pm, |
| 201 | Object installLock, Object packagesLock) { |
| 202 | this(context, pm, installLock, packagesLock, |
| 203 | Environment.getDataDirectory(), |
| 204 | new File(Environment.getDataDirectory(), "user")); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 207 | /** |
| 208 | * Available for testing purposes. |
| 209 | */ |
| 210 | private UserManagerService(Context context, PackageManagerService pm, |
| 211 | Object installLock, Object packagesLock, |
| 212 | File dataDir, File baseUserPath) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 213 | mContext = context; |
| 214 | mPm = pm; |
| 215 | mInstallLock = installLock; |
| 216 | mPackagesLock = packagesLock; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 217 | mHandler = new MainHandler(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 218 | synchronized (mInstallLock) { |
| 219 | synchronized (mPackagesLock) { |
| 220 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 221 | mUsersDir.mkdirs(); |
| 222 | // Make zeroth user directory, for services to migrate their files to that location |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 223 | File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 224 | userZeroDir.mkdirs(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 225 | FileUtils.setPermissions(mUsersDir.toString(), |
| 226 | FileUtils.S_IRWXU|FileUtils.S_IRWXG |
| 227 | |FileUtils.S_IROTH|FileUtils.S_IXOTH, |
| 228 | -1, -1); |
| 229 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 230 | initDefaultGuestRestrictions(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 231 | readUserListLocked(); |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 232 | sInstance = this; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void systemReady() { |
| 238 | synchronized (mInstallLock) { |
| 239 | synchronized (mPackagesLock) { |
Amith Yamasani | 756901d | 2012-10-12 12:30:07 -0700 | [diff] [blame] | 240 | // Prune out any partially created/partially removed users. |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 241 | ArrayList<UserInfo> partials = new ArrayList<UserInfo>(); |
| 242 | for (int i = 0; i < mUsers.size(); i++) { |
| 243 | UserInfo ui = mUsers.valueAt(i); |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 244 | if ((ui.partial || ui.guestToRemove) && i != 0) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 245 | partials.add(ui); |
| 246 | } |
| 247 | } |
| 248 | for (int i = 0; i < partials.size(); i++) { |
| 249 | UserInfo ui = partials.get(i); |
Amith Yamasani | a789248 | 2015-08-07 11:09:05 -0700 | [diff] [blame] | 250 | Slog.w(LOG_TAG, "Removing partially created user " + ui.id |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 251 | + " (name=" + ui.name + ")"); |
| 252 | removeUserStateLocked(ui.id); |
| 253 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 254 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 255 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 256 | onUserForeground(UserHandle.USER_SYSTEM); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 257 | mAppOpsService = IAppOpsService.Stub.asInterface( |
| 258 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
| 259 | for (int i = 0; i < mUserIds.length; ++i) { |
| 260 | try { |
| 261 | mAppOpsService.setUserRestrictions(mUserRestrictions.get(mUserIds[i]), mUserIds[i]); |
| 262 | } catch (RemoteException e) { |
| 263 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 264 | } |
| 265 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 269 | public UserInfo getPrimaryUser() { |
| 270 | checkManageUsersPermission("query users"); |
| 271 | synchronized (mPackagesLock) { |
| 272 | for (int i = 0; i < mUsers.size(); i++) { |
| 273 | UserInfo ui = mUsers.valueAt(i); |
| 274 | if (ui.isPrimary()) { |
| 275 | return ui; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 283 | public List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 284 | checkManageUsersPermission("query users"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 285 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 286 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
| 287 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 288 | UserInfo ui = mUsers.valueAt(i); |
| 289 | if (ui.partial) { |
| 290 | continue; |
| 291 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 292 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 293 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 294 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 295 | } |
| 296 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 297 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 300 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 301 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 302 | if (userId != UserHandle.getCallingUserId()) { |
| 303 | checkManageUsersPermission("getting profiles related to user " + userId); |
| 304 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 305 | final long ident = Binder.clearCallingIdentity(); |
| 306 | try { |
| 307 | synchronized (mPackagesLock) { |
| 308 | return getProfilesLocked(userId, enabledOnly); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 309 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 310 | } finally { |
| 311 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 315 | /** Assume permissions already checked and caller's identity cleared */ |
| 316 | private List<UserInfo> getProfilesLocked(int userId, boolean enabledOnly) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 317 | UserInfo user = getUserInfoLocked(userId); |
| 318 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 319 | if (user == null) { |
| 320 | // Probably a dying user |
| 321 | return users; |
| 322 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 323 | for (int i = 0; i < mUsers.size(); i++) { |
| 324 | UserInfo profile = mUsers.valueAt(i); |
| 325 | if (!isProfileOf(user, profile)) { |
| 326 | continue; |
| 327 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 328 | if (enabledOnly && !profile.isEnabled()) { |
| 329 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 330 | } |
Amith Yamasani | 70fcf0c | 2014-07-11 08:40:19 -0700 | [diff] [blame] | 331 | if (mRemovingUserIds.get(profile.id)) { |
| 332 | continue; |
| 333 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 334 | users.add(profile); |
| 335 | } |
| 336 | return users; |
| 337 | } |
| 338 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 339 | @Override |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 340 | public int getCredentialOwnerProfile(int userHandle) { |
| 341 | checkManageUsersPermission("get the credential owner"); |
| 342 | if (CONFIG_PROFILES_SHARE_CREDENTIAL) { |
| 343 | synchronized (mPackagesLock) { |
| 344 | UserInfo profileParent = getProfileParentLocked(userHandle); |
| 345 | if (profileParent != null) { |
| 346 | return profileParent.id; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return userHandle; |
| 352 | } |
| 353 | |
| 354 | @Override |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 355 | public UserInfo getProfileParent(int userHandle) { |
| 356 | checkManageUsersPermission("get the profile parent"); |
| 357 | synchronized (mPackagesLock) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 358 | return getProfileParentLocked(userHandle); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | private UserInfo getProfileParentLocked(int userHandle) { |
| 363 | UserInfo profile = getUserInfoLocked(userHandle); |
| 364 | if (profile == null) { |
| 365 | return null; |
| 366 | } |
| 367 | int parentUserId = profile.profileGroupId; |
| 368 | if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 369 | return null; |
| 370 | } else { |
| 371 | return getUserInfoLocked(parentUserId); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 375 | private boolean isProfileOf(UserInfo user, UserInfo profile) { |
| 376 | return user.id == profile.id || |
| 377 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 378 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 382 | public void setUserEnabled(int userId) { |
| 383 | checkManageUsersPermission("enable user"); |
| 384 | synchronized (mPackagesLock) { |
| 385 | UserInfo info = getUserInfoLocked(userId); |
| 386 | if (info != null && !info.isEnabled()) { |
| 387 | info.flags ^= UserInfo.FLAG_DISABLED; |
| 388 | writeUserLocked(info); |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 394 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 395 | checkManageUsersPermission("query user"); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 396 | synchronized (mPackagesLock) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 397 | return getUserInfoLocked(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 401 | @Override |
| 402 | public boolean isRestricted() { |
| 403 | synchronized (mPackagesLock) { |
| 404 | return getUserInfoLocked(UserHandle.getCallingUserId()).isRestricted(); |
| 405 | } |
| 406 | } |
| 407 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Should be locked on mUsers before calling this. |
| 410 | */ |
| 411 | private UserInfo getUserInfoLocked(int userId) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 412 | UserInfo ui = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 413 | // 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] | 414 | if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 415 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 416 | return null; |
| 417 | } |
| 418 | return ui; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 421 | public boolean exists(int userId) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 422 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 423 | return ArrayUtils.contains(mUserIds, userId); |
| 424 | } |
| 425 | } |
| 426 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 427 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 428 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 429 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 430 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 431 | synchronized (mPackagesLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 432 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 433 | if (info == null || info.partial) { |
| 434 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 435 | return; |
| 436 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 437 | if (name != null && !name.equals(info.name)) { |
| 438 | info.name = name; |
| 439 | writeUserLocked(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 440 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 443 | if (changed) { |
| 444 | sendUserInfoChangedBroadcast(userId); |
| 445 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 448 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 449 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 450 | checkManageUsersPermission("update users"); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 451 | long ident = Binder.clearCallingIdentity(); |
| 452 | try { |
| 453 | synchronized (mPackagesLock) { |
| 454 | UserInfo info = mUsers.get(userId); |
| 455 | if (info == null || info.partial) { |
| 456 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 457 | return; |
| 458 | } |
| 459 | writeBitmapLocked(info, bitmap); |
| 460 | writeUserLocked(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 461 | } |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 462 | sendUserInfoChangedBroadcast(userId); |
| 463 | } finally { |
| 464 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 465 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | private void sendUserInfoChangedBroadcast(int userId) { |
| 469 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 470 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 471 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 472 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 475 | @Override |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 476 | public ParcelFileDescriptor getUserIcon(int userId) { |
| 477 | String iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 478 | synchronized (mPackagesLock) { |
| 479 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 480 | if (info == null || info.partial) { |
| 481 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 482 | return null; |
| 483 | } |
Nicolas Prevot | 88cc346 | 2014-05-14 14:51:48 +0100 | [diff] [blame] | 484 | int callingGroupId = mUsers.get(UserHandle.getCallingUserId()).profileGroupId; |
| 485 | if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 486 | || callingGroupId != info.profileGroupId) { |
| 487 | checkManageUsersPermission("get the icon of a user who is not related"); |
| 488 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 489 | if (info.iconPath == null) { |
| 490 | return null; |
| 491 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 492 | iconPath = info.iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 493 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 494 | |
| 495 | try { |
| 496 | return ParcelFileDescriptor.open( |
| 497 | new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY); |
| 498 | } catch (FileNotFoundException e) { |
| 499 | Log.e(LOG_TAG, "Couldn't find icon file", e); |
| 500 | } |
| 501 | return null; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 504 | public void makeInitialized(int userId) { |
| 505 | checkManageUsersPermission("makeInitialized"); |
| 506 | synchronized (mPackagesLock) { |
| 507 | UserInfo info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 508 | if (info == null || info.partial) { |
| 509 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
| 510 | } |
| 511 | if ((info.flags&UserInfo.FLAG_INITIALIZED) == 0) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 512 | info.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 513 | scheduleWriteUserLocked(info); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 518 | /** |
| 519 | * If default guest restrictions haven't been initialized yet, add the basic |
| 520 | * restrictions. |
| 521 | */ |
| 522 | private void initDefaultGuestRestrictions() { |
| 523 | if (mGuestRestrictions.isEmpty()) { |
| 524 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 525 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 529 | @Override |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 530 | public Bundle getDefaultGuestRestrictions() { |
| 531 | checkManageUsersPermission("getDefaultGuestRestrictions"); |
| 532 | synchronized (mPackagesLock) { |
| 533 | return new Bundle(mGuestRestrictions); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 539 | checkManageUsersPermission("setDefaultGuestRestrictions"); |
| 540 | synchronized (mPackagesLock) { |
| 541 | mGuestRestrictions.clear(); |
| 542 | mGuestRestrictions.putAll(restrictions); |
| 543 | writeUserListLocked(); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | @Override |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 548 | public boolean hasUserRestriction(String restrictionKey, int userId) { |
| 549 | synchronized (mPackagesLock) { |
| 550 | Bundle restrictions = mUserRestrictions.get(userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 551 | return restrictions != null && restrictions.getBoolean(restrictionKey); |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 556 | public Bundle getUserRestrictions(int userId) { |
| 557 | // checkManageUsersPermission("getUserRestrictions"); |
| 558 | |
| 559 | synchronized (mPackagesLock) { |
| 560 | Bundle restrictions = mUserRestrictions.get(userId); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 561 | return restrictions != null ? new Bundle(restrictions) : new Bundle(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
| 565 | @Override |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 566 | public void setUserRestriction(String key, boolean value, int userId) { |
Fyodor Kupolov | d4b2604 | 2015-07-27 14:30:59 -0700 | [diff] [blame] | 567 | checkManageUsersPermission("setUserRestriction"); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 568 | synchronized (mPackagesLock) { |
| 569 | if (!SYSTEM_CONTROLLED_RESTRICTIONS.contains(key)) { |
| 570 | Bundle restrictions = getUserRestrictions(userId); |
| 571 | restrictions.putBoolean(key, value); |
| 572 | setUserRestrictionsInternalLocked(restrictions, userId); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void setSystemControlledUserRestriction(String key, boolean value, int userId) { |
| 579 | checkSystemOrRoot("setSystemControlledUserRestriction"); |
| 580 | synchronized (mPackagesLock) { |
| 581 | Bundle restrictions = getUserRestrictions(userId); |
| 582 | restrictions.putBoolean(key, value); |
| 583 | setUserRestrictionsInternalLocked(restrictions, userId); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 588 | public void setUserRestrictions(Bundle restrictions, int userId) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 589 | checkManageUsersPermission("setUserRestrictions"); |
Amith Yamasani | 0343ec3 | 2013-07-22 14:52:06 -0700 | [diff] [blame] | 590 | if (restrictions == null) return; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 591 | |
| 592 | synchronized (mPackagesLock) { |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 593 | final Bundle oldUserRestrictions = mUserRestrictions.get(userId); |
| 594 | // Restore the original state of system controlled restrictions from oldUserRestrictions |
| 595 | for (String key : SYSTEM_CONTROLLED_RESTRICTIONS) { |
| 596 | restrictions.remove(key); |
| 597 | if (oldUserRestrictions.containsKey(key)) { |
| 598 | restrictions.putBoolean(key, oldUserRestrictions.getBoolean(key)); |
| 599 | } |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 600 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 601 | setUserRestrictionsInternalLocked(restrictions, userId); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 605 | private void setUserRestrictionsInternalLocked(Bundle restrictions, int userId) { |
| 606 | final Bundle userRestrictions = mUserRestrictions.get(userId); |
| 607 | userRestrictions.clear(); |
| 608 | userRestrictions.putAll(restrictions); |
| 609 | long token = Binder.clearCallingIdentity(); |
| 610 | try { |
| 611 | mAppOpsService.setUserRestrictions(userRestrictions, userId); |
| 612 | } catch (RemoteException e) { |
| 613 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 614 | } finally { |
| 615 | Binder.restoreCallingIdentity(token); |
| 616 | } |
| 617 | scheduleWriteUserLocked(mUsers.get(userId)); |
| 618 | } |
| 619 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 620 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 621 | * Check if we've hit the limit of how many users can be created. |
| 622 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 623 | private boolean isUserLimitReachedLocked() { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 624 | return getAliveUsersExcludingGuestsCountLocked() >= UserManager.getMaxSupportedUsers(); |
| 625 | } |
| 626 | |
| 627 | @Override |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame^] | 628 | public boolean canAddMoreManagedProfiles(int userId) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 629 | checkManageUsersPermission("check if more managed profiles can be added."); |
| 630 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 631 | return false; |
| 632 | } |
Fyodor Kupolov | b615799 | 2015-06-05 15:32:28 -0700 | [diff] [blame] | 633 | if (!mContext.getPackageManager().hasSystemFeature( |
| 634 | PackageManager.FEATURE_MANAGED_USERS)) { |
| 635 | return false; |
| 636 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame^] | 637 | // Limit number of managed profiles that can be created |
| 638 | int managedProfilesCount = getProfiles(userId, true).size() - 1; |
| 639 | if (managedProfilesCount >= MAX_MANAGED_PROFILES) { |
| 640 | return false; |
| 641 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 642 | synchronized(mPackagesLock) { |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame^] | 643 | UserInfo userInfo = getUserInfoLocked(userId); |
| 644 | if (!userInfo.canHaveProfile()) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 645 | return false; |
| 646 | } |
| 647 | int usersCount = getAliveUsersExcludingGuestsCountLocked(); |
| 648 | // We allow creating a managed profile in the special case where there is only one user. |
| 649 | return usersCount == 1 || usersCount < UserManager.getMaxSupportedUsers(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | private int getAliveUsersExcludingGuestsCountLocked() { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 654 | int aliveUserCount = 0; |
| 655 | final int totalUserCount = mUsers.size(); |
| 656 | // Skip over users being removed |
| 657 | for (int i = 0; i < totalUserCount; i++) { |
| 658 | UserInfo user = mUsers.valueAt(i); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 659 | if (!mRemovingUserIds.get(user.id) |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 660 | && !user.isGuest() && !user.partial) { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 661 | aliveUserCount++; |
| 662 | } |
| 663 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 664 | return aliveUserCount; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 668 | * Enforces that only the system UID or root's UID or apps that have the |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 669 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 670 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 671 | * |
| 672 | * @param message used as message if SecurityException is thrown |
| 673 | * @throws SecurityException if the caller is not system or root |
| 674 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 675 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 676 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 677 | if (uid != Process.SYSTEM_UID && uid != 0 |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 678 | && ActivityManager.checkComponentPermission( |
| 679 | android.Manifest.permission.MANAGE_USERS, |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 680 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 681 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 682 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 683 | } |
| 684 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 685 | private static void checkSystemOrRoot(String message) { |
| 686 | final int uid = Binder.getCallingUid(); |
| 687 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 688 | throw new SecurityException("Only system may call: " + message); |
| 689 | } |
| 690 | } |
| 691 | |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 692 | private void writeBitmapLocked(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 693 | try { |
| 694 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 695 | File file = new File(dir, USER_PHOTO_FILENAME); |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 696 | File tmp = new File(dir, USER_PHOTO_FILENAME_TMP); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 697 | if (!dir.exists()) { |
| 698 | dir.mkdir(); |
| 699 | FileUtils.setPermissions( |
| 700 | dir.getPath(), |
| 701 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 702 | -1, -1); |
| 703 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 704 | FileOutputStream os; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 705 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp)) |
| 706 | && tmp.renameTo(file)) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 707 | info.iconPath = file.getAbsolutePath(); |
| 708 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 709 | try { |
| 710 | os.close(); |
| 711 | } catch (IOException ioe) { |
| 712 | // What the ... ! |
| 713 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 714 | tmp.delete(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 715 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 716 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 717 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 720 | /** |
| 721 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 722 | * cache it elsewhere. |
| 723 | * @return the array of user ids. |
| 724 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 725 | public int[] getUserIds() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 726 | synchronized (mPackagesLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 727 | return mUserIds; |
| 728 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 731 | int[] getUserIdsLPr() { |
| 732 | return mUserIds; |
| 733 | } |
| 734 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 735 | private void readUserListLocked() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 736 | if (!mUserListFile.exists()) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 737 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 738 | return; |
| 739 | } |
| 740 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 741 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 742 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 743 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 744 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 745 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 746 | int type; |
| 747 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 748 | && type != XmlPullParser.END_DOCUMENT) { |
| 749 | ; |
| 750 | } |
| 751 | |
| 752 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 753 | Slog.e(LOG_TAG, "Unable to read user list"); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 754 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 758 | mNextSerialNumber = -1; |
| 759 | if (parser.getName().equals(TAG_USERS)) { |
| 760 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 761 | if (lastSerialNumber != null) { |
| 762 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 763 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 764 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 765 | if (versionNumber != null) { |
| 766 | mUserVersion = Integer.parseInt(versionNumber); |
| 767 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 770 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 771 | if (type == XmlPullParser.START_TAG) { |
| 772 | final String name = parser.getName(); |
| 773 | if (name.equals(TAG_USER)) { |
| 774 | String id = parser.getAttributeValue(null, ATTR_ID); |
| 775 | UserInfo user = readUserLocked(Integer.parseInt(id)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 776 | |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 777 | if (user != null) { |
| 778 | mUsers.put(user.id, user); |
| 779 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 780 | mNextSerialNumber = user.id + 1; |
| 781 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 782 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 783 | } else if (name.equals(TAG_GUEST_RESTRICTIONS)) { |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 784 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 785 | && type != XmlPullParser.END_TAG) { |
| 786 | if (type == XmlPullParser.START_TAG) { |
| 787 | if (parser.getName().equals(TAG_RESTRICTIONS)) { |
| 788 | readRestrictionsLocked(parser, mGuestRestrictions); |
| 789 | } |
| 790 | break; |
| 791 | } |
| 792 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 793 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 796 | updateUserIdsLocked(); |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 797 | upgradeIfNecessaryLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 798 | } catch (IOException ioe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 799 | fallbackToSingleUserLocked(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 800 | } catch (XmlPullParserException pe) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 801 | fallbackToSingleUserLocked(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 802 | } finally { |
| 803 | if (fis != null) { |
| 804 | try { |
| 805 | fis.close(); |
| 806 | } catch (IOException e) { |
| 807 | } |
| 808 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 812 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 813 | * 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] | 814 | */ |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 815 | private void upgradeIfNecessaryLocked() { |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 816 | int userVersion = mUserVersion; |
| 817 | if (userVersion < 1) { |
| 818 | // Assign a proper name for the owner, if not initialized correctly before |
| 819 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 820 | if ("Primary".equals(user.name)) { |
| 821 | user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 822 | scheduleWriteUserLocked(user); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 823 | } |
| 824 | userVersion = 1; |
| 825 | } |
| 826 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 827 | if (userVersion < 2) { |
| 828 | // Owner should be marked as initialized |
| 829 | UserInfo user = mUsers.get(UserHandle.USER_OWNER); |
| 830 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 831 | user.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 832 | scheduleWriteUserLocked(user); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 833 | } |
| 834 | userVersion = 2; |
| 835 | } |
| 836 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 837 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 838 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 839 | userVersion = 4; |
| 840 | } |
| 841 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 842 | if (userVersion < 5) { |
| 843 | initDefaultGuestRestrictions(); |
| 844 | userVersion = 5; |
| 845 | } |
| 846 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 847 | if (userVersion < USER_VERSION) { |
| 848 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 849 | + USER_VERSION); |
| 850 | } else { |
| 851 | mUserVersion = userVersion; |
| 852 | writeUserListLocked(); |
| 853 | } |
| 854 | } |
| 855 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 856 | private void fallbackToSingleUserLocked() { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 857 | int flags = UserInfo.FLAG_INITIALIZED; |
| 858 | // In split system user mode, the admin and primary flags are assigned to the first human |
| 859 | // user. |
| 860 | if (!UserManager.isSplitSystemUser()) { |
| 861 | flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY; |
| 862 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 863 | // Create the system user |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 864 | UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 865 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 866 | flags); |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 867 | mUsers.put(system.id, system); |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 868 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 869 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 870 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 871 | Bundle restrictions = new Bundle(); |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 872 | mUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions); |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 873 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 874 | updateUserIdsLocked(); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 875 | initDefaultGuestRestrictions(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 876 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 877 | writeUserListLocked(); |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 878 | writeUserLocked(system); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 879 | } |
| 880 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 881 | private void scheduleWriteUserLocked(UserInfo userInfo) { |
| 882 | if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) { |
| 883 | Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo); |
| 884 | mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY); |
| 885 | } |
| 886 | } |
| 887 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 888 | /* |
| 889 | * Writes the user file in this format: |
| 890 | * |
| 891 | * <user flags="20039023" id="0"> |
| 892 | * <name>Primary</name> |
| 893 | * </user> |
| 894 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 895 | private void writeUserLocked(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 896 | FileOutputStream fos = null; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 897 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 898 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 899 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 900 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 901 | |
| 902 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 903 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 904 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 905 | serializer.startDocument(null, true); |
| 906 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 907 | |
| 908 | serializer.startTag(null, TAG_USER); |
| 909 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 910 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 911 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 912 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 913 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 914 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 915 | if (userInfo.iconPath != null) { |
| 916 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 917 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 918 | if (userInfo.partial) { |
| 919 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 920 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 921 | if (userInfo.guestToRemove) { |
| 922 | serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true"); |
| 923 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 924 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 925 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 926 | Integer.toString(userInfo.profileGroupId)); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 927 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 928 | |
| 929 | serializer.startTag(null, TAG_NAME); |
| 930 | serializer.text(userInfo.name); |
| 931 | serializer.endTag(null, TAG_NAME); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 932 | Bundle restrictions = mUserRestrictions.get(userInfo.id); |
| 933 | if (restrictions != null) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 934 | writeRestrictionsLocked(serializer, restrictions); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 935 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 936 | serializer.endTag(null, TAG_USER); |
| 937 | |
| 938 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 939 | userFile.finishWrite(fos); |
| 940 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 941 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 942 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * Writes the user list file in this format: |
| 948 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 949 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 950 | * <user id="0"></user> |
| 951 | * <user id="2"></user> |
| 952 | * </users> |
| 953 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 954 | private void writeUserListLocked() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 955 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 956 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 957 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 958 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 959 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 960 | |
| 961 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 962 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 963 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 964 | serializer.startDocument(null, true); |
| 965 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 966 | |
| 967 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 968 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 969 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 970 | |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 971 | serializer.startTag(null, TAG_GUEST_RESTRICTIONS); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 972 | writeRestrictionsLocked(serializer, mGuestRestrictions); |
| 973 | serializer.endTag(null, TAG_GUEST_RESTRICTIONS); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 974 | for (int i = 0; i < mUsers.size(); i++) { |
| 975 | UserInfo user = mUsers.valueAt(i); |
| 976 | serializer.startTag(null, TAG_USER); |
| 977 | serializer.attribute(null, ATTR_ID, Integer.toString(user.id)); |
| 978 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | serializer.endTag(null, TAG_USERS); |
| 982 | |
| 983 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 984 | userListFile.finishWrite(fos); |
| 985 | } catch (Exception e) { |
| 986 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 987 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 988 | } |
| 989 | } |
| 990 | |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 991 | private void writeRestrictionsLocked(XmlSerializer serializer, Bundle restrictions) |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 992 | throws IOException { |
| 993 | serializer.startTag(null, TAG_RESTRICTIONS); |
| 994 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 995 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 996 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 997 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 998 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
| 999 | writeBoolean(serializer, restrictions, |
| 1000 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 1001 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 1002 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
| 1003 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 1004 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_REMOVE_USER); |
| 1005 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES); |
| 1006 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_VPN); |
| 1007 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_TETHERING); |
Stuart Scott | 94b038b | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 1008 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_NETWORK_RESET); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1009 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_FACTORY_RESET); |
| 1010 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADD_USER); |
| 1011 | writeBoolean(serializer, restrictions, UserManager.ENSURE_VERIFY_APPS); |
| 1012 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); |
| 1013 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); |
| 1014 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_APPS_CONTROL); |
| 1015 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA); |
| 1016 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1017 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_ADJUST_VOLUME); |
Amith Yamasani | 390989d | 2014-07-17 10:52:03 -0700 | [diff] [blame] | 1018 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_CALLS); |
| 1019 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_SMS); |
Jeff Sharkey | 2cc03e5 | 2015-03-20 11:24:04 -0700 | [diff] [blame] | 1020 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_FUN); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1021 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CREATE_WINDOWS); |
Amith Yamasani | 26af829 | 2014-09-09 09:57:27 -0700 | [diff] [blame] | 1022 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE); |
| 1023 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_OUTGOING_BEAM); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1024 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_WALLPAPER); |
Benjamin Franz | bff46ba | 2015-03-05 18:33:51 +0000 | [diff] [blame] | 1025 | writeBoolean(serializer, restrictions, UserManager.DISALLOW_SAFE_BOOT); |
Nicolas Prevot | f0029c1 | 2015-06-25 14:25:41 -0700 | [diff] [blame] | 1026 | writeBoolean(serializer, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1027 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 1028 | } |
| 1029 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1030 | private UserInfo readUserLocked(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1031 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1032 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1033 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1034 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1035 | long creationTime = 0L; |
| 1036 | long lastLoggedInTime = 0L; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1037 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1038 | boolean partial = false; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1039 | boolean guestToRemove = false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1040 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1041 | |
| 1042 | FileInputStream fis = null; |
| 1043 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1044 | AtomicFile userFile = |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1045 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1046 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1047 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1048 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1049 | int type; |
| 1050 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1051 | && type != XmlPullParser.END_DOCUMENT) { |
| 1052 | ; |
| 1053 | } |
| 1054 | |
| 1055 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1056 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1057 | return null; |
| 1058 | } |
| 1059 | |
| 1060 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1061 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 1062 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1063 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1064 | return null; |
| 1065 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1066 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 1067 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1068 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1069 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 1070 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1071 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 1072 | UserInfo.NO_PROFILE_GROUP_ID); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1073 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 1074 | if ("true".equals(valueString)) { |
| 1075 | partial = true; |
| 1076 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1077 | valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE); |
| 1078 | if ("true".equals(valueString)) { |
| 1079 | guestToRemove = true; |
| 1080 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1081 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1082 | int outerDepth = parser.getDepth(); |
| 1083 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1084 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1085 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1086 | continue; |
| 1087 | } |
| 1088 | String tag = parser.getName(); |
| 1089 | if (TAG_NAME.equals(tag)) { |
| 1090 | type = parser.next(); |
| 1091 | if (type == XmlPullParser.TEXT) { |
| 1092 | name = parser.getText(); |
| 1093 | } |
| 1094 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1095 | readRestrictionsLocked(parser, restrictions); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1099 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1100 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1101 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1102 | userInfo.creationTime = creationTime; |
| 1103 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1104 | userInfo.partial = partial; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1105 | userInfo.guestToRemove = guestToRemove; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1106 | userInfo.profileGroupId = profileGroupId; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1107 | mUserRestrictions.append(id, restrictions); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1108 | return userInfo; |
| 1109 | |
| 1110 | } catch (IOException ioe) { |
| 1111 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1112 | } finally { |
| 1113 | if (fis != null) { |
| 1114 | try { |
| 1115 | fis.close(); |
| 1116 | } catch (IOException e) { |
| 1117 | } |
| 1118 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1119 | } |
| 1120 | return null; |
| 1121 | } |
| 1122 | |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1123 | private void readRestrictionsLocked(XmlPullParser parser, Bundle restrictions) |
| 1124 | throws IOException { |
| 1125 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_WIFI); |
| 1126 | readBoolean(parser, restrictions, UserManager.DISALLOW_MODIFY_ACCOUNTS); |
| 1127 | readBoolean(parser, restrictions, UserManager.DISALLOW_INSTALL_APPS); |
| 1128 | readBoolean(parser, restrictions, UserManager.DISALLOW_UNINSTALL_APPS); |
| 1129 | readBoolean(parser, restrictions, UserManager.DISALLOW_SHARE_LOCATION); |
| 1130 | readBoolean(parser, restrictions, |
| 1131 | UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); |
| 1132 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_BLUETOOTH); |
| 1133 | readBoolean(parser, restrictions, UserManager.DISALLOW_USB_FILE_TRANSFER); |
| 1134 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CREDENTIALS); |
| 1135 | readBoolean(parser, restrictions, UserManager.DISALLOW_REMOVE_USER); |
| 1136 | readBoolean(parser, restrictions, UserManager.DISALLOW_DEBUGGING_FEATURES); |
| 1137 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_VPN); |
| 1138 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_TETHERING); |
Stuart Scott | 94b038b | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 1139 | readBoolean(parser, restrictions, UserManager.DISALLOW_NETWORK_RESET); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1140 | readBoolean(parser, restrictions, UserManager.DISALLOW_FACTORY_RESET); |
| 1141 | readBoolean(parser, restrictions, UserManager.DISALLOW_ADD_USER); |
| 1142 | readBoolean(parser, restrictions, UserManager.ENSURE_VERIFY_APPS); |
| 1143 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); |
| 1144 | readBoolean(parser, restrictions, UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); |
| 1145 | readBoolean(parser, restrictions, UserManager.DISALLOW_APPS_CONTROL); |
| 1146 | readBoolean(parser, restrictions, |
| 1147 | UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA); |
| 1148 | readBoolean(parser, restrictions, UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 1149 | readBoolean(parser, restrictions, UserManager.DISALLOW_ADJUST_VOLUME); |
Amith Yamasani | 390989d | 2014-07-17 10:52:03 -0700 | [diff] [blame] | 1150 | readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_CALLS); |
| 1151 | readBoolean(parser, restrictions, UserManager.DISALLOW_SMS); |
Jeff Sharkey | 2cc03e5 | 2015-03-20 11:24:04 -0700 | [diff] [blame] | 1152 | readBoolean(parser, restrictions, UserManager.DISALLOW_FUN); |
Jason Monk | 1c7c319 | 2014-06-26 12:52:18 -0400 | [diff] [blame] | 1153 | readBoolean(parser, restrictions, UserManager.DISALLOW_CREATE_WINDOWS); |
Amith Yamasani | 26af829 | 2014-09-09 09:57:27 -0700 | [diff] [blame] | 1154 | readBoolean(parser, restrictions, UserManager.DISALLOW_CROSS_PROFILE_COPY_PASTE); |
| 1155 | readBoolean(parser, restrictions, UserManager.DISALLOW_OUTGOING_BEAM); |
Benjamin Franz | f3ece36 | 2015-02-11 10:51:10 +0000 | [diff] [blame] | 1156 | readBoolean(parser, restrictions, UserManager.DISALLOW_WALLPAPER); |
Benjamin Franz | bff46ba | 2015-03-05 18:33:51 +0000 | [diff] [blame] | 1157 | readBoolean(parser, restrictions, UserManager.DISALLOW_SAFE_BOOT); |
Nicolas Prevot | f0029c1 | 2015-06-25 14:25:41 -0700 | [diff] [blame] | 1158 | readBoolean(parser, restrictions, UserManager.ALLOW_PARENT_PROFILE_APP_LINKING); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1159 | } |
| 1160 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1161 | private void readBoolean(XmlPullParser parser, Bundle restrictions, |
| 1162 | String restrictionKey) { |
| 1163 | String value = parser.getAttributeValue(null, restrictionKey); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1164 | if (value != null) { |
| 1165 | restrictions.putBoolean(restrictionKey, Boolean.parseBoolean(value)); |
| 1166 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | private void writeBoolean(XmlSerializer xml, Bundle restrictions, String restrictionKey) |
| 1170 | throws IOException { |
| 1171 | if (restrictions.containsKey(restrictionKey)) { |
| 1172 | xml.attribute(null, restrictionKey, |
| 1173 | Boolean.toString(restrictions.getBoolean(restrictionKey))); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1177 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 1178 | String valueString = parser.getAttributeValue(null, attr); |
| 1179 | if (valueString == null) return defaultValue; |
| 1180 | try { |
| 1181 | return Integer.parseInt(valueString); |
| 1182 | } catch (NumberFormatException nfe) { |
| 1183 | return defaultValue; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 1188 | String valueString = parser.getAttributeValue(null, attr); |
| 1189 | if (valueString == null) return defaultValue; |
| 1190 | try { |
| 1191 | return Long.parseLong(valueString); |
| 1192 | } catch (NumberFormatException nfe) { |
| 1193 | return defaultValue; |
| 1194 | } |
| 1195 | } |
| 1196 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1197 | private boolean isPackageInstalled(String pkg, int userId) { |
| 1198 | final ApplicationInfo info = mPm.getApplicationInfo(pkg, |
| 1199 | PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1200 | userId); |
| 1201 | if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 1202 | return false; |
| 1203 | } |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1207 | /** |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1208 | * Removes all the restrictions files (res_<packagename>) for a given user. |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1209 | * Does not do any permissions checking. |
| 1210 | */ |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1211 | private void cleanAppRestrictions(int userId) { |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1212 | synchronized (mPackagesLock) { |
| 1213 | File dir = Environment.getUserSystemDirectory(userId); |
| 1214 | String[] files = dir.list(); |
| 1215 | if (files == null) return; |
| 1216 | for (String fileName : files) { |
| 1217 | if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) { |
| 1218 | File resFile = new File(dir, fileName); |
| 1219 | if (resFile.exists()) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1220 | resFile.delete(); |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1227 | /** |
| 1228 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 1229 | */ |
| 1230 | private void cleanAppRestrictionsForPackage(String pkg, int userId) { |
| 1231 | synchronized (mPackagesLock) { |
| 1232 | File dir = Environment.getUserSystemDirectory(userId); |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1233 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1234 | if (resFile.exists()) { |
| 1235 | resFile.delete(); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1240 | @Override |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1241 | public UserInfo createProfileForUser(String name, int flags, int userId) { |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1242 | checkManageUsersPermission("Only the system can create users"); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1243 | return createUserInternal(name, flags, userId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1246 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1247 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1248 | checkManageUsersPermission("Only the system can create users"); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1249 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1250 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1251 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1252 | private UserInfo createUserInternal(String name, int flags, int parentId) { |
Julia Reynolds | 7517502 | 2014-06-26 16:35:00 -0400 | [diff] [blame] | 1253 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1254 | UserManager.DISALLOW_ADD_USER, false)) { |
| 1255 | Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled."); |
| 1256 | return null; |
| 1257 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1258 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1259 | return null; |
| 1260 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1261 | final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1262 | final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1263 | final long ident = Binder.clearCallingIdentity(); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1264 | UserInfo userInfo = null; |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1265 | final int userId; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1266 | try { |
| 1267 | synchronized (mInstallLock) { |
| 1268 | synchronized (mPackagesLock) { |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1269 | UserInfo parent = null; |
| 1270 | if (parentId != UserHandle.USER_NULL) { |
| 1271 | parent = getUserInfoLocked(parentId); |
| 1272 | if (parent == null) return null; |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1273 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame^] | 1274 | if (isManagedProfile && !canAddMoreManagedProfiles(parentId)) { |
| 1275 | Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1276 | return null; |
| 1277 | } |
| 1278 | if (!isGuest && !isManagedProfile && isUserLimitReachedLocked()) { |
| 1279 | // If we're not adding a guest user or a managed profile and the limit has |
| 1280 | // been reached, cannot add a user. |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1281 | return null; |
| 1282 | } |
| 1283 | // If we're adding a guest and there already exists one, bail. |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1284 | if (isGuest && findCurrentGuestUserLocked() != null) { |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1285 | return null; |
| 1286 | } |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1287 | // In split system user mode, we assign the first human user the primary flag. |
Xiaohui Chen | 203243a | 2015-07-16 11:55:47 -0700 | [diff] [blame] | 1288 | // And if there is no device owner, we also assign the admin flag to primary |
| 1289 | // user. |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1290 | if (UserManager.isSplitSystemUser() |
Xiaohui Chen | 203243a | 2015-07-16 11:55:47 -0700 | [diff] [blame] | 1291 | && !isGuest && !isManagedProfile && getPrimaryUser() == null) { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1292 | flags |= UserInfo.FLAG_PRIMARY; |
Xiaohui Chen | 203243a | 2015-07-16 11:55:47 -0700 | [diff] [blame] | 1293 | DevicePolicyManager devicePolicyManager = (DevicePolicyManager) |
| 1294 | mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 1295 | if (devicePolicyManager == null |
| 1296 | || devicePolicyManager.getDeviceOwner() == null) { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1297 | flags |= UserInfo.FLAG_ADMIN; |
| 1298 | } |
| 1299 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1300 | userId = getNextAvailableIdLocked(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1301 | userInfo = new UserInfo(userId, name, null, flags); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1302 | userInfo.serialNumber = mNextSerialNumber++; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1303 | long now = System.currentTimeMillis(); |
| 1304 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1305 | userInfo.partial = true; |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1306 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1307 | mUsers.put(userId, userInfo); |
| 1308 | writeUserListLocked(); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1309 | if (parent != null) { |
| 1310 | if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1311 | parent.profileGroupId = parent.id; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1312 | scheduleWriteUserLocked(parent); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1313 | } |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1314 | userInfo.profileGroupId = parent.profileGroupId; |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1315 | } |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1316 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 1317 | for (VolumeInfo vol : storage.getWritablePrivateVolumes()) { |
| 1318 | final String volumeUuid = vol.getFsUuid(); |
| 1319 | try { |
| 1320 | final File userDir = Environment.getDataUserDirectory(volumeUuid, |
| 1321 | userId); |
Jeff Sharkey | 3bb8c85 | 2015-07-06 16:44:23 -0700 | [diff] [blame] | 1322 | prepareUserDirectory(mContext, volumeUuid, userId); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1323 | enforceSerialNumber(userDir, userInfo.serialNumber); |
| 1324 | } catch (IOException e) { |
| 1325 | Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e); |
| 1326 | } |
| 1327 | } |
| 1328 | mPm.createNewUserLILPw(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1329 | userInfo.partial = false; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1330 | scheduleWriteUserLocked(userInfo); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1331 | updateUserIdsLocked(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1332 | Bundle restrictions = new Bundle(); |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1333 | mUserRestrictions.append(userId, restrictions); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1334 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1335 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1336 | mPm.newUserCreated(userId); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1337 | if (userInfo != null) { |
| 1338 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 1339 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userInfo.id); |
| 1340 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1341 | android.Manifest.permission.MANAGE_USERS); |
| 1342 | } |
| 1343 | } finally { |
| 1344 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1345 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1346 | return userInfo; |
| 1347 | } |
| 1348 | |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1349 | private int numberOfUsersOfTypeLocked(int flags, boolean excludeDying) { |
| 1350 | int count = 0; |
| 1351 | for (int i = mUsers.size() - 1; i >= 0; i--) { |
| 1352 | UserInfo user = mUsers.valueAt(i); |
| 1353 | if (!excludeDying || !mRemovingUserIds.get(user.id)) { |
| 1354 | if ((user.flags & flags) != 0) { |
| 1355 | count++; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | return count; |
| 1360 | } |
| 1361 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1362 | /** |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1363 | * Find the current guest user. If the Guest user is partial, |
| 1364 | * then do not include it in the results as it is about to die. |
| 1365 | * This is different than {@link #numberOfUsersOfTypeLocked(int, boolean)} due to |
| 1366 | * the special handling of Guests being removed. |
| 1367 | */ |
| 1368 | private UserInfo findCurrentGuestUserLocked() { |
| 1369 | final int size = mUsers.size(); |
| 1370 | for (int i = 0; i < size; i++) { |
| 1371 | final UserInfo user = mUsers.valueAt(i); |
| 1372 | if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) { |
| 1373 | return user; |
| 1374 | } |
| 1375 | } |
| 1376 | return null; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1380 | * Mark this guest user for deletion to allow us to create another guest |
| 1381 | * and switch to that user before actually removing this guest. |
| 1382 | * @param userHandle the userid of the current guest |
| 1383 | * @return whether the user could be marked for deletion |
| 1384 | */ |
| 1385 | public boolean markGuestForDeletion(int userHandle) { |
| 1386 | checkManageUsersPermission("Only the system can remove users"); |
| 1387 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1388 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1389 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
| 1393 | long ident = Binder.clearCallingIdentity(); |
| 1394 | try { |
| 1395 | final UserInfo user; |
| 1396 | synchronized (mPackagesLock) { |
| 1397 | user = mUsers.get(userHandle); |
| 1398 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
| 1399 | return false; |
| 1400 | } |
| 1401 | if (!user.isGuest()) { |
| 1402 | return false; |
| 1403 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1404 | // We set this to a guest user that is to be removed. This is a temporary state |
| 1405 | // where we are allowed to add new Guest users, even if this one is still not |
| 1406 | // removed. This user will still show up in getUserInfo() calls. |
| 1407 | // If we don't get around to removing this Guest user, it will be purged on next |
| 1408 | // startup. |
| 1409 | user.guestToRemove = true; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1410 | // Mark it as disabled, so that it isn't returned any more when |
| 1411 | // profiles are queried. |
| 1412 | user.flags |= UserInfo.FLAG_DISABLED; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1413 | writeUserLocked(user); |
| 1414 | } |
| 1415 | } finally { |
| 1416 | Binder.restoreCallingIdentity(ident); |
| 1417 | } |
| 1418 | return true; |
| 1419 | } |
| 1420 | |
| 1421 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1422 | * Removes a user and all data directories created for that user. This method should be called |
| 1423 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1424 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1425 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1426 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1427 | checkManageUsersPermission("Only the system can remove users"); |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 1428 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1429 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1430 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1434 | long ident = Binder.clearCallingIdentity(); |
| 1435 | try { |
| 1436 | final UserInfo user; |
| 1437 | synchronized (mPackagesLock) { |
| 1438 | user = mUsers.get(userHandle); |
Kenny Guy | 17c9d69 | 2014-06-13 13:51:42 +0100 | [diff] [blame] | 1439 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1440 | return false; |
| 1441 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1442 | |
| 1443 | // We remember deleted user IDs to prevent them from being |
| 1444 | // reused during the current boot; they can still be reused |
| 1445 | // after a reboot. |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1446 | mRemovingUserIds.put(userHandle, true); |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1447 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1448 | try { |
| 1449 | mAppOpsService.removeUser(userHandle); |
| 1450 | } catch (RemoteException e) { |
| 1451 | Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e); |
| 1452 | } |
| 1453 | // Set this to a partially created user, so that the user will be purged |
| 1454 | // on next startup, in case the runtime stops now before stopping and |
| 1455 | // removing the user completely. |
| 1456 | user.partial = true; |
| 1457 | // Mark it as disabled, so that it isn't returned any more when |
| 1458 | // profiles are queried. |
| 1459 | user.flags |= UserInfo.FLAG_DISABLED; |
| 1460 | writeUserLocked(user); |
| 1461 | } |
| 1462 | |
| 1463 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 1464 | && user.isManagedProfile()) { |
| 1465 | // Send broadcast to notify system that the user removed was a |
| 1466 | // managed user. |
| 1467 | sendProfileRemovedBroadcast(user.profileGroupId, user.id); |
| 1468 | } |
| 1469 | |
| 1470 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
| 1471 | int res; |
| 1472 | try { |
| 1473 | res = ActivityManagerNative.getDefault().stopUser(userHandle, |
| 1474 | new IStopUserCallback.Stub() { |
| 1475 | @Override |
| 1476 | public void userStopped(int userId) { |
| 1477 | finishRemoveUser(userId); |
| 1478 | } |
| 1479 | @Override |
| 1480 | public void userStopAborted(int userId) { |
| 1481 | } |
| 1482 | }); |
| 1483 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1484 | return false; |
| 1485 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1486 | return res == ActivityManager.USER_OP_SUCCESS; |
| 1487 | } finally { |
| 1488 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1489 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1492 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1493 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1494 | // Let other services shutdown any activity and clean up their state before completely |
| 1495 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1496 | long ident = Binder.clearCallingIdentity(); |
| 1497 | try { |
| 1498 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 1499 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1500 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1501 | android.Manifest.permission.MANAGE_USERS, |
| 1502 | |
| 1503 | new BroadcastReceiver() { |
| 1504 | @Override |
| 1505 | public void onReceive(Context context, Intent intent) { |
| 1506 | if (DBG) { |
| 1507 | Slog.i(LOG_TAG, |
| 1508 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 1509 | + userHandle); |
| 1510 | } |
| 1511 | new Thread() { |
| 1512 | public void run() { |
| 1513 | synchronized (mInstallLock) { |
| 1514 | synchronized (mPackagesLock) { |
| 1515 | removeUserStateLocked(userHandle); |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | }.start(); |
| 1520 | } |
| 1521 | }, |
| 1522 | |
| 1523 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1524 | } finally { |
| 1525 | Binder.restoreCallingIdentity(ident); |
| 1526 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1529 | private void removeUserStateLocked(final int userHandle) { |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 1530 | mContext.getSystemService(StorageManager.class) |
| 1531 | .deleteUserKey(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1532 | // Cleanup package manager settings |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 1533 | mPm.cleanUpUserLILPw(this, userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1534 | |
| 1535 | // Remove this user from the list |
| 1536 | mUsers.remove(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1537 | // Remove user file |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1538 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1539 | userFile.delete(); |
| 1540 | // Update the user list |
| 1541 | writeUserListLocked(); |
| 1542 | updateUserIdsLocked(); |
| 1543 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
| 1544 | } |
| 1545 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 1546 | private void removeDirectoryRecursive(File parent) { |
| 1547 | if (parent.isDirectory()) { |
| 1548 | String[] files = parent.list(); |
| 1549 | for (String filename : files) { |
| 1550 | File child = new File(parent, filename); |
| 1551 | removeDirectoryRecursive(child); |
| 1552 | } |
| 1553 | } |
| 1554 | parent.delete(); |
| 1555 | } |
| 1556 | |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 1557 | private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1558 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Adam Connors | d4b584e | 2014-06-09 13:55:47 +0100 | [diff] [blame] | 1559 | managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 1560 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 1561 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); |
| 1562 | mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null); |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1565 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1566 | public Bundle getApplicationRestrictions(String packageName) { |
| 1567 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 1568 | } |
| 1569 | |
| 1570 | @Override |
| 1571 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1572 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1573 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1574 | checkManageUsersPermission("Only system can get restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1575 | } |
| 1576 | synchronized (mPackagesLock) { |
| 1577 | // Read the restrictions from XML |
| 1578 | return readApplicationRestrictionsLocked(packageName, userId); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1583 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1584 | int userId) { |
| 1585 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1586 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1587 | checkManageUsersPermission("Only system can set restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1588 | } |
| 1589 | synchronized (mPackagesLock) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1590 | if (restrictions == null || restrictions.isEmpty()) { |
| 1591 | cleanAppRestrictionsForPackage(packageName, userId); |
| 1592 | } else { |
| 1593 | // Write the restrictions to XML |
| 1594 | writeApplicationRestrictionsLocked(packageName, restrictions, userId); |
| 1595 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1596 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 1597 | |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1598 | if (isPackageInstalled(packageName, userId)) { |
| 1599 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 1600 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 1601 | changeIntent.setPackage(packageName); |
| 1602 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1603 | mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId)); |
| 1604 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1607 | @Override |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1608 | public void removeRestrictions() { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1609 | checkManageUsersPermission("Only system can remove restrictions"); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1610 | final int userHandle = UserHandle.getCallingUserId(); |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1611 | removeRestrictionsForUser(userHandle, true); |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1614 | private void removeRestrictionsForUser(final int userHandle, boolean unhideApps) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1615 | synchronized (mPackagesLock) { |
| 1616 | // Remove all user restrictions |
| 1617 | setUserRestrictions(new Bundle(), userHandle); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1618 | // Remove any app restrictions |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1619 | cleanAppRestrictions(userHandle); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1620 | } |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1621 | if (unhideApps) { |
| 1622 | unhideAllInstalledAppsForUser(userHandle); |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1626 | private void unhideAllInstalledAppsForUser(final int userHandle) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1627 | mHandler.post(new Runnable() { |
| 1628 | @Override |
| 1629 | public void run() { |
| 1630 | List<ApplicationInfo> apps = |
| 1631 | mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1632 | userHandle).getList(); |
| 1633 | final long ident = Binder.clearCallingIdentity(); |
| 1634 | try { |
| 1635 | for (ApplicationInfo appInfo : apps) { |
| 1636 | if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0 |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1637 | && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN) |
| 1638 | != 0) { |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1639 | mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false, |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1640 | userHandle); |
| 1641 | } |
| 1642 | } |
| 1643 | } finally { |
| 1644 | Binder.restoreCallingIdentity(ident); |
| 1645 | } |
| 1646 | } |
| 1647 | }); |
| 1648 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1649 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1650 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1651 | try { |
| 1652 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 1653 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 1654 | } catch (NameNotFoundException nnfe) { |
| 1655 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1656 | } finally { |
| 1657 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1658 | } |
| 1659 | } |
| 1660 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1661 | private Bundle readApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1662 | int userId) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1663 | AtomicFile restrictionsFile = |
| 1664 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 1665 | packageToRestrictionsFileName(packageName))); |
| 1666 | return readApplicationRestrictionsLocked(restrictionsFile); |
| 1667 | } |
| 1668 | |
| 1669 | @VisibleForTesting |
| 1670 | static Bundle readApplicationRestrictionsLocked(AtomicFile restrictionsFile) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1671 | final Bundle restrictions = new Bundle(); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1672 | final ArrayList<String> values = new ArrayList<>(); |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 1673 | if (!restrictionsFile.getBaseFile().exists()) { |
Fyodor Kupolov | f6ee224 | 2015-04-06 10:15:07 -0700 | [diff] [blame] | 1674 | return restrictions; |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 1675 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1676 | |
| 1677 | FileInputStream fis = null; |
| 1678 | try { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1679 | fis = restrictionsFile.openRead(); |
| 1680 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1681 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1682 | XmlUtils.nextElement(parser); |
| 1683 | if (parser.getEventType() != XmlPullParser.START_TAG) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1684 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 1685 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1686 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1687 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1688 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 1689 | readEntry(restrictions, values, parser); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1690 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1691 | } catch (IOException|XmlPullParserException e) { |
| 1692 | Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1693 | } finally { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1694 | IoUtils.closeQuietly(fis); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1695 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1696 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1697 | } |
| 1698 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1699 | private static void readEntry(Bundle restrictions, ArrayList<String> values, |
| 1700 | XmlPullParser parser) throws XmlPullParserException, IOException { |
| 1701 | int type = parser.getEventType(); |
| 1702 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 1703 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 1704 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
| 1705 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 1706 | if (multiple != null) { |
| 1707 | values.clear(); |
| 1708 | int count = Integer.parseInt(multiple); |
| 1709 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1710 | if (type == XmlPullParser.START_TAG |
| 1711 | && parser.getName().equals(TAG_VALUE)) { |
| 1712 | values.add(parser.nextText().trim()); |
| 1713 | count--; |
| 1714 | } |
| 1715 | } |
| 1716 | String [] valueStrings = new String[values.size()]; |
| 1717 | values.toArray(valueStrings); |
| 1718 | restrictions.putStringArray(key, valueStrings); |
| 1719 | } else if (ATTR_TYPE_BUNDLE.equals(valType)) { |
| 1720 | restrictions.putBundle(key, readBundleEntry(parser, values)); |
| 1721 | } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) { |
| 1722 | final int outerDepth = parser.getDepth(); |
| 1723 | ArrayList<Bundle> bundleList = new ArrayList<>(); |
| 1724 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1725 | Bundle childBundle = readBundleEntry(parser, values); |
| 1726 | bundleList.add(childBundle); |
| 1727 | } |
| 1728 | restrictions.putParcelableArray(key, |
| 1729 | bundleList.toArray(new Bundle[bundleList.size()])); |
| 1730 | } else { |
| 1731 | String value = parser.nextText().trim(); |
| 1732 | if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 1733 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 1734 | } else if (ATTR_TYPE_INTEGER.equals(valType)) { |
| 1735 | restrictions.putInt(key, Integer.parseInt(value)); |
| 1736 | } else { |
| 1737 | restrictions.putString(key, value); |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values) |
| 1744 | throws IOException, XmlPullParserException { |
| 1745 | Bundle childBundle = new Bundle(); |
| 1746 | final int outerDepth = parser.getDepth(); |
| 1747 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1748 | readEntry(childBundle, values, parser); |
| 1749 | } |
| 1750 | return childBundle; |
| 1751 | } |
| 1752 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1753 | private void writeApplicationRestrictionsLocked(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1754 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1755 | AtomicFile restrictionsFile = new AtomicFile( |
| 1756 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1757 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1758 | writeApplicationRestrictionsLocked(restrictions, restrictionsFile); |
| 1759 | } |
| 1760 | |
| 1761 | @VisibleForTesting |
| 1762 | static void writeApplicationRestrictionsLocked(Bundle restrictions, |
| 1763 | AtomicFile restrictionsFile) { |
| 1764 | FileOutputStream fos = null; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1765 | try { |
| 1766 | fos = restrictionsFile.startWrite(); |
| 1767 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1768 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1769 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1770 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1771 | serializer.startDocument(null, true); |
| 1772 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1773 | |
| 1774 | serializer.startTag(null, TAG_RESTRICTIONS); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1775 | writeBundle(restrictions, serializer); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1776 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 1777 | |
| 1778 | serializer.endDocument(); |
| 1779 | restrictionsFile.finishWrite(fos); |
| 1780 | } catch (Exception e) { |
| 1781 | restrictionsFile.failWrite(fos); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1782 | Slog.e(LOG_TAG, "Error writing application restrictions list", e); |
| 1783 | } |
| 1784 | } |
| 1785 | |
| 1786 | private static void writeBundle(Bundle restrictions, XmlSerializer serializer) |
| 1787 | throws IOException { |
| 1788 | for (String key : restrictions.keySet()) { |
| 1789 | Object value = restrictions.get(key); |
| 1790 | serializer.startTag(null, TAG_ENTRY); |
| 1791 | serializer.attribute(null, ATTR_KEY, key); |
| 1792 | |
| 1793 | if (value instanceof Boolean) { |
| 1794 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 1795 | serializer.text(value.toString()); |
| 1796 | } else if (value instanceof Integer) { |
| 1797 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER); |
| 1798 | serializer.text(value.toString()); |
| 1799 | } else if (value == null || value instanceof String) { |
| 1800 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 1801 | serializer.text(value != null ? (String) value : ""); |
| 1802 | } else if (value instanceof Bundle) { |
| 1803 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 1804 | writeBundle((Bundle) value, serializer); |
| 1805 | } else if (value instanceof Parcelable[]) { |
| 1806 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY); |
| 1807 | Parcelable[] array = (Parcelable[]) value; |
| 1808 | for (Parcelable parcelable : array) { |
| 1809 | if (!(parcelable instanceof Bundle)) { |
| 1810 | throw new IllegalArgumentException("bundle-array can only hold Bundles"); |
| 1811 | } |
| 1812 | serializer.startTag(null, TAG_ENTRY); |
| 1813 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 1814 | writeBundle((Bundle) parcelable, serializer); |
| 1815 | serializer.endTag(null, TAG_ENTRY); |
| 1816 | } |
| 1817 | } else { |
| 1818 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 1819 | String[] values = (String[]) value; |
| 1820 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
| 1821 | for (String choice : values) { |
| 1822 | serializer.startTag(null, TAG_VALUE); |
| 1823 | serializer.text(choice != null ? choice : ""); |
| 1824 | serializer.endTag(null, TAG_VALUE); |
| 1825 | } |
| 1826 | } |
| 1827 | serializer.endTag(null, TAG_ENTRY); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1832 | public int getUserSerialNumber(int userHandle) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1833 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1834 | if (!exists(userHandle)) return -1; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1835 | return getUserInfoLocked(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | @Override |
| 1840 | public int getUserHandle(int userSerialNumber) { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1841 | synchronized (mPackagesLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1842 | for (int userId : mUserIds) { |
Kenny Guy | 945f883 | 2015-02-10 15:17:26 +0000 | [diff] [blame] | 1843 | UserInfo info = getUserInfoLocked(userId); |
| 1844 | if (info != null && info.serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1845 | } |
| 1846 | // Not found |
| 1847 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1848 | } |
| 1849 | } |
| 1850 | |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 1851 | @Override |
| 1852 | public long getUserCreationTime(int userHandle) { |
| 1853 | int callingUserId = UserHandle.getCallingUserId(); |
| 1854 | UserInfo userInfo = null; |
| 1855 | synchronized (mPackagesLock) { |
| 1856 | if (callingUserId == userHandle) { |
| 1857 | userInfo = getUserInfoLocked(userHandle); |
| 1858 | } else { |
| 1859 | UserInfo parent = getProfileParentLocked(userHandle); |
| 1860 | if (parent != null && parent.id == callingUserId) { |
| 1861 | userInfo = getUserInfoLocked(userHandle); |
| 1862 | } |
| 1863 | } |
| 1864 | } |
| 1865 | if (userInfo == null) { |
| 1866 | throw new SecurityException("userHandle can only be the calling user or a managed " |
| 1867 | + "profile associated with this user"); |
| 1868 | } |
| 1869 | return userInfo.creationTime; |
| 1870 | } |
| 1871 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1872 | /** |
| 1873 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 1874 | */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1875 | private void updateUserIdsLocked() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1876 | int num = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1877 | for (int i = 0; i < mUsers.size(); i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1878 | if (!mUsers.valueAt(i).partial) { |
| 1879 | num++; |
| 1880 | } |
| 1881 | } |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1882 | final int[] newUsers = new int[num]; |
| 1883 | int n = 0; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1884 | for (int i = 0; i < mUsers.size(); i++) { |
| 1885 | if (!mUsers.valueAt(i).partial) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1886 | newUsers[n++] = mUsers.keyAt(i); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1887 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1888 | } |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1889 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1893 | * Make a note of the last started time of a user and do some cleanup. |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1894 | * @param userId the user that was just foregrounded |
| 1895 | */ |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 1896 | public void onUserForeground(int userId) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1897 | synchronized (mPackagesLock) { |
| 1898 | UserInfo user = mUsers.get(userId); |
| 1899 | long now = System.currentTimeMillis(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1900 | if (user == null || user.partial) { |
| 1901 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 1902 | return; |
| 1903 | } |
| 1904 | if (now > EPOCH_PLUS_30_YEARS) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1905 | user.lastLoggedInTime = now; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1906 | scheduleWriteUserLocked(user); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1912 | * 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] | 1913 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 1914 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1915 | * @return |
| 1916 | */ |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1917 | private int getNextAvailableIdLocked() { |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1918 | synchronized (mPackagesLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1919 | int i = MIN_USER_ID; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1920 | while (i < Integer.MAX_VALUE) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 1921 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1922 | break; |
| 1923 | } |
| 1924 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1925 | } |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1926 | return i; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1927 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1928 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1929 | |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1930 | private String packageToRestrictionsFileName(String packageName) { |
| 1931 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 1932 | } |
| 1933 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1934 | /** |
| 1935 | * Create new {@code /data/user/[id]} directory and sets default |
| 1936 | * permissions. |
| 1937 | */ |
Jeff Sharkey | 3bb8c85 | 2015-07-06 16:44:23 -0700 | [diff] [blame] | 1938 | public static void prepareUserDirectory(Context context, String volumeUuid, int userId) { |
| 1939 | final StorageManager storage = context.getSystemService(StorageManager.class); |
| 1940 | final File userDir = Environment.getDataUserDirectory(volumeUuid, userId); |
| 1941 | storage.createNewUserDir(userId, userDir); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | /** |
| 1945 | * Enforce that serial number stored in user directory inode matches the |
| 1946 | * given expected value. Gracefully sets the serial number if currently |
| 1947 | * undefined. |
| 1948 | * |
| 1949 | * @throws IOException when problem extracting serial number, or serial |
| 1950 | * number is mismatched. |
| 1951 | */ |
| 1952 | public static void enforceSerialNumber(File file, int serialNumber) throws IOException { |
| 1953 | final int foundSerial = getSerialNumber(file); |
| 1954 | Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial); |
| 1955 | |
| 1956 | if (foundSerial == -1) { |
| 1957 | Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid"); |
| 1958 | try { |
| 1959 | setSerialNumber(file, serialNumber); |
| 1960 | } catch (IOException e) { |
| 1961 | Slog.w(LOG_TAG, "Failed to set serial number on " + file, e); |
| 1962 | } |
| 1963 | |
| 1964 | } else if (foundSerial != serialNumber) { |
| 1965 | throw new IOException("Found serial number " + foundSerial |
| 1966 | + " doesn't match expected " + serialNumber); |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | /** |
| 1971 | * Set serial number stored in user directory inode. |
| 1972 | * |
| 1973 | * @throws IOException if serial number was already set |
| 1974 | */ |
| 1975 | private static void setSerialNumber(File file, int serialNumber) |
| 1976 | throws IOException { |
| 1977 | try { |
| 1978 | final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8); |
| 1979 | Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE); |
| 1980 | } catch (ErrnoException e) { |
| 1981 | throw e.rethrowAsIOException(); |
| 1982 | } |
| 1983 | } |
| 1984 | |
| 1985 | /** |
| 1986 | * Return serial number stored in user directory inode. |
| 1987 | * |
| 1988 | * @return parsed serial number, or -1 if not set |
| 1989 | */ |
| 1990 | private static int getSerialNumber(File file) throws IOException { |
| 1991 | try { |
| 1992 | final byte[] buf = new byte[256]; |
| 1993 | final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf); |
| 1994 | final String serial = new String(buf, 0, len); |
| 1995 | try { |
| 1996 | return Integer.parseInt(serial); |
| 1997 | } catch (NumberFormatException e) { |
| 1998 | throw new IOException("Bad serial number: " + serial); |
| 1999 | } |
| 2000 | } catch (ErrnoException e) { |
| 2001 | if (e.errno == OsConstants.ENODATA) { |
| 2002 | return -1; |
| 2003 | } else { |
| 2004 | throw e.rethrowAsIOException(); |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2009 | @Override |
| 2010 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2011 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2012 | != PackageManager.PERMISSION_GRANTED) { |
| 2013 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 2014 | + Binder.getCallingPid() |
| 2015 | + ", uid=" + Binder.getCallingUid() |
| 2016 | + " without permission " |
| 2017 | + android.Manifest.permission.DUMP); |
| 2018 | return; |
| 2019 | } |
| 2020 | |
| 2021 | long now = System.currentTimeMillis(); |
| 2022 | StringBuilder sb = new StringBuilder(); |
| 2023 | synchronized (mPackagesLock) { |
| 2024 | pw.println("Users:"); |
| 2025 | for (int i = 0; i < mUsers.size(); i++) { |
| 2026 | UserInfo user = mUsers.valueAt(i); |
| 2027 | if (user == null) continue; |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 2028 | pw.print(" "); pw.print(user); pw.print(" serialNo="); pw.print(user.serialNumber); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2029 | if (mRemovingUserIds.get(mUsers.keyAt(i))) pw.print(" <removing> "); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2030 | if (user.partial) pw.print(" <partial>"); |
| 2031 | pw.println(); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2032 | pw.print(" Created: "); |
| 2033 | if (user.creationTime == 0) { |
| 2034 | pw.println("<unknown>"); |
| 2035 | } else { |
| 2036 | sb.setLength(0); |
| 2037 | TimeUtils.formatDuration(now - user.creationTime, sb); |
| 2038 | sb.append(" ago"); |
| 2039 | pw.println(sb); |
| 2040 | } |
| 2041 | pw.print(" Last logged in: "); |
| 2042 | if (user.lastLoggedInTime == 0) { |
| 2043 | pw.println("<unknown>"); |
| 2044 | } else { |
| 2045 | sb.setLength(0); |
| 2046 | TimeUtils.formatDuration(now - user.lastLoggedInTime, sb); |
| 2047 | sb.append(" ago"); |
| 2048 | pw.println(sb); |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | } |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2053 | |
| 2054 | final class MainHandler extends Handler { |
| 2055 | |
| 2056 | @Override |
| 2057 | public void handleMessage(Message msg) { |
| 2058 | switch (msg.what) { |
| 2059 | case WRITE_USER_MSG: |
| 2060 | removeMessages(WRITE_USER_MSG, msg.obj); |
| 2061 | synchronized (mPackagesLock) { |
| 2062 | int userId = ((UserInfo) msg.obj).id; |
| 2063 | UserInfo userInfo = mUsers.get(userId); |
| 2064 | if (userInfo != null) { |
| 2065 | writeUserLocked(userInfo); |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 2071 | |
| 2072 | /** |
| 2073 | * @param userId |
| 2074 | * @return whether the user has been initialized yet |
| 2075 | */ |
| 2076 | boolean isInitialized(int userId) { |
| 2077 | return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 2078 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2079 | } |