public class ActionDisabledByAdminDialogHelper {
private static final String TAG = ActionDisabledByAdminDialogHelper.class.getName();
- private EnforcedAdmin mEnforcedAdmin;
+ @VisibleForTesting EnforcedAdmin mEnforcedAdmin;
private ViewGroup mDialogView;
private String mRestriction = null;
private Activity mActivity;
EnforcedAdmin enforcedAdmin) {
mEnforcedAdmin = enforcedAdmin;
mRestriction = restriction;
-
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, getEnforcementAdminUserId(),
mRestriction);
- return builder
- .setPositiveButton(R.string.okay, null)
- .setNeutralButton(R.string.learn_more,
- (dialog, which) -> {
- showAdminPolicies(mEnforcedAdmin, mActivity);
- mActivity.finish();
- })
- .setView(mDialogView);
+ builder.setPositiveButton(R.string.okay, null).setView(mDialogView);
+ maybeSetLearnMoreButton(builder);
+ return builder;
+ }
+
+ @VisibleForTesting
+ void maybeSetLearnMoreButton(AlertDialog.Builder builder) {
+ // The "Learn more" button appears only if the restriction is enforced by an admin in the
+ // same profile group. Otherwise the admin package and its policies are not accessible to
+ // the current user.
+ final UserManager um = UserManager.get(mActivity.getApplicationContext());
+ if (um.isSameProfileGroup(getEnforcementAdminUserId(mEnforcedAdmin), um.getUserHandle())) {
+ builder.setNeutralButton(R.string.learn_more, (dialog, which) -> {
+ showAdminPolicies(mEnforcedAdmin, mActivity);
+ mActivity.finish();
+ });
+ }
}
public void updateDialog(String restriction, EnforcedAdmin admin) {
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.widget.FrameLayout;
import android.widget.TextView;
+import androidx.appcompat.app.AlertDialog;
+
import com.android.settings.R;
import com.android.settings.Settings;
import com.android.settings.applications.specialaccess.deviceadmin.DeviceAdminAdd;
mHelper.setAdminSupportDetails(mActivity, null, admin);
assertNull(admin.component);
}
-}
+ @Test
+ public void testMaybeSetLearnMoreButton() {
+ final UserManager userManager = RuntimeEnvironment.application.getSystemService(
+ UserManager.class);
+ final ShadowUserManager userManagerShadow = Shadow.extract(userManager);
+ final ComponentName component = new ComponentName("some.package.name",
+ "some.package.name.SomeClass");
+ mHelper.mEnforcedAdmin = new EnforcedAdmin(component, UserHandle.of(123));
+
+ // Set up for shadow call.
+ userManagerShadow.getSameProfileGroupIds().put(123, 0);
+
+ // Test that the button is shown when user IDs are in the same profile group
+ AlertDialog.Builder builder = mock(AlertDialog.Builder.class);
+ mHelper.maybeSetLearnMoreButton(builder);
+ verify(builder).setNeutralButton(anyInt(), any());
+
+ // Test that the button is not shown when user IDs are not in the same profile group
+ userManagerShadow.getSameProfileGroupIds().clear();
+ builder = mock(AlertDialog.Builder.class);
+ mHelper.maybeSetLearnMoreButton(builder);
+ verify(builder, never()).setNeutralButton(anyInt(), any());
+ }
+}
import android.os.UserManager;
import android.os.UserManager.EnforcingUser;
+import com.google.android.collect.Maps;
+
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
private boolean mIsQuietModeEnabled = false;
private int[] profileIdsForUser = new int[0];
private boolean mUserSwitchEnabled;
+ private final Map<Integer, Integer> mSameProfileGroupIds = Maps.newHashMap();
public void addProfile(UserInfo userInfo) {
mUserProfileInfos.add(userInfo);
return sIsSupportsMultipleUsers;
}
+ @Implementation
+ protected boolean isSameProfileGroup(@UserIdInt int userId, int otherUserId) {
+ return mSameProfileGroupIds.containsKey(userId)
+ && mSameProfileGroupIds.get(userId) == otherUserId
+ || mSameProfileGroupIds.containsKey(otherUserId)
+ && mSameProfileGroupIds.get(otherUserId) == userId;
+ }
+
+ public Map<Integer, Integer> getSameProfileGroupIds() {
+ return mSameProfileGroupIds;
+ }
+
public void setSupportsMultipleUsers(boolean supports) {
sIsSupportsMultipleUsers = supports;
}