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