From edbe2cca130e0e00bddd25cae3fd043bf7d246c0 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Tue, 8 Dec 2020 22:48:53 -0800 Subject: [PATCH] Add a developer option to enable / disable location indicators. Also bundled location related developer options into the location category. Bug:175357420 Test: Manual && make RunSettingsRoboTests ROBOTEST_FILTER=LocationScanningPreferenceControllerTest Change-Id: Ic33ad6e70d258c9afc2cefba70ab6e7b2bafd16a --- res/values/strings.xml | 18 ++- res/xml/development_settings.xml | 29 +++-- src/com/android/settings/Utils.java | 5 + ...ocationIndicatorsPreferenceController.java | 52 ++++++++ ...ionIndicatorsPreferenceControllerTest.java | 115 ++++++++++++++++++ 5 files changed, 206 insertions(+), 13 deletions(-) create mode 100644 src/com/android/settings/location/LocationIndicatorsPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/location/LocationIndicatorsPreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 0171c7af145..75b470bc637 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -11403,6 +11403,19 @@ Autofill developer options have been reset + + + Location + + Status bar location indicator + + Show for all locations, including network and connectivity + + + Force full GNSS measurements + + Track all GNSS constellations and frequencies with no duty cycling + Device theme @@ -11470,11 +11483,6 @@ This feature has been turned off because it slows down your phone - - Force full GNSS measurements - - Track all GNSS constellations and frequencies with no duty cycling - Always show crash dialog diff --git a/res/xml/development_settings.xml b/res/xml/development_settings.xml index f06ed2eef28..f5549f999d0 100644 --- a/res/xml/development_settings.xml +++ b/res/xml/development_settings.xml @@ -175,14 +175,6 @@ android:title="@string/automatic_system_heap_dump_title" android:summary="@string/automatic_system_heap_dump_summary" /> - - - - @@ -696,4 +688,25 @@ android:targetClass="com.android.settings.development.storage.BlobInfoListView" /> + + + + + + + + + diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java index b6a9f59f354..4390aad2512 100644 --- a/src/com/android/settings/Utils.java +++ b/src/com/android/settings/Utils.java @@ -142,6 +142,11 @@ public final class Utils extends com.android.settingslib.Utils { */ public static final String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled"; + /** + * Whether to show location indicators. + */ + public static final String PROPERTY_LOCATION_INDICATORS_ENABLED = "location_indicators_enabled"; + /** * Finds a matching activity for a preference's intent. If a matching * activity is not found, it will remove the preference. diff --git a/src/com/android/settings/location/LocationIndicatorsPreferenceController.java b/src/com/android/settings/location/LocationIndicatorsPreferenceController.java new file mode 100644 index 00000000000..f7b740860c9 --- /dev/null +++ b/src/com/android/settings/location/LocationIndicatorsPreferenceController.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.location; + +import android.content.Context; +import android.content.pm.PackageManager; +import android.provider.DeviceConfig; + +import com.android.settings.Utils; +import com.android.settings.core.TogglePreferenceController; + +/** Controller for location indicators toggle. */ +public class LocationIndicatorsPreferenceController extends TogglePreferenceController { + + public LocationIndicatorsPreferenceController(Context context, String preferenceKey) { + super(context, preferenceKey); + } + + @Override + public boolean isChecked() { + return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, + Utils.PROPERTY_LOCATION_INDICATORS_ENABLED, false); + } + + @Override + public boolean setChecked(boolean isChecked) { + DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY, + Utils.PROPERTY_LOCATION_INDICATORS_ENABLED, Boolean.toString(isChecked), true); + return true; + } + + @Override + public int getAvailabilityStatus() { + // Location indicators feature is only available on devices that support location. + return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION) + ? AVAILABLE : UNSUPPORTED_ON_DEVICE; + } +} diff --git a/tests/robotests/src/com/android/settings/location/LocationIndicatorsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/LocationIndicatorsPreferenceControllerTest.java new file mode 100644 index 00000000000..e1182fdba84 --- /dev/null +++ b/tests/robotests/src/com/android/settings/location/LocationIndicatorsPreferenceControllerTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.location; + + +import static com.android.settings.core.BasePreferenceController.AVAILABLE; +import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.pm.PackageManager; +import android.provider.DeviceConfig; + +import androidx.test.core.app.ApplicationProvider; + +import com.android.settings.Utils; +import com.android.settings.testutils.shadow.ShadowDeviceConfig; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +/** + * Unit tests for {@link LocationIndicatorsPreferenceController}. + */ +@RunWith(RobolectricTestRunner.class) +@Config(shadows = {ShadowDeviceConfig.class}) +public class LocationIndicatorsPreferenceControllerTest { + @Mock + PackageManager mPackageManager; + private Context mContext; + private LocationIndicatorsPreferenceController mController; + + @Before + public void setUp() { + mContext = spy(ApplicationProvider.getApplicationContext()); + mController = new LocationIndicatorsPreferenceController(mContext, "key"); + + MockitoAnnotations.initMocks(this); + when(mContext.getPackageManager()).thenReturn(mPackageManager); + } + + @After + public void tearDown() { + ShadowDeviceConfig.reset(); + } + + /** + * Verify the location indicator settings are visible when location feature is supported + * on the device. + */ + @Test + public void getAvailabilityStatus_locationSupported_shouldReturnAVAILABLE() { + when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_LOCATION))).thenReturn( + true); + assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); + } + + /** + * Verify the location indicator settings are not visible when location feature is not supported + * on the device. + */ + @Test + public void getAvailabilityStatus_locationNotSupported_shouldReturnUNSUPPORTED() { + when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_LOCATION))).thenReturn( + false); + assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); + } + + /** + * Verify the location indicator preference is checked when the feature is enabled. + */ + @Test + public void isChecked_featureEnabled_shouldReturnTrue() { + final boolean enabled = true; + DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY, + Utils.PROPERTY_LOCATION_INDICATORS_ENABLED, Boolean.toString(enabled), true); + assertThat(mController.isChecked()).isTrue(); + } + + /** + * Verify the location indicator preference is unchecked when the feature is not enabled. + */ + @Test + public void isChecked_featureNotEnabled_shouldReturnFalse() { + final boolean enabled = false; + DeviceConfig.setProperty(DeviceConfig.NAMESPACE_PRIVACY, + Utils.PROPERTY_LOCATION_INDICATORS_ENABLED, Boolean.toString(enabled), true); + assertThat(mController.isChecked()).isFalse(); + } +}