import com.android.settingslib.core.lifecycle.events.OnResume;
import java.util.List;
+import java.util.Map;
public class LocationServicePreferenceController extends LocationBasePreferenceController
implements LifecycleObserver, OnResume, OnPause {
private static final String TAG = "LocationServicePrefCtrl";
/** Key for preference category "Location services" */
private static final String KEY_LOCATION_SERVICES = "location_services";
+ /** Key for preference category "Location services for work" */
+ private static final String KEY_LOCATION_SERVICES_MANAGED = "location_services_managed_profile";
@VisibleForTesting
static final IntentFilter INTENT_FILTER_INJECTED_SETTING_CHANGED =
new IntentFilter(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED);
private PreferenceCategory mCategoryLocationServices;
+ private PreferenceCategory mCategoryLocationServicesManaged;
private final LocationSettings mFragment;
private final AppSettingsInjector mInjector;
- /** Receives UPDATE_INTENT */
+ /** Receives UPDATE_INTENT */
@VisibleForTesting
BroadcastReceiver mInjectedSettingsReceiver;
}
@Override
- public boolean isAvailable() {
- // If managed profile has lock-down on location access then its injected location services
- // must not be shown.
- return mInjector.hasInjectedSettings(mLocationEnabler.isManagedProfileRestrictedByBase()
- ? UserHandle.myUserId() : UserHandle.USER_CURRENT);
- }
-
- @Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mCategoryLocationServices = screen.findPreference(KEY_LOCATION_SERVICES);
+ mCategoryLocationServicesManaged = screen.findPreference(KEY_LOCATION_SERVICES_MANAGED);
}
@Override
public void updateState(Preference preference) {
mCategoryLocationServices.removeAll();
- final List<Preference> prefs = getLocationServices();
- for (Preference pref : prefs) {
- if (pref instanceof RestrictedAppPreference) {
- ((RestrictedAppPreference) pref).checkRestrictionAndSetDisabled();
+ mCategoryLocationServicesManaged.removeAll();
+ final Map<Integer, List<Preference>> prefs = getLocationServices();
+ boolean showPrimary = false;
+ boolean showManaged = false;
+ for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) {
+ for (Preference pref : entry.getValue()) {
+ if (pref instanceof RestrictedAppPreference) {
+ ((RestrictedAppPreference) pref).checkRestrictionAndSetDisabled();
+ }
+ }
+ if (entry.getKey() == UserHandle.myUserId()) {
+ LocationSettings.addPreferencesSorted(entry.getValue(), mCategoryLocationServices);
+ showPrimary = true;
+ } else {
+ LocationSettings.addPreferencesSorted(entry.getValue(),
+ mCategoryLocationServicesManaged);
+ showManaged = true;
}
}
- LocationSettings.addPreferencesSorted(prefs, mCategoryLocationServices);
+ mCategoryLocationServices.setVisible(showPrimary);
+ mCategoryLocationServicesManaged.setVisible(showManaged);
}
@Override
mContext.unregisterReceiver(mInjectedSettingsReceiver);
}
- private List<Preference> getLocationServices() {
+ private Map<Integer, 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());
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
+import android.util.ArrayMap;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUserManager.class)
public class LocationServicePreferenceControllerTest {
+ private static final String LOCATION_SERVICES_MANAGED_PROFILE_KEY =
+ "location_services_managed_profile";
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocationSettings mFragment;
@Mock
- private PreferenceCategory mCategory;
+ private PreferenceCategory mCategoryPrimary;
+ @Mock
+ private PreferenceCategory mCategoryManaged;
@Mock
private PreferenceScreen mScreen;
@Mock
mController = spy(new LocationServicePreferenceController(
mContext, mFragment, mLifecycle, mSettingsInjector));
final String key = mController.getPreferenceKey();
- when(mScreen.findPreference(key)).thenReturn(mCategory);
- when(mCategory.getKey()).thenReturn(key);
+ when(mScreen.findPreference(key)).thenReturn(mCategoryPrimary);
+ when(mScreen.findPreference(LOCATION_SERVICES_MANAGED_PROFILE_KEY)).thenReturn(
+ mCategoryManaged);
+ when(mCategoryPrimary.getKey()).thenReturn(key);
+ when(mCategoryManaged.getKey()).thenReturn(LOCATION_SERVICES_MANAGED_PROFILE_KEY);
when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
.thenReturn(mDevicePolicyManager);
}
@Test
- public void isAvailable_noInjectedSettings_shouldReturnFalse() {
- doReturn(false).when(mSettingsInjector).hasInjectedSettings(anyInt());
-
- assertThat(mController.isAvailable()).isFalse();
- }
-
- @Test
- public void isAvailable_hasInjectedSettings_shouldReturnFalse() {
- doReturn(true).when(mSettingsInjector).hasInjectedSettings(anyInt());
-
- assertThat(mController.isAvailable()).isTrue();
- }
-
- @Test
public void onResume_shouldRegisterListener() {
mController.onResume();
@Test
public void updateState_shouldRemoveAllAndAddInjectedSettings() {
final List<Preference> preferences = new ArrayList<>();
+ final Map<Integer, List<Preference>> map = new ArrayMap<>();
final Preference pref1 = new Preference(mContext);
pref1.setTitle("Title1");
final Preference pref2 = new Preference(mContext);
pref2.setTitle("Title2");
preferences.add(pref1);
preferences.add(pref2);
- doReturn(preferences)
- .when(mSettingsInjector).getInjectedSettings(any(Context.class), anyInt());
+ map.put(UserHandle.myUserId(), preferences);
+ doReturn(map)
+ .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);
+ mController.updateState(mCategoryPrimary);
- verify(mCategory).removeAll();
- verify(mCategory).addPreference(pref1);
- verify(mCategory).addPreference(pref2);
+ verify(mCategoryPrimary).removeAll();
+ verify(mCategoryPrimary).addPreference(pref1);
+ verify(mCategoryPrimary).addPreference(pref2);
}
@Test
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
- mController.updateState(mCategory);
+ mController.updateState(mCategoryPrimary);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.myUserId()));
}
enforcingUsers);
mController.displayPreference(mScreen);
- mController.updateState(mCategory);
+ mController.updateState(mCategoryPrimary);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.USER_CURRENT));
}
UserManager.DISALLOW_CONFIG_LOCATION);
pref.setTitle("Location Accuracy");
preferences.add(pref);
- doReturn(preferences).when(mSettingsInjector)
+ final Map<Integer, List<Preference>> map = new ArrayMap<>();
+ map.put(UserHandle.myUserId(), preferences);
+ doReturn(map).when(mSettingsInjector)
.getInjectedSettings(any(Context.class), anyInt());
ShadowUserManager.getShadow().setProfileIdsWithDisabled(new int[]{UserHandle.myUserId()});
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
- mController.updateState(mCategory);
+ mController.updateState(mCategoryPrimary);
assertThat(pref.isEnabled()).isFalse();
assertThat(pref.isDisabledByAdmin()).isTrue();