Extend background app behavior for direct channel
Bug: 74395023
Test: Modify sensor logger to use direct channel. Check that connection
is not active when the app becomes idle, and becomes active once the app
is no longer idle.
Change-Id: Ib4ec4b639698bf81f58051af0047a4d0982124a5
diff --git a/services/sensorservice/SensorDirectConnection.cpp b/services/sensorservice/SensorDirectConnection.cpp
index 106efd6..1622e77 100644
--- a/services/sensorservice/SensorDirectConnection.cpp
+++ b/services/sensorservice/SensorDirectConnection.cpp
@@ -93,6 +93,23 @@
return nullptr;
}
+void SensorService::SensorDirectConnection::updateSensorSubscriptions() {
+ if (!hasSensorAccess()) {
+ stopAll(true /* backupRecord */);
+ } else {
+ recoverAll();
+ }
+}
+
+void SensorService::SensorDirectConnection::setSensorAccess(bool hasAccess) {
+ mHasSensorAccess = hasAccess;
+ updateSensorSubscriptions();
+}
+
+bool SensorService::SensorDirectConnection::hasSensorAccess() const {
+ return mHasSensorAccess && !mService->mSensorPrivacyPolicy->isSensorPrivacyEnabled();
+}
+
status_t SensorService::SensorDirectConnection::enableDisable(
int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs,
int reservedFlags) {
diff --git a/services/sensorservice/SensorDirectConnection.h b/services/sensorservice/SensorDirectConnection.h
index ead08d3..fa88fbc 100644
--- a/services/sensorservice/SensorDirectConnection.h
+++ b/services/sensorservice/SensorDirectConnection.h
@@ -54,6 +54,9 @@
// called by SensorService when return to NORMAL mode.
void recoverAll();
+ void updateSensorSubscriptions();
+
+ void setSensorAccess(bool hasAccess);
protected:
virtual ~SensorDirectConnection();
// ISensorEventConnection functions
@@ -66,6 +69,7 @@
virtual int32_t configureChannel(int handle, int rateLevel);
virtual void destroy();
private:
+ bool hasSensorAccess() const;
const sp<SensorService> mService;
const uid_t mUid;
const sensors_direct_mem_t mMem;
@@ -76,6 +80,8 @@
std::unordered_map<int, int> mActivated;
std::unordered_map<int, int> mActivatedBackup;
+ std::atomic_bool mHasSensorAccess = true;
+
mutable Mutex mDestroyLock;
bool mDestroyed;
};
diff --git a/services/sensorservice/SensorService.cpp b/services/sensorservice/SensorService.cpp
index 5ae7c51..92c27b1 100644
--- a/services/sensorservice/SensorService.cpp
+++ b/services/sensorservice/SensorService.cpp
@@ -302,6 +302,7 @@
void SensorService::setSensorAccess(uid_t uid, bool hasAccess) {
ConnectionSafeAutolock connLock = mConnectionHolder.lock(mLock);
const auto& connections = connLock.getActiveConnections();
+ const auto& directConnections = connLock.getDirectConnections();
mLock.unlock();
for (const sp<SensorEventConnection>& conn : connections) {
@@ -309,6 +310,12 @@
conn->setSensorAccess(hasAccess);
}
}
+
+ for (const sp<SensorDirectConnection>& conn : directConnections) {
+ if (conn->getUid() == uid) {
+ conn->setSensorAccess(hasAccess);
+ }
+ }
}
const Sensor& SensorService::registerSensor(SensorInterface* s, bool isDebug, bool isVirtual) {
@@ -645,7 +652,7 @@
connection->updateSensorSubscriptions();
}
for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
- connection->stopAll(true /* backupRecord */);
+ connection->updateSensorSubscriptions();
}
dev.disableAllSensors();
// Clear all pending flush connections for all active sensors. If one of the active
@@ -676,7 +683,7 @@
connection->updateSensorSubscriptions();
}
for (const sp<SensorDirectConnection>& connection : connLock->getDirectConnections()) {
- connection->recoverAll();
+ connection->updateSensorSubscriptions();
}
}