Merge "Add "Use location" state to TimeZoneCapabilities"

This commit is contained in:
Neil Fuller
2023-01-31 15:57:43 +00:00
committed by Android (Google) Code Review
6 changed files with 150 additions and 92 deletions

View File

@@ -19,10 +19,12 @@ import android.app.time.DetectorStatusTypes;
import android.app.time.LocationTimeZoneAlgorithmStatus; import android.app.time.LocationTimeZoneAlgorithmStatus;
import android.app.time.TelephonyTimeZoneAlgorithmStatus; import android.app.time.TelephonyTimeZoneAlgorithmStatus;
import android.app.time.TimeManager; import android.app.time.TimeManager;
import android.app.time.TimeZoneCapabilities;
import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneDetectorStatus; import android.app.time.TimeZoneDetectorStatus;
import android.content.Context; import android.content.Context;
import android.location.LocationManager;
import android.service.timezone.TimeZoneProviderStatus; import android.service.timezone.TimeZoneProviderStatus;
import android.service.timezone.TimeZoneProviderStatus.DependencyStatus;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -43,14 +45,12 @@ import java.util.concurrent.Executor;
public class LocationProviderStatusPreferenceController public class LocationProviderStatusPreferenceController
extends BasePreferenceController implements TimeManager.TimeZoneDetectorListener { extends BasePreferenceController implements TimeManager.TimeZoneDetectorListener {
private final TimeManager mTimeManager; private final TimeManager mTimeManager;
private final LocationManager mLocationManager;
private BannerMessagePreference mPreference = null; private BannerMessagePreference mPreference = null;
public LocationProviderStatusPreferenceController(Context context, String preferenceKey) { public LocationProviderStatusPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey); super(context, preferenceKey);
mTimeManager = context.getSystemService(TimeManager.class); mTimeManager = context.getSystemService(TimeManager.class);
mLocationManager = context.getSystemService(LocationManager.class);
Executor mainExecutor = context.getMainExecutor(); Executor mainExecutor = context.getMainExecutor();
mTimeManager.addTimeZoneDetectorListener(mainExecutor, this); mTimeManager.addTimeZoneDetectorListener(mainExecutor, this);
@@ -123,11 +123,15 @@ public class LocationProviderStatusPreferenceController
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
boolean locationEnabled = mLocationManager.isLocationEnabled(); final TimeZoneCapabilitiesAndConfig timeZoneCapabilitiesAndConfig =
mTimeManager.getTimeZoneCapabilitiesAndConfig();
final TimeZoneDetectorStatus detectorStatus = final TimeZoneDetectorStatus detectorStatus =
mTimeManager.getTimeZoneCapabilitiesAndConfig().getDetectorStatus(); timeZoneCapabilitiesAndConfig.getDetectorStatus();
final TimeZoneCapabilities timeZoneCapabilities =
timeZoneCapabilitiesAndConfig.getCapabilities();
if (!locationEnabled && hasLocationTimeZoneNoTelephonyFallback(detectorStatus)) { if (!timeZoneCapabilities.isUseLocationEnabled()
&& hasLocationTimeZoneNoTelephonyFallback(detectorStatus)) {
return mContext.getResources().getString( return mContext.getResources().getString(
R.string.location_time_zone_detection_status_summary_blocked_by_settings); R.string.location_time_zone_detection_status_summary_blocked_by_settings);
} }
@@ -137,7 +141,7 @@ public class LocationProviderStatusPreferenceController
return ""; return "";
} }
int status = ltzpStatus.getLocationDetectionDependencyStatus(); @DependencyStatus int status = ltzpStatus.getLocationDetectionDependencyStatus();
if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_ENVIRONMENT) { if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_ENVIRONMENT) {
return mContext.getResources().getString( return mContext.getResources().getString(

View File

@@ -25,7 +25,6 @@ import android.app.time.TimeZoneCapabilities;
import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneConfiguration;
import android.content.Context; import android.content.Context;
import android.location.LocationManager;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -50,7 +49,6 @@ public class LocationTimeZoneDetectionPreferenceController
private static final String TAG = "location_time_zone_detection"; private static final String TAG = "location_time_zone_detection";
private final TimeManager mTimeManager; private final TimeManager mTimeManager;
private final LocationManager mLocationManager;
private TimeZoneCapabilitiesAndConfig mTimeZoneCapabilitiesAndConfig; private TimeZoneCapabilitiesAndConfig mTimeZoneCapabilitiesAndConfig;
private InstrumentedPreferenceFragment mFragment; private InstrumentedPreferenceFragment mFragment;
private Preference mPreference; private Preference mPreference;
@@ -58,7 +56,6 @@ public class LocationTimeZoneDetectionPreferenceController
public LocationTimeZoneDetectionPreferenceController(Context context) { public LocationTimeZoneDetectionPreferenceController(Context context) {
super(context, TAG); super(context, TAG);
mTimeManager = context.getSystemService(TimeManager.class); mTimeManager = context.getSystemService(TimeManager.class);
mLocationManager = context.getSystemService(LocationManager.class);
} }
void setFragment(InstrumentedPreferenceFragment fragment) { void setFragment(InstrumentedPreferenceFragment fragment) {
@@ -68,14 +65,18 @@ public class LocationTimeZoneDetectionPreferenceController
@Override @Override
public boolean isChecked() { public boolean isChecked() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
mTimeManager.getTimeZoneCapabilitiesAndConfig(); getTimeZoneCapabilitiesAndConfig(/*forceRefresh=*/false);
TimeZoneConfiguration configuration = capabilitiesAndConfig.getConfiguration(); TimeZoneConfiguration configuration = capabilitiesAndConfig.getConfiguration();
return configuration.isGeoDetectionEnabled(); return configuration.isGeoDetectionEnabled();
} }
@Override @Override
public boolean setChecked(boolean isChecked) { public boolean setChecked(boolean isChecked) {
if (isChecked && !mLocationManager.isLocationEnabled()) { TimeZoneCapabilitiesAndConfig timeZoneCapabilitiesAndConfig =
getTimeZoneCapabilitiesAndConfig(/*forceRefresh=*/false);
boolean isLocationEnabled =
timeZoneCapabilitiesAndConfig.getCapabilities().isUseLocationEnabled();
if (isChecked && !isLocationEnabled) {
new LocationToggleDisabledDialogFragment(mContext) new LocationToggleDisabledDialogFragment(mContext)
.show(mFragment.getFragmentManager(), TAG); .show(mFragment.getFragmentManager(), TAG);
// Toggle status is not updated. // Toggle status is not updated.
@@ -157,11 +158,13 @@ public class LocationTimeZoneDetectionPreferenceController
// The preference should not be visible, but text is referenced in case this changes. // The preference should not be visible, but text is referenced in case this changes.
summaryResId = R.string.location_time_zone_detection_not_allowed; summaryResId = R.string.location_time_zone_detection_not_allowed;
} else if (configureGeoDetectionEnabledCapability == CAPABILITY_NOT_APPLICABLE) { } else if (configureGeoDetectionEnabledCapability == CAPABILITY_NOT_APPLICABLE) {
// The TimeZoneCapabilities deliberately doesn't provide information about why the user boolean isLocationEnabled =
// doesn't have the capability, but the user's "location enabled" being off and the timeZoneCapabilitiesAndConfig.getCapabilities().isUseLocationEnabled();
// global automatic detection setting will always be considered overriding reasons why // The TimeZoneCapabilities cannot provide implementation-specific information about why
// location time zone detection cannot be used. // the user doesn't have the capability, but the user's "location enabled" being off and
if (!mLocationManager.isLocationEnabled()) { // the global automatic detection setting will always be considered overriding reasons
// why location time zone detection cannot be used.
if (!isLocationEnabled) {
summaryResId = R.string.location_app_permission_summary_location_off; summaryResId = R.string.location_app_permission_summary_location_off;
} else if (!configuration.isAutoDetectionEnabled()) { } else if (!configuration.isAutoDetectionEnabled()) {
summaryResId = R.string.location_time_zone_detection_auto_is_off; summaryResId = R.string.location_time_zone_detection_auto_is_off;
@@ -184,6 +187,10 @@ public class LocationTimeZoneDetectionPreferenceController
return mContext.getString(summaryResId); return mContext.getString(summaryResId);
} }
/**
* Implementation of {@link TimeManager.TimeZoneDetectorListener#onChange()}. Called by the
* system server after a change that affects {@link TimeZoneCapabilitiesAndConfig}.
*/
@Override @Override
public void onChange() { public void onChange() {
refreshUi(); refreshUi();

View File

@@ -37,7 +37,6 @@ import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneConfiguration;
import android.app.time.TimeZoneDetectorStatus; import android.app.time.TimeZoneDetectorStatus;
import android.content.Context; import android.content.Context;
import android.location.LocationManager;
import android.os.UserHandle; import android.os.UserHandle;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -62,8 +61,6 @@ public class AutoTimeZonePreferenceControllerTest {
private Preference mPreference; private Preference mPreference;
@Mock @Mock
private TimeManager mTimeManager; private TimeManager mTimeManager;
@Mock
private LocationManager mLocationManager;
@Before @Before
public void setUp() { public void setUp() {
@@ -73,9 +70,6 @@ public class AutoTimeZonePreferenceControllerTest {
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
} }
@Test @Test
@@ -267,6 +261,7 @@ public class AutoTimeZonePreferenceControllerTest {
: Capabilities.CAPABILITY_NOT_SUPPORTED; : Capabilities.CAPABILITY_NOT_SUPPORTED;
TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM) TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM)
.setConfigureAutoDetectionEnabledCapability(configureAutoDetectionEnabledCapability) .setConfigureAutoDetectionEnabledCapability(configureAutoDetectionEnabledCapability)
.setUseLocationEnabled(true)
.setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED) .setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED)
.setSetManualTimeZoneCapability(Capabilities.CAPABILITY_POSSESSED) .setSetManualTimeZoneCapability(Capabilities.CAPABILITY_POSSESSED)
.build(); .build();

View File

@@ -29,7 +29,9 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.time.Capabilities; import android.app.time.Capabilities;
import android.app.time.Capabilities.CapabilityState;
import android.app.time.LocationTimeZoneAlgorithmStatus; import android.app.time.LocationTimeZoneAlgorithmStatus;
import android.app.time.LocationTimeZoneAlgorithmStatus.ProviderStatus;
import android.app.time.TelephonyTimeZoneAlgorithmStatus; import android.app.time.TelephonyTimeZoneAlgorithmStatus;
import android.app.time.TimeManager; import android.app.time.TimeManager;
import android.app.time.TimeZoneCapabilities; import android.app.time.TimeZoneCapabilities;
@@ -37,9 +39,9 @@ import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneConfiguration;
import android.app.time.TimeZoneDetectorStatus; import android.app.time.TimeZoneDetectorStatus;
import android.content.Context; import android.content.Context;
import android.location.LocationManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.service.timezone.TimeZoneProviderStatus; import android.service.timezone.TimeZoneProviderStatus;
import android.service.timezone.TimeZoneProviderStatus.DependencyStatus;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -60,8 +62,6 @@ public class LocationProviderStatusPreferenceControllerTest {
private Context mContext; private Context mContext;
@Mock @Mock
private TimeManager mTimeManager; private TimeManager mTimeManager;
@Mock
private LocationManager mLocationManager;
@Before @Before
public void setUp() { public void setUp() {
@@ -69,8 +69,6 @@ public class LocationProviderStatusPreferenceControllerTest {
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager);
when(mLocationManager.isLocationEnabled()).thenReturn(true);
when(mContext.getString( when(mContext.getString(
R.string.location_time_zone_detection_status_summary_blocked_by_settings)) R.string.location_time_zone_detection_status_summary_blocked_by_settings))
.thenReturn("BBS"); .thenReturn("BBS");
@@ -78,11 +76,12 @@ public class LocationProviderStatusPreferenceControllerTest {
@Test @Test
public void testCapabilityStatus() { public void testCapabilityStatus() {
LocationProviderStatusPreferenceController controller =
new LocationProviderStatusPreferenceController(mContext, "LPSPC");
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false, TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK); DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
LocationProviderStatusPreferenceController controller =
new LocationProviderStatusPreferenceController(mContext, "LPSPC");
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
@@ -95,14 +94,16 @@ public class LocationProviderStatusPreferenceControllerTest {
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, DEPENDENCY_STATUS_OK); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(true, capabilitiesAndConfig = createCapabilitiesAndConfig(true,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, DEPENDENCY_STATUS_OK); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
@@ -111,31 +112,36 @@ public class LocationProviderStatusPreferenceControllerTest {
@Test @Test
public void testProviderStatus_primaryCertain() { public void testProviderStatus_primaryCertain() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
LocationProviderStatusPreferenceController controller = LocationProviderStatusPreferenceController controller =
new LocationProviderStatusPreferenceController(mContext, "LPSPC"); new LocationProviderStatusPreferenceController(mContext, "LPSPC");
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, DEPENDENCY_STATUS_OK); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
@@ -144,32 +150,36 @@ public class LocationProviderStatusPreferenceControllerTest {
@Test @Test
public void testProviderStatus_primaryUncertain() { public void testProviderStatus_primaryUncertain() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK, PROVIDER_STATUS_IS_CERTAIN);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
LocationProviderStatusPreferenceController controller = LocationProviderStatusPreferenceController controller =
new LocationProviderStatusPreferenceController(mContext, "LPSPC"); new LocationProviderStatusPreferenceController(mContext, "LPSPC");
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, DEPENDENCY_STATUS_OK, PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
PROVIDER_STATUS_IS_CERTAIN); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, PROVIDER_STATUS_IS_UNCERTAIN); PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_OK, PROVIDER_STATUS_IS_UNCERTAIN); PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_OK,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
@@ -177,18 +187,21 @@ public class LocationProviderStatusPreferenceControllerTest {
} }
@Test @Test
public void testProviderStatus_nullProviders() { public void testProviderStatus_nullProviderStatuses() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
null, null);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
LocationProviderStatusPreferenceController controller = LocationProviderStatusPreferenceController controller =
new LocationProviderStatusPreferenceController(mContext, "LPSPC"); new LocationProviderStatusPreferenceController(mContext, "LPSPC");
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false,
PROVIDER_STATUS_IS_CERTAIN, null,
PROVIDER_STATUS_IS_CERTAIN, null);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, null); PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS,
PROVIDER_STATUS_IS_CERTAIN, null);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
@@ -200,39 +213,63 @@ public class LocationProviderStatusPreferenceControllerTest {
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.CONDITIONALLY_UNAVAILABLE); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
capabilitiesAndConfig = createCapabilitiesAndConfig(false, null, capabilitiesAndConfig = createCapabilitiesAndConfig(false,
DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); PROVIDER_STATUS_IS_CERTAIN, null,
PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(controller.getAvailabilityStatus()).isEqualTo( assertThat(controller.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE_UNSEARCHABLE); BasePreferenceController.AVAILABLE_UNSEARCHABLE);
} }
private TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(boolean capabilitySupported, private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(
@Nullable Integer primary, @Nullable Integer secondary) { boolean userCanConfigureGeoDetection,
return createCapabilitiesAndConfig(capabilitySupported, primary, secondary, @Nullable @DependencyStatus Integer primaryProviderLocationStatus,
PROVIDER_STATUS_IS_CERTAIN); @Nullable @DependencyStatus Integer secondaryProviderLocationStatus) {
return createCapabilitiesAndConfig(userCanConfigureGeoDetection,
PROVIDER_STATUS_IS_CERTAIN, primaryProviderLocationStatus,
PROVIDER_STATUS_IS_CERTAIN, secondaryProviderLocationStatus);
} }
private TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(boolean capabilitySupported, private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(
@Nullable Integer primary, @Nullable Integer secondary, int primaryProviderStatus) { boolean userCanConfigureGeoDetection,
TimeZoneDetectorStatus status = new TimeZoneDetectorStatus(DETECTOR_STATUS_RUNNING, @ProviderStatus int primaryProviderStatus,
new TelephonyTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING), @Nullable @DependencyStatus Integer primaryProviderLocationStatus,
new LocationTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING, @ProviderStatus int secondaryProviderStatus,
primaryProviderStatus, primary != null @Nullable @DependencyStatus Integer secondaryProviderLocationStatus) {
? new TimeZoneProviderStatus.Builder().setLocationDetectionDependencyStatus( TelephonyTimeZoneAlgorithmStatus telephonyTimeZoneAlgorithmStatus =
primary).build() : null, PROVIDER_STATUS_IS_CERTAIN, secondary != null new TelephonyTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING);
? new TimeZoneProviderStatus.Builder().setLocationDetectionDependencyStatus(
secondary).build() : null));
TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder( LocationTimeZoneAlgorithmStatus locationTimeZoneAlgorithmStatus =
UserHandle.SYSTEM).setConfigureAutoDetectionEnabledCapability( new LocationTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING,
Capabilities.CAPABILITY_POSSESSED).setConfigureGeoDetectionEnabledCapability( primaryProviderStatus,
capabilitySupported ? Capabilities.CAPABILITY_POSSESSED createTimeZoneProviderStatusOrNull(primaryProviderLocationStatus),
: Capabilities.CAPABILITY_NOT_SUPPORTED).setSetManualTimeZoneCapability( secondaryProviderStatus,
Capabilities.CAPABILITY_POSSESSED).build(); createTimeZoneProviderStatusOrNull(secondaryProviderLocationStatus));
TimeZoneDetectorStatus status = new TimeZoneDetectorStatus(DETECTOR_STATUS_RUNNING,
telephonyTimeZoneAlgorithmStatus, locationTimeZoneAlgorithmStatus);
@CapabilityState int configureGeoDetectionEnabledCapability = userCanConfigureGeoDetection
? Capabilities.CAPABILITY_POSSESSED : Capabilities.CAPABILITY_NOT_SUPPORTED;
TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM)
.setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED)
.setUseLocationEnabled(true)
.setConfigureGeoDetectionEnabledCapability(configureGeoDetectionEnabledCapability)
.setSetManualTimeZoneCapability(Capabilities.CAPABILITY_POSSESSED)
.build();
return new TimeZoneCapabilitiesAndConfig(status, capabilities, return new TimeZoneCapabilitiesAndConfig(status, capabilities,
new TimeZoneConfiguration.Builder().build()); new TimeZoneConfiguration.Builder().build());
} }
private static TimeZoneProviderStatus createTimeZoneProviderStatusOrNull(
@Nullable @DependencyStatus Integer locationDependencyStatusOrNull) {
if (locationDependencyStatusOrNull == null) {
return null;
}
return new TimeZoneProviderStatus.Builder()
.setLocationDetectionDependencyStatus(locationDependencyStatusOrNull)
.build();
}
} }

View File

@@ -27,9 +27,10 @@ import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_N
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.time.Capabilities.CapabilityState; import android.app.time.Capabilities.CapabilityState;
@@ -41,7 +42,6 @@ import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneConfiguration;
import android.app.time.TimeZoneDetectorStatus; import android.app.time.TimeZoneDetectorStatus;
import android.content.Context; import android.content.Context;
import android.location.LocationManager;
import android.os.UserHandle; import android.os.UserHandle;
import com.android.settings.R; import com.android.settings.R;
@@ -60,8 +60,6 @@ import org.robolectric.RuntimeEnvironment;
public class LocationTimeZoneDetectionPreferenceControllerTest { public class LocationTimeZoneDetectionPreferenceControllerTest {
@Mock @Mock
private TimeManager mTimeManager; private TimeManager mTimeManager;
@Mock
private LocationManager mLocationManager;
private Context mContext; private Context mContext;
private LocationTimeZoneDetectionPreferenceController mController; private LocationTimeZoneDetectionPreferenceController mController;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -72,14 +70,16 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager);
when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager);
mController = new LocationTimeZoneDetectionPreferenceController(mContext); mController = new LocationTimeZoneDetectionPreferenceController(mContext);
mController.setFragment(mFragment); mController.setFragment(mFragment);
} }
@Test @Test
public void setChecked_withTrue_shouldUpdateSetting_whenLocationIsEnabled() { public void setChecked_withTrue_shouldUpdateSetting_whenLocationIsEnabled() {
when(mLocationManager.isLocationEnabled()).thenReturn(true); boolean useLocationEnabled = true;
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
// Simulate the UI being clicked. // Simulate the UI being clicked.
mController.setChecked(true); mController.setChecked(true);
@@ -98,17 +98,25 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
@Test @Test
public void setChecked_withTrue_shouldDoNothing_whenLocationIsDisabled() { public void setChecked_withTrue_shouldDoNothing_whenLocationIsDisabled() {
when(mLocationManager.isLocationEnabled()).thenReturn(false); boolean useLocationEnabled = false;
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
// Simulate the UI being clicked. // Simulate the UI being clicked.
mController.setChecked(true); mController.setChecked(true);
// Verify the TimeManager was not called. // Verify the TimeManager was not updated.
verifyNoInteractions(mTimeManager); verify(mTimeManager, never()).updateTimeZoneConfiguration(any());
} }
@Test @Test
public void setChecked_withFalse_shouldUpdateSetting() { public void setChecked_withFalse_shouldUpdateSetting() {
boolean useLocationEnabled = false;
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
// Simulate the UI being clicked. // Simulate the UI being clicked.
mController.setChecked(false); mController.setChecked(false);
@@ -121,8 +129,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
@Test @Test
public void testLocationTimeZoneDetection_supported_shouldBeShown() { public void testLocationTimeZoneDetection_supported_shouldBeShown() {
boolean useLocationEnabled = false;
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
createTimeZoneCapabilitiesAndConfig(CAPABILITY_POSSESSED); createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isAvailable()).isTrue(); assertThat(mController.isAvailable()).isTrue();
@@ -130,8 +139,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
@Test @Test
public void testLocationTimeZoneDetection_unsupported_shouldNotBeShown() { public void testLocationTimeZoneDetection_unsupported_shouldNotBeShown() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = boolean useLocationEnabled = false;
createTimeZoneCapabilitiesAndConfig(CAPABILITY_NOT_SUPPORTED); TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createTimeZoneCapabilitiesAndConfig(
useLocationEnabled, CAPABILITY_NOT_SUPPORTED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.isAvailable()).isFalse(); assertThat(mController.isAvailable()).isFalse();
@@ -142,8 +152,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
*/ */
@Test @Test
public void testLocationTimeZoneDetection_summary_geoDetectionEnabled() { public void testLocationTimeZoneDetection_summary_geoDetectionEnabled() {
boolean useLocationEnabled = false;
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
createTimeZoneCapabilitiesAndConfig(CAPABILITY_POSSESSED); createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
assertThat(mController.getSummary()).isEqualTo( assertThat(mController.getSummary()).isEqualTo(
@@ -152,11 +163,11 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
@Test @Test
public void testLocationTimeZoneDetection_toggleIsOn_whenGeoDetectionEnabledAnsMlsIsOff() { public void testLocationTimeZoneDetection_toggleIsOn_whenGeoDetectionEnabledAnsMlsIsOff() {
TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = boolean useLocationEnabled = false;
createTimeZoneCapabilitiesAndConfig(CAPABILITY_NOT_APPLICABLE); TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createTimeZoneCapabilitiesAndConfig(
useLocationEnabled, CAPABILITY_NOT_APPLICABLE);
when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig);
when(mLocationManager.isLocationEnabled()).thenReturn(false);
assertThat(mController.isChecked()).isTrue(); assertThat(mController.isChecked()).isTrue();
assertThat(mController.getSummary()).isEqualTo( assertThat(mController.getSummary()).isEqualTo(
@@ -164,6 +175,7 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
} }
private static TimeZoneCapabilitiesAndConfig createTimeZoneCapabilitiesAndConfig( private static TimeZoneCapabilitiesAndConfig createTimeZoneCapabilitiesAndConfig(
boolean useLocationEnabled,
@CapabilityState int configureGeoDetectionEnabledCapability) { @CapabilityState int configureGeoDetectionEnabledCapability) {
// Create a status that matches the user's capability state. // Create a status that matches the user's capability state.
@@ -191,6 +203,7 @@ public class LocationTimeZoneDetectionPreferenceControllerTest {
UserHandle arbitraryUserHandle = UserHandle.of(123); UserHandle arbitraryUserHandle = UserHandle.of(123);
TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(arbitraryUserHandle) TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(arbitraryUserHandle)
.setConfigureAutoDetectionEnabledCapability(CAPABILITY_POSSESSED) .setConfigureAutoDetectionEnabledCapability(CAPABILITY_POSSESSED)
.setUseLocationEnabled(useLocationEnabled)
.setConfigureGeoDetectionEnabledCapability(configureGeoDetectionEnabledCapability) .setConfigureGeoDetectionEnabledCapability(configureGeoDetectionEnabledCapability)
.setSetManualTimeZoneCapability(CAPABILITY_NOT_APPLICABLE) .setSetManualTimeZoneCapability(CAPABILITY_NOT_APPLICABLE)
.build(); .build();

View File

@@ -112,8 +112,10 @@ public class TimeZonePreferenceControllerTest {
PROVIDER_STATUS_NOT_PRESENT, null)); PROVIDER_STATUS_NOT_PRESENT, null));
int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED
: Capabilities.CAPABILITY_NOT_SUPPORTED; : Capabilities.CAPABILITY_NOT_SUPPORTED;
boolean useLocationEnabled = true;
TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM) TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM)
.setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED) .setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED)
.setUseLocationEnabled(useLocationEnabled)
.setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED) .setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED)
.setSetManualTimeZoneCapability(suggestManualCapability) .setSetManualTimeZoneCapability(suggestManualCapability)
.build(); .build();