From f8b0cdd6ff6795a492afa31674bc1922d609245f Mon Sep 17 00:00:00 2001 From: Michael Groover Date: Wed, 17 May 2023 14:31:10 -0500 Subject: [PATCH] Do not allow Sensors Off to be toggled from the lock screen When the Sensors Off quick settings tile was originally added, the primary concern was the tile accidentally being toggled off from the lock screen. However, if Sensors Off can be toggled on from the lock screen, this could be enabled on a stolen device to prevent determining the location of the device. This commit updates the Sensors Off tile to prevent its state from being modified at all from the lock screen. Bug: 282071050 Test: make RunSettingsRoboTests ROBOTEST_FILTER=SensorsOffTest Test: Manually verified tile couldn't be toggled when device locked Change-Id: Ib2ea1d92c0b215ebeaf33fb8e4f5e7e297133b82 --- .../development/qstile/DevelopmentTiles.java | 4 +- .../development/qstile/SensorsOffTest.java | 107 ++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/development/qstile/SensorsOffTest.java diff --git a/src/com/android/settings/development/qstile/DevelopmentTiles.java b/src/com/android/settings/development/qstile/DevelopmentTiles.java index 3b6c646cb40..71a50b91090 100644 --- a/src/com/android/settings/development/qstile/DevelopmentTiles.java +++ b/src/com/android/settings/development/qstile/DevelopmentTiles.java @@ -401,8 +401,8 @@ public abstract class DevelopmentTiles extends TileService { @Override public void setIsEnabled(boolean isEnabled) { - // Don't allow sensors to be reenabled from the lock screen. - if (mIsEnabled && mKeyguardManager.isKeyguardLocked()) { + // Don't allow sensors to be toggled from the lock screen. + if (mKeyguardManager.isKeyguardLocked()) { return; } mMetricsFeatureProvider.action(getApplicationContext(), SettingsEnums.QS_SENSOR_PRIVACY, diff --git a/tests/robotests/src/com/android/settings/development/qstile/SensorsOffTest.java b/tests/robotests/src/com/android/settings/development/qstile/SensorsOffTest.java new file mode 100644 index 00000000000..d4f31a38a0e --- /dev/null +++ b/tests/robotests/src/com/android/settings/development/qstile/SensorsOffTest.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2023 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.development.qstile; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import android.app.KeyguardManager; +import android.content.Context; +import android.hardware.SensorPrivacyManager; + +import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; + +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.RuntimeEnvironment; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(RobolectricTestRunner.class) +public class SensorsOffTest { + @Mock + private KeyguardManager mKeyguardManager; + @Mock + private MetricsFeatureProvider mMetricsFeatureProvider; + @Mock + private SensorPrivacyManager mSensorPrivacyManager; + + private Context mContext; + private DevelopmentTiles.SensorsOff mSensorsOff; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; + mSensorsOff = new DevelopmentTiles.SensorsOff(); + ReflectionHelpers.setField(mSensorsOff, "mBase", mContext); + ReflectionHelpers.setField(mSensorsOff, "mKeyguardManager", mKeyguardManager); + ReflectionHelpers.setField(mSensorsOff, "mMetricsFeatureProvider", mMetricsFeatureProvider); + ReflectionHelpers.setField(mSensorsOff, "mSensorPrivacyManager", mSensorPrivacyManager); + } + + @Test + public void setIsEnabled_trueWithKeyguardLocked_sensorPrivacyNotModified() { + ReflectionHelpers.setField(mSensorsOff, "mIsEnabled", false); + doReturn(true).when(mKeyguardManager).isKeyguardLocked(); + + mSensorsOff.setIsEnabled(true); + + verify(mSensorPrivacyManager, never()).setAllSensorPrivacy(true); + assertThat(mSensorsOff.isEnabled()).isFalse(); + } + + @Test + public void setIsEnabled_trueWithKeyguardUnlocked_sensorPrivacyModified() { + ReflectionHelpers.setField(mSensorsOff, "mIsEnabled", false); + doReturn(false).when(mKeyguardManager).isKeyguardLocked(); + + mSensorsOff.setIsEnabled(true); + + verify(mSensorPrivacyManager, times(1)).setAllSensorPrivacy(true); + assertThat(mSensorsOff.isEnabled()).isTrue(); + } + + @Test + public void setIsEnabled_falseWithKeyguardLocked_sensorPrivacyNotModified() { + ReflectionHelpers.setField(mSensorsOff, "mIsEnabled", true); + doReturn(true).when(mKeyguardManager).isKeyguardLocked(); + + mSensorsOff.setIsEnabled(false); + + verify(mSensorPrivacyManager, never()).setAllSensorPrivacy(false); + assertThat(mSensorsOff.isEnabled()).isTrue(); + } + + @Test + public void setIsEnabled_falseWithKeyguardUnlocked_sensorPrivacyModified() { + ReflectionHelpers.setField(mSensorsOff, "mIsEnabled", true); + doReturn(false).when(mKeyguardManager).isKeyguardLocked(); + + mSensorsOff.setIsEnabled(false); + + verify(mSensorPrivacyManager, times(1)).setAllSensorPrivacy(false); + assertThat(mSensorsOff.isEnabled()).isFalse(); + } +}