Correct SensorPrivacyState if needed, prevent state from going bad

If a device doesn't support sensor privacy, it should not be allowed to
enable it. If it has been enabled, disable it.

Bug: 280005257
Fixes: 280878138
Test: atestSensorPrivacyCameraTest
Change-Id: Ic68f61624ae427682f3636ed1717f492d6f484ab
This commit is contained in:
Nate Myren
2023-04-28 12:05:00 -07:00
parent 8c01119c26
commit e1825fcda3

View File

@@ -274,13 +274,14 @@ public final class SensorPrivacyService extends SystemService {
mHandler = new SensorPrivacyHandler(FgThread.get().getLooper(), mContext);
mSensorPrivacyStateController = SensorPrivacyStateController.getInstance();
correctStateIfNeeded();
int[] micAndCameraOps = new int[]{OP_RECORD_AUDIO, OP_PHONE_CALL_MICROPHONE,
OP_CAMERA, OP_PHONE_CALL_CAMERA, OP_RECEIVE_EXPLICIT_USER_INTERACTION_AUDIO};
mAppOpsManager.startWatchingNoted(micAndCameraOps, this);
mAppOpsManager.startWatchingStarted(micAndCameraOps, this);
mContext.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -313,6 +314,20 @@ public final class SensorPrivacyService extends SystemService {
userId, toggleType, sensor, state.isEnabled()));
}
// If sensor privacy is enabled for a sensor, but the device doesn't support sensor privacy
// for that sensor, then disable privacy
private void correctStateIfNeeded() {
mSensorPrivacyStateController.forEachState((type, user, sensor, state) -> {
if (type != TOGGLE_TYPE_SOFTWARE) {
return;
}
if (!supportsSensorToggle(TOGGLE_TYPE_SOFTWARE, sensor) && state.isEnabled()) {
setToggleSensorPrivacyUnchecked(
TOGGLE_TYPE_SOFTWARE, user, OTHER, sensor, false);
}
});
}
@Override
public void onUserRestrictionsChanged(int userId, Bundle newRestrictions,
Bundle prevRestrictions) {
@@ -721,15 +736,30 @@ public final class SensorPrivacyService extends SystemService {
if (userId == UserHandle.USER_CURRENT) {
userId = mCurrentUser;
}
if (!canChangeToggleSensorPrivacy(userId, sensor)) {
return;
}
if (enable && !supportsSensorToggle(TOGGLE_TYPE_SOFTWARE, sensor)) {
// Do not enable sensor privacy if the device doesn't support it
return;
}
setToggleSensorPrivacyUnchecked(TOGGLE_TYPE_SOFTWARE, userId, source, sensor, enable);
}
private void setToggleSensorPrivacyUnchecked(int toggleType, int userId, int source,
int sensor, boolean enable) {
if (DEBUG) {
Log.d(TAG, "callingUid=" + Binder.getCallingUid()
+ " callingPid=" + Binder.getCallingPid()
+ " setToggleSensorPrivacyUnchecked("
+ "userId=" + userId
+ " source=" + source
+ " sensor=" + sensor
+ " enable=" + enable
+ ")");
}
final long[] lastChange = new long[1];
mSensorPrivacyStateController.atomic(() -> {
SensorState sensorState = mSensorPrivacyStateController