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

Commit 17b449fc authored by Evan Severson's avatar Evan Severson Committed by Android (Google) Code Review
Browse files

Merge changes from topic "hardware_sensor_privacy_apis"

* changes:
  Upadate sensor service to use new sensor privacy api
  Sync native sensor privacy aidl with frameworks/base
parents 6f70c481 4c197856
Loading
Loading
Loading
Loading
+22 −10
Original line number Diff line number Diff line
@@ -55,12 +55,12 @@ sp<hardware::ISensorPrivacyManager> SensorPrivacyManager::getService()
    return service;
}

bool SensorPrivacyManager::supportsSensorToggle(int sensor) {
bool SensorPrivacyManager::supportsSensorToggle(int toggleType, int sensor) {
    if (mSupportedCache.find(sensor) == mSupportedCache.end()) {
        sp<hardware::ISensorPrivacyManager> service = getService();
        if (service != nullptr) {
            bool result;
            service->supportsSensorToggle(sensor, &result);
            service->supportsSensorToggle(toggleType, sensor, &result);
            mSupportedCache[sensor] = result;
            return result;
        }
@@ -80,12 +80,12 @@ void SensorPrivacyManager::addSensorPrivacyListener(
    }
}

status_t SensorPrivacyManager::addIndividualSensorPrivacyListener(int userId, int sensor,
status_t SensorPrivacyManager::addToggleSensorPrivacyListener(
        const sp<hardware::ISensorPrivacyListener>& listener)
{
    sp<hardware::ISensorPrivacyManager> service = getService();
    if (service != nullptr) {
        return service->addIndividualSensorPrivacyListener(userId, sensor, listener)
        return service->addToggleSensorPrivacyListener(listener)
                .transactionError();
    }
    return UNEXPECTED_NULL;
@@ -100,12 +100,12 @@ void SensorPrivacyManager::removeSensorPrivacyListener(
    }
}

void SensorPrivacyManager::removeIndividualSensorPrivacyListener(int sensor,
void SensorPrivacyManager::removeToggleSensorPrivacyListener(
        const sp<hardware::ISensorPrivacyListener>& listener)
{
    sp<hardware::ISensorPrivacyManager> service = getService();
    if (service != nullptr) {
        service->removeIndividualSensorPrivacyListener(sensor, listener);
        service->removeToggleSensorPrivacyListener(listener);
    }
}

@@ -121,24 +121,36 @@ bool SensorPrivacyManager::isSensorPrivacyEnabled()
    return false;
}

bool SensorPrivacyManager::isIndividualSensorPrivacyEnabled(int userId, int sensor)
bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int sensor)
{
	sp<hardware::ISensorPrivacyManager> service = getService();
    if (service != nullptr) {
        bool result;
        service->isIndividualSensorPrivacyEnabled(userId, sensor, &result);
        service->isCombinedToggleSensorPrivacyEnabled(sensor, &result);
        return result;
    }
    // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
    return false;
}

status_t SensorPrivacyManager::isIndividualSensorPrivacyEnabled(int userId, int sensor,
bool SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor)
{
    sp<hardware::ISensorPrivacyManager> service = getService();
    if (service != nullptr) {
        bool result;
        service->isToggleSensorPrivacyEnabled(toggleType, sensor, &result);
        return result;
    }
    // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
    return false;
}

status_t SensorPrivacyManager::isToggleSensorPrivacyEnabled(int toggleType, int sensor,
        bool &returnVal)
{
    sp<hardware::ISensorPrivacyManager> service = getService();
    if (service != nullptr) {
        binder::Status res = service->isIndividualSensorPrivacyEnabled(userId, sensor, &returnVal);
        binder::Status res = service->isToggleSensorPrivacyEnabled(toggleType, sensor, &returnVal);
        return res.transactionError();
    }
    // if the SensorPrivacyManager is not available then assume sensor privacy is disabled
+1 −1
Original line number Diff line number Diff line
@@ -20,5 +20,5 @@ package android.hardware;
 * @hide
 */
oneway interface ISensorPrivacyListener {
    void onSensorPrivacyChanged(boolean enabled);
    void onSensorPrivacyChanged(int toggleType, int sensor, boolean enabled);
}
+8 −6
Original line number Diff line number Diff line
@@ -20,23 +20,25 @@ import android.hardware.ISensorPrivacyListener;

/** @hide */
interface ISensorPrivacyManager {
    boolean supportsSensorToggle(int sensor);
    boolean supportsSensorToggle(int toggleType, int sensor);

    void addSensorPrivacyListener(in ISensorPrivacyListener listener);

    void addIndividualSensorPrivacyListener(int userId, int sensor, in ISensorPrivacyListener listener);
    void addToggleSensorPrivacyListener(in ISensorPrivacyListener listener);

    void removeSensorPrivacyListener(in ISensorPrivacyListener listener);

    void removeIndividualSensorPrivacyListener(int sensor, in ISensorPrivacyListener listener);
    void removeToggleSensorPrivacyListener(in ISensorPrivacyListener listener);

    boolean isSensorPrivacyEnabled();

    boolean isIndividualSensorPrivacyEnabled(int userId, int sensor);
    boolean isCombinedToggleSensorPrivacyEnabled(int sensor);

    boolean isToggleSensorPrivacyEnabled(int toggleType, int sensor);

    void setSensorPrivacy(boolean enable);

    void setIndividualSensorPrivacy(int userId, int source, int sensor, boolean enable);
    void setToggleSensorPrivacy(int userId, int source, int sensor, boolean enable);

    void setIndividualSensorPrivacyForProfileGroup(int userId, int source, int sensor, boolean enable);
    void setToggleSensorPrivacyForProfileGroup(int userId, int source, int sensor, boolean enable);
}
+13 −9
Original line number Diff line number Diff line
@@ -31,22 +31,26 @@ class SensorPrivacyManager
{
public:
    enum {
        INDIVIDUAL_SENSOR_MICROPHONE = 1,
        INDIVIDUAL_SENSOR_CAMERA = 2
        TOGGLE_SENSOR_MICROPHONE = 1,
        TOGGLE_SENSOR_CAMERA = 2
    };

    enum {
        TOGGLE_TYPE_SOFTWARE = 1,
        TOGGLE_TYPE_HARDWARE = 2
    };

    SensorPrivacyManager();

    bool supportsSensorToggle(int sensor);
    bool supportsSensorToggle(int toggleType, int sensor);
    void addSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener>& listener);
    status_t addIndividualSensorPrivacyListener(int userId, int sensor,
            const sp<hardware::ISensorPrivacyListener>& listener);
    status_t addToggleSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener>& listener);
    void removeSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener>& listener);
    void removeIndividualSensorPrivacyListener(int sensor,
            const sp<hardware::ISensorPrivacyListener>& listener);
    void removeToggleSensorPrivacyListener(const sp<hardware::ISensorPrivacyListener>& listener);
    bool isSensorPrivacyEnabled();
    bool isIndividualSensorPrivacyEnabled(int userId, int sensor);
    status_t isIndividualSensorPrivacyEnabled(int userId, int sensor, bool &result);
    bool isToggleSensorPrivacyEnabled(int sensor);
    bool isToggleSensorPrivacyEnabled(int toggleType, int sensor);
    status_t isToggleSensorPrivacyEnabled(int toggleType, int sensor, bool &result);

    status_t linkToDeath(const sp<IBinder::DeathRecipient>& recipient);
    status_t unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient);
+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(
Loading