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