Switch auto time setting to use APIs

Switch auto time setting to use TimeManager APIs instead of low-level
direct settings access.

This makes the AutoTimePreferenceController approach the same as
AutoTimeZonePreferenceController. Most of the logic about what to
display and whether toggles are enabled is moved to the service.

This change has the side effect of adding support in SettingsUI for
devices with no auto time detection mechanism configured at all, i.e.
the auto time toggle will stop being shown in SettingsUI. There are no
known devices that require this currently, but it is a theoretical
possibility if config.xml is configured in particular ways.

Bug: 172891783
Test: Manual testing with different settings for "time origin
priorities" (i.e. empty, non-empty)
Test: m ROBOTEST_FILTER=com.android.settings.datetime RunSettingsRoboTests -j40

Change-Id: I4112fb51adb0d06a1dbc39a44ea109d6df899153
This commit is contained in:
Neil Fuller
2022-05-30 15:43:17 +01:00
parent 25b832f061
commit 579a19a793
8 changed files with 244 additions and 95 deletions

View File

@@ -18,17 +18,25 @@ package com.android.settings.datetime;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.time.Capabilities;
import android.app.time.TimeCapabilities;
import android.app.time.TimeCapabilitiesAndConfig;
import android.app.time.TimeConfiguration;
import android.app.time.TimeManager;
import android.content.Context;
import android.provider.Settings;
import android.os.UserHandle;
import com.android.settingslib.RestrictedSwitchPreference;
import androidx.preference.Preference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@@ -38,43 +46,123 @@ public class AutoTimePreferenceControllerTest {
@Mock
private UpdateTimeAndDateCallback mCallback;
private Context mContext;
private RestrictedSwitchPreference mPreference;
private AutoTimePreferenceController mController;
private Preference mPreference;
@Mock
private TimeManager mTimeManager;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new RestrictedSwitchPreference(mContext);
mContext = spy(RuntimeEnvironment.application);
mPreference = new Preference(mContext);
when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
mController = new AutoTimePreferenceController(mContext, mCallback);
}
@Test
public void testIsEnabled_shouldReadFromSettingsProvider() {
// Disabled
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME, 0);
assertThat(mController.isEnabled()).isFalse();
public void autoTimeNotSupported_notAvailable() {
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */false, /* autoEnabled= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
// Enabled
Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME, 1);
assertThat(mController.isEnabled()).isTrue();
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void autoTimeNotSupported_notEnable() {
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */false, /* autoEnabled= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isEnabled()).isFalse();
}
@Test
public void testIsEnabled_shouldReadFromTimeManagerConfig() {
{
// Disabled
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isEnabled()).isFalse();
}
{
// Enabled
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */true, /* autoEnabled= */true);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isEnabled()).isTrue();
}
}
@Test
public void updatePreferenceChange_prefIsChecked_shouldUpdatePreferenceAndNotifyCallback() {
mController.onPreferenceChange(mPreference, true);
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
when(mTimeManager.updateTimeConfiguration(Mockito.any())).thenReturn(true);
assertThat(mController.onPreferenceChange(mPreference, true)).isTrue();
verify(mCallback).updateTimeAndDateDisplay(mContext);
// Check the service was asked to change the configuration correctly.
TimeConfiguration timeConfiguration = new TimeConfiguration.Builder()
.setAutoDetectionEnabled(true)
.build();
verify(mTimeManager).updateTimeConfiguration(timeConfiguration);
// Update the mTimeManager mock so that it now returns the expected updated config.
TimeCapabilitiesAndConfig capabilitiesAndConfigAfterUpdate =
createCapabilitiesAndConfig(/* autoSupported= */true, /* autoEnabled= */true);
when(mTimeManager.getTimeCapabilitiesAndConfig())
.thenReturn(capabilitiesAndConfigAfterUpdate);
assertThat(mController.isEnabled()).isTrue();
verify(mCallback).updateTimeAndDateDisplay(mContext);
}
@Test
public void updatePreferenceChange_prefIsUnchecked_shouldUpdatePreferenceAndNotifyCallback() {
mController.onPreferenceChange(mPreference, false);
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* autoSupported= */true, /* autoEnabled= */true);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
when(mTimeManager.updateTimeConfiguration(Mockito.any())).thenReturn(true);
assertThat(mController.onPreferenceChange(mPreference, false)).isTrue();
verify(mCallback).updateTimeAndDateDisplay(mContext);
// Check the service was asked to change the configuration correctly.
TimeConfiguration timeConfiguration = new TimeConfiguration.Builder()
.setAutoDetectionEnabled(false)
.build();
verify(mTimeManager).updateTimeConfiguration(timeConfiguration);
// Update the mTimeManager mock so that it now returns the expected updated config.
TimeCapabilitiesAndConfig capabilitiesAndConfigAfterUpdate =
createCapabilitiesAndConfig(/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig())
.thenReturn(capabilitiesAndConfigAfterUpdate);
assertThat(mController.isEnabled()).isFalse();
verify(mCallback).updateTimeAndDateDisplay(mContext);
}
private static TimeCapabilitiesAndConfig createCapabilitiesAndConfig(
boolean autoSupported, boolean autoEnabled) {
int configureAutoDetectionEnabledCapability =
autoSupported ? Capabilities.CAPABILITY_POSSESSED
: Capabilities.CAPABILITY_NOT_SUPPORTED;
TimeCapabilities capabilities = new TimeCapabilities.Builder(UserHandle.SYSTEM)
.setConfigureAutoDetectionEnabledCapability(configureAutoDetectionEnabledCapability)
.setSuggestManualTimeCapability(Capabilities.CAPABILITY_POSSESSED)
.build();
TimeConfiguration config = new TimeConfiguration.Builder()
.setAutoDetectionEnabled(autoEnabled)
.build();
return new TimeCapabilitiesAndConfig(capabilities, config);
}
}

View File

@@ -46,9 +46,7 @@ public class AutoTimeZonePreferenceControllerTest {
@Mock
private UpdateTimeAndDateCallback mCallback;
@Mock
private Context mContext;
private AutoTimeZonePreferenceController mController;
private Preference mPreference;
@Mock
private TimeManager mTimeManager;
@@ -69,10 +67,10 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, true /* isFromSUW */);
assertThat(mController.isAvailable()).isFalse();
assertThat(controller.isAvailable()).isFalse();
}
@Test
@@ -81,10 +79,10 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* isFromSUW */);
assertThat(mController.isAvailable()).isTrue();
assertThat(controller.isAvailable()).isTrue();
}
@Test
@@ -93,10 +91,10 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */false, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* fromSUW */);
assertThat(mController.isAvailable()).isFalse();
assertThat(controller.isAvailable()).isFalse();
}
@Test
@@ -105,10 +103,10 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */false, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, true /* fromSUW */);
assertThat(mController.isEnabled()).isFalse();
assertThat(controller.isEnabled()).isFalse();
}
@Test
@@ -117,10 +115,10 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */false, /* autoEnabled= */true);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, true /* fromSUW */);
assertThat(mController.isEnabled()).isTrue();
assertThat(controller.isEnabled()).isTrue();
}
@Test
@@ -129,15 +127,15 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */false, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* fromSUW */);
assertThat(mController.isEnabled()).isFalse();
assertThat(controller.isEnabled()).isFalse();
}
@Test
public void testIsEnabled_shouldReadFromTimeManagerConfig() {
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* fromSUW */);
{
@@ -146,7 +144,7 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */true, /* autoEnabled= */false);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isEnabled()).isFalse();
assertThat(controller.isEnabled()).isFalse();
}
{
@@ -155,7 +153,7 @@ public class AutoTimeZonePreferenceControllerTest {
/* autoSupported= */true, /* autoEnabled= */true);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isEnabled()).isTrue();
assertThat(controller.isEnabled()).isTrue();
}
}
@@ -166,10 +164,10 @@ public class AutoTimeZonePreferenceControllerTest {
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
when(mTimeManager.updateTimeZoneConfiguration(Mockito.any())).thenReturn(true);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, mCallback, false /* fromSUW */);
assertThat(mController.onPreferenceChange(mPreference, true)).isTrue();
assertThat(controller.onPreferenceChange(mPreference, true)).isTrue();
verify(mCallback).updateTimeAndDateDisplay(mContext);
// Check the service was asked to change the configuration correctly.
@@ -184,7 +182,7 @@ public class AutoTimeZonePreferenceControllerTest {
when(mTimeManager.getTimeZoneCapabilitiesAndConfig())
.thenReturn(capabilitiesAndConfigAfterUpdate);
assertThat(mController.isEnabled()).isTrue();
assertThat(controller.isEnabled()).isTrue();
}
@Test
@@ -194,10 +192,10 @@ public class AutoTimeZonePreferenceControllerTest {
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
when(mTimeManager.updateTimeZoneConfiguration(Mockito.any())).thenReturn(true);
mController = new AutoTimeZonePreferenceController(
AutoTimeZonePreferenceController controller = new AutoTimeZonePreferenceController(
mContext, mCallback, false /* fromSUW */);
assertThat(mController.onPreferenceChange(mPreference, false)).isTrue();
assertThat(controller.onPreferenceChange(mPreference, false)).isTrue();
verify(mCallback).updateTimeAndDateDisplay(mContext);
// Check the service was asked to change the configuration correctly.
@@ -212,7 +210,7 @@ public class AutoTimeZonePreferenceControllerTest {
when(mTimeManager.getTimeZoneCapabilitiesAndConfig())
.thenReturn(capabilitiesAndConfigAfterUpdate);
assertThat(mController.isEnabled()).isFalse();
assertThat(controller.isEnabled()).isFalse();
}
private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(

View File

@@ -21,8 +21,14 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.time.Capabilities;
import android.app.time.TimeCapabilities;
import android.app.time.TimeCapabilitiesAndConfig;
import android.app.time.TimeConfiguration;
import android.app.time.TimeManager;
import android.app.timedetector.TimeDetector;
import android.content.Context;
import android.os.UserHandle;
import com.android.settingslib.RestrictedPreference;
@@ -42,9 +48,9 @@ public class DatePreferenceControllerTest {
@Mock
private TimeDetector mTimeDetector;
@Mock
private DatePreferenceController.DatePreferenceHost mHost;
private TimeManager mTimeManager;
@Mock
private AutoTimePreferenceController mAutoTimePreferenceController;
private DatePreferenceController.DatePreferenceHost mHost;
private RestrictedPreference mPreference;
private DatePreferenceController mController;
@@ -53,8 +59,9 @@ public class DatePreferenceControllerTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(TimeDetector.class)).thenReturn(mTimeDetector);
when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
mPreference = new RestrictedPreference(RuntimeEnvironment.application);
mController = new DatePreferenceController(mContext, mHost, mAutoTimePreferenceController);
mController = new DatePreferenceController(mContext, mHost);
}
@Test
@@ -73,7 +80,9 @@ public class DatePreferenceControllerTest {
// Make sure not disabled by admin.
mPreference.setDisabledByAdmin(null);
when(mAutoTimePreferenceController.isEnabled()).thenReturn(true);
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* suggestManualAllowed= */false);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
@@ -84,7 +93,9 @@ public class DatePreferenceControllerTest {
// Make sure not disabled by admin.
mPreference.setDisabledByAdmin(null);
when(mAutoTimePreferenceController.isEnabled()).thenReturn(false);
TimeCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(
/* suggestManualAllowed= */true);
when(mTimeManager.getTimeCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();
@@ -102,4 +113,18 @@ public class DatePreferenceControllerTest {
// Should show date picker
verify(mHost).showDatePicker();
}
private static TimeCapabilitiesAndConfig createCapabilitiesAndConfig(
boolean suggestManualAllowed) {
int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED
: Capabilities.CAPABILITY_NOT_SUPPORTED;
TimeCapabilities capabilities = new TimeCapabilities.Builder(UserHandle.SYSTEM)
.setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED)
.setSuggestManualTimeCapability(suggestManualCapability)
.build();
TimeConfiguration config = new TimeConfiguration.Builder()
.setAutoDetectionEnabled(!suggestManualAllowed)
.build();
return new TimeCapabilitiesAndConfig(capabilities, config);
}
}

View File

@@ -41,7 +41,7 @@ public class TimePreferenceControllerTest {
@Mock
private TimePreferenceController.TimePreferenceHost mHost;
@Mock
private AutoTimePreferenceController mAutoTimePreferenceController;
private DatePreferenceController mDatePreferenceController;
private TimePreferenceController mController;
private RestrictedPreference mPreference;
@@ -50,7 +50,7 @@ public class TimePreferenceControllerTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mPreference = new RestrictedPreference(RuntimeEnvironment.application);
mController = new TimePreferenceController(mContext, mHost, mAutoTimePreferenceController);
mController = new TimePreferenceController(mContext, mHost, mDatePreferenceController);
}
@Test
@@ -59,22 +59,22 @@ public class TimePreferenceControllerTest {
}
@Test
public void updateState_autoTimeEnabled_shouldDisablePref() {
public void updateState_dateEntryDisabled_shouldDisablePref() {
// Make sure not disabled by admin.
mPreference.setDisabledByAdmin(null);
when(mAutoTimePreferenceController.isEnabled()).thenReturn(true);
when(mDatePreferenceController.isEnabled()).thenReturn(false);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void updateState_autoTimeDisabled_shouldEnablePref() {
public void updateState_dateEntryEnabled_shouldEnablePref() {
// Make sure not disabled by admin.
mPreference.setDisabledByAdmin(null);
when(mAutoTimePreferenceController.isEnabled()).thenReturn(false);
when(mDatePreferenceController.isEnabled()).thenReturn(true);
mController.updateState(mPreference);
assertThat(mPreference.isEnabled()).isTrue();