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