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