blob: 1f1a9b81ed489562db358257de06ae0ed157cbc7 [file] [log] [blame]
Sudheer Shanka7ff866d2016-01-12 17:22:06 +00001/*
2 * Copyright (C) 2016 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.settingslib;
18
Sudheer Shanka7664cba2016-01-15 11:39:04 +000019import android.app.AppGlobals;
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000020import android.app.admin.DevicePolicyManager;
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
Sudheer Shanka7664cba2016-01-15 11:39:04 +000024import android.content.pm.IPackageManager;
Sudheer Shanka45e298f2016-01-14 18:41:14 +000025import android.content.pm.UserInfo;
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000026import android.graphics.drawable.Drawable;
27import android.os.Bundle;
Sudheer Shanka7664cba2016-01-15 11:39:04 +000028import android.os.RemoteException;
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000029import android.os.UserHandle;
Sudheer Shanka7664cba2016-01-15 11:39:04 +000030import android.os.UserManager;
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000031import android.provider.Settings;
32import android.text.Spanned;
33import android.text.SpannableStringBuilder;
34import android.text.style.ForegroundColorSpan;
35import android.text.style.ImageSpan;
36import android.view.MenuItem;
37import android.widget.TextView;
38
Sudheer Shanka45e298f2016-01-14 18:41:14 +000039import com.android.internal.widget.LockPatternUtils;
40
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000041import java.util.List;
42
43/**
44 * Utility class to host methods usable in adding a restricted padlock icon and showing admin
45 * support message dialog.
46 */
47public class RestrictedLockUtils {
48 /**
49 * @return drawables for displaying with settings that are locked by a device admin.
50 */
51 public static Drawable getRestrictedPadlock(Context context) {
Sudheer Shanka40400a62016-02-23 11:42:11 +000052 Drawable restrictedPadlock = context.getDrawable(R.drawable.ic_info);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000053 final int iconSize = context.getResources().getDimensionPixelSize(
Sudheer Shanka40400a62016-02-23 11:42:11 +000054 R.dimen.restricted_icon_size);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000055 restrictedPadlock.setBounds(0, 0, iconSize, iconSize);
56 return restrictedPadlock;
57 }
58
59 /**
60 * Checks if a restriction is enforced on a user and returns the enforced admin and
61 * admin userId.
62 *
63 * @param userRestriction Restriction to check
64 * @param userId User which we need to check if restriction is enforced on.
Sudheer Shanka45e298f2016-01-14 18:41:14 +000065 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
66 * or {@code null} If the restriction is not set. If the restriction is set by both device owner
67 * and profile owner, then the admin component will be set to {@code null} and userId to
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000068 * {@link UserHandle#USER_NULL}.
69 */
70 public static EnforcedAdmin checkIfRestrictionEnforced(Context context,
71 String userRestriction, int userId) {
72 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
73 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +000074 if (dpm == null) {
75 return null;
76 }
Sudheer Shanka7ff866d2016-01-12 17:22:06 +000077 ComponentName deviceOwner = dpm.getDeviceOwnerComponentOnAnyUser();
78 int deviceOwnerUserId = dpm.getDeviceOwnerUserId();
79 boolean enforcedByDeviceOwner = false;
80 if (deviceOwner != null && deviceOwnerUserId != UserHandle.USER_NULL) {
81 Bundle enforcedRestrictions = dpm.getUserRestrictions(deviceOwner, deviceOwnerUserId);
82 if (enforcedRestrictions != null
83 && enforcedRestrictions.getBoolean(userRestriction, false)) {
84 enforcedByDeviceOwner = true;
85 }
86 }
87
88 ComponentName profileOwner = null;
89 boolean enforcedByProfileOwner = false;
90 if (userId != UserHandle.USER_NULL) {
91 profileOwner = dpm.getProfileOwnerAsUser(userId);
92 if (profileOwner != null) {
93 Bundle enforcedRestrictions = dpm.getUserRestrictions(profileOwner, userId);
94 if (enforcedRestrictions != null
95 && enforcedRestrictions.getBoolean(userRestriction, false)) {
96 enforcedByProfileOwner = true;
97 }
98 }
99 }
100
101 if (!enforcedByDeviceOwner && !enforcedByProfileOwner) {
102 return null;
103 }
104
105 EnforcedAdmin admin = null;
106 if (enforcedByDeviceOwner && enforcedByProfileOwner) {
107 admin = new EnforcedAdmin();
108 } else if (enforcedByDeviceOwner) {
109 admin = new EnforcedAdmin(deviceOwner, deviceOwnerUserId);
110 } else {
111 admin = new EnforcedAdmin(profileOwner, userId);
112 }
113 return admin;
114 }
115
Sudheer Shankaa8fbbb32016-02-11 17:17:57 +0000116 public static boolean hasBaseUserRestriction(Context context,
117 String userRestriction, int userId) {
118 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
119 return um.hasBaseUserRestriction(userRestriction, UserHandle.of(userId));
120 }
121
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000122 /**
Sudheer Shanka39cef942016-01-22 20:48:04 +0000123 * Checks if keyguard features are disabled by policy.
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000124 *
Sudheer Shanka39cef942016-01-22 20:48:04 +0000125 * @param keyguardFeatures Could be any of keyguard features that can be
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000126 * disabled by {@link android.app.admin.DevicePolicyManager#setKeyguardDisabledFeatures}.
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000127 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
128 * or {@code null} If the notification features are not disabled. If the restriction is set by
129 * multiple admins, then the admin component will be set to {@code null} and userId to
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000130 * {@link UserHandle#USER_NULL}.
131 */
Sudheer Shanka39cef942016-01-22 20:48:04 +0000132 public static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
Clara Bayarri6b6c5a22016-01-28 11:33:25 +0000133 int keyguardFeatures, int userId) {
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000134 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
135 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +0000136 if (dpm == null) {
137 return null;
138 }
Sudheer Shanka39cef942016-01-22 20:48:04 +0000139 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
140 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
141 EnforcedAdmin enforcedAdmin = null;
Sudheer Shanka39cef942016-01-22 20:48:04 +0000142 if (um.getUserInfo(userId).isManagedProfile()) {
143 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userId);
144 if (admins == null) {
145 return null;
146 }
Ruben Brunk74272132016-01-20 21:59:09 -0800147 for (ComponentName admin : admins) {
Sudheer Shanka39cef942016-01-22 20:48:04 +0000148 if ((dpm.getKeyguardDisabledFeatures(admin, userId) & keyguardFeatures) != 0) {
149 if (enforcedAdmin == null) {
150 enforcedAdmin = new EnforcedAdmin(admin, userId);
Ruben Brunk74272132016-01-20 21:59:09 -0800151 } else {
Sudheer Shanka39cef942016-01-22 20:48:04 +0000152 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
Ruben Brunk74272132016-01-20 21:59:09 -0800153 }
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000154 }
155 }
Sudheer Shanka39cef942016-01-22 20:48:04 +0000156 } else {
157 // Consider all admins for this user and the profiles that are visible from this
158 // user that do not use a separate work challenge.
159 for (UserInfo userInfo : um.getProfiles(userId)) {
160 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
161 if (admins == null) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000162 continue;
Sudheer Shanka39cef942016-01-22 20:48:04 +0000163 }
164 final boolean isSeparateProfileChallengeEnabled =
165 lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
166 for (ComponentName admin : admins) {
167 if (!isSeparateProfileChallengeEnabled) {
168 if ((dpm.getKeyguardDisabledFeatures(admin, userInfo.id)
169 & keyguardFeatures) != 0) {
170 if (enforcedAdmin == null) {
171 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
172 } else {
173 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
174 }
175 // This same admins could have set policies both on the managed profile
176 // and on the parent. So, if the admin has set the policy on the
177 // managed profile here, we don't need to further check if that admin
178 // has set policy on the parent admin.
179 continue;
180 }
181 }
182 if (userInfo.isManagedProfile()) {
183 // If userInfo.id is a managed profile, we also need to look at
184 // the policies set on the parent.
Sudheer Shanka978fc0d2016-01-28 13:51:10 +0000185 DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
Sudheer Shanka39cef942016-01-22 20:48:04 +0000186 if ((parentDpm.getKeyguardDisabledFeatures(admin, userInfo.id)
187 & keyguardFeatures) != 0) {
188 if (enforcedAdmin == null) {
189 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
190 } else {
191 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
192 }
193 }
194 }
195 }
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000196 }
197 }
198 return enforcedAdmin;
199 }
200
Sudheer Shanka7664cba2016-01-15 11:39:04 +0000201 public static EnforcedAdmin checkIfUninstallBlocked(Context context,
202 String packageName, int userId) {
203 EnforcedAdmin allAppsControlDisallowedAdmin = checkIfRestrictionEnforced(context,
204 UserManager.DISALLOW_APPS_CONTROL, userId);
205 if (allAppsControlDisallowedAdmin != null) {
206 return allAppsControlDisallowedAdmin;
207 }
208 EnforcedAdmin allAppsUninstallDisallowedAdmin = checkIfRestrictionEnforced(context,
209 UserManager.DISALLOW_UNINSTALL_APPS, userId);
210 if (allAppsUninstallDisallowedAdmin != null) {
211 return allAppsUninstallDisallowedAdmin;
212 }
213 IPackageManager ipm = AppGlobals.getPackageManager();
214 try {
215 if (ipm.getBlockUninstallForUser(packageName, userId)) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000216 return getProfileOrDeviceOwner(context, userId);
Sudheer Shanka7664cba2016-01-15 11:39:04 +0000217 }
218 } catch (RemoteException e) {
219 // Nothing to do
220 }
221 return null;
222 }
223
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000224 /**
Andrei Stingaceanu44af4822016-01-28 20:03:41 +0000225 * Check if an application is suspended.
226 *
227 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
228 * or {@code null} if the application is not suspended.
229 */
230 public static EnforcedAdmin checkIfApplicationIsSuspended(Context context, String packageName,
231 int userId) {
232 IPackageManager ipm = AppGlobals.getPackageManager();
233 try {
Andrei Stingaceanu355b2322016-02-12 16:43:51 +0000234 if (ipm.isPackageSuspendedForUser(packageName, userId)) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000235 return getProfileOrDeviceOwner(context, userId);
Andrei Stingaceanu44af4822016-01-28 20:03:41 +0000236 }
237 } catch (RemoteException e) {
238 // Nothing to do
239 }
240 return null;
241 }
242
Sudheer Shanka56925862016-01-28 19:43:59 +0000243 public static EnforcedAdmin checkIfInputMethodDisallowed(Context context,
244 String packageName, int userId) {
245 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
246 Context.DEVICE_POLICY_SERVICE);
247 if (dpm == null) {
248 return null;
249 }
250 EnforcedAdmin admin = getProfileOrDeviceOwner(context, userId);
251 boolean permitted = true;
252 if (admin != null) {
253 permitted = dpm.isInputMethodPermittedByAdmin(admin.component,
254 packageName, userId);
255 }
256 int managedProfileId = getManagedProfileId(context, userId);
257 EnforcedAdmin profileAdmin = getProfileOrDeviceOwner(context, managedProfileId);
258 boolean permittedByProfileAdmin = true;
259 if (profileAdmin != null) {
260 permittedByProfileAdmin = dpm.isInputMethodPermittedByAdmin(profileAdmin.component,
261 packageName, managedProfileId);
262 }
263 if (!permitted && !permittedByProfileAdmin) {
264 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
265 } else if (!permitted) {
266 return admin;
267 } else if (!permittedByProfileAdmin) {
268 return profileAdmin;
269 }
270 return null;
271 }
272
273 public static EnforcedAdmin checkIfAccessibilityServiceDisallowed(Context context,
274 String packageName, int userId) {
275 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
276 Context.DEVICE_POLICY_SERVICE);
277 if (dpm == null) {
278 return null;
279 }
280 EnforcedAdmin admin = getProfileOrDeviceOwner(context, userId);
281 boolean permitted = true;
282 if (admin != null) {
283 permitted = dpm.isAccessibilityServicePermittedByAdmin(admin.component,
284 packageName, userId);
285 }
286 int managedProfileId = getManagedProfileId(context, userId);
287 EnforcedAdmin profileAdmin = getProfileOrDeviceOwner(context, managedProfileId);
288 boolean permittedByProfileAdmin = true;
289 if (profileAdmin != null) {
290 permittedByProfileAdmin = dpm.isAccessibilityServicePermittedByAdmin(
291 profileAdmin.component, packageName, managedProfileId);
292 }
293 if (!permitted && !permittedByProfileAdmin) {
294 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
295 } else if (!permitted) {
296 return admin;
297 } else if (!permittedByProfileAdmin) {
298 return profileAdmin;
299 }
300 return null;
301 }
302
303 private static int getManagedProfileId(Context context, int userId) {
304 UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
305 List<UserInfo> userProfiles = um.getProfiles(userId);
306 for (UserInfo uInfo : userProfiles) {
307 if (uInfo.id == userId) {
308 continue;
309 }
310 if (uInfo.isManagedProfile()) {
311 return uInfo.id;
312 }
313 }
314 return UserHandle.USER_NULL;
315 }
316
Andrei Stingaceanu44af4822016-01-28 20:03:41 +0000317 /**
Sudheer Shanka20e22962016-01-18 11:22:45 +0000318 * Check if account management for a specific type of account is disabled by admin.
319 * Only a profile or device owner can disable account management. So, we check if account
320 * management is disabled and return profile or device owner on the calling user.
321 *
322 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
323 * or {@code null} if the account management is not disabled.
324 */
325 public static EnforcedAdmin checkIfAccountManagementDisabled(Context context,
Sudheer Shanka56925862016-01-28 19:43:59 +0000326 String accountType, int userId) {
Sudheer Shanka20e22962016-01-18 11:22:45 +0000327 if (accountType == null) {
328 return null;
329 }
330 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
331 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +0000332 if (dpm == null) {
333 return null;
334 }
Sudheer Shanka20e22962016-01-18 11:22:45 +0000335 boolean isAccountTypeDisabled = false;
Sudheer Shanka56925862016-01-28 19:43:59 +0000336 String[] disabledTypes = dpm.getAccountTypesWithManagementDisabledAsUser(userId);
Sudheer Shanka20e22962016-01-18 11:22:45 +0000337 for (String type : disabledTypes) {
338 if (accountType.equals(type)) {
339 isAccountTypeDisabled = true;
340 break;
341 }
342 }
343 if (!isAccountTypeDisabled) {
344 return null;
345 }
Sudheer Shanka56925862016-01-28 19:43:59 +0000346 return getProfileOrDeviceOwner(context, userId);
Sudheer Shanka20e22962016-01-18 11:22:45 +0000347 }
348
349 /**
350 * Checks if {@link android.app.admin.DevicePolicyManager#setAutoTimeRequired} is enforced
351 * on the device.
352 *
353 * @return EnforcedAdmin Object containing the device owner component and
354 * userId the device owner is running as, or {@code null} setAutoTimeRequired is not enforced.
355 */
356 public static EnforcedAdmin checkIfAutoTimeRequired(Context context) {
357 DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
358 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +0000359 if (dpm == null || !dpm.getAutoTimeRequired()) {
Sudheer Shanka20e22962016-01-18 11:22:45 +0000360 return null;
361 }
362 ComponentName adminComponent = dpm.getDeviceOwnerComponentOnCallingUser();
363 return new EnforcedAdmin(adminComponent, UserHandle.myUserId());
364 }
365
366 /**
Sudheer Shanka56925862016-01-28 19:43:59 +0000367 * Checks if an admin has enforced minimum password quality requirements on the given user.
Sudheer Shanka20e22962016-01-18 11:22:45 +0000368 *
369 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
370 * or {@code null} if no quality requirements are set. If the requirements are set by
371 * multiple device admins, then the admin component will be set to {@code null} and userId to
372 * {@link UserHandle#USER_NULL}.
373 *
374 */
Sudheer Shanka56925862016-01-28 19:43:59 +0000375 public static EnforcedAdmin checkIfPasswordQualityIsSet(Context context, int userId) {
Sudheer Shanka20e22962016-01-18 11:22:45 +0000376 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
377 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +0000378 if (dpm == null) {
379 return null;
380 }
Sudheer Shanka56925862016-01-28 19:43:59 +0000381
382 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
383 EnforcedAdmin enforcedAdmin = null;
384 if (lockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
385 // userId is managed profile and has a separate challenge, only consider
386 // the admins in that user.
387 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userId);
388 if (admins == null) {
389 return null;
390 }
Sudheer Shanka0fa74122016-01-21 13:47:51 +0000391 for (ComponentName admin : admins) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000392 if (dpm.getPasswordQuality(admin, userId)
393 > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
394 if (enforcedAdmin == null) {
395 enforcedAdmin = new EnforcedAdmin(admin, userId);
Sudheer Shanka0fa74122016-01-21 13:47:51 +0000396 } else {
Sudheer Shanka56925862016-01-28 19:43:59 +0000397 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
Sudheer Shanka0fa74122016-01-21 13:47:51 +0000398 }
Sudheer Shanka20e22962016-01-18 11:22:45 +0000399 }
400 }
Sudheer Shanka56925862016-01-28 19:43:59 +0000401 } else {
402 // Return all admins for this user and the profiles that are visible from this
403 // user that do not use a separate work challenge.
404 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
405 for (UserInfo userInfo : um.getProfiles(userId)) {
406 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
407 if (admins == null) {
408 continue;
409 }
410 final boolean isSeparateProfileChallengeEnabled =
411 lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
412 for (ComponentName admin : admins) {
413 if (!isSeparateProfileChallengeEnabled) {
414 if (dpm.getPasswordQuality(admin, userInfo.id)
415 > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
416 if (enforcedAdmin == null) {
417 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
418 } else {
419 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
420 }
421 // This same admins could have set policies both on the managed profile
422 // and on the parent. So, if the admin has set the policy on the
423 // managed profile here, we don't need to further check if that admin
424 // has set policy on the parent admin.
425 continue;
426 }
427 }
428 if (userInfo.isManagedProfile()) {
429 // If userInfo.id is a managed profile, we also need to look at
430 // the policies set on the parent.
431 DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
432 if (parentDpm.getPasswordQuality(admin, userInfo.id)
433 > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
434 if (enforcedAdmin == null) {
435 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
436 } else {
437 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
438 }
439 }
440 }
441 }
Sudheer Shanka20e22962016-01-18 11:22:45 +0000442 }
443 }
444 return enforcedAdmin;
445 }
446
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000447 /**
448 * Checks if any admin has set maximum time to lock.
449 *
450 * @return EnforcedAdmin Object containing the enforced admin component and admin user details,
451 * or {@code null} if no admin has set this restriction. If multiple admins has set this, then
452 * the admin component will be set to {@code null} and userId to {@link UserHandle#USER_NULL}
453 */
454 public static EnforcedAdmin checkIfMaximumTimeToLockIsSet(Context context) {
455 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
456 Context.DEVICE_POLICY_SERVICE);
457 LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
458 EnforcedAdmin enforcedAdmin = null;
459 final int userId = UserHandle.myUserId();
460 if (lockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000461 // userId is managed profile and has a separate challenge, only consider
462 // the admins in that user.
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000463 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userId);
464 if (admins == null) {
465 return null;
466 }
467 for (ComponentName admin : admins) {
468 if (dpm.getMaximumTimeToLock(admin, userId) > 0) {
469 if (enforcedAdmin == null) {
470 enforcedAdmin = new EnforcedAdmin(admin, userId);
471 } else {
472 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
473 }
474 }
475 }
476 } else {
477 // Return all admins for this user and the profiles that are visible from this
478 // user that do not use a separate work challenge.
479 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
480 for (UserInfo userInfo : um.getProfiles(userId)) {
481 final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
482 if (admins == null) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000483 continue;
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000484 }
Sudheer Shanka39cef942016-01-22 20:48:04 +0000485 final boolean isSeparateProfileChallengeEnabled =
486 lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000487 for (ComponentName admin : admins) {
Sudheer Shanka39cef942016-01-22 20:48:04 +0000488 if (!isSeparateProfileChallengeEnabled) {
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000489 if (dpm.getMaximumTimeToLock(admin, userInfo.id) > 0) {
490 if (enforcedAdmin == null) {
491 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
492 } else {
493 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
494 }
495 // This same admins could have set policies both on the managed profile
496 // and on the parent. So, if the admin has set the policy on the
497 // managed profile here, we don't need to further check if that admin
498 // has set policy on the parent admin.
499 continue;
500 }
501 }
502 if (userInfo.isManagedProfile()) {
503 // If userInfo.id is a managed profile, we also need to look at
504 // the policies set on the parent.
Sudheer Shanka978fc0d2016-01-28 13:51:10 +0000505 DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000506 if (parentDpm.getMaximumTimeToLock(admin, userInfo.id) > 0) {
507 if (enforcedAdmin == null) {
508 enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
509 } else {
510 return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
511 }
512 }
513 }
514 }
515 }
516 }
517 return enforcedAdmin;
518 }
519
Sudheer Shanka56925862016-01-28 19:43:59 +0000520 public static EnforcedAdmin getProfileOrDeviceOwner(Context context, int userId) {
521 if (userId == UserHandle.USER_NULL) {
522 return null;
523 }
Sudheer Shanka20e22962016-01-18 11:22:45 +0000524 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
525 Context.DEVICE_POLICY_SERVICE);
Sudheer Shankac61087102016-01-26 14:00:16 +0000526 if (dpm == null) {
527 return null;
528 }
Sudheer Shanka56925862016-01-28 19:43:59 +0000529 ComponentName adminComponent = dpm.getProfileOwnerAsUser(userId);
Sudheer Shanka20e22962016-01-18 11:22:45 +0000530 if (adminComponent != null) {
Sudheer Shanka56925862016-01-28 19:43:59 +0000531 return new EnforcedAdmin(adminComponent, userId);
Sudheer Shanka20e22962016-01-18 11:22:45 +0000532 }
Sudheer Shanka56925862016-01-28 19:43:59 +0000533 if (dpm.getDeviceOwnerUserId() == userId) {
534 adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
535 if (adminComponent != null) {
536 return new EnforcedAdmin(adminComponent, userId);
537 }
Sudheer Shanka20e22962016-01-18 11:22:45 +0000538 }
539 return null;
540 }
541
Sudheer Shanka1d0169b2016-02-03 00:25:50 +0000542 public static EnforcedAdmin getDeviceOwner(Context context) {
543 final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
544 Context.DEVICE_POLICY_SERVICE);
545 if (dpm == null) {
546 return null;
547 }
548 ComponentName adminComponent = dpm.getDeviceOwnerComponentOnAnyUser();
549 if (adminComponent != null) {
550 return new EnforcedAdmin(adminComponent, dpm.getDeviceOwnerUserId());
551 }
552 return null;
553 }
554
Sudheer Shanka20e22962016-01-18 11:22:45 +0000555 /**
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000556 * Set the menu item as disabled by admin by adding a restricted padlock at the end of the
557 * text and set the click listener which will send an intent to show the admin support details
Sudheer Shanka7664cba2016-01-15 11:39:04 +0000558 * dialog. If the admin is null, remove the padlock and disabled color span. When the admin is
559 * null, we also set the OnMenuItemClickListener to null, so if you want to set a custom
560 * OnMenuItemClickListener, set it after calling this method.
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000561 */
562 public static void setMenuItemAsDisabledByAdmin(final Context context,
563 final MenuItem item, final EnforcedAdmin admin) {
564 SpannableStringBuilder sb = new SpannableStringBuilder(item.getTitle());
565 removeExistingRestrictedSpans(sb);
566
Sudheer Shanka7664cba2016-01-15 11:39:04 +0000567 if (admin != null) {
568 final int disabledColor = context.getColor(R.color.disabled_text_color);
569 sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(),
570 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
571 ImageSpan image = new RestrictedLockImageSpan(context);
572 sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000573
Sudheer Shanka7664cba2016-01-15 11:39:04 +0000574 item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
575 @Override
576 public boolean onMenuItemClick(MenuItem item) {
577 sendShowAdminSupportDetailsIntent(context, admin);
578 return true;
579 }
580 });
581 } else {
582 item.setOnMenuItemClickListener(null);
583 }
584 item.setTitle(sb);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000585 }
586
587 private static void removeExistingRestrictedSpans(SpannableStringBuilder sb) {
588 final int length = sb.length();
589 RestrictedLockImageSpan[] imageSpans = sb.getSpans(length - 1, length,
590 RestrictedLockImageSpan.class);
591 for (ImageSpan span : imageSpans) {
Sudheer Shanka20e22962016-01-18 11:22:45 +0000592 final int start = sb.getSpanStart(span);
593 final int end = sb.getSpanEnd(span);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000594 sb.removeSpan(span);
Sudheer Shanka20e22962016-01-18 11:22:45 +0000595 sb.delete(start, end);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000596 }
597 ForegroundColorSpan[] colorSpans = sb.getSpans(0, length, ForegroundColorSpan.class);
598 for (ForegroundColorSpan span : colorSpans) {
599 sb.removeSpan(span);
600 }
601 }
602
603 /**
604 * Send the intent to trigger the {@link android.settings.ShowAdminSupportDetailsDialog}.
605 */
606 public static void sendShowAdminSupportDetailsIntent(Context context, EnforcedAdmin admin) {
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000607 final Intent intent = getShowAdminSupportDetailsIntent(context, admin);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000608 int adminUserId = UserHandle.myUserId();
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000609 if (admin.userId != UserHandle.USER_NULL) {
610 adminUserId = admin.userId;
611 }
612 context.startActivityAsUser(intent, new UserHandle(adminUserId));
613 }
614
615 public static Intent getShowAdminSupportDetailsIntent(Context context, EnforcedAdmin admin) {
616 final Intent intent = new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS);
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000617 if (admin != null) {
618 if (admin.component != null) {
619 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin.component);
620 }
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000621 int adminUserId = UserHandle.myUserId();
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000622 if (admin.userId != UserHandle.USER_NULL) {
623 adminUserId = admin.userId;
624 }
625 intent.putExtra(Intent.EXTRA_USER_ID, adminUserId);
626 }
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000627 return intent;
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000628 }
629
630 public static void setTextViewPadlock(Context context,
631 TextView textView, boolean showPadlock) {
632 final SpannableStringBuilder sb = new SpannableStringBuilder(textView.getText());
633 removeExistingRestrictedSpans(sb);
634 if (showPadlock) {
635 final ImageSpan image = new RestrictedLockImageSpan(context);
636 sb.append(" ", image, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
637 }
638 textView.setText(sb);
639 }
640
Sudheer Shanka9f77a712016-01-12 19:38:53 +0000641 /**
642 * Takes a {@link android.widget.TextView} and applies an alpha so that the text looks like
643 * disabled and appends a padlock to the text. This assumes that there are no
644 * ForegroundColorSpans and RestrictedLockImageSpans used on the TextView.
645 */
646 public static void setTextViewAsDisabledByAdmin(Context context,
647 TextView textView, boolean disabled) {
648 final SpannableStringBuilder sb = new SpannableStringBuilder(textView.getText());
649 removeExistingRestrictedSpans(sb);
650 if (disabled) {
651 final int disabledColor = context.getColor(R.color.disabled_text_color);
652 sb.setSpan(new ForegroundColorSpan(disabledColor), 0, sb.length(),
653 Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Sudheer Shankaf271fd52016-03-07 13:13:22 -0800654 textView.setCompoundDrawables(null, null, getRestrictedPadlock(context), null);
655 textView.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(
656 R.dimen.restricted_icon_padding));
657 } else {
658 textView.setCompoundDrawables(null, null, null, null);
Sudheer Shanka9f77a712016-01-12 19:38:53 +0000659 }
660 textView.setText(sb);
661 }
662
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000663 public static class EnforcedAdmin {
664 public ComponentName component = null;
665 public int userId = UserHandle.USER_NULL;
666
Sudheer Shanka45e298f2016-01-14 18:41:14 +0000667 // We use this to represent the case where a policy is enforced by multiple admins.
668 public final static EnforcedAdmin MULTIPLE_ENFORCED_ADMIN = new EnforcedAdmin();
669
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000670 public EnforcedAdmin(ComponentName component, int userId) {
671 this.component = component;
672 this.userId = userId;
673 }
674
Sudheer Shanka1c09db22016-01-18 11:18:35 +0000675 public EnforcedAdmin(EnforcedAdmin other) {
676 if (other == null) {
677 throw new IllegalArgumentException();
678 }
679 this.component = other.component;
680 this.userId = other.userId;
681 }
682
683 public EnforcedAdmin() {}
684
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000685 @Override
686 public boolean equals(Object object) {
687 if (object == this) return true;
688 if (!(object instanceof EnforcedAdmin)) return false;
689 EnforcedAdmin other = (EnforcedAdmin) object;
690 if (userId != other.userId) {
691 return false;
692 }
693 if ((component == null && other == null) ||
694 (component != null && component.equals(other))) {
695 return true;
696 }
697 return false;
698 }
699
700 @Override
701 public String toString() {
702 return "EnforcedAdmin{component=" + component + ",userId=" + userId + "}";
703 }
704
705 public void copyTo(EnforcedAdmin other) {
706 if (other == null) {
Sudheer Shanka1c09db22016-01-18 11:18:35 +0000707 throw new IllegalArgumentException();
Sudheer Shanka1c7cda82015-12-31 14:46:02 +0000708 }
709 other.component = component;
710 other.userId = userId;
711 }
Sudheer Shanka7ff866d2016-01-12 17:22:06 +0000712 }
Ruben Brunk74272132016-01-20 21:59:09 -0800713}