Device-aware native SensorManager.
Add device id to SensorManager instances based on the devices
this UID is present on. SensorManager queries the native VDM
instance for the deviceId and the sensor policy of that device.
If the UID is present only on a single virtual device and that
device has custom sensors, then give it that device's sensors by
default. If the UID is not on any virtual devices or is on more
than one, then give it the default device's sensors.
Fix: 303535376
Test: manual with streamed app using sensor NDK to a virtual device
Change-Id: I275a010a7a70b34176deaf4c9dcd61188c83ac32
diff --git a/libs/sensor/SensorManager.cpp b/libs/sensor/SensorManager.cpp
index 980f8d1..f3de2ef 100644
--- a/libs/sensor/SensorManager.cpp
+++ b/libs/sensor/SensorManager.cpp
@@ -26,6 +26,8 @@
#include <utils/RefBase.h>
#include <utils/Singleton.h>
+#include <android/companion/virtualnative/IVirtualDeviceManagerNative.h>
+
#include <binder/IBinder.h>
#include <binder/IPermissionController.h>
#include <binder/IServiceManager.h>
@@ -39,6 +41,43 @@
namespace android {
// ----------------------------------------------------------------------------
+namespace {
+
+using ::android::companion::virtualnative::IVirtualDeviceManagerNative;
+
+static constexpr int DEVICE_ID_DEFAULT = 0;
+
+// Returns the deviceId of the device where this uid is observed. If the uid is present on more than
+// one devices, return the default deviceId.
+int getDeviceIdForUid(uid_t uid) {
+ sp<IBinder> binder =
+ defaultServiceManager()->checkService(String16("virtualdevice_native"));
+ if (binder != nullptr) {
+ auto vdm = interface_cast<IVirtualDeviceManagerNative>(binder);
+ std::vector<int> deviceIds;
+ vdm->getDeviceIdsForUid(uid, &deviceIds);
+ // If the UID is associated with multiple virtual devices, use the default device's
+ // sensors as we cannot disambiguate here. This effectively means that the app has
+ // activities on different devices at the same time, so it must handle the device
+ // awareness by itself.
+ if (deviceIds.size() == 1) {
+ const int deviceId = deviceIds.at(0);
+ int devicePolicy = IVirtualDeviceManagerNative::DEVICE_POLICY_DEFAULT;
+ vdm->getDevicePolicy(deviceId,
+ IVirtualDeviceManagerNative::POLICY_TYPE_SENSORS,
+ &devicePolicy);
+ if (devicePolicy == IVirtualDeviceManagerNative::DEVICE_POLICY_CUSTOM) {
+ return deviceId;
+ }
+ }
+ } else {
+ ALOGW("Cannot get virtualdevice_native service");
+ }
+ return DEVICE_ID_DEFAULT;
+}
+
+} // namespace
+
Mutex SensorManager::sLock;
std::map<String16, SensorManager*> SensorManager::sPackageInstances;
@@ -53,6 +92,7 @@
sensorManager = iterator->second;
} else {
String16 opPackageName = packageName;
+ const uid_t uid = IPCThreadState::self()->getCallingUid();
// It is possible that the calling code has no access to the package name.
// In this case we will get the packages for the calling UID and pick the
@@ -63,7 +103,6 @@
if (opPackageName.size() <= 0) {
sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
if (binder != nullptr) {
- const uid_t uid = IPCThreadState::self()->getCallingUid();
Vector<String16> packages;
interface_cast<IPermissionController>(binder)->getPackagesForUid(uid, packages);
if (!packages.isEmpty()) {
@@ -76,7 +115,10 @@
}
}
- sensorManager = new SensorManager(opPackageName);
+ // Check if the calling UID is observed on a virtual device. If so, provide that device's
+ // sensors by default instead of the default device's sensors.
+ const int deviceId = getDeviceIdForUid(uid);
+ sensorManager = new SensorManager(opPackageName, deviceId);
// If we had no package name, we looked it up from the UID and the sensor
// manager instance we created should also be mapped to the empty package
@@ -102,8 +144,9 @@
}
}
-SensorManager::SensorManager(const String16& opPackageName)
- : mSensorList(nullptr), mOpPackageName(opPackageName), mDirectConnectionHandle(1) {
+SensorManager::SensorManager(const String16& opPackageName, int deviceId)
+ : mSensorList(nullptr), mOpPackageName(opPackageName), mDeviceId(deviceId),
+ mDirectConnectionHandle(1) {
Mutex::Autolock _l(mLock);
assertStateLocked();
}
@@ -174,7 +217,12 @@
mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);
- mSensors = mSensorServer->getSensorList(mOpPackageName);
+ if (mDeviceId == DEVICE_ID_DEFAULT) {
+ mSensors = mSensorServer->getSensorList(mOpPackageName);
+ } else {
+ mSensors = mSensorServer->getRuntimeSensorList(mOpPackageName, mDeviceId);
+ }
+
size_t count = mSensors.size();
// If count is 0, mSensorList will be non-null. This is old
// existing behavior and callers expect this.