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 | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 19 | import android.Manifest; |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 24 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 25 | import android.app.ActivityManagerNative; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 26 | import android.app.IActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 27 | import android.app.IStopUserCallback; |
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; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 38 | import android.os.Debug; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 39 | import android.os.Environment; |
| 40 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 41 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 42 | import android.os.IUserManager; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 43 | import android.os.Message; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 44 | import android.os.ParcelFileDescriptor; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 45 | import android.os.Parcelable; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 46 | import android.os.PersistableBundle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 47 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 48 | import android.os.RemoteException; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 49 | import android.os.ResultReceiver; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 50 | import android.os.ServiceManager; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 51 | import android.os.ShellCommand; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 53 | import android.os.UserManager; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 54 | import android.os.UserManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 55 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 56 | import android.os.storage.StorageManager; |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 57 | import android.os.storage.VolumeInfo; |
| 58 | import android.system.ErrnoException; |
| 59 | import android.system.Os; |
| 60 | import android.system.OsConstants; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 61 | import android.util.AtomicFile; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 62 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 63 | import android.util.Slog; |
| 64 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 65 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 66 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 67 | import android.util.Xml; |
| 68 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 69 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 70 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 71 | import com.android.internal.app.IAppOpsService; |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 72 | import com.android.internal.logging.MetricsLogger; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 73 | import com.android.internal.util.FastXmlSerializer; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 74 | import com.android.internal.util.Preconditions; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 75 | import com.android.internal.util.XmlUtils; |
Clara Bayarri | 10ad84a | 2015-12-01 17:38:05 +0000 | [diff] [blame] | 76 | import com.android.internal.widget.LockPatternUtils; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 77 | import com.android.server.LocalServices; |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 78 | import com.android.server.pm.Installer.StorageFlags; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 79 | |
| 80 | import org.xmlpull.v1.XmlPullParser; |
| 81 | import org.xmlpull.v1.XmlPullParserException; |
| 82 | import org.xmlpull.v1.XmlSerializer; |
| 83 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 84 | import java.io.BufferedOutputStream; |
| 85 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 86 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 87 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 88 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 89 | import java.io.FileOutputStream; |
| 90 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 91 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 92 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 93 | import java.util.ArrayList; |
| 94 | import java.util.List; |
| 95 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 96 | import libcore.io.IoUtils; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 97 | import libcore.util.Objects; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 98 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 99 | /** |
| 100 | * Service for {@link UserManager}. |
| 101 | * |
| 102 | * Method naming convention: |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 103 | * <ul> |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 104 | * <li> Methods suffixed with "LP" should be called within the {@link #mPackagesLock} lock. |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 105 | * <li> Methods suffixed with "LR" should be called within the {@link #mRestrictionsLock} lock. |
| 106 | * <li> Methods suffixed with "LU" should be called within the {@link #mUsersLock} lock. |
| 107 | * </ul> |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 108 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 109 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 110 | private static final String LOG_TAG = "UserManagerService"; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 111 | static final boolean DBG = true; // DO NOT SUBMIT WITH TRUE |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 112 | private static final boolean DBG_WITH_STACKTRACE = false; // DO NOT SUBMIT WITH TRUE |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 113 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 114 | private static final String TAG_NAME = "name"; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 115 | private static final String TAG_ACCOUNT = "account"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 116 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 117 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 118 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 119 | private static final String ATTR_CREATION_TIME = "created"; |
| 120 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 121 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 122 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 123 | private static final String ATTR_PARTIAL = "partial"; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 124 | private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 125 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 126 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 127 | private static final String ATTR_RESTRICTED_PROFILE_PARENT_ID = "restrictedProfileParentId"; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 128 | private static final String ATTR_SEED_ACCOUNT_NAME = "seedAccountName"; |
| 129 | private static final String ATTR_SEED_ACCOUNT_TYPE = "seedAccountType"; |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 130 | private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 131 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 132 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 133 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 134 | private static final String TAG_DEVICE_POLICY_RESTRICTIONS = "device_policy_restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 135 | private static final String TAG_ENTRY = "entry"; |
| 136 | private static final String TAG_VALUE = "value"; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 137 | private static final String TAG_SEED_ACCOUNT_OPTIONS = "seedAccountOptions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 138 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 139 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 140 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 141 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 142 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 143 | private static final String ATTR_TYPE_STRING = "s"; |
| 144 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
Amith Yamasani | 5b5aa40 | 2014-06-01 20:10:14 -0700 | [diff] [blame] | 145 | private static final String ATTR_TYPE_INTEGER = "i"; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 146 | private static final String ATTR_TYPE_BUNDLE = "B"; |
| 147 | private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 148 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 149 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 150 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 151 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 152 | private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 153 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 154 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 155 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 156 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 157 | private static final int MIN_USER_ID = 10; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 158 | // We need to keep process uid within Integer.MAX_VALUE. |
| 159 | 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] | 160 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 161 | private static final int USER_VERSION = 6; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 162 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 163 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 164 | |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 165 | // 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] | 166 | // without first making sure that the rest of the framework is prepared for it. |
| 167 | private static final int MAX_MANAGED_PROFILES = 1; |
| 168 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 169 | static final int WRITE_USER_MSG = 1; |
| 170 | static final int WRITE_USER_DELAY = 2*1000; // 2 seconds |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 171 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 172 | private static final String XATTR_SERIAL = "user.serial"; |
| 173 | |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 174 | // Tron counters |
| 175 | private static final String TRON_GUEST_CREATED = "users_guest_created"; |
| 176 | private static final String TRON_USER_CREATED = "users_user_created"; |
| 177 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 178 | private final Context mContext; |
| 179 | private final PackageManagerService mPm; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 180 | private final Object mPackagesLock; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 181 | // Short-term lock for internal state, when interaction/sync with PM is not required |
| 182 | private final Object mUsersLock = new Object(); |
| 183 | private final Object mRestrictionsLock = new Object(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 184 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 185 | private final Handler mHandler; |
| 186 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 187 | private final File mUsersDir; |
| 188 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 189 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 190 | /** |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 191 | * User-related information that is used for persisting to flash. Only UserInfo is |
| 192 | * directly exposed to other system apps. |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 193 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 194 | private static class UserData { |
| 195 | // Basic user information and properties |
| 196 | UserInfo info; |
| 197 | // Account name used when there is a strong association between a user and an account |
| 198 | String account; |
| 199 | // Account information for seeding into a newly created user. This could also be |
| 200 | // used for login validation for an existing user, for updating their credentials. |
| 201 | // In the latter case, data may not need to be persisted as it is only valid for the |
| 202 | // current login session. |
| 203 | String seedAccountName; |
| 204 | String seedAccountType; |
| 205 | PersistableBundle seedAccountOptions; |
| 206 | // Whether to perist the seed account information to be available after a boot |
| 207 | boolean persistSeedData; |
| 208 | |
| 209 | void clearSeedAccountData() { |
| 210 | seedAccountName = null; |
| 211 | seedAccountType = null; |
| 212 | seedAccountOptions = null; |
| 213 | persistSeedData = false; |
| 214 | } |
| 215 | } |
| 216 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 217 | @GuardedBy("mUsersLock") |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 218 | private final SparseArray<UserData> mUsers = new SparseArray<>(); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 219 | |
| 220 | /** |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 221 | * User restrictions set via UserManager. This doesn't include restrictions set by |
| 222 | * device owner / profile owners. |
| 223 | * |
| 224 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 225 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 226 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 227 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 228 | * (Otherwise we won't be able to detect what restrictions have changed in |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 229 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 230 | */ |
| 231 | @GuardedBy("mRestrictionsLock") |
| 232 | private final SparseArray<Bundle> mBaseUserRestrictions = new SparseArray<>(); |
| 233 | |
| 234 | /** |
| 235 | * Cached user restrictions that are in effect -- i.e. {@link #mBaseUserRestrictions} combined |
| 236 | * with device / profile owner restrictions. We'll initialize it lazily; use |
| 237 | * {@link #getEffectiveUserRestrictions} to access it. |
| 238 | * |
| 239 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 240 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 241 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 242 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 243 | * (Otherwise we won't be able to detect what restrictions have changed in |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 244 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 245 | */ |
| 246 | @GuardedBy("mRestrictionsLock") |
| 247 | private final SparseArray<Bundle> mCachedEffectiveUserRestrictions = new SparseArray<>(); |
| 248 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 249 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 250 | * User restrictions that have already been applied in |
| 251 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)}. We use it to detect restrictions |
| 252 | * that have changed since the last |
| 253 | * {@link #updateUserRestrictionsInternalLR(Bundle, int)} call. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 254 | */ |
| 255 | @GuardedBy("mRestrictionsLock") |
| 256 | private final SparseArray<Bundle> mAppliedUserRestrictions = new SparseArray<>(); |
| 257 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 258 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 259 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
| 260 | * that should be applied to all users, including guests. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 261 | */ |
| 262 | @GuardedBy("mRestrictionsLock") |
| 263 | private Bundle mDevicePolicyGlobalUserRestrictions; |
| 264 | |
| 265 | /** |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 266 | * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService} |
| 267 | * for each user. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 268 | */ |
| 269 | @GuardedBy("mRestrictionsLock") |
| 270 | private final SparseArray<Bundle> mDevicePolicyLocalUserRestrictions = new SparseArray<>(); |
| 271 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 272 | @GuardedBy("mGuestRestrictions") |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 273 | private final Bundle mGuestRestrictions = new Bundle(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 277 | * for several seconds to work around a VFS caching issue. |
| 278 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 279 | @GuardedBy("mUsersLock") |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 280 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 281 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 282 | @GuardedBy("mUsersLock") |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 283 | private int[] mUserIds; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 284 | @GuardedBy("mPackagesLock") |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 285 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 286 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 287 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 288 | private IAppOpsService mAppOpsService; |
| 289 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 290 | private final LocalService mLocalService; |
| 291 | |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 292 | @GuardedBy("mUsersLock") |
| 293 | private boolean mIsDeviceManaged; |
| 294 | |
| 295 | @GuardedBy("mUsersLock") |
| 296 | private final SparseBooleanArray mIsUserManaged = new SparseBooleanArray(); |
| 297 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 298 | @GuardedBy("mUserRestrictionsListeners") |
| 299 | private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners = |
| 300 | new ArrayList<>(); |
| 301 | |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 302 | private final LockPatternUtils mLockPatternUtils; |
| 303 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 304 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 305 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 306 | public static UserManagerService getInstance() { |
| 307 | synchronized (UserManagerService.class) { |
| 308 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 309 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 310 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 311 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 312 | @VisibleForTesting |
| 313 | UserManagerService(File dataDir) { |
| 314 | this(null, null, new Object(), dataDir); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Called by package manager to create the service. This is closely |
| 319 | * associated with the package manager, and the given lock is the |
| 320 | * package manager's own lock. |
| 321 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 322 | UserManagerService(Context context, PackageManagerService pm, Object packagesLock) { |
| 323 | this(context, pm, packagesLock, Environment.getDataDirectory()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 326 | private UserManagerService(Context context, PackageManagerService pm, |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 327 | Object packagesLock, File dataDir) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 328 | mContext = context; |
| 329 | mPm = pm; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 330 | mPackagesLock = packagesLock; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 331 | mHandler = new MainHandler(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 332 | synchronized (mPackagesLock) { |
| 333 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 334 | mUsersDir.mkdirs(); |
| 335 | // Make zeroth user directory, for services to migrate their files to that location |
| 336 | File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM)); |
| 337 | userZeroDir.mkdirs(); |
| 338 | FileUtils.setPermissions(mUsersDir.toString(), |
| 339 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, |
| 340 | -1, -1); |
| 341 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 342 | initDefaultGuestRestrictions(); |
| 343 | readUserListLP(); |
| 344 | sInstance = this; |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 345 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 346 | mLocalService = new LocalService(); |
| 347 | LocalServices.addService(UserManagerInternal.class, mLocalService); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 348 | mLockPatternUtils = new LockPatternUtils(mContext); |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void systemReady() { |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 352 | // Prune out any partially created, partially removed and ephemeral users. |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 353 | ArrayList<UserInfo> partials = new ArrayList<>(); |
| 354 | synchronized (mUsersLock) { |
| 355 | final int userSize = mUsers.size(); |
| 356 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 357 | UserInfo ui = mUsers.valueAt(i).info; |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 358 | if ((ui.partial || ui.guestToRemove || ui.isEphemeral()) && i != 0) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 359 | partials.add(ui); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 360 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 361 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 362 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 363 | final int partialsSize = partials.size(); |
| 364 | for (int i = 0; i < partialsSize; i++) { |
| 365 | UserInfo ui = partials.get(i); |
| 366 | Slog.w(LOG_TAG, "Removing partially created user " + ui.id |
| 367 | + " (name=" + ui.name + ")"); |
| 368 | removeUserState(ui.id); |
| 369 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 370 | |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 371 | onUserForeground(UserHandle.USER_SYSTEM); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 372 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 373 | mAppOpsService = IAppOpsService.Stub.asInterface( |
| 374 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 375 | |
| 376 | synchronized (mRestrictionsLock) { |
| 377 | applyUserRestrictionsLR(UserHandle.USER_SYSTEM); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 378 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Override |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 382 | public String getUserAccount(int userId) { |
| 383 | checkManageUserAndAcrossUsersFullPermission("get user account"); |
| 384 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 385 | return mUsers.get(userId).account; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | public void setUserAccount(int userId, String accountName) { |
| 391 | checkManageUserAndAcrossUsersFullPermission("set user account"); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 392 | UserData userToUpdate = null; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 393 | synchronized (mPackagesLock) { |
| 394 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 395 | final UserData userData = mUsers.get(userId); |
| 396 | if (userData == null) { |
| 397 | Slog.e(LOG_TAG, "User not found for setting user account: u" + userId); |
| 398 | return; |
| 399 | } |
| 400 | String currentAccount = userData.account; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 401 | if (!Objects.equal(currentAccount, accountName)) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 402 | userData.account = accountName; |
| 403 | userToUpdate = userData; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | if (userToUpdate != null) { |
| 408 | writeUserLP(userToUpdate); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | @Override |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 414 | public UserInfo getPrimaryUser() { |
| 415 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 416 | synchronized (mUsersLock) { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 417 | final int userSize = mUsers.size(); |
| 418 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 419 | UserInfo ui = mUsers.valueAt(i).info; |
Xiaohui Chen | d3e9e18 | 2015-11-18 13:37:32 -0800 | [diff] [blame] | 420 | if (ui.isPrimary() && !mRemovingUserIds.get(ui.id)) { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 421 | return ui; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | return null; |
| 426 | } |
| 427 | |
| 428 | @Override |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 429 | public @NonNull List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 430 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 431 | synchronized (mUsersLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 432 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 433 | final int userSize = mUsers.size(); |
| 434 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 435 | UserInfo ui = mUsers.valueAt(i).info; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 436 | if (ui.partial) { |
| 437 | continue; |
| 438 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 439 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 440 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 441 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 442 | } |
| 443 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 444 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 447 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 448 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 449 | if (userId != UserHandle.getCallingUserId()) { |
| 450 | checkManageUsersPermission("getting profiles related to user " + userId); |
| 451 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 452 | final long ident = Binder.clearCallingIdentity(); |
| 453 | try { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 454 | synchronized (mUsersLock) { |
| 455 | return getProfilesLU(userId, enabledOnly); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 456 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 457 | } finally { |
| 458 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 462 | /** Assume permissions already checked and caller's identity cleared */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 463 | private List<UserInfo> getProfilesLU(int userId, boolean enabledOnly) { |
| 464 | UserInfo user = getUserInfoLU(userId); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 465 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 466 | if (user == null) { |
| 467 | // Probably a dying user |
| 468 | return users; |
| 469 | } |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 470 | final int userSize = mUsers.size(); |
| 471 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 472 | UserInfo profile = mUsers.valueAt(i).info; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 473 | if (!isProfileOf(user, profile)) { |
| 474 | continue; |
| 475 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 476 | if (enabledOnly && !profile.isEnabled()) { |
| 477 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 478 | } |
Amith Yamasani | 70fcf0c | 2014-07-11 08:40:19 -0700 | [diff] [blame] | 479 | if (mRemovingUserIds.get(profile.id)) { |
| 480 | continue; |
| 481 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 482 | users.add(profile); |
| 483 | } |
| 484 | return users; |
| 485 | } |
| 486 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 487 | @Override |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 488 | public int getCredentialOwnerProfile(int userHandle) { |
| 489 | checkManageUsersPermission("get the credential owner"); |
Clara Bayarri | a177111 | 2015-12-18 16:29:18 +0000 | [diff] [blame] | 490 | if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 491 | synchronized (mUsersLock) { |
| 492 | UserInfo profileParent = getProfileParentLU(userHandle); |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 493 | if (profileParent != null) { |
| 494 | return profileParent.id; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return userHandle; |
| 500 | } |
| 501 | |
| 502 | @Override |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 503 | public boolean isSameProfileGroup(int userId, int otherUserId) { |
| 504 | if (userId == otherUserId) return true; |
| 505 | checkManageUsersPermission("check if in the same profile group"); |
| 506 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 507 | return isSameProfileGroupLP(userId, otherUserId); |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 511 | private boolean isSameProfileGroupLP(int userId, int otherUserId) { |
| 512 | synchronized (mUsersLock) { |
| 513 | UserInfo userInfo = getUserInfoLU(userId); |
| 514 | if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 515 | return false; |
| 516 | } |
| 517 | UserInfo otherUserInfo = getUserInfoLU(otherUserId); |
| 518 | if (otherUserInfo == null |
| 519 | || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 520 | return false; |
| 521 | } |
| 522 | return userInfo.profileGroupId == otherUserInfo.profileGroupId; |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 523 | } |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | @Override |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 527 | public UserInfo getProfileParent(int userHandle) { |
| 528 | checkManageUsersPermission("get the profile parent"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 529 | synchronized (mUsersLock) { |
| 530 | return getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 534 | private UserInfo getProfileParentLU(int userHandle) { |
| 535 | UserInfo profile = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 536 | if (profile == null) { |
| 537 | return null; |
| 538 | } |
| 539 | int parentUserId = profile.profileGroupId; |
| 540 | if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 541 | return null; |
| 542 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 543 | return getUserInfoLU(parentUserId); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 547 | private static boolean isProfileOf(UserInfo user, UserInfo profile) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 548 | return user.id == profile.id || |
| 549 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 550 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 553 | private void broadcastProfileAvailabilityChanges(UserHandle profileHandle, |
| 554 | UserHandle parentHandle, Bundle extras) { |
| 555 | // Send intent to profile |
| 556 | Intent intent = new Intent(Intent.ACTION_AVAILABILITY_CHANGED); |
| 557 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 558 | intent.putExtras(extras); |
| 559 | mContext.sendBroadcastAsUser(intent, profileHandle); |
| 560 | |
| 561 | // Send intent to parent |
| 562 | if (parentHandle != null) { |
| 563 | intent = new Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED); |
| 564 | intent.putExtra(Intent.EXTRA_USER, profileHandle); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame] | 565 | intent.putExtra(Intent.EXTRA_USER_HANDLE, profileHandle.getIdentifier()); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 566 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 567 | intent.putExtras(extras); |
| 568 | mContext.sendBroadcastAsUser(intent, parentHandle); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void setQuietModeEnabled(int userHandle, boolean enableQuietMode) { |
| 574 | checkManageUsersPermission("silence profile"); |
| 575 | boolean changed = false; |
| 576 | UserInfo profile, parent; |
| 577 | synchronized (mPackagesLock) { |
| 578 | synchronized (mUsersLock) { |
| 579 | profile = getUserInfoLU(userHandle); |
| 580 | parent = getProfileParentLU(userHandle); |
| 581 | |
| 582 | } |
| 583 | if (profile == null || !profile.isManagedProfile()) { |
| 584 | throw new IllegalArgumentException("User " + userHandle + " is not a profile"); |
| 585 | } |
| 586 | if (profile.isQuietModeEnabled() != enableQuietMode) { |
| 587 | profile.flags ^= UserInfo.FLAG_QUIET_MODE; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 588 | writeUserLP(getUserDataLU(profile.id)); |
Rubin Xu | 0a29ecd | 2015-11-04 15:11:48 +0000 | [diff] [blame] | 589 | changed = true; |
| 590 | } |
| 591 | } |
| 592 | if (changed) { |
| 593 | Bundle extras = new Bundle(); |
| 594 | extras.putBoolean(Intent.EXTRA_QUIET_MODE, enableQuietMode); |
| 595 | broadcastProfileAvailabilityChanges(profile.getUserHandle(), |
| 596 | parent != null ? parent.getUserHandle() : null, extras); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public boolean isQuietModeEnabled(int userHandle) { |
| 602 | synchronized (mPackagesLock) { |
| 603 | UserInfo info; |
| 604 | synchronized (mUsersLock) { |
| 605 | info = getUserInfoLU(userHandle); |
| 606 | } |
| 607 | if (info == null || !info.isManagedProfile()) { |
| 608 | throw new IllegalArgumentException("User " + userHandle + " is not a profile"); |
| 609 | } |
| 610 | return info.isQuietModeEnabled(); |
| 611 | } |
| 612 | } |
| 613 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 614 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 615 | public void setUserEnabled(int userId) { |
| 616 | checkManageUsersPermission("enable user"); |
| 617 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 618 | UserInfo info; |
| 619 | synchronized (mUsersLock) { |
| 620 | info = getUserInfoLU(userId); |
| 621 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 622 | if (info != null && !info.isEnabled()) { |
| 623 | info.flags ^= UserInfo.FLAG_DISABLED; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 624 | writeUserLP(getUserDataLU(info.id)); |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 630 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 631 | checkManageUsersPermission("query user"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 632 | synchronized (mUsersLock) { |
| 633 | return getUserInfoLU(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 637 | @Override |
| 638 | public boolean isRestricted() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 639 | synchronized (mUsersLock) { |
| 640 | return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 644 | @Override |
| 645 | public boolean canHaveRestrictedProfile(int userId) { |
| 646 | checkManageUsersPermission("canHaveRestrictedProfile"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 647 | synchronized (mUsersLock) { |
| 648 | final UserInfo userInfo = getUserInfoLU(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 649 | if (userInfo == null || !userInfo.canHaveProfile()) { |
| 650 | return false; |
| 651 | } |
| 652 | if (!userInfo.isAdmin()) { |
| 653 | return false; |
| 654 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 655 | // restricted profile can be created if there is no DO set and the admin user has no PO; |
| 656 | return !mIsDeviceManaged && !mIsUserManaged.get(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 657 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 660 | /* |
| 661 | * Should be locked on mUsers before calling this. |
| 662 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 663 | private UserInfo getUserInfoLU(int userId) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 664 | final UserData userData = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 665 | // If it is partial and not in the process of being removed, return as unknown user. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 666 | if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 667 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 668 | return null; |
| 669 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 670 | return userData != null ? userData.info : null; |
| 671 | } |
| 672 | |
| 673 | private UserData getUserDataLU(int userId) { |
| 674 | final UserData userData = mUsers.get(userId); |
| 675 | // If it is partial and not in the process of being removed, return as unknown user. |
| 676 | if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) { |
| 677 | return null; |
| 678 | } |
| 679 | return userData; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 682 | /** |
| 683 | * Obtains {@link #mUsersLock} and return UserInfo from mUsers. |
| 684 | * <p>No permissions checking or any addition checks are made</p> |
| 685 | */ |
| 686 | private UserInfo getUserInfoNoChecks(int userId) { |
| 687 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 688 | final UserData userData = mUsers.get(userId); |
| 689 | return userData != null ? userData.info : null; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Obtains {@link #mUsersLock} and return UserData from mUsers. |
| 695 | * <p>No permissions checking or any addition checks are made</p> |
| 696 | */ |
| 697 | private UserData getUserDataNoChecks(int userId) { |
| 698 | synchronized (mUsersLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 699 | return mUsers.get(userId); |
| 700 | } |
| 701 | } |
| 702 | |
Amith Yamasani | 236b2b5 | 2015-08-18 14:32:14 -0700 | [diff] [blame] | 703 | /** Called by PackageManagerService */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 704 | public boolean exists(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 705 | return getUserInfoNoChecks(userId) != null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 708 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 709 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 710 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 711 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 712 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 713 | UserData userData = getUserDataNoChecks(userId); |
| 714 | if (userData == null || userData.info.partial) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 715 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 716 | return; |
| 717 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 718 | if (name != null && !name.equals(userData.info.name)) { |
| 719 | userData.info.name = name; |
| 720 | writeUserLP(userData); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 721 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 722 | } |
| 723 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 724 | if (changed) { |
| 725 | sendUserInfoChangedBroadcast(userId); |
| 726 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 729 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 730 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 731 | checkManageUsersPermission("update users"); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 732 | long ident = Binder.clearCallingIdentity(); |
| 733 | try { |
| 734 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 735 | UserData userData = getUserDataNoChecks(userId); |
| 736 | if (userData == null || userData.info.partial) { |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 737 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 738 | return; |
| 739 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 740 | writeBitmapLP(userData.info, bitmap); |
| 741 | writeUserLP(userData); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 742 | } |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 743 | sendUserInfoChangedBroadcast(userId); |
| 744 | } finally { |
| 745 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 746 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | private void sendUserInfoChangedBroadcast(int userId) { |
| 750 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 751 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 752 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 753 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 756 | @Override |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 757 | public ParcelFileDescriptor getUserIcon(int userId) { |
| 758 | String iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 759 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 760 | UserInfo info = getUserInfoNoChecks(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 761 | if (info == null || info.partial) { |
| 762 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 763 | return null; |
| 764 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 765 | int callingGroupId = getUserInfoNoChecks(UserHandle.getCallingUserId()).profileGroupId; |
Nicolas Prevot | 88cc346 | 2014-05-14 14:51:48 +0100 | [diff] [blame] | 766 | if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 767 | || callingGroupId != info.profileGroupId) { |
| 768 | checkManageUsersPermission("get the icon of a user who is not related"); |
| 769 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 770 | if (info.iconPath == null) { |
| 771 | return null; |
| 772 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 773 | iconPath = info.iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 774 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 775 | |
| 776 | try { |
| 777 | return ParcelFileDescriptor.open( |
| 778 | new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY); |
| 779 | } catch (FileNotFoundException e) { |
| 780 | Log.e(LOG_TAG, "Couldn't find icon file", e); |
| 781 | } |
| 782 | return null; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 785 | public void makeInitialized(int userId) { |
| 786 | checkManageUsersPermission("makeInitialized"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 787 | boolean scheduleWriteUser = false; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 788 | UserData userData; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 789 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 790 | userData = mUsers.get(userId); |
| 791 | if (userData == null || userData.info.partial) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 792 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 793 | return; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 794 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 795 | if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 796 | userData.info.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 797 | scheduleWriteUser = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 800 | if (scheduleWriteUser) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 801 | scheduleWriteUser(userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 802 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 805 | /** |
| 806 | * If default guest restrictions haven't been initialized yet, add the basic |
| 807 | * restrictions. |
| 808 | */ |
| 809 | private void initDefaultGuestRestrictions() { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 810 | synchronized (mGuestRestrictions) { |
| 811 | if (mGuestRestrictions.isEmpty()) { |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 812 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 813 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
| 814 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
| 815 | } |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 819 | @Override |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 820 | public Bundle getDefaultGuestRestrictions() { |
| 821 | checkManageUsersPermission("getDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 822 | synchronized (mGuestRestrictions) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 823 | return new Bundle(mGuestRestrictions); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | @Override |
| 828 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 829 | checkManageUsersPermission("setDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 830 | synchronized (mGuestRestrictions) { |
| 831 | mGuestRestrictions.clear(); |
| 832 | mGuestRestrictions.putAll(restrictions); |
| 833 | } |
| 834 | synchronized (mPackagesLock) { |
| 835 | writeUserListLP(); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 839 | /** |
| 840 | * See {@link UserManagerInternal#setDevicePolicyUserRestrictions(int, Bundle, Bundle)} |
| 841 | */ |
| 842 | void setDevicePolicyUserRestrictions(int userId, @NonNull Bundle local, |
| 843 | @Nullable Bundle global) { |
| 844 | Preconditions.checkNotNull(local); |
| 845 | boolean globalChanged = false; |
| 846 | boolean localChanged; |
| 847 | synchronized (mRestrictionsLock) { |
| 848 | if (global != null) { |
| 849 | // Update global. |
| 850 | globalChanged = !UserRestrictionsUtils.areEqual( |
| 851 | mDevicePolicyGlobalUserRestrictions, global); |
| 852 | if (globalChanged) { |
| 853 | mDevicePolicyGlobalUserRestrictions = global; |
| 854 | } |
| 855 | } |
| 856 | { |
| 857 | // Update local. |
| 858 | final Bundle prev = mDevicePolicyLocalUserRestrictions.get(userId); |
| 859 | localChanged = !UserRestrictionsUtils.areEqual(prev, local); |
| 860 | if (localChanged) { |
| 861 | mDevicePolicyLocalUserRestrictions.put(userId, local); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | if (DBG) { |
| 866 | Log.d(LOG_TAG, "setDevicePolicyUserRestrictions: userId=" + userId |
| 867 | + " global=" + global + (globalChanged ? " (changed)" : "") |
| 868 | + " local=" + local + (localChanged ? " (changed)" : "") |
| 869 | ); |
| 870 | } |
| 871 | // Don't call them within the mRestrictionsLock. |
| 872 | synchronized (mPackagesLock) { |
| 873 | if (globalChanged) { |
| 874 | writeUserListLP(); |
| 875 | } |
| 876 | if (localChanged) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 877 | writeUserLP(getUserDataNoChecks(userId)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | synchronized (mRestrictionsLock) { |
| 882 | if (globalChanged) { |
| 883 | applyUserRestrictionsForAllUsersLR(); |
| 884 | } else if (localChanged) { |
| 885 | applyUserRestrictionsLR(userId); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 890 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 891 | private Bundle computeEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 892 | final Bundle baseRestrictions = |
| 893 | UserRestrictionsUtils.nonNull(mBaseUserRestrictions.get(userId)); |
| 894 | final Bundle global = mDevicePolicyGlobalUserRestrictions; |
| 895 | final Bundle local = mDevicePolicyLocalUserRestrictions.get(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 896 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 897 | if (UserRestrictionsUtils.isEmpty(global) && UserRestrictionsUtils.isEmpty(local)) { |
| 898 | // Common case first. |
| 899 | return baseRestrictions; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 900 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 901 | final Bundle effective = UserRestrictionsUtils.clone(baseRestrictions); |
| 902 | UserRestrictionsUtils.merge(effective, global); |
| 903 | UserRestrictionsUtils.merge(effective, local); |
| 904 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 905 | return effective; |
| 906 | } |
| 907 | |
| 908 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 909 | private void invalidateEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 910 | if (DBG) { |
| 911 | Log.d(LOG_TAG, "invalidateEffectiveUserRestrictions userId=" + userId); |
| 912 | } |
| 913 | mCachedEffectiveUserRestrictions.remove(userId); |
| 914 | } |
| 915 | |
| 916 | private Bundle getEffectiveUserRestrictions(int userId) { |
| 917 | synchronized (mRestrictionsLock) { |
| 918 | Bundle restrictions = mCachedEffectiveUserRestrictions.get(userId); |
| 919 | if (restrictions == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 920 | restrictions = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 921 | mCachedEffectiveUserRestrictions.put(userId, restrictions); |
| 922 | } |
| 923 | return restrictions; |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 927 | /** @return a specific user restriction that's in effect currently. */ |
| 928 | @Override |
| 929 | public boolean hasUserRestriction(String restrictionKey, int userId) { |
| 930 | Bundle restrictions = getEffectiveUserRestrictions(userId); |
| 931 | return restrictions != null && restrictions.getBoolean(restrictionKey); |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * @return UserRestrictions that are in effect currently. This always returns a new |
| 936 | * {@link Bundle}. |
| 937 | */ |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 938 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 939 | public Bundle getUserRestrictions(int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 940 | return UserRestrictionsUtils.clone(getEffectiveUserRestrictions(userId)); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | @Override |
Zoltan Szatmary-Ban | 3bbcedd | 2015-11-26 13:45:51 +0000 | [diff] [blame] | 944 | public boolean hasBaseUserRestriction(String restrictionKey, int userId) { |
| 945 | checkManageUsersPermission("hasBaseUserRestriction"); |
| 946 | synchronized (mRestrictionsLock) { |
| 947 | Bundle bundle = mBaseUserRestrictions.get(userId); |
| 948 | return (bundle != null && bundle.getBoolean(restrictionKey, false)); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | @Override |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 953 | public void setUserRestriction(String key, boolean value, int userId) { |
Fyodor Kupolov | d4b2604 | 2015-07-27 14:30:59 -0700 | [diff] [blame] | 954 | checkManageUsersPermission("setUserRestriction"); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 955 | synchronized (mRestrictionsLock) { |
| 956 | // Note we can't modify Bundles stored in mBaseUserRestrictions directly, so create |
| 957 | // a copy. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 958 | final Bundle newRestrictions = UserRestrictionsUtils.clone( |
| 959 | mBaseUserRestrictions.get(userId)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 960 | newRestrictions.putBoolean(key, value); |
| 961 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 962 | updateUserRestrictionsInternalLR(newRestrictions, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 966 | /** |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 967 | * Optionally updating user restrictions, calculate the effective user restrictions and also |
| 968 | * propagate to other services and system settings. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 969 | * |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 970 | * @param newRestrictions User restrictions to set. |
| 971 | * If null, will not update user restrictions and only does the propagation. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 972 | * @param userId target user ID. |
| 973 | */ |
| 974 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 975 | private void updateUserRestrictionsInternalLR( |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 976 | @Nullable Bundle newRestrictions, int userId) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 977 | |
| 978 | final Bundle prevAppliedRestrictions = UserRestrictionsUtils.nonNull( |
| 979 | mAppliedUserRestrictions.get(userId)); |
| 980 | |
| 981 | // Update base restrictions. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 982 | if (newRestrictions != null) { |
| 983 | // If newRestrictions == the current one, it's probably a bug. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 984 | final Bundle prevBaseRestrictions = mBaseUserRestrictions.get(userId); |
| 985 | |
| 986 | Preconditions.checkState(prevBaseRestrictions != newRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 987 | Preconditions.checkState(mCachedEffectiveUserRestrictions.get(userId) |
| 988 | != newRestrictions); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 989 | |
| 990 | if (!UserRestrictionsUtils.areEqual(prevBaseRestrictions, newRestrictions)) { |
| 991 | mBaseUserRestrictions.put(userId, newRestrictions); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 992 | scheduleWriteUser(getUserDataNoChecks(userId)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 993 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 996 | final Bundle effective = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 997 | |
Makoto Onuki | 759a763 | 2015-10-28 16:43:10 -0700 | [diff] [blame] | 998 | mCachedEffectiveUserRestrictions.put(userId, effective); |
| 999 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1000 | // Apply the new restrictions. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1001 | if (DBG) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1002 | debug("Applying user restrictions: userId=" + userId |
| 1003 | + " new=" + effective + " prev=" + prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1006 | if (mAppOpsService != null) { // We skip it until system-ready. |
| 1007 | final long token = Binder.clearCallingIdentity(); |
| 1008 | try { |
| 1009 | mAppOpsService.setUserRestrictions(effective, userId); |
| 1010 | } catch (RemoteException e) { |
| 1011 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 1012 | } finally { |
| 1013 | Binder.restoreCallingIdentity(token); |
| 1014 | } |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1015 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1016 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1017 | propagateUserRestrictionsLR(userId, effective, prevAppliedRestrictions); |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1018 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1019 | mAppliedUserRestrictions.put(userId, new Bundle(effective)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1022 | private void propagateUserRestrictionsLR(final int userId, |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1023 | Bundle newRestrictions, Bundle prevRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1024 | // Note this method doesn't touch any state, meaning it doesn't require mRestrictionsLock |
| 1025 | // actually, but we still need some kind of synchronization otherwise we might end up |
| 1026 | // calling listeners out-of-order, thus "LR". |
| 1027 | |
| 1028 | if (UserRestrictionsUtils.areEqual(newRestrictions, prevRestrictions)) { |
| 1029 | return; |
| 1030 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1031 | |
| 1032 | final Bundle newRestrictionsFinal = new Bundle(newRestrictions); |
| 1033 | final Bundle prevRestrictionsFinal = new Bundle(prevRestrictions); |
| 1034 | |
| 1035 | mHandler.post(new Runnable() { |
| 1036 | @Override |
| 1037 | public void run() { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 1038 | UserRestrictionsUtils.applyUserRestrictions( |
| 1039 | mContext, userId, newRestrictionsFinal, prevRestrictionsFinal); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1040 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 1041 | final UserRestrictionsListener[] listeners; |
| 1042 | synchronized (mUserRestrictionsListeners) { |
| 1043 | listeners = new UserRestrictionsListener[mUserRestrictionsListeners.size()]; |
| 1044 | mUserRestrictionsListeners.toArray(listeners); |
| 1045 | } |
| 1046 | for (int i = 0; i < listeners.length; i++) { |
| 1047 | listeners[i].onUserRestrictionsChanged(userId, |
| 1048 | newRestrictionsFinal, prevRestrictionsFinal); |
| 1049 | } |
| 1050 | } |
| 1051 | }); |
| 1052 | } |
| 1053 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1054 | // Package private for the inner class. |
| 1055 | void applyUserRestrictionsLR(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1056 | updateUserRestrictionsInternalLR(null, userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | @GuardedBy("mRestrictionsLock") |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1060 | // Package private for the inner class. |
| 1061 | void applyUserRestrictionsForAllUsersLR() { |
| 1062 | if (DBG) { |
| 1063 | debug("applyUserRestrictionsForAllUsersLR"); |
| 1064 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1065 | // First, invalidate all cached values. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 1066 | mCachedEffectiveUserRestrictions.clear(); |
| 1067 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1068 | // We don't want to call into ActivityManagerNative while taking a lock, so we'll call |
| 1069 | // it on a handler. |
| 1070 | final Runnable r = new Runnable() { |
| 1071 | @Override |
| 1072 | public void run() { |
| 1073 | // Then get the list of running users. |
| 1074 | final int[] runningUsers; |
| 1075 | try { |
| 1076 | runningUsers = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 1077 | } catch (RemoteException e) { |
| 1078 | Log.w(LOG_TAG, "Unable to access ActivityManagerNative"); |
| 1079 | return; |
| 1080 | } |
| 1081 | // Then re-calculate the effective restrictions and apply, only for running users. |
| 1082 | // It's okay if a new user has started after the getRunningUserIds() call, |
| 1083 | // because we'll do the same thing (re-calculate the restrictions and apply) |
| 1084 | // when we start a user. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1085 | synchronized (mRestrictionsLock) { |
| 1086 | for (int i = 0; i < runningUsers.length; i++) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1087 | applyUserRestrictionsLR(runningUsers[i]); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | }; |
| 1092 | mHandler.post(r); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1095 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1096 | * Check if we've hit the limit of how many users can be created. |
| 1097 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1098 | private boolean isUserLimitReached() { |
| 1099 | int count; |
| 1100 | synchronized (mUsersLock) { |
| 1101 | count = getAliveUsersExcludingGuestsCountLU(); |
| 1102 | } |
| 1103 | return count >= UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | @Override |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1107 | public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1108 | checkManageUsersPermission("check if more managed profiles can be added."); |
| 1109 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1110 | return false; |
| 1111 | } |
Fyodor Kupolov | b615799 | 2015-06-05 15:32:28 -0700 | [diff] [blame] | 1112 | if (!mContext.getPackageManager().hasSystemFeature( |
| 1113 | PackageManager.FEATURE_MANAGED_USERS)) { |
| 1114 | return false; |
| 1115 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1116 | // Limit number of managed profiles that can be created |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1117 | final int managedProfilesCount = getProfiles(userId, true).size() - 1; |
| 1118 | final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0; |
| 1119 | if (managedProfilesCount - profilesRemovedCount >= MAX_MANAGED_PROFILES) { |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1120 | return false; |
| 1121 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1122 | synchronized(mUsersLock) { |
| 1123 | UserInfo userInfo = getUserInfoLU(userId); |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 1124 | if (!userInfo.canHaveProfile()) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1125 | return false; |
| 1126 | } |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1127 | int usersCountAfterRemoving = getAliveUsersExcludingGuestsCountLU() |
| 1128 | - profilesRemovedCount; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1129 | // We allow creating a managed profile in the special case where there is only one user. |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 1130 | return usersCountAfterRemoving == 1 |
| 1131 | || usersCountAfterRemoving < UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1135 | private int getAliveUsersExcludingGuestsCountLU() { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1136 | int aliveUserCount = 0; |
| 1137 | final int totalUserCount = mUsers.size(); |
| 1138 | // Skip over users being removed |
| 1139 | for (int i = 0; i < totalUserCount; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1140 | UserInfo user = mUsers.valueAt(i).info; |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1141 | if (!mRemovingUserIds.get(user.id) |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1142 | && !user.isGuest() && !user.partial) { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 1143 | aliveUserCount++; |
| 1144 | } |
| 1145 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1146 | return aliveUserCount; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1150 | * Enforces that only the system UID or root's UID or apps that have the |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1151 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} and |
| 1152 | * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL INTERACT_ACROSS_USERS_FULL} |
| 1153 | * permissions can make certain calls to the UserManager. |
| 1154 | * |
| 1155 | * @param message used as message if SecurityException is thrown |
| 1156 | * @throws SecurityException if the caller does not have enough privilege. |
| 1157 | */ |
| 1158 | private static final void checkManageUserAndAcrossUsersFullPermission(String message) { |
| 1159 | final int uid = Binder.getCallingUid(); |
| 1160 | if (uid != Process.SYSTEM_UID && uid != 0 |
| 1161 | && ActivityManager.checkComponentPermission( |
| 1162 | Manifest.permission.MANAGE_USERS, |
| 1163 | uid, -1, true) != PackageManager.PERMISSION_GRANTED |
| 1164 | && ActivityManager.checkComponentPermission( |
| 1165 | Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1166 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1167 | throw new SecurityException( |
| 1168 | "You need MANAGE_USERS and INTERACT_ACROSS_USERS_FULL permission to: " |
| 1169 | + message); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | /** |
| 1174 | * 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] | 1175 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 1176 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1177 | * |
| 1178 | * @param message used as message if SecurityException is thrown |
| 1179 | * @throws SecurityException if the caller is not system or root |
| 1180 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1181 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1182 | final int uid = Binder.getCallingUid(); |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1183 | if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 1184 | && ActivityManager.checkComponentPermission( |
| 1185 | android.Manifest.permission.MANAGE_USERS, |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 1186 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 1187 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 1188 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 1189 | } |
| 1190 | |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 1191 | /** |
| 1192 | * Enforces that only the system UID or root's UID (on any user) can make certain calls to the |
| 1193 | * UserManager. |
| 1194 | * |
| 1195 | * @param message used as message if SecurityException is thrown |
| 1196 | * @throws SecurityException if the caller is not system or root |
| 1197 | */ |
| 1198 | private static void checkSystemOrRoot(String message) { |
| 1199 | final int uid = Binder.getCallingUid(); |
| 1200 | if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID) { |
| 1201 | throw new SecurityException("Only system may: " + message); |
| 1202 | } |
| 1203 | } |
| 1204 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1205 | private void writeBitmapLP(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1206 | try { |
| 1207 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 1208 | File file = new File(dir, USER_PHOTO_FILENAME); |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1209 | File tmp = new File(dir, USER_PHOTO_FILENAME_TMP); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1210 | if (!dir.exists()) { |
| 1211 | dir.mkdir(); |
| 1212 | FileUtils.setPermissions( |
| 1213 | dir.getPath(), |
| 1214 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 1215 | -1, -1); |
| 1216 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1217 | FileOutputStream os; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1218 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp)) |
| 1219 | && tmp.renameTo(file)) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 1220 | info.iconPath = file.getAbsolutePath(); |
| 1221 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 1222 | try { |
| 1223 | os.close(); |
| 1224 | } catch (IOException ioe) { |
| 1225 | // What the ... ! |
| 1226 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 1227 | tmp.delete(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1228 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1229 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1230 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1233 | /** |
| 1234 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 1235 | * cache it elsewhere. |
| 1236 | * @return the array of user ids. |
| 1237 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 1238 | public int[] getUserIds() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1239 | synchronized (mUsersLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 1240 | return mUserIds; |
| 1241 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1244 | private void readUserListLP() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1245 | if (!mUserListFile.exists()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1246 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1250 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1251 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1252 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1253 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1254 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1255 | int type; |
| 1256 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1257 | && type != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1258 | // Skip |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1262 | Slog.e(LOG_TAG, "Unable to read user list"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1263 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1264 | return; |
| 1265 | } |
| 1266 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1267 | mNextSerialNumber = -1; |
| 1268 | if (parser.getName().equals(TAG_USERS)) { |
| 1269 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 1270 | if (lastSerialNumber != null) { |
| 1271 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 1272 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1273 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 1274 | if (versionNumber != null) { |
| 1275 | mUserVersion = Integer.parseInt(versionNumber); |
| 1276 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1279 | final Bundle newDevicePolicyGlobalUserRestrictions = new Bundle(); |
| 1280 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1281 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1282 | if (type == XmlPullParser.START_TAG) { |
| 1283 | final String name = parser.getName(); |
| 1284 | if (name.equals(TAG_USER)) { |
| 1285 | String id = parser.getAttributeValue(null, ATTR_ID); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1286 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1287 | UserData userData = readUserLP(Integer.parseInt(id)); |
| 1288 | |
| 1289 | if (userData != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1290 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1291 | mUsers.put(userData.info.id, userData); |
| 1292 | if (mNextSerialNumber < 0 |
| 1293 | || mNextSerialNumber <= userData.info.id) { |
| 1294 | mNextSerialNumber = userData.info.id + 1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1295 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1296 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1297 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1298 | } else if (name.equals(TAG_GUEST_RESTRICTIONS)) { |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1299 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1300 | && type != XmlPullParser.END_TAG) { |
| 1301 | if (type == XmlPullParser.START_TAG) { |
| 1302 | if (parser.getName().equals(TAG_RESTRICTIONS)) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1303 | synchronized (mGuestRestrictions) { |
| 1304 | UserRestrictionsUtils |
| 1305 | .readRestrictions(parser, mGuestRestrictions); |
| 1306 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1307 | } else if (parser.getName().equals(TAG_DEVICE_POLICY_RESTRICTIONS) |
| 1308 | ) { |
| 1309 | UserRestrictionsUtils.readRestrictions(parser, |
| 1310 | newDevicePolicyGlobalUserRestrictions); |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1311 | } |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1315 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1316 | } |
| 1317 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1318 | synchronized (mRestrictionsLock) { |
| 1319 | mDevicePolicyGlobalUserRestrictions = newDevicePolicyGlobalUserRestrictions; |
| 1320 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1321 | updateUserIds(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1322 | upgradeIfNecessaryLP(); |
| 1323 | } catch (IOException | XmlPullParserException e) { |
| 1324 | fallbackToSingleUserLP(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1325 | } finally { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1326 | IoUtils.closeQuietly(fis); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1330 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1331 | * 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] | 1332 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1333 | private void upgradeIfNecessaryLP() { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1334 | final int originalVersion = mUserVersion; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1335 | int userVersion = mUserVersion; |
| 1336 | if (userVersion < 1) { |
| 1337 | // Assign a proper name for the owner, if not initialized correctly before |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1338 | UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM); |
| 1339 | if ("Primary".equals(userData.info.name)) { |
| 1340 | userData.info.name = |
| 1341 | mContext.getResources().getString(com.android.internal.R.string.owner_name); |
| 1342 | scheduleWriteUser(userData); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1343 | } |
| 1344 | userVersion = 1; |
| 1345 | } |
| 1346 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1347 | if (userVersion < 2) { |
| 1348 | // Owner should be marked as initialized |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1349 | UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM); |
| 1350 | if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 1351 | userData.info.flags |= UserInfo.FLAG_INITIALIZED; |
| 1352 | scheduleWriteUser(userData); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1353 | } |
| 1354 | userVersion = 2; |
| 1355 | } |
| 1356 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1357 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1358 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1359 | userVersion = 4; |
| 1360 | } |
| 1361 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1362 | if (userVersion < 5) { |
| 1363 | initDefaultGuestRestrictions(); |
| 1364 | userVersion = 5; |
| 1365 | } |
| 1366 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1367 | if (userVersion < 6) { |
| 1368 | final boolean splitSystemUser = UserManager.isSplitSystemUser(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1369 | synchronized (mUsersLock) { |
| 1370 | for (int i = 0; i < mUsers.size(); i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1371 | UserData userData = mUsers.valueAt(i); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1372 | // In non-split mode, only user 0 can have restricted profiles |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1373 | if (!splitSystemUser && userData.info.isRestricted() |
| 1374 | && (userData.info.restrictedProfileParentId |
| 1375 | == UserInfo.NO_PROFILE_GROUP_ID)) { |
| 1376 | userData.info.restrictedProfileParentId = UserHandle.USER_SYSTEM; |
| 1377 | scheduleWriteUser(userData); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1378 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1379 | } |
| 1380 | } |
| 1381 | userVersion = 6; |
| 1382 | } |
| 1383 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1384 | if (userVersion < USER_VERSION) { |
| 1385 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 1386 | + USER_VERSION); |
| 1387 | } else { |
| 1388 | mUserVersion = userVersion; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1389 | |
| 1390 | if (originalVersion < mUserVersion) { |
| 1391 | writeUserListLP(); |
| 1392 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1396 | private void fallbackToSingleUserLP() { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1397 | int flags = UserInfo.FLAG_INITIALIZED; |
| 1398 | // In split system user mode, the admin and primary flags are assigned to the first human |
| 1399 | // user. |
| 1400 | if (!UserManager.isSplitSystemUser()) { |
| 1401 | flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY; |
| 1402 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1403 | // Create the system user |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1404 | UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1405 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1406 | flags); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1407 | UserData userData = new UserData(); |
| 1408 | userData.info = system; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1409 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1410 | mUsers.put(system.id, userData); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1411 | } |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1412 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 1413 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1414 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1415 | Bundle restrictions = new Bundle(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1416 | synchronized (mRestrictionsLock) { |
| 1417 | mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions); |
| 1418 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1419 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1420 | updateUserIds(); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1421 | initDefaultGuestRestrictions(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1422 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1423 | writeUserListLP(); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1424 | writeUserLP(userData); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1427 | private void scheduleWriteUser(UserData UserData) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1428 | if (DBG) { |
| 1429 | debug("scheduleWriteUser"); |
| 1430 | } |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1431 | // No need to wrap it within a lock -- worst case, we'll just post the same message |
| 1432 | // twice. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1433 | if (!mHandler.hasMessages(WRITE_USER_MSG, UserData)) { |
| 1434 | Message msg = mHandler.obtainMessage(WRITE_USER_MSG, UserData); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1435 | mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY); |
| 1436 | } |
| 1437 | } |
| 1438 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1439 | /* |
| 1440 | * Writes the user file in this format: |
| 1441 | * |
| 1442 | * <user flags="20039023" id="0"> |
| 1443 | * <name>Primary</name> |
| 1444 | * </user> |
| 1445 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1446 | private void writeUserLP(UserData userData) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1447 | if (DBG) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1448 | debug("writeUserLP " + userData); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1449 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1450 | FileOutputStream fos = null; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1451 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userData.info.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1452 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1453 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1454 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1455 | |
| 1456 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1457 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1458 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1459 | serializer.startDocument(null, true); |
| 1460 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1461 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1462 | final UserInfo userInfo = userData.info; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1463 | serializer.startTag(null, TAG_USER); |
| 1464 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1465 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1466 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1467 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 1468 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 1469 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1470 | if (userInfo.iconPath != null) { |
| 1471 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 1472 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1473 | if (userInfo.partial) { |
| 1474 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 1475 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1476 | if (userInfo.guestToRemove) { |
| 1477 | serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true"); |
| 1478 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1479 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1480 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 1481 | Integer.toString(userInfo.profileGroupId)); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1482 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1483 | if (userInfo.restrictedProfileParentId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1484 | serializer.attribute(null, ATTR_RESTRICTED_PROFILE_PARENT_ID, |
| 1485 | Integer.toString(userInfo.restrictedProfileParentId)); |
| 1486 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1487 | // Write seed data |
| 1488 | if (userData.persistSeedData) { |
| 1489 | if (userData.seedAccountName != null) { |
| 1490 | serializer.attribute(null, ATTR_SEED_ACCOUNT_NAME, userData.seedAccountName); |
| 1491 | } |
| 1492 | if (userData.seedAccountType != null) { |
| 1493 | serializer.attribute(null, ATTR_SEED_ACCOUNT_TYPE, userData.seedAccountType); |
| 1494 | } |
| 1495 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1496 | serializer.startTag(null, TAG_NAME); |
| 1497 | serializer.text(userInfo.name); |
| 1498 | serializer.endTag(null, TAG_NAME); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1499 | synchronized (mRestrictionsLock) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1500 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1501 | mBaseUserRestrictions.get(userInfo.id), TAG_RESTRICTIONS); |
| 1502 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1503 | mDevicePolicyLocalUserRestrictions.get(userInfo.id), |
| 1504 | TAG_DEVICE_POLICY_RESTRICTIONS); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1505 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1506 | |
| 1507 | if (userData.account != null) { |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1508 | serializer.startTag(null, TAG_ACCOUNT); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1509 | serializer.text(userData.account); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1510 | serializer.endTag(null, TAG_ACCOUNT); |
| 1511 | } |
| 1512 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1513 | if (userData.persistSeedData && userData.seedAccountOptions != null) { |
| 1514 | serializer.startTag(null, TAG_SEED_ACCOUNT_OPTIONS); |
| 1515 | userData.seedAccountOptions.saveToXml(serializer); |
| 1516 | serializer.endTag(null, TAG_SEED_ACCOUNT_OPTIONS); |
| 1517 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1518 | serializer.endTag(null, TAG_USER); |
| 1519 | |
| 1520 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1521 | userFile.finishWrite(fos); |
| 1522 | } catch (Exception ioe) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1523 | Slog.e(LOG_TAG, "Error writing user info " + userData.info.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1524 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * Writes the user list file in this format: |
| 1530 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1531 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1532 | * <user id="0"></user> |
| 1533 | * <user id="2"></user> |
| 1534 | * </users> |
| 1535 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1536 | private void writeUserListLP() { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1537 | if (DBG) { |
| 1538 | debug("writeUserList"); |
| 1539 | } |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1540 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1541 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1542 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1543 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1544 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1545 | |
| 1546 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1547 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1548 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1549 | serializer.startDocument(null, true); |
| 1550 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1551 | |
| 1552 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1553 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1554 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1555 | |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1556 | serializer.startTag(null, TAG_GUEST_RESTRICTIONS); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1557 | synchronized (mGuestRestrictions) { |
| 1558 | UserRestrictionsUtils |
| 1559 | .writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS); |
| 1560 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1561 | serializer.endTag(null, TAG_GUEST_RESTRICTIONS); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1562 | synchronized (mRestrictionsLock) { |
| 1563 | UserRestrictionsUtils.writeRestrictions(serializer, |
| 1564 | mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS); |
| 1565 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1566 | int[] userIdsToWrite; |
| 1567 | synchronized (mUsersLock) { |
| 1568 | userIdsToWrite = new int[mUsers.size()]; |
| 1569 | for (int i = 0; i < userIdsToWrite.length; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1570 | UserInfo user = mUsers.valueAt(i).info; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1571 | userIdsToWrite[i] = user.id; |
| 1572 | } |
| 1573 | } |
| 1574 | for (int id : userIdsToWrite) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1575 | serializer.startTag(null, TAG_USER); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1576 | serializer.attribute(null, ATTR_ID, Integer.toString(id)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1577 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | serializer.endTag(null, TAG_USERS); |
| 1581 | |
| 1582 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1583 | userListFile.finishWrite(fos); |
| 1584 | } catch (Exception e) { |
| 1585 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1586 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1590 | private UserData readUserLP(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1591 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1592 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1593 | String name = null; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1594 | String account = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1595 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1596 | long creationTime = 0L; |
| 1597 | long lastLoggedInTime = 0L; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1598 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1599 | int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1600 | boolean partial = false; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1601 | boolean guestToRemove = false; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1602 | boolean persistSeedData = false; |
| 1603 | String seedAccountName = null; |
| 1604 | String seedAccountType = null; |
| 1605 | PersistableBundle seedAccountOptions = null; |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1606 | Bundle baseRestrictions = new Bundle(); |
| 1607 | Bundle localRestrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1608 | |
| 1609 | FileInputStream fis = null; |
| 1610 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1611 | AtomicFile userFile = |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1612 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1613 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1614 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1615 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1616 | int type; |
| 1617 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1618 | && type != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1619 | // Skip |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1623 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1624 | return null; |
| 1625 | } |
| 1626 | |
| 1627 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1628 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 1629 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1630 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1631 | return null; |
| 1632 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1633 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 1634 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1635 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1636 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 1637 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1638 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 1639 | UserInfo.NO_PROFILE_GROUP_ID); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1640 | restrictedProfileParentId = readIntAttribute(parser, |
| 1641 | ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1642 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 1643 | if ("true".equals(valueString)) { |
| 1644 | partial = true; |
| 1645 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1646 | valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE); |
| 1647 | if ("true".equals(valueString)) { |
| 1648 | guestToRemove = true; |
| 1649 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1650 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1651 | seedAccountName = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_NAME); |
| 1652 | seedAccountType = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_TYPE); |
| 1653 | if (seedAccountName != null || seedAccountType != null) { |
| 1654 | persistSeedData = true; |
| 1655 | } |
| 1656 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1657 | int outerDepth = parser.getDepth(); |
| 1658 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1659 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1660 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1661 | continue; |
| 1662 | } |
| 1663 | String tag = parser.getName(); |
| 1664 | if (TAG_NAME.equals(tag)) { |
| 1665 | type = parser.next(); |
| 1666 | if (type == XmlPullParser.TEXT) { |
| 1667 | name = parser.getText(); |
| 1668 | } |
| 1669 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1670 | UserRestrictionsUtils.readRestrictions(parser, baseRestrictions); |
| 1671 | } else if (TAG_DEVICE_POLICY_RESTRICTIONS.equals(tag)) { |
| 1672 | UserRestrictionsUtils.readRestrictions(parser, localRestrictions); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 1673 | } else if (TAG_ACCOUNT.equals(tag)) { |
| 1674 | type = parser.next(); |
| 1675 | if (type == XmlPullParser.TEXT) { |
| 1676 | account = parser.getText(); |
| 1677 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1678 | } else if (TAG_SEED_ACCOUNT_OPTIONS.equals(tag)) { |
| 1679 | seedAccountOptions = PersistableBundle.restoreFromXml(parser); |
| 1680 | persistSeedData = true; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1684 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1685 | // Create the UserInfo object that gets passed around |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1686 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1687 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1688 | userInfo.creationTime = creationTime; |
| 1689 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1690 | userInfo.partial = partial; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1691 | userInfo.guestToRemove = guestToRemove; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1692 | userInfo.profileGroupId = profileGroupId; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1693 | userInfo.restrictedProfileParentId = restrictedProfileParentId; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1694 | |
| 1695 | // Create the UserData object that's internal to this class |
| 1696 | UserData userData = new UserData(); |
| 1697 | userData.info = userInfo; |
| 1698 | userData.account = account; |
| 1699 | userData.seedAccountName = seedAccountName; |
| 1700 | userData.seedAccountType = seedAccountType; |
| 1701 | userData.persistSeedData = persistSeedData; |
| 1702 | userData.seedAccountOptions = seedAccountOptions; |
| 1703 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1704 | synchronized (mRestrictionsLock) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 1705 | mBaseUserRestrictions.put(id, baseRestrictions); |
| 1706 | mDevicePolicyLocalUserRestrictions.put(id, localRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1707 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1708 | return userData; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1709 | } catch (IOException ioe) { |
| 1710 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1711 | } finally { |
| 1712 | if (fis != null) { |
| 1713 | try { |
| 1714 | fis.close(); |
| 1715 | } catch (IOException e) { |
| 1716 | } |
| 1717 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1718 | } |
| 1719 | return null; |
| 1720 | } |
| 1721 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1722 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 1723 | String valueString = parser.getAttributeValue(null, attr); |
| 1724 | if (valueString == null) return defaultValue; |
| 1725 | try { |
| 1726 | return Integer.parseInt(valueString); |
| 1727 | } catch (NumberFormatException nfe) { |
| 1728 | return defaultValue; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 1733 | String valueString = parser.getAttributeValue(null, attr); |
| 1734 | if (valueString == null) return defaultValue; |
| 1735 | try { |
| 1736 | return Long.parseLong(valueString); |
| 1737 | } catch (NumberFormatException nfe) { |
| 1738 | return defaultValue; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1742 | private boolean isPackageInstalled(String pkg, int userId) { |
| 1743 | final ApplicationInfo info = mPm.getApplicationInfo(pkg, |
| 1744 | PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1745 | userId); |
| 1746 | if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 1747 | return false; |
| 1748 | } |
| 1749 | return true; |
| 1750 | } |
| 1751 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1752 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1753 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 1754 | */ |
| 1755 | private void cleanAppRestrictionsForPackage(String pkg, int userId) { |
| 1756 | synchronized (mPackagesLock) { |
| 1757 | File dir = Environment.getUserSystemDirectory(userId); |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1758 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1759 | if (resFile.exists()) { |
| 1760 | resFile.delete(); |
| 1761 | } |
| 1762 | } |
| 1763 | } |
| 1764 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1765 | @Override |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1766 | public UserInfo createProfileForUser(String name, int flags, int userId) { |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1767 | checkManageUsersPermission("Only the system can create users"); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1768 | return createUserInternal(name, flags, userId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1771 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1772 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1773 | checkManageUsersPermission("Only the system can create users"); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1774 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1775 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1776 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1777 | private UserInfo createUserInternal(String name, int flags, int parentId) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1778 | if (hasUserRestriction(UserManager.DISALLOW_ADD_USER, UserHandle.getCallingUserId())) { |
Julia Reynolds | 7517502 | 2014-06-26 16:35:00 -0400 | [diff] [blame] | 1779 | Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled."); |
| 1780 | return null; |
| 1781 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1782 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1783 | return null; |
| 1784 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1785 | final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1786 | final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1787 | final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1788 | final long ident = Binder.clearCallingIdentity(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1789 | UserInfo userInfo; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1790 | UserData userData; |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1791 | final int userId; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1792 | try { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1793 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1794 | UserData parent = null; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1795 | if (parentId != UserHandle.USER_NULL) { |
| 1796 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1797 | parent = getUserDataLU(parentId); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1798 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1799 | if (parent == null) return null; |
| 1800 | } |
| 1801 | if (isManagedProfile && !canAddMoreManagedProfiles(parentId, false)) { |
| 1802 | Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId); |
| 1803 | return null; |
| 1804 | } |
| 1805 | if (!isGuest && !isManagedProfile && isUserLimitReached()) { |
| 1806 | // If we're not adding a guest user or a managed profile and the limit has |
| 1807 | // been reached, cannot add a user. |
| 1808 | return null; |
| 1809 | } |
| 1810 | // If we're adding a guest and there already exists one, bail. |
| 1811 | if (isGuest && findCurrentGuestUser() != null) { |
| 1812 | return null; |
| 1813 | } |
| 1814 | // In legacy mode, restricted profile's parent can only be the owner user |
| 1815 | if (isRestricted && !UserManager.isSplitSystemUser() |
| 1816 | && (parentId != UserHandle.USER_SYSTEM)) { |
| 1817 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be owner"); |
| 1818 | return null; |
| 1819 | } |
| 1820 | if (isRestricted && UserManager.isSplitSystemUser()) { |
| 1821 | if (parent == null) { |
| 1822 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be " |
| 1823 | + "specified"); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1824 | return null; |
| 1825 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1826 | if (!parent.info.canHaveProfile()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1827 | Log.w(LOG_TAG, "Cannot add restricted profile - profiles cannot be " |
| 1828 | + "created for the specified parent user id " + parentId); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1829 | return null; |
| 1830 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1831 | } |
| 1832 | // In split system user mode, we assign the first human user the primary flag. |
| 1833 | // And if there is no device owner, we also assign the admin flag to primary user. |
| 1834 | if (UserManager.isSplitSystemUser() |
| 1835 | && !isGuest && !isManagedProfile && getPrimaryUser() == null) { |
| 1836 | flags |= UserInfo.FLAG_PRIMARY; |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 1837 | synchronized (mUsersLock) { |
| 1838 | if (!mIsDeviceManaged) { |
| 1839 | flags |= UserInfo.FLAG_ADMIN; |
| 1840 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1841 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1842 | } |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1843 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1844 | if (parent != null && parent.info.isEphemeral()) { |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1845 | flags |= UserInfo.FLAG_EPHEMERAL; |
| 1846 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1847 | userId = getNextAvailableId(); |
| 1848 | userInfo = new UserInfo(userId, name, null, flags); |
| 1849 | userInfo.serialNumber = mNextSerialNumber++; |
| 1850 | long now = System.currentTimeMillis(); |
| 1851 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
| 1852 | userInfo.partial = true; |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1853 | userData = new UserData(); |
| 1854 | userData.info = userInfo; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1855 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
| 1856 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1857 | mUsers.put(userId, userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1858 | } |
| 1859 | writeUserListLP(); |
| 1860 | if (parent != null) { |
| 1861 | if (isManagedProfile) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1862 | if (parent.info.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1863 | parent.info.profileGroupId = parent.info.id; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1864 | writeUserLP(parent); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1865 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1866 | userInfo.profileGroupId = parent.info.profileGroupId; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1867 | } else if (isRestricted) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1868 | if (parent.info.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1869 | parent.info.restrictedProfileParentId = parent.info.id; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1870 | writeUserLP(parent); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1871 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1872 | userInfo.restrictedProfileParentId = parent.info.restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1873 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1874 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1875 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1876 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1877 | storage.createUserKey(userId, userInfo.serialNumber, userInfo.isEphemeral()); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1878 | for (VolumeInfo vol : storage.getWritablePrivateVolumes()) { |
| 1879 | final String volumeUuid = vol.getFsUuid(); |
| 1880 | try { |
| 1881 | final File userDir = Environment.getDataUserDirectory(volumeUuid, userId); |
Lenka Trochtova | c4dd021 | 2015-11-18 12:22:06 +0100 | [diff] [blame] | 1882 | storage.prepareUserStorage( |
| 1883 | volumeUuid, userId, userInfo.serialNumber, userInfo.isEphemeral()); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1884 | enforceSerialNumber(userDir, userInfo.serialNumber); |
| 1885 | } catch (IOException e) { |
| 1886 | Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e); |
| 1887 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1888 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1889 | mPm.createNewUser(userId); |
| 1890 | userInfo.partial = false; |
| 1891 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1892 | writeUserLP(userData); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1893 | } |
| 1894 | updateUserIds(); |
| 1895 | Bundle restrictions = new Bundle(); |
Fyodor Kupolov | e04462c | 2015-11-30 15:02:53 -0800 | [diff] [blame] | 1896 | if (isGuest) { |
| 1897 | synchronized (mGuestRestrictions) { |
| 1898 | restrictions.putAll(mGuestRestrictions); |
| 1899 | } |
| 1900 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 1901 | synchronized (mRestrictionsLock) { |
| 1902 | mBaseUserRestrictions.append(userId, restrictions); |
| 1903 | } |
| 1904 | mPm.newUserCreated(userId); |
| 1905 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 1906 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1907 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1908 | android.Manifest.permission.MANAGE_USERS); |
Fyodor Kupolov | 8385e4b | 2015-12-29 18:15:32 -0800 | [diff] [blame] | 1909 | MetricsLogger.count(mContext, isGuest ? TRON_GUEST_CREATED : TRON_USER_CREATED, 1); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1910 | } finally { |
| 1911 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1912 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1913 | return userInfo; |
| 1914 | } |
| 1915 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1916 | /** |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1917 | * @hide |
| 1918 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1919 | @Override |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1920 | public UserInfo createRestrictedProfile(String name, int parentUserId) { |
| 1921 | checkManageUsersPermission("setupRestrictedProfile"); |
| 1922 | final UserInfo user = createProfileForUser(name, UserInfo.FLAG_RESTRICTED, parentUserId); |
| 1923 | if (user == null) { |
| 1924 | return null; |
| 1925 | } |
| 1926 | setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user.id); |
| 1927 | // Change the setting before applying the DISALLOW_SHARE_LOCATION restriction, otherwise |
| 1928 | // the putIntForUser() will fail. |
| 1929 | android.provider.Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1930 | android.provider.Settings.Secure.LOCATION_MODE, |
| 1931 | android.provider.Settings.Secure.LOCATION_MODE_OFF, user.id); |
| 1932 | setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user.id); |
| 1933 | return user; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1937 | * Find the current guest user. If the Guest user is partial, |
| 1938 | * 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] | 1939 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1940 | private UserInfo findCurrentGuestUser() { |
| 1941 | synchronized (mUsersLock) { |
| 1942 | final int size = mUsers.size(); |
| 1943 | for (int i = 0; i < size; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1944 | final UserInfo user = mUsers.valueAt(i).info; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1945 | if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) { |
| 1946 | return user; |
| 1947 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | return null; |
| 1951 | } |
| 1952 | |
| 1953 | /** |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1954 | * Mark this guest user for deletion to allow us to create another guest |
| 1955 | * and switch to that user before actually removing this guest. |
| 1956 | * @param userHandle the userid of the current guest |
| 1957 | * @return whether the user could be marked for deletion |
| 1958 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1959 | @Override |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1960 | public boolean markGuestForDeletion(int userHandle) { |
| 1961 | checkManageUsersPermission("Only the system can remove users"); |
| 1962 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1963 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1964 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1965 | return false; |
| 1966 | } |
| 1967 | |
| 1968 | long ident = Binder.clearCallingIdentity(); |
| 1969 | try { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1970 | final UserData userData; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1971 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1972 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1973 | userData = mUsers.get(userHandle); |
| 1974 | if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1975 | return false; |
| 1976 | } |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1977 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1978 | if (!userData.info.isGuest()) { |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1979 | return false; |
| 1980 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1981 | // We set this to a guest user that is to be removed. This is a temporary state |
| 1982 | // where we are allowed to add new Guest users, even if this one is still not |
| 1983 | // removed. This user will still show up in getUserInfo() calls. |
| 1984 | // If we don't get around to removing this Guest user, it will be purged on next |
| 1985 | // startup. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1986 | userData.info.guestToRemove = true; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1987 | // Mark it as disabled, so that it isn't returned any more when |
| 1988 | // profiles are queried. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 1989 | userData.info.flags |= UserInfo.FLAG_DISABLED; |
| 1990 | writeUserLP(userData); |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1991 | } |
| 1992 | } finally { |
| 1993 | Binder.restoreCallingIdentity(ident); |
| 1994 | } |
| 1995 | return true; |
| 1996 | } |
| 1997 | |
| 1998 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1999 | * Removes a user and all data directories created for that user. This method should be called |
| 2000 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 2001 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2002 | */ |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2003 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 2004 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2005 | checkManageUsersPermission("Only the system can remove users"); |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 2006 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 2007 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 2008 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 2009 | return false; |
| 2010 | } |
| 2011 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2012 | long ident = Binder.clearCallingIdentity(); |
| 2013 | try { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2014 | final UserData userData; |
Fyodor Kupolov | 0df68cd | 2015-10-01 13:54:22 -0700 | [diff] [blame] | 2015 | int currentUser = ActivityManager.getCurrentUser(); |
| 2016 | if (currentUser == userHandle) { |
| 2017 | Log.w(LOG_TAG, "Current user cannot be removed"); |
| 2018 | return false; |
| 2019 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2020 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2021 | synchronized (mUsersLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2022 | userData = mUsers.get(userHandle); |
| 2023 | if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2024 | return false; |
| 2025 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 2026 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2027 | // We remember deleted user IDs to prevent them from being |
| 2028 | // reused during the current boot; they can still be reused |
| 2029 | // after a reboot. |
| 2030 | mRemovingUserIds.put(userHandle, true); |
| 2031 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 2032 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2033 | try { |
| 2034 | mAppOpsService.removeUser(userHandle); |
| 2035 | } catch (RemoteException e) { |
| 2036 | Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e); |
| 2037 | } |
| 2038 | // Set this to a partially created user, so that the user will be purged |
| 2039 | // on next startup, in case the runtime stops now before stopping and |
| 2040 | // removing the user completely. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2041 | userData.info.partial = true; |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2042 | // Mark it as disabled, so that it isn't returned any more when |
| 2043 | // profiles are queried. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2044 | userData.info.flags |= UserInfo.FLAG_DISABLED; |
| 2045 | writeUserLP(userData); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2046 | } |
| 2047 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2048 | if (userData.info.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 2049 | && userData.info.isManagedProfile()) { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2050 | // Send broadcast to notify system that the user removed was a |
| 2051 | // managed user. |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2052 | sendProfileRemovedBroadcast(userData.info.profileGroupId, userData.info.id); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
| 2056 | int res; |
| 2057 | try { |
Fyodor Kupolov | 9cbfc9e | 2015-10-07 15:52:33 -0700 | [diff] [blame] | 2058 | res = ActivityManagerNative.getDefault().stopUser(userHandle, /* force= */ true, |
| 2059 | new IStopUserCallback.Stub() { |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2060 | @Override |
| 2061 | public void userStopped(int userId) { |
| 2062 | finishRemoveUser(userId); |
| 2063 | } |
| 2064 | @Override |
| 2065 | public void userStopAborted(int userId) { |
| 2066 | } |
| 2067 | }); |
| 2068 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2069 | return false; |
| 2070 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 2071 | return res == ActivityManager.USER_OP_SUCCESS; |
| 2072 | } finally { |
| 2073 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2074 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2077 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 2078 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2079 | // Let other services shutdown any activity and clean up their state before completely |
| 2080 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2081 | long ident = Binder.clearCallingIdentity(); |
| 2082 | try { |
| 2083 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 2084 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2085 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 2086 | android.Manifest.permission.MANAGE_USERS, |
| 2087 | |
| 2088 | new BroadcastReceiver() { |
| 2089 | @Override |
| 2090 | public void onReceive(Context context, Intent intent) { |
| 2091 | if (DBG) { |
| 2092 | Slog.i(LOG_TAG, |
| 2093 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 2094 | + userHandle); |
| 2095 | } |
| 2096 | new Thread() { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2097 | @Override |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2098 | public void run() { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 2099 | // Clean up any ActivityManager state |
| 2100 | LocalServices.getService(ActivityManagerInternal.class) |
| 2101 | .onUserRemoved(userHandle); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2102 | removeUserState(userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 2103 | } |
| 2104 | }.start(); |
| 2105 | } |
| 2106 | }, |
| 2107 | |
| 2108 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 2109 | } finally { |
| 2110 | Binder.restoreCallingIdentity(ident); |
| 2111 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2112 | } |
| 2113 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2114 | private void removeUserState(final int userHandle) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 2115 | mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2116 | // Cleanup package manager settings |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2117 | mPm.cleanUpUser(this, userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2118 | |
| 2119 | // Remove this user from the list |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2120 | synchronized (mUsersLock) { |
| 2121 | mUsers.remove(userHandle); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2122 | mIsUserManaged.delete(userHandle); |
| 2123 | } |
| 2124 | synchronized (mRestrictionsLock) { |
| 2125 | mBaseUserRestrictions.remove(userHandle); |
| 2126 | mAppliedUserRestrictions.remove(userHandle); |
| 2127 | mCachedEffectiveUserRestrictions.remove(userHandle); |
| 2128 | mDevicePolicyLocalUserRestrictions.remove(userHandle); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2129 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2130 | // Remove user file |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2131 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2132 | userFile.delete(); |
| 2133 | // Update the user list |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2134 | synchronized (mPackagesLock) { |
| 2135 | writeUserListLP(); |
| 2136 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2137 | updateUserIds(); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2138 | File userDir = Environment.getUserSystemDirectory(userHandle); |
| 2139 | File renamedUserDir = Environment.getUserSystemDirectory(UserHandle.USER_NULL - userHandle); |
| 2140 | if (userDir.renameTo(renamedUserDir)) { |
| 2141 | removeDirectoryRecursive(renamedUserDir); |
| 2142 | } else { |
| 2143 | removeDirectoryRecursive(userDir); |
| 2144 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 2147 | private void removeDirectoryRecursive(File parent) { |
| 2148 | if (parent.isDirectory()) { |
| 2149 | String[] files = parent.list(); |
| 2150 | for (String filename : files) { |
| 2151 | File child = new File(parent, filename); |
| 2152 | removeDirectoryRecursive(child); |
| 2153 | } |
| 2154 | } |
| 2155 | parent.delete(); |
| 2156 | } |
| 2157 | |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2158 | private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2159 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Adam Connors | d4b584e | 2014-06-09 13:55:47 +0100 | [diff] [blame] | 2160 | managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 2161 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2162 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); |
Jeff Sharkey | af6ec29 | 2015-12-17 11:19:00 -0700 | [diff] [blame] | 2163 | managedProfileIntent.putExtra(Intent.EXTRA_USER_HANDLE, removedUserId); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 2164 | mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null); |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2167 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2168 | public Bundle getApplicationRestrictions(String packageName) { |
| 2169 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 2170 | } |
| 2171 | |
| 2172 | @Override |
| 2173 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2174 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2175 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 2176 | checkSystemOrRoot("get application restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2177 | } |
| 2178 | synchronized (mPackagesLock) { |
| 2179 | // Read the restrictions from XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2180 | return readApplicationRestrictionsLP(packageName, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2185 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2186 | int userId) { |
Esteban Talavera | 5b9f167 | 2015-12-11 15:22:34 +0000 | [diff] [blame] | 2187 | checkSystemOrRoot("set application restrictions"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2188 | synchronized (mPackagesLock) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2189 | if (restrictions == null || restrictions.isEmpty()) { |
| 2190 | cleanAppRestrictionsForPackage(packageName, userId); |
| 2191 | } else { |
| 2192 | // Write the restrictions to XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2193 | writeApplicationRestrictionsLP(packageName, restrictions, userId); |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2194 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2195 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 2196 | |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 2197 | if (isPackageInstalled(packageName, userId)) { |
| 2198 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 2199 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 2200 | changeIntent.setPackage(packageName); |
| 2201 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 2202 | mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId)); |
| 2203 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2207 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2208 | try { |
| 2209 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 2210 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 2211 | } catch (NameNotFoundException nnfe) { |
| 2212 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 2213 | } finally { |
| 2214 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2218 | private Bundle readApplicationRestrictionsLP(String packageName, int userId) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2219 | AtomicFile restrictionsFile = |
| 2220 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 2221 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2222 | return readApplicationRestrictionsLP(restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2226 | static Bundle readApplicationRestrictionsLP(AtomicFile restrictionsFile) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2227 | final Bundle restrictions = new Bundle(); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2228 | final ArrayList<String> values = new ArrayList<>(); |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2229 | if (!restrictionsFile.getBaseFile().exists()) { |
Fyodor Kupolov | f6ee224 | 2015-04-06 10:15:07 -0700 | [diff] [blame] | 2230 | return restrictions; |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 2231 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2232 | |
| 2233 | FileInputStream fis = null; |
| 2234 | try { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2235 | fis = restrictionsFile.openRead(); |
| 2236 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2237 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2238 | XmlUtils.nextElement(parser); |
| 2239 | if (parser.getEventType() != XmlPullParser.START_TAG) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2240 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 2241 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2242 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2243 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2244 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 2245 | readEntry(restrictions, values, parser); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2246 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2247 | } catch (IOException|XmlPullParserException e) { |
| 2248 | Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2249 | } finally { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2250 | IoUtils.closeQuietly(fis); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2251 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2252 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2253 | } |
| 2254 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2255 | private static void readEntry(Bundle restrictions, ArrayList<String> values, |
| 2256 | XmlPullParser parser) throws XmlPullParserException, IOException { |
| 2257 | int type = parser.getEventType(); |
| 2258 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 2259 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 2260 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
| 2261 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 2262 | if (multiple != null) { |
| 2263 | values.clear(); |
| 2264 | int count = Integer.parseInt(multiple); |
| 2265 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 2266 | if (type == XmlPullParser.START_TAG |
| 2267 | && parser.getName().equals(TAG_VALUE)) { |
| 2268 | values.add(parser.nextText().trim()); |
| 2269 | count--; |
| 2270 | } |
| 2271 | } |
| 2272 | String [] valueStrings = new String[values.size()]; |
| 2273 | values.toArray(valueStrings); |
| 2274 | restrictions.putStringArray(key, valueStrings); |
| 2275 | } else if (ATTR_TYPE_BUNDLE.equals(valType)) { |
| 2276 | restrictions.putBundle(key, readBundleEntry(parser, values)); |
| 2277 | } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) { |
| 2278 | final int outerDepth = parser.getDepth(); |
| 2279 | ArrayList<Bundle> bundleList = new ArrayList<>(); |
| 2280 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2281 | Bundle childBundle = readBundleEntry(parser, values); |
| 2282 | bundleList.add(childBundle); |
| 2283 | } |
| 2284 | restrictions.putParcelableArray(key, |
| 2285 | bundleList.toArray(new Bundle[bundleList.size()])); |
| 2286 | } else { |
| 2287 | String value = parser.nextText().trim(); |
| 2288 | if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 2289 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 2290 | } else if (ATTR_TYPE_INTEGER.equals(valType)) { |
| 2291 | restrictions.putInt(key, Integer.parseInt(value)); |
| 2292 | } else { |
| 2293 | restrictions.putString(key, value); |
| 2294 | } |
| 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values) |
| 2300 | throws IOException, XmlPullParserException { |
| 2301 | Bundle childBundle = new Bundle(); |
| 2302 | final int outerDepth = parser.getDepth(); |
| 2303 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 2304 | readEntry(childBundle, values, parser); |
| 2305 | } |
| 2306 | return childBundle; |
| 2307 | } |
| 2308 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2309 | private void writeApplicationRestrictionsLP(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 2310 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2311 | AtomicFile restrictionsFile = new AtomicFile( |
| 2312 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2313 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2314 | writeApplicationRestrictionsLP(restrictions, restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2318 | static void writeApplicationRestrictionsLP(Bundle restrictions, AtomicFile restrictionsFile) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2319 | FileOutputStream fos = null; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2320 | try { |
| 2321 | fos = restrictionsFile.startWrite(); |
| 2322 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 2323 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2324 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 2325 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2326 | serializer.startDocument(null, true); |
| 2327 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 2328 | |
| 2329 | serializer.startTag(null, TAG_RESTRICTIONS); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2330 | writeBundle(restrictions, serializer); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2331 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 2332 | |
| 2333 | serializer.endDocument(); |
| 2334 | restrictionsFile.finishWrite(fos); |
| 2335 | } catch (Exception e) { |
| 2336 | restrictionsFile.failWrite(fos); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2337 | Slog.e(LOG_TAG, "Error writing application restrictions list", e); |
| 2338 | } |
| 2339 | } |
| 2340 | |
| 2341 | private static void writeBundle(Bundle restrictions, XmlSerializer serializer) |
| 2342 | throws IOException { |
| 2343 | for (String key : restrictions.keySet()) { |
| 2344 | Object value = restrictions.get(key); |
| 2345 | serializer.startTag(null, TAG_ENTRY); |
| 2346 | serializer.attribute(null, ATTR_KEY, key); |
| 2347 | |
| 2348 | if (value instanceof Boolean) { |
| 2349 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 2350 | serializer.text(value.toString()); |
| 2351 | } else if (value instanceof Integer) { |
| 2352 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER); |
| 2353 | serializer.text(value.toString()); |
| 2354 | } else if (value == null || value instanceof String) { |
| 2355 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 2356 | serializer.text(value != null ? (String) value : ""); |
| 2357 | } else if (value instanceof Bundle) { |
| 2358 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2359 | writeBundle((Bundle) value, serializer); |
| 2360 | } else if (value instanceof Parcelable[]) { |
| 2361 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY); |
| 2362 | Parcelable[] array = (Parcelable[]) value; |
| 2363 | for (Parcelable parcelable : array) { |
| 2364 | if (!(parcelable instanceof Bundle)) { |
| 2365 | throw new IllegalArgumentException("bundle-array can only hold Bundles"); |
| 2366 | } |
| 2367 | serializer.startTag(null, TAG_ENTRY); |
| 2368 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2369 | writeBundle((Bundle) parcelable, serializer); |
| 2370 | serializer.endTag(null, TAG_ENTRY); |
| 2371 | } |
| 2372 | } else { |
| 2373 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 2374 | String[] values = (String[]) value; |
| 2375 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
| 2376 | for (String choice : values) { |
| 2377 | serializer.startTag(null, TAG_VALUE); |
| 2378 | serializer.text(choice != null ? choice : ""); |
| 2379 | serializer.endTag(null, TAG_VALUE); |
| 2380 | } |
| 2381 | } |
| 2382 | serializer.endTag(null, TAG_ENTRY); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2387 | public int getUserSerialNumber(int userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2388 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2389 | if (!exists(userHandle)) return -1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2390 | return getUserInfoLU(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | @Override |
| 2395 | public int getUserHandle(int userSerialNumber) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2396 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2397 | for (int userId : mUserIds) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2398 | UserInfo info = getUserInfoLU(userId); |
Kenny Guy | 945f883 | 2015-02-10 15:17:26 +0000 | [diff] [blame] | 2399 | if (info != null && info.serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2400 | } |
| 2401 | // Not found |
| 2402 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2403 | } |
| 2404 | } |
| 2405 | |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2406 | @Override |
| 2407 | public long getUserCreationTime(int userHandle) { |
| 2408 | int callingUserId = UserHandle.getCallingUserId(); |
| 2409 | UserInfo userInfo = null; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2410 | synchronized (mUsersLock) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2411 | if (callingUserId == userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2412 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2413 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2414 | UserInfo parent = getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2415 | if (parent != null && parent.id == callingUserId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2416 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | if (userInfo == null) { |
| 2421 | throw new SecurityException("userHandle can only be the calling user or a managed " |
| 2422 | + "profile associated with this user"); |
| 2423 | } |
| 2424 | return userInfo.creationTime; |
| 2425 | } |
| 2426 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2427 | /** |
| 2428 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 2429 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2430 | private void updateUserIds() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2431 | int num = 0; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2432 | synchronized (mUsersLock) { |
| 2433 | final int userSize = mUsers.size(); |
| 2434 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2435 | if (!mUsers.valueAt(i).info.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2436 | num++; |
| 2437 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2438 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2439 | final int[] newUsers = new int[num]; |
| 2440 | int n = 0; |
| 2441 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2442 | if (!mUsers.valueAt(i).info.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2443 | newUsers[n++] = mUsers.keyAt(i); |
| 2444 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2445 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2446 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | /** |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 2451 | * Called right before a user is started. This gives us a chance to prepare |
| 2452 | * app storage and apply any user restrictions. |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2453 | */ |
| 2454 | public void onBeforeStartUser(int userId) { |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 2455 | mPm.reconcileAppsData(userId, Installer.FLAG_DE_STORAGE); |
| 2456 | |
| 2457 | if (userId != UserHandle.USER_SYSTEM) { |
| 2458 | synchronized (mRestrictionsLock) { |
| 2459 | applyUserRestrictionsLR(userId); |
| 2460 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | /** |
Jeff Sharkey | 0e62384c | 2016-01-13 18:52:55 -0700 | [diff] [blame] | 2465 | * Called right before a user is unlocked. This gives us a chance to prepare |
| 2466 | * app storage. |
| 2467 | */ |
| 2468 | public void onBeforeUnlockUser(int userId) { |
| 2469 | mPm.reconcileAppsData(userId, Installer.FLAG_CE_STORAGE); |
| 2470 | } |
| 2471 | |
| 2472 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2473 | * 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] | 2474 | * @param userId the user that was just foregrounded |
| 2475 | */ |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 2476 | public void onUserForeground(int userId) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2477 | UserData userData = getUserDataNoChecks(userId); |
| 2478 | if (userData == null || userData.info.partial) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2479 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 2480 | return; |
| 2481 | } |
| 2482 | long now = System.currentTimeMillis(); |
| 2483 | if (now > EPOCH_PLUS_30_YEARS) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2484 | userData.info.lastLoggedInTime = now; |
| 2485 | scheduleWriteUser(userData); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2490 | * 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] | 2491 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 2492 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2493 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2494 | private int getNextAvailableId() { |
| 2495 | synchronized (mUsersLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2496 | int i = MIN_USER_ID; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2497 | while (i < MAX_USER_ID) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2498 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2499 | return i; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 2500 | } |
| 2501 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2502 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2503 | } |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2504 | throw new IllegalStateException("No user id available!"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2505 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2506 | |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2507 | private String packageToRestrictionsFileName(String packageName) { |
| 2508 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 2509 | } |
| 2510 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2511 | /** |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2512 | * Enforce that serial number stored in user directory inode matches the |
| 2513 | * given expected value. Gracefully sets the serial number if currently |
| 2514 | * undefined. |
| 2515 | * |
| 2516 | * @throws IOException when problem extracting serial number, or serial |
| 2517 | * number is mismatched. |
| 2518 | */ |
| 2519 | public static void enforceSerialNumber(File file, int serialNumber) throws IOException { |
| 2520 | final int foundSerial = getSerialNumber(file); |
| 2521 | Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial); |
| 2522 | |
| 2523 | if (foundSerial == -1) { |
| 2524 | Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid"); |
| 2525 | try { |
| 2526 | setSerialNumber(file, serialNumber); |
| 2527 | } catch (IOException e) { |
| 2528 | Slog.w(LOG_TAG, "Failed to set serial number on " + file, e); |
| 2529 | } |
| 2530 | |
| 2531 | } else if (foundSerial != serialNumber) { |
| 2532 | throw new IOException("Found serial number " + foundSerial |
| 2533 | + " doesn't match expected " + serialNumber); |
| 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | /** |
| 2538 | * Set serial number stored in user directory inode. |
| 2539 | * |
| 2540 | * @throws IOException if serial number was already set |
| 2541 | */ |
| 2542 | private static void setSerialNumber(File file, int serialNumber) |
| 2543 | throws IOException { |
| 2544 | try { |
| 2545 | final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8); |
| 2546 | Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE); |
| 2547 | } catch (ErrnoException e) { |
| 2548 | throw e.rethrowAsIOException(); |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | /** |
| 2553 | * Return serial number stored in user directory inode. |
| 2554 | * |
| 2555 | * @return parsed serial number, or -1 if not set |
| 2556 | */ |
| 2557 | private static int getSerialNumber(File file) throws IOException { |
| 2558 | try { |
| 2559 | final byte[] buf = new byte[256]; |
| 2560 | final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf); |
| 2561 | final String serial = new String(buf, 0, len); |
| 2562 | try { |
| 2563 | return Integer.parseInt(serial); |
| 2564 | } catch (NumberFormatException e) { |
| 2565 | throw new IOException("Bad serial number: " + serial); |
| 2566 | } |
| 2567 | } catch (ErrnoException e) { |
| 2568 | if (e.errno == OsConstants.ENODATA) { |
| 2569 | return -1; |
| 2570 | } else { |
| 2571 | throw e.rethrowAsIOException(); |
| 2572 | } |
| 2573 | } |
| 2574 | } |
| 2575 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2576 | @Override |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2577 | public void setSeedAccountData(int userId, String accountName, String accountType, |
| 2578 | PersistableBundle accountOptions, boolean persist) { |
| 2579 | checkManageUsersPermission("Require MANAGE_USERS permission to set user seed data"); |
| 2580 | synchronized (mPackagesLock) { |
| 2581 | final UserData userData; |
| 2582 | synchronized (mUsersLock) { |
| 2583 | userData = getUserDataLU(userId); |
| 2584 | if (userData == null) { |
| 2585 | Slog.e(LOG_TAG, "No such user for settings seed data u=" + userId); |
| 2586 | return; |
| 2587 | } |
| 2588 | userData.seedAccountName = accountName; |
| 2589 | userData.seedAccountType = accountType; |
| 2590 | userData.seedAccountOptions = accountOptions; |
| 2591 | userData.persistSeedData = persist; |
| 2592 | } |
| 2593 | if (persist) { |
| 2594 | writeUserLP(userData); |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | |
| 2599 | @Override |
| 2600 | public String getSeedAccountName() throws RemoteException { |
| 2601 | checkManageUsersPermission("Cannot get seed account information"); |
| 2602 | synchronized (mUsersLock) { |
| 2603 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 2604 | return userData.seedAccountName; |
| 2605 | } |
| 2606 | } |
| 2607 | |
| 2608 | @Override |
| 2609 | public String getSeedAccountType() throws RemoteException { |
| 2610 | checkManageUsersPermission("Cannot get seed account information"); |
| 2611 | synchronized (mUsersLock) { |
| 2612 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 2613 | return userData.seedAccountType; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | @Override |
| 2618 | public PersistableBundle getSeedAccountOptions() throws RemoteException { |
| 2619 | checkManageUsersPermission("Cannot get seed account information"); |
| 2620 | synchronized (mUsersLock) { |
| 2621 | UserData userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 2622 | return userData.seedAccountOptions; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | @Override |
| 2627 | public void clearSeedAccountData() throws RemoteException { |
| 2628 | checkManageUsersPermission("Cannot clear seed account information"); |
| 2629 | synchronized (mPackagesLock) { |
| 2630 | UserData userData; |
| 2631 | synchronized (mUsersLock) { |
| 2632 | userData = getUserDataLU(UserHandle.getCallingUserId()); |
| 2633 | if (userData == null) return; |
| 2634 | userData.clearSeedAccountData(); |
| 2635 | } |
| 2636 | writeUserLP(userData); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | @Override |
| 2641 | public boolean someUserHasSeedAccount(String accountName, String accountType) |
| 2642 | throws RemoteException { |
| 2643 | checkManageUsersPermission("Cannot check seed account information"); |
| 2644 | synchronized (mUsersLock) { |
| 2645 | final int userSize = mUsers.size(); |
| 2646 | for (int i = 0; i < userSize; i++) { |
| 2647 | final UserData data = mUsers.valueAt(i); |
| 2648 | if (data.info.isInitialized()) continue; |
| 2649 | if (data.seedAccountName == null || !data.seedAccountName.equals(accountName)) { |
| 2650 | continue; |
| 2651 | } |
| 2652 | if (data.seedAccountType == null || !data.seedAccountType.equals(accountType)) { |
| 2653 | continue; |
| 2654 | } |
| 2655 | return true; |
| 2656 | } |
| 2657 | } |
| 2658 | return false; |
| 2659 | } |
| 2660 | |
| 2661 | @Override |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2662 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 2663 | FileDescriptor err, String[] args, ResultReceiver resultReceiver) { |
| 2664 | (new Shell()).exec(this, in, out, err, args, resultReceiver); |
| 2665 | } |
| 2666 | |
| 2667 | int onShellCommand(Shell shell, String cmd) { |
| 2668 | if (cmd == null) { |
| 2669 | return shell.handleDefaultCommands(cmd); |
| 2670 | } |
| 2671 | |
| 2672 | final PrintWriter pw = shell.getOutPrintWriter(); |
| 2673 | try { |
| 2674 | switch(cmd) { |
| 2675 | case "list": |
| 2676 | return runList(pw); |
| 2677 | } |
| 2678 | } catch (RemoteException e) { |
| 2679 | pw.println("Remote exception: " + e); |
| 2680 | } |
| 2681 | return -1; |
| 2682 | } |
| 2683 | |
| 2684 | private int runList(PrintWriter pw) throws RemoteException { |
| 2685 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 2686 | final List<UserInfo> users = getUsers(false); |
| 2687 | if (users == null) { |
| 2688 | pw.println("Error: couldn't get users"); |
| 2689 | return 1; |
| 2690 | } else { |
| 2691 | pw.println("Users:"); |
| 2692 | for (int i = 0; i < users.size(); i++) { |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 2693 | String running = am.isUserRunning(users.get(i).id, 0) ? " running" : ""; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2694 | pw.println("\t" + users.get(i).toString() + running); |
| 2695 | } |
| 2696 | return 0; |
| 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2701 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2702 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2703 | != PackageManager.PERMISSION_GRANTED) { |
| 2704 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 2705 | + Binder.getCallingPid() |
| 2706 | + ", uid=" + Binder.getCallingUid() |
| 2707 | + " without permission " |
| 2708 | + android.Manifest.permission.DUMP); |
| 2709 | return; |
| 2710 | } |
| 2711 | |
| 2712 | long now = System.currentTimeMillis(); |
| 2713 | StringBuilder sb = new StringBuilder(); |
| 2714 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2715 | synchronized (mUsersLock) { |
| 2716 | pw.println("Users:"); |
| 2717 | for (int i = 0; i < mUsers.size(); i++) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2718 | UserData userData = mUsers.valueAt(i); |
| 2719 | if (userData == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2720 | continue; |
| 2721 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2722 | UserInfo userInfo = userData.info; |
| 2723 | final int userId = userInfo.id; |
| 2724 | pw.print(" "); pw.print(userInfo); |
| 2725 | pw.print(" serialNo="); pw.print(userInfo.serialNumber); |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2726 | if (mRemovingUserIds.get(userId)) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2727 | pw.print(" <removing> "); |
| 2728 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2729 | if (userInfo.partial) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2730 | pw.print(" <partial>"); |
| 2731 | } |
| 2732 | pw.println(); |
| 2733 | pw.print(" Created: "); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2734 | if (userInfo.creationTime == 0) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2735 | pw.println("<unknown>"); |
| 2736 | } else { |
| 2737 | sb.setLength(0); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2738 | TimeUtils.formatDuration(now - userInfo.creationTime, sb); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2739 | sb.append(" ago"); |
| 2740 | pw.println(sb); |
| 2741 | } |
| 2742 | pw.print(" Last logged in: "); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2743 | if (userInfo.lastLoggedInTime == 0) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2744 | pw.println("<unknown>"); |
| 2745 | } else { |
| 2746 | sb.setLength(0); |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2747 | TimeUtils.formatDuration(now - userInfo.lastLoggedInTime, sb); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2748 | sb.append(" ago"); |
| 2749 | pw.println(sb); |
| 2750 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2751 | pw.print(" Has profile owner: "); |
| 2752 | pw.println(mIsUserManaged.get(userId)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2753 | pw.println(" Restrictions:"); |
| 2754 | synchronized (mRestrictionsLock) { |
| 2755 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2756 | pw, " ", mBaseUserRestrictions.get(userInfo.id)); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2757 | pw.println(" Device policy local restrictions:"); |
| 2758 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2759 | pw, " ", mDevicePolicyLocalUserRestrictions.get(userInfo.id)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2760 | pw.println(" Effective restrictions:"); |
| 2761 | UserRestrictionsUtils.dumpRestrictions( |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2762 | pw, " ", mCachedEffectiveUserRestrictions.get(userInfo.id)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2763 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2764 | |
| 2765 | if (userData.account != null) { |
| 2766 | pw.print(" Account name: " + userData.account); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 2767 | pw.println(); |
| 2768 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2769 | |
| 2770 | if (userData.seedAccountName != null) { |
| 2771 | pw.print(" Seed account name: " + userData.seedAccountName); |
| 2772 | pw.println(); |
| 2773 | if (userData.seedAccountType != null) { |
| 2774 | pw.print(" account type: " + userData.seedAccountType); |
| 2775 | pw.println(); |
| 2776 | } |
| 2777 | if (userData.seedAccountOptions != null) { |
| 2778 | pw.print(" account options exist"); |
| 2779 | pw.println(); |
| 2780 | } |
| 2781 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2782 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2783 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2784 | pw.println(); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2785 | pw.println(" Device policy global restrictions:"); |
| 2786 | synchronized (mRestrictionsLock) { |
| 2787 | UserRestrictionsUtils |
| 2788 | .dumpRestrictions(pw, " ", mDevicePolicyGlobalUserRestrictions); |
| 2789 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 2790 | pw.println(); |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2791 | pw.println(" Guest restrictions:"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2792 | synchronized (mGuestRestrictions) { |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2793 | UserRestrictionsUtils.dumpRestrictions(pw, " ", mGuestRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame] | 2794 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2795 | synchronized (mUsersLock) { |
| 2796 | pw.println(); |
| 2797 | pw.println(" Device managed: " + mIsDeviceManaged); |
| 2798 | } |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2799 | // Dump some capabilities |
| 2800 | pw.println(); |
| 2801 | pw.println(" Max users: " + UserManager.getMaxSupportedUsers()); |
| 2802 | pw.println(" Supports switchable users: " + UserManager.supportsMultipleUsers()); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2803 | } |
| 2804 | } |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2805 | |
| 2806 | final class MainHandler extends Handler { |
| 2807 | |
| 2808 | @Override |
| 2809 | public void handleMessage(Message msg) { |
| 2810 | switch (msg.what) { |
| 2811 | case WRITE_USER_MSG: |
| 2812 | removeMessages(WRITE_USER_MSG, msg.obj); |
| 2813 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2814 | int userId = ((UserData) msg.obj).info.id; |
| 2815 | UserData userData = getUserDataNoChecks(userId); |
| 2816 | if (userData != null) { |
| 2817 | writeUserLP(userData); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | } |
| 2821 | } |
| 2822 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 2823 | |
| 2824 | /** |
| 2825 | * @param userId |
| 2826 | * @return whether the user has been initialized yet |
| 2827 | */ |
| 2828 | boolean isInitialized(int userId) { |
| 2829 | return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 2830 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2831 | |
| 2832 | private class LocalService extends UserManagerInternal { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2833 | @Override |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2834 | public void setDevicePolicyUserRestrictions(int userId, @NonNull Bundle localRestrictions, |
| 2835 | @Nullable Bundle globalRestrictions) { |
| 2836 | UserManagerService.this.setDevicePolicyUserRestrictions(userId, localRestrictions, |
| 2837 | globalRestrictions); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | @Override |
| 2841 | public Bundle getBaseUserRestrictions(int userId) { |
| 2842 | synchronized (mRestrictionsLock) { |
| 2843 | return mBaseUserRestrictions.get(userId); |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | @Override |
| 2848 | public void setBaseUserRestrictionsByDpmsForMigration( |
| 2849 | int userId, Bundle baseRestrictions) { |
| 2850 | synchronized (mRestrictionsLock) { |
| 2851 | mBaseUserRestrictions.put(userId, new Bundle(baseRestrictions)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2852 | invalidateEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2855 | final UserData userData = getUserDataNoChecks(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2856 | synchronized (mPackagesLock) { |
Amith Yamasani | 1274787 | 2015-12-07 14:19:49 -0800 | [diff] [blame] | 2857 | if (userData != null) { |
| 2858 | writeUserLP(userData); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2859 | } else { |
| 2860 | Slog.w(LOG_TAG, "UserInfo not found for " + userId); |
| 2861 | } |
| 2862 | } |
| 2863 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2864 | |
| 2865 | @Override |
| 2866 | public boolean getUserRestriction(int userId, String key) { |
| 2867 | return getUserRestrictions(userId).getBoolean(key); |
| 2868 | } |
| 2869 | |
| 2870 | @Override |
| 2871 | public void addUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2872 | synchronized (mUserRestrictionsListeners) { |
| 2873 | mUserRestrictionsListeners.add(listener); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | @Override |
| 2878 | public void removeUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2879 | synchronized (mUserRestrictionsListeners) { |
| 2880 | mUserRestrictionsListeners.remove(listener); |
| 2881 | } |
| 2882 | } |
Makoto Onuki | e7927da | 2015-11-25 10:05:17 -0800 | [diff] [blame] | 2883 | |
| 2884 | @Override |
| 2885 | public void setDeviceManaged(boolean isManaged) { |
| 2886 | synchronized (mUsersLock) { |
| 2887 | mIsDeviceManaged = isManaged; |
| 2888 | } |
| 2889 | } |
| 2890 | |
| 2891 | @Override |
| 2892 | public void setUserManaged(int userId, boolean isManaged) { |
| 2893 | synchronized (mUsersLock) { |
| 2894 | mIsUserManaged.put(userId, isManaged); |
| 2895 | } |
| 2896 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2897 | } |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2898 | |
| 2899 | private class Shell extends ShellCommand { |
| 2900 | @Override |
| 2901 | public int onCommand(String cmd) { |
| 2902 | return onShellCommand(this, cmd); |
| 2903 | } |
| 2904 | |
| 2905 | @Override |
| 2906 | public void onHelp() { |
| 2907 | final PrintWriter pw = getOutPrintWriter(); |
| 2908 | pw.println("User manager (user) commands:"); |
| 2909 | pw.println(" help"); |
| 2910 | pw.println(" Print this help text."); |
| 2911 | pw.println(""); |
| 2912 | pw.println(" list"); |
| 2913 | pw.println(" Prints all users on the system."); |
| 2914 | } |
| 2915 | } |
Makoto Onuki | 1a2cd74 | 2015-11-16 13:51:27 -0800 | [diff] [blame] | 2916 | |
| 2917 | private static void debug(String message) { |
| 2918 | Log.d(LOG_TAG, message + |
| 2919 | (DBG_WITH_STACKTRACE ? " called at\n" + Debug.getCallers(10, " ") : "")); |
| 2920 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2921 | } |