Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.pm; |
| 18 | |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 23 | import android.app.ActivityManagerInternal; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 24 | import android.app.ActivityManagerNative; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 25 | import android.app.IActivityManager; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 26 | import android.app.IStopUserCallback; |
Xiaohui Chen | 203243a | 2015-07-16 11:55:47 -0700 | [diff] [blame] | 27 | import android.app.admin.DevicePolicyManager; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 28 | import android.app.admin.DevicePolicyManagerInternal; |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 32 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 34 | import android.content.pm.PackageManager.NameNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 35 | import android.content.pm.UserInfo; |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 36 | import android.graphics.Bitmap; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 37 | import android.os.Binder; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 39 | import android.os.Environment; |
| 40 | import android.os.FileUtils; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 41 | import android.os.Handler; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 42 | import android.os.IUserManager; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 43 | import android.os.Message; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 44 | import android.os.ParcelFileDescriptor; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 45 | import android.os.Parcelable; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 46 | import android.os.Process; |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 48 | import android.os.ResultReceiver; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 49 | import android.os.ServiceManager; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 50 | import android.os.ShellCommand; |
Clara Bayarri | f05b9d0 | 2015-10-23 13:25:19 +0100 | [diff] [blame] | 51 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 52 | import android.os.UserHandle; |
Jeff Sharkey | 27bd34d | 2012-09-16 12:49:00 -0700 | [diff] [blame] | 53 | import android.os.UserManager; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 54 | import android.os.UserManagerInternal; |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 55 | import android.os.UserManagerInternal.UserRestrictionsListener; |
Paul Crowley | 85e4e81 | 2015-05-19 12:42:00 +0100 | [diff] [blame] | 56 | import android.os.storage.StorageManager; |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 57 | import android.os.storage.VolumeInfo; |
| 58 | import android.system.ErrnoException; |
| 59 | import android.system.Os; |
| 60 | import android.system.OsConstants; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 61 | import android.util.AtomicFile; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 62 | import android.util.Log; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 63 | import android.util.Slog; |
| 64 | import android.util.SparseArray; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 65 | import android.util.SparseBooleanArray; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 66 | import android.util.TimeUtils; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 67 | import android.util.Xml; |
| 68 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 69 | import com.android.internal.annotations.GuardedBy; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 70 | import com.android.internal.annotations.VisibleForTesting; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 71 | import com.android.internal.app.IAppOpsService; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 72 | import com.android.internal.util.FastXmlSerializer; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 73 | import com.android.internal.util.Preconditions; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 74 | import com.android.internal.util.XmlUtils; |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 75 | import com.android.server.LocalServices; |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 76 | |
| 77 | import org.xmlpull.v1.XmlPullParser; |
| 78 | import org.xmlpull.v1.XmlPullParserException; |
| 79 | import org.xmlpull.v1.XmlSerializer; |
| 80 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 81 | import java.io.BufferedOutputStream; |
| 82 | import java.io.File; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 83 | import java.io.FileDescriptor; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 84 | import java.io.FileInputStream; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 85 | import java.io.FileNotFoundException; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 86 | import java.io.FileOutputStream; |
| 87 | import java.io.IOException; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 88 | import java.io.PrintWriter; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 89 | import java.nio.charset.StandardCharsets; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 90 | import java.util.ArrayList; |
| 91 | import java.util.List; |
| 92 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 93 | import libcore.io.IoUtils; |
| 94 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Service for {@link UserManager}. |
| 97 | * |
| 98 | * Method naming convention: |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 99 | * <ul> |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 100 | * <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] | 101 | * <li> Methods suffixed with "LR" should be called within the {@link #mRestrictionsLock} lock. |
| 102 | * <li> Methods suffixed with "LU" should be called within the {@link #mUsersLock} lock. |
| 103 | * </ul> |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 104 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 105 | public class UserManagerService extends IUserManager.Stub { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 106 | private static final String LOG_TAG = "UserManagerService"; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 107 | private static final boolean DBG = false; // DO NOT SUBMIT WITH TRUE |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 108 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 109 | private static final String TAG_NAME = "name"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 110 | private static final String ATTR_FLAGS = "flags"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 111 | private static final String ATTR_ICON_PATH = "icon"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 112 | private static final String ATTR_ID = "id"; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 113 | private static final String ATTR_CREATION_TIME = "created"; |
| 114 | private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn"; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 115 | private static final String ATTR_SERIAL_NO = "serialNumber"; |
| 116 | private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber"; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 117 | private static final String ATTR_PARTIAL = "partial"; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 118 | private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove"; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 119 | private static final String ATTR_USER_VERSION = "version"; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 120 | private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId"; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 121 | private static final String ATTR_RESTRICTED_PROFILE_PARENT_ID = "restrictedProfileParentId"; |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 122 | private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 123 | private static final String TAG_USERS = "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 124 | private static final String TAG_USER = "user"; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 125 | private static final String TAG_RESTRICTIONS = "restrictions"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 126 | private static final String TAG_ENTRY = "entry"; |
| 127 | private static final String TAG_VALUE = "value"; |
| 128 | private static final String ATTR_KEY = "key"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 129 | private static final String ATTR_VALUE_TYPE = "type"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 130 | private static final String ATTR_MULTIPLE = "m"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 131 | |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 132 | private static final String ATTR_TYPE_STRING_ARRAY = "sa"; |
| 133 | private static final String ATTR_TYPE_STRING = "s"; |
| 134 | private static final String ATTR_TYPE_BOOLEAN = "b"; |
Amith Yamasani | 5b5aa40 | 2014-06-01 20:10:14 -0700 | [diff] [blame] | 135 | private static final String ATTR_TYPE_INTEGER = "i"; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 136 | private static final String ATTR_TYPE_BUNDLE = "B"; |
| 137 | private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA"; |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 138 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 139 | private static final String USER_INFO_DIR = "system" + File.separator + "users"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 140 | private static final String USER_LIST_FILENAME = "userlist.xml"; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 141 | private static final String USER_PHOTO_FILENAME = "photo.png"; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 142 | private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp"; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 143 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 144 | private static final String RESTRICTIONS_FILE_PREFIX = "res_"; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 145 | private static final String XML_SUFFIX = ".xml"; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 146 | |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 147 | private static final int MIN_USER_ID = 10; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 148 | // We need to keep process uid within Integer.MAX_VALUE. |
| 149 | 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] | 150 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 151 | private static final int USER_VERSION = 6; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 152 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 153 | private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms |
| 154 | |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 155 | // 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] | 156 | // without first making sure that the rest of the framework is prepared for it. |
| 157 | private static final int MAX_MANAGED_PROFILES = 1; |
| 158 | |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 159 | static final int WRITE_USER_MSG = 1; |
| 160 | static final int WRITE_USER_DELAY = 2*1000; // 2 seconds |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 161 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 162 | private static final String XATTR_SERIAL = "user.serial"; |
| 163 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 164 | private final Context mContext; |
| 165 | private final PackageManagerService mPm; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 166 | private final Object mPackagesLock; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 167 | // Short-term lock for internal state, when interaction/sync with PM is not required |
| 168 | private final Object mUsersLock = new Object(); |
| 169 | private final Object mRestrictionsLock = new Object(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 171 | private final Handler mHandler; |
| 172 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 173 | private final File mUsersDir; |
| 174 | private final File mUserListFile; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 175 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 176 | @GuardedBy("mUsersLock") |
| 177 | private final SparseArray<UserInfo> mUsers = new SparseArray<>(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * User restrictions set via UserManager. This doesn't include restrictions set by |
| 181 | * device owner / profile owners. |
| 182 | * |
| 183 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 184 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 185 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 186 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 187 | * (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] | 188 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 189 | */ |
| 190 | @GuardedBy("mRestrictionsLock") |
| 191 | private final SparseArray<Bundle> mBaseUserRestrictions = new SparseArray<>(); |
| 192 | |
| 193 | /** |
| 194 | * Cached user restrictions that are in effect -- i.e. {@link #mBaseUserRestrictions} combined |
| 195 | * with device / profile owner restrictions. We'll initialize it lazily; use |
| 196 | * {@link #getEffectiveUserRestrictions} to access it. |
| 197 | * |
| 198 | * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user, |
| 199 | * a new {@link Bundle} should always be created and set. This is because a {@link Bundle} |
| 200 | * maybe shared between {@link #mBaseUserRestrictions} and |
| 201 | * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately. |
| 202 | * (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] | 203 | * {@link #updateUserRestrictionsInternalLR}. |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 204 | */ |
| 205 | @GuardedBy("mRestrictionsLock") |
| 206 | private final SparseArray<Bundle> mCachedEffectiveUserRestrictions = new SparseArray<>(); |
| 207 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 208 | /** |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 209 | * User restrictions that have already been applied in {@link #applyUserRestrictionsLR}. We |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 210 | * use it to detect restrictions that have changed since the last |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 211 | * {@link #applyUserRestrictionsLR} call. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 212 | */ |
| 213 | @GuardedBy("mRestrictionsLock") |
| 214 | private final SparseArray<Bundle> mAppliedUserRestrictions = new SparseArray<>(); |
| 215 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 216 | @GuardedBy("mGuestRestrictions") |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 217 | private final Bundle mGuestRestrictions = new Bundle(); |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * Set of user IDs being actively removed. Removed IDs linger in this set |
| 221 | * for several seconds to work around a VFS caching issue. |
| 222 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 223 | @GuardedBy("mUsersLock") |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 224 | private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray(); |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 225 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 226 | @GuardedBy("mUsersLock") |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 227 | private int[] mUserIds; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 228 | @GuardedBy("mPackagesLock") |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 229 | private int mNextSerialNumber; |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 230 | private int mUserVersion = 0; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 231 | |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 232 | private IAppOpsService mAppOpsService; |
| 233 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 234 | private final LocalService mLocalService; |
| 235 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 236 | @GuardedBy("mUserRestrictionsListeners") |
| 237 | private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners = |
| 238 | new ArrayList<>(); |
| 239 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 240 | private static UserManagerService sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 241 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 242 | public static UserManagerService getInstance() { |
| 243 | synchronized (UserManagerService.class) { |
| 244 | return sInstance; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 245 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 246 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 247 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 248 | @VisibleForTesting |
| 249 | UserManagerService(File dataDir) { |
| 250 | this(null, null, new Object(), dataDir); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Called by package manager to create the service. This is closely |
| 255 | * associated with the package manager, and the given lock is the |
| 256 | * package manager's own lock. |
| 257 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 258 | UserManagerService(Context context, PackageManagerService pm, Object packagesLock) { |
| 259 | this(context, pm, packagesLock, Environment.getDataDirectory()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 262 | private UserManagerService(Context context, PackageManagerService pm, |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 263 | Object packagesLock, File dataDir) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 264 | mContext = context; |
| 265 | mPm = pm; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 266 | mPackagesLock = packagesLock; |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 267 | mHandler = new MainHandler(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 268 | synchronized (mPackagesLock) { |
| 269 | mUsersDir = new File(dataDir, USER_INFO_DIR); |
| 270 | mUsersDir.mkdirs(); |
| 271 | // Make zeroth user directory, for services to migrate their files to that location |
| 272 | File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM)); |
| 273 | userZeroDir.mkdirs(); |
| 274 | FileUtils.setPermissions(mUsersDir.toString(), |
| 275 | FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH, |
| 276 | -1, -1); |
| 277 | mUserListFile = new File(mUsersDir, USER_LIST_FILENAME); |
| 278 | initDefaultGuestRestrictions(); |
| 279 | readUserListLP(); |
| 280 | sInstance = this; |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 281 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 282 | mLocalService = new LocalService(); |
| 283 | LocalServices.addService(UserManagerInternal.class, mLocalService); |
Xiaohui Chen | 4be96e4 | 2015-05-06 09:55:43 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void systemReady() { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 287 | // Prune out any partially created/partially removed users. |
| 288 | ArrayList<UserInfo> partials = new ArrayList<>(); |
| 289 | synchronized (mUsersLock) { |
| 290 | final int userSize = mUsers.size(); |
| 291 | for (int i = 0; i < userSize; i++) { |
| 292 | UserInfo ui = mUsers.valueAt(i); |
| 293 | if ((ui.partial || ui.guestToRemove) && i != 0) { |
| 294 | partials.add(ui); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 295 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 296 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 297 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 298 | final int partialsSize = partials.size(); |
| 299 | for (int i = 0; i < partialsSize; i++) { |
| 300 | UserInfo ui = partials.get(i); |
| 301 | Slog.w(LOG_TAG, "Removing partially created user " + ui.id |
| 302 | + " (name=" + ui.name + ")"); |
| 303 | removeUserState(ui.id); |
| 304 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 305 | onUserForeground(UserHandle.USER_SYSTEM); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 306 | mAppOpsService = IAppOpsService.Stub.asInterface( |
| 307 | ServiceManager.getService(Context.APP_OPS_SERVICE)); |
| 308 | for (int i = 0; i < mUserIds.length; ++i) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 309 | final int userId = mUserIds[i]; |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 310 | try { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 311 | mAppOpsService.setUserRestrictions(getEffectiveUserRestrictions(userId), userId); |
Jason Monk | 6206299 | 2014-05-06 09:55:28 -0400 | [diff] [blame] | 312 | } catch (RemoteException e) { |
| 313 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 314 | } |
| 315 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 319 | public UserInfo getPrimaryUser() { |
| 320 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 321 | synchronized (mUsersLock) { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 322 | final int userSize = mUsers.size(); |
| 323 | for (int i = 0; i < userSize; i++) { |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 324 | UserInfo ui = mUsers.valueAt(i); |
| 325 | if (ui.isPrimary()) { |
| 326 | return ui; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | return null; |
| 331 | } |
| 332 | |
| 333 | @Override |
Xiaohui Chen | 594f208 | 2015-08-18 11:04:20 -0700 | [diff] [blame] | 334 | public @NonNull List<UserInfo> getUsers(boolean excludeDying) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 335 | checkManageUsersPermission("query users"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 336 | synchronized (mUsersLock) { |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 337 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 338 | final int userSize = mUsers.size(); |
| 339 | for (int i = 0; i < userSize; i++) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 340 | UserInfo ui = mUsers.valueAt(i); |
| 341 | if (ui.partial) { |
| 342 | continue; |
| 343 | } |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 344 | if (!excludeDying || !mRemovingUserIds.get(ui.id)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 345 | users.add(ui); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 346 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 347 | } |
| 348 | return users; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 349 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 352 | @Override |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 353 | public List<UserInfo> getProfiles(int userId, boolean enabledOnly) { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 354 | if (userId != UserHandle.getCallingUserId()) { |
| 355 | checkManageUsersPermission("getting profiles related to user " + userId); |
| 356 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 357 | final long ident = Binder.clearCallingIdentity(); |
| 358 | try { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 359 | synchronized (mUsersLock) { |
| 360 | return getProfilesLU(userId, enabledOnly); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 361 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 362 | } finally { |
| 363 | Binder.restoreCallingIdentity(ident); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 367 | /** Assume permissions already checked and caller's identity cleared */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 368 | private List<UserInfo> getProfilesLU(int userId, boolean enabledOnly) { |
| 369 | UserInfo user = getUserInfoLU(userId); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 370 | ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size()); |
Amith Yamasani | dda003f | 2014-08-28 18:06:51 -0700 | [diff] [blame] | 371 | if (user == null) { |
| 372 | // Probably a dying user |
| 373 | return users; |
| 374 | } |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 375 | final int userSize = mUsers.size(); |
| 376 | for (int i = 0; i < userSize; i++) { |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 377 | UserInfo profile = mUsers.valueAt(i); |
| 378 | if (!isProfileOf(user, profile)) { |
| 379 | continue; |
| 380 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 381 | if (enabledOnly && !profile.isEnabled()) { |
| 382 | continue; |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 383 | } |
Amith Yamasani | 70fcf0c | 2014-07-11 08:40:19 -0700 | [diff] [blame] | 384 | if (mRemovingUserIds.get(profile.id)) { |
| 385 | continue; |
| 386 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 387 | users.add(profile); |
| 388 | } |
| 389 | return users; |
| 390 | } |
| 391 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 392 | @Override |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 393 | public int getCredentialOwnerProfile(int userHandle) { |
| 394 | checkManageUsersPermission("get the credential owner"); |
Clara Bayarri | 965da39 | 2015-10-28 17:53:53 +0000 | [diff] [blame] | 395 | if (!mContext.getSystemService(StorageManager.class).isPerUserEncryptionEnabled()) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 396 | synchronized (mUsersLock) { |
| 397 | UserInfo profileParent = getProfileParentLU(userHandle); |
Andres Morales | c5548c0 | 2015-08-05 10:23:12 -0700 | [diff] [blame] | 398 | if (profileParent != null) { |
| 399 | return profileParent.id; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return userHandle; |
| 405 | } |
| 406 | |
| 407 | @Override |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 408 | public boolean isSameProfileGroup(int userId, int otherUserId) { |
| 409 | if (userId == otherUserId) return true; |
| 410 | checkManageUsersPermission("check if in the same profile group"); |
| 411 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 412 | return isSameProfileGroupLP(userId, otherUserId); |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 416 | private boolean isSameProfileGroupLP(int userId, int otherUserId) { |
| 417 | synchronized (mUsersLock) { |
| 418 | UserInfo userInfo = getUserInfoLU(userId); |
| 419 | if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 420 | return false; |
| 421 | } |
| 422 | UserInfo otherUserInfo = getUserInfoLU(otherUserId); |
| 423 | if (otherUserInfo == null |
| 424 | || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 425 | return false; |
| 426 | } |
| 427 | return userInfo.profileGroupId == otherUserInfo.profileGroupId; |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 428 | } |
Xiaohui Chen | fd5b774 | 2015-10-14 15:47:04 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | @Override |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 432 | public UserInfo getProfileParent(int userHandle) { |
| 433 | checkManageUsersPermission("get the profile parent"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 434 | synchronized (mUsersLock) { |
| 435 | return getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 439 | private UserInfo getProfileParentLU(int userHandle) { |
| 440 | UserInfo profile = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 441 | if (profile == null) { |
| 442 | return null; |
| 443 | } |
| 444 | int parentUserId = profile.profileGroupId; |
| 445 | if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 446 | return null; |
| 447 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 448 | return getUserInfoLU(parentUserId); |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 452 | private static boolean isProfileOf(UserInfo user, UserInfo profile) { |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 453 | return user.id == profile.id || |
| 454 | (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 455 | && user.profileGroupId == profile.profileGroupId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | @Override |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 459 | public void setUserEnabled(int userId) { |
| 460 | checkManageUsersPermission("enable user"); |
| 461 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 462 | UserInfo info; |
| 463 | synchronized (mUsersLock) { |
| 464 | info = getUserInfoLU(userId); |
| 465 | } |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 466 | if (info != null && !info.isEnabled()) { |
| 467 | info.flags ^= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 468 | writeUserLP(info); |
Alexandra Gherghina | df35d57 | 2014-04-09 13:54:39 +0100 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | @Override |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 474 | public UserInfo getUserInfo(int userId) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 475 | checkManageUsersPermission("query user"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 476 | synchronized (mUsersLock) { |
| 477 | return getUserInfoLU(userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 481 | @Override |
| 482 | public boolean isRestricted() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 483 | synchronized (mUsersLock) { |
| 484 | return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 488 | @Override |
| 489 | public boolean canHaveRestrictedProfile(int userId) { |
| 490 | checkManageUsersPermission("canHaveRestrictedProfile"); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 491 | synchronized (mUsersLock) { |
| 492 | final UserInfo userInfo = getUserInfoLU(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 493 | if (userInfo == null || !userInfo.canHaveProfile()) { |
| 494 | return false; |
| 495 | } |
| 496 | if (!userInfo.isAdmin()) { |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService( |
| 501 | Context.DEVICE_POLICY_SERVICE); |
| 502 | // restricted profile can be created if there is no DO set and the admin user has no PO |
| 503 | return dpm.getDeviceOwner() == null && dpm.getProfileOwnerAsUser(userId) == null; |
| 504 | } |
| 505 | |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 506 | /* |
| 507 | * Should be locked on mUsers before calling this. |
| 508 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 509 | private UserInfo getUserInfoLU(int userId) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 510 | UserInfo ui = mUsers.get(userId); |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 511 | // If it is partial and not in the process of being removed, return as unknown user. |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 512 | if (ui != null && ui.partial && !mRemovingUserIds.get(userId)) { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 513 | Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId); |
| 514 | return null; |
| 515 | } |
| 516 | return ui; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 519 | /** |
| 520 | * Obtains {@link #mUsersLock} and return UserInfo from mUsers. |
| 521 | * <p>No permissions checking or any addition checks are made</p> |
| 522 | */ |
| 523 | private UserInfo getUserInfoNoChecks(int userId) { |
| 524 | synchronized (mUsersLock) { |
| 525 | return mUsers.get(userId); |
| 526 | } |
| 527 | } |
| 528 | |
Amith Yamasani | 236b2b5 | 2015-08-18 14:32:14 -0700 | [diff] [blame] | 529 | /** Called by PackageManagerService */ |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 530 | public boolean exists(int userId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 531 | return getUserInfoNoChecks(userId) != null; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 534 | @Override |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 535 | public void setUserName(int userId, String name) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 536 | checkManageUsersPermission("rename users"); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 537 | boolean changed = false; |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 538 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 539 | UserInfo info = getUserInfoNoChecks(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 540 | if (info == null || info.partial) { |
| 541 | Slog.w(LOG_TAG, "setUserName: unknown user #" + userId); |
| 542 | return; |
| 543 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 544 | if (name != null && !name.equals(info.name)) { |
| 545 | info.name = name; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 546 | writeUserLP(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 547 | changed = true; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 550 | if (changed) { |
| 551 | sendUserInfoChangedBroadcast(userId); |
| 552 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 555 | @Override |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 556 | public void setUserIcon(int userId, Bitmap bitmap) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 557 | checkManageUsersPermission("update users"); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 558 | long ident = Binder.clearCallingIdentity(); |
| 559 | try { |
| 560 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 561 | UserInfo info = getUserInfoNoChecks(userId); |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 562 | if (info == null || info.partial) { |
| 563 | Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId); |
| 564 | return; |
| 565 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 566 | writeBitmapLP(info, bitmap); |
| 567 | writeUserLP(info); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 568 | } |
Jason Monk | 9a94453 | 2014-07-08 09:31:21 -0400 | [diff] [blame] | 569 | sendUserInfoChangedBroadcast(userId); |
| 570 | } finally { |
| 571 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 572 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | private void sendUserInfoChangedBroadcast(int userId) { |
| 576 | Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED); |
| 577 | changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 578 | changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | 6fc1d4e | 2013-05-08 16:43:58 -0700 | [diff] [blame] | 579 | mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 582 | @Override |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 583 | public ParcelFileDescriptor getUserIcon(int userId) { |
| 584 | String iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 585 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 586 | UserInfo info = getUserInfoNoChecks(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 587 | if (info == null || info.partial) { |
| 588 | Slog.w(LOG_TAG, "getUserIcon: unknown user #" + userId); |
| 589 | return null; |
| 590 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 591 | int callingGroupId = getUserInfoNoChecks(UserHandle.getCallingUserId()).profileGroupId; |
Nicolas Prevot | 88cc346 | 2014-05-14 14:51:48 +0100 | [diff] [blame] | 592 | if (callingGroupId == UserInfo.NO_PROFILE_GROUP_ID |
| 593 | || callingGroupId != info.profileGroupId) { |
| 594 | checkManageUsersPermission("get the icon of a user who is not related"); |
| 595 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 596 | if (info.iconPath == null) { |
| 597 | return null; |
| 598 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 599 | iconPath = info.iconPath; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 600 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 601 | |
| 602 | try { |
| 603 | return ParcelFileDescriptor.open( |
| 604 | new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY); |
| 605 | } catch (FileNotFoundException e) { |
| 606 | Log.e(LOG_TAG, "Couldn't find icon file", e); |
| 607 | } |
| 608 | return null; |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 611 | public void makeInitialized(int userId) { |
| 612 | checkManageUsersPermission("makeInitialized"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 613 | boolean scheduleWriteUser = false; |
| 614 | UserInfo info; |
| 615 | synchronized (mUsersLock) { |
| 616 | info = mUsers.get(userId); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 617 | if (info == null || info.partial) { |
| 618 | Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 619 | return; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 620 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 621 | if ((info.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 622 | info.flags |= UserInfo.FLAG_INITIALIZED; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 623 | scheduleWriteUser = true; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 626 | if (scheduleWriteUser) { |
| 627 | scheduleWriteUser(info); |
| 628 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 631 | /** |
| 632 | * If default guest restrictions haven't been initialized yet, add the basic |
| 633 | * restrictions. |
| 634 | */ |
| 635 | private void initDefaultGuestRestrictions() { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 636 | synchronized (mGuestRestrictions) { |
| 637 | if (mGuestRestrictions.isEmpty()) { |
| 638 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true); |
| 639 | mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true); |
| 640 | } |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 644 | @Override |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 645 | public Bundle getDefaultGuestRestrictions() { |
| 646 | checkManageUsersPermission("getDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 647 | synchronized (mGuestRestrictions) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 648 | return new Bundle(mGuestRestrictions); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public void setDefaultGuestRestrictions(Bundle restrictions) { |
| 654 | checkManageUsersPermission("setDefaultGuestRestrictions"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 655 | synchronized (mGuestRestrictions) { |
| 656 | mGuestRestrictions.clear(); |
| 657 | mGuestRestrictions.putAll(restrictions); |
| 658 | } |
| 659 | synchronized (mPackagesLock) { |
| 660 | writeUserListLP(); |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 664 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 665 | private Bundle computeEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 666 | final DevicePolicyManagerInternal dpmi = |
| 667 | LocalServices.getService(DevicePolicyManagerInternal.class); |
| 668 | final Bundle systemRestrictions = mBaseUserRestrictions.get(userId); |
| 669 | |
| 670 | final Bundle effective; |
| 671 | if (dpmi == null) { |
| 672 | // TODO Make sure it's because DPMS is disabled and not because we called it too early. |
| 673 | effective = systemRestrictions; |
| 674 | } else { |
| 675 | effective = dpmi.getComposedUserRestrictions(userId, systemRestrictions); |
| 676 | } |
| 677 | return effective; |
| 678 | } |
| 679 | |
| 680 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 681 | private void invalidateEffectiveUserRestrictionsLR(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 682 | if (DBG) { |
| 683 | Log.d(LOG_TAG, "invalidateEffectiveUserRestrictions userId=" + userId); |
| 684 | } |
| 685 | mCachedEffectiveUserRestrictions.remove(userId); |
| 686 | } |
| 687 | |
| 688 | private Bundle getEffectiveUserRestrictions(int userId) { |
| 689 | synchronized (mRestrictionsLock) { |
| 690 | Bundle restrictions = mCachedEffectiveUserRestrictions.get(userId); |
| 691 | if (restrictions == null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 692 | restrictions = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 693 | mCachedEffectiveUserRestrictions.put(userId, restrictions); |
| 694 | } |
| 695 | return restrictions; |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 699 | /** @return a specific user restriction that's in effect currently. */ |
| 700 | @Override |
| 701 | public boolean hasUserRestriction(String restrictionKey, int userId) { |
| 702 | Bundle restrictions = getEffectiveUserRestrictions(userId); |
| 703 | return restrictions != null && restrictions.getBoolean(restrictionKey); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * @return UserRestrictions that are in effect currently. This always returns a new |
| 708 | * {@link Bundle}. |
| 709 | */ |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 710 | @Override |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 711 | public Bundle getUserRestrictions(int userId) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 712 | Bundle restrictions = getEffectiveUserRestrictions(userId); |
| 713 | return restrictions != null ? new Bundle(restrictions) : new Bundle(); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | @Override |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 717 | public void setUserRestriction(String key, boolean value, int userId) { |
Fyodor Kupolov | d4b2604 | 2015-07-27 14:30:59 -0700 | [diff] [blame] | 718 | checkManageUsersPermission("setUserRestriction"); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 719 | if (!UserRestrictionsUtils.SYSTEM_CONTROLLED_USER_RESTRICTIONS.contains(key)) { |
| 720 | setUserRestrictionNoCheck(key, value, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | @Override |
| 725 | public void setSystemControlledUserRestriction(String key, boolean value, int userId) { |
| 726 | checkSystemOrRoot("setSystemControlledUserRestriction"); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 727 | setUserRestrictionNoCheck(key, value, userId); |
| 728 | } |
| 729 | |
| 730 | private void setUserRestrictionNoCheck(String key, boolean value, int userId) { |
| 731 | synchronized (mRestrictionsLock) { |
| 732 | // Note we can't modify Bundles stored in mBaseUserRestrictions directly, so create |
| 733 | // a copy. |
| 734 | final Bundle newRestrictions = new Bundle(); |
| 735 | UserRestrictionsUtils.merge(newRestrictions, mBaseUserRestrictions.get(userId)); |
| 736 | newRestrictions.putBoolean(key, value); |
| 737 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 738 | updateUserRestrictionsInternalLR(newRestrictions, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 742 | /** |
| 743 | * Optionally updating user restrictions, calculate the effective user restrictions by |
| 744 | * consulting {@link com.android.server.devicepolicy.DevicePolicyManagerService} and also |
| 745 | * apply it to {@link com.android.server.AppOpsService}. |
| 746 | * TODO applyUserRestrictionsLocked() should also apply to system settings. |
| 747 | * |
| 748 | * @param newRestrictions User restrictions to set. If null, only the effective restrictions |
| 749 | * will be updated. Note don't pass an existing Bundle in {@link #mBaseUserRestrictions} |
| 750 | * or {@link #mCachedEffectiveUserRestrictions}; that'll most likely cause a sub |
| 751 | * @param userId target user ID. |
| 752 | */ |
| 753 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 754 | private void updateUserRestrictionsInternalLR( |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 755 | @Nullable Bundle newRestrictions, int userId) { |
| 756 | if (DBG) { |
| 757 | Log.d(LOG_TAG, "updateUserRestrictionsInternalLocked userId=" + userId |
| 758 | + " bundle=" + newRestrictions); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 759 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 760 | // Update system restrictions. |
| 761 | if (newRestrictions != null) { |
| 762 | // If newRestrictions == the current one, it's probably a bug. |
| 763 | Preconditions.checkState(mBaseUserRestrictions.get(userId) != newRestrictions); |
| 764 | Preconditions.checkState(mCachedEffectiveUserRestrictions.get(userId) |
| 765 | != newRestrictions); |
| 766 | mBaseUserRestrictions.put(userId, newRestrictions); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 767 | scheduleWriteUser(getUserInfoNoChecks(userId)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 770 | final Bundle effective = computeEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 771 | |
Makoto Onuki | 759a763 | 2015-10-28 16:43:10 -0700 | [diff] [blame] | 772 | mCachedEffectiveUserRestrictions.put(userId, effective); |
| 773 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 774 | applyUserRestrictionsLR(userId, effective); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 777 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 778 | private void applyUserRestrictionsLR(int userId, Bundle newRestrictions) { |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 779 | if (newRestrictions == null) { |
| 780 | newRestrictions = Bundle.EMPTY; |
| 781 | } |
| 782 | |
| 783 | Bundle prevRestrictions = mAppliedUserRestrictions.get(userId); |
| 784 | if (prevRestrictions == null) { |
| 785 | prevRestrictions = Bundle.EMPTY; |
| 786 | } |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 787 | |
| 788 | if (DBG) { |
| 789 | Log.d(LOG_TAG, "applyUserRestrictionsRL userId=" + userId |
| 790 | + " new=" + newRestrictions + " prev=" + prevRestrictions); |
| 791 | } |
| 792 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 793 | final long token = Binder.clearCallingIdentity(); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 794 | try { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 795 | mAppOpsService.setUserRestrictions(newRestrictions, userId); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 796 | } catch (RemoteException e) { |
| 797 | Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions"); |
| 798 | } finally { |
| 799 | Binder.restoreCallingIdentity(token); |
| 800 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 801 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 802 | UserRestrictionsUtils.applyUserRestrictionsLR( |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 803 | mContext, userId, newRestrictions, prevRestrictions); |
| 804 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 805 | notifyUserRestrictionsListeners(userId, newRestrictions, prevRestrictions); |
| 806 | |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 807 | mAppliedUserRestrictions.put(userId, new Bundle(newRestrictions)); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 810 | private void notifyUserRestrictionsListeners(final int userId, |
| 811 | Bundle newRestrictions, Bundle prevRestrictions) { |
| 812 | |
| 813 | final Bundle newRestrictionsFinal = new Bundle(newRestrictions); |
| 814 | final Bundle prevRestrictionsFinal = new Bundle(prevRestrictions); |
| 815 | |
| 816 | mHandler.post(new Runnable() { |
| 817 | @Override |
| 818 | public void run() { |
| 819 | final UserRestrictionsListener[] listeners; |
| 820 | synchronized (mUserRestrictionsListeners) { |
| 821 | listeners = new UserRestrictionsListener[mUserRestrictionsListeners.size()]; |
| 822 | mUserRestrictionsListeners.toArray(listeners); |
| 823 | } |
| 824 | for (int i = 0; i < listeners.length; i++) { |
| 825 | listeners[i].onUserRestrictionsChanged(userId, |
| 826 | newRestrictionsFinal, prevRestrictionsFinal); |
| 827 | } |
| 828 | } |
| 829 | }); |
| 830 | } |
| 831 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 832 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 833 | private void updateEffectiveUserRestrictionsLR(int userId) { |
| 834 | updateUserRestrictionsInternalLR(null, userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 838 | private void updateEffectiveUserRestrictionsForAllUsersLR() { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 839 | // First, invalidate all cached values. |
Makoto Onuki | 4f16073 | 2015-10-27 17:15:38 -0700 | [diff] [blame] | 840 | mCachedEffectiveUserRestrictions.clear(); |
| 841 | |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 842 | // We don't want to call into ActivityManagerNative while taking a lock, so we'll call |
| 843 | // it on a handler. |
| 844 | final Runnable r = new Runnable() { |
| 845 | @Override |
| 846 | public void run() { |
| 847 | // Then get the list of running users. |
| 848 | final int[] runningUsers; |
| 849 | try { |
| 850 | runningUsers = ActivityManagerNative.getDefault().getRunningUserIds(); |
| 851 | } catch (RemoteException e) { |
| 852 | Log.w(LOG_TAG, "Unable to access ActivityManagerNative"); |
| 853 | return; |
| 854 | } |
| 855 | // Then re-calculate the effective restrictions and apply, only for running users. |
| 856 | // It's okay if a new user has started after the getRunningUserIds() call, |
| 857 | // because we'll do the same thing (re-calculate the restrictions and apply) |
| 858 | // when we start a user. |
| 859 | // TODO: "Apply restrictions upon user start hasn't been implemented. Implement it. |
| 860 | synchronized (mRestrictionsLock) { |
| 861 | for (int i = 0; i < runningUsers.length; i++) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 862 | updateUserRestrictionsInternalLR(null, runningUsers[i]); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
| 866 | }; |
| 867 | mHandler.post(r); |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 870 | /** |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 871 | * Check if we've hit the limit of how many users can be created. |
| 872 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 873 | private boolean isUserLimitReached() { |
| 874 | int count; |
| 875 | synchronized (mUsersLock) { |
| 876 | count = getAliveUsersExcludingGuestsCountLU(); |
| 877 | } |
| 878 | return count >= UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | @Override |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 882 | public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 883 | checkManageUsersPermission("check if more managed profiles can be added."); |
| 884 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 885 | return false; |
| 886 | } |
Fyodor Kupolov | b615799 | 2015-06-05 15:32:28 -0700 | [diff] [blame] | 887 | if (!mContext.getPackageManager().hasSystemFeature( |
| 888 | PackageManager.FEATURE_MANAGED_USERS)) { |
| 889 | return false; |
| 890 | } |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 891 | // Limit number of managed profiles that can be created |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 892 | final int managedProfilesCount = getProfiles(userId, true).size() - 1; |
| 893 | final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0; |
| 894 | if (managedProfilesCount - profilesRemovedCount >= MAX_MANAGED_PROFILES) { |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 895 | return false; |
| 896 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 897 | synchronized(mUsersLock) { |
| 898 | UserInfo userInfo = getUserInfoLU(userId); |
Nicolas Prevot | b818681 | 2015-08-06 15:00:03 +0100 | [diff] [blame] | 899 | if (!userInfo.canHaveProfile()) { |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 900 | return false; |
| 901 | } |
Nicolas Prevot | 07387fe | 2015-10-30 17:53:30 +0000 | [diff] [blame] | 902 | int usersCountAfterRemoving = getAliveUsersExcludingGuestsCountLU() |
| 903 | - profilesRemovedCount; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 904 | // 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] | 905 | return usersCountAfterRemoving == 1 |
| 906 | || usersCountAfterRemoving < UserManager.getMaxSupportedUsers(); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 910 | private int getAliveUsersExcludingGuestsCountLU() { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 911 | int aliveUserCount = 0; |
| 912 | final int totalUserCount = mUsers.size(); |
| 913 | // Skip over users being removed |
| 914 | for (int i = 0; i < totalUserCount; i++) { |
| 915 | UserInfo user = mUsers.valueAt(i); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 916 | if (!mRemovingUserIds.get(user.id) |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 917 | && !user.isGuest() && !user.partial) { |
Amith Yamasani | f584f01 | 2014-05-19 17:57:25 -0700 | [diff] [blame] | 918 | aliveUserCount++; |
| 919 | } |
| 920 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 921 | return aliveUserCount; |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /** |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 925 | * 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] | 926 | * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 927 | * permission can make certain calls to the UserManager. |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 928 | * |
| 929 | * @param message used as message if SecurityException is thrown |
| 930 | * @throws SecurityException if the caller is not system or root |
| 931 | */ |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 932 | private static final void checkManageUsersPermission(String message) { |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 933 | final int uid = Binder.getCallingUid(); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 934 | if (uid != Process.SYSTEM_UID && uid != 0 |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 935 | && ActivityManager.checkComponentPermission( |
| 936 | android.Manifest.permission.MANAGE_USERS, |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 937 | uid, -1, true) != PackageManager.PERMISSION_GRANTED) { |
| 938 | throw new SecurityException("You need MANAGE_USERS permission to: " + message); |
| 939 | } |
Emily Bernier | 7a2b4d1 | 2014-04-23 12:51:35 -0400 | [diff] [blame] | 940 | } |
| 941 | |
Fyodor Kupolov | b501330 | 2015-04-17 17:59:14 -0700 | [diff] [blame] | 942 | private static void checkSystemOrRoot(String message) { |
| 943 | final int uid = Binder.getCallingUid(); |
| 944 | if (uid != Process.SYSTEM_UID && uid != 0) { |
| 945 | throw new SecurityException("Only system may call: " + message); |
| 946 | } |
| 947 | } |
| 948 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 949 | private void writeBitmapLP(UserInfo info, Bitmap bitmap) { |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 950 | try { |
| 951 | File dir = new File(mUsersDir, Integer.toString(info.id)); |
| 952 | File file = new File(dir, USER_PHOTO_FILENAME); |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 953 | File tmp = new File(dir, USER_PHOTO_FILENAME_TMP); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 954 | if (!dir.exists()) { |
| 955 | dir.mkdir(); |
| 956 | FileUtils.setPermissions( |
| 957 | dir.getPath(), |
| 958 | FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, |
| 959 | -1, -1); |
| 960 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 961 | FileOutputStream os; |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 962 | if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp)) |
| 963 | && tmp.renameTo(file)) { |
Amith Yamasani | 3b49f07 | 2012-09-17 10:21:43 -0700 | [diff] [blame] | 964 | info.iconPath = file.getAbsolutePath(); |
| 965 | } |
Amith Yamasani | e928d7d | 2012-09-17 21:46:51 -0700 | [diff] [blame] | 966 | try { |
| 967 | os.close(); |
| 968 | } catch (IOException ioe) { |
| 969 | // What the ... ! |
| 970 | } |
Adrian Roos | 1bdff91 | 2015-02-17 15:51:35 +0100 | [diff] [blame] | 971 | tmp.delete(); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 972 | } catch (FileNotFoundException e) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 973 | Slog.w(LOG_TAG, "Error setting photo for user ", e); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 974 | } |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 977 | /** |
| 978 | * Returns an array of user ids. This array is cached here for quick access, so do not modify or |
| 979 | * cache it elsewhere. |
| 980 | * @return the array of user ids. |
| 981 | */ |
Dianne Hackborn | 1676c85 | 2012-09-10 14:52:30 -0700 | [diff] [blame] | 982 | public int[] getUserIds() { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 983 | synchronized (mUsersLock) { |
Dianne Hackborn | 7767eac | 2012-08-23 18:25:40 -0700 | [diff] [blame] | 984 | return mUserIds; |
| 985 | } |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 988 | private void readUserListLP() { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 989 | if (!mUserListFile.exists()) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 990 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 991 | return; |
| 992 | } |
| 993 | FileInputStream fis = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 994 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 995 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 996 | fis = userListFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 997 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 998 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 999 | int type; |
| 1000 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1001 | && type != XmlPullParser.END_DOCUMENT) { |
| 1002 | ; |
| 1003 | } |
| 1004 | |
| 1005 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1006 | Slog.e(LOG_TAG, "Unable to read user list"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1007 | fallbackToSingleUserLP(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1008 | return; |
| 1009 | } |
| 1010 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1011 | mNextSerialNumber = -1; |
| 1012 | if (parser.getName().equals(TAG_USERS)) { |
| 1013 | String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO); |
| 1014 | if (lastSerialNumber != null) { |
| 1015 | mNextSerialNumber = Integer.parseInt(lastSerialNumber); |
| 1016 | } |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1017 | String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION); |
| 1018 | if (versionNumber != null) { |
| 1019 | mUserVersion = Integer.parseInt(versionNumber); |
| 1020 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1023 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1024 | if (type == XmlPullParser.START_TAG) { |
| 1025 | final String name = parser.getName(); |
| 1026 | if (name.equals(TAG_USER)) { |
| 1027 | String id = parser.getAttributeValue(null, ATTR_ID); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1028 | UserInfo user = readUserLP(Integer.parseInt(id)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1029 | |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1030 | if (user != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1031 | synchronized (mUsersLock) { |
| 1032 | mUsers.put(user.id, user); |
| 1033 | if (mNextSerialNumber < 0 || mNextSerialNumber <= user.id) { |
| 1034 | mNextSerialNumber = user.id + 1; |
| 1035 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1036 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1037 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1038 | } else if (name.equals(TAG_GUEST_RESTRICTIONS)) { |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1039 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1040 | && type != XmlPullParser.END_TAG) { |
| 1041 | if (type == XmlPullParser.START_TAG) { |
| 1042 | if (parser.getName().equals(TAG_RESTRICTIONS)) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1043 | synchronized (mGuestRestrictions) { |
| 1044 | UserRestrictionsUtils |
| 1045 | .readRestrictions(parser, mGuestRestrictions); |
| 1046 | } |
Amith Yamasani | da0b168 | 2014-11-21 12:58:17 -0800 | [diff] [blame] | 1047 | } |
| 1048 | break; |
| 1049 | } |
| 1050 | } |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1051 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1054 | updateUserIds(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1055 | upgradeIfNecessaryLP(); |
| 1056 | } catch (IOException | XmlPullParserException e) { |
| 1057 | fallbackToSingleUserLP(); |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1058 | } finally { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1059 | IoUtils.closeQuietly(fis); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1060 | } |
| 1061 | } |
| 1062 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1063 | /** |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1064 | * 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] | 1065 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1066 | private void upgradeIfNecessaryLP() { |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1067 | int userVersion = mUserVersion; |
| 1068 | if (userVersion < 1) { |
| 1069 | // Assign a proper name for the owner, if not initialized correctly before |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1070 | UserInfo user = getUserInfoNoChecks(UserHandle.USER_SYSTEM); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1071 | if ("Primary".equals(user.name)) { |
| 1072 | user.name = mContext.getResources().getString(com.android.internal.R.string.owner_name); |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1073 | scheduleWriteUser(user); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1074 | } |
| 1075 | userVersion = 1; |
| 1076 | } |
| 1077 | |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1078 | if (userVersion < 2) { |
| 1079 | // Owner should be marked as initialized |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1080 | UserInfo user = getUserInfoNoChecks(UserHandle.USER_SYSTEM); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1081 | if ((user.flags & UserInfo.FLAG_INITIALIZED) == 0) { |
| 1082 | user.flags |= UserInfo.FLAG_INITIALIZED; |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1083 | scheduleWriteUser(user); |
Amith Yamasani | bc962505 | 2012-11-15 14:39:18 -0800 | [diff] [blame] | 1084 | } |
| 1085 | userVersion = 2; |
| 1086 | } |
| 1087 | |
Amith Yamasani | 350962c | 2013-08-06 11:18:53 -0700 | [diff] [blame] | 1088 | |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1089 | if (userVersion < 4) { |
Amith Yamasani | 5e486f5 | 2013-08-07 11:06:44 -0700 | [diff] [blame] | 1090 | userVersion = 4; |
| 1091 | } |
| 1092 | |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1093 | if (userVersion < 5) { |
| 1094 | initDefaultGuestRestrictions(); |
| 1095 | userVersion = 5; |
| 1096 | } |
| 1097 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1098 | if (userVersion < 6) { |
| 1099 | final boolean splitSystemUser = UserManager.isSplitSystemUser(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1100 | synchronized (mUsersLock) { |
| 1101 | for (int i = 0; i < mUsers.size(); i++) { |
| 1102 | UserInfo user = mUsers.valueAt(i); |
| 1103 | // In non-split mode, only user 0 can have restricted profiles |
| 1104 | if (!splitSystemUser && user.isRestricted() |
| 1105 | && (user.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID)) { |
| 1106 | user.restrictedProfileParentId = UserHandle.USER_SYSTEM; |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1107 | scheduleWriteUser(user); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1108 | } |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | userVersion = 6; |
| 1112 | } |
| 1113 | |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1114 | if (userVersion < USER_VERSION) { |
| 1115 | Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to " |
| 1116 | + USER_VERSION); |
| 1117 | } else { |
| 1118 | mUserVersion = userVersion; |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1119 | writeUserListLP(); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1123 | private void fallbackToSingleUserLP() { |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1124 | int flags = UserInfo.FLAG_INITIALIZED; |
| 1125 | // In split system user mode, the admin and primary flags are assigned to the first human |
| 1126 | // user. |
| 1127 | if (!UserManager.isSplitSystemUser()) { |
| 1128 | flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY; |
| 1129 | } |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1130 | // Create the system user |
Xiaohui Chen | 70f6c38 | 2015-04-28 14:21:43 -0700 | [diff] [blame] | 1131 | UserInfo system = new UserInfo(UserHandle.USER_SYSTEM, |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1132 | mContext.getResources().getString(com.android.internal.R.string.owner_name), null, |
Xiaohui Chen | b31e14a | 2015-07-13 16:04:55 -0700 | [diff] [blame] | 1133 | flags); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1134 | synchronized (mUsersLock) { |
| 1135 | mUsers.put(system.id, system); |
| 1136 | } |
Amith Yamasani | 634cf31 | 2012-10-04 17:34:21 -0700 | [diff] [blame] | 1137 | mNextSerialNumber = MIN_USER_ID; |
Geoffrey Borggaard | 15b8b2c | 2013-08-28 22:11:10 -0400 | [diff] [blame] | 1138 | mUserVersion = USER_VERSION; |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1139 | |
Geoffrey Borggaard | e45e45e3 | 2013-01-24 10:03:20 -0500 | [diff] [blame] | 1140 | Bundle restrictions = new Bundle(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1141 | synchronized (mRestrictionsLock) { |
| 1142 | mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions); |
| 1143 | } |
Amith Yamasani | 67df64b | 2012-12-14 12:09:36 -0800 | [diff] [blame] | 1144 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1145 | updateUserIds(); |
Amith Yamasani | aa6634e | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 1146 | initDefaultGuestRestrictions(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1147 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1148 | writeUserListLP(); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1149 | writeUserLP(system); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
Makoto Onuki | 9e935a3 | 2015-11-06 14:24:24 -0800 | [diff] [blame] | 1152 | private void scheduleWriteUser(UserInfo userInfo) { |
| 1153 | // No need to wrap it within a lock -- worst case, we'll just post the same message |
| 1154 | // twice. |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 1155 | if (!mHandler.hasMessages(WRITE_USER_MSG, userInfo)) { |
| 1156 | Message msg = mHandler.obtainMessage(WRITE_USER_MSG, userInfo); |
| 1157 | mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * Writes the user file in this format: |
| 1163 | * |
| 1164 | * <user flags="20039023" id="0"> |
| 1165 | * <name>Primary</name> |
| 1166 | * </user> |
| 1167 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1168 | private void writeUserLP(UserInfo userInfo) { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1169 | FileOutputStream fos = null; |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1170 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userInfo.id + XML_SUFFIX)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1171 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1172 | fos = userFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1173 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1174 | |
| 1175 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1176 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1177 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1178 | serializer.startDocument(null, true); |
| 1179 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1180 | |
| 1181 | serializer.startTag(null, TAG_USER); |
| 1182 | serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1183 | serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1184 | serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags)); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1185 | serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime)); |
| 1186 | serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME, |
| 1187 | Long.toString(userInfo.lastLoggedInTime)); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1188 | if (userInfo.iconPath != null) { |
| 1189 | serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath); |
| 1190 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1191 | if (userInfo.partial) { |
| 1192 | serializer.attribute(null, ATTR_PARTIAL, "true"); |
| 1193 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1194 | if (userInfo.guestToRemove) { |
| 1195 | serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true"); |
| 1196 | } |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1197 | if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1198 | serializer.attribute(null, ATTR_PROFILE_GROUP_ID, |
| 1199 | Integer.toString(userInfo.profileGroupId)); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1200 | } |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1201 | if (userInfo.restrictedProfileParentId != UserInfo.NO_PROFILE_GROUP_ID) { |
| 1202 | serializer.attribute(null, ATTR_RESTRICTED_PROFILE_PARENT_ID, |
| 1203 | Integer.toString(userInfo.restrictedProfileParentId)); |
| 1204 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1205 | serializer.startTag(null, TAG_NAME); |
| 1206 | serializer.text(userInfo.name); |
| 1207 | serializer.endTag(null, TAG_NAME); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1208 | Bundle restrictions; |
| 1209 | synchronized (mRestrictionsLock) { |
| 1210 | restrictions = mBaseUserRestrictions.get(userInfo.id); |
| 1211 | } |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1212 | if (restrictions != null) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1213 | UserRestrictionsUtils |
| 1214 | .writeRestrictions(serializer, restrictions, TAG_RESTRICTIONS); |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1215 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1216 | serializer.endTag(null, TAG_USER); |
| 1217 | |
| 1218 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1219 | userFile.finishWrite(fos); |
| 1220 | } catch (Exception ioe) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1221 | Slog.e(LOG_TAG, "Error writing user info " + userInfo.id + "\n" + ioe); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1222 | userFile.failWrite(fos); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | /* |
| 1227 | * Writes the user list file in this format: |
| 1228 | * |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1229 | * <users nextSerialNumber="3"> |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1230 | * <user id="0"></user> |
| 1231 | * <user id="2"></user> |
| 1232 | * </users> |
| 1233 | */ |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1234 | private void writeUserListLP() { |
Amith Yamasani | 742a671 | 2011-05-04 14:49:28 -0700 | [diff] [blame] | 1235 | FileOutputStream fos = null; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1236 | AtomicFile userListFile = new AtomicFile(mUserListFile); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1237 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1238 | fos = userListFile.startWrite(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1239 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1240 | |
| 1241 | // XmlSerializer serializer = XmlUtils.serializerInstance(); |
| 1242 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1243 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1244 | serializer.startDocument(null, true); |
| 1245 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1246 | |
| 1247 | serializer.startTag(null, TAG_USERS); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1248 | serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber)); |
Amith Yamasani | 6f34b41 | 2012-10-22 18:19:27 -0700 | [diff] [blame] | 1249 | serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1250 | |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1251 | serializer.startTag(null, TAG_GUEST_RESTRICTIONS); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1252 | synchronized (mGuestRestrictions) { |
| 1253 | UserRestrictionsUtils |
| 1254 | .writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS); |
| 1255 | } |
Amith Yamasani | e4afaa3 | 2014-06-30 14:55:07 +0530 | [diff] [blame] | 1256 | serializer.endTag(null, TAG_GUEST_RESTRICTIONS); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1257 | int[] userIdsToWrite; |
| 1258 | synchronized (mUsersLock) { |
| 1259 | userIdsToWrite = new int[mUsers.size()]; |
| 1260 | for (int i = 0; i < userIdsToWrite.length; i++) { |
| 1261 | UserInfo user = mUsers.valueAt(i); |
| 1262 | userIdsToWrite[i] = user.id; |
| 1263 | } |
| 1264 | } |
| 1265 | for (int id : userIdsToWrite) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1266 | serializer.startTag(null, TAG_USER); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1267 | serializer.attribute(null, ATTR_ID, Integer.toString(id)); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1268 | serializer.endTag(null, TAG_USER); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | serializer.endTag(null, TAG_USERS); |
| 1272 | |
| 1273 | serializer.endDocument(); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1274 | userListFile.finishWrite(fos); |
| 1275 | } catch (Exception e) { |
| 1276 | userListFile.failWrite(fos); |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1277 | Slog.e(LOG_TAG, "Error writing user list"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1278 | } |
| 1279 | } |
| 1280 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1281 | private UserInfo readUserLP(int id) { |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1282 | int flags = 0; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1283 | int serialNumber = id; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1284 | String name = null; |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1285 | String iconPath = null; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1286 | long creationTime = 0L; |
| 1287 | long lastLoggedInTime = 0L; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1288 | int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1289 | int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1290 | boolean partial = false; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1291 | boolean guestToRemove = false; |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1292 | Bundle restrictions = new Bundle(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1293 | |
| 1294 | FileInputStream fis = null; |
| 1295 | try { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1296 | AtomicFile userFile = |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1297 | new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX)); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1298 | fis = userFile.openRead(); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1299 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1300 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1301 | int type; |
| 1302 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 1303 | && type != XmlPullParser.END_DOCUMENT) { |
| 1304 | ; |
| 1305 | } |
| 1306 | |
| 1307 | if (type != XmlPullParser.START_TAG) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1308 | Slog.e(LOG_TAG, "Unable to read user " + id); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1309 | return null; |
| 1310 | } |
| 1311 | |
| 1312 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) { |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1313 | int storedId = readIntAttribute(parser, ATTR_ID, -1); |
| 1314 | if (storedId != id) { |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1315 | Slog.e(LOG_TAG, "User id does not match the file name"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1316 | return null; |
| 1317 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1318 | serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id); |
| 1319 | flags = readIntAttribute(parser, ATTR_FLAGS, 0); |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1320 | iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1321 | creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0); |
| 1322 | lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1323 | profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, |
| 1324 | UserInfo.NO_PROFILE_GROUP_ID); |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1325 | restrictedProfileParentId = readIntAttribute(parser, |
| 1326 | ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1327 | String valueString = parser.getAttributeValue(null, ATTR_PARTIAL); |
| 1328 | if ("true".equals(valueString)) { |
| 1329 | partial = true; |
| 1330 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1331 | valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE); |
| 1332 | if ("true".equals(valueString)) { |
| 1333 | guestToRemove = true; |
| 1334 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1335 | |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1336 | int outerDepth = parser.getDepth(); |
| 1337 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT |
| 1338 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1339 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1340 | continue; |
| 1341 | } |
| 1342 | String tag = parser.getName(); |
| 1343 | if (TAG_NAME.equals(tag)) { |
| 1344 | type = parser.next(); |
| 1345 | if (type == XmlPullParser.TEXT) { |
| 1346 | name = parser.getText(); |
| 1347 | } |
| 1348 | } else if (TAG_RESTRICTIONS.equals(tag)) { |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 1349 | UserRestrictionsUtils.readRestrictions(parser, restrictions); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1353 | |
Amith Yamasani | b8151ec | 2012-04-18 18:02:48 -0700 | [diff] [blame] | 1354 | UserInfo userInfo = new UserInfo(id, name, iconPath, flags); |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1355 | userInfo.serialNumber = serialNumber; |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1356 | userInfo.creationTime = creationTime; |
| 1357 | userInfo.lastLoggedInTime = lastLoggedInTime; |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1358 | userInfo.partial = partial; |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1359 | userInfo.guestToRemove = guestToRemove; |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1360 | userInfo.profileGroupId = profileGroupId; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1361 | userInfo.restrictedProfileParentId = restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1362 | synchronized (mRestrictionsLock) { |
| 1363 | mBaseUserRestrictions.append(id, restrictions); |
| 1364 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1365 | return userInfo; |
| 1366 | |
| 1367 | } catch (IOException ioe) { |
| 1368 | } catch (XmlPullParserException pe) { |
Dianne Hackborn | bfd89b3 | 2011-12-15 18:22:54 -0800 | [diff] [blame] | 1369 | } finally { |
| 1370 | if (fis != null) { |
| 1371 | try { |
| 1372 | fis.close(); |
| 1373 | } catch (IOException e) { |
| 1374 | } |
| 1375 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1376 | } |
| 1377 | return null; |
| 1378 | } |
| 1379 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 1380 | private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) { |
| 1381 | String valueString = parser.getAttributeValue(null, attr); |
| 1382 | if (valueString == null) return defaultValue; |
| 1383 | try { |
| 1384 | return Integer.parseInt(valueString); |
| 1385 | } catch (NumberFormatException nfe) { |
| 1386 | return defaultValue; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) { |
| 1391 | String valueString = parser.getAttributeValue(null, attr); |
| 1392 | if (valueString == null) return defaultValue; |
| 1393 | try { |
| 1394 | return Long.parseLong(valueString); |
| 1395 | } catch (NumberFormatException nfe) { |
| 1396 | return defaultValue; |
| 1397 | } |
| 1398 | } |
| 1399 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1400 | private boolean isPackageInstalled(String pkg, int userId) { |
| 1401 | final ApplicationInfo info = mPm.getApplicationInfo(pkg, |
| 1402 | PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1403 | userId); |
| 1404 | if (info == null || (info.flags&ApplicationInfo.FLAG_INSTALLED) == 0) { |
| 1405 | return false; |
| 1406 | } |
| 1407 | return true; |
| 1408 | } |
| 1409 | |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1410 | /** |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1411 | * Removes all the restrictions files (res_<packagename>) for a given user. |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1412 | * Does not do any permissions checking. |
| 1413 | */ |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1414 | private void cleanAppRestrictions(int userId) { |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1415 | synchronized (mPackagesLock) { |
| 1416 | File dir = Environment.getUserSystemDirectory(userId); |
| 1417 | String[] files = dir.list(); |
| 1418 | if (files == null) return; |
| 1419 | for (String fileName : files) { |
| 1420 | if (fileName.startsWith(RESTRICTIONS_FILE_PREFIX)) { |
| 1421 | File resFile = new File(dir, fileName); |
| 1422 | if (resFile.exists()) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1423 | resFile.delete(); |
Amith Yamasani | b82add2 | 2013-07-09 11:24:44 -0700 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1430 | /** |
| 1431 | * Removes the app restrictions file for a specific package and user id, if it exists. |
| 1432 | */ |
| 1433 | private void cleanAppRestrictionsForPackage(String pkg, int userId) { |
| 1434 | synchronized (mPackagesLock) { |
| 1435 | File dir = Environment.getUserSystemDirectory(userId); |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1436 | File resFile = new File(dir, packageToRestrictionsFileName(pkg)); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1437 | if (resFile.exists()) { |
| 1438 | resFile.delete(); |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1443 | @Override |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1444 | public UserInfo createProfileForUser(String name, int flags, int userId) { |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1445 | checkManageUsersPermission("Only the system can create users"); |
Kenny Guy | 2a76494 | 2014-04-02 13:29:20 +0100 | [diff] [blame] | 1446 | return createUserInternal(name, flags, userId); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1449 | @Override |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1450 | public UserInfo createUser(String name, int flags) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1451 | checkManageUsersPermission("Only the system can create users"); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1452 | return createUserInternal(name, flags, UserHandle.USER_NULL); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1453 | } |
Amith Yamasani | faea76f | 2012-09-11 10:59:48 -0700 | [diff] [blame] | 1454 | |
Jessica Hummel | be81c80 | 2014-04-22 15:49:22 +0100 | [diff] [blame] | 1455 | private UserInfo createUserInternal(String name, int flags, int parentId) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1456 | if (hasUserRestriction(UserManager.DISALLOW_ADD_USER, UserHandle.getCallingUserId())) { |
Julia Reynolds | 7517502 | 2014-06-26 16:35:00 -0400 | [diff] [blame] | 1457 | Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled."); |
| 1458 | return null; |
| 1459 | } |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1460 | if (ActivityManager.isLowRamDeviceStatic()) { |
| 1461 | return null; |
| 1462 | } |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1463 | final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0; |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1464 | final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0; |
Fyodor Kupolov | 06a484a | 2015-08-21 16:33:20 -0700 | [diff] [blame] | 1465 | final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1466 | final long ident = Binder.clearCallingIdentity(); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1467 | UserInfo userInfo; |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 1468 | final int userId; |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1469 | try { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1470 | synchronized (mPackagesLock) { |
| 1471 | UserInfo parent = null; |
| 1472 | if (parentId != UserHandle.USER_NULL) { |
| 1473 | synchronized (mUsersLock) { |
| 1474 | parent = getUserInfoLU(parentId); |
Nicolas Prevot | c6d033e | 2014-02-27 13:11:09 +0000 | [diff] [blame] | 1475 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1476 | if (parent == null) return null; |
| 1477 | } |
| 1478 | if (isManagedProfile && !canAddMoreManagedProfiles(parentId, false)) { |
| 1479 | Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId); |
| 1480 | return null; |
| 1481 | } |
| 1482 | if (!isGuest && !isManagedProfile && isUserLimitReached()) { |
| 1483 | // If we're not adding a guest user or a managed profile and the limit has |
| 1484 | // been reached, cannot add a user. |
| 1485 | return null; |
| 1486 | } |
| 1487 | // If we're adding a guest and there already exists one, bail. |
| 1488 | if (isGuest && findCurrentGuestUser() != null) { |
| 1489 | return null; |
| 1490 | } |
| 1491 | // In legacy mode, restricted profile's parent can only be the owner user |
| 1492 | if (isRestricted && !UserManager.isSplitSystemUser() |
| 1493 | && (parentId != UserHandle.USER_SYSTEM)) { |
| 1494 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be owner"); |
| 1495 | return null; |
| 1496 | } |
| 1497 | if (isRestricted && UserManager.isSplitSystemUser()) { |
| 1498 | if (parent == null) { |
| 1499 | Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be " |
| 1500 | + "specified"); |
Nicolas Prevot | 12678a9 | 2015-05-13 12:15:03 -0700 | [diff] [blame] | 1501 | return null; |
| 1502 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1503 | if (!parent.canHaveProfile()) { |
| 1504 | Log.w(LOG_TAG, "Cannot add restricted profile - profiles cannot be " |
| 1505 | + "created for the specified parent user id " + parentId); |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1506 | return null; |
| 1507 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1508 | } |
| 1509 | // In split system user mode, we assign the first human user the primary flag. |
| 1510 | // And if there is no device owner, we also assign the admin flag to primary user. |
| 1511 | if (UserManager.isSplitSystemUser() |
| 1512 | && !isGuest && !isManagedProfile && getPrimaryUser() == null) { |
| 1513 | flags |= UserInfo.FLAG_PRIMARY; |
| 1514 | DevicePolicyManager devicePolicyManager = (DevicePolicyManager) |
| 1515 | mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 1516 | if (devicePolicyManager == null |
| 1517 | || devicePolicyManager.getDeviceOwner() == null) { |
| 1518 | flags |= UserInfo.FLAG_ADMIN; |
Amith Yamasani | 95ab784 | 2014-08-11 17:09:26 -0700 | [diff] [blame] | 1519 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1520 | } |
| 1521 | userId = getNextAvailableId(); |
| 1522 | userInfo = new UserInfo(userId, name, null, flags); |
| 1523 | userInfo.serialNumber = mNextSerialNumber++; |
| 1524 | long now = System.currentTimeMillis(); |
| 1525 | userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0; |
| 1526 | userInfo.partial = true; |
| 1527 | Environment.getUserSystemDirectory(userInfo.id).mkdirs(); |
| 1528 | synchronized (mUsersLock) { |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1529 | mUsers.put(userId, userInfo); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1530 | } |
| 1531 | writeUserListLP(); |
| 1532 | if (parent != null) { |
| 1533 | if (isManagedProfile) { |
| 1534 | if (parent.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1535 | parent.profileGroupId = parent.id; |
| 1536 | writeUserLP(parent); |
Kenny Guy | a52dc3e | 2014-02-11 15:33:14 +0000 | [diff] [blame] | 1537 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1538 | userInfo.profileGroupId = parent.profileGroupId; |
| 1539 | } else if (isRestricted) { |
| 1540 | if (parent.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID) { |
| 1541 | parent.restrictedProfileParentId = parent.id; |
| 1542 | writeUserLP(parent); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 1543 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1544 | userInfo.restrictedProfileParentId = parent.restrictedProfileParentId; |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 1545 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1546 | } |
Dianne Hackborn | 4428e17 | 2012-08-24 17:43:05 -0700 | [diff] [blame] | 1547 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1548 | final StorageManager storage = mContext.getSystemService(StorageManager.class); |
| 1549 | storage.createUserKey(userId, userInfo.serialNumber); |
| 1550 | for (VolumeInfo vol : storage.getWritablePrivateVolumes()) { |
| 1551 | final String volumeUuid = vol.getFsUuid(); |
| 1552 | try { |
| 1553 | final File userDir = Environment.getDataUserDirectory(volumeUuid, userId); |
| 1554 | storage.prepareUserStorage(volumeUuid, userId, userInfo.serialNumber); |
| 1555 | enforceSerialNumber(userDir, userInfo.serialNumber); |
| 1556 | } catch (IOException e) { |
| 1557 | Log.wtf(LOG_TAG, "Failed to create user directory on " + volumeUuid, e); |
| 1558 | } |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1559 | } |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1560 | mPm.createNewUser(userId); |
| 1561 | userInfo.partial = false; |
| 1562 | synchronized (mPackagesLock) { |
| 1563 | writeUserLP(userInfo); |
| 1564 | } |
| 1565 | updateUserIds(); |
| 1566 | Bundle restrictions = new Bundle(); |
| 1567 | synchronized (mRestrictionsLock) { |
| 1568 | mBaseUserRestrictions.append(userId, restrictions); |
| 1569 | } |
| 1570 | mPm.newUserCreated(userId); |
| 1571 | Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED); |
| 1572 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId); |
| 1573 | mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1574 | android.Manifest.permission.MANAGE_USERS); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1575 | } finally { |
| 1576 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1577 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 1578 | return userInfo; |
| 1579 | } |
| 1580 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1581 | /** |
Fyodor Kupolov | 02cb6e7 | 2015-09-18 18:20:55 -0700 | [diff] [blame] | 1582 | * @hide |
| 1583 | */ |
| 1584 | public UserInfo createRestrictedProfile(String name, int parentUserId) { |
| 1585 | checkManageUsersPermission("setupRestrictedProfile"); |
| 1586 | final UserInfo user = createProfileForUser(name, UserInfo.FLAG_RESTRICTED, parentUserId); |
| 1587 | if (user == null) { |
| 1588 | return null; |
| 1589 | } |
| 1590 | setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user.id); |
| 1591 | // Change the setting before applying the DISALLOW_SHARE_LOCATION restriction, otherwise |
| 1592 | // the putIntForUser() will fail. |
| 1593 | android.provider.Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 1594 | android.provider.Settings.Secure.LOCATION_MODE, |
| 1595 | android.provider.Settings.Secure.LOCATION_MODE_OFF, user.id); |
| 1596 | setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user.id); |
| 1597 | return user; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1601 | * Find the current guest user. If the Guest user is partial, |
| 1602 | * 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] | 1603 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1604 | private UserInfo findCurrentGuestUser() { |
| 1605 | synchronized (mUsersLock) { |
| 1606 | final int size = mUsers.size(); |
| 1607 | for (int i = 0; i < size; i++) { |
| 1608 | final UserInfo user = mUsers.valueAt(i); |
| 1609 | if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) { |
| 1610 | return user; |
| 1611 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | return null; |
| 1615 | } |
| 1616 | |
| 1617 | /** |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1618 | * Mark this guest user for deletion to allow us to create another guest |
| 1619 | * and switch to that user before actually removing this guest. |
| 1620 | * @param userHandle the userid of the current guest |
| 1621 | * @return whether the user could be marked for deletion |
| 1622 | */ |
| 1623 | public boolean markGuestForDeletion(int userHandle) { |
| 1624 | checkManageUsersPermission("Only the system can remove users"); |
| 1625 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1626 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1627 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
| 1631 | long ident = Binder.clearCallingIdentity(); |
| 1632 | try { |
| 1633 | final UserInfo user; |
| 1634 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1635 | synchronized (mUsersLock) { |
| 1636 | user = mUsers.get(userHandle); |
| 1637 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
| 1638 | return false; |
| 1639 | } |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1640 | } |
| 1641 | if (!user.isGuest()) { |
| 1642 | return false; |
| 1643 | } |
Adam Lesinski | eddeb49 | 2014-09-08 17:50:03 -0700 | [diff] [blame] | 1644 | // We set this to a guest user that is to be removed. This is a temporary state |
| 1645 | // where we are allowed to add new Guest users, even if this one is still not |
| 1646 | // removed. This user will still show up in getUserInfo() calls. |
| 1647 | // If we don't get around to removing this Guest user, it will be purged on next |
| 1648 | // startup. |
| 1649 | user.guestToRemove = true; |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1650 | // Mark it as disabled, so that it isn't returned any more when |
| 1651 | // profiles are queried. |
| 1652 | user.flags |= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1653 | writeUserLP(user); |
Amith Yamasani | 1df1473 | 2014-08-29 21:37:27 -0700 | [diff] [blame] | 1654 | } |
| 1655 | } finally { |
| 1656 | Binder.restoreCallingIdentity(ident); |
| 1657 | } |
| 1658 | return true; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1662 | * Removes a user and all data directories created for that user. This method should be called |
| 1663 | * after the user's processes have been terminated. |
Dianne Hackborn | 10ad982 | 2014-03-17 11:28:36 -0700 | [diff] [blame] | 1664 | * @param userHandle the user's id |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 1665 | */ |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 1666 | public boolean removeUser(int userHandle) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1667 | checkManageUsersPermission("Only the system can remove users"); |
Julia Reynolds | 4ac5f85 | 2014-06-23 17:38:51 -0400 | [diff] [blame] | 1668 | if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean( |
| 1669 | UserManager.DISALLOW_REMOVE_USER, false)) { |
| 1670 | Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled."); |
| 1671 | return false; |
| 1672 | } |
| 1673 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1674 | long ident = Binder.clearCallingIdentity(); |
| 1675 | try { |
| 1676 | final UserInfo user; |
Fyodor Kupolov | 0df68cd | 2015-10-01 13:54:22 -0700 | [diff] [blame] | 1677 | int currentUser = ActivityManager.getCurrentUser(); |
| 1678 | if (currentUser == userHandle) { |
| 1679 | Log.w(LOG_TAG, "Current user cannot be removed"); |
| 1680 | return false; |
| 1681 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1682 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1683 | synchronized (mUsersLock) { |
| 1684 | user = mUsers.get(userHandle); |
| 1685 | if (userHandle == 0 || user == null || mRemovingUserIds.get(userHandle)) { |
| 1686 | return false; |
| 1687 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1688 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1689 | // We remember deleted user IDs to prevent them from being |
| 1690 | // reused during the current boot; they can still be reused |
| 1691 | // after a reboot. |
| 1692 | mRemovingUserIds.put(userHandle, true); |
| 1693 | } |
Jeff Sharkey | 6eb0939 | 2014-11-14 15:57:59 -0800 | [diff] [blame] | 1694 | |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1695 | try { |
| 1696 | mAppOpsService.removeUser(userHandle); |
| 1697 | } catch (RemoteException e) { |
| 1698 | Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e); |
| 1699 | } |
| 1700 | // Set this to a partially created user, so that the user will be purged |
| 1701 | // on next startup, in case the runtime stops now before stopping and |
| 1702 | // removing the user completely. |
| 1703 | user.partial = true; |
| 1704 | // Mark it as disabled, so that it isn't returned any more when |
| 1705 | // profiles are queried. |
| 1706 | user.flags |= UserInfo.FLAG_DISABLED; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1707 | writeUserLP(user); |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | if (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID |
| 1711 | && user.isManagedProfile()) { |
| 1712 | // Send broadcast to notify system that the user removed was a |
| 1713 | // managed user. |
| 1714 | sendProfileRemovedBroadcast(user.profileGroupId, user.id); |
| 1715 | } |
| 1716 | |
| 1717 | if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle); |
| 1718 | int res; |
| 1719 | try { |
| 1720 | res = ActivityManagerNative.getDefault().stopUser(userHandle, |
| 1721 | new IStopUserCallback.Stub() { |
| 1722 | @Override |
| 1723 | public void userStopped(int userId) { |
| 1724 | finishRemoveUser(userId); |
| 1725 | } |
| 1726 | @Override |
| 1727 | public void userStopAborted(int userId) { |
| 1728 | } |
| 1729 | }); |
| 1730 | } catch (RemoteException e) { |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1731 | return false; |
| 1732 | } |
Kenny Guy | ee58b4f | 2014-05-23 15:19:53 +0100 | [diff] [blame] | 1733 | return res == ActivityManager.USER_OP_SUCCESS; |
| 1734 | } finally { |
| 1735 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1736 | } |
Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1739 | void finishRemoveUser(final int userHandle) { |
Amith Yamasani | 1638931 | 2012-10-17 21:20:14 -0700 | [diff] [blame] | 1740 | if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1741 | // Let other services shutdown any activity and clean up their state before completely |
| 1742 | // wiping the user's system directory and removing from the user list |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1743 | long ident = Binder.clearCallingIdentity(); |
| 1744 | try { |
| 1745 | Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED); |
| 1746 | addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1747 | mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL, |
| 1748 | android.Manifest.permission.MANAGE_USERS, |
| 1749 | |
| 1750 | new BroadcastReceiver() { |
| 1751 | @Override |
| 1752 | public void onReceive(Context context, Intent intent) { |
| 1753 | if (DBG) { |
| 1754 | Slog.i(LOG_TAG, |
| 1755 | "USER_REMOVED broadcast sent, cleaning up user data " |
| 1756 | + userHandle); |
| 1757 | } |
| 1758 | new Thread() { |
| 1759 | public void run() { |
Amith Yamasani | 515d406 | 2015-09-28 11:30:06 -0700 | [diff] [blame] | 1760 | // Clean up any ActivityManager state |
| 1761 | LocalServices.getService(ActivityManagerInternal.class) |
| 1762 | .onUserRemoved(userHandle); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1763 | removeUserState(userHandle); |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 1764 | } |
| 1765 | }.start(); |
| 1766 | } |
| 1767 | }, |
| 1768 | |
| 1769 | null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | 5dc5a00 | 2012-09-15 19:33:48 -0700 | [diff] [blame] | 1770 | } finally { |
| 1771 | Binder.restoreCallingIdentity(ident); |
| 1772 | } |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1775 | private void removeUserState(final int userHandle) { |
Jeff Sharkey | f9fc6d6 | 2015-11-08 16:46:05 -0800 | [diff] [blame] | 1776 | mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1777 | // Cleanup package manager settings |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1778 | mPm.cleanUpUser(this, userHandle); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1779 | |
| 1780 | // Remove this user from the list |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1781 | synchronized (mUsersLock) { |
| 1782 | mUsers.remove(userHandle); |
| 1783 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1784 | // Remove user file |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1785 | AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX)); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1786 | userFile.delete(); |
| 1787 | // Update the user list |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 1788 | synchronized (mPackagesLock) { |
| 1789 | writeUserListLP(); |
| 1790 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1791 | updateUserIds(); |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 1792 | removeDirectoryRecursive(Environment.getUserSystemDirectory(userHandle)); |
| 1793 | } |
| 1794 | |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 1795 | private void removeDirectoryRecursive(File parent) { |
| 1796 | if (parent.isDirectory()) { |
| 1797 | String[] files = parent.list(); |
| 1798 | for (String filename : files) { |
| 1799 | File child = new File(parent, filename); |
| 1800 | removeDirectoryRecursive(child); |
| 1801 | } |
| 1802 | } |
| 1803 | parent.delete(); |
| 1804 | } |
| 1805 | |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 1806 | private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) { |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1807 | Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
Adam Connors | d4b584e | 2014-06-09 13:55:47 +0100 | [diff] [blame] | 1808 | managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 1809 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | f8d3a23 | 2014-05-15 16:09:52 +0100 | [diff] [blame] | 1810 | managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId)); |
| 1811 | mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null); |
Adam Connors | 7b66ed5 | 2014-04-14 11:58:10 +0100 | [diff] [blame] | 1812 | } |
| 1813 | |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 1814 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1815 | public Bundle getApplicationRestrictions(String packageName) { |
| 1816 | return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId()); |
| 1817 | } |
| 1818 | |
| 1819 | @Override |
| 1820 | public Bundle getApplicationRestrictionsForUser(String packageName, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1821 | if (UserHandle.getCallingUserId() != userId |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1822 | || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) { |
Esteban Talavera | 2a5d6c6 | 2015-10-21 10:55:56 +0100 | [diff] [blame] | 1823 | checkManageUsersPermission("get application restrictions for other users/apps"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1824 | } |
| 1825 | synchronized (mPackagesLock) { |
| 1826 | // Read the restrictions from XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1827 | return readApplicationRestrictionsLP(packageName, userId); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | @Override |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1832 | public void setApplicationRestrictions(String packageName, Bundle restrictions, |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1833 | int userId) { |
Esteban Talavera | 2a5d6c6 | 2015-10-21 10:55:56 +0100 | [diff] [blame] | 1834 | checkManageUsersPermission("set application restrictions"); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1835 | synchronized (mPackagesLock) { |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1836 | if (restrictions == null || restrictions.isEmpty()) { |
| 1837 | cleanAppRestrictionsForPackage(packageName, userId); |
| 1838 | } else { |
| 1839 | // Write the restrictions to XML |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1840 | writeApplicationRestrictionsLP(packageName, restrictions, userId); |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1841 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1842 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 1843 | |
Kenny Guy | d21b218 | 2014-07-17 16:38:55 +0100 | [diff] [blame] | 1844 | if (isPackageInstalled(packageName, userId)) { |
| 1845 | // Notify package of changes via an intent - only sent to explicitly registered receivers. |
| 1846 | Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED); |
| 1847 | changeIntent.setPackage(packageName); |
| 1848 | changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 1849 | mContext.sendBroadcastAsUser(changeIntent, new UserHandle(userId)); |
| 1850 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1851 | } |
| 1852 | |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1853 | private void unhideAllInstalledAppsForUser(final int userHandle) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1854 | mHandler.post(new Runnable() { |
| 1855 | @Override |
| 1856 | public void run() { |
| 1857 | List<ApplicationInfo> apps = |
| 1858 | mPm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES, |
| 1859 | userHandle).getList(); |
| 1860 | final long ident = Binder.clearCallingIdentity(); |
| 1861 | try { |
| 1862 | for (ApplicationInfo appInfo : apps) { |
| 1863 | if ((appInfo.flags & ApplicationInfo.FLAG_INSTALLED) != 0 |
Alex Klyubin | b9f8a52 | 2015-02-03 11:12:59 -0800 | [diff] [blame] | 1864 | && (appInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_HIDDEN) |
| 1865 | != 0) { |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 1866 | mPm.setApplicationHiddenSettingAsUser(appInfo.packageName, false, |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 1867 | userHandle); |
| 1868 | } |
| 1869 | } |
| 1870 | } finally { |
| 1871 | Binder.restoreCallingIdentity(ident); |
| 1872 | } |
| 1873 | } |
| 1874 | }); |
| 1875 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1876 | private int getUidForPackage(String packageName) { |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1877 | long ident = Binder.clearCallingIdentity(); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1878 | try { |
| 1879 | return mContext.getPackageManager().getApplicationInfo(packageName, |
| 1880 | PackageManager.GET_UNINSTALLED_PACKAGES).uid; |
| 1881 | } catch (NameNotFoundException nnfe) { |
| 1882 | return -1; |
Amith Yamasani | 9429afb | 2013-04-10 18:40:51 -0700 | [diff] [blame] | 1883 | } finally { |
| 1884 | Binder.restoreCallingIdentity(ident); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1888 | private Bundle readApplicationRestrictionsLP(String packageName, int userId) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1889 | AtomicFile restrictionsFile = |
| 1890 | new AtomicFile(new File(Environment.getUserSystemDirectory(userId), |
| 1891 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1892 | return readApplicationRestrictionsLP(restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1896 | static Bundle readApplicationRestrictionsLP(AtomicFile restrictionsFile) { |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1897 | final Bundle restrictions = new Bundle(); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1898 | final ArrayList<String> values = new ArrayList<>(); |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 1899 | if (!restrictionsFile.getBaseFile().exists()) { |
Fyodor Kupolov | f6ee224 | 2015-04-06 10:15:07 -0700 | [diff] [blame] | 1900 | return restrictions; |
Fyodor Kupolov | 6f34d36 | 2015-04-02 12:42:13 -0700 | [diff] [blame] | 1901 | } |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1902 | |
| 1903 | FileInputStream fis = null; |
| 1904 | try { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1905 | fis = restrictionsFile.openRead(); |
| 1906 | XmlPullParser parser = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1907 | parser.setInput(fis, StandardCharsets.UTF_8.name()); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1908 | XmlUtils.nextElement(parser); |
| 1909 | if (parser.getEventType() != XmlPullParser.START_TAG) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1910 | Slog.e(LOG_TAG, "Unable to read restrictions file " |
| 1911 | + restrictionsFile.getBaseFile()); |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1912 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1913 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1914 | while (parser.next() != XmlPullParser.END_DOCUMENT) { |
| 1915 | readEntry(restrictions, values, parser); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1916 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1917 | } catch (IOException|XmlPullParserException e) { |
| 1918 | Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1919 | } finally { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1920 | IoUtils.closeQuietly(fis); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1921 | } |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1922 | return restrictions; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1923 | } |
| 1924 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1925 | private static void readEntry(Bundle restrictions, ArrayList<String> values, |
| 1926 | XmlPullParser parser) throws XmlPullParserException, IOException { |
| 1927 | int type = parser.getEventType(); |
| 1928 | if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) { |
| 1929 | String key = parser.getAttributeValue(null, ATTR_KEY); |
| 1930 | String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE); |
| 1931 | String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE); |
| 1932 | if (multiple != null) { |
| 1933 | values.clear(); |
| 1934 | int count = Integer.parseInt(multiple); |
| 1935 | while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1936 | if (type == XmlPullParser.START_TAG |
| 1937 | && parser.getName().equals(TAG_VALUE)) { |
| 1938 | values.add(parser.nextText().trim()); |
| 1939 | count--; |
| 1940 | } |
| 1941 | } |
| 1942 | String [] valueStrings = new String[values.size()]; |
| 1943 | values.toArray(valueStrings); |
| 1944 | restrictions.putStringArray(key, valueStrings); |
| 1945 | } else if (ATTR_TYPE_BUNDLE.equals(valType)) { |
| 1946 | restrictions.putBundle(key, readBundleEntry(parser, values)); |
| 1947 | } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) { |
| 1948 | final int outerDepth = parser.getDepth(); |
| 1949 | ArrayList<Bundle> bundleList = new ArrayList<>(); |
| 1950 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1951 | Bundle childBundle = readBundleEntry(parser, values); |
| 1952 | bundleList.add(childBundle); |
| 1953 | } |
| 1954 | restrictions.putParcelableArray(key, |
| 1955 | bundleList.toArray(new Bundle[bundleList.size()])); |
| 1956 | } else { |
| 1957 | String value = parser.nextText().trim(); |
| 1958 | if (ATTR_TYPE_BOOLEAN.equals(valType)) { |
| 1959 | restrictions.putBoolean(key, Boolean.parseBoolean(value)); |
| 1960 | } else if (ATTR_TYPE_INTEGER.equals(valType)) { |
| 1961 | restrictions.putInt(key, Integer.parseInt(value)); |
| 1962 | } else { |
| 1963 | restrictions.putString(key, value); |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values) |
| 1970 | throws IOException, XmlPullParserException { |
| 1971 | Bundle childBundle = new Bundle(); |
| 1972 | final int outerDepth = parser.getDepth(); |
| 1973 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1974 | readEntry(childBundle, values, parser); |
| 1975 | } |
| 1976 | return childBundle; |
| 1977 | } |
| 1978 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1979 | private void writeApplicationRestrictionsLP(String packageName, |
Amith Yamasani | 7e99bc0 | 2013-04-16 18:24:51 -0700 | [diff] [blame] | 1980 | Bundle restrictions, int userId) { |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1981 | AtomicFile restrictionsFile = new AtomicFile( |
| 1982 | new File(Environment.getUserSystemDirectory(userId), |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 1983 | packageToRestrictionsFileName(packageName))); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1984 | writeApplicationRestrictionsLP(restrictions, restrictionsFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | @VisibleForTesting |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 1988 | static void writeApplicationRestrictionsLP(Bundle restrictions, AtomicFile restrictionsFile) { |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1989 | FileOutputStream fos = null; |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1990 | try { |
| 1991 | fos = restrictionsFile.startWrite(); |
| 1992 | final BufferedOutputStream bos = new BufferedOutputStream(fos); |
| 1993 | |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1994 | final XmlSerializer serializer = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 1995 | serializer.setOutput(bos, StandardCharsets.UTF_8.name()); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 1996 | serializer.startDocument(null, true); |
| 1997 | serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); |
| 1998 | |
| 1999 | serializer.startTag(null, TAG_RESTRICTIONS); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2000 | writeBundle(restrictions, serializer); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2001 | serializer.endTag(null, TAG_RESTRICTIONS); |
| 2002 | |
| 2003 | serializer.endDocument(); |
| 2004 | restrictionsFile.finishWrite(fos); |
| 2005 | } catch (Exception e) { |
| 2006 | restrictionsFile.failWrite(fos); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 2007 | Slog.e(LOG_TAG, "Error writing application restrictions list", e); |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | private static void writeBundle(Bundle restrictions, XmlSerializer serializer) |
| 2012 | throws IOException { |
| 2013 | for (String key : restrictions.keySet()) { |
| 2014 | Object value = restrictions.get(key); |
| 2015 | serializer.startTag(null, TAG_ENTRY); |
| 2016 | serializer.attribute(null, ATTR_KEY, key); |
| 2017 | |
| 2018 | if (value instanceof Boolean) { |
| 2019 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN); |
| 2020 | serializer.text(value.toString()); |
| 2021 | } else if (value instanceof Integer) { |
| 2022 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER); |
| 2023 | serializer.text(value.toString()); |
| 2024 | } else if (value == null || value instanceof String) { |
| 2025 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING); |
| 2026 | serializer.text(value != null ? (String) value : ""); |
| 2027 | } else if (value instanceof Bundle) { |
| 2028 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2029 | writeBundle((Bundle) value, serializer); |
| 2030 | } else if (value instanceof Parcelable[]) { |
| 2031 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY); |
| 2032 | Parcelable[] array = (Parcelable[]) value; |
| 2033 | for (Parcelable parcelable : array) { |
| 2034 | if (!(parcelable instanceof Bundle)) { |
| 2035 | throw new IllegalArgumentException("bundle-array can only hold Bundles"); |
| 2036 | } |
| 2037 | serializer.startTag(null, TAG_ENTRY); |
| 2038 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE); |
| 2039 | writeBundle((Bundle) parcelable, serializer); |
| 2040 | serializer.endTag(null, TAG_ENTRY); |
| 2041 | } |
| 2042 | } else { |
| 2043 | serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY); |
| 2044 | String[] values = (String[]) value; |
| 2045 | serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length)); |
| 2046 | for (String choice : values) { |
| 2047 | serializer.startTag(null, TAG_VALUE); |
| 2048 | serializer.text(choice != null ? choice : ""); |
| 2049 | serializer.endTag(null, TAG_VALUE); |
| 2050 | } |
| 2051 | } |
| 2052 | serializer.endTag(null, TAG_ENTRY); |
Amith Yamasani | df2e92a | 2013-03-01 17:04:38 -0800 | [diff] [blame] | 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | @Override |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2057 | public int getUserSerialNumber(int userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2058 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2059 | if (!exists(userHandle)) return -1; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2060 | return getUserInfoLU(userHandle).serialNumber; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | @Override |
| 2065 | public int getUserHandle(int userSerialNumber) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2066 | synchronized (mUsersLock) { |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2067 | for (int userId : mUserIds) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2068 | UserInfo info = getUserInfoLU(userId); |
Kenny Guy | 945f883 | 2015-02-10 15:17:26 +0000 | [diff] [blame] | 2069 | if (info != null && info.serialNumber == userSerialNumber) return userId; |
Amith Yamasani | 2a00329 | 2012-08-14 18:25:45 -0700 | [diff] [blame] | 2070 | } |
| 2071 | // Not found |
| 2072 | return -1; |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2076 | @Override |
| 2077 | public long getUserCreationTime(int userHandle) { |
| 2078 | int callingUserId = UserHandle.getCallingUserId(); |
| 2079 | UserInfo userInfo = null; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2080 | synchronized (mUsersLock) { |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2081 | if (callingUserId == userHandle) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2082 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2083 | } else { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2084 | UserInfo parent = getProfileParentLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2085 | if (parent != null && parent.id == callingUserId) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2086 | userInfo = getUserInfoLU(userHandle); |
Fyodor Kupolov | ff7233e | 2015-04-08 11:28:52 -0700 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | if (userInfo == null) { |
| 2091 | throw new SecurityException("userHandle can only be the calling user or a managed " |
| 2092 | + "profile associated with this user"); |
| 2093 | } |
| 2094 | return userInfo.creationTime; |
| 2095 | } |
| 2096 | |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2097 | /** |
| 2098 | * Caches the list of user ids in an array, adjusting the array size when necessary. |
| 2099 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2100 | private void updateUserIds() { |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2101 | int num = 0; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2102 | synchronized (mUsersLock) { |
| 2103 | final int userSize = mUsers.size(); |
| 2104 | for (int i = 0; i < userSize; i++) { |
| 2105 | if (!mUsers.valueAt(i).partial) { |
| 2106 | num++; |
| 2107 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2108 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2109 | final int[] newUsers = new int[num]; |
| 2110 | int n = 0; |
| 2111 | for (int i = 0; i < userSize; i++) { |
| 2112 | if (!mUsers.valueAt(i).partial) { |
| 2113 | newUsers[n++] = mUsers.keyAt(i); |
| 2114 | } |
Dianne Hackborn | d4ac8d7 | 2012-09-27 23:20:10 -0700 | [diff] [blame] | 2115 | } |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2116 | mUserIds = newUsers; |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /** |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 2121 | * 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] | 2122 | * @param userId the user that was just foregrounded |
| 2123 | */ |
Amith Yamasani | 06bf824 | 2015-05-08 16:36:21 -0700 | [diff] [blame] | 2124 | public void onUserForeground(int userId) { |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 2125 | UserInfo user = getUserInfoNoChecks(userId); |
| 2126 | if (user == null || user.partial) { |
| 2127 | Slog.w(LOG_TAG, "userForeground: unknown user #" + userId); |
| 2128 | return; |
| 2129 | } |
| 2130 | long now = System.currentTimeMillis(); |
| 2131 | if (now > EPOCH_PLUS_30_YEARS) { |
| 2132 | user.lastLoggedInTime = now; |
| 2133 | scheduleWriteUser(user); |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | /** |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2138 | * 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] | 2139 | * TODO: May not be a good idea to recycle ids, in case it results in confusion |
| 2140 | * for data and battery stats collection, or unexpected cross-talk. |
Amith Yamasani | 0b28549 | 2011-04-14 17:35:23 -0700 | [diff] [blame] | 2141 | */ |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2142 | private int getNextAvailableId() { |
| 2143 | synchronized (mUsersLock) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2144 | int i = MIN_USER_ID; |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2145 | while (i < MAX_USER_ID) { |
Jeff Sharkey | ffe0cb4 | 2012-11-05 17:24:43 -0800 | [diff] [blame] | 2146 | if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) { |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2147 | return i; |
Amith Yamasani | 19526374 | 2012-08-21 15:40:12 -0700 | [diff] [blame] | 2148 | } |
| 2149 | i++; |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2150 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2151 | } |
Xiaohui Chen | 621b3fc | 2015-10-02 14:41:42 -0700 | [diff] [blame] | 2152 | throw new IllegalStateException("No user id available!"); |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2153 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2154 | |
Amith Yamasani | fc95e70 | 2013-09-26 13:20:17 -0700 | [diff] [blame] | 2155 | private String packageToRestrictionsFileName(String packageName) { |
| 2156 | return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX; |
| 2157 | } |
| 2158 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2159 | /** |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 2160 | * Enforce that serial number stored in user directory inode matches the |
| 2161 | * given expected value. Gracefully sets the serial number if currently |
| 2162 | * undefined. |
| 2163 | * |
| 2164 | * @throws IOException when problem extracting serial number, or serial |
| 2165 | * number is mismatched. |
| 2166 | */ |
| 2167 | public static void enforceSerialNumber(File file, int serialNumber) throws IOException { |
| 2168 | final int foundSerial = getSerialNumber(file); |
| 2169 | Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial); |
| 2170 | |
| 2171 | if (foundSerial == -1) { |
| 2172 | Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid"); |
| 2173 | try { |
| 2174 | setSerialNumber(file, serialNumber); |
| 2175 | } catch (IOException e) { |
| 2176 | Slog.w(LOG_TAG, "Failed to set serial number on " + file, e); |
| 2177 | } |
| 2178 | |
| 2179 | } else if (foundSerial != serialNumber) { |
| 2180 | throw new IOException("Found serial number " + foundSerial |
| 2181 | + " doesn't match expected " + serialNumber); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | /** |
| 2186 | * Set serial number stored in user directory inode. |
| 2187 | * |
| 2188 | * @throws IOException if serial number was already set |
| 2189 | */ |
| 2190 | private static void setSerialNumber(File file, int serialNumber) |
| 2191 | throws IOException { |
| 2192 | try { |
| 2193 | final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8); |
| 2194 | Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE); |
| 2195 | } catch (ErrnoException e) { |
| 2196 | throw e.rethrowAsIOException(); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | /** |
| 2201 | * Return serial number stored in user directory inode. |
| 2202 | * |
| 2203 | * @return parsed serial number, or -1 if not set |
| 2204 | */ |
| 2205 | private static int getSerialNumber(File file) throws IOException { |
| 2206 | try { |
| 2207 | final byte[] buf = new byte[256]; |
| 2208 | final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf); |
| 2209 | final String serial = new String(buf, 0, len); |
| 2210 | try { |
| 2211 | return Integer.parseInt(serial); |
| 2212 | } catch (NumberFormatException e) { |
| 2213 | throw new IOException("Bad serial number: " + serial); |
| 2214 | } |
| 2215 | } catch (ErrnoException e) { |
| 2216 | if (e.errno == OsConstants.ENODATA) { |
| 2217 | return -1; |
| 2218 | } else { |
| 2219 | throw e.rethrowAsIOException(); |
| 2220 | } |
| 2221 | } |
| 2222 | } |
| 2223 | |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2224 | @Override |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2225 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 2226 | FileDescriptor err, String[] args, ResultReceiver resultReceiver) { |
| 2227 | (new Shell()).exec(this, in, out, err, args, resultReceiver); |
| 2228 | } |
| 2229 | |
| 2230 | int onShellCommand(Shell shell, String cmd) { |
| 2231 | if (cmd == null) { |
| 2232 | return shell.handleDefaultCommands(cmd); |
| 2233 | } |
| 2234 | |
| 2235 | final PrintWriter pw = shell.getOutPrintWriter(); |
| 2236 | try { |
| 2237 | switch(cmd) { |
| 2238 | case "list": |
| 2239 | return runList(pw); |
| 2240 | } |
| 2241 | } catch (RemoteException e) { |
| 2242 | pw.println("Remote exception: " + e); |
| 2243 | } |
| 2244 | return -1; |
| 2245 | } |
| 2246 | |
| 2247 | private int runList(PrintWriter pw) throws RemoteException { |
| 2248 | final IActivityManager am = ActivityManagerNative.getDefault(); |
| 2249 | final List<UserInfo> users = getUsers(false); |
| 2250 | if (users == null) { |
| 2251 | pw.println("Error: couldn't get users"); |
| 2252 | return 1; |
| 2253 | } else { |
| 2254 | pw.println("Users:"); |
| 2255 | for (int i = 0; i < users.size(); i++) { |
Jeff Sharkey | e17ac15 | 2015-11-06 22:40:29 -0800 | [diff] [blame] | 2256 | String running = am.isUserRunning(users.get(i).id, 0) ? " running" : ""; |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2257 | pw.println("\t" + users.get(i).toString() + running); |
| 2258 | } |
| 2259 | return 0; |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | @Override |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2264 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2265 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2266 | != PackageManager.PERMISSION_GRANTED) { |
| 2267 | pw.println("Permission Denial: can't dump UserManager from from pid=" |
| 2268 | + Binder.getCallingPid() |
| 2269 | + ", uid=" + Binder.getCallingUid() |
| 2270 | + " without permission " |
| 2271 | + android.Manifest.permission.DUMP); |
| 2272 | return; |
| 2273 | } |
| 2274 | |
| 2275 | long now = System.currentTimeMillis(); |
| 2276 | StringBuilder sb = new StringBuilder(); |
| 2277 | synchronized (mPackagesLock) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2278 | synchronized (mUsersLock) { |
| 2279 | pw.println("Users:"); |
| 2280 | for (int i = 0; i < mUsers.size(); i++) { |
| 2281 | UserInfo user = mUsers.valueAt(i); |
| 2282 | if (user == null) { |
| 2283 | continue; |
| 2284 | } |
| 2285 | pw.print(" "); pw.print(user); |
| 2286 | pw.print(" serialNo="); pw.print(user.serialNumber); |
| 2287 | if (mRemovingUserIds.get(mUsers.keyAt(i))) { |
| 2288 | pw.print(" <removing> "); |
| 2289 | } |
| 2290 | if (user.partial) { |
| 2291 | pw.print(" <partial>"); |
| 2292 | } |
| 2293 | pw.println(); |
| 2294 | pw.print(" Created: "); |
| 2295 | if (user.creationTime == 0) { |
| 2296 | pw.println("<unknown>"); |
| 2297 | } else { |
| 2298 | sb.setLength(0); |
| 2299 | TimeUtils.formatDuration(now - user.creationTime, sb); |
| 2300 | sb.append(" ago"); |
| 2301 | pw.println(sb); |
| 2302 | } |
| 2303 | pw.print(" Last logged in: "); |
| 2304 | if (user.lastLoggedInTime == 0) { |
| 2305 | pw.println("<unknown>"); |
| 2306 | } else { |
| 2307 | sb.setLength(0); |
| 2308 | TimeUtils.formatDuration(now - user.lastLoggedInTime, sb); |
| 2309 | sb.append(" ago"); |
| 2310 | pw.println(sb); |
| 2311 | } |
| 2312 | pw.println(" Restrictions:"); |
| 2313 | synchronized (mRestrictionsLock) { |
| 2314 | UserRestrictionsUtils.dumpRestrictions( |
| 2315 | pw, " ", mBaseUserRestrictions.get(user.id)); |
| 2316 | pw.println(" Effective restrictions:"); |
| 2317 | UserRestrictionsUtils.dumpRestrictions( |
| 2318 | pw, " ", mCachedEffectiveUserRestrictions.get(user.id)); |
| 2319 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2320 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2321 | } |
Makoto Onuki | a4f1197 | 2015-10-01 13:19:58 -0700 | [diff] [blame] | 2322 | pw.println(); |
| 2323 | pw.println("Guest restrictions:"); |
Fyodor Kupolov | e80085d | 2015-11-06 18:21:39 -0800 | [diff] [blame^] | 2324 | synchronized (mGuestRestrictions) { |
| 2325 | UserRestrictionsUtils.dumpRestrictions(pw, " ", mGuestRestrictions); |
| 2326 | } |
Amith Yamasani | 920ace0 | 2012-09-20 22:15:37 -0700 | [diff] [blame] | 2327 | } |
| 2328 | } |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2329 | |
| 2330 | final class MainHandler extends Handler { |
| 2331 | |
| 2332 | @Override |
| 2333 | public void handleMessage(Message msg) { |
| 2334 | switch (msg.what) { |
| 2335 | case WRITE_USER_MSG: |
| 2336 | removeMessages(WRITE_USER_MSG, msg.obj); |
| 2337 | synchronized (mPackagesLock) { |
| 2338 | int userId = ((UserInfo) msg.obj).id; |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2339 | UserInfo userInfo = getUserInfoNoChecks(userId); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2340 | if (userInfo != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2341 | writeUserLP(userInfo); |
Fyodor Kupolov | 75d0ea8 | 2014-12-15 16:21:07 -0800 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | } |
Amith Yamasani | bb054c9 | 2015-07-09 14:16:27 -0700 | [diff] [blame] | 2347 | |
| 2348 | /** |
| 2349 | * @param userId |
| 2350 | * @return whether the user has been initialized yet |
| 2351 | */ |
| 2352 | boolean isInitialized(int userId) { |
| 2353 | return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0; |
| 2354 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2355 | |
| 2356 | private class LocalService extends UserManagerInternal { |
| 2357 | |
| 2358 | @Override |
| 2359 | public Object getUserRestrictionsLock() { |
| 2360 | return mRestrictionsLock; |
| 2361 | } |
| 2362 | |
| 2363 | @Override |
| 2364 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2365 | public void updateEffectiveUserRestrictionsLR(int userId) { |
| 2366 | UserManagerService.this.updateEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | @Override |
| 2370 | @GuardedBy("mRestrictionsLock") |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2371 | public void updateEffectiveUserRestrictionsForAllUsersLR() { |
| 2372 | UserManagerService.this.updateEffectiveUserRestrictionsForAllUsersLR(); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | @Override |
| 2376 | public Bundle getBaseUserRestrictions(int userId) { |
| 2377 | synchronized (mRestrictionsLock) { |
| 2378 | return mBaseUserRestrictions.get(userId); |
| 2379 | } |
| 2380 | } |
| 2381 | |
| 2382 | @Override |
| 2383 | public void setBaseUserRestrictionsByDpmsForMigration( |
| 2384 | int userId, Bundle baseRestrictions) { |
| 2385 | synchronized (mRestrictionsLock) { |
| 2386 | mBaseUserRestrictions.put(userId, new Bundle(baseRestrictions)); |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2387 | invalidateEffectiveUserRestrictionsLR(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2388 | } |
| 2389 | |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2390 | final UserInfo userInfo = getUserInfoNoChecks(userId); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2391 | synchronized (mPackagesLock) { |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2392 | if (userInfo != null) { |
Fyodor Kupolov | 8240275 | 2015-10-28 14:54:51 -0700 | [diff] [blame] | 2393 | writeUserLP(userInfo); |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2394 | } else { |
| 2395 | Slog.w(LOG_TAG, "UserInfo not found for " + userId); |
| 2396 | } |
| 2397 | } |
| 2398 | } |
Makoto Onuki | d45a4a2 | 2015-11-02 17:17:38 -0800 | [diff] [blame] | 2399 | |
| 2400 | @Override |
| 2401 | public boolean getUserRestriction(int userId, String key) { |
| 2402 | return getUserRestrictions(userId).getBoolean(key); |
| 2403 | } |
| 2404 | |
| 2405 | @Override |
| 2406 | public void addUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2407 | synchronized (mUserRestrictionsListeners) { |
| 2408 | mUserRestrictionsListeners.add(listener); |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | @Override |
| 2413 | public void removeUserRestrictionsListener(UserRestrictionsListener listener) { |
| 2414 | synchronized (mUserRestrictionsListeners) { |
| 2415 | mUserRestrictionsListeners.remove(listener); |
| 2416 | } |
| 2417 | } |
Makoto Onuki | 068c54a | 2015-10-13 14:34:03 -0700 | [diff] [blame] | 2418 | } |
Todd Kennedy | 60459ab | 2015-10-30 11:32:16 -0700 | [diff] [blame] | 2419 | |
| 2420 | private class Shell extends ShellCommand { |
| 2421 | @Override |
| 2422 | public int onCommand(String cmd) { |
| 2423 | return onShellCommand(this, cmd); |
| 2424 | } |
| 2425 | |
| 2426 | @Override |
| 2427 | public void onHelp() { |
| 2428 | final PrintWriter pw = getOutPrintWriter(); |
| 2429 | pw.println("User manager (user) commands:"); |
| 2430 | pw.println(" help"); |
| 2431 | pw.println(" Print this help text."); |
| 2432 | pw.println(""); |
| 2433 | pw.println(" list"); |
| 2434 | pw.println(" Prints all users on the system."); |
| 2435 | } |
| 2436 | } |
Amith Yamasani | 4b2e934 | 2011-03-31 12:38:53 -0700 | [diff] [blame] | 2437 | } |