blob: 5fd196b4eb5bf1fb4bc1b6d8867f17113858cfc7 [file] [log] [blame]
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001/*
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
17package com.android.server.pm;
18
Xiaohui Chenb3b92582015-12-07 11:22:13 -080019import android.Manifest;
Xiaohui Chen594f2082015-08-18 11:04:20 -070020import android.annotation.NonNull;
Makoto Onuki068c54a2015-10-13 14:34:03 -070021import android.annotation.Nullable;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070022import android.app.Activity;
Amith Yamasani2a003292012-08-14 18:25:45 -070023import android.app.ActivityManager;
Amith Yamasani515d4062015-09-28 11:30:06 -070024import android.app.ActivityManagerInternal;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070025import android.app.ActivityManagerNative;
Todd Kennedy60459ab2015-10-30 11:32:16 -070026import android.app.IActivityManager;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070027import android.app.IStopUserCallback;
Amith Yamasanidb6a14c2012-10-17 21:16:52 -070028import android.content.BroadcastReceiver;
Amith Yamasani258848d2012-08-10 17:06:33 -070029import android.content.Context;
30import android.content.Intent;
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +010031import android.content.IntentFilter;
Amith Yamasani1a7472e2013-07-02 11:17:30 -070032import android.content.pm.ApplicationInfo;
Amith Yamasani0b285492011-04-14 17:35:23 -070033import android.content.pm.PackageManager;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080034import android.content.pm.PackageManager.NameNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070035import android.content.pm.UserInfo;
Lenka Trochtova02fee152015-12-22 14:26:18 +010036import android.content.res.Resources;
Amith Yamasanie928d7d2012-09-17 21:46:51 -070037import android.graphics.Bitmap;
Amith Yamasani258848d2012-08-10 17:06:33 -070038import android.os.Binder;
Amith Yamasanie4cf7342012-12-17 11:12:09 -080039import android.os.Bundle;
Makoto Onuki1a2cd742015-11-16 13:51:27 -080040import android.os.Debug;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070041import android.os.Environment;
42import android.os.FileUtils;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080043import android.os.Handler;
Svet Ganov9cea80cd2016-02-16 11:47:00 -080044import android.os.IBinder;
Amith Yamasani258848d2012-08-10 17:06:33 -070045import android.os.IUserManager;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -080046import android.os.Message;
Adrian Roos1bdff912015-02-17 15:51:35 +010047import android.os.ParcelFileDescriptor;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070048import android.os.Parcelable;
Amith Yamasani12747872015-12-07 14:19:49 -080049import android.os.PersistableBundle;
Amith Yamasani258848d2012-08-10 17:06:33 -070050import android.os.Process;
Dianne Hackborn80a4af22012-08-27 19:18:31 -070051import android.os.RemoteException;
Todd Kennedy60459ab2015-10-30 11:32:16 -070052import android.os.ResultReceiver;
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +010053import android.os.SELinux;
Jason Monk62062992014-05-06 09:55:28 -040054import android.os.ServiceManager;
Todd Kennedy60459ab2015-10-30 11:32:16 -070055import android.os.ShellCommand;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070056import android.os.UserHandle;
Jeff Sharkey27bd34d2012-09-16 12:49:00 -070057import android.os.UserManager;
Makoto Onuki068c54a2015-10-13 14:34:03 -070058import android.os.UserManagerInternal;
Makoto Onukid45a4a22015-11-02 17:17:38 -080059import android.os.UserManagerInternal.UserRestrictionsListener;
Paul Crowley85e4e812015-05-19 12:42:00 +010060import android.os.storage.StorageManager;
Jeff Sharkey6dce4962015-07-03 18:08:41 -070061import android.os.storage.VolumeInfo;
62import android.system.ErrnoException;
63import android.system.Os;
64import android.system.OsConstants;
Amith Yamasani2a003292012-08-14 18:25:45 -070065import android.util.AtomicFile;
Amith Yamasani655d0e22013-06-12 14:19:10 -070066import android.util.Log;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070067import android.util.Slog;
68import android.util.SparseArray;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080069import android.util.SparseBooleanArray;
Amith Yamasani920ace02012-09-20 22:15:37 -070070import android.util.TimeUtils;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070071import android.util.Xml;
72
Makoto Onuki068c54a2015-10-13 14:34:03 -070073import com.android.internal.annotations.GuardedBy;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070074import com.android.internal.annotations.VisibleForTesting;
Jason Monk62062992014-05-06 09:55:28 -040075import com.android.internal.app.IAppOpsService;
Fyodor Kupolov8385e4b2015-12-29 18:15:32 -080076import com.android.internal.logging.MetricsLogger;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080077import com.android.internal.util.FastXmlSerializer;
Makoto Onuki068c54a2015-10-13 14:34:03 -070078import com.android.internal.util.Preconditions;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070079import com.android.internal.util.XmlUtils;
Clara Bayarri10ad84a2015-12-01 17:38:05 +000080import com.android.internal.widget.LockPatternUtils;
Amith Yamasani515d4062015-09-28 11:30:06 -070081import com.android.server.LocalServices;
Jeff Sharkey47f71082016-02-01 17:03:54 -070082
83import libcore.io.IoUtils;
84import libcore.util.Objects;
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -080085
86import org.xmlpull.v1.XmlPullParser;
87import org.xmlpull.v1.XmlPullParserException;
88import org.xmlpull.v1.XmlSerializer;
89
Amith Yamasani4b2e9342011-03-31 12:38:53 -070090import java.io.BufferedOutputStream;
91import java.io.File;
Amith Yamasani920ace02012-09-20 22:15:37 -070092import java.io.FileDescriptor;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070093import java.io.FileInputStream;
Amith Yamasanib8151ec2012-04-18 18:02:48 -070094import java.io.FileNotFoundException;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070095import java.io.FileOutputStream;
96import java.io.IOException;
Amith Yamasani920ace02012-09-20 22:15:37 -070097import java.io.PrintWriter;
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +010098import java.nio.charset.StandardCharsets;
Amith Yamasani4b2e9342011-03-31 12:38:53 -070099import java.util.ArrayList;
100import java.util.List;
101
Makoto Onuki068c54a2015-10-13 14:34:03 -0700102/**
103 * Service for {@link UserManager}.
104 *
105 * Method naming convention:
Fyodor Kupolov82402752015-10-28 14:54:51 -0700106 * <ul>
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800107 * <li> Methods suffixed with "LP" should be called within the {@link #mPackagesLock} lock.
Fyodor Kupolov82402752015-10-28 14:54:51 -0700108 * <li> Methods suffixed with "LR" should be called within the {@link #mRestrictionsLock} lock.
109 * <li> Methods suffixed with "LU" should be called within the {@link #mUsersLock} lock.
110 * </ul>
Makoto Onuki068c54a2015-10-13 14:34:03 -0700111 */
Amith Yamasani258848d2012-08-10 17:06:33 -0700112public class UserManagerService extends IUserManager.Stub {
Amith Yamasani2a003292012-08-14 18:25:45 -0700113 private static final String LOG_TAG = "UserManagerService";
Makoto Onuki81c61ea2016-01-22 11:22:26 -0800114 static final boolean DBG = false; // DO NOT SUBMIT WITH TRUE
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800115 private static final boolean DBG_WITH_STACKTRACE = false; // DO NOT SUBMIT WITH TRUE
Amith Yamasani16389312012-10-17 21:20:14 -0700116
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700117 private static final String TAG_NAME = "name";
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800118 private static final String TAG_ACCOUNT = "account";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700119 private static final String ATTR_FLAGS = "flags";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700120 private static final String ATTR_ICON_PATH = "icon";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700121 private static final String ATTR_ID = "id";
Amith Yamasani920ace02012-09-20 22:15:37 -0700122 private static final String ATTR_CREATION_TIME = "created";
123 private static final String ATTR_LAST_LOGGED_IN_TIME = "lastLoggedIn";
Amith Yamasani2a003292012-08-14 18:25:45 -0700124 private static final String ATTR_SERIAL_NO = "serialNumber";
125 private static final String ATTR_NEXT_SERIAL_NO = "nextSerialNumber";
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700126 private static final String ATTR_PARTIAL = "partial";
Adam Lesinskieddeb492014-09-08 17:50:03 -0700127 private static final String ATTR_GUEST_TO_REMOVE = "guestToRemove";
Amith Yamasani6f34b412012-10-22 18:19:27 -0700128 private static final String ATTR_USER_VERSION = "version";
Kenny Guy2a764942014-04-02 13:29:20 +0100129 private static final String ATTR_PROFILE_GROUP_ID = "profileGroupId";
Fyodor Kupolov06a484a2015-08-21 16:33:20 -0700130 private static final String ATTR_RESTRICTED_PROFILE_PARENT_ID = "restrictedProfileParentId";
Amith Yamasani12747872015-12-07 14:19:49 -0800131 private static final String ATTR_SEED_ACCOUNT_NAME = "seedAccountName";
132 private static final String ATTR_SEED_ACCOUNT_TYPE = "seedAccountType";
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530133 private static final String TAG_GUEST_RESTRICTIONS = "guestRestrictions";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700134 private static final String TAG_USERS = "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700135 private static final String TAG_USER = "user";
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800136 private static final String TAG_RESTRICTIONS = "restrictions";
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800137 private static final String TAG_DEVICE_POLICY_RESTRICTIONS = "device_policy_restrictions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800138 private static final String TAG_ENTRY = "entry";
139 private static final String TAG_VALUE = "value";
Amith Yamasani12747872015-12-07 14:19:49 -0800140 private static final String TAG_SEED_ACCOUNT_OPTIONS = "seedAccountOptions";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800141 private static final String ATTR_KEY = "key";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700142 private static final String ATTR_VALUE_TYPE = "type";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800143 private static final String ATTR_MULTIPLE = "m";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700144
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700145 private static final String ATTR_TYPE_STRING_ARRAY = "sa";
146 private static final String ATTR_TYPE_STRING = "s";
147 private static final String ATTR_TYPE_BOOLEAN = "b";
Amith Yamasani5b5aa402014-06-01 20:10:14 -0700148 private static final String ATTR_TYPE_INTEGER = "i";
Fyodor Kupolov262f9952015-03-23 18:55:11 -0700149 private static final String ATTR_TYPE_BUNDLE = "B";
150 private static final String ATTR_TYPE_BUNDLE_ARRAY = "BA";
Amith Yamasani7e99bc02013-04-16 18:24:51 -0700151
Amith Yamasani0b285492011-04-14 17:35:23 -0700152 private static final String USER_INFO_DIR = "system" + File.separator + "users";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700153 private static final String USER_LIST_FILENAME = "userlist.xml";
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700154 private static final String USER_PHOTO_FILENAME = "photo.png";
Adrian Roos1bdff912015-02-17 15:51:35 +0100155 private static final String USER_PHOTO_FILENAME_TMP = USER_PHOTO_FILENAME + ".tmp";
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700156
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800157 private static final String RESTRICTIONS_FILE_PREFIX = "res_";
Amith Yamasanifc95e702013-09-26 13:20:17 -0700158 private static final String XML_SUFFIX = ".xml";
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800159
Amith Yamasani634cf312012-10-04 17:34:21 -0700160 private static final int MIN_USER_ID = 10;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -0700161 // We need to keep process uid within Integer.MAX_VALUE.
162 private static final int MAX_USER_ID = Integer.MAX_VALUE / UserHandle.PER_USER_RANGE;
Amith Yamasani634cf312012-10-04 17:34:21 -0700163
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700164 private static final int USER_VERSION = 6;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700165
Amith Yamasani920ace02012-09-20 22:15:37 -0700166 private static final long EPOCH_PLUS_30_YEARS = 30L * 365 * 24 * 60 * 60 * 1000L; // ms
167
Nicolas Prevotb8186812015-08-06 15:00:03 +0100168 // Maximum number of managed profiles permitted per user is 1. This cannot be increased
Amith Yamasani95ab7842014-08-11 17:09:26 -0700169 // without first making sure that the rest of the framework is prepared for it.
170 private static final int MAX_MANAGED_PROFILES = 1;
171
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800172 static final int WRITE_USER_MSG = 1;
173 static final int WRITE_USER_DELAY = 2*1000; // 2 seconds
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530174
Jeff Sharkey6dce4962015-07-03 18:08:41 -0700175 private static final String XATTR_SERIAL = "user.serial";
176
Fyodor Kupolov8385e4b2015-12-29 18:15:32 -0800177 // Tron counters
178 private static final String TRON_GUEST_CREATED = "users_guest_created";
179 private static final String TRON_USER_CREATED = "users_user_created";
180
Dianne Hackborn4428e172012-08-24 17:43:05 -0700181 private final Context mContext;
182 private final PackageManagerService mPm;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700183 private final Object mPackagesLock;
Fyodor Kupolov82402752015-10-28 14:54:51 -0700184 // Short-term lock for internal state, when interaction/sync with PM is not required
185 private final Object mUsersLock = new Object();
186 private final Object mRestrictionsLock = new Object();
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700187
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800188 private final Handler mHandler;
189
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700190 private final File mUsersDir;
191 private final File mUserListFile;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700192
Svet Ganov9cea80cd2016-02-16 11:47:00 -0800193 private static final IBinder mUserRestriconToken = new Binder();
194
Makoto Onuki068c54a2015-10-13 14:34:03 -0700195 /**
Amith Yamasani12747872015-12-07 14:19:49 -0800196 * User-related information that is used for persisting to flash. Only UserInfo is
197 * directly exposed to other system apps.
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800198 */
Amith Yamasani12747872015-12-07 14:19:49 -0800199 private static class UserData {
200 // Basic user information and properties
201 UserInfo info;
202 // Account name used when there is a strong association between a user and an account
203 String account;
204 // Account information for seeding into a newly created user. This could also be
205 // used for login validation for an existing user, for updating their credentials.
206 // In the latter case, data may not need to be persisted as it is only valid for the
207 // current login session.
208 String seedAccountName;
209 String seedAccountType;
210 PersistableBundle seedAccountOptions;
211 // Whether to perist the seed account information to be available after a boot
212 boolean persistSeedData;
213
214 void clearSeedAccountData() {
215 seedAccountName = null;
216 seedAccountType = null;
217 seedAccountOptions = null;
218 persistSeedData = false;
219 }
220 }
221
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800222 @GuardedBy("mUsersLock")
Amith Yamasani12747872015-12-07 14:19:49 -0800223 private final SparseArray<UserData> mUsers = new SparseArray<>();
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800224
225 /**
Makoto Onuki068c54a2015-10-13 14:34:03 -0700226 * User restrictions set via UserManager. This doesn't include restrictions set by
227 * device owner / profile owners.
228 *
229 * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user,
230 * a new {@link Bundle} should always be created and set. This is because a {@link Bundle}
231 * maybe shared between {@link #mBaseUserRestrictions} and
232 * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately.
233 * (Otherwise we won't be able to detect what restrictions have changed in
Fyodor Kupolov82402752015-10-28 14:54:51 -0700234 * {@link #updateUserRestrictionsInternalLR}.
Makoto Onuki068c54a2015-10-13 14:34:03 -0700235 */
236 @GuardedBy("mRestrictionsLock")
237 private final SparseArray<Bundle> mBaseUserRestrictions = new SparseArray<>();
238
239 /**
240 * Cached user restrictions that are in effect -- i.e. {@link #mBaseUserRestrictions} combined
241 * with device / profile owner restrictions. We'll initialize it lazily; use
242 * {@link #getEffectiveUserRestrictions} to access it.
243 *
244 * DO NOT Change existing {@link Bundle} in it. When changing a restriction for a user,
245 * a new {@link Bundle} should always be created and set. This is because a {@link Bundle}
246 * maybe shared between {@link #mBaseUserRestrictions} and
247 * {@link #mCachedEffectiveUserRestrictions}, but they should always updated separately.
248 * (Otherwise we won't be able to detect what restrictions have changed in
Fyodor Kupolov82402752015-10-28 14:54:51 -0700249 * {@link #updateUserRestrictionsInternalLR}.
Makoto Onuki068c54a2015-10-13 14:34:03 -0700250 */
251 @GuardedBy("mRestrictionsLock")
252 private final SparseArray<Bundle> mCachedEffectiveUserRestrictions = new SparseArray<>();
253
Makoto Onuki4f160732015-10-27 17:15:38 -0700254 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800255 * User restrictions that have already been applied in
256 * {@link #updateUserRestrictionsInternalLR(Bundle, int)}. We use it to detect restrictions
257 * that have changed since the last
258 * {@link #updateUserRestrictionsInternalLR(Bundle, int)} call.
Makoto Onuki4f160732015-10-27 17:15:38 -0700259 */
260 @GuardedBy("mRestrictionsLock")
261 private final SparseArray<Bundle> mAppliedUserRestrictions = new SparseArray<>();
262
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800263 /**
Makoto Onukie7927da2015-11-25 10:05:17 -0800264 * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService}
265 * that should be applied to all users, including guests.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800266 */
267 @GuardedBy("mRestrictionsLock")
268 private Bundle mDevicePolicyGlobalUserRestrictions;
269
270 /**
Makoto Onukie7927da2015-11-25 10:05:17 -0800271 * User restrictions set by {@link com.android.server.devicepolicy.DevicePolicyManagerService}
272 * for each user.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800273 */
274 @GuardedBy("mRestrictionsLock")
275 private final SparseArray<Bundle> mDevicePolicyLocalUserRestrictions = new SparseArray<>();
276
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800277 @GuardedBy("mGuestRestrictions")
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530278 private final Bundle mGuestRestrictions = new Bundle();
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800279
280 /**
281 * Set of user IDs being actively removed. Removed IDs linger in this set
282 * for several seconds to work around a VFS caching issue.
283 */
Fyodor Kupolov82402752015-10-28 14:54:51 -0700284 @GuardedBy("mUsersLock")
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800285 private final SparseBooleanArray mRemovingUserIds = new SparseBooleanArray();
Dianne Hackborn4428e172012-08-24 17:43:05 -0700286
Fyodor Kupolov82402752015-10-28 14:54:51 -0700287 @GuardedBy("mUsersLock")
Amith Yamasani0b285492011-04-14 17:35:23 -0700288 private int[] mUserIds;
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800289 @GuardedBy("mPackagesLock")
Amith Yamasani2a003292012-08-14 18:25:45 -0700290 private int mNextSerialNumber;
Amith Yamasani6f34b412012-10-22 18:19:27 -0700291 private int mUserVersion = 0;
Amith Yamasani0b285492011-04-14 17:35:23 -0700292
Jason Monk62062992014-05-06 09:55:28 -0400293 private IAppOpsService mAppOpsService;
294
Makoto Onuki068c54a2015-10-13 14:34:03 -0700295 private final LocalService mLocalService;
296
Makoto Onukie7927da2015-11-25 10:05:17 -0800297 @GuardedBy("mUsersLock")
298 private boolean mIsDeviceManaged;
299
300 @GuardedBy("mUsersLock")
301 private final SparseBooleanArray mIsUserManaged = new SparseBooleanArray();
302
Makoto Onukid45a4a22015-11-02 17:17:38 -0800303 @GuardedBy("mUserRestrictionsListeners")
304 private final ArrayList<UserRestrictionsListener> mUserRestrictionsListeners =
305 new ArrayList<>();
306
Clara Bayarria1771112015-12-18 16:29:18 +0000307 private final LockPatternUtils mLockPatternUtils;
308
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +0100309 /**
310 * Whether all users should be created ephemeral.
311 */
312 @GuardedBy("mUsersLock")
313 private boolean mForceEphemeralUsers;
314
Amith Yamasani258848d2012-08-10 17:06:33 -0700315 private static UserManagerService sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700316
Dianne Hackborn4428e172012-08-24 17:43:05 -0700317 public static UserManagerService getInstance() {
318 synchronized (UserManagerService.class) {
319 return sInstance;
Amith Yamasani258848d2012-08-10 17:06:33 -0700320 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700321 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700322
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800323 @VisibleForTesting
324 UserManagerService(File dataDir) {
325 this(null, null, new Object(), dataDir);
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700326 }
327
Dianne Hackborn4428e172012-08-24 17:43:05 -0700328 /**
329 * Called by package manager to create the service. This is closely
330 * associated with the package manager, and the given lock is the
331 * package manager's own lock.
332 */
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800333 UserManagerService(Context context, PackageManagerService pm, Object packagesLock) {
334 this(context, pm, packagesLock, Environment.getDataDirectory());
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700335 }
336
Dianne Hackborn4428e172012-08-24 17:43:05 -0700337 private UserManagerService(Context context, PackageManagerService pm,
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800338 Object packagesLock, File dataDir) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700339 mContext = context;
340 mPm = pm;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700341 mPackagesLock = packagesLock;
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -0800342 mHandler = new MainHandler();
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800343 synchronized (mPackagesLock) {
344 mUsersDir = new File(dataDir, USER_INFO_DIR);
345 mUsersDir.mkdirs();
346 // Make zeroth user directory, for services to migrate their files to that location
347 File userZeroDir = new File(mUsersDir, String.valueOf(UserHandle.USER_SYSTEM));
348 userZeroDir.mkdirs();
349 FileUtils.setPermissions(mUsersDir.toString(),
350 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH | FileUtils.S_IXOTH,
351 -1, -1);
352 mUserListFile = new File(mUsersDir, USER_LIST_FILENAME);
353 initDefaultGuestRestrictions();
354 readUserListLP();
355 sInstance = this;
Xiaohui Chen4be96e42015-05-06 09:55:43 -0700356 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700357 mLocalService = new LocalService();
358 LocalServices.addService(UserManagerInternal.class, mLocalService);
Clara Bayarria1771112015-12-18 16:29:18 +0000359 mLockPatternUtils = new LockPatternUtils(mContext);
Xiaohui Chen4be96e42015-05-06 09:55:43 -0700360 }
361
362 void systemReady() {
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100363 // Prune out any partially created, partially removed and ephemeral users.
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800364 ArrayList<UserInfo> partials = new ArrayList<>();
365 synchronized (mUsersLock) {
366 final int userSize = mUsers.size();
367 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -0800368 UserInfo ui = mUsers.valueAt(i).info;
Lenka Trochtovac4dd0212015-11-18 12:22:06 +0100369 if ((ui.partial || ui.guestToRemove || ui.isEphemeral()) && i != 0) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800370 partials.add(ui);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700371 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700372 }
Dianne Hackborn4428e172012-08-24 17:43:05 -0700373 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800374 final int partialsSize = partials.size();
375 for (int i = 0; i < partialsSize; i++) {
376 UserInfo ui = partials.get(i);
377 Slog.w(LOG_TAG, "Removing partially created user " + ui.id
378 + " (name=" + ui.name + ")");
379 removeUserState(ui.id);
380 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800381
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700382 onUserForeground(UserHandle.USER_SYSTEM);
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800383
Jason Monk62062992014-05-06 09:55:28 -0400384 mAppOpsService = IAppOpsService.Stub.asInterface(
385 ServiceManager.getService(Context.APP_OPS_SERVICE));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800386
387 synchronized (mRestrictionsLock) {
388 applyUserRestrictionsLR(UserHandle.USER_SYSTEM);
Jason Monk62062992014-05-06 09:55:28 -0400389 }
Amith Yamasani258848d2012-08-10 17:06:33 -0700390 }
391
392 @Override
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800393 public String getUserAccount(int userId) {
394 checkManageUserAndAcrossUsersFullPermission("get user account");
395 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -0800396 return mUsers.get(userId).account;
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800397 }
398 }
399
400 @Override
401 public void setUserAccount(int userId, String accountName) {
402 checkManageUserAndAcrossUsersFullPermission("set user account");
Amith Yamasani12747872015-12-07 14:19:49 -0800403 UserData userToUpdate = null;
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800404 synchronized (mPackagesLock) {
405 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -0800406 final UserData userData = mUsers.get(userId);
407 if (userData == null) {
408 Slog.e(LOG_TAG, "User not found for setting user account: u" + userId);
409 return;
410 }
411 String currentAccount = userData.account;
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800412 if (!Objects.equal(currentAccount, accountName)) {
Amith Yamasani12747872015-12-07 14:19:49 -0800413 userData.account = accountName;
414 userToUpdate = userData;
Xiaohui Chenb3b92582015-12-07 11:22:13 -0800415 }
416 }
417
418 if (userToUpdate != null) {
419 writeUserLP(userToUpdate);
420 }
421 }
422 }
423
424 @Override
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700425 public UserInfo getPrimaryUser() {
426 checkManageUsersPermission("query users");
Fyodor Kupolov82402752015-10-28 14:54:51 -0700427 synchronized (mUsersLock) {
Amith Yamasani515d4062015-09-28 11:30:06 -0700428 final int userSize = mUsers.size();
429 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -0800430 UserInfo ui = mUsers.valueAt(i).info;
Xiaohui Chend3e9e182015-11-18 13:37:32 -0800431 if (ui.isPrimary() && !mRemovingUserIds.get(ui.id)) {
Xiaohui Chen70f6c382015-04-28 14:21:43 -0700432 return ui;
433 }
434 }
435 }
436 return null;
437 }
438
439 @Override
Xiaohui Chen594f2082015-08-18 11:04:20 -0700440 public @NonNull List<UserInfo> getUsers(boolean excludeDying) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700441 checkManageUsersPermission("query users");
Fyodor Kupolov82402752015-10-28 14:54:51 -0700442 synchronized (mUsersLock) {
Amith Yamasani13593602012-03-22 16:16:17 -0700443 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasani515d4062015-09-28 11:30:06 -0700444 final int userSize = mUsers.size();
445 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -0800446 UserInfo ui = mUsers.valueAt(i).info;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700447 if (ui.partial) {
448 continue;
449 }
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -0800450 if (!excludeDying || !mRemovingUserIds.get(ui.id)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700451 users.add(ui);
Amith Yamasani920ace02012-09-20 22:15:37 -0700452 }
Amith Yamasani13593602012-03-22 16:16:17 -0700453 }
454 return users;
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700455 }
Amith Yamasani13593602012-03-22 16:16:17 -0700456 }
457
Amith Yamasani258848d2012-08-10 17:06:33 -0700458 @Override
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100459 public List<UserInfo> getProfiles(int userId, boolean enabledOnly) {
Amith Yamasani4f582632014-02-19 14:31:52 -0800460 if (userId != UserHandle.getCallingUserId()) {
461 checkManageUsersPermission("getting profiles related to user " + userId);
462 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700463 final long ident = Binder.clearCallingIdentity();
464 try {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700465 synchronized (mUsersLock) {
466 return getProfilesLU(userId, enabledOnly);
Alexandra Gherghina385124d2014-04-03 13:37:39 +0100467 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700468 } finally {
469 Binder.restoreCallingIdentity(ident);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000470 }
471 }
472
Amith Yamasanibe465322014-04-24 13:45:17 -0700473 /** Assume permissions already checked and caller's identity cleared */
Fyodor Kupolov82402752015-10-28 14:54:51 -0700474 private List<UserInfo> getProfilesLU(int userId, boolean enabledOnly) {
475 UserInfo user = getUserInfoLU(userId);
Amith Yamasanibe465322014-04-24 13:45:17 -0700476 ArrayList<UserInfo> users = new ArrayList<UserInfo>(mUsers.size());
Amith Yamasanidda003f2014-08-28 18:06:51 -0700477 if (user == null) {
478 // Probably a dying user
479 return users;
480 }
Amith Yamasani515d4062015-09-28 11:30:06 -0700481 final int userSize = mUsers.size();
482 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -0800483 UserInfo profile = mUsers.valueAt(i).info;
Amith Yamasanibe465322014-04-24 13:45:17 -0700484 if (!isProfileOf(user, profile)) {
485 continue;
486 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100487 if (enabledOnly && !profile.isEnabled()) {
488 continue;
Amith Yamasanibe465322014-04-24 13:45:17 -0700489 }
Amith Yamasani70fcf0c2014-07-11 08:40:19 -0700490 if (mRemovingUserIds.get(profile.id)) {
491 continue;
492 }
Amith Yamasanibe465322014-04-24 13:45:17 -0700493 users.add(profile);
494 }
495 return users;
496 }
497
Jessica Hummelbe81c802014-04-22 15:49:22 +0100498 @Override
Andres Moralesc5548c02015-08-05 10:23:12 -0700499 public int getCredentialOwnerProfile(int userHandle) {
500 checkManageUsersPermission("get the credential owner");
Clara Bayarria1771112015-12-18 16:29:18 +0000501 if (!mLockPatternUtils.isSeparateProfileChallengeEnabled(userHandle)) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700502 synchronized (mUsersLock) {
503 UserInfo profileParent = getProfileParentLU(userHandle);
Andres Moralesc5548c02015-08-05 10:23:12 -0700504 if (profileParent != null) {
505 return profileParent.id;
506 }
507 }
508 }
509
510 return userHandle;
511 }
512
513 @Override
Xiaohui Chenfd5b7742015-10-14 15:47:04 -0700514 public boolean isSameProfileGroup(int userId, int otherUserId) {
515 if (userId == otherUserId) return true;
516 checkManageUsersPermission("check if in the same profile group");
517 synchronized (mPackagesLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700518 return isSameProfileGroupLP(userId, otherUserId);
Xiaohui Chenfd5b7742015-10-14 15:47:04 -0700519 }
520 }
521
Fyodor Kupolov82402752015-10-28 14:54:51 -0700522 private boolean isSameProfileGroupLP(int userId, int otherUserId) {
523 synchronized (mUsersLock) {
524 UserInfo userInfo = getUserInfoLU(userId);
525 if (userInfo == null || userInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
526 return false;
527 }
528 UserInfo otherUserInfo = getUserInfoLU(otherUserId);
529 if (otherUserInfo == null
530 || otherUserInfo.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
531 return false;
532 }
533 return userInfo.profileGroupId == otherUserInfo.profileGroupId;
Xiaohui Chenfd5b7742015-10-14 15:47:04 -0700534 }
Xiaohui Chenfd5b7742015-10-14 15:47:04 -0700535 }
536
537 @Override
Jessica Hummelbe81c802014-04-22 15:49:22 +0100538 public UserInfo getProfileParent(int userHandle) {
539 checkManageUsersPermission("get the profile parent");
Fyodor Kupolov82402752015-10-28 14:54:51 -0700540 synchronized (mUsersLock) {
541 return getProfileParentLU(userHandle);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700542 }
543 }
544
Fyodor Kupolov82402752015-10-28 14:54:51 -0700545 private UserInfo getProfileParentLU(int userHandle) {
546 UserInfo profile = getUserInfoLU(userHandle);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -0700547 if (profile == null) {
548 return null;
549 }
550 int parentUserId = profile.profileGroupId;
551 if (parentUserId == UserInfo.NO_PROFILE_GROUP_ID) {
552 return null;
553 } else {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700554 return getUserInfoLU(parentUserId);
Jessica Hummelbe81c802014-04-22 15:49:22 +0100555 }
556 }
557
Fyodor Kupolov82402752015-10-28 14:54:51 -0700558 private static boolean isProfileOf(UserInfo user, UserInfo profile) {
Kenny Guy2a764942014-04-02 13:29:20 +0100559 return user.id == profile.id ||
560 (user.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
561 && user.profileGroupId == profile.profileGroupId);
Kenny Guya52dc3e2014-02-11 15:33:14 +0000562 }
563
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000564 private void broadcastProfileAvailabilityChanges(UserHandle profileHandle,
Rubin Xuf13c9802016-01-21 18:06:00 +0000565 UserHandle parentHandle, boolean inQuietMode) {
566 Intent intent = new Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABILITY_CHANGED);
567 intent.putExtra(Intent.EXTRA_QUIET_MODE, inQuietMode);
568 intent.putExtra(Intent.EXTRA_USER, profileHandle);
569 intent.putExtra(Intent.EXTRA_USER_HANDLE, profileHandle.getIdentifier());
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000570 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Rubin Xuf13c9802016-01-21 18:06:00 +0000571 mContext.sendBroadcastAsUser(intent, parentHandle);
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000572 }
573
574 @Override
575 public void setQuietModeEnabled(int userHandle, boolean enableQuietMode) {
576 checkManageUsersPermission("silence profile");
577 boolean changed = false;
578 UserInfo profile, parent;
579 synchronized (mPackagesLock) {
580 synchronized (mUsersLock) {
581 profile = getUserInfoLU(userHandle);
582 parent = getProfileParentLU(userHandle);
583
584 }
585 if (profile == null || !profile.isManagedProfile()) {
586 throw new IllegalArgumentException("User " + userHandle + " is not a profile");
587 }
588 if (profile.isQuietModeEnabled() != enableQuietMode) {
589 profile.flags ^= UserInfo.FLAG_QUIET_MODE;
Amith Yamasani12747872015-12-07 14:19:49 -0800590 writeUserLP(getUserDataLU(profile.id));
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000591 changed = true;
592 }
593 }
594 if (changed) {
Rubin Xuf13c9802016-01-21 18:06:00 +0000595 long identity = Binder.clearCallingIdentity();
596 try {
597 if (enableQuietMode) {
598 ActivityManagerNative.getDefault().stopUser(userHandle, /* force */true, null);
599 } else {
600 ActivityManagerNative.getDefault().startUserInBackground(userHandle);
601 }
602 } catch (RemoteException e) {
603 Slog.e(LOG_TAG, "fail to start/stop user for quiet mode", e);
604 } finally {
605 Binder.restoreCallingIdentity(identity);
606 }
607
608 broadcastProfileAvailabilityChanges(profile.getUserHandle(), parent.getUserHandle(),
609 enableQuietMode);
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000610 }
611 }
612
613 @Override
614 public boolean isQuietModeEnabled(int userHandle) {
615 synchronized (mPackagesLock) {
616 UserInfo info;
617 synchronized (mUsersLock) {
618 info = getUserInfoLU(userHandle);
619 }
620 if (info == null || !info.isManagedProfile()) {
Rubin Xuf13c9802016-01-21 18:06:00 +0000621 return false;
Rubin Xu0a29ecd2015-11-04 15:11:48 +0000622 }
623 return info.isQuietModeEnabled();
624 }
625 }
626
Kenny Guya52dc3e2014-02-11 15:33:14 +0000627 @Override
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100628 public void setUserEnabled(int userId) {
629 checkManageUsersPermission("enable user");
630 synchronized (mPackagesLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700631 UserInfo info;
632 synchronized (mUsersLock) {
633 info = getUserInfoLU(userId);
634 }
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100635 if (info != null && !info.isEnabled()) {
636 info.flags ^= UserInfo.FLAG_DISABLED;
Amith Yamasani12747872015-12-07 14:19:49 -0800637 writeUserLP(getUserDataLU(info.id));
Alexandra Gherghinadf35d572014-04-09 13:54:39 +0100638 }
639 }
640 }
641
642 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -0700643 public UserInfo getUserInfo(int userId) {
Tony Makb531d082016-03-16 14:49:52 +0000644 int callingUserId = UserHandle.getCallingUserId();
645 if (callingUserId != userId && !hasManageUsersPermission()) {
646 synchronized (mPackagesLock) {
647 if (!isSameProfileGroupLP(callingUserId, userId)) {
648 throw new SecurityException(
649 "You need MANAGE_USERS permission to: query users outside profile" +
650 " group");
651 }
652 }
Tony Mak4dc008c2016-03-16 10:46:49 +0000653 }
Fyodor Kupolov82402752015-10-28 14:54:51 -0700654 synchronized (mUsersLock) {
655 return getUserInfoLU(userId);
Amith Yamasani13593602012-03-22 16:16:17 -0700656 }
657 }
658
Amith Yamasani71e6c692013-03-24 17:39:28 -0700659 @Override
660 public boolean isRestricted() {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700661 synchronized (mUsersLock) {
662 return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted();
Amith Yamasani71e6c692013-03-24 17:39:28 -0700663 }
664 }
665
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700666 @Override
667 public boolean canHaveRestrictedProfile(int userId) {
668 checkManageUsersPermission("canHaveRestrictedProfile");
Fyodor Kupolov82402752015-10-28 14:54:51 -0700669 synchronized (mUsersLock) {
670 final UserInfo userInfo = getUserInfoLU(userId);
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700671 if (userInfo == null || !userInfo.canHaveProfile()) {
672 return false;
673 }
674 if (!userInfo.isAdmin()) {
675 return false;
676 }
Makoto Onukie7927da2015-11-25 10:05:17 -0800677 // restricted profile can be created if there is no DO set and the admin user has no PO;
678 return !mIsDeviceManaged && !mIsUserManaged.get(userId);
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700679 }
Fyodor Kupolov1c363152015-09-02 13:27:21 -0700680 }
681
Amith Yamasani195263742012-08-21 15:40:12 -0700682 /*
683 * Should be locked on mUsers before calling this.
684 */
Fyodor Kupolov82402752015-10-28 14:54:51 -0700685 private UserInfo getUserInfoLU(int userId) {
Amith Yamasani12747872015-12-07 14:19:49 -0800686 final UserData userData = mUsers.get(userId);
Amith Yamasani16389312012-10-17 21:20:14 -0700687 // If it is partial and not in the process of being removed, return as unknown user.
Amith Yamasani12747872015-12-07 14:19:49 -0800688 if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700689 Slog.w(LOG_TAG, "getUserInfo: unknown user #" + userId);
690 return null;
691 }
Amith Yamasani12747872015-12-07 14:19:49 -0800692 return userData != null ? userData.info : null;
693 }
694
695 private UserData getUserDataLU(int userId) {
696 final UserData userData = mUsers.get(userId);
697 // If it is partial and not in the process of being removed, return as unknown user.
698 if (userData != null && userData.info.partial && !mRemovingUserIds.get(userId)) {
699 return null;
700 }
701 return userData;
Amith Yamasani195263742012-08-21 15:40:12 -0700702 }
703
Fyodor Kupolov82402752015-10-28 14:54:51 -0700704 /**
705 * Obtains {@link #mUsersLock} and return UserInfo from mUsers.
706 * <p>No permissions checking or any addition checks are made</p>
707 */
708 private UserInfo getUserInfoNoChecks(int userId) {
709 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -0800710 final UserData userData = mUsers.get(userId);
711 return userData != null ? userData.info : null;
712 }
713 }
714
715 /**
716 * Obtains {@link #mUsersLock} and return UserData from mUsers.
717 * <p>No permissions checking or any addition checks are made</p>
718 */
719 private UserData getUserDataNoChecks(int userId) {
720 synchronized (mUsersLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700721 return mUsers.get(userId);
722 }
723 }
724
Amith Yamasani236b2b52015-08-18 14:32:14 -0700725 /** Called by PackageManagerService */
Amith Yamasani13593602012-03-22 16:16:17 -0700726 public boolean exists(int userId) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700727 return getUserInfoNoChecks(userId) != null;
Amith Yamasani13593602012-03-22 16:16:17 -0700728 }
729
Amith Yamasani258848d2012-08-10 17:06:33 -0700730 @Override
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700731 public void setUserName(int userId, String name) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700732 checkManageUsersPermission("rename users");
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700733 boolean changed = false;
Dianne Hackborn4428e172012-08-24 17:43:05 -0700734 synchronized (mPackagesLock) {
Amith Yamasani12747872015-12-07 14:19:49 -0800735 UserData userData = getUserDataNoChecks(userId);
736 if (userData == null || userData.info.partial) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700737 Slog.w(LOG_TAG, "setUserName: unknown user #" + userId);
738 return;
739 }
Amith Yamasani12747872015-12-07 14:19:49 -0800740 if (name != null && !name.equals(userData.info.name)) {
741 userData.info.name = name;
742 writeUserLP(userData);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700743 changed = true;
Amith Yamasani13593602012-03-22 16:16:17 -0700744 }
745 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700746 if (changed) {
747 sendUserInfoChangedBroadcast(userId);
748 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -0700749 }
750
Amith Yamasani258848d2012-08-10 17:06:33 -0700751 @Override
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700752 public void setUserIcon(int userId, Bitmap bitmap) {
Amith Yamasani2a003292012-08-14 18:25:45 -0700753 checkManageUsersPermission("update users");
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100754 if (hasUserRestriction(UserManager.DISALLOW_SET_USER_ICON, userId)) {
755 Log.w(LOG_TAG, "Cannot set user icon. DISALLOW_SET_USER_ICON is enabled.");
756 return;
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700757 }
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100758 mLocalService.setUserIcon(userId, bitmap);
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700759 }
760
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +0100761
762
Amith Yamasanie928d7d2012-09-17 21:46:51 -0700763 private void sendUserInfoChangedBroadcast(int userId) {
764 Intent changedIntent = new Intent(Intent.ACTION_USER_INFO_CHANGED);
765 changedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
766 changedIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
Amith Yamasani6fc1d4e2013-05-08 16:43:58 -0700767 mContext.sendBroadcastAsUser(changedIntent, UserHandle.ALL);
Amith Yamasanib8151ec2012-04-18 18:02:48 -0700768 }
769
Amith Yamasani258848d2012-08-10 17:06:33 -0700770 @Override
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +0100771 public ParcelFileDescriptor getUserIcon(int targetUserId) {
Adrian Roos1bdff912015-02-17 15:51:35 +0100772 String iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700773 synchronized (mPackagesLock) {
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +0100774 UserInfo targetUserInfo = getUserInfoNoChecks(targetUserId);
775 if (targetUserInfo == null || targetUserInfo.partial) {
776 Slog.w(LOG_TAG, "getUserIcon: unknown user #" + targetUserId);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700777 return null;
778 }
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +0100779
780 final int callingUserId = UserHandle.getCallingUserId();
781 final int callingGroupId = getUserInfoNoChecks(callingUserId).profileGroupId;
782 final int targetGroupId = targetUserInfo.profileGroupId;
783 final boolean sameGroup = (callingGroupId != UserInfo.NO_PROFILE_GROUP_ID
784 && callingGroupId == targetGroupId);
785 if ((callingUserId != targetUserId) && !sameGroup) {
Nicolas Prevot88cc3462014-05-14 14:51:48 +0100786 checkManageUsersPermission("get the icon of a user who is not related");
787 }
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +0100788
789 if (targetUserInfo.iconPath == null) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700790 return null;
791 }
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +0100792 iconPath = targetUserInfo.iconPath;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700793 }
Adrian Roos1bdff912015-02-17 15:51:35 +0100794
795 try {
796 return ParcelFileDescriptor.open(
797 new File(iconPath), ParcelFileDescriptor.MODE_READ_ONLY);
798 } catch (FileNotFoundException e) {
799 Log.e(LOG_TAG, "Couldn't find icon file", e);
800 }
801 return null;
Amith Yamasani3b49f072012-09-17 10:21:43 -0700802 }
803
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700804 public void makeInitialized(int userId) {
805 checkManageUsersPermission("makeInitialized");
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800806 boolean scheduleWriteUser = false;
Amith Yamasani12747872015-12-07 14:19:49 -0800807 UserData userData;
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800808 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -0800809 userData = mUsers.get(userId);
810 if (userData == null || userData.info.partial) {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700811 Slog.w(LOG_TAG, "makeInitialized: unknown user #" + userId);
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800812 return;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -0700813 }
Amith Yamasani12747872015-12-07 14:19:49 -0800814 if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) {
815 userData.info.flags |= UserInfo.FLAG_INITIALIZED;
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800816 scheduleWriteUser = true;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700817 }
818 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800819 if (scheduleWriteUser) {
Amith Yamasani12747872015-12-07 14:19:49 -0800820 scheduleWriteUser(userData);
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800821 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -0700822 }
823
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700824 /**
825 * If default guest restrictions haven't been initialized yet, add the basic
826 * restrictions.
827 */
828 private void initDefaultGuestRestrictions() {
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800829 synchronized (mGuestRestrictions) {
830 if (mGuestRestrictions.isEmpty()) {
Fyodor Kupolove04462c2015-11-30 15:02:53 -0800831 mGuestRestrictions.putBoolean(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true);
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800832 mGuestRestrictions.putBoolean(UserManager.DISALLOW_OUTGOING_CALLS, true);
833 mGuestRestrictions.putBoolean(UserManager.DISALLOW_SMS, true);
834 }
Amith Yamasaniaa6634e2014-10-06 14:20:28 -0700835 }
836 }
837
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800838 @Override
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530839 public Bundle getDefaultGuestRestrictions() {
840 checkManageUsersPermission("getDefaultGuestRestrictions");
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800841 synchronized (mGuestRestrictions) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530842 return new Bundle(mGuestRestrictions);
843 }
844 }
845
846 @Override
847 public void setDefaultGuestRestrictions(Bundle restrictions) {
848 checkManageUsersPermission("setDefaultGuestRestrictions");
Fyodor Kupolove80085d2015-11-06 18:21:39 -0800849 synchronized (mGuestRestrictions) {
850 mGuestRestrictions.clear();
851 mGuestRestrictions.putAll(restrictions);
852 }
853 synchronized (mPackagesLock) {
854 writeUserListLP();
Amith Yamasanie4afaa32014-06-30 14:55:07 +0530855 }
856 }
857
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800858 /**
859 * See {@link UserManagerInternal#setDevicePolicyUserRestrictions(int, Bundle, Bundle)}
860 */
Makoto Onuki2a3c3da2016-02-18 14:25:30 -0800861 void setDevicePolicyUserRestrictionsInner(int userId, @NonNull Bundle local,
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800862 @Nullable Bundle global) {
863 Preconditions.checkNotNull(local);
864 boolean globalChanged = false;
865 boolean localChanged;
866 synchronized (mRestrictionsLock) {
867 if (global != null) {
868 // Update global.
869 globalChanged = !UserRestrictionsUtils.areEqual(
870 mDevicePolicyGlobalUserRestrictions, global);
871 if (globalChanged) {
872 mDevicePolicyGlobalUserRestrictions = global;
873 }
874 }
875 {
876 // Update local.
877 final Bundle prev = mDevicePolicyLocalUserRestrictions.get(userId);
878 localChanged = !UserRestrictionsUtils.areEqual(prev, local);
879 if (localChanged) {
880 mDevicePolicyLocalUserRestrictions.put(userId, local);
881 }
882 }
883 }
884 if (DBG) {
885 Log.d(LOG_TAG, "setDevicePolicyUserRestrictions: userId=" + userId
886 + " global=" + global + (globalChanged ? " (changed)" : "")
887 + " local=" + local + (localChanged ? " (changed)" : "")
888 );
889 }
890 // Don't call them within the mRestrictionsLock.
891 synchronized (mPackagesLock) {
892 if (globalChanged) {
893 writeUserListLP();
894 }
895 if (localChanged) {
Amith Yamasani12747872015-12-07 14:19:49 -0800896 writeUserLP(getUserDataNoChecks(userId));
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800897 }
898 }
899
900 synchronized (mRestrictionsLock) {
901 if (globalChanged) {
902 applyUserRestrictionsForAllUsersLR();
903 } else if (localChanged) {
904 applyUserRestrictionsLR(userId);
905 }
906 }
907 }
908
Makoto Onuki068c54a2015-10-13 14:34:03 -0700909 @GuardedBy("mRestrictionsLock")
Fyodor Kupolov82402752015-10-28 14:54:51 -0700910 private Bundle computeEffectiveUserRestrictionsLR(int userId) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800911 final Bundle baseRestrictions =
912 UserRestrictionsUtils.nonNull(mBaseUserRestrictions.get(userId));
913 final Bundle global = mDevicePolicyGlobalUserRestrictions;
914 final Bundle local = mDevicePolicyLocalUserRestrictions.get(userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700915
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800916 if (UserRestrictionsUtils.isEmpty(global) && UserRestrictionsUtils.isEmpty(local)) {
917 // Common case first.
918 return baseRestrictions;
Makoto Onuki068c54a2015-10-13 14:34:03 -0700919 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800920 final Bundle effective = UserRestrictionsUtils.clone(baseRestrictions);
921 UserRestrictionsUtils.merge(effective, global);
922 UserRestrictionsUtils.merge(effective, local);
923
Makoto Onuki068c54a2015-10-13 14:34:03 -0700924 return effective;
925 }
926
927 @GuardedBy("mRestrictionsLock")
Fyodor Kupolov82402752015-10-28 14:54:51 -0700928 private void invalidateEffectiveUserRestrictionsLR(int userId) {
Makoto Onuki068c54a2015-10-13 14:34:03 -0700929 if (DBG) {
930 Log.d(LOG_TAG, "invalidateEffectiveUserRestrictions userId=" + userId);
931 }
932 mCachedEffectiveUserRestrictions.remove(userId);
933 }
934
935 private Bundle getEffectiveUserRestrictions(int userId) {
936 synchronized (mRestrictionsLock) {
937 Bundle restrictions = mCachedEffectiveUserRestrictions.get(userId);
938 if (restrictions == null) {
Fyodor Kupolov82402752015-10-28 14:54:51 -0700939 restrictions = computeEffectiveUserRestrictionsLR(userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -0700940 mCachedEffectiveUserRestrictions.put(userId, restrictions);
941 }
942 return restrictions;
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700943 }
944 }
945
Makoto Onuki068c54a2015-10-13 14:34:03 -0700946 /** @return a specific user restriction that's in effect currently. */
947 @Override
948 public boolean hasUserRestriction(String restrictionKey, int userId) {
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800949 if (!UserRestrictionsUtils.isValidRestriction(restrictionKey)) {
950 return false;
951 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700952 Bundle restrictions = getEffectiveUserRestrictions(userId);
953 return restrictions != null && restrictions.getBoolean(restrictionKey);
954 }
955
956 /**
957 * @return UserRestrictions that are in effect currently. This always returns a new
958 * {@link Bundle}.
959 */
Amith Yamasani8cd28b52014-06-08 17:54:27 -0700960 @Override
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800961 public Bundle getUserRestrictions(int userId) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800962 return UserRestrictionsUtils.clone(getEffectiveUserRestrictions(userId));
Amith Yamasanie4cf7342012-12-17 11:12:09 -0800963 }
964
965 @Override
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +0000966 public boolean hasBaseUserRestriction(String restrictionKey, int userId) {
967 checkManageUsersPermission("hasBaseUserRestriction");
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800968 if (!UserRestrictionsUtils.isValidRestriction(restrictionKey)) {
969 return false;
970 }
Zoltan Szatmary-Ban3bbcedd2015-11-26 13:45:51 +0000971 synchronized (mRestrictionsLock) {
972 Bundle bundle = mBaseUserRestrictions.get(userId);
973 return (bundle != null && bundle.getBoolean(restrictionKey, false));
974 }
975 }
976
977 @Override
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700978 public void setUserRestriction(String key, boolean value, int userId) {
Fyodor Kupolovd4b26042015-07-27 14:30:59 -0700979 checkManageUsersPermission("setUserRestriction");
Makoto Onuki1f1ceef2016-01-28 11:32:32 -0800980 if (!UserRestrictionsUtils.isValidRestriction(key)) {
981 return;
982 }
Makoto Onuki068c54a2015-10-13 14:34:03 -0700983 synchronized (mRestrictionsLock) {
984 // Note we can't modify Bundles stored in mBaseUserRestrictions directly, so create
985 // a copy.
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800986 final Bundle newRestrictions = UserRestrictionsUtils.clone(
987 mBaseUserRestrictions.get(userId));
Makoto Onuki068c54a2015-10-13 14:34:03 -0700988 newRestrictions.putBoolean(key, value);
989
Fyodor Kupolov82402752015-10-28 14:54:51 -0700990 updateUserRestrictionsInternalLR(newRestrictions, userId);
Fyodor Kupolovb5013302015-04-17 17:59:14 -0700991 }
992 }
993
Makoto Onuki068c54a2015-10-13 14:34:03 -0700994 /**
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800995 * Optionally updating user restrictions, calculate the effective user restrictions and also
996 * propagate to other services and system settings.
Makoto Onuki068c54a2015-10-13 14:34:03 -0700997 *
Makoto Onuki1a2cd742015-11-16 13:51:27 -0800998 * @param newRestrictions User restrictions to set.
999 * If null, will not update user restrictions and only does the propagation.
Makoto Onuki068c54a2015-10-13 14:34:03 -07001000 * @param userId target user ID.
1001 */
1002 @GuardedBy("mRestrictionsLock")
Fyodor Kupolov82402752015-10-28 14:54:51 -07001003 private void updateUserRestrictionsInternalLR(
Makoto Onuki068c54a2015-10-13 14:34:03 -07001004 @Nullable Bundle newRestrictions, int userId) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001005
1006 final Bundle prevAppliedRestrictions = UserRestrictionsUtils.nonNull(
1007 mAppliedUserRestrictions.get(userId));
1008
1009 // Update base restrictions.
Makoto Onuki068c54a2015-10-13 14:34:03 -07001010 if (newRestrictions != null) {
1011 // If newRestrictions == the current one, it's probably a bug.
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001012 final Bundle prevBaseRestrictions = mBaseUserRestrictions.get(userId);
1013
1014 Preconditions.checkState(prevBaseRestrictions != newRestrictions);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001015 Preconditions.checkState(mCachedEffectiveUserRestrictions.get(userId)
1016 != newRestrictions);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001017
1018 if (!UserRestrictionsUtils.areEqual(prevBaseRestrictions, newRestrictions)) {
1019 mBaseUserRestrictions.put(userId, newRestrictions);
Amith Yamasani12747872015-12-07 14:19:49 -08001020 scheduleWriteUser(getUserDataNoChecks(userId));
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001021 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001022 }
1023
Fyodor Kupolov82402752015-10-28 14:54:51 -07001024 final Bundle effective = computeEffectiveUserRestrictionsLR(userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001025
Makoto Onuki759a7632015-10-28 16:43:10 -07001026 mCachedEffectiveUserRestrictions.put(userId, effective);
1027
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001028 // Apply the new restrictions.
Makoto Onuki4f160732015-10-27 17:15:38 -07001029 if (DBG) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001030 debug("Applying user restrictions: userId=" + userId
1031 + " new=" + effective + " prev=" + prevAppliedRestrictions);
Makoto Onuki4f160732015-10-27 17:15:38 -07001032 }
1033
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001034 if (mAppOpsService != null) { // We skip it until system-ready.
1035 final long token = Binder.clearCallingIdentity();
1036 try {
Svet Ganov9cea80cd2016-02-16 11:47:00 -08001037 mAppOpsService.setUserRestrictions(effective, mUserRestriconToken, userId);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001038 } catch (RemoteException e) {
1039 Log.w(LOG_TAG, "Unable to notify AppOpsService of UserRestrictions");
1040 } finally {
1041 Binder.restoreCallingIdentity(token);
1042 }
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001043 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001044
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001045 propagateUserRestrictionsLR(userId, effective, prevAppliedRestrictions);
Makoto Onuki4f160732015-10-27 17:15:38 -07001046
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001047 mAppliedUserRestrictions.put(userId, new Bundle(effective));
Makoto Onuki068c54a2015-10-13 14:34:03 -07001048 }
1049
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001050 private void propagateUserRestrictionsLR(final int userId,
Makoto Onukid45a4a22015-11-02 17:17:38 -08001051 Bundle newRestrictions, Bundle prevRestrictions) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001052 // Note this method doesn't touch any state, meaning it doesn't require mRestrictionsLock
1053 // actually, but we still need some kind of synchronization otherwise we might end up
1054 // calling listeners out-of-order, thus "LR".
1055
1056 if (UserRestrictionsUtils.areEqual(newRestrictions, prevRestrictions)) {
1057 return;
1058 }
Makoto Onukid45a4a22015-11-02 17:17:38 -08001059
1060 final Bundle newRestrictionsFinal = new Bundle(newRestrictions);
1061 final Bundle prevRestrictionsFinal = new Bundle(prevRestrictions);
1062
1063 mHandler.post(new Runnable() {
1064 @Override
1065 public void run() {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -07001066 UserRestrictionsUtils.applyUserRestrictions(
1067 mContext, userId, newRestrictionsFinal, prevRestrictionsFinal);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001068
Makoto Onukid45a4a22015-11-02 17:17:38 -08001069 final UserRestrictionsListener[] listeners;
1070 synchronized (mUserRestrictionsListeners) {
1071 listeners = new UserRestrictionsListener[mUserRestrictionsListeners.size()];
1072 mUserRestrictionsListeners.toArray(listeners);
1073 }
1074 for (int i = 0; i < listeners.length; i++) {
1075 listeners[i].onUserRestrictionsChanged(userId,
1076 newRestrictionsFinal, prevRestrictionsFinal);
1077 }
1078 }
1079 });
1080 }
1081
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001082 // Package private for the inner class.
1083 void applyUserRestrictionsLR(int userId) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07001084 updateUserRestrictionsInternalLR(null, userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001085 }
1086
1087 @GuardedBy("mRestrictionsLock")
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001088 // Package private for the inner class.
1089 void applyUserRestrictionsForAllUsersLR() {
1090 if (DBG) {
1091 debug("applyUserRestrictionsForAllUsersLR");
1092 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07001093 // First, invalidate all cached values.
Makoto Onuki4f160732015-10-27 17:15:38 -07001094 mCachedEffectiveUserRestrictions.clear();
1095
Makoto Onuki068c54a2015-10-13 14:34:03 -07001096 // We don't want to call into ActivityManagerNative while taking a lock, so we'll call
1097 // it on a handler.
1098 final Runnable r = new Runnable() {
1099 @Override
1100 public void run() {
1101 // Then get the list of running users.
1102 final int[] runningUsers;
1103 try {
1104 runningUsers = ActivityManagerNative.getDefault().getRunningUserIds();
1105 } catch (RemoteException e) {
1106 Log.w(LOG_TAG, "Unable to access ActivityManagerNative");
1107 return;
1108 }
1109 // Then re-calculate the effective restrictions and apply, only for running users.
1110 // It's okay if a new user has started after the getRunningUserIds() call,
1111 // because we'll do the same thing (re-calculate the restrictions and apply)
1112 // when we start a user.
Makoto Onuki068c54a2015-10-13 14:34:03 -07001113 synchronized (mRestrictionsLock) {
1114 for (int i = 0; i < runningUsers.length; i++) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001115 applyUserRestrictionsLR(runningUsers[i]);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001116 }
1117 }
1118 }
1119 };
1120 mHandler.post(r);
Fyodor Kupolovb5013302015-04-17 17:59:14 -07001121 }
1122
Amith Yamasani258848d2012-08-10 17:06:33 -07001123 /**
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001124 * Check if we've hit the limit of how many users can be created.
1125 */
Fyodor Kupolov82402752015-10-28 14:54:51 -07001126 private boolean isUserLimitReached() {
1127 int count;
1128 synchronized (mUsersLock) {
1129 count = getAliveUsersExcludingGuestsCountLU();
1130 }
1131 return count >= UserManager.getMaxSupportedUsers();
Nicolas Prevot12678a92015-05-13 12:15:03 -07001132 }
1133
1134 @Override
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001135 public boolean canAddMoreManagedProfiles(int userId, boolean allowedToRemoveOne) {
Nicolas Prevot12678a92015-05-13 12:15:03 -07001136 checkManageUsersPermission("check if more managed profiles can be added.");
1137 if (ActivityManager.isLowRamDeviceStatic()) {
1138 return false;
1139 }
Fyodor Kupolovb6157992015-06-05 15:32:28 -07001140 if (!mContext.getPackageManager().hasSystemFeature(
1141 PackageManager.FEATURE_MANAGED_USERS)) {
1142 return false;
1143 }
Nicolas Prevotb8186812015-08-06 15:00:03 +01001144 // Limit number of managed profiles that can be created
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001145 final int managedProfilesCount = getProfiles(userId, true).size() - 1;
1146 final int profilesRemovedCount = managedProfilesCount > 0 && allowedToRemoveOne ? 1 : 0;
1147 if (managedProfilesCount - profilesRemovedCount >= MAX_MANAGED_PROFILES) {
Nicolas Prevotb8186812015-08-06 15:00:03 +01001148 return false;
1149 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07001150 synchronized(mUsersLock) {
1151 UserInfo userInfo = getUserInfoLU(userId);
Nicolas Prevotb8186812015-08-06 15:00:03 +01001152 if (!userInfo.canHaveProfile()) {
Nicolas Prevot12678a92015-05-13 12:15:03 -07001153 return false;
1154 }
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001155 int usersCountAfterRemoving = getAliveUsersExcludingGuestsCountLU()
1156 - profilesRemovedCount;
Nicolas Prevot12678a92015-05-13 12:15:03 -07001157 // We allow creating a managed profile in the special case where there is only one user.
Nicolas Prevot07387fe2015-10-30 17:53:30 +00001158 return usersCountAfterRemoving == 1
1159 || usersCountAfterRemoving < UserManager.getMaxSupportedUsers();
Nicolas Prevot12678a92015-05-13 12:15:03 -07001160 }
1161 }
1162
Fyodor Kupolov82402752015-10-28 14:54:51 -07001163 private int getAliveUsersExcludingGuestsCountLU() {
Amith Yamasanif584f012014-05-19 17:57:25 -07001164 int aliveUserCount = 0;
1165 final int totalUserCount = mUsers.size();
1166 // Skip over users being removed
1167 for (int i = 0; i < totalUserCount; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08001168 UserInfo user = mUsers.valueAt(i).info;
Amith Yamasani95ab7842014-08-11 17:09:26 -07001169 if (!mRemovingUserIds.get(user.id)
Amith Yamasani1df14732014-08-29 21:37:27 -07001170 && !user.isGuest() && !user.partial) {
Amith Yamasanif584f012014-05-19 17:57:25 -07001171 aliveUserCount++;
1172 }
1173 }
Nicolas Prevot12678a92015-05-13 12:15:03 -07001174 return aliveUserCount;
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001175 }
1176
1177 /**
Amith Yamasani195263742012-08-21 15:40:12 -07001178 * Enforces that only the system UID or root's UID or apps that have the
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001179 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS} and
1180 * {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL INTERACT_ACROSS_USERS_FULL}
1181 * permissions can make certain calls to the UserManager.
1182 *
1183 * @param message used as message if SecurityException is thrown
1184 * @throws SecurityException if the caller does not have enough privilege.
1185 */
1186 private static final void checkManageUserAndAcrossUsersFullPermission(String message) {
1187 final int uid = Binder.getCallingUid();
1188 if (uid != Process.SYSTEM_UID && uid != 0
1189 && ActivityManager.checkComponentPermission(
1190 Manifest.permission.MANAGE_USERS,
1191 uid, -1, true) != PackageManager.PERMISSION_GRANTED
1192 && ActivityManager.checkComponentPermission(
1193 Manifest.permission.INTERACT_ACROSS_USERS_FULL,
1194 uid, -1, true) != PackageManager.PERMISSION_GRANTED) {
1195 throw new SecurityException(
1196 "You need MANAGE_USERS and INTERACT_ACROSS_USERS_FULL permission to: "
1197 + message);
1198 }
1199 }
1200
1201 /**
1202 * Enforces that only the system UID or root's UID or apps that have the
Dianne Hackborn10ad9822014-03-17 11:28:36 -07001203 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}
Amith Yamasani195263742012-08-21 15:40:12 -07001204 * permission can make certain calls to the UserManager.
Amith Yamasani258848d2012-08-10 17:06:33 -07001205 *
1206 * @param message used as message if SecurityException is thrown
1207 * @throws SecurityException if the caller is not system or root
Tony Mak4dc008c2016-03-16 10:46:49 +00001208 * @see #hasManageUsersPermission()
Amith Yamasani258848d2012-08-10 17:06:33 -07001209 */
Amith Yamasanibe465322014-04-24 13:45:17 -07001210 private static final void checkManageUsersPermission(String message) {
Tony Mak4dc008c2016-03-16 10:46:49 +00001211 if (!hasManageUsersPermission()) {
Amith Yamasanibe465322014-04-24 13:45:17 -07001212 throw new SecurityException("You need MANAGE_USERS permission to: " + message);
1213 }
Emily Bernier7a2b4d12014-04-23 12:51:35 -04001214 }
1215
Esteban Talavera5b9f1672015-12-11 15:22:34 +00001216 /**
Tony Mak4dc008c2016-03-16 10:46:49 +00001217 * @return whether the calling UID is system UID or root's UID or the calling app has the
1218 * {@link android.Manifest.permission#MANAGE_USERS MANAGE_USERS}.
1219 */
1220 private static final boolean hasManageUsersPermission() {
1221 final int callingUid = Binder.getCallingUid();
1222 return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID)
1223 || callingUid == Process.ROOT_UID
1224 || ActivityManager.checkComponentPermission(
1225 android.Manifest.permission.MANAGE_USERS,
1226 callingUid, -1, true) == PackageManager.PERMISSION_GRANTED;
1227 }
1228
1229 /**
Esteban Talavera5b9f1672015-12-11 15:22:34 +00001230 * Enforces that only the system UID or root's UID (on any user) can make certain calls to the
1231 * UserManager.
1232 *
1233 * @param message used as message if SecurityException is thrown
1234 * @throws SecurityException if the caller is not system or root
1235 */
1236 private static void checkSystemOrRoot(String message) {
1237 final int uid = Binder.getCallingUid();
1238 if (!UserHandle.isSameApp(uid, Process.SYSTEM_UID) && uid != Process.ROOT_UID) {
1239 throw new SecurityException("Only system may: " + message);
1240 }
1241 }
1242
Fyodor Kupolov82402752015-10-28 14:54:51 -07001243 private void writeBitmapLP(UserInfo info, Bitmap bitmap) {
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001244 try {
1245 File dir = new File(mUsersDir, Integer.toString(info.id));
1246 File file = new File(dir, USER_PHOTO_FILENAME);
Adrian Roos1bdff912015-02-17 15:51:35 +01001247 File tmp = new File(dir, USER_PHOTO_FILENAME_TMP);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001248 if (!dir.exists()) {
1249 dir.mkdir();
1250 FileUtils.setPermissions(
1251 dir.getPath(),
1252 FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
1253 -1, -1);
1254 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001255 FileOutputStream os;
Adrian Roos1bdff912015-02-17 15:51:35 +01001256 if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(tmp))
Oleksandr Peletskyicd6fa302016-02-25 16:28:39 +01001257 && tmp.renameTo(file) && SELinux.restorecon(file)) {
Amith Yamasani3b49f072012-09-17 10:21:43 -07001258 info.iconPath = file.getAbsolutePath();
1259 }
Amith Yamasanie928d7d2012-09-17 21:46:51 -07001260 try {
1261 os.close();
1262 } catch (IOException ioe) {
1263 // What the ... !
1264 }
Adrian Roos1bdff912015-02-17 15:51:35 +01001265 tmp.delete();
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001266 } catch (FileNotFoundException e) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001267 Slog.w(LOG_TAG, "Error setting photo for user ", e);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001268 }
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001269 }
1270
Amith Yamasani0b285492011-04-14 17:35:23 -07001271 /**
1272 * Returns an array of user ids. This array is cached here for quick access, so do not modify or
1273 * cache it elsewhere.
1274 * @return the array of user ids.
1275 */
Dianne Hackborn1676c852012-09-10 14:52:30 -07001276 public int[] getUserIds() {
Fyodor Kupolov82402752015-10-28 14:54:51 -07001277 synchronized (mUsersLock) {
Dianne Hackborn7767eac2012-08-23 18:25:40 -07001278 return mUserIds;
1279 }
Amith Yamasani0b285492011-04-14 17:35:23 -07001280 }
1281
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001282 private void readUserListLP() {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001283 if (!mUserListFile.exists()) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001284 fallbackToSingleUserLP();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001285 return;
1286 }
1287 FileInputStream fis = null;
Amith Yamasani2a003292012-08-14 18:25:45 -07001288 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001289 try {
Amith Yamasani2a003292012-08-14 18:25:45 -07001290 fis = userListFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001291 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001292 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001293 int type;
1294 while ((type = parser.next()) != XmlPullParser.START_TAG
1295 && type != XmlPullParser.END_DOCUMENT) {
Amith Yamasani12747872015-12-07 14:19:49 -08001296 // Skip
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001297 }
1298
1299 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001300 Slog.e(LOG_TAG, "Unable to read user list");
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001301 fallbackToSingleUserLP();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001302 return;
1303 }
1304
Amith Yamasani2a003292012-08-14 18:25:45 -07001305 mNextSerialNumber = -1;
1306 if (parser.getName().equals(TAG_USERS)) {
1307 String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
1308 if (lastSerialNumber != null) {
1309 mNextSerialNumber = Integer.parseInt(lastSerialNumber);
1310 }
Amith Yamasani6f34b412012-10-22 18:19:27 -07001311 String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
1312 if (versionNumber != null) {
1313 mUserVersion = Integer.parseInt(versionNumber);
1314 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001315 }
1316
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001317 final Bundle newDevicePolicyGlobalUserRestrictions = new Bundle();
1318
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001319 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301320 if (type == XmlPullParser.START_TAG) {
1321 final String name = parser.getName();
1322 if (name.equals(TAG_USER)) {
1323 String id = parser.getAttributeValue(null, ATTR_ID);
Amith Yamasani6f34b412012-10-22 18:19:27 -07001324
Amith Yamasani12747872015-12-07 14:19:49 -08001325 UserData userData = readUserLP(Integer.parseInt(id));
1326
1327 if (userData != null) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07001328 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08001329 mUsers.put(userData.info.id, userData);
1330 if (mNextSerialNumber < 0
1331 || mNextSerialNumber <= userData.info.id) {
1332 mNextSerialNumber = userData.info.id + 1;
Fyodor Kupolov82402752015-10-28 14:54:51 -07001333 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301334 }
Amith Yamasani2a003292012-08-14 18:25:45 -07001335 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301336 } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
Amith Yamasanida0b1682014-11-21 12:58:17 -08001337 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1338 && type != XmlPullParser.END_TAG) {
1339 if (type == XmlPullParser.START_TAG) {
1340 if (parser.getName().equals(TAG_RESTRICTIONS)) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001341 synchronized (mGuestRestrictions) {
1342 UserRestrictionsUtils
1343 .readRestrictions(parser, mGuestRestrictions);
1344 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001345 } else if (parser.getName().equals(TAG_DEVICE_POLICY_RESTRICTIONS)
1346 ) {
1347 UserRestrictionsUtils.readRestrictions(parser,
1348 newDevicePolicyGlobalUserRestrictions);
Amith Yamasanida0b1682014-11-21 12:58:17 -08001349 }
1350 break;
1351 }
1352 }
Amith Yamasani258848d2012-08-10 17:06:33 -07001353 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001354 }
1355 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001356 synchronized (mRestrictionsLock) {
1357 mDevicePolicyGlobalUserRestrictions = newDevicePolicyGlobalUserRestrictions;
1358 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07001359 updateUserIds();
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001360 upgradeIfNecessaryLP();
1361 } catch (IOException | XmlPullParserException e) {
1362 fallbackToSingleUserLP();
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001363 } finally {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001364 IoUtils.closeQuietly(fis);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001365 }
1366 }
1367
Amith Yamasani6f34b412012-10-22 18:19:27 -07001368 /**
Amith Yamasanibc9625052012-11-15 14:39:18 -08001369 * Upgrade steps between versions, either for fixing bugs or changing the data format.
Amith Yamasani6f34b412012-10-22 18:19:27 -07001370 */
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001371 private void upgradeIfNecessaryLP() {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001372 final int originalVersion = mUserVersion;
Amith Yamasani6f34b412012-10-22 18:19:27 -07001373 int userVersion = mUserVersion;
1374 if (userVersion < 1) {
1375 // Assign a proper name for the owner, if not initialized correctly before
Amith Yamasani12747872015-12-07 14:19:49 -08001376 UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM);
1377 if ("Primary".equals(userData.info.name)) {
1378 userData.info.name =
1379 mContext.getResources().getString(com.android.internal.R.string.owner_name);
1380 scheduleWriteUser(userData);
Amith Yamasani6f34b412012-10-22 18:19:27 -07001381 }
1382 userVersion = 1;
1383 }
1384
Amith Yamasanibc9625052012-11-15 14:39:18 -08001385 if (userVersion < 2) {
1386 // Owner should be marked as initialized
Amith Yamasani12747872015-12-07 14:19:49 -08001387 UserData userData = getUserDataNoChecks(UserHandle.USER_SYSTEM);
1388 if ((userData.info.flags & UserInfo.FLAG_INITIALIZED) == 0) {
1389 userData.info.flags |= UserInfo.FLAG_INITIALIZED;
1390 scheduleWriteUser(userData);
Amith Yamasanibc9625052012-11-15 14:39:18 -08001391 }
1392 userVersion = 2;
1393 }
1394
Amith Yamasani350962c2013-08-06 11:18:53 -07001395
Amith Yamasani5e486f52013-08-07 11:06:44 -07001396 if (userVersion < 4) {
Amith Yamasani5e486f52013-08-07 11:06:44 -07001397 userVersion = 4;
1398 }
1399
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001400 if (userVersion < 5) {
1401 initDefaultGuestRestrictions();
1402 userVersion = 5;
1403 }
1404
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001405 if (userVersion < 6) {
1406 final boolean splitSystemUser = UserManager.isSplitSystemUser();
Fyodor Kupolov82402752015-10-28 14:54:51 -07001407 synchronized (mUsersLock) {
1408 for (int i = 0; i < mUsers.size(); i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08001409 UserData userData = mUsers.valueAt(i);
Fyodor Kupolov82402752015-10-28 14:54:51 -07001410 // In non-split mode, only user 0 can have restricted profiles
Amith Yamasani12747872015-12-07 14:19:49 -08001411 if (!splitSystemUser && userData.info.isRestricted()
1412 && (userData.info.restrictedProfileParentId
1413 == UserInfo.NO_PROFILE_GROUP_ID)) {
1414 userData.info.restrictedProfileParentId = UserHandle.USER_SYSTEM;
1415 scheduleWriteUser(userData);
Fyodor Kupolov82402752015-10-28 14:54:51 -07001416 }
Fyodor Kupolov1c363152015-09-02 13:27:21 -07001417 }
1418 }
1419 userVersion = 6;
1420 }
1421
Amith Yamasani6f34b412012-10-22 18:19:27 -07001422 if (userVersion < USER_VERSION) {
1423 Slog.w(LOG_TAG, "User version " + mUserVersion + " didn't upgrade as expected to "
1424 + USER_VERSION);
1425 } else {
1426 mUserVersion = userVersion;
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001427
1428 if (originalVersion < mUserVersion) {
1429 writeUserListLP();
1430 }
Amith Yamasani6f34b412012-10-22 18:19:27 -07001431 }
1432 }
1433
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001434 private void fallbackToSingleUserLP() {
Xiaohui Chenb31e14a2015-07-13 16:04:55 -07001435 int flags = UserInfo.FLAG_INITIALIZED;
1436 // In split system user mode, the admin and primary flags are assigned to the first human
1437 // user.
1438 if (!UserManager.isSplitSystemUser()) {
1439 flags |= UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY;
1440 }
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001441 // Create the system user
Xiaohui Chen70f6c382015-04-28 14:21:43 -07001442 UserInfo system = new UserInfo(UserHandle.USER_SYSTEM,
Amith Yamasani6f34b412012-10-22 18:19:27 -07001443 mContext.getResources().getString(com.android.internal.R.string.owner_name), null,
Xiaohui Chenb31e14a2015-07-13 16:04:55 -07001444 flags);
Amith Yamasani12747872015-12-07 14:19:49 -08001445 UserData userData = new UserData();
1446 userData.info = system;
Fyodor Kupolov82402752015-10-28 14:54:51 -07001447 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08001448 mUsers.put(system.id, userData);
Fyodor Kupolov82402752015-10-28 14:54:51 -07001449 }
Amith Yamasani634cf312012-10-04 17:34:21 -07001450 mNextSerialNumber = MIN_USER_ID;
Geoffrey Borggaard15b8b2c2013-08-28 22:11:10 -04001451 mUserVersion = USER_VERSION;
Amith Yamasani67df64b2012-12-14 12:09:36 -08001452
Geoffrey Borggaarde45e45e32013-01-24 10:03:20 -05001453 Bundle restrictions = new Bundle();
Makoto Onuki068c54a2015-10-13 14:34:03 -07001454 synchronized (mRestrictionsLock) {
1455 mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions);
1456 }
Amith Yamasani67df64b2012-12-14 12:09:36 -08001457
Fyodor Kupolov82402752015-10-28 14:54:51 -07001458 updateUserIds();
Amith Yamasaniaa6634e2014-10-06 14:20:28 -07001459 initDefaultGuestRestrictions();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001460
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001461 writeUserListLP();
Amith Yamasani12747872015-12-07 14:19:49 -08001462 writeUserLP(userData);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001463 }
1464
Amith Yamasani12747872015-12-07 14:19:49 -08001465 private void scheduleWriteUser(UserData UserData) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001466 if (DBG) {
1467 debug("scheduleWriteUser");
1468 }
Makoto Onuki9e935a32015-11-06 14:24:24 -08001469 // No need to wrap it within a lock -- worst case, we'll just post the same message
1470 // twice.
Amith Yamasani12747872015-12-07 14:19:49 -08001471 if (!mHandler.hasMessages(WRITE_USER_MSG, UserData)) {
1472 Message msg = mHandler.obtainMessage(WRITE_USER_MSG, UserData);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08001473 mHandler.sendMessageDelayed(msg, WRITE_USER_DELAY);
1474 }
1475 }
1476
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001477 /*
1478 * Writes the user file in this format:
1479 *
1480 * <user flags="20039023" id="0">
1481 * <name>Primary</name>
1482 * </user>
1483 */
Amith Yamasani12747872015-12-07 14:19:49 -08001484 private void writeUserLP(UserData userData) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001485 if (DBG) {
Amith Yamasani12747872015-12-07 14:19:49 -08001486 debug("writeUserLP " + userData);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001487 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001488 FileOutputStream fos = null;
Amith Yamasani12747872015-12-07 14:19:49 -08001489 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userData.info.id + XML_SUFFIX));
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001490 try {
Amith Yamasani2a003292012-08-14 18:25:45 -07001491 fos = userFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001492 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1493
1494 // XmlSerializer serializer = XmlUtils.serializerInstance();
1495 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001496 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001497 serializer.startDocument(null, true);
1498 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1499
Amith Yamasani12747872015-12-07 14:19:49 -08001500 final UserInfo userInfo = userData.info;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001501 serializer.startTag(null, TAG_USER);
1502 serializer.attribute(null, ATTR_ID, Integer.toString(userInfo.id));
Amith Yamasani2a003292012-08-14 18:25:45 -07001503 serializer.attribute(null, ATTR_SERIAL_NO, Integer.toString(userInfo.serialNumber));
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001504 serializer.attribute(null, ATTR_FLAGS, Integer.toString(userInfo.flags));
Amith Yamasani920ace02012-09-20 22:15:37 -07001505 serializer.attribute(null, ATTR_CREATION_TIME, Long.toString(userInfo.creationTime));
1506 serializer.attribute(null, ATTR_LAST_LOGGED_IN_TIME,
1507 Long.toString(userInfo.lastLoggedInTime));
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001508 if (userInfo.iconPath != null) {
1509 serializer.attribute(null, ATTR_ICON_PATH, userInfo.iconPath);
1510 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001511 if (userInfo.partial) {
1512 serializer.attribute(null, ATTR_PARTIAL, "true");
1513 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001514 if (userInfo.guestToRemove) {
1515 serializer.attribute(null, ATTR_GUEST_TO_REMOVE, "true");
1516 }
Kenny Guy2a764942014-04-02 13:29:20 +01001517 if (userInfo.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID) {
1518 serializer.attribute(null, ATTR_PROFILE_GROUP_ID,
1519 Integer.toString(userInfo.profileGroupId));
Kenny Guya52dc3e2014-02-11 15:33:14 +00001520 }
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001521 if (userInfo.restrictedProfileParentId != UserInfo.NO_PROFILE_GROUP_ID) {
1522 serializer.attribute(null, ATTR_RESTRICTED_PROFILE_PARENT_ID,
1523 Integer.toString(userInfo.restrictedProfileParentId));
1524 }
Amith Yamasani12747872015-12-07 14:19:49 -08001525 // Write seed data
1526 if (userData.persistSeedData) {
1527 if (userData.seedAccountName != null) {
1528 serializer.attribute(null, ATTR_SEED_ACCOUNT_NAME, userData.seedAccountName);
1529 }
1530 if (userData.seedAccountType != null) {
1531 serializer.attribute(null, ATTR_SEED_ACCOUNT_TYPE, userData.seedAccountType);
1532 }
1533 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001534 serializer.startTag(null, TAG_NAME);
1535 serializer.text(userInfo.name);
1536 serializer.endTag(null, TAG_NAME);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001537 synchronized (mRestrictionsLock) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001538 UserRestrictionsUtils.writeRestrictions(serializer,
1539 mBaseUserRestrictions.get(userInfo.id), TAG_RESTRICTIONS);
1540 UserRestrictionsUtils.writeRestrictions(serializer,
1541 mDevicePolicyLocalUserRestrictions.get(userInfo.id),
1542 TAG_DEVICE_POLICY_RESTRICTIONS);
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001543 }
Amith Yamasani12747872015-12-07 14:19:49 -08001544
1545 if (userData.account != null) {
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001546 serializer.startTag(null, TAG_ACCOUNT);
Amith Yamasani12747872015-12-07 14:19:49 -08001547 serializer.text(userData.account);
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001548 serializer.endTag(null, TAG_ACCOUNT);
1549 }
1550
Amith Yamasani12747872015-12-07 14:19:49 -08001551 if (userData.persistSeedData && userData.seedAccountOptions != null) {
1552 serializer.startTag(null, TAG_SEED_ACCOUNT_OPTIONS);
1553 userData.seedAccountOptions.saveToXml(serializer);
1554 serializer.endTag(null, TAG_SEED_ACCOUNT_OPTIONS);
1555 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001556 serializer.endTag(null, TAG_USER);
1557
1558 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -07001559 userFile.finishWrite(fos);
1560 } catch (Exception ioe) {
Amith Yamasani12747872015-12-07 14:19:49 -08001561 Slog.e(LOG_TAG, "Error writing user info " + userData.info.id + "\n" + ioe);
Amith Yamasani2a003292012-08-14 18:25:45 -07001562 userFile.failWrite(fos);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001563 }
1564 }
1565
1566 /*
1567 * Writes the user list file in this format:
1568 *
Amith Yamasani2a003292012-08-14 18:25:45 -07001569 * <users nextSerialNumber="3">
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001570 * <user id="0"></user>
1571 * <user id="2"></user>
1572 * </users>
1573 */
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001574 private void writeUserListLP() {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001575 if (DBG) {
1576 debug("writeUserList");
1577 }
Amith Yamasani742a6712011-05-04 14:49:28 -07001578 FileOutputStream fos = null;
Amith Yamasani2a003292012-08-14 18:25:45 -07001579 AtomicFile userListFile = new AtomicFile(mUserListFile);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001580 try {
Amith Yamasani2a003292012-08-14 18:25:45 -07001581 fos = userListFile.startWrite();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001582 final BufferedOutputStream bos = new BufferedOutputStream(fos);
1583
1584 // XmlSerializer serializer = XmlUtils.serializerInstance();
1585 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001586 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001587 serializer.startDocument(null, true);
1588 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
1589
1590 serializer.startTag(null, TAG_USERS);
Amith Yamasani2a003292012-08-14 18:25:45 -07001591 serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
Amith Yamasani6f34b412012-10-22 18:19:27 -07001592 serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001593
Adam Lesinskieddeb492014-09-08 17:50:03 -07001594 serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001595 synchronized (mGuestRestrictions) {
1596 UserRestrictionsUtils
1597 .writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS);
1598 }
Amith Yamasanie4afaa32014-06-30 14:55:07 +05301599 serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001600 synchronized (mRestrictionsLock) {
1601 UserRestrictionsUtils.writeRestrictions(serializer,
1602 mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS);
1603 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07001604 int[] userIdsToWrite;
1605 synchronized (mUsersLock) {
1606 userIdsToWrite = new int[mUsers.size()];
1607 for (int i = 0; i < userIdsToWrite.length; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08001608 UserInfo user = mUsers.valueAt(i).info;
Fyodor Kupolov82402752015-10-28 14:54:51 -07001609 userIdsToWrite[i] = user.id;
1610 }
1611 }
1612 for (int id : userIdsToWrite) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001613 serializer.startTag(null, TAG_USER);
Fyodor Kupolov82402752015-10-28 14:54:51 -07001614 serializer.attribute(null, ATTR_ID, Integer.toString(id));
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001615 serializer.endTag(null, TAG_USER);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001616 }
1617
1618 serializer.endTag(null, TAG_USERS);
1619
1620 serializer.endDocument();
Amith Yamasani2a003292012-08-14 18:25:45 -07001621 userListFile.finishWrite(fos);
1622 } catch (Exception e) {
1623 userListFile.failWrite(fos);
Amith Yamasani0b285492011-04-14 17:35:23 -07001624 Slog.e(LOG_TAG, "Error writing user list");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001625 }
1626 }
1627
Amith Yamasani12747872015-12-07 14:19:49 -08001628 private UserData readUserLP(int id) {
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001629 int flags = 0;
Amith Yamasani2a003292012-08-14 18:25:45 -07001630 int serialNumber = id;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001631 String name = null;
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001632 String account = null;
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001633 String iconPath = null;
Amith Yamasani920ace02012-09-20 22:15:37 -07001634 long creationTime = 0L;
1635 long lastLoggedInTime = 0L;
Kenny Guy2a764942014-04-02 13:29:20 +01001636 int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001637 int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001638 boolean partial = false;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001639 boolean guestToRemove = false;
Amith Yamasani12747872015-12-07 14:19:49 -08001640 boolean persistSeedData = false;
1641 String seedAccountName = null;
1642 String seedAccountType = null;
1643 PersistableBundle seedAccountOptions = null;
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001644 Bundle baseRestrictions = new Bundle();
1645 Bundle localRestrictions = new Bundle();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001646
1647 FileInputStream fis = null;
1648 try {
Amith Yamasani2a003292012-08-14 18:25:45 -07001649 AtomicFile userFile =
Amith Yamasanifc95e702013-09-26 13:20:17 -07001650 new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
Amith Yamasani2a003292012-08-14 18:25:45 -07001651 fis = userFile.openRead();
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001652 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01001653 parser.setInput(fis, StandardCharsets.UTF_8.name());
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001654 int type;
1655 while ((type = parser.next()) != XmlPullParser.START_TAG
1656 && type != XmlPullParser.END_DOCUMENT) {
Amith Yamasani12747872015-12-07 14:19:49 -08001657 // Skip
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001658 }
1659
1660 if (type != XmlPullParser.START_TAG) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001661 Slog.e(LOG_TAG, "Unable to read user " + id);
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001662 return null;
1663 }
1664
1665 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
Amith Yamasani920ace02012-09-20 22:15:37 -07001666 int storedId = readIntAttribute(parser, ATTR_ID, -1);
1667 if (storedId != id) {
Amith Yamasani0b285492011-04-14 17:35:23 -07001668 Slog.e(LOG_TAG, "User id does not match the file name");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001669 return null;
1670 }
Amith Yamasani920ace02012-09-20 22:15:37 -07001671 serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
1672 flags = readIntAttribute(parser, ATTR_FLAGS, 0);
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001673 iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
Amith Yamasani920ace02012-09-20 22:15:37 -07001674 creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
1675 lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
Kenny Guy2a764942014-04-02 13:29:20 +01001676 profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID,
1677 UserInfo.NO_PROFILE_GROUP_ID);
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001678 restrictedProfileParentId = readIntAttribute(parser,
1679 ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001680 String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
1681 if ("true".equals(valueString)) {
1682 partial = true;
1683 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001684 valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
1685 if ("true".equals(valueString)) {
1686 guestToRemove = true;
1687 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001688
Amith Yamasani12747872015-12-07 14:19:49 -08001689 seedAccountName = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_NAME);
1690 seedAccountType = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_TYPE);
1691 if (seedAccountName != null || seedAccountType != null) {
1692 persistSeedData = true;
1693 }
1694
Amith Yamasanie4cf7342012-12-17 11:12:09 -08001695 int outerDepth = parser.getDepth();
1696 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
1697 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
1698 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
1699 continue;
1700 }
1701 String tag = parser.getName();
1702 if (TAG_NAME.equals(tag)) {
1703 type = parser.next();
1704 if (type == XmlPullParser.TEXT) {
1705 name = parser.getText();
1706 }
1707 } else if (TAG_RESTRICTIONS.equals(tag)) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001708 UserRestrictionsUtils.readRestrictions(parser, baseRestrictions);
1709 } else if (TAG_DEVICE_POLICY_RESTRICTIONS.equals(tag)) {
1710 UserRestrictionsUtils.readRestrictions(parser, localRestrictions);
Xiaohui Chenb3b92582015-12-07 11:22:13 -08001711 } else if (TAG_ACCOUNT.equals(tag)) {
1712 type = parser.next();
1713 if (type == XmlPullParser.TEXT) {
1714 account = parser.getText();
1715 }
Amith Yamasani12747872015-12-07 14:19:49 -08001716 } else if (TAG_SEED_ACCOUNT_OPTIONS.equals(tag)) {
1717 seedAccountOptions = PersistableBundle.restoreFromXml(parser);
1718 persistSeedData = true;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001719 }
1720 }
1721 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001722
Amith Yamasani12747872015-12-07 14:19:49 -08001723 // Create the UserInfo object that gets passed around
Amith Yamasanib8151ec2012-04-18 18:02:48 -07001724 UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
Amith Yamasani2a003292012-08-14 18:25:45 -07001725 userInfo.serialNumber = serialNumber;
Amith Yamasani920ace02012-09-20 22:15:37 -07001726 userInfo.creationTime = creationTime;
1727 userInfo.lastLoggedInTime = lastLoggedInTime;
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07001728 userInfo.partial = partial;
Adam Lesinskieddeb492014-09-08 17:50:03 -07001729 userInfo.guestToRemove = guestToRemove;
Kenny Guy2a764942014-04-02 13:29:20 +01001730 userInfo.profileGroupId = profileGroupId;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001731 userInfo.restrictedProfileParentId = restrictedProfileParentId;
Amith Yamasani12747872015-12-07 14:19:49 -08001732
1733 // Create the UserData object that's internal to this class
1734 UserData userData = new UserData();
1735 userData.info = userInfo;
1736 userData.account = account;
1737 userData.seedAccountName = seedAccountName;
1738 userData.seedAccountType = seedAccountType;
1739 userData.persistSeedData = persistSeedData;
1740 userData.seedAccountOptions = seedAccountOptions;
1741
Makoto Onuki068c54a2015-10-13 14:34:03 -07001742 synchronized (mRestrictionsLock) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08001743 mBaseUserRestrictions.put(id, baseRestrictions);
1744 mDevicePolicyLocalUserRestrictions.put(id, localRestrictions);
Makoto Onuki068c54a2015-10-13 14:34:03 -07001745 }
Amith Yamasani12747872015-12-07 14:19:49 -08001746 return userData;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001747 } catch (IOException ioe) {
1748 } catch (XmlPullParserException pe) {
Dianne Hackbornbfd89b32011-12-15 18:22:54 -08001749 } finally {
1750 if (fis != null) {
1751 try {
1752 fis.close();
1753 } catch (IOException e) {
1754 }
1755 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001756 }
1757 return null;
1758 }
1759
Amith Yamasani920ace02012-09-20 22:15:37 -07001760 private int readIntAttribute(XmlPullParser parser, String attr, int defaultValue) {
1761 String valueString = parser.getAttributeValue(null, attr);
1762 if (valueString == null) return defaultValue;
1763 try {
1764 return Integer.parseInt(valueString);
1765 } catch (NumberFormatException nfe) {
1766 return defaultValue;
1767 }
1768 }
1769
1770 private long readLongAttribute(XmlPullParser parser, String attr, long defaultValue) {
1771 String valueString = parser.getAttributeValue(null, attr);
1772 if (valueString == null) return defaultValue;
1773 try {
1774 return Long.parseLong(valueString);
1775 } catch (NumberFormatException nfe) {
1776 return defaultValue;
1777 }
1778 }
1779
Amith Yamasanib82add22013-07-09 11:24:44 -07001780 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001781 * Removes the app restrictions file for a specific package and user id, if it exists.
1782 */
1783 private void cleanAppRestrictionsForPackage(String pkg, int userId) {
1784 synchronized (mPackagesLock) {
1785 File dir = Environment.getUserSystemDirectory(userId);
Amith Yamasanifc95e702013-09-26 13:20:17 -07001786 File resFile = new File(dir, packageToRestrictionsFileName(pkg));
Amith Yamasani1a7472e2013-07-02 11:17:30 -07001787 if (resFile.exists()) {
1788 resFile.delete();
1789 }
1790 }
1791 }
1792
Kenny Guya52dc3e2014-02-11 15:33:14 +00001793 @Override
Kenny Guy2a764942014-04-02 13:29:20 +01001794 public UserInfo createProfileForUser(String name, int flags, int userId) {
Kenny Guya52dc3e2014-02-11 15:33:14 +00001795 checkManageUsersPermission("Only the system can create users");
Kenny Guy2a764942014-04-02 13:29:20 +01001796 return createUserInternal(name, flags, userId);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001797 }
1798
Amith Yamasani258848d2012-08-10 17:06:33 -07001799 @Override
Amith Yamasani13593602012-03-22 16:16:17 -07001800 public UserInfo createUser(String name, int flags) {
Amith Yamasani2a003292012-08-14 18:25:45 -07001801 checkManageUsersPermission("Only the system can create users");
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001802 return createUserInternal(name, flags, UserHandle.USER_NULL);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001803 }
Amith Yamasanifaea76f2012-09-11 10:59:48 -07001804
Jessica Hummelbe81c802014-04-22 15:49:22 +01001805 private UserInfo createUserInternal(String name, int flags, int parentId) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001806 if (hasUserRestriction(UserManager.DISALLOW_ADD_USER, UserHandle.getCallingUserId())) {
Julia Reynolds75175022014-06-26 16:35:00 -04001807 Log.w(LOG_TAG, "Cannot add user. DISALLOW_ADD_USER is enabled.");
1808 return null;
1809 }
phweisse9c44062016-02-10 12:57:38 +01001810 return createUserInternalUnchecked(name, flags, parentId);
1811 }
1812
1813 private UserInfo createUserInternalUnchecked(String name, int flags, int parentId) {
Nicolas Prevot12678a92015-05-13 12:15:03 -07001814 if (ActivityManager.isLowRamDeviceStatic()) {
1815 return null;
1816 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001817 final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
Nicolas Prevot12678a92015-05-13 12:15:03 -07001818 final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
Fyodor Kupolov06a484a2015-08-21 16:33:20 -07001819 final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001820 final long ident = Binder.clearCallingIdentity();
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001821 UserInfo userInfo;
Amith Yamasani12747872015-12-07 14:19:49 -08001822 UserData userData;
Amith Yamasanibb054c92015-07-09 14:16:27 -07001823 final int userId;
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001824 try {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001825 synchronized (mPackagesLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08001826 UserData parent = null;
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001827 if (parentId != UserHandle.USER_NULL) {
1828 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08001829 parent = getUserDataLU(parentId);
Nicolas Prevotc6d033e2014-02-27 13:11:09 +00001830 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001831 if (parent == null) return null;
1832 }
1833 if (isManagedProfile && !canAddMoreManagedProfiles(parentId, false)) {
1834 Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId);
1835 return null;
1836 }
1837 if (!isGuest && !isManagedProfile && isUserLimitReached()) {
1838 // If we're not adding a guest user or a managed profile and the limit has
1839 // been reached, cannot add a user.
1840 return null;
1841 }
1842 // If we're adding a guest and there already exists one, bail.
1843 if (isGuest && findCurrentGuestUser() != null) {
1844 return null;
1845 }
1846 // In legacy mode, restricted profile's parent can only be the owner user
1847 if (isRestricted && !UserManager.isSplitSystemUser()
1848 && (parentId != UserHandle.USER_SYSTEM)) {
1849 Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be owner");
1850 return null;
1851 }
1852 if (isRestricted && UserManager.isSplitSystemUser()) {
1853 if (parent == null) {
1854 Log.w(LOG_TAG, "Cannot add restricted profile - parent user must be "
1855 + "specified");
Nicolas Prevot12678a92015-05-13 12:15:03 -07001856 return null;
1857 }
Amith Yamasani12747872015-12-07 14:19:49 -08001858 if (!parent.info.canHaveProfile()) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001859 Log.w(LOG_TAG, "Cannot add restricted profile - profiles cannot be "
1860 + "created for the specified parent user id " + parentId);
Amith Yamasani95ab7842014-08-11 17:09:26 -07001861 return null;
1862 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001863 }
Lenka Trochtova024f9792016-02-17 13:55:17 +01001864 if (!UserManager.isSplitSystemUser() && (flags & UserInfo.FLAG_EPHEMERAL) != 0) {
1865 Log.e(LOG_TAG,
1866 "Ephemeral users are supported on split-system-user systems only.");
1867 return null;
1868 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001869 // In split system user mode, we assign the first human user the primary flag.
1870 // And if there is no device owner, we also assign the admin flag to primary user.
1871 if (UserManager.isSplitSystemUser()
1872 && !isGuest && !isManagedProfile && getPrimaryUser() == null) {
1873 flags |= UserInfo.FLAG_PRIMARY;
Makoto Onukie7927da2015-11-25 10:05:17 -08001874 synchronized (mUsersLock) {
1875 if (!mIsDeviceManaged) {
1876 flags |= UserInfo.FLAG_ADMIN;
1877 }
Amith Yamasani95ab7842014-08-11 17:09:26 -07001878 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001879 }
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001880
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +01001881 userId = getNextAvailableId();
1882 Environment.getUserSystemDirectory(userId).mkdirs();
Lenka Trochtova02fee152015-12-22 14:26:18 +01001883 boolean ephemeralGuests = Resources.getSystem()
1884 .getBoolean(com.android.internal.R.bool.config_guestUserEphemeral);
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +01001885
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001886 synchronized (mUsersLock) {
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +01001887 // Add ephemeral flag to guests/users if required. Also inherit it from parent.
1888 if ((isGuest && ephemeralGuests) || mForceEphemeralUsers
1889 || (parent != null && parent.info.isEphemeral())) {
1890 flags |= UserInfo.FLAG_EPHEMERAL;
1891 }
1892
1893 userInfo = new UserInfo(userId, name, null, flags);
1894 userInfo.serialNumber = mNextSerialNumber++;
1895 long now = System.currentTimeMillis();
1896 userInfo.creationTime = (now > EPOCH_PLUS_30_YEARS) ? now : 0;
1897 userInfo.partial = true;
1898 userData = new UserData();
1899 userData.info = userInfo;
Amith Yamasani12747872015-12-07 14:19:49 -08001900 mUsers.put(userId, userData);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001901 }
1902 writeUserListLP();
1903 if (parent != null) {
1904 if (isManagedProfile) {
Amith Yamasani12747872015-12-07 14:19:49 -08001905 if (parent.info.profileGroupId == UserInfo.NO_PROFILE_GROUP_ID) {
1906 parent.info.profileGroupId = parent.info.id;
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001907 writeUserLP(parent);
Kenny Guya52dc3e2014-02-11 15:33:14 +00001908 }
Amith Yamasani12747872015-12-07 14:19:49 -08001909 userInfo.profileGroupId = parent.info.profileGroupId;
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001910 } else if (isRestricted) {
Amith Yamasani12747872015-12-07 14:19:49 -08001911 if (parent.info.restrictedProfileParentId == UserInfo.NO_PROFILE_GROUP_ID) {
1912 parent.info.restrictedProfileParentId = parent.info.id;
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001913 writeUserLP(parent);
Jeff Sharkey6dce4962015-07-03 18:08:41 -07001914 }
Amith Yamasani12747872015-12-07 14:19:49 -08001915 userInfo.restrictedProfileParentId = parent.info.restrictedProfileParentId;
Makoto Onuki068c54a2015-10-13 14:34:03 -07001916 }
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001917 }
Dianne Hackborn4428e172012-08-24 17:43:05 -07001918 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001919 final StorageManager storage = mContext.getSystemService(StorageManager.class);
Lenka Trochtovac4dd0212015-11-18 12:22:06 +01001920 storage.createUserKey(userId, userInfo.serialNumber, userInfo.isEphemeral());
Jeff Sharkey47f71082016-02-01 17:03:54 -07001921 prepareUserStorage(userId, userInfo.serialNumber,
1922 StorageManager.FLAG_STORAGE_DE | StorageManager.FLAG_STORAGE_CE);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001923 mPm.createNewUser(userId);
1924 userInfo.partial = false;
1925 synchronized (mPackagesLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08001926 writeUserLP(userData);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001927 }
1928 updateUserIds();
1929 Bundle restrictions = new Bundle();
Fyodor Kupolove04462c2015-11-30 15:02:53 -08001930 if (isGuest) {
1931 synchronized (mGuestRestrictions) {
1932 restrictions.putAll(mGuestRestrictions);
1933 }
1934 }
Fyodor Kupolove80085d2015-11-06 18:21:39 -08001935 synchronized (mRestrictionsLock) {
1936 mBaseUserRestrictions.append(userId, restrictions);
1937 }
1938 mPm.newUserCreated(userId);
1939 Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
1940 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
1941 mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
1942 android.Manifest.permission.MANAGE_USERS);
Fyodor Kupolov8385e4b2015-12-29 18:15:32 -08001943 MetricsLogger.count(mContext, isGuest ? TRON_GUEST_CREATED : TRON_USER_CREATED, 1);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07001944 } finally {
1945 Binder.restoreCallingIdentity(ident);
Amith Yamasani258848d2012-08-10 17:06:33 -07001946 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07001947 return userInfo;
1948 }
1949
Amith Yamasani0b285492011-04-14 17:35:23 -07001950 /**
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001951 * @hide
1952 */
Amith Yamasani12747872015-12-07 14:19:49 -08001953 @Override
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001954 public UserInfo createRestrictedProfile(String name, int parentUserId) {
1955 checkManageUsersPermission("setupRestrictedProfile");
1956 final UserInfo user = createProfileForUser(name, UserInfo.FLAG_RESTRICTED, parentUserId);
1957 if (user == null) {
1958 return null;
1959 }
Fyodor Kupolov9e912ba2016-02-09 18:29:43 -08001960 long identity = Binder.clearCallingIdentity();
1961 try {
1962 setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user.id);
1963 // Change the setting before applying the DISALLOW_SHARE_LOCATION restriction, otherwise
1964 // the putIntForUser() will fail.
1965 android.provider.Settings.Secure.putIntForUser(mContext.getContentResolver(),
1966 android.provider.Settings.Secure.LOCATION_MODE,
1967 android.provider.Settings.Secure.LOCATION_MODE_OFF, user.id);
1968 setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, true, user.id);
1969 } finally {
1970 Binder.restoreCallingIdentity(identity);
1971 }
Fyodor Kupolov02cb6e72015-09-18 18:20:55 -07001972 return user;
1973 }
1974
1975 /**
Adam Lesinskieddeb492014-09-08 17:50:03 -07001976 * Find the current guest user. If the Guest user is partial,
1977 * then do not include it in the results as it is about to die.
Adam Lesinskieddeb492014-09-08 17:50:03 -07001978 */
Fyodor Kupolov82402752015-10-28 14:54:51 -07001979 private UserInfo findCurrentGuestUser() {
1980 synchronized (mUsersLock) {
1981 final int size = mUsers.size();
1982 for (int i = 0; i < size; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08001983 final UserInfo user = mUsers.valueAt(i).info;
Fyodor Kupolov82402752015-10-28 14:54:51 -07001984 if (user.isGuest() && !user.guestToRemove && !mRemovingUserIds.get(user.id)) {
1985 return user;
1986 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07001987 }
1988 }
1989 return null;
1990 }
1991
1992 /**
Amith Yamasani1df14732014-08-29 21:37:27 -07001993 * Mark this guest user for deletion to allow us to create another guest
1994 * and switch to that user before actually removing this guest.
1995 * @param userHandle the userid of the current guest
1996 * @return whether the user could be marked for deletion
1997 */
Amith Yamasani12747872015-12-07 14:19:49 -08001998 @Override
Amith Yamasani1df14732014-08-29 21:37:27 -07001999 public boolean markGuestForDeletion(int userHandle) {
2000 checkManageUsersPermission("Only the system can remove users");
2001 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
2002 UserManager.DISALLOW_REMOVE_USER, false)) {
2003 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
2004 return false;
2005 }
2006
2007 long ident = Binder.clearCallingIdentity();
2008 try {
Amith Yamasani12747872015-12-07 14:19:49 -08002009 final UserData userData;
Amith Yamasani1df14732014-08-29 21:37:27 -07002010 synchronized (mPackagesLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002011 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08002012 userData = mUsers.get(userHandle);
2013 if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002014 return false;
2015 }
Amith Yamasani1df14732014-08-29 21:37:27 -07002016 }
Amith Yamasani12747872015-12-07 14:19:49 -08002017 if (!userData.info.isGuest()) {
Amith Yamasani1df14732014-08-29 21:37:27 -07002018 return false;
2019 }
Adam Lesinskieddeb492014-09-08 17:50:03 -07002020 // We set this to a guest user that is to be removed. This is a temporary state
2021 // where we are allowed to add new Guest users, even if this one is still not
2022 // removed. This user will still show up in getUserInfo() calls.
2023 // If we don't get around to removing this Guest user, it will be purged on next
2024 // startup.
Amith Yamasani12747872015-12-07 14:19:49 -08002025 userData.info.guestToRemove = true;
Amith Yamasani1df14732014-08-29 21:37:27 -07002026 // Mark it as disabled, so that it isn't returned any more when
2027 // profiles are queried.
Amith Yamasani12747872015-12-07 14:19:49 -08002028 userData.info.flags |= UserInfo.FLAG_DISABLED;
2029 writeUserLP(userData);
Amith Yamasani1df14732014-08-29 21:37:27 -07002030 }
2031 } finally {
2032 Binder.restoreCallingIdentity(ident);
2033 }
2034 return true;
2035 }
2036
2037 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07002038 * Removes a user and all data directories created for that user. This method should be called
2039 * after the user's processes have been terminated.
Dianne Hackborn10ad9822014-03-17 11:28:36 -07002040 * @param userHandle the user's id
Amith Yamasani0b285492011-04-14 17:35:23 -07002041 */
Amith Yamasani12747872015-12-07 14:19:49 -08002042 @Override
Amith Yamasani258848d2012-08-10 17:06:33 -07002043 public boolean removeUser(int userHandle) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002044 checkManageUsersPermission("Only the system can remove users");
Julia Reynolds4ac5f852014-06-23 17:38:51 -04002045 if (getUserRestrictions(UserHandle.getCallingUserId()).getBoolean(
2046 UserManager.DISALLOW_REMOVE_USER, false)) {
2047 Log.w(LOG_TAG, "Cannot remove user. DISALLOW_REMOVE_USER is enabled.");
2048 return false;
2049 }
2050
Kenny Guyee58b4f2014-05-23 15:19:53 +01002051 long ident = Binder.clearCallingIdentity();
2052 try {
Amith Yamasani12747872015-12-07 14:19:49 -08002053 final UserData userData;
Fyodor Kupolov0df68cd2015-10-01 13:54:22 -07002054 int currentUser = ActivityManager.getCurrentUser();
2055 if (currentUser == userHandle) {
2056 Log.w(LOG_TAG, "Current user cannot be removed");
2057 return false;
2058 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01002059 synchronized (mPackagesLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002060 synchronized (mUsersLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08002061 userData = mUsers.get(userHandle);
2062 if (userHandle == 0 || userData == null || mRemovingUserIds.get(userHandle)) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002063 return false;
2064 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08002065
Fyodor Kupolov82402752015-10-28 14:54:51 -07002066 // We remember deleted user IDs to prevent them from being
2067 // reused during the current boot; they can still be reused
2068 // after a reboot.
2069 mRemovingUserIds.put(userHandle, true);
2070 }
Jeff Sharkey6eb09392014-11-14 15:57:59 -08002071
Kenny Guyee58b4f2014-05-23 15:19:53 +01002072 try {
2073 mAppOpsService.removeUser(userHandle);
2074 } catch (RemoteException e) {
2075 Log.w(LOG_TAG, "Unable to notify AppOpsService of removing user", e);
2076 }
2077 // Set this to a partially created user, so that the user will be purged
2078 // on next startup, in case the runtime stops now before stopping and
2079 // removing the user completely.
Amith Yamasani12747872015-12-07 14:19:49 -08002080 userData.info.partial = true;
Kenny Guyee58b4f2014-05-23 15:19:53 +01002081 // Mark it as disabled, so that it isn't returned any more when
2082 // profiles are queried.
Amith Yamasani12747872015-12-07 14:19:49 -08002083 userData.info.flags |= UserInfo.FLAG_DISABLED;
2084 writeUserLP(userData);
Kenny Guyee58b4f2014-05-23 15:19:53 +01002085 }
2086
Amith Yamasani12747872015-12-07 14:19:49 -08002087 if (userData.info.profileGroupId != UserInfo.NO_PROFILE_GROUP_ID
2088 && userData.info.isManagedProfile()) {
Kenny Guyee58b4f2014-05-23 15:19:53 +01002089 // Send broadcast to notify system that the user removed was a
2090 // managed user.
Amith Yamasani12747872015-12-07 14:19:49 -08002091 sendProfileRemovedBroadcast(userData.info.profileGroupId, userData.info.id);
Kenny Guyee58b4f2014-05-23 15:19:53 +01002092 }
2093
2094 if (DBG) Slog.i(LOG_TAG, "Stopping user " + userHandle);
2095 int res;
2096 try {
Fyodor Kupolov9cbfc9e2015-10-07 15:52:33 -07002097 res = ActivityManagerNative.getDefault().stopUser(userHandle, /* force= */ true,
2098 new IStopUserCallback.Stub() {
Kenny Guyee58b4f2014-05-23 15:19:53 +01002099 @Override
2100 public void userStopped(int userId) {
2101 finishRemoveUser(userId);
2102 }
2103 @Override
2104 public void userStopAborted(int userId) {
2105 }
2106 });
2107 } catch (RemoteException e) {
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002108 return false;
2109 }
Kenny Guyee58b4f2014-05-23 15:19:53 +01002110 return res == ActivityManager.USER_OP_SUCCESS;
2111 } finally {
2112 Binder.restoreCallingIdentity(ident);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002113 }
Dianne Hackborn80a4af22012-08-27 19:18:31 -07002114 }
2115
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07002116 void finishRemoveUser(final int userHandle) {
Amith Yamasani16389312012-10-17 21:20:14 -07002117 if (DBG) Slog.i(LOG_TAG, "finishRemoveUser " + userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07002118 // Let other services shutdown any activity and clean up their state before completely
2119 // wiping the user's system directory and removing from the user list
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002120 long ident = Binder.clearCallingIdentity();
2121 try {
2122 Intent addedIntent = new Intent(Intent.ACTION_USER_REMOVED);
2123 addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07002124 mContext.sendOrderedBroadcastAsUser(addedIntent, UserHandle.ALL,
2125 android.Manifest.permission.MANAGE_USERS,
2126
2127 new BroadcastReceiver() {
2128 @Override
2129 public void onReceive(Context context, Intent intent) {
2130 if (DBG) {
2131 Slog.i(LOG_TAG,
2132 "USER_REMOVED broadcast sent, cleaning up user data "
2133 + userHandle);
2134 }
2135 new Thread() {
Amith Yamasani12747872015-12-07 14:19:49 -08002136 @Override
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07002137 public void run() {
Amith Yamasani515d4062015-09-28 11:30:06 -07002138 // Clean up any ActivityManager state
2139 LocalServices.getService(ActivityManagerInternal.class)
2140 .onUserRemoved(userHandle);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002141 removeUserState(userHandle);
Amith Yamasanidb6a14c2012-10-17 21:16:52 -07002142 }
2143 }.start();
2144 }
2145 },
2146
2147 null, Activity.RESULT_OK, null, null);
Dianne Hackborn5dc5a002012-09-15 19:33:48 -07002148 } finally {
2149 Binder.restoreCallingIdentity(ident);
2150 }
Amith Yamasani2a003292012-08-14 18:25:45 -07002151 }
2152
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002153 private void removeUserState(final int userHandle) {
Jeff Sharkeyf9fc6d62015-11-08 16:46:05 -08002154 mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002155 // Cleanup package manager settings
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002156 mPm.cleanUpUser(this, userHandle);
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002157
2158 // Remove this user from the list
Fyodor Kupolov82402752015-10-28 14:54:51 -07002159 synchronized (mUsersLock) {
2160 mUsers.remove(userHandle);
Makoto Onukie7927da2015-11-25 10:05:17 -08002161 mIsUserManaged.delete(userHandle);
2162 }
2163 synchronized (mRestrictionsLock) {
2164 mBaseUserRestrictions.remove(userHandle);
2165 mAppliedUserRestrictions.remove(userHandle);
2166 mCachedEffectiveUserRestrictions.remove(userHandle);
2167 mDevicePolicyLocalUserRestrictions.remove(userHandle);
Fyodor Kupolov82402752015-10-28 14:54:51 -07002168 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002169 // Remove user file
Amith Yamasanifc95e702013-09-26 13:20:17 -07002170 AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002171 userFile.delete();
2172 // Update the user list
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002173 synchronized (mPackagesLock) {
2174 writeUserListLP();
2175 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07002176 updateUserIds();
Amith Yamasani12747872015-12-07 14:19:49 -08002177 File userDir = Environment.getUserSystemDirectory(userHandle);
2178 File renamedUserDir = Environment.getUserSystemDirectory(UserHandle.USER_NULL - userHandle);
2179 if (userDir.renameTo(renamedUserDir)) {
2180 removeDirectoryRecursive(renamedUserDir);
2181 } else {
2182 removeDirectoryRecursive(userDir);
2183 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002184 }
2185
Amith Yamasani61f57372012-08-31 12:12:28 -07002186 private void removeDirectoryRecursive(File parent) {
2187 if (parent.isDirectory()) {
2188 String[] files = parent.list();
2189 for (String filename : files) {
2190 File child = new File(parent, filename);
2191 removeDirectoryRecursive(child);
2192 }
2193 }
2194 parent.delete();
2195 }
2196
Kenny Guyf8d3a232014-05-15 16:09:52 +01002197 private void sendProfileRemovedBroadcast(int parentUserId, int removedUserId) {
Adam Connors7b66ed52014-04-14 11:58:10 +01002198 Intent managedProfileIntent = new Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED);
Adam Connorsd4b584e2014-06-09 13:55:47 +01002199 managedProfileIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY |
2200 Intent.FLAG_RECEIVER_FOREGROUND);
Kenny Guyf8d3a232014-05-15 16:09:52 +01002201 managedProfileIntent.putExtra(Intent.EXTRA_USER, new UserHandle(removedUserId));
Jeff Sharkeyaf6ec292015-12-17 11:19:00 -07002202 managedProfileIntent.putExtra(Intent.EXTRA_USER_HANDLE, removedUserId);
Kenny Guyf8d3a232014-05-15 16:09:52 +01002203 mContext.sendBroadcastAsUser(managedProfileIntent, new UserHandle(parentUserId), null);
Adam Connors7b66ed52014-04-14 11:58:10 +01002204 }
2205
Amith Yamasani2a003292012-08-14 18:25:45 -07002206 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002207 public Bundle getApplicationRestrictions(String packageName) {
2208 return getApplicationRestrictionsForUser(packageName, UserHandle.getCallingUserId());
2209 }
2210
2211 @Override
2212 public Bundle getApplicationRestrictionsForUser(String packageName, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002213 if (UserHandle.getCallingUserId() != userId
Amith Yamasani9429afb2013-04-10 18:40:51 -07002214 || !UserHandle.isSameApp(Binder.getCallingUid(), getUidForPackage(packageName))) {
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002215 checkSystemOrRoot("get application restrictions for other users/apps");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002216 }
2217 synchronized (mPackagesLock) {
2218 // Read the restrictions from XML
Fyodor Kupolov82402752015-10-28 14:54:51 -07002219 return readApplicationRestrictionsLP(packageName, userId);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002220 }
2221 }
2222
2223 @Override
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002224 public void setApplicationRestrictions(String packageName, Bundle restrictions,
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002225 int userId) {
Esteban Talavera5b9f1672015-12-11 15:22:34 +00002226 checkSystemOrRoot("set application restrictions");
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002227 synchronized (mPackagesLock) {
Kenny Guyd21b2182014-07-17 16:38:55 +01002228 if (restrictions == null || restrictions.isEmpty()) {
2229 cleanAppRestrictionsForPackage(packageName, userId);
2230 } else {
2231 // Write the restrictions to XML
Fyodor Kupolov82402752015-10-28 14:54:51 -07002232 writeApplicationRestrictionsLP(packageName, restrictions, userId);
Kenny Guyd21b2182014-07-17 16:38:55 +01002233 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002234 }
Robin Lee66e5d962014-04-09 16:44:21 +01002235
Fyodor Kupolovd2846122016-02-11 18:06:34 -08002236 // Notify package of changes via an intent - only sent to explicitly registered receivers.
2237 Intent changeIntent = new Intent(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
2238 changeIntent.setPackage(packageName);
2239 changeIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
2240 mContext.sendBroadcastAsUser(changeIntent, UserHandle.of(userId));
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002241 }
2242
2243 private int getUidForPackage(String packageName) {
Amith Yamasani9429afb2013-04-10 18:40:51 -07002244 long ident = Binder.clearCallingIdentity();
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002245 try {
2246 return mContext.getPackageManager().getApplicationInfo(packageName,
Fyodor Kupolovd2846122016-02-11 18:06:34 -08002247 PackageManager.MATCH_UNINSTALLED_PACKAGES).uid;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002248 } catch (NameNotFoundException nnfe) {
2249 return -1;
Amith Yamasani9429afb2013-04-10 18:40:51 -07002250 } finally {
2251 Binder.restoreCallingIdentity(ident);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002252 }
2253 }
2254
Fyodor Kupolov82402752015-10-28 14:54:51 -07002255 private Bundle readApplicationRestrictionsLP(String packageName, int userId) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002256 AtomicFile restrictionsFile =
2257 new AtomicFile(new File(Environment.getUserSystemDirectory(userId),
2258 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002259 return readApplicationRestrictionsLP(restrictionsFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002260 }
2261
2262 @VisibleForTesting
Fyodor Kupolov82402752015-10-28 14:54:51 -07002263 static Bundle readApplicationRestrictionsLP(AtomicFile restrictionsFile) {
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002264 final Bundle restrictions = new Bundle();
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002265 final ArrayList<String> values = new ArrayList<>();
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07002266 if (!restrictionsFile.getBaseFile().exists()) {
Fyodor Kupolovf6ee2242015-04-06 10:15:07 -07002267 return restrictions;
Fyodor Kupolov6f34d362015-04-02 12:42:13 -07002268 }
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002269
2270 FileInputStream fis = null;
2271 try {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002272 fis = restrictionsFile.openRead();
2273 XmlPullParser parser = Xml.newPullParser();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002274 parser.setInput(fis, StandardCharsets.UTF_8.name());
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002275 XmlUtils.nextElement(parser);
2276 if (parser.getEventType() != XmlPullParser.START_TAG) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002277 Slog.e(LOG_TAG, "Unable to read restrictions file "
2278 + restrictionsFile.getBaseFile());
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002279 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002280 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002281 while (parser.next() != XmlPullParser.END_DOCUMENT) {
2282 readEntry(restrictions, values, parser);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002283 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002284 } catch (IOException|XmlPullParserException e) {
2285 Log.w(LOG_TAG, "Error parsing " + restrictionsFile.getBaseFile(), e);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002286 } finally {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002287 IoUtils.closeQuietly(fis);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002288 }
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002289 return restrictions;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002290 }
2291
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002292 private static void readEntry(Bundle restrictions, ArrayList<String> values,
2293 XmlPullParser parser) throws XmlPullParserException, IOException {
2294 int type = parser.getEventType();
2295 if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_ENTRY)) {
2296 String key = parser.getAttributeValue(null, ATTR_KEY);
2297 String valType = parser.getAttributeValue(null, ATTR_VALUE_TYPE);
2298 String multiple = parser.getAttributeValue(null, ATTR_MULTIPLE);
2299 if (multiple != null) {
2300 values.clear();
2301 int count = Integer.parseInt(multiple);
2302 while (count > 0 && (type = parser.next()) != XmlPullParser.END_DOCUMENT) {
2303 if (type == XmlPullParser.START_TAG
2304 && parser.getName().equals(TAG_VALUE)) {
2305 values.add(parser.nextText().trim());
2306 count--;
2307 }
2308 }
2309 String [] valueStrings = new String[values.size()];
2310 values.toArray(valueStrings);
2311 restrictions.putStringArray(key, valueStrings);
2312 } else if (ATTR_TYPE_BUNDLE.equals(valType)) {
2313 restrictions.putBundle(key, readBundleEntry(parser, values));
2314 } else if (ATTR_TYPE_BUNDLE_ARRAY.equals(valType)) {
2315 final int outerDepth = parser.getDepth();
2316 ArrayList<Bundle> bundleList = new ArrayList<>();
2317 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
2318 Bundle childBundle = readBundleEntry(parser, values);
2319 bundleList.add(childBundle);
2320 }
2321 restrictions.putParcelableArray(key,
2322 bundleList.toArray(new Bundle[bundleList.size()]));
2323 } else {
2324 String value = parser.nextText().trim();
2325 if (ATTR_TYPE_BOOLEAN.equals(valType)) {
2326 restrictions.putBoolean(key, Boolean.parseBoolean(value));
2327 } else if (ATTR_TYPE_INTEGER.equals(valType)) {
2328 restrictions.putInt(key, Integer.parseInt(value));
2329 } else {
2330 restrictions.putString(key, value);
2331 }
2332 }
2333 }
2334 }
2335
2336 private static Bundle readBundleEntry(XmlPullParser parser, ArrayList<String> values)
2337 throws IOException, XmlPullParserException {
2338 Bundle childBundle = new Bundle();
2339 final int outerDepth = parser.getDepth();
2340 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
2341 readEntry(childBundle, values, parser);
2342 }
2343 return childBundle;
2344 }
2345
Fyodor Kupolov82402752015-10-28 14:54:51 -07002346 private void writeApplicationRestrictionsLP(String packageName,
Amith Yamasani7e99bc02013-04-16 18:24:51 -07002347 Bundle restrictions, int userId) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002348 AtomicFile restrictionsFile = new AtomicFile(
2349 new File(Environment.getUserSystemDirectory(userId),
Amith Yamasanifc95e702013-09-26 13:20:17 -07002350 packageToRestrictionsFileName(packageName)));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002351 writeApplicationRestrictionsLP(restrictions, restrictionsFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002352 }
2353
2354 @VisibleForTesting
Fyodor Kupolov82402752015-10-28 14:54:51 -07002355 static void writeApplicationRestrictionsLP(Bundle restrictions, AtomicFile restrictionsFile) {
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002356 FileOutputStream fos = null;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002357 try {
2358 fos = restrictionsFile.startWrite();
2359 final BufferedOutputStream bos = new BufferedOutputStream(fos);
2360
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002361 final XmlSerializer serializer = new FastXmlSerializer();
Wojciech Staszkiewicz9e9e2e72015-05-08 14:58:46 +01002362 serializer.setOutput(bos, StandardCharsets.UTF_8.name());
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002363 serializer.startDocument(null, true);
2364 serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
2365
2366 serializer.startTag(null, TAG_RESTRICTIONS);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002367 writeBundle(restrictions, serializer);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002368 serializer.endTag(null, TAG_RESTRICTIONS);
2369
2370 serializer.endDocument();
2371 restrictionsFile.finishWrite(fos);
2372 } catch (Exception e) {
2373 restrictionsFile.failWrite(fos);
Fyodor Kupolov262f9952015-03-23 18:55:11 -07002374 Slog.e(LOG_TAG, "Error writing application restrictions list", e);
2375 }
2376 }
2377
2378 private static void writeBundle(Bundle restrictions, XmlSerializer serializer)
2379 throws IOException {
2380 for (String key : restrictions.keySet()) {
2381 Object value = restrictions.get(key);
2382 serializer.startTag(null, TAG_ENTRY);
2383 serializer.attribute(null, ATTR_KEY, key);
2384
2385 if (value instanceof Boolean) {
2386 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BOOLEAN);
2387 serializer.text(value.toString());
2388 } else if (value instanceof Integer) {
2389 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_INTEGER);
2390 serializer.text(value.toString());
2391 } else if (value == null || value instanceof String) {
2392 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING);
2393 serializer.text(value != null ? (String) value : "");
2394 } else if (value instanceof Bundle) {
2395 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
2396 writeBundle((Bundle) value, serializer);
2397 } else if (value instanceof Parcelable[]) {
2398 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE_ARRAY);
2399 Parcelable[] array = (Parcelable[]) value;
2400 for (Parcelable parcelable : array) {
2401 if (!(parcelable instanceof Bundle)) {
2402 throw new IllegalArgumentException("bundle-array can only hold Bundles");
2403 }
2404 serializer.startTag(null, TAG_ENTRY);
2405 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_BUNDLE);
2406 writeBundle((Bundle) parcelable, serializer);
2407 serializer.endTag(null, TAG_ENTRY);
2408 }
2409 } else {
2410 serializer.attribute(null, ATTR_VALUE_TYPE, ATTR_TYPE_STRING_ARRAY);
2411 String[] values = (String[]) value;
2412 serializer.attribute(null, ATTR_MULTIPLE, Integer.toString(values.length));
2413 for (String choice : values) {
2414 serializer.startTag(null, TAG_VALUE);
2415 serializer.text(choice != null ? choice : "");
2416 serializer.endTag(null, TAG_VALUE);
2417 }
2418 }
2419 serializer.endTag(null, TAG_ENTRY);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08002420 }
2421 }
2422
2423 @Override
Amith Yamasani2a003292012-08-14 18:25:45 -07002424 public int getUserSerialNumber(int userHandle) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002425 synchronized (mUsersLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002426 if (!exists(userHandle)) return -1;
Fyodor Kupolov82402752015-10-28 14:54:51 -07002427 return getUserInfoLU(userHandle).serialNumber;
Amith Yamasani2a003292012-08-14 18:25:45 -07002428 }
2429 }
2430
2431 @Override
2432 public int getUserHandle(int userSerialNumber) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002433 synchronized (mUsersLock) {
Amith Yamasani2a003292012-08-14 18:25:45 -07002434 for (int userId : mUserIds) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002435 UserInfo info = getUserInfoLU(userId);
Kenny Guy945f8832015-02-10 15:17:26 +00002436 if (info != null && info.serialNumber == userSerialNumber) return userId;
Amith Yamasani2a003292012-08-14 18:25:45 -07002437 }
2438 // Not found
2439 return -1;
Amith Yamasani13593602012-03-22 16:16:17 -07002440 }
2441 }
2442
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002443 @Override
2444 public long getUserCreationTime(int userHandle) {
2445 int callingUserId = UserHandle.getCallingUserId();
2446 UserInfo userInfo = null;
Fyodor Kupolov82402752015-10-28 14:54:51 -07002447 synchronized (mUsersLock) {
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002448 if (callingUserId == userHandle) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002449 userInfo = getUserInfoLU(userHandle);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002450 } else {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002451 UserInfo parent = getProfileParentLU(userHandle);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002452 if (parent != null && parent.id == callingUserId) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002453 userInfo = getUserInfoLU(userHandle);
Fyodor Kupolovff7233e2015-04-08 11:28:52 -07002454 }
2455 }
2456 }
2457 if (userInfo == null) {
2458 throw new SecurityException("userHandle can only be the calling user or a managed "
2459 + "profile associated with this user");
2460 }
2461 return userInfo.creationTime;
2462 }
2463
Amith Yamasani0b285492011-04-14 17:35:23 -07002464 /**
2465 * Caches the list of user ids in an array, adjusting the array size when necessary.
2466 */
Fyodor Kupolov82402752015-10-28 14:54:51 -07002467 private void updateUserIds() {
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002468 int num = 0;
Fyodor Kupolov82402752015-10-28 14:54:51 -07002469 synchronized (mUsersLock) {
2470 final int userSize = mUsers.size();
2471 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08002472 if (!mUsers.valueAt(i).info.partial) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002473 num++;
2474 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002475 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07002476 final int[] newUsers = new int[num];
2477 int n = 0;
2478 for (int i = 0; i < userSize; i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08002479 if (!mUsers.valueAt(i).info.partial) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002480 newUsers[n++] = mUsers.keyAt(i);
2481 }
Dianne Hackbornd4ac8d72012-09-27 23:20:10 -07002482 }
Fyodor Kupolov82402752015-10-28 14:54:51 -07002483 mUserIds = newUsers;
Amith Yamasani0b285492011-04-14 17:35:23 -07002484 }
2485 }
2486
2487 /**
Jeff Sharkey47f71082016-02-01 17:03:54 -07002488 * Prepare storage areas for given user on all mounted devices.
2489 */
2490 private void prepareUserStorage(int userId, int userSerial, int flags) {
2491 final StorageManager storage = mContext.getSystemService(StorageManager.class);
2492 for (VolumeInfo vol : storage.getWritablePrivateVolumes()) {
2493 final String volumeUuid = vol.getFsUuid();
2494 storage.prepareUserStorage(volumeUuid, userId, userSerial, flags);
2495 }
2496 }
2497
2498 /**
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07002499 * Called right before a user is started. This gives us a chance to prepare
2500 * app storage and apply any user restrictions.
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002501 */
2502 public void onBeforeStartUser(int userId) {
Jeff Sharkey47f71082016-02-01 17:03:54 -07002503 final int userSerial = getUserSerialNumber(userId);
2504 prepareUserStorage(userId, userSerial, StorageManager.FLAG_STORAGE_DE);
2505 mPm.reconcileAppsData(userId, StorageManager.FLAG_STORAGE_DE);
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07002506
2507 if (userId != UserHandle.USER_SYSTEM) {
2508 synchronized (mRestrictionsLock) {
2509 applyUserRestrictionsLR(userId);
2510 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002511 }
2512 }
2513
2514 /**
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07002515 * Called right before a user is unlocked. This gives us a chance to prepare
2516 * app storage.
2517 */
2518 public void onBeforeUnlockUser(int userId) {
Jeff Sharkey47f71082016-02-01 17:03:54 -07002519 final int userSerial = getUserSerialNumber(userId);
2520 prepareUserStorage(userId, userSerial, StorageManager.FLAG_STORAGE_CE);
2521 mPm.reconcileAppsData(userId, StorageManager.FLAG_STORAGE_CE);
Jeff Sharkey0e62384c2016-01-13 18:52:55 -07002522 }
2523
2524 /**
Amith Yamasani1a7472e2013-07-02 11:17:30 -07002525 * Make a note of the last started time of a user and do some cleanup.
Amith Yamasani920ace02012-09-20 22:15:37 -07002526 * @param userId the user that was just foregrounded
2527 */
Amith Yamasani06bf8242015-05-08 16:36:21 -07002528 public void onUserForeground(int userId) {
Amith Yamasani12747872015-12-07 14:19:49 -08002529 UserData userData = getUserDataNoChecks(userId);
2530 if (userData == null || userData.info.partial) {
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002531 Slog.w(LOG_TAG, "userForeground: unknown user #" + userId);
2532 return;
2533 }
2534 long now = System.currentTimeMillis();
2535 if (now > EPOCH_PLUS_30_YEARS) {
Amith Yamasani12747872015-12-07 14:19:49 -08002536 userData.info.lastLoggedInTime = now;
2537 scheduleWriteUser(userData);
Amith Yamasani920ace02012-09-20 22:15:37 -07002538 }
2539 }
2540
2541 /**
Amith Yamasani0b285492011-04-14 17:35:23 -07002542 * Returns the next available user id, filling in any holes in the ids.
Amith Yamasani742a6712011-05-04 14:49:28 -07002543 * TODO: May not be a good idea to recycle ids, in case it results in confusion
2544 * for data and battery stats collection, or unexpected cross-talk.
Amith Yamasani0b285492011-04-14 17:35:23 -07002545 */
Fyodor Kupolov82402752015-10-28 14:54:51 -07002546 private int getNextAvailableId() {
2547 synchronized (mUsersLock) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08002548 int i = MIN_USER_ID;
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07002549 while (i < MAX_USER_ID) {
Jeff Sharkeyffe0cb42012-11-05 17:24:43 -08002550 if (mUsers.indexOfKey(i) < 0 && !mRemovingUserIds.get(i)) {
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07002551 return i;
Amith Yamasani195263742012-08-21 15:40:12 -07002552 }
2553 i++;
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002554 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002555 }
Xiaohui Chen621b3fc2015-10-02 14:41:42 -07002556 throw new IllegalStateException("No user id available!");
Amith Yamasani4b2e9342011-03-31 12:38:53 -07002557 }
Amith Yamasani920ace02012-09-20 22:15:37 -07002558
Amith Yamasanifc95e702013-09-26 13:20:17 -07002559 private String packageToRestrictionsFileName(String packageName) {
2560 return RESTRICTIONS_FILE_PREFIX + packageName + XML_SUFFIX;
2561 }
2562
Jeff Sharkey6dce4962015-07-03 18:08:41 -07002563 /**
Jeff Sharkey6dce4962015-07-03 18:08:41 -07002564 * Enforce that serial number stored in user directory inode matches the
2565 * given expected value. Gracefully sets the serial number if currently
2566 * undefined.
2567 *
2568 * @throws IOException when problem extracting serial number, or serial
2569 * number is mismatched.
2570 */
2571 public static void enforceSerialNumber(File file, int serialNumber) throws IOException {
2572 final int foundSerial = getSerialNumber(file);
2573 Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial);
2574
2575 if (foundSerial == -1) {
2576 Slog.d(LOG_TAG, "Serial number missing on " + file + "; assuming current is valid");
2577 try {
2578 setSerialNumber(file, serialNumber);
2579 } catch (IOException e) {
2580 Slog.w(LOG_TAG, "Failed to set serial number on " + file, e);
2581 }
2582
2583 } else if (foundSerial != serialNumber) {
2584 throw new IOException("Found serial number " + foundSerial
2585 + " doesn't match expected " + serialNumber);
2586 }
2587 }
2588
2589 /**
2590 * Set serial number stored in user directory inode.
2591 *
2592 * @throws IOException if serial number was already set
2593 */
2594 private static void setSerialNumber(File file, int serialNumber)
2595 throws IOException {
2596 try {
2597 final byte[] buf = Integer.toString(serialNumber).getBytes(StandardCharsets.UTF_8);
2598 Os.setxattr(file.getAbsolutePath(), XATTR_SERIAL, buf, OsConstants.XATTR_CREATE);
2599 } catch (ErrnoException e) {
2600 throw e.rethrowAsIOException();
2601 }
2602 }
2603
2604 /**
2605 * Return serial number stored in user directory inode.
2606 *
2607 * @return parsed serial number, or -1 if not set
2608 */
2609 private static int getSerialNumber(File file) throws IOException {
2610 try {
2611 final byte[] buf = new byte[256];
2612 final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf);
2613 final String serial = new String(buf, 0, len);
2614 try {
2615 return Integer.parseInt(serial);
2616 } catch (NumberFormatException e) {
2617 throw new IOException("Bad serial number: " + serial);
2618 }
2619 } catch (ErrnoException e) {
2620 if (e.errno == OsConstants.ENODATA) {
2621 return -1;
2622 } else {
2623 throw e.rethrowAsIOException();
2624 }
2625 }
2626 }
2627
Amith Yamasani920ace02012-09-20 22:15:37 -07002628 @Override
Amith Yamasani12747872015-12-07 14:19:49 -08002629 public void setSeedAccountData(int userId, String accountName, String accountType,
2630 PersistableBundle accountOptions, boolean persist) {
2631 checkManageUsersPermission("Require MANAGE_USERS permission to set user seed data");
2632 synchronized (mPackagesLock) {
2633 final UserData userData;
2634 synchronized (mUsersLock) {
2635 userData = getUserDataLU(userId);
2636 if (userData == null) {
2637 Slog.e(LOG_TAG, "No such user for settings seed data u=" + userId);
2638 return;
2639 }
2640 userData.seedAccountName = accountName;
2641 userData.seedAccountType = accountType;
2642 userData.seedAccountOptions = accountOptions;
2643 userData.persistSeedData = persist;
2644 }
2645 if (persist) {
2646 writeUserLP(userData);
2647 }
2648 }
2649 }
2650
2651 @Override
2652 public String getSeedAccountName() throws RemoteException {
2653 checkManageUsersPermission("Cannot get seed account information");
2654 synchronized (mUsersLock) {
2655 UserData userData = getUserDataLU(UserHandle.getCallingUserId());
2656 return userData.seedAccountName;
2657 }
2658 }
2659
2660 @Override
2661 public String getSeedAccountType() throws RemoteException {
2662 checkManageUsersPermission("Cannot get seed account information");
2663 synchronized (mUsersLock) {
2664 UserData userData = getUserDataLU(UserHandle.getCallingUserId());
2665 return userData.seedAccountType;
2666 }
2667 }
2668
2669 @Override
2670 public PersistableBundle getSeedAccountOptions() throws RemoteException {
2671 checkManageUsersPermission("Cannot get seed account information");
2672 synchronized (mUsersLock) {
2673 UserData userData = getUserDataLU(UserHandle.getCallingUserId());
2674 return userData.seedAccountOptions;
2675 }
2676 }
2677
2678 @Override
2679 public void clearSeedAccountData() throws RemoteException {
2680 checkManageUsersPermission("Cannot clear seed account information");
2681 synchronized (mPackagesLock) {
2682 UserData userData;
2683 synchronized (mUsersLock) {
2684 userData = getUserDataLU(UserHandle.getCallingUserId());
2685 if (userData == null) return;
2686 userData.clearSeedAccountData();
2687 }
2688 writeUserLP(userData);
2689 }
2690 }
2691
2692 @Override
2693 public boolean someUserHasSeedAccount(String accountName, String accountType)
2694 throws RemoteException {
2695 checkManageUsersPermission("Cannot check seed account information");
2696 synchronized (mUsersLock) {
2697 final int userSize = mUsers.size();
2698 for (int i = 0; i < userSize; i++) {
2699 final UserData data = mUsers.valueAt(i);
2700 if (data.info.isInitialized()) continue;
2701 if (data.seedAccountName == null || !data.seedAccountName.equals(accountName)) {
2702 continue;
2703 }
2704 if (data.seedAccountType == null || !data.seedAccountType.equals(accountType)) {
2705 continue;
2706 }
2707 return true;
2708 }
2709 }
2710 return false;
2711 }
2712
2713 @Override
Todd Kennedy60459ab2015-10-30 11:32:16 -07002714 public void onShellCommand(FileDescriptor in, FileDescriptor out,
2715 FileDescriptor err, String[] args, ResultReceiver resultReceiver) {
2716 (new Shell()).exec(this, in, out, err, args, resultReceiver);
2717 }
2718
2719 int onShellCommand(Shell shell, String cmd) {
2720 if (cmd == null) {
2721 return shell.handleDefaultCommands(cmd);
2722 }
2723
2724 final PrintWriter pw = shell.getOutPrintWriter();
2725 try {
2726 switch(cmd) {
2727 case "list":
2728 return runList(pw);
2729 }
2730 } catch (RemoteException e) {
2731 pw.println("Remote exception: " + e);
2732 }
2733 return -1;
2734 }
2735
2736 private int runList(PrintWriter pw) throws RemoteException {
2737 final IActivityManager am = ActivityManagerNative.getDefault();
2738 final List<UserInfo> users = getUsers(false);
2739 if (users == null) {
2740 pw.println("Error: couldn't get users");
2741 return 1;
2742 } else {
2743 pw.println("Users:");
2744 for (int i = 0; i < users.size(); i++) {
Jeff Sharkeye17ac152015-11-06 22:40:29 -08002745 String running = am.isUserRunning(users.get(i).id, 0) ? " running" : "";
Todd Kennedy60459ab2015-10-30 11:32:16 -07002746 pw.println("\t" + users.get(i).toString() + running);
2747 }
2748 return 0;
2749 }
2750 }
2751
2752 @Override
Amith Yamasani920ace02012-09-20 22:15:37 -07002753 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2754 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2755 != PackageManager.PERMISSION_GRANTED) {
2756 pw.println("Permission Denial: can't dump UserManager from from pid="
2757 + Binder.getCallingPid()
2758 + ", uid=" + Binder.getCallingUid()
2759 + " without permission "
2760 + android.Manifest.permission.DUMP);
2761 return;
2762 }
2763
2764 long now = System.currentTimeMillis();
2765 StringBuilder sb = new StringBuilder();
2766 synchronized (mPackagesLock) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002767 synchronized (mUsersLock) {
2768 pw.println("Users:");
2769 for (int i = 0; i < mUsers.size(); i++) {
Amith Yamasani12747872015-12-07 14:19:49 -08002770 UserData userData = mUsers.valueAt(i);
2771 if (userData == null) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002772 continue;
2773 }
Amith Yamasani12747872015-12-07 14:19:49 -08002774 UserInfo userInfo = userData.info;
2775 final int userId = userInfo.id;
2776 pw.print(" "); pw.print(userInfo);
2777 pw.print(" serialNo="); pw.print(userInfo.serialNumber);
Makoto Onukie7927da2015-11-25 10:05:17 -08002778 if (mRemovingUserIds.get(userId)) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002779 pw.print(" <removing> ");
2780 }
Amith Yamasani12747872015-12-07 14:19:49 -08002781 if (userInfo.partial) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002782 pw.print(" <partial>");
2783 }
2784 pw.println();
2785 pw.print(" Created: ");
Amith Yamasani12747872015-12-07 14:19:49 -08002786 if (userInfo.creationTime == 0) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002787 pw.println("<unknown>");
2788 } else {
2789 sb.setLength(0);
Amith Yamasani12747872015-12-07 14:19:49 -08002790 TimeUtils.formatDuration(now - userInfo.creationTime, sb);
Fyodor Kupolov82402752015-10-28 14:54:51 -07002791 sb.append(" ago");
2792 pw.println(sb);
2793 }
2794 pw.print(" Last logged in: ");
Amith Yamasani12747872015-12-07 14:19:49 -08002795 if (userInfo.lastLoggedInTime == 0) {
Fyodor Kupolov82402752015-10-28 14:54:51 -07002796 pw.println("<unknown>");
2797 } else {
2798 sb.setLength(0);
Amith Yamasani12747872015-12-07 14:19:49 -08002799 TimeUtils.formatDuration(now - userInfo.lastLoggedInTime, sb);
Fyodor Kupolov82402752015-10-28 14:54:51 -07002800 sb.append(" ago");
2801 pw.println(sb);
2802 }
Makoto Onukie7927da2015-11-25 10:05:17 -08002803 pw.print(" Has profile owner: ");
2804 pw.println(mIsUserManaged.get(userId));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002805 pw.println(" Restrictions:");
2806 synchronized (mRestrictionsLock) {
2807 UserRestrictionsUtils.dumpRestrictions(
Amith Yamasani12747872015-12-07 14:19:49 -08002808 pw, " ", mBaseUserRestrictions.get(userInfo.id));
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002809 pw.println(" Device policy local restrictions:");
2810 UserRestrictionsUtils.dumpRestrictions(
Amith Yamasani12747872015-12-07 14:19:49 -08002811 pw, " ", mDevicePolicyLocalUserRestrictions.get(userInfo.id));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002812 pw.println(" Effective restrictions:");
2813 UserRestrictionsUtils.dumpRestrictions(
Amith Yamasani12747872015-12-07 14:19:49 -08002814 pw, " ", mCachedEffectiveUserRestrictions.get(userInfo.id));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002815 }
Amith Yamasani12747872015-12-07 14:19:49 -08002816
2817 if (userData.account != null) {
2818 pw.print(" Account name: " + userData.account);
Xiaohui Chenb3b92582015-12-07 11:22:13 -08002819 pw.println();
2820 }
Amith Yamasani12747872015-12-07 14:19:49 -08002821
2822 if (userData.seedAccountName != null) {
2823 pw.print(" Seed account name: " + userData.seedAccountName);
2824 pw.println();
2825 if (userData.seedAccountType != null) {
2826 pw.print(" account type: " + userData.seedAccountType);
2827 pw.println();
2828 }
2829 if (userData.seedAccountOptions != null) {
2830 pw.print(" account options exist");
2831 pw.println();
2832 }
2833 }
Amith Yamasani920ace02012-09-20 22:15:37 -07002834 }
Amith Yamasani920ace02012-09-20 22:15:37 -07002835 }
Amith Yamasani12747872015-12-07 14:19:49 -08002836 pw.println();
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002837 pw.println(" Device policy global restrictions:");
2838 synchronized (mRestrictionsLock) {
2839 UserRestrictionsUtils
2840 .dumpRestrictions(pw, " ", mDevicePolicyGlobalUserRestrictions);
2841 }
Makoto Onukia4f11972015-10-01 13:19:58 -07002842 pw.println();
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002843 pw.println(" Guest restrictions:");
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002844 synchronized (mGuestRestrictions) {
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002845 UserRestrictionsUtils.dumpRestrictions(pw, " ", mGuestRestrictions);
Fyodor Kupolove80085d2015-11-06 18:21:39 -08002846 }
Makoto Onukie7927da2015-11-25 10:05:17 -08002847 synchronized (mUsersLock) {
2848 pw.println();
2849 pw.println(" Device managed: " + mIsDeviceManaged);
2850 }
Amith Yamasani12747872015-12-07 14:19:49 -08002851 // Dump some capabilities
2852 pw.println();
2853 pw.println(" Max users: " + UserManager.getMaxSupportedUsers());
2854 pw.println(" Supports switchable users: " + UserManager.supportsMultipleUsers());
Lenka Trochtova02fee152015-12-22 14:26:18 +01002855 pw.println(" All guests ephemeral: " + Resources.getSystem().getBoolean(
2856 com.android.internal.R.bool.config_guestUserEphemeral));
Amith Yamasani920ace02012-09-20 22:15:37 -07002857 }
2858 }
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08002859
2860 final class MainHandler extends Handler {
2861
2862 @Override
2863 public void handleMessage(Message msg) {
2864 switch (msg.what) {
2865 case WRITE_USER_MSG:
2866 removeMessages(WRITE_USER_MSG, msg.obj);
2867 synchronized (mPackagesLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08002868 int userId = ((UserData) msg.obj).info.id;
2869 UserData userData = getUserDataNoChecks(userId);
2870 if (userData != null) {
2871 writeUserLP(userData);
Fyodor Kupolov75d0ea82014-12-15 16:21:07 -08002872 }
2873 }
2874 }
2875 }
2876 }
Amith Yamasanibb054c92015-07-09 14:16:27 -07002877
2878 /**
2879 * @param userId
2880 * @return whether the user has been initialized yet
2881 */
2882 boolean isInitialized(int userId) {
2883 return (getUserInfo(userId).flags & UserInfo.FLAG_INITIALIZED) != 0;
2884 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07002885
2886 private class LocalService extends UserManagerInternal {
Makoto Onuki068c54a2015-10-13 14:34:03 -07002887 @Override
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002888 public void setDevicePolicyUserRestrictions(int userId, @NonNull Bundle localRestrictions,
2889 @Nullable Bundle globalRestrictions) {
Makoto Onuki2a3c3da2016-02-18 14:25:30 -08002890 UserManagerService.this.setDevicePolicyUserRestrictionsInner(userId, localRestrictions,
Makoto Onuki1a2cd742015-11-16 13:51:27 -08002891 globalRestrictions);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002892 }
2893
2894 @Override
2895 public Bundle getBaseUserRestrictions(int userId) {
2896 synchronized (mRestrictionsLock) {
2897 return mBaseUserRestrictions.get(userId);
2898 }
2899 }
2900
2901 @Override
2902 public void setBaseUserRestrictionsByDpmsForMigration(
2903 int userId, Bundle baseRestrictions) {
2904 synchronized (mRestrictionsLock) {
2905 mBaseUserRestrictions.put(userId, new Bundle(baseRestrictions));
Fyodor Kupolov82402752015-10-28 14:54:51 -07002906 invalidateEffectiveUserRestrictionsLR(userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002907 }
2908
Amith Yamasani12747872015-12-07 14:19:49 -08002909 final UserData userData = getUserDataNoChecks(userId);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002910 synchronized (mPackagesLock) {
Amith Yamasani12747872015-12-07 14:19:49 -08002911 if (userData != null) {
2912 writeUserLP(userData);
Makoto Onuki068c54a2015-10-13 14:34:03 -07002913 } else {
2914 Slog.w(LOG_TAG, "UserInfo not found for " + userId);
2915 }
2916 }
2917 }
Makoto Onukid45a4a22015-11-02 17:17:38 -08002918
2919 @Override
2920 public boolean getUserRestriction(int userId, String key) {
2921 return getUserRestrictions(userId).getBoolean(key);
2922 }
2923
2924 @Override
2925 public void addUserRestrictionsListener(UserRestrictionsListener listener) {
2926 synchronized (mUserRestrictionsListeners) {
2927 mUserRestrictionsListeners.add(listener);
2928 }
2929 }
2930
2931 @Override
2932 public void removeUserRestrictionsListener(UserRestrictionsListener listener) {
2933 synchronized (mUserRestrictionsListeners) {
2934 mUserRestrictionsListeners.remove(listener);
2935 }
2936 }
Makoto Onukie7927da2015-11-25 10:05:17 -08002937
2938 @Override
2939 public void setDeviceManaged(boolean isManaged) {
2940 synchronized (mUsersLock) {
2941 mIsDeviceManaged = isManaged;
2942 }
2943 }
2944
2945 @Override
2946 public void setUserManaged(int userId, boolean isManaged) {
2947 synchronized (mUsersLock) {
2948 mIsUserManaged.put(userId, isManaged);
2949 }
2950 }
Oleksandr Peletskyi7f1f1df2016-01-18 15:40:21 +01002951
2952 @Override
2953 public void setUserIcon(int userId, Bitmap bitmap) {
2954 long ident = Binder.clearCallingIdentity();
2955 try {
2956 synchronized (mPackagesLock) {
2957 UserData userData = getUserDataNoChecks(userId);
2958 if (userData == null || userData.info.partial) {
2959 Slog.w(LOG_TAG, "setUserIcon: unknown user #" + userId);
2960 return;
2961 }
2962 writeBitmapLP(userData.info, bitmap);
2963 writeUserLP(userData);
2964 }
2965 sendUserInfoChangedBroadcast(userId);
2966 } finally {
2967 Binder.restoreCallingIdentity(ident);
2968 }
2969 }
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +01002970
2971 @Override
2972 public void setForceEphemeralUsers(boolean forceEphemeralUsers) {
2973 synchronized (mUsersLock) {
2974 mForceEphemeralUsers = forceEphemeralUsers;
2975 }
2976 }
2977
2978 @Override
2979 public void removeAllUsers() {
2980 if (UserHandle.USER_SYSTEM == ActivityManager.getCurrentUser()) {
2981 // Remove the non-system users straight away.
2982 removeNonSystemUsers();
2983 } else {
2984 // Switch to the system user first and then remove the other users.
2985 BroadcastReceiver userSwitchedReceiver = new BroadcastReceiver() {
2986 @Override
2987 public void onReceive(Context context, Intent intent) {
2988 int userId =
2989 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
2990 if (userId != UserHandle.USER_SYSTEM) {
2991 return;
2992 }
2993 mContext.unregisterReceiver(this);
2994 removeNonSystemUsers();
2995 }
2996 };
2997 IntentFilter userSwitchedFilter = new IntentFilter();
2998 userSwitchedFilter.addAction(Intent.ACTION_USER_SWITCHED);
2999 mContext.registerReceiver(
3000 userSwitchedReceiver, userSwitchedFilter, null, mHandler);
3001
3002 // Switch to the system user.
3003 ActivityManager am =
3004 (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
3005 am.switchUser(UserHandle.USER_SYSTEM);
3006 }
3007 }
phweisse9c44062016-02-10 12:57:38 +01003008
3009 @Override
Lenka Trochtova1ddda472016-02-12 10:42:12 +01003010 public void onEphemeralUserStop(int userId) {
3011 synchronized (mUsersLock) {
3012 UserInfo userInfo = getUserInfoLU(userId);
3013 if (userInfo != null && userInfo.isEphemeral()) {
3014 // Do not allow switching back to the ephemeral user again as the user is going
3015 // to be deleted.
3016 userInfo.flags |= UserInfo.FLAG_DISABLED;
3017 if (userInfo.isGuest()) {
3018 // Indicate that the guest will be deleted after it stops.
3019 userInfo.guestToRemove = true;
3020 }
3021 }
3022 }
3023 }
3024
3025 @Override
phweisse9c44062016-02-10 12:57:38 +01003026 public UserInfo createUserEvenWhenDisallowed(String name, int flags) {
3027 UserInfo user = createUserInternalUnchecked(name, flags, UserHandle.USER_NULL);
3028 // Keep this in sync with UserManager.createUser
3029 if (user != null && !user.isAdmin()) {
3030 setUserRestriction(UserManager.DISALLOW_SMS, true, user.id);
3031 setUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, true, user.id);
3032 }
3033 return user;
3034 }
Lenka Trochtovaf348e8e2016-01-07 17:20:34 +01003035 }
3036
3037 /* Remove all the users except of the system one. */
3038 private void removeNonSystemUsers() {
3039 ArrayList<UserInfo> usersToRemove = new ArrayList<>();
3040 synchronized (mUsersLock) {
3041 final int userSize = mUsers.size();
3042 for (int i = 0; i < userSize; i++) {
3043 UserInfo ui = mUsers.valueAt(i).info;
3044 if (ui.id != UserHandle.USER_SYSTEM) {
3045 usersToRemove.add(ui);
3046 }
3047 }
3048 }
3049 for (UserInfo ui: usersToRemove) {
3050 removeUser(ui.id);
3051 }
Makoto Onuki068c54a2015-10-13 14:34:03 -07003052 }
Todd Kennedy60459ab2015-10-30 11:32:16 -07003053
3054 private class Shell extends ShellCommand {
3055 @Override
3056 public int onCommand(String cmd) {
3057 return onShellCommand(this, cmd);
3058 }
3059
3060 @Override
3061 public void onHelp() {
3062 final PrintWriter pw = getOutPrintWriter();
3063 pw.println("User manager (user) commands:");
3064 pw.println(" help");
3065 pw.println(" Print this help text.");
3066 pw.println("");
3067 pw.println(" list");
3068 pw.println(" Prints all users on the system.");
3069 }
3070 }
Makoto Onuki1a2cd742015-11-16 13:51:27 -08003071
3072 private static void debug(String message) {
3073 Log.d(LOG_TAG, message +
3074 (DBG_WITH_STACKTRACE ? " called at\n" + Debug.getCallers(10, " ") : ""));
3075 }
Amith Yamasani4b2e9342011-03-31 12:38:53 -07003076}