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