diff options
-rw-r--r-- | services/sensorservice/SensorService.cpp | 108 | ||||
-rw-r--r-- | services/sensorservice/SensorService.h | 35 |
2 files changed, 68 insertions, 75 deletions
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp index 971491dbef..3c164aadd9 100644 --- a/services/sensorservice/SensorService.cpp +++ b/services/sensorservice/SensorService.cpp @@ -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,33 +775,25 @@ 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); - } + conn->onMicSensorAccessChanged(true); } for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) { - if (conn->getUserId() == userId) { - conn->onMicSensorAccessChanged(true); - } + 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); - } + conn->onMicSensorAccessChanged(false); } for (const sp<SensorEventConnection>& conn : connLock.getActiveConnections()) { - if (conn->getUserId() == userId) { - conn->onMicSensorAccessChanged(false); - } + conn->onMicSensorAccessChanged(false); } } @@ -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); - } + 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); - } + if (enabled) { + service->disableAllSensors(); } else { - if (enabled) { - service->disableAllSensors(); - } else { - service->enableAllSensors(); - } + 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); - - if (err != OK) { - ALOGE("Cannot register a mic listener."); - return err; - } - mSensorPrivacyEnabled = spm.isIndividualSensorPrivacyEnabled(userId, - SensorPrivacyManager::INDIVIDUAL_SENSOR_MICROPHONE); + mSensorPrivacyEnabled = + spm.isToggleSensorPrivacyEnabled( + SensorPrivacyManager::TOGGLE_TYPE_SOFTWARE, + SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE) + || spm.isToggleSensorPrivacyEnabled( + SensorPrivacyManager::TOGGLE_TYPE_HARDWARE, + SensorPrivacyManager::TOGGLE_SENSOR_MICROPHONE); + spm.addToggleSensorPrivacyListener(this); +} - mIsIndividualMic = true; - mUserId = userId; - return OK; +void SensorService::MicrophonePrivacyPolicy::unregisterSelf() { + AutoCallerClear acc; + SensorPrivacyManager spm; + spm.removeToggleSensorPrivacyListener(this); } -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; +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(); + + if (service != nullptr) { + if (enabled) { + service->capRates(); + } else { + service->uncapRates(); } - mMicSensorPrivacyPolicies[userId] = userPolicy; } - return mMicSensorPrivacyPolicies[userId]->isSensorPrivacyEnabled(); + return binder::Status::ok(); } SensorService::ConnectionSafeAutolock::ConnectionSafeAutolock( diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h index b009829ed6..7194db3275 100644 --- a/services/sensorservice/SensorService.h +++ b/services/sensorservice/SensorService.h @@ -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; |