Check if sensor is accessible on flush

In cases where halVersion is less than or equal to 1 or the sensor is
virtual the sensor service does not first check that the sensor is
accessible. This was causing failures for tests where a virtual sensor
was used because the flush command was not returning error. Now
INVALID_OPERATION is returned when flush is called on an idle sensor.

Test: set IGNORE_HARDWARE_FUSION to true and cts-tradefed && run cts -m
CtsSensorTestCases -t
android.hardware.cts.SensorTest#testBatchAndFlushUidIdle which fails
before this change, but passes now
Bug: 160282248

Change-Id: I6860bcdb9be341b5e2025caf7606d071779c9b39
diff --git a/services/sensorservice/SensorEventConnection.cpp b/services/sensorservice/SensorEventConnection.cpp
index 9b30dce..b4b5f98 100644
--- a/services/sensorservice/SensorEventConnection.cpp
+++ b/services/sensorservice/SensorEventConnection.cpp
@@ -272,11 +272,16 @@
     }
 }
 
-void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) {
-    Mutex::Autolock _l(mConnectionLock);
-    if (mSensorInfo.count(handle) > 0) {
-        FlushInfo& flushInfo = mSensorInfo[handle];
-        flushInfo.mPendingFlushEventsToSend++;
+bool SensorService::SensorEventConnection::incrementPendingFlushCountIfHasAccess(int32_t handle) {
+    if (hasSensorAccess()) {
+        Mutex::Autolock _l(mConnectionLock);
+        if (mSensorInfo.count(handle) > 0) {
+            FlushInfo& flushInfo = mSensorInfo[handle];
+            flushInfo.mPendingFlushEventsToSend++;
+        }
+        return true;
+    } else {
+        return false;
     }
 }