From ff465382ce97ccbf127cd162f6f026a058df3475 Mon Sep 17 00:00:00 2001 From: Kate Montgomery Date: Sat, 22 Jan 2022 01:08:00 +0000 Subject: [PATCH] Use user setting to determine whether or not to show system location accesses. Bug: 191503437 Test: atest LocationControllerImplTest Test: manual Change-Id: I0a8ca87db98f30c64f143608f1f5ecd55d072393 --- .../policy/LocationControllerImpl.java | 36 +++++++++++++++---- .../policy/LocationControllerImplTest.java | 22 ++++++++++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java index 2a98694109a52..5e91a2571bc35 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/LocationControllerImpl.java @@ -30,6 +30,7 @@ import android.content.IntentFilter; import android.content.PermissionChecker; import android.content.pm.PackageManager; import android.content.pm.UserInfo; +import android.database.ContentObserver; import android.location.LocationManager; import android.os.Handler; import android.os.Looper; @@ -55,6 +56,7 @@ import com.android.systemui.dagger.qualifiers.Main; import com.android.systemui.settings.UserTracker; import com.android.systemui.util.DeviceConfigProxy; import com.android.systemui.util.Utils; +import com.android.systemui.util.settings.SecureSettings; import java.util.ArrayList; import java.util.List; @@ -77,17 +79,21 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio private final H mHandler; private final Handler mBackgroundHandler; private final PackageManager mPackageManager; + private final ContentObserver mContentObserver; + private final SecureSettings mSecureSettings; private boolean mAreActiveLocationRequests; private boolean mShouldDisplayAllAccesses; - private boolean mShowSystemAccesses; + private boolean mShowSystemAccessesFlag; + private boolean mShowSystemAccessesSetting; @Inject public LocationControllerImpl(Context context, AppOpsController appOpsController, DeviceConfigProxy deviceConfigProxy, @Main Looper mainLooper, @Background Handler backgroundHandler, BroadcastDispatcher broadcastDispatcher, BootCompleteCache bootCompleteCache, - UserTracker userTracker, PackageManager packageManager, UiEventLogger uiEventLogger) { + UserTracker userTracker, PackageManager packageManager, UiEventLogger uiEventLogger, + SecureSettings secureSettings) { mContext = context; mAppOpsController = appOpsController; mDeviceConfigProxy = deviceConfigProxy; @@ -95,10 +101,22 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio mHandler = new H(mainLooper); mUserTracker = userTracker; mUiEventLogger = uiEventLogger; + mSecureSettings = secureSettings; mBackgroundHandler = backgroundHandler; mPackageManager = packageManager; mShouldDisplayAllAccesses = getAllAccessesSetting(); - mShowSystemAccesses = getShowSystemSetting(); + mShowSystemAccessesFlag = getShowSystemFlag(); + mShowSystemAccessesSetting = getShowSystemSetting(); + mContentObserver = new ContentObserver(mBackgroundHandler) { + @Override + public void onChange(boolean selfChange) { + mShowSystemAccessesSetting = getShowSystemSetting(); + } + }; + + // Register to listen for changes in Settings.Secure settings. + mSecureSettings.registerContentObserver( + Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, mContentObserver); // Register to listen for changes in DeviceConfig settings. mDeviceConfigProxy.addOnPropertiesChangedListener( @@ -106,7 +124,7 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio backgroundHandler::post, properties -> { mShouldDisplayAllAccesses = getAllAccessesSetting(); - mShowSystemAccesses = getShowSystemSetting(); + mShowSystemAccessesFlag = getShowSystemSetting(); updateActiveLocationRequests(); }); @@ -195,10 +213,15 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SMALL_ENABLED, false); } - private boolean getShowSystemSetting() { + private boolean getShowSystemFlag() { return mDeviceConfigProxy.getBoolean(DeviceConfig.NAMESPACE_PRIVACY, SystemUiDeviceConfigFlags.PROPERTY_LOCATION_INDICATORS_SHOW_SYSTEM, false); } + + private boolean getShowSystemSetting() { + return mSecureSettings.getInt(Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0) == 1; + } + /** * Returns true if there currently exist active high power location requests. */ @@ -226,6 +249,7 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio } boolean hadActiveLocationRequests = mAreActiveLocationRequests; boolean shouldDisplay = false; + boolean showSystem = mShowSystemAccessesFlag || mShowSystemAccessesSetting; boolean systemAppOp = false; boolean nonSystemAppOp = false; boolean isSystemApp; @@ -243,7 +267,7 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio nonSystemAppOp = true; } - shouldDisplay = mShowSystemAccesses || shouldDisplay || !isSystemApp; + shouldDisplay = showSystem || shouldDisplay || !isSystemApp; } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/LocationControllerImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/LocationControllerImplTest.java index a6e942673187a..f5554c585c233 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/LocationControllerImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/LocationControllerImplTest.java @@ -29,6 +29,7 @@ import android.location.LocationManager; import android.os.Handler; import android.os.UserHandle; import android.provider.DeviceConfig; +import android.provider.Settings; import android.testing.AndroidTestingRunner; import android.testing.TestableLooper; import android.testing.TestableLooper.RunWithLooper; @@ -46,6 +47,7 @@ import com.android.systemui.settings.UserTracker; import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback; import com.android.systemui.util.DeviceConfigProxy; import com.android.systemui.util.DeviceConfigProxyFake; +import com.android.systemui.util.settings.SecureSettings; import com.google.common.collect.ImmutableList; @@ -67,6 +69,7 @@ public class LocationControllerImplTest extends SysuiTestCase { @Mock private AppOpsController mAppOpsController; @Mock private UserTracker mUserTracker; + @Mock private SecureSettings mSecureSettings; @Before public void setup() { @@ -88,7 +91,8 @@ public class LocationControllerImplTest extends SysuiTestCase { mock(BootCompleteCache.class), mUserTracker, mContext.getPackageManager(), - mUiEventLogger); + mUiEventLogger, + mSecureSettings); mTestableLooper.processAllMessages(); } @@ -204,7 +208,7 @@ public class LocationControllerImplTest extends SysuiTestCase { } @Test - public void testCallbackNotified_additionalOps_shouldShowSystem() { + public void testCallbackNotified_additionalOps_shouldNotShowSystem() { LocationChangeCallback callback = mock(LocationChangeCallback.class); mLocationController.addCallback(callback); mDeviceConfigProxy.setProperty( @@ -229,7 +233,9 @@ public class LocationControllerImplTest extends SysuiTestCase { @Test - public void testCallbackNotified_additionalOps_shouldNotShowSystem() { + public void testCallbackNotified_additionalOps_shouldShowSystem() { + when(mSecureSettings.getInt(Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0)) + .thenReturn(1); LocationChangeCallback callback = mock(LocationChangeCallback.class); mLocationController.addCallback(callback); mDeviceConfigProxy.setProperty( @@ -261,6 +267,16 @@ public class LocationControllerImplTest extends SysuiTestCase { mTestableLooper.processAllMessages(); verify(callback, times(1)).onLocationActiveChanged(false); + + when(mSecureSettings.getInt(Settings.Secure.LOCATION_SHOW_SYSTEM_OPS, 0)) + .thenReturn(0); + mLocationController.onActiveStateChanged(AppOpsManager.OP_FINE_LOCATION, 0, + "com.google.android.gms", true); + + mTestableLooper.processAllMessages(); + + // onLocationActive(true) was not called again because the setting is disabled. + verify(callback, times(1)).onLocationActiveChanged(true); } @Test