Use new RestrictedLockUtils API

This is made necessary by changes to the SystemAPI the lib relies on.

Test: RunSettingsRoboTests
Bug: 116798569
Change-Id: I2812ce9e58e3fb15a5579ddc10cd0edf33d0ed44
This commit is contained in:
Philip P. Moltmann
2018-10-08 10:46:04 -07:00
parent 1e80322df8
commit 035ea3b0d1
9 changed files with 64 additions and 25 deletions

View File

@@ -46,10 +46,17 @@ public class MonitoringCertInfoActivity extends Activity implements OnClickListe
mUserId = getIntent().getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
final UserHandle user;
if (mUserId == UserHandle.USER_NULL) {
user = null;
} else {
user = UserHandle.of(mUserId);
}
DevicePolicyManager dpm = getSystemService(DevicePolicyManager.class);
final int numberOfCertificates = getIntent().getIntExtra(
Settings.EXTRA_NUMBER_OF_CERTIFICATES, 1);
final int titleId = RestrictedLockUtils.getProfileOrDeviceOwner(this, mUserId) != null
final int titleId = RestrictedLockUtils.getProfileOrDeviceOwner(this, user) != null
? R.plurals.ssl_ca_cert_settings_button // Check certificate
: R.plurals.ssl_ca_cert_dialog_title; // Trust or remove certificate
final CharSequence title = getResources().getQuantityText(titleId, numberOfCertificates);

View File

@@ -222,8 +222,8 @@ public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragm
public EnforcedAdmin getRestrictionEnforcedAdmin() {
mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(),
mRestrictionKey, UserHandle.myUserId());
if (mEnforcedAdmin != null && mEnforcedAdmin.userId == UserHandle.USER_NULL) {
mEnforcedAdmin.userId = UserHandle.myUserId();
if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) {
mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
}
return mEnforcedAdmin;
}

View File

@@ -229,7 +229,7 @@ class TrustedCredentialsDialogBuilder extends AlertDialog.Builder {
&& !mDpm.isCaCertApproved(certHolder.getAlias(), certHolder.getUserId());
final boolean isProfileOrDeviceOwner = RestrictedLockUtils.getProfileOrDeviceOwner(
mActivity, certHolder.getUserId()) != null;
mActivity, UserHandle.of(certHolder.getUserId())) != null;
// Show trust button only when it requires consumer user (non-PO/DO) to approve
CharSequence displayText = mActivity.getText(!isProfileOrDeviceOwner && mNeedsApproval

View File

@@ -215,8 +215,8 @@ public abstract class RestrictedDashboardFragment extends DashboardFragment {
public EnforcedAdmin getRestrictionEnforcedAdmin() {
mEnforcedAdmin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getActivity(),
mRestrictionKey, UserHandle.myUserId());
if (mEnforcedAdmin != null && mEnforcedAdmin.userId == UserHandle.USER_NULL) {
mEnforcedAdmin.userId = UserHandle.myUserId();
if (mEnforcedAdmin != null && mEnforcedAdmin.user == null) {
mEnforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
}
return mEnforcedAdmin;
}

View File

@@ -53,12 +53,18 @@ public class ActionDisabledByAdminDialog extends Activity
@androidx.annotation.VisibleForTesting
EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.myUserId());
final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(UserHandle.myUserId()));
if (intent == null) {
return admin;
}
admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
admin.userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
int userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
if (userId == UserHandle.USER_NULL) {
admin.user = null;
} else {
admin.user = UserHandle.of(userId);
}
return admin;
}

View File

@@ -16,6 +16,7 @@
package com.android.settings.enterprise;
import android.annotation.UserIdInt;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
@@ -60,6 +61,14 @@ public class ActionDisabledByAdminDialogHelper {
mActivity = activity;
}
private @UserIdInt int getEnforcementAdminUserId() {
if (mEnforcedAdmin.user == null) {
return UserHandle.USER_NULL;
} else {
return mEnforcedAdmin.user.getIdentifier();
}
}
public AlertDialog.Builder prepareDialogBuilder(String restriction,
EnforcedAdmin enforcedAdmin) {
mEnforcedAdmin = enforcedAdmin;
@@ -68,7 +77,7 @@ public class ActionDisabledByAdminDialogHelper {
final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
mDialogView = (ViewGroup) LayoutInflater.from(builder.getContext()).inflate(
R.layout.admin_support_details_dialog, null);
initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
initializeDialogViews(mDialogView, mEnforcedAdmin.component, getEnforcementAdminUserId(),
mRestriction);
return builder
.setPositiveButton(R.string.okay, null)
@@ -86,7 +95,7 @@ public class ActionDisabledByAdminDialogHelper {
}
mEnforcedAdmin = admin;
mRestriction = restriction;
initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
initializeDialogViews(mDialogView, mEnforcedAdmin.component, getEnforcementAdminUserId(),
mRestriction);
}
@@ -108,7 +117,15 @@ public class ActionDisabledByAdminDialogHelper {
}
setAdminSupportTitle(root, restriction);
setAdminSupportDetails(mActivity, root, new EnforcedAdmin(admin, userId));
final UserHandle user;
if (userId == UserHandle.USER_NULL) {
user = null;
} else {
user = UserHandle.of(userId);
}
setAdminSupportDetails(mActivity, root, new EnforcedAdmin(admin, user));
}
@VisibleForTesting
@@ -152,20 +169,27 @@ public class ActionDisabledByAdminDialogHelper {
if (enforcedAdmin == null || enforcedAdmin.component == null) {
return;
}
final int userId;
if (enforcedAdmin.user == null) {
userId = UserHandle.USER_NULL;
} else {
userId = enforcedAdmin.user.getIdentifier();
}
final DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
Context.DEVICE_POLICY_SERVICE);
if (!RestrictedLockUtilsInternal.isAdminInCurrentUserOrProfile(activity,
enforcedAdmin.component) || !RestrictedLockUtils.isCurrentUserOrProfile(
activity, enforcedAdmin.userId)) {
activity, userId)) {
enforcedAdmin.component = null;
} else {
if (enforcedAdmin.userId == UserHandle.USER_NULL) {
enforcedAdmin.userId = UserHandle.myUserId();
if (enforcedAdmin.user == null) {
enforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
}
CharSequence supportMessage = null;
if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
supportMessage = dpm.getShortSupportMessageForUser(
enforcedAdmin.component, enforcedAdmin.userId);
supportMessage = dpm.getShortSupportMessageForUser(enforcedAdmin.component, userId);
}
if (supportMessage != null) {
final TextView textView = root.findViewById(R.id.admin_support_msg);
@@ -183,8 +207,7 @@ public class ActionDisabledByAdminDialogHelper {
enforcedAdmin.component);
intent.putExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, true);
// DeviceAdminAdd class may need to run as managed profile.
activity.startActivityAsUser(intent,
new UserHandle(enforcedAdmin.userId));
activity.startActivityAsUser(intent, enforcedAdmin.user);
} else {
intent.setClass(activity, Settings.DeviceAdminSettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

View File

@@ -31,6 +31,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.View;
@@ -67,7 +68,8 @@ public class BluetoothEnablerTest {
@BeforeClass
public static void beforeClass() {
sFakeEnforcedAdmin = new EnforcedAdmin(new ComponentName("test.package", "test.Class"), 10);
sFakeEnforcedAdmin = new EnforcedAdmin(new ComponentName("test.package", "test.Class"),
UserHandle.of(10));
}
@Mock

View File

@@ -27,6 +27,7 @@ import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.UserInfo;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.view.ViewGroup;
import android.widget.FrameLayout;
@@ -76,7 +77,7 @@ public class ActionDisabledByAdminDialogHelperTest {
final int userId = 123;
final ComponentName component = new ComponentName("some.package.name",
"some.package.name.SomeClass");
final EnforcedAdmin admin = new EnforcedAdmin(component, userId);
final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(userId));
mHelper.showAdminPolicies(admin, mActivity);
@@ -90,7 +91,7 @@ public class ActionDisabledByAdminDialogHelperTest {
@Test
public void testShowAdminPoliciesWithoutComponent() {
final int userId = 123;
final EnforcedAdmin admin = new EnforcedAdmin(null, userId);
final EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.of(userId));
mHelper.showAdminPolicies(admin, mActivity);
final Intent intent = mActivityShadow.getNextStartedActivity();
assertEquals(intent.getComponent(), new ComponentName(mActivity,
@@ -146,7 +147,7 @@ public class ActionDisabledByAdminDialogHelperTest {
final ViewGroup view = new FrameLayout(mActivity);
final ComponentName component = new ComponentName("some.package.name",
"some.package.name.SomeClass");
final EnforcedAdmin admin = new EnforcedAdmin(component, 123);
final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(123));
final TextView textView = new TextView(mActivity);
textView.setId(R.id.admin_support_msg);
@@ -169,7 +170,7 @@ public class ActionDisabledByAdminDialogHelperTest {
final ShadowUserManager userManagerShadow = Shadow.extract(userManager);
final ComponentName component = new ComponentName("some.package.name",
"some.package.name.SomeClass");
final EnforcedAdmin admin = new EnforcedAdmin(component, 123);
final EnforcedAdmin admin = new EnforcedAdmin(component, UserHandle.of(123));
dpmShadow.setShortSupportMessageForUser(component, 123, "some message");
dpmShadow.setIsAdminActiveAsUser(false);

View File

@@ -43,7 +43,7 @@ public class ActionDisabledByAdminDialogTest {
public void testGetAdminDetailsFromIntent() {
final int userId = 123;
final ComponentName component = new ComponentName("com.some.package", ".SomeClass");
final EnforcedAdmin expectedAdmin = new EnforcedAdmin(component, userId);
final EnforcedAdmin expectedAdmin = new EnforcedAdmin(component, UserHandle.of(userId));
final Intent intent = new Intent();
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, component);
@@ -54,7 +54,7 @@ public class ActionDisabledByAdminDialogTest {
@Test
public void testGetAdminDetailsFromNullIntent() {
final int userId = UserHandle.myUserId();
final EnforcedAdmin expectedAdmin = new EnforcedAdmin(null, userId);
final EnforcedAdmin expectedAdmin = new EnforcedAdmin(null, UserHandle.of(userId));
Assert.assertEquals(expectedAdmin, mDialog.getAdminDetailsFromIntent(null));
}