Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cc17b636 authored by Nate Myren's avatar Nate Myren Committed by Android (Google) Code Review
Browse files

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

parents ff9c6d96 e1825fcd
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -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