diff --git a/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java b/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java index 9380f130846..a64273bc9f1 100644 --- a/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java +++ b/src/com/android/settings/datetime/LocationProviderStatusPreferenceController.java @@ -19,10 +19,12 @@ import android.app.time.DetectorStatusTypes; import android.app.time.LocationTimeZoneAlgorithmStatus; import android.app.time.TelephonyTimeZoneAlgorithmStatus; import android.app.time.TimeManager; +import android.app.time.TimeZoneCapabilities; +import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneDetectorStatus; import android.content.Context; -import android.location.LocationManager; import android.service.timezone.TimeZoneProviderStatus; +import android.service.timezone.TimeZoneProviderStatus.DependencyStatus; import android.text.TextUtils; import androidx.annotation.Nullable; @@ -43,14 +45,12 @@ import java.util.concurrent.Executor; public class LocationProviderStatusPreferenceController extends BasePreferenceController implements TimeManager.TimeZoneDetectorListener { private final TimeManager mTimeManager; - private final LocationManager mLocationManager; private BannerMessagePreference mPreference = null; public LocationProviderStatusPreferenceController(Context context, String preferenceKey) { super(context, preferenceKey); mTimeManager = context.getSystemService(TimeManager.class); - mLocationManager = context.getSystemService(LocationManager.class); Executor mainExecutor = context.getMainExecutor(); mTimeManager.addTimeZoneDetectorListener(mainExecutor, this); @@ -123,11 +123,15 @@ public class LocationProviderStatusPreferenceController @Override public CharSequence getSummary() { - boolean locationEnabled = mLocationManager.isLocationEnabled(); + final TimeZoneCapabilitiesAndConfig timeZoneCapabilitiesAndConfig = + mTimeManager.getTimeZoneCapabilitiesAndConfig(); 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( R.string.location_time_zone_detection_status_summary_blocked_by_settings); } @@ -137,7 +141,7 @@ public class LocationProviderStatusPreferenceController return ""; } - int status = ltzpStatus.getLocationDetectionDependencyStatus(); + @DependencyStatus int status = ltzpStatus.getLocationDetectionDependencyStatus(); if (status == TimeZoneProviderStatus.DEPENDENCY_STATUS_BLOCKED_BY_ENVIRONMENT) { return mContext.getResources().getString( diff --git a/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceController.java b/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceController.java index 36cbc9e0b80..d5674666efb 100644 --- a/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceController.java +++ b/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceController.java @@ -25,7 +25,6 @@ import android.app.time.TimeZoneCapabilities; import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneConfiguration; import android.content.Context; -import android.location.LocationManager; import androidx.preference.Preference; import androidx.preference.PreferenceScreen; @@ -50,7 +49,6 @@ public class LocationTimeZoneDetectionPreferenceController private static final String TAG = "location_time_zone_detection"; private final TimeManager mTimeManager; - private final LocationManager mLocationManager; private TimeZoneCapabilitiesAndConfig mTimeZoneCapabilitiesAndConfig; private InstrumentedPreferenceFragment mFragment; private Preference mPreference; @@ -58,7 +56,6 @@ public class LocationTimeZoneDetectionPreferenceController public LocationTimeZoneDetectionPreferenceController(Context context) { super(context, TAG); mTimeManager = context.getSystemService(TimeManager.class); - mLocationManager = context.getSystemService(LocationManager.class); } void setFragment(InstrumentedPreferenceFragment fragment) { @@ -68,14 +65,18 @@ public class LocationTimeZoneDetectionPreferenceController @Override public boolean isChecked() { TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = - mTimeManager.getTimeZoneCapabilitiesAndConfig(); + getTimeZoneCapabilitiesAndConfig(/*forceRefresh=*/false); TimeZoneConfiguration configuration = capabilitiesAndConfig.getConfiguration(); return configuration.isGeoDetectionEnabled(); } @Override 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) .show(mFragment.getFragmentManager(), TAG); // 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. summaryResId = R.string.location_time_zone_detection_not_allowed; } else if (configureGeoDetectionEnabledCapability == CAPABILITY_NOT_APPLICABLE) { - // The TimeZoneCapabilities deliberately doesn't provide information about why the user - // doesn't have the capability, but the user's "location enabled" being off and the - // global automatic detection setting will always be considered overriding reasons why - // location time zone detection cannot be used. - if (!mLocationManager.isLocationEnabled()) { + boolean isLocationEnabled = + timeZoneCapabilitiesAndConfig.getCapabilities().isUseLocationEnabled(); + // The TimeZoneCapabilities cannot provide implementation-specific information about why + // the user doesn't have the capability, but the user's "location enabled" being off and + // 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; } else if (!configuration.isAutoDetectionEnabled()) { summaryResId = R.string.location_time_zone_detection_auto_is_off; @@ -184,6 +187,10 @@ public class LocationTimeZoneDetectionPreferenceController return mContext.getString(summaryResId); } + /** + * Implementation of {@link TimeManager.TimeZoneDetectorListener#onChange()}. Called by the + * system server after a change that affects {@link TimeZoneCapabilitiesAndConfig}. + */ @Override public void onChange() { refreshUi(); diff --git a/tests/robotests/src/com/android/settings/datetime/AutoTimeZonePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/AutoTimeZonePreferenceControllerTest.java index 41aca50f1af..7685a5bedc0 100644 --- a/tests/robotests/src/com/android/settings/datetime/AutoTimeZonePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/AutoTimeZonePreferenceControllerTest.java @@ -37,7 +37,6 @@ import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneDetectorStatus; import android.content.Context; -import android.location.LocationManager; import android.os.UserHandle; import androidx.preference.Preference; @@ -62,8 +61,6 @@ public class AutoTimeZonePreferenceControllerTest { private Preference mPreference; @Mock private TimeManager mTimeManager; - @Mock - private LocationManager mLocationManager; @Before public void setUp() { @@ -73,9 +70,6 @@ public class AutoTimeZonePreferenceControllerTest { mPreference = new Preference(mContext); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); - when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager); - - when(mLocationManager.isLocationEnabled()).thenReturn(true); } @Test @@ -267,6 +261,7 @@ public class AutoTimeZonePreferenceControllerTest { : Capabilities.CAPABILITY_NOT_SUPPORTED; TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM) .setConfigureAutoDetectionEnabledCapability(configureAutoDetectionEnabledCapability) + .setUseLocationEnabled(true) .setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED) .setSetManualTimeZoneCapability(Capabilities.CAPABILITY_POSSESSED) .build(); diff --git a/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java index 4f9f1cc2df1..b838355d2fd 100644 --- a/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/LocationProviderStatusPreferenceControllerTest.java @@ -29,7 +29,9 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.app.time.Capabilities; +import android.app.time.Capabilities.CapabilityState; import android.app.time.LocationTimeZoneAlgorithmStatus; +import android.app.time.LocationTimeZoneAlgorithmStatus.ProviderStatus; import android.app.time.TelephonyTimeZoneAlgorithmStatus; import android.app.time.TimeManager; import android.app.time.TimeZoneCapabilities; @@ -37,9 +39,9 @@ import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneDetectorStatus; import android.content.Context; -import android.location.LocationManager; import android.os.UserHandle; import android.service.timezone.TimeZoneProviderStatus; +import android.service.timezone.TimeZoneProviderStatus.DependencyStatus; import androidx.annotation.Nullable; @@ -60,8 +62,6 @@ public class LocationProviderStatusPreferenceControllerTest { private Context mContext; @Mock private TimeManager mTimeManager; - @Mock - private LocationManager mLocationManager; @Before public void setUp() { @@ -69,8 +69,6 @@ public class LocationProviderStatusPreferenceControllerTest { mContext = spy(RuntimeEnvironment.application); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); - when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager); - when(mLocationManager.isLocationEnabled()).thenReturn(true); when(mContext.getString( R.string.location_time_zone_detection_status_summary_blocked_by_settings)) .thenReturn("BBS"); @@ -78,11 +76,12 @@ public class LocationProviderStatusPreferenceControllerTest { @Test public void testCapabilityStatus() { + LocationProviderStatusPreferenceController controller = + new LocationProviderStatusPreferenceController(mContext, "LPSPC"); + TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); - LocationProviderStatusPreferenceController controller = - new LocationProviderStatusPreferenceController(mContext, "LPSPC"); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); @@ -95,14 +94,16 @@ public class LocationProviderStatusPreferenceControllerTest { BasePreferenceController.CONDITIONALLY_UNAVAILABLE); 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); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.AVAILABLE_UNSEARCHABLE); 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); assertThat(controller.getAvailabilityStatus()).isEqualTo( @@ -111,31 +112,36 @@ public class LocationProviderStatusPreferenceControllerTest { @Test public void testProviderStatus_primaryCertain() { - TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false, - DEPENDENCY_STATUS_OK, DEPENDENCY_STATUS_OK); - when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); LocationProviderStatusPreferenceController controller = 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( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); 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); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.AVAILABLE_UNSEARCHABLE); - capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, - DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); + capabilitiesAndConfig = createCapabilitiesAndConfig(false, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); - capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, - DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); + capabilitiesAndConfig = createCapabilitiesAndConfig(false, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( @@ -144,32 +150,36 @@ public class LocationProviderStatusPreferenceControllerTest { @Test 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 = 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( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); capabilitiesAndConfig = createCapabilitiesAndConfig(false, - DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, DEPENDENCY_STATUS_OK, - PROVIDER_STATUS_IS_CERTAIN); + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.AVAILABLE_UNSEARCHABLE); - capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, - DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS, PROVIDER_STATUS_IS_UNCERTAIN); + capabilitiesAndConfig = createCapabilitiesAndConfig(false, + PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_OK, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.AVAILABLE_UNSEARCHABLE); - capabilitiesAndConfig = createCapabilitiesAndConfig(false, DEPENDENCY_STATUS_OK, - DEPENDENCY_STATUS_OK, PROVIDER_STATUS_IS_UNCERTAIN); + capabilitiesAndConfig = createCapabilitiesAndConfig(false, + PROVIDER_STATUS_IS_UNCERTAIN, DEPENDENCY_STATUS_OK, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_OK); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( @@ -177,18 +187,21 @@ public class LocationProviderStatusPreferenceControllerTest { } @Test - public void testProviderStatus_nullProviders() { - TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createCapabilitiesAndConfig(false, - null, null); - when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); + public void testProviderStatus_nullProviderStatuses() { LocationProviderStatusPreferenceController controller = 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( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); 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); assertThat(controller.getAvailabilityStatus()).isEqualTo( @@ -200,39 +213,63 @@ public class LocationProviderStatusPreferenceControllerTest { assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.CONDITIONALLY_UNAVAILABLE); - capabilitiesAndConfig = createCapabilitiesAndConfig(false, null, - DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); + capabilitiesAndConfig = createCapabilitiesAndConfig(false, + PROVIDER_STATUS_IS_CERTAIN, null, + PROVIDER_STATUS_IS_CERTAIN, DEPENDENCY_STATUS_BLOCKED_BY_SETTINGS); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(controller.getAvailabilityStatus()).isEqualTo( BasePreferenceController.AVAILABLE_UNSEARCHABLE); } - private TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(boolean capabilitySupported, - @Nullable Integer primary, @Nullable Integer secondary) { - return createCapabilitiesAndConfig(capabilitySupported, primary, secondary, - PROVIDER_STATUS_IS_CERTAIN); + private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig( + boolean userCanConfigureGeoDetection, + @Nullable @DependencyStatus Integer primaryProviderLocationStatus, + @Nullable @DependencyStatus Integer secondaryProviderLocationStatus) { + return createCapabilitiesAndConfig(userCanConfigureGeoDetection, + PROVIDER_STATUS_IS_CERTAIN, primaryProviderLocationStatus, + PROVIDER_STATUS_IS_CERTAIN, secondaryProviderLocationStatus); } - private TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig(boolean capabilitySupported, - @Nullable Integer primary, @Nullable Integer secondary, int primaryProviderStatus) { - TimeZoneDetectorStatus status = new TimeZoneDetectorStatus(DETECTOR_STATUS_RUNNING, - new TelephonyTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING), - new LocationTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING, - primaryProviderStatus, primary != null - ? new TimeZoneProviderStatus.Builder().setLocationDetectionDependencyStatus( - primary).build() : null, PROVIDER_STATUS_IS_CERTAIN, secondary != null - ? new TimeZoneProviderStatus.Builder().setLocationDetectionDependencyStatus( - secondary).build() : null)); + private static TimeZoneCapabilitiesAndConfig createCapabilitiesAndConfig( + boolean userCanConfigureGeoDetection, + @ProviderStatus int primaryProviderStatus, + @Nullable @DependencyStatus Integer primaryProviderLocationStatus, + @ProviderStatus int secondaryProviderStatus, + @Nullable @DependencyStatus Integer secondaryProviderLocationStatus) { + TelephonyTimeZoneAlgorithmStatus telephonyTimeZoneAlgorithmStatus = + new TelephonyTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING); - TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder( - UserHandle.SYSTEM).setConfigureAutoDetectionEnabledCapability( - Capabilities.CAPABILITY_POSSESSED).setConfigureGeoDetectionEnabledCapability( - capabilitySupported ? Capabilities.CAPABILITY_POSSESSED - : Capabilities.CAPABILITY_NOT_SUPPORTED).setSetManualTimeZoneCapability( - Capabilities.CAPABILITY_POSSESSED).build(); + LocationTimeZoneAlgorithmStatus locationTimeZoneAlgorithmStatus = + new LocationTimeZoneAlgorithmStatus(DETECTION_ALGORITHM_STATUS_RUNNING, + primaryProviderStatus, + createTimeZoneProviderStatusOrNull(primaryProviderLocationStatus), + secondaryProviderStatus, + 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, new TimeZoneConfiguration.Builder().build()); } + + private static TimeZoneProviderStatus createTimeZoneProviderStatusOrNull( + @Nullable @DependencyStatus Integer locationDependencyStatusOrNull) { + if (locationDependencyStatusOrNull == null) { + return null; + } + return new TimeZoneProviderStatus.Builder() + .setLocationDetectionDependencyStatus(locationDependencyStatusOrNull) + .build(); + } } diff --git a/tests/robotests/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceControllerTest.java index ea8336601b3..baef01c8eca 100644 --- a/tests/robotests/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/LocationTimeZoneDetectionPreferenceControllerTest.java @@ -27,9 +27,10 @@ import static android.app.time.LocationTimeZoneAlgorithmStatus.PROVIDER_STATUS_N 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.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; import android.app.time.Capabilities.CapabilityState; @@ -41,7 +42,6 @@ import android.app.time.TimeZoneCapabilitiesAndConfig; import android.app.time.TimeZoneConfiguration; import android.app.time.TimeZoneDetectorStatus; import android.content.Context; -import android.location.LocationManager; import android.os.UserHandle; import com.android.settings.R; @@ -60,8 +60,6 @@ import org.robolectric.RuntimeEnvironment; public class LocationTimeZoneDetectionPreferenceControllerTest { @Mock private TimeManager mTimeManager; - @Mock - private LocationManager mLocationManager; private Context mContext; private LocationTimeZoneDetectionPreferenceController mController; @Mock(answer = Answers.RETURNS_DEEP_STUBS) @@ -72,14 +70,16 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); when(mContext.getSystemService(TimeManager.class)).thenReturn(mTimeManager); - when(mContext.getSystemService(LocationManager.class)).thenReturn(mLocationManager); mController = new LocationTimeZoneDetectionPreferenceController(mContext); mController.setFragment(mFragment); } @Test 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. mController.setChecked(true); @@ -98,17 +98,25 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { @Test 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. mController.setChecked(true); - // Verify the TimeManager was not called. - verifyNoInteractions(mTimeManager); + // Verify the TimeManager was not updated. + verify(mTimeManager, never()).updateTimeZoneConfiguration(any()); } @Test public void setChecked_withFalse_shouldUpdateSetting() { + boolean useLocationEnabled = false; + TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = + createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED); + when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); + // Simulate the UI being clicked. mController.setChecked(false); @@ -121,8 +129,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { @Test public void testLocationTimeZoneDetection_supported_shouldBeShown() { + boolean useLocationEnabled = false; TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = - createTimeZoneCapabilitiesAndConfig(CAPABILITY_POSSESSED); + createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(mController.isAvailable()).isTrue(); @@ -130,8 +139,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { @Test public void testLocationTimeZoneDetection_unsupported_shouldNotBeShown() { - TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = - createTimeZoneCapabilitiesAndConfig(CAPABILITY_NOT_SUPPORTED); + boolean useLocationEnabled = false; + TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createTimeZoneCapabilitiesAndConfig( + useLocationEnabled, CAPABILITY_NOT_SUPPORTED); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(mController.isAvailable()).isFalse(); @@ -142,8 +152,9 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { */ @Test public void testLocationTimeZoneDetection_summary_geoDetectionEnabled() { + boolean useLocationEnabled = false; TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = - createTimeZoneCapabilitiesAndConfig(CAPABILITY_POSSESSED); + createTimeZoneCapabilitiesAndConfig(useLocationEnabled, CAPABILITY_POSSESSED); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); assertThat(mController.getSummary()).isEqualTo( @@ -152,11 +163,11 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { @Test public void testLocationTimeZoneDetection_toggleIsOn_whenGeoDetectionEnabledAnsMlsIsOff() { - TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = - createTimeZoneCapabilitiesAndConfig(CAPABILITY_NOT_APPLICABLE); + boolean useLocationEnabled = false; + TimeZoneCapabilitiesAndConfig capabilitiesAndConfig = createTimeZoneCapabilitiesAndConfig( + useLocationEnabled, CAPABILITY_NOT_APPLICABLE); when(mTimeManager.getTimeZoneCapabilitiesAndConfig()).thenReturn(capabilitiesAndConfig); - when(mLocationManager.isLocationEnabled()).thenReturn(false); assertThat(mController.isChecked()).isTrue(); assertThat(mController.getSummary()).isEqualTo( @@ -164,6 +175,7 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { } private static TimeZoneCapabilitiesAndConfig createTimeZoneCapabilitiesAndConfig( + boolean useLocationEnabled, @CapabilityState int configureGeoDetectionEnabledCapability) { // Create a status that matches the user's capability state. @@ -191,6 +203,7 @@ public class LocationTimeZoneDetectionPreferenceControllerTest { UserHandle arbitraryUserHandle = UserHandle.of(123); TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(arbitraryUserHandle) .setConfigureAutoDetectionEnabledCapability(CAPABILITY_POSSESSED) + .setUseLocationEnabled(useLocationEnabled) .setConfigureGeoDetectionEnabledCapability(configureGeoDetectionEnabledCapability) .setSetManualTimeZoneCapability(CAPABILITY_NOT_APPLICABLE) .build(); diff --git a/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java index 77ab9a2a2d3..10a5e5d670a 100644 --- a/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/datetime/TimeZonePreferenceControllerTest.java @@ -112,8 +112,10 @@ public class TimeZonePreferenceControllerTest { PROVIDER_STATUS_NOT_PRESENT, null)); int suggestManualCapability = suggestManualAllowed ? Capabilities.CAPABILITY_POSSESSED : Capabilities.CAPABILITY_NOT_SUPPORTED; + boolean useLocationEnabled = true; TimeZoneCapabilities capabilities = new TimeZoneCapabilities.Builder(UserHandle.SYSTEM) .setConfigureAutoDetectionEnabledCapability(Capabilities.CAPABILITY_POSSESSED) + .setUseLocationEnabled(useLocationEnabled) .setConfigureGeoDetectionEnabledCapability(Capabilities.CAPABILITY_NOT_SUPPORTED) .setSetManualTimeZoneCapability(suggestManualCapability) .build();