Merge "Fix Settings getProfileIdsWithDisabled usage"
This commit is contained in:
committed by
Android (Google) Code Review
commit
a5470e4c24
@@ -434,18 +434,20 @@ public final class Utils extends com.android.settingslib.Utils {
|
|||||||
* {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
|
* {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
|
||||||
*/
|
*/
|
||||||
public static UserHandle getManagedProfileWithDisabled(UserManager userManager) {
|
public static UserHandle getManagedProfileWithDisabled(UserManager userManager) {
|
||||||
// TODO: Call getManagedProfileId from here once Robolectric supports
|
return getManagedProfileWithDisabled(userManager, UserHandle.myUserId());
|
||||||
// API level 24 and UserManager.getProfileIdsWithDisabled can be Mocked (to avoid having
|
}
|
||||||
// yet another implementation that loops over user profiles in this method). In the meantime
|
|
||||||
// we need to use UserManager.getProfiles that is available on API 23 (the one currently
|
/**
|
||||||
// used for Settings Robolectric tests).
|
* Returns the managed profile of the given user or {@code null} if none is found. Unlike
|
||||||
final int myUserId = UserHandle.myUserId();
|
* {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
|
||||||
final List<UserInfo> profiles = userManager.getProfiles(myUserId);
|
*/
|
||||||
|
private static UserHandle getManagedProfileWithDisabled(UserManager um, int parentUserId) {
|
||||||
|
final List<UserInfo> profiles = um.getProfiles(parentUserId);
|
||||||
final int count = profiles.size();
|
final int count = profiles.size();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
final UserInfo profile = profiles.get(i);
|
final UserInfo profile = profiles.get(i);
|
||||||
if (profile.isManagedProfile()
|
if (profile.isManagedProfile()
|
||||||
&& profile.getUserHandle().getIdentifier() != myUserId) {
|
&& profile.getUserHandle().getIdentifier() != parentUserId) {
|
||||||
return profile.getUserHandle();
|
return profile.getUserHandle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -454,15 +456,14 @@ public final class Utils extends com.android.settingslib.Utils {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the id for the given user's managed profile.
|
* Retrieves the id for the given user's managed profile.
|
||||||
|
* Unlike {@link #getManagedProfile} this method returns enabled and disabled managed profiles.
|
||||||
*
|
*
|
||||||
* @return the managed profile id or UserHandle.USER_NULL if there is none.
|
* @return the managed profile id or UserHandle.USER_NULL if there is none.
|
||||||
*/
|
*/
|
||||||
public static int getManagedProfileId(UserManager um, int parentUserId) {
|
public static int getManagedProfileId(UserManager um, int parentUserId) {
|
||||||
final int[] profileIds = um.getProfileIdsWithDisabled(parentUserId);
|
final UserHandle profile = getManagedProfileWithDisabled(um, parentUserId);
|
||||||
for (int profileId : profileIds) {
|
if (profile != null) {
|
||||||
if (profileId != parentUserId) {
|
return profile.getIdentifier();
|
||||||
return profileId;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return UserHandle.USER_NULL;
|
return UserHandle.USER_NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFIC
|
|||||||
|
|
||||||
import android.app.KeyguardManager;
|
import android.app.KeyguardManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.database.ContentObserver;
|
import android.database.ContentObserver;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
@@ -42,6 +43,8 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
|||||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The controller of the sensitive notifications.
|
* The controller of the sensitive notifications.
|
||||||
*/
|
*/
|
||||||
@@ -74,11 +77,13 @@ public class RedactNotificationPreferenceController extends TogglePreferenceCont
|
|||||||
mKm = context.getSystemService(KeyguardManager.class);
|
mKm = context.getSystemService(KeyguardManager.class);
|
||||||
|
|
||||||
mProfileUserId = UserHandle.myUserId();
|
mProfileUserId = UserHandle.myUserId();
|
||||||
final int[] profileIds = mUm.getProfileIdsWithDisabled(UserHandle.myUserId());
|
final List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId());
|
||||||
|
final int count = profiles.size();
|
||||||
for (int profileId : profileIds) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (profileId != UserHandle.myUserId()) {
|
final UserInfo profile = profiles.get(i);
|
||||||
mProfileUserId = profileId;
|
if (profile.isManagedProfile()
|
||||||
|
&& profile.getUserHandle().getIdentifier() != UserHandle.myUserId()) {
|
||||||
|
mProfileUserId = profile.getUserHandle().getIdentifier();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,16 +18,11 @@ package com.android.settings.applications;
|
|||||||
|
|
||||||
import static android.text.format.DateUtils.DAY_IN_MILLIS;
|
import static android.text.format.DateUtils.DAY_IN_MILLIS;
|
||||||
|
|
||||||
import static com.android.settings.applications.AppStateNotificationBridge
|
import static com.android.settings.applications.AppStateNotificationBridge.FILTER_APP_NOTIFICATION_BLOCKED;
|
||||||
.FILTER_APP_NOTIFICATION_BLOCKED;
|
import static com.android.settings.applications.AppStateNotificationBridge.FILTER_APP_NOTIFICATION_FREQUENCY;
|
||||||
import static com.android.settings.applications.AppStateNotificationBridge
|
import static com.android.settings.applications.AppStateNotificationBridge.FILTER_APP_NOTIFICATION_RECENCY;
|
||||||
.FILTER_APP_NOTIFICATION_FREQUENCY;
|
import static com.android.settings.applications.AppStateNotificationBridge.FREQUENCY_NOTIFICATION_COMPARATOR;
|
||||||
import static com.android.settings.applications.AppStateNotificationBridge
|
import static com.android.settings.applications.AppStateNotificationBridge.RECENT_NOTIFICATION_COMPARATOR;
|
||||||
.FILTER_APP_NOTIFICATION_RECENCY;
|
|
||||||
import static com.android.settings.applications.AppStateNotificationBridge
|
|
||||||
.FREQUENCY_NOTIFICATION_COMPARATOR;
|
|
||||||
import static com.android.settings.applications.AppStateNotificationBridge
|
|
||||||
.RECENT_NOTIFICATION_COMPARATOR;
|
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
@@ -47,12 +42,12 @@ import android.app.usage.UsageEvents;
|
|||||||
import android.app.usage.UsageEvents.Event;
|
import android.app.usage.UsageEvents.Event;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
|
||||||
@@ -71,6 +66,7 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@@ -288,7 +284,8 @@ public class AppStateNotificationBridgeTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testLoadAllExtraInfo_multipleUsers() throws RemoteException {
|
public void testLoadAllExtraInfo_multipleUsers() throws RemoteException {
|
||||||
// has work profile
|
// has work profile
|
||||||
when(mUserManager.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[]{1});
|
when(mUserManager.getProfiles(anyInt())).thenReturn(Arrays.asList(
|
||||||
|
new UserInfo(1, "", UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE)));
|
||||||
mBridge = new AppStateNotificationBridge(mContext, mState,
|
mBridge = new AppStateNotificationBridge(mContext, mState,
|
||||||
mock(AppStateBaseBridge.Callback.class), mUsageStats, mUserManager, mBackend);
|
mock(AppStateBaseBridge.Callback.class), mUsageStats, mUserManager, mBackend);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.hardware.fingerprint.FingerprintManager;
|
import android.hardware.fingerprint.FingerprintManager;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
@@ -39,6 +40,8 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.shadows.ShadowApplication;
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class FingerprintProfileStatusPreferenceControllerTest {
|
public class FingerprintProfileStatusPreferenceControllerTest {
|
||||||
|
|
||||||
@@ -71,7 +74,8 @@ public class FingerprintProfileStatusPreferenceControllerTest {
|
|||||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
|
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
|
||||||
.thenReturn(mLockPatternUtils);
|
.thenReturn(mLockPatternUtils);
|
||||||
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {1234});
|
when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(
|
||||||
|
new UserInfo(1234, "", UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE)));
|
||||||
mController = new FingerprintProfileStatusPreferenceController(mContext, TEST_PREF_KEY);
|
mController = new FingerprintProfileStatusPreferenceController(mContext, TEST_PREF_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
|
|||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
@@ -118,6 +119,9 @@ public class LocationInjectedServicesPreferenceControllerTest {
|
|||||||
final int fakeWorkProfileId = 123;
|
final int fakeWorkProfileId = 123;
|
||||||
ShadowUserManager.getShadow().setProfileIdsWithDisabled(
|
ShadowUserManager.getShadow().setProfileIdsWithDisabled(
|
||||||
new int[]{UserHandle.myUserId(), fakeWorkProfileId});
|
new int[]{UserHandle.myUserId(), fakeWorkProfileId});
|
||||||
|
ShadowUserManager.getShadow().addProfile(new UserInfo(UserHandle.myUserId(), "", 0));
|
||||||
|
ShadowUserManager.getShadow().addProfile(new UserInfo(fakeWorkProfileId, "",
|
||||||
|
UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE));
|
||||||
|
|
||||||
// Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return non-null.
|
// Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return non-null.
|
||||||
final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
|
final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
|
||||||
|
|||||||
@@ -34,11 +34,11 @@ import static org.mockito.Mockito.when;
|
|||||||
import android.app.KeyguardManager;
|
import android.app.KeyguardManager;
|
||||||
import android.app.admin.DevicePolicyManager;
|
import android.app.admin.DevicePolicyManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.preference.Preference;
|
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.internal.widget.LockPatternUtils;
|
import com.android.internal.widget.LockPatternUtils;
|
||||||
@@ -57,6 +57,8 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(shadows = {
|
@Config(shadows = {
|
||||||
ShadowUtils.class,
|
ShadowUtils.class,
|
||||||
@@ -95,7 +97,7 @@ public class RedactNotificationPreferenceControllerTest {
|
|||||||
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mUm);
|
when(mMockContext.getSystemService(UserManager.class)).thenReturn(mUm);
|
||||||
when(mMockContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDpm);
|
when(mMockContext.getSystemService(DevicePolicyManager.class)).thenReturn(mDpm);
|
||||||
when(mMockContext.getSystemService(KeyguardManager.class)).thenReturn(mKm);
|
when(mMockContext.getSystemService(KeyguardManager.class)).thenReturn(mKm);
|
||||||
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {0});
|
when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(new UserInfo(0, "", 0)));
|
||||||
|
|
||||||
mController = new RedactNotificationPreferenceController(
|
mController = new RedactNotificationPreferenceController(
|
||||||
mMockContext, RedactNotificationPreferenceController.KEY_LOCKSCREEN_REDACT);
|
mMockContext, RedactNotificationPreferenceController.KEY_LOCKSCREEN_REDACT);
|
||||||
@@ -105,7 +107,9 @@ public class RedactNotificationPreferenceControllerTest {
|
|||||||
mController.getPreferenceKey())).thenReturn(mPreference);
|
mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
assertThat(mController.mProfileUserId).isEqualTo(0);
|
assertThat(mController.mProfileUserId).isEqualTo(0);
|
||||||
|
|
||||||
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {0, 10});
|
when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(
|
||||||
|
new UserInfo(5, "", 0),
|
||||||
|
new UserInfo(10, "", UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE)));
|
||||||
mWorkController = new RedactNotificationPreferenceController(mMockContext,
|
mWorkController = new RedactNotificationPreferenceController(mMockContext,
|
||||||
RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT);
|
RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT);
|
||||||
mWorkPreference = new RestrictedSwitchPreference(mContext);
|
mWorkPreference = new RestrictedSwitchPreference(mContext);
|
||||||
@@ -137,7 +141,8 @@ public class RedactNotificationPreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void getAvailabilityStatus_noWorkProfile() {
|
public void getAvailabilityStatus_noWorkProfile() {
|
||||||
// reset controllers with no work profile
|
// reset controllers with no work profile
|
||||||
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {UserHandle.myUserId()});
|
when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(
|
||||||
|
new UserInfo(UserHandle.myUserId(), "", 0)));
|
||||||
mWorkController = new RedactNotificationPreferenceController(mMockContext,
|
mWorkController = new RedactNotificationPreferenceController(mMockContext,
|
||||||
RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT);
|
RedactNotificationPreferenceController.KEY_LOCKSCREEN_WORK_PROFILE_REDACT);
|
||||||
mController = new RedactNotificationPreferenceController(mMockContext,
|
mController = new RedactNotificationPreferenceController(mMockContext,
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.UserInfo;
|
||||||
import android.hardware.fingerprint.FingerprintManager;
|
import android.hardware.fingerprint.FingerprintManager;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
|
|
||||||
@@ -49,6 +50,8 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.shadows.ShadowApplication;
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class VisiblePatternProfilePreferenceControllerTest {
|
public class VisiblePatternProfilePreferenceControllerTest {
|
||||||
|
|
||||||
@@ -82,7 +85,9 @@ public class VisiblePatternProfilePreferenceControllerTest {
|
|||||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
|
when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
|
||||||
.thenReturn(mLockPatternUtils);
|
.thenReturn(mLockPatternUtils);
|
||||||
when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {FAKE_PROFILE_USER_ID});
|
when(mUm.getProfiles(anyInt())).thenReturn(Arrays.asList(new UserInfo(
|
||||||
|
FAKE_PROFILE_USER_ID, "", UserInfo.FLAG_MANAGED_PROFILE | UserInfo.FLAG_PROFILE)));
|
||||||
|
|
||||||
|
|
||||||
mLifecycleOwner = () -> mLifecycle;
|
mLifecycleOwner = () -> mLifecycle;
|
||||||
mLifecycle = new Lifecycle(mLifecycleOwner);
|
mLifecycle = new Lifecycle(mLifecycleOwner);
|
||||||
|
|||||||
Reference in New Issue
Block a user