Fix shifting problem in location services page

Controller generates the injection location settings
in updateState() which happens in onResume.
That's the primary reason why we observed the shifting issue.

In a good practice, we generate preference in displayPreference(),
and then update the setting state in updateState().

In this cl, we create a base controller class to encapsulate
most implementation, and developer need to inject location
services in child class.

Test: Add work profile, and see correct services list.
Bug: 183169265
Change-Id: I5735ba974da87ad83b56791abd8a8637c2317571
This commit is contained in:
Tsung-Mao Fang
2021-06-09 16:09:00 +08:00
parent 603cd6c44c
commit cbf5ccab0e
4 changed files with 135 additions and 120 deletions

View File

@@ -67,8 +67,6 @@ public class LocationInjectedServicesPreferenceControllerTest {
@Mock
private PreferenceCategory mCategoryPrimary;
@Mock
private PreferenceCategory mCategoryManaged;
@Mock
private PreferenceScreen mScreen;
@Mock
private AppSettingsInjector mSettingsInjector;
@@ -114,31 +112,6 @@ public class LocationInjectedServicesPreferenceControllerTest {
verify(mContext).unregisterReceiver(mController.mInjectedSettingsReceiver);
}
@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);
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(mCategoryPrimary);
verify(mCategoryPrimary).removeAll();
verify(mCategoryPrimary).addPreference(pref1);
verify(mCategoryPrimary).addPreference(pref2);
}
@Test
public void workProfileDisallowShareLocationOn_getParentUserLocationServicesOnly() {
final int fakeWorkProfileId = 123;
@@ -158,7 +131,6 @@ public class LocationInjectedServicesPreferenceControllerTest {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
mController.updateState(mCategoryPrimary);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.myUserId()));
}
@@ -178,7 +150,6 @@ public class LocationInjectedServicesPreferenceControllerTest {
enforcingUsers);
mController.displayPreference(mScreen);
mController.updateState(mCategoryPrimary);
verify(mSettingsInjector).getInjectedSettings(
any(Context.class), eq(UserHandle.USER_CURRENT));
}
@@ -216,7 +187,6 @@ public class LocationInjectedServicesPreferenceControllerTest {
when(mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser()).thenReturn(componentName);
mController.displayPreference(mScreen);
mController.updateState(mCategoryPrimary);
assertThat(pref.isEnabled()).isFalse();
assertThat(pref.isDisabledByAdmin()).isTrue();