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
This commit is contained in:
Michael Groover
2023-05-17 14:31:10 -05:00
parent dbd9841a2f
commit f8b0cdd6ff
2 changed files with 109 additions and 2 deletions

View File

@@ -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,

View File

@@ -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();
}
}