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

Commit 4c197856 authored by Evan Severson's avatar Evan Severson
Browse files

Upadate sensor service to use new sensor privacy api

The new calls no longer require user ids since that is managed
internally in the sensor privacy service and calling services will
receive updates when the state is changed or user is changed who has a
different setting. Calling services should treat the state as a global
setting.

The new calls also support hardware and software types of controls. This
change treats both the same.

Test: atest CtsSensorRatePermissionTestCases
Bug: 191745272
Change-Id: Ifa905110b0b75fbc48a37b7e49e269621f03129d
parent 6f3e1c00
Loading
Loading
Loading
Loading
+47 −61
Original line number Diff line number Diff line
@@ -134,6 +134,7 @@ SensorService::SensorService()
      mWakeLockAcquired(false), mLastReportedProxIsActive(false) {
    mUidPolicy = new UidPolicy(this);
    mSensorPrivacyPolicy = new SensorPrivacyPolicy(this);
    mMicSensorPrivacyPolicy = new MicrophonePrivacyPolicy(this);
}

bool SensorService::initializeHmacKey() {
@@ -369,6 +370,9 @@ void SensorService::onFirstRef() {

            // Start watching sensor privacy changes
            mSensorPrivacyPolicy->registerSelf();

            // Start watching mic sensor privacy changes
            mMicSensorPrivacyPolicy->registerSelf();
        }
    }
}
@@ -439,9 +443,7 @@ SensorService::~SensorService() {
    }
    mUidPolicy->unregisterSelf();
    mSensorPrivacyPolicy->unregisterSelf();
    for (auto const& [userId, policy] : mMicSensorPrivacyPolicies) {
        policy->unregisterSelf();
    }
    mMicSensorPrivacyPolicy->unregisterSelf();
}

status_t SensorService::dump(int fd, const Vector<String16>& args) {
@@ -773,35 +775,27 @@ void SensorService::enableAllSensorsLocked(ConnectionSafeAutolock* connLock) {
    checkAndReportProxStateChangeLocked();
}

void SensorService::capRates(userid_t userId) {
void SensorService::capRates() {
    ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
    for (const sp<SensorDirectConnection>& conn : connLock.getDirectConnections()) {
        if (conn->getUserId() == userId) {
        conn->onMicSensorAccessChanged(true);
    }
    }

    for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) {
        if (conn->getUserId() == userId) {
        conn->onMicSensorAccessChanged(true);
    }
}
}

void SensorService::uncapRates(userid_t userId) {
void SensorService::uncapRates() {
    ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
    for (const sp<SensorDirectConnection>& conn : connLock.getDirectConnections()) {
        if (conn->getUserId() == userId) {
        conn->onMicSensorAccessChanged(false);
    }
    }

    for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) {
        if (conn->getUserId() == userId) {
        conn->onMicSensorAccessChanged(false);
    }
}
}

// NOTE: This is a remote API - make sure all args are validated
status_t SensorService::shellCommand(int in, int out, int err, Vector<String16>& args) {
@@ -2243,7 +2237,6 @@ bool SensorService::isSensorInCappedSet(int sensorType) {

status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* requestedPeriodNs,
        const String16& opPackageName) {
    uid_t uid = IPCThreadState::self()->getCallingUid();
    if (*requestedPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) {
        return OK;
    }
@@ -2255,7 +2248,7 @@ status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* req
        }
        return OK;
    }
    if (isMicSensorPrivacyEnabledForUid(uid)) {
    if (mMicSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
        *requestedPeriodNs = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS;
        return OK;
    }
@@ -2264,7 +2257,6 @@ status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* req

status_t SensorService::adjustRateLevelBasedOnMicAndPermission(int* requestedRateLevel,
        const String16& opPackageName) {
    uid_t uid = IPCThreadState::self()->getCallingUid();
    if (*requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
        return OK;
    }
@@ -2276,7 +2268,7 @@ status_t SensorService::adjustRateLevelBasedOnMicAndPermission(int* requestedRat
        }
        return OK;
    }
    if (isMicSensorPrivacyEnabledForUid(uid)) {
    if (mMicSensorPrivacyPolicy->isSensorPrivacyEnabled()) {
        *requestedRateLevel = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
        return OK;
    }
@@ -2293,69 +2285,63 @@ void SensorService::SensorPrivacyPolicy::registerSelf() {
void SensorService::SensorPrivacyPolicy::unregisterSelf() {
    AutoCallerClear acc;
    SensorPrivacyManager spm;
    if (mIsIndividualMic) {
        spm.removeIndividualSensorPrivacyListener(
                SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
    } else {
    spm.removeSensorPrivacyListener(this);
}
}

bool SensorService::SensorPrivacyPolicy::isSensorPrivacyEnabled() {
    return mSensorPrivacyEnabled;
}

binder::Status SensorService::SensorPrivacyPolicy::onSensorPrivacyChanged(bool enabled) {
binder::Status SensorService::SensorPrivacyPolicy::onSensorPrivacyChanged(int toggleType __unused,
        int sensor __unused, bool enabled) {
    mSensorPrivacyEnabled = enabled;
    sp<SensorService> service = mService.promote();

    if (service != nullptr) {
        if (mIsIndividualMic) {
            if (enabled) {
                service->capRates(mUserId);
            } else {
                service->uncapRates(mUserId);
            }
        } else {
        if (enabled) {
            service->disableAllSensors();
        } else {
            service->enableAllSensors();
        }
    }
    }
    return binder::Status::ok();
}

status_t SensorService::SensorPrivacyPolicy::registerSelfForIndividual(int userId) {
    Mutex::Autolock _l(mSensorPrivacyLock);
void SensorService::MicrophonePrivacyPolicy::registerSelf() {
    AutoCallerClear acc;
    SensorPrivacyManager spm;
    status_t err = spm.addIndividualSensorPrivacyListener(userId,
            SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE, this);
    mSensorPrivacyEnabled =
            spm.isToggleSensorPrivacyEnabled(
                    SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE,
            SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE)
                    || spm.isToggleSensorPrivacyEnabled(
                            SensorPrivacyManager::TOGGLE_TYPE_HARDWARE,
                            SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE);
    spm.addToggleSensorPrivacyListener(this);
}

    if (err != OK) {
        ALOGE("Cannot register a mic listener.");
        return err;
void SensorService::MicrophonePrivacyPolicy::unregisterSelf() {
    AutoCallerClear acc;
    SensorPrivacyManager spm;
    spm.removeToggleSensorPrivacyListener(this);
}
    mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId,
                SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE);

    mIsIndividualMic = true;
    mUserId = userId;
    return OK;
binder::Status SensorService::MicrophonePrivacyPolicy::onSensorPrivacyChanged(int toggleType __unused,
        int sensor, bool enabled) {
    if (sensor != SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE) {
        return binder::Status::ok();
    }
    mSensorPrivacyEnabled = enabled;
    sp<SensorService> service = mService.promote();

bool SensorService::isMicSensorPrivacyEnabledForUid(uid_t uid) {
    userid_t userId = multiuser_get_user_id(uid);
    if (mMicSensorPrivacyPolicies.find(userId) == mMicSensorPrivacyPolicies.end()) {
        sp<SensorPrivacyPolicy> userPolicy = new SensorPrivacyPolicy(this);
        if (userPolicy->registerSelfForIndividual(userId) != OK) {
            return false;
    if (service != nullptr) {
        if (enabled) {
            service->capRates();
        } else {
            service->uncapRates();
        }
        mMicSensorPrivacyPolicies[userId] = userPolicy;
    }
    return mMicSensorPrivacyPolicies[userId]->isSensorPrivacyEnabled();
    return binder::Status::ok();
}

SensorService::ConnectionSafeAutolock::ConnectionSafeAutolock(
+21 −14
Original line number Diff line number Diff line
@@ -289,22 +289,32 @@ private:
    class SensorPrivacyPolicy : public hardware::BnSensorPrivacyListener {
        public:
            explicit SensorPrivacyPolicy(wp<SensorService> service)
                    : mService(service), mIsIndividualMic(false), mUserId(0) {}
                    : mService(service) {}
            void registerSelf();
            void unregisterSelf();

            status_t registerSelfForIndividual(int userId);

            bool isSensorPrivacyEnabled();

            binder::Status onSensorPrivacyChanged(bool enabled);
            binder::Status onSensorPrivacyChanged(int toggleType, int sensor,
                                                  bool enabled);

        private:
        protected:
            std::atomic_bool mSensorPrivacyEnabled;
            wp<SensorService> mService;

        private:
            Mutex mSensorPrivacyLock;
            std::atomic_bool mSensorPrivacyEnabled;
            bool mIsIndividualMic;
            userid_t mUserId;
    };

    class MicrophonePrivacyPolicy : public SensorPrivacyPolicy {
        public:
            explicit MicrophonePrivacyPolicy(wp<SensorService> service)
                    : SensorPrivacyPolicy(service) {}
            void registerSelf();
            void unregisterSelf();

            binder::Status onSensorPrivacyChanged(int toggleType, int sensor,
                                                  bool enabled);
    };

    // A class automatically clearing and restoring binder caller identity inside
@@ -444,9 +454,9 @@ private:
    void enableAllSensorsLocked(ConnectionSafeAutolock* connLock);

    // Caps active direct connections (when the mic toggle is flipped to on)
    void capRates(userid_t userId);
    void capRates();
    // Removes the capped rate on active direct connections (when the mic toggle is flipped to off)
    void uncapRates(userid_t userId);
    void uncapRates();

    static inline bool isAudioServerOrSystemServerUid(uid_t uid) {
        return multiuser_get_app_id(uid) == AID_SYSTEM || uid == AID_AUDIOSERVER;
@@ -497,10 +507,7 @@ private:
    static Mutex sPackageTargetVersionLock;
    static String16 sSensorInterfaceDescriptorPrefix;

    // Map from user to SensorPrivacyPolicy
    std::map<userid_t, sp<SensorPrivacyPolicy>> mMicSensorPrivacyPolicies;
    // Checks if the mic sensor privacy is enabled for the uid
    bool isMicSensorPrivacyEnabledForUid(uid_t uid);
    sp<MicrophonePrivacyPolicy> mMicSensorPrivacyPolicy;

    // Keeps track of the handles of all proximity sensors in the system.
    std::vector<int32_t> mProxSensorHandles;