Revert "Disable sensors when an app goes to background"

This reverts commit 8a7ec57d045258e2d84beffd4e7d03f6b95e26e4.

Bug: 74395023
Test: Revert

Change-Id: Ibfdb5da3ed121294b72f5d176e358e6e3221d9d5
diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp
index 77d8c11..e799372 100644
--- a/services/sensorservice/SensorEventConnection.cpp
+++ b/services/sensorservice/SensorEventConnection.cpp
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include <cinttypes>
 #include <sys/socket.h>
 #include <utils/threads.h>
 
@@ -70,17 +69,17 @@
 }
 
 bool SensorService::SensorEventConnection::needsWakeLock() {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     return !mDead && mWakeLockRefCount > 0;
 }
 
 void SensorService::SensorEventConnection::resetWakeLockRefCount() {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     mWakeLockRefCount = 0;
 }
 
 void SensorService::SensorEventConnection::dump(String8& result) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     result.appendFormat("\tOperating Mode: ");
     if (!mService->isWhiteListedPackage(getPackageName())) {
         result.append("RESTRICTED\n");
@@ -93,7 +92,7 @@
             "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize,
             mMaxCacheSize);
     for (auto& it : mSensorInfo) {
-        const FlushInfo& flushInfo = it.second.flushInfo;
+        const FlushInfo& flushInfo = it.second;
         result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n",
                             mService->getSensorName(it.first).string(),
                             it.first,
@@ -122,7 +121,7 @@
  */
 void SensorService::SensorEventConnection::dump(util::ProtoOutputStream* proto) const {
     using namespace service::SensorEventConnectionProto;
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
 
     if (!mService->isWhiteListedPackage(getPackageName())) {
         proto->write(OPERATING_MODE, OP_MODE_RESTRICTED);
@@ -137,7 +136,7 @@
     proto->write(CACHE_SIZE, int32_t(mCacheSize));
     proto->write(MAX_CACHE_SIZE, int32_t(mMaxCacheSize));
     for (auto& it : mSensorInfo) {
-        const FlushInfo& flushInfo = it.second.flushInfo;
+        const FlushInfo& flushInfo = it.second;
         const uint64_t token = proto->start(FLUSH_INFOS);
         proto->write(FlushInfoProto::SENSOR_NAME,
                 std::string(mService->getSensorName(it.first)));
@@ -158,33 +157,28 @@
 #endif
 }
 
-bool SensorService::SensorEventConnection::addSensor(
-    int32_t handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
+    Mutex::Autolock _l(mConnectionLock);
     sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
     if (si == nullptr ||
         !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
         mSensorInfo.count(handle) > 0) {
         return false;
     }
-
-    SensorRequest request = {
-      .samplingPeriodNs = samplingPeriodNs,
-      .maxBatchReportLatencyNs = maxBatchReportLatencyNs,
-      .reservedFlags = reservedFlags
-    };
-
-    mSensorInfo[handle] = request;
+    mSensorInfo[handle] = FlushInfo();
     return true;
 }
 
 bool SensorService::SensorEventConnection::removeSensor(int32_t handle) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    return mSensorInfo.erase(handle) > 0;
+    Mutex::Autolock _l(mConnectionLock);
+    if (mSensorInfo.erase(handle) >= 0) {
+        return true;
+    }
+    return false;
 }
 
 std::vector<int32_t> SensorService::SensorEventConnection::getActiveSensorHandles() const {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     std::vector<int32_t> list;
     for (auto& it : mSensorInfo) {
         list.push_back(it.first);
@@ -193,17 +187,17 @@
 }
 
 bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    return mSensorInfo.count(handle) + mSensorInfoBackup.count(handle) > 0;
+    Mutex::Autolock _l(mConnectionLock);
+    return mSensorInfo.count(handle) > 0;
 }
 
 bool SensorService::SensorEventConnection::hasAnySensor() const {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    return mSensorInfo.size() + mSensorInfoBackup.size() ? true : false;
+    Mutex::Autolock _l(mConnectionLock);
+    return mSensorInfo.size() ? true : false;
 }
 
 bool SensorService::SensorEventConnection::hasOneShotSensors() const {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     for (auto &it : mSensorInfo) {
         const int handle = it.first;
         sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
@@ -220,15 +214,15 @@
 
 void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle,
                                 bool value) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     if (mSensorInfo.count(handle) > 0) {
-        FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
+        FlushInfo& flushInfo = mSensorInfo[handle];
         flushInfo.mFirstFlushPending = value;
     }
 }
 
 void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     updateLooperRegistrationLocked(looper);
 }
 
@@ -279,9 +273,9 @@
 }
 
 void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     if (mSensorInfo.count(handle) > 0) {
-        FlushInfo& flushInfo = mSensorInfo[handle].flushInfo;
+        FlushInfo& flushInfo = mSensorInfo[handle];
         flushInfo.mPendingFlushEventsToSend++;
     }
 }
@@ -295,7 +289,7 @@
     std::unique_ptr<sensors_event_t[]> sanitizedBuffer;
 
     int count = 0;
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     if (scratch) {
         size_t i=0;
         while (i<numEvents) {
@@ -316,7 +310,7 @@
                 continue;
             }
 
-            FlushInfo& flushInfo = mSensorInfo[sensor_handle].flushInfo;
+            FlushInfo& flushInfo = mSensorInfo[sensor_handle];
             // Check if there is a pending flush_complete event for this sensor on this connection.
             if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true &&
                     mapFlushEventsToConnections[i] == this) {
@@ -437,55 +431,9 @@
     return size < 0 ? status_t(size) : status_t(NO_ERROR);
 }
 
-void SensorService::SensorEventConnection::updateSensorSubscriptions() {
-    if (!hasSensorAccess()) {
-        stopAll();
-    } else {
-        recoverAll();
-    }
-}
-
 void SensorService::SensorEventConnection::setSensorAccess(const bool hasAccess) {
-    if (mHasSensorAccess != hasAccess) {
-        mHasSensorAccess = hasAccess;
-        updateSensorSubscriptions();
-    }
-}
-
-void SensorService::SensorEventConnection::stopAll() {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    if (!mSensorInfo.empty()) {
-        mSensorInfoBackup = mSensorInfo;
-        mSensorInfo.clear();
-
-        for (auto& it : mSensorInfoBackup) {
-            int32_t handle = it.first;
-
-            status_t err =  mService->disable(this, handle);
-
-            if (err != NO_ERROR) {
-                ALOGE("Error disabling sensor %d", handle);
-            }
-        }
-    }
-}
-
-void SensorService::SensorEventConnection::recoverAll() {
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    for (auto& it : mSensorInfoBackup) {
-        int32_t handle = it.first;
-        SensorRequest &request = it.second;
-
-        status_t err =  mService->enable(
-            this, handle, request.samplingPeriodNs, request.maxBatchReportLatencyNs,
-            request.reservedFlags, mOpPackageName);
-
-        if (err != NO_ERROR) {
-            ALOGE("Error recovering sensor %d", handle);
-        }
-    }
-
-    mSensorInfoBackup.clear();
+    Mutex::Autolock _l(mConnectionLock);
+    mHasSensorAccess = hasAccess;
 }
 
 bool SensorService::SensorEventConnection::hasSensorAccess() {
@@ -587,7 +535,7 @@
             continue;
         }
 
-        FlushInfo& flushInfo = it.second.flushInfo;
+        FlushInfo& flushInfo = it.second;
         while (flushInfo.mPendingFlushEventsToSend > 0) {
             flushCompleteEvent.meta_data.sensor = handle;
             bool wakeUpSensor = si->getSensor().isWakeUpSensor();
@@ -612,7 +560,7 @@
     // half the size of the socket buffer allocated in BitTube whichever is smaller.
     const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2,
             int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2)));
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+    Mutex::Autolock _l(mConnectionLock);
     // Send pending flush complete events (if any)
     sendPendingFlushEventsLocked();
     for (int numEventsSent = 0; numEventsSent < mCacheSize;) {
@@ -679,7 +627,7 @@
                 continue;
             }
 
-            FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor].flushInfo;
+            FlushInfo& flushInfo = mSensorInfo[scratch[j].meta_data.sensor];
             flushInfo.mPendingFlushEventsToSend++;
             ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d",
                      flushInfo.mPendingFlushEventsToSend);
@@ -715,21 +663,13 @@
     } else {
         err = mService->disable(this, handle);
     }
-
     return err;
 }
 
 status_t SensorService::SensorEventConnection::setEventRate(
         int handle, nsecs_t samplingPeriodNs)
 {
-    status_t err = mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
-
-    std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
-    if (err == NO_ERROR && mSensorInfo.count(handle) > 0) {
-        mSensorInfo[handle].samplingPeriodNs = samplingPeriodNs;
-    }
-
-    return err;
+    return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
 }
 
 status_t  SensorService::SensorEventConnection::flush() {
@@ -750,7 +690,7 @@
             // and remove the fd from Looper. Call checkWakeLockState to know if SensorService
             // can release the wake-lock.
             ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd);
-            std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+            Mutex::Autolock _l(mConnectionLock);
             mDead = true;
             mWakeLockRefCount = 0;
             updateLooperRegistrationLocked(mService->getLooper());
@@ -769,7 +709,7 @@
         unsigned char buf[sizeof(sensors_event_t)];
         ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT);
         {
-            std::lock_guard<std::recursive_mutex> _l(mConnectionLock);
+            Mutex::Autolock _l(mConnectionLock);
             if (numBytesRead == sizeof(sensors_event_t)) {
                 if (!mDataInjectionMode) {
                     ALOGE("Data injected in normal mode, dropping event"