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