Merge "Correct SensorPrivacyState if needed, prevent state from going bad" into udc-dev

This commit is contained in:
Nate Myren
2023-05-11 18:36:09 +00:00
committed by Android (Google) Code Review

View File

@@ -274,13 +274,14 @@ public final class SensorPrivacyService extends SystemService {
mHandler = new SensorPrivacyHandler(FgThread.get().getLooper(), mContext); mHandler = new SensorPrivacyHandler(FgThread.get().getLooper(), mContext);
mSensorPrivacyStateController = SensorPrivacyStateController.getInstance(); mSensorPrivacyStateController = SensorPrivacyStateController.getInstance();
correctStateIfNeeded();
int[] micAndCameraOps = new int[]{OP_RECORD_AUDIO, OP_PHONE_CALL_MICROPHONE, int[] micAndCameraOps = new int[]{OP_RECORD_AUDIO, OP_PHONE_CALL_MICROPHONE,
OP_CAMERA, OP_PHONE_CALL_CAMERA, OP_RECEIVE_EXPLICIT_USER_INTERACTION_AUDIO}; OP_CAMERA, OP_PHONE_CALL_CAMERA, OP_RECEIVE_EXPLICIT_USER_INTERACTION_AUDIO};
mAppOpsManager.startWatchingNoted(micAndCameraOps, this); mAppOpsManager.startWatchingNoted(micAndCameraOps, this);
mAppOpsManager.startWatchingStarted(micAndCameraOps, this); mAppOpsManager.startWatchingStarted(micAndCameraOps, this);
mContext.registerReceiver(new BroadcastReceiver() { mContext.registerReceiver(new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@@ -313,6 +314,20 @@ public final class SensorPrivacyService extends SystemService {
userId, toggleType, sensor, state.isEnabled())); 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 @Override
public void onUserRestrictionsChanged(int userId, Bundle newRestrictions, public void onUserRestrictionsChanged(int userId, Bundle newRestrictions,
Bundle prevRestrictions) { Bundle prevRestrictions) {
@@ -721,15 +736,30 @@ public final class SensorPrivacyService extends SystemService {
if (userId == UserHandle.USER_CURRENT) { if (userId == UserHandle.USER_CURRENT) {
userId = mCurrentUser; userId = mCurrentUser;
} }
if (!canChangeToggleSensorPrivacy(userId, sensor)) { if (!canChangeToggleSensorPrivacy(userId, sensor)) {
return; 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); setToggleSensorPrivacyUnchecked(TOGGLE_TYPE_SOFTWARE, userId, source, sensor, enable);
} }
private void setToggleSensorPrivacyUnchecked(int toggleType, int userId, int source, private void setToggleSensorPrivacyUnchecked(int toggleType, int userId, int source,
int sensor, boolean enable) { 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]; final long[] lastChange = new long[1];
mSensorPrivacyStateController.atomic(() -> { mSensorPrivacyStateController.atomic(() -> {
SensorState sensorState = mSensorPrivacyStateController SensorState sensorState = mSensorPrivacyStateController