Require AR permission to access step events

Requires that an application targeting Android Q and later has the AR
runtime permission in order to access the Step Detector and the Step
Counter. Applications that target pre-Q are not required to hold the
permission. If an application that targets pre-Q holds the GMS Core AR
install time permission and the user revokes the permission, then the
application will no longer be able to receive Step Detector or Step
Counter events.

The following scenarios were tested:
A = Receive events without explicitly granting permission
B = Receive events after granting runtime permission
C = Receive events after revoking any available permission

Install = GMS Core install time permission
Runtime = android.permission.ACTIVITY_RECOGNITION runtime permission

  SDK Version:  |   Has Permission: | Could Receive:
Compile  Target |  Install  Runtime |  A    B    C
   Q       Q         Yes      Yes     No   Yes  No
   Q       Q         Yes      No      No   N/A  N/A
   Q       Q         No       Yes     No   Yes  No
   Q       Q         No       No      No   N/A  N/A
   Q       P         Yes      Yes     Yes  Yes  No
   Q       P         Yes      No      Yes  Yes  No
   Q       P         No       Yes     Yes  Yes  No
   Q       P         No       No      Yes  Yes  N/A
   P       P         Yes      Yes     Yes  Yes  No
   P       P         Yes      No      Yes  Yes  No
   P       P         No       Yes     Yes  Yes  No
   P       P         No       No      Yes  Yes  N/A

Bug: 130640415
Test: Builds
Test: Scenarios listed aboved
Change-Id: Iffe4aff731efd1fe20fa7b4a8245262db8b32d03
diff --git a/services/sensorservice/SensorService.h b/services/sensorservice/SensorService.h
index 5076967..e6ec96d 100644
--- a/services/sensorservice/SensorService.h
+++ b/services/sensorservice/SensorService.h
@@ -20,6 +20,7 @@
 #include "SensorList.h"
 #include "RecentEventLogger.h"
 
+#include <binder/AppOpsManager.h>
 #include <binder/BinderService.h>
 #include <binder/IUidObserver.h>
 #include <cutils/compiler.h>
@@ -243,6 +244,8 @@
             sensors_event_t const* buffer, const int count);
     static bool canAccessSensor(const Sensor& sensor, const char* operation,
             const String16& opPackageName);
+    static bool hasPermissionForSensor(const Sensor& sensor);
+    static int getTargetSdkVersion(const String16& opPackageName);
     // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
     // method checks whether all the events from these wake up sensors have been delivered to the
     // corresponding applications, if yes the wakelock is released.
@@ -343,6 +346,10 @@
 
     sp<UidPolicy> mUidPolicy;
     sp<SensorPrivacyPolicy> mSensorPrivacyPolicy;
+
+    static AppOpsManager sAppOpsManager;
+    static std::map<String16, int> sPackageTargetVersion;
+    static Mutex sPackageTargetVersionLock;
 };
 
 } // namespace android