Disable "Learn more" button for unknown apps disabled by admin on secondary profiles.

Test: packages/apps/Settings/tests/robotests/src/com/android/settings/applications/appinfo/ExternalSourcesDetailsTest.java
Test: packages/apps/Settings/tests/robotests/src/com/android/settings/enterprise/ActionDisabledByAdminDialogHelperTest.java
Fixes:118881180
Change-Id: I8f6dbd9decba331fbe0a3495a2989f570f2afa67
This commit is contained in:
Irina Dumitrescu
2019-03-08 23:51:04 +00:00
parent 4524c700f3
commit e69f336850
3 changed files with 64 additions and 11 deletions

View File

@@ -55,7 +55,7 @@ import java.util.Objects;
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;
@@ -80,20 +80,28 @@ public class ActionDisabledByAdminDialogHelper {
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) {

View File

@@ -20,6 +20,11 @@ import static org.junit.Assert.assertEquals;
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;
@@ -33,6 +38,8 @@ import android.view.ViewGroup;
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;
@@ -179,5 +186,28 @@ public class ActionDisabledByAdminDialogHelperTest {
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());
}
}

View File

@@ -22,6 +22,8 @@ import android.os.UserHandle;
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;
@@ -48,6 +50,7 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
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);
@@ -138,6 +141,18 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
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;
}