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