Merge "Check EnforcedAdmin to see if location is locked down by device policy."

This commit is contained in:
TreeHugger Robot
2018-06-09 01:23:26 +00:00
committed by Android (Google) Code Review
3 changed files with 78 additions and 10 deletions

View File

@@ -25,6 +25,7 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import android.util.Log;
import com.android.settings.Utils;
import com.android.settings.widget.RestrictedAppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -130,8 +131,11 @@ public class LocationServicePreferenceController extends LocationBasePreferenceC
private List<Preference> getLocationServices() {
// If location access is locked down by device policy then we only show injected settings
// for the primary profile.
final int profileUserId = Utils.getManagedProfileId(mUserManager, UserHandle.myUserId());
return mInjector.getInjectedSettings(mFragment.getPreferenceManager().getContext(),
mLocationEnabler.isManagedProfileRestrictedByBase()
(profileUserId != UserHandle.USER_NULL
&& mLocationEnabler.getShareLocationEnforcedAdmin(profileUserId) != null)
? UserHandle.myUserId() : UserHandle.USER_CURRENT);
}
}

View File

@@ -16,6 +16,7 @@
package com.android.settings.location;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -25,24 +26,17 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.admin.DevicePolicyManager;
import androidx.lifecycle.LifecycleOwner;
import android.content.ComponentName;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settings.widget.RestrictedAppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -52,6 +46,14 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(
shadows = {
@@ -133,6 +135,8 @@ public class LocationServicePreferenceControllerTest {
doReturn(preferences)
.when(mSettingsInjector).getInjectedSettings(any(Context.class), anyInt());
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
ShadowUserManager.getShadow().setProfileIdsWithDisabled(new int[]{UserHandle.myUserId()});
mController.displayPreference(mScreen);
mController.updateState(mCategory);
@@ -142,6 +146,50 @@ public class LocationServicePreferenceControllerTest {
verify(mCategory).addPreference(pref2);
}
@Test
public void workProfileDisallowShareLocationOn_getParentUserLocationServicesOnly() {
final int fakeWorkProfileId = 123;
ShadowUserManager.getShadow().setProfileIdsWithDisabled(
new int[]{UserHandle.myUserId(), fakeWorkProfileId});
// Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return non-null.
final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
enforcingUsers.add(new UserManager.EnforcingUser(fakeWorkProfileId,
UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));
final ComponentName componentName = new ComponentName("test", "test");
// Ensure that RestrictedLockUtils.checkIfRestrictionEnforced doesn't return null.
ShadowUserManager.getShadow().setUserRestrictionSources(
UserManager.DISALLOW_SHARE_LOCATION,
UserHandle.of(fakeWorkProfileId),
enforcingUsers);
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.myUserId()));
}
@Test
public void workProfileDisallowShareLocationOff_getAllUserLocationServices() {
final int fakeWorkProfileId = 123;
ShadowUserManager.getShadow().setProfileIdsWithDisabled(
new int[]{UserHandle.myUserId(), fakeWorkProfileId});
// Mock RestrictedLockUtils.checkIfRestrictionEnforced and let it return null.
// Empty enforcing users.
final List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
ShadowUserManager.getShadow().setUserRestrictionSources(
UserManager.DISALLOW_SHARE_LOCATION,
UserHandle.of(fakeWorkProfileId),
enforcingUsers);
mController.displayPreference(mScreen);
mController.updateState(mCategory);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.USER_CURRENT));
}
@Test
public void onLocationModeChanged_shouldRequestReloadInjectedSettigns() {
mController.onLocationModeChanged(Settings.Secure.LOCATION_MODE_BATTERY_SAVING, false);
@@ -158,8 +206,9 @@ public class LocationServicePreferenceControllerTest {
preferences.add(pref);
doReturn(preferences).when(mSettingsInjector)
.getInjectedSettings(any(Context.class), anyInt());
ShadowUserManager.getShadow().setProfileIdsWithDisabled(new int[]{UserHandle.myUserId()});
int userId = UserHandle.myUserId();
final int userId = UserHandle.myUserId();
List<UserManager.EnforcingUser> enforcingUsers = new ArrayList<>();
enforcingUsers.add(new UserManager.EnforcingUser(userId,
UserManager.RESTRICTION_SOURCE_DEVICE_OWNER));

View File

@@ -30,6 +30,7 @@ import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -45,6 +46,7 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
private final List<UserInfo> mUserProfileInfos = new ArrayList<>();
private final Set<Integer> mManagedProfiles = new HashSet<>();
private boolean mIsQuietModeEnabled = false;
private int[] profileIdsForUser;
@Resetter
public void reset() {
@@ -96,7 +98,11 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
@Implementation
public List<EnforcingUser> getUserRestrictionSources(
String restrictionKey, UserHandle userHandle) {
return mRestrictionSources.get(restrictionKey + userHandle.getIdentifier());
// Return empty list when there is no enforcing user, otherwise might trigger
// NullPointer Exception in RestrictedLockUtils.checkIfRestrictionEnforced.
List<EnforcingUser> enforcingUsers =
mRestrictionSources.get(restrictionKey + userHandle.getIdentifier());
return enforcingUsers == null ? Collections.emptyList() : enforcingUsers;
}
public void setUserRestrictionSources(
@@ -121,4 +127,13 @@ public class ShadowUserManager extends org.robolectric.shadows.ShadowUserManager
public void setQuietModeEnabled(boolean enabled) {
mIsQuietModeEnabled = enabled;
}
@Implementation
public int[] getProfileIdsWithDisabled(@UserIdInt int userId) {
return profileIdsForUser;
}
public void setProfileIdsWithDisabled(int[] profileIds) {
profileIdsForUser = profileIds;
}
}