Add restriction helper method for Cross profile calendar UI.

Bug: 117976974
Test: make ROBOTEST_FILTER=CrossProfileCalendarPreferenceControllerTest -j40 RunSettingsRoboTests
Change-Id: I0156ef91f806f055a5ac67e84817771c57ddd839
This commit is contained in:
yuemingw
2018-11-30 17:21:48 +00:00
committed by Yueming Wang
parent 0f856197b5
commit 1874c2592c

View File

@@ -38,6 +38,7 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.MenuItem;
import android.widget.TextView;
@@ -52,6 +53,9 @@ import java.util.List;
* support message dialog.
*/
public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
private static final String LOG_TAG = "RestrictedLockUtils";
/**
* @return drawables for displaying with settings that are locked by a device admin.
*/
@@ -305,6 +309,42 @@ public class RestrictedLockUtilsInternal extends RestrictedLockUtils {
return null;
}
/**
* @param userId user id of a managed profile.
* @return profile owner admin if cross profile calendar is disallowed.
*/
public static EnforcedAdmin getCrossProfileCalendarEnforcingAdmin(Context context, int userId) {
final Context managedProfileContext = createPackageContextAsUser(
context, userId);
final DevicePolicyManager dpm = managedProfileContext.getSystemService(
DevicePolicyManager.class);
if (dpm == null) {
return null;
}
final EnforcedAdmin admin = getProfileOwner(context, userId);
if (admin == null) {
return null;
}
if (dpm.getCrossProfileCalendarPackages().isEmpty()) {
return admin;
}
return null;
}
/**
* @param userId user id of a managed profile.
* @return a context created from the given context for the given user, or null if it fails.
*/
private static Context createPackageContextAsUser(Context context, int userId) {
try {
return context.createPackageContextAsUser(
context.getPackageName(), 0 /* flags */, UserHandle.of(userId));
} catch (PackageManager.NameNotFoundException e) {
Log.e(LOG_TAG, "Failed to create user context", e);
}
return null;
}
public static EnforcedAdmin checkIfAccessibilityServiceDisallowed(Context context,
String packageName, int userId) {
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(