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