Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3492ae46 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Device-aware native SensorManager." into main

parents 3feea1b9 f1d48af0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ cc_library {
        "liblog",
        "libhardware",
        "libpermission",
        "android.companion.virtual.virtualdevice_aidl-cpp",
    ],

    export_include_dirs: ["include"],
+53 −5
Original line number Diff line number Diff line
@@ -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& SensorManager::getInstanceForPackage(const String16& packageName)
        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 @@ SensorManager& SensorManager::getInstanceForPackage(const String16& packageName)
        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& SensorManager::getInstanceForPackage(const String16& packageName)
            }
        }

        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 @@ void SensorManager::removeInstanceForPackage(const String16& packageName) {
    }
}

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 @@ status_t SensorManager::assertStateLocked() {
        mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this));
        IInterface::asBinder(mSensorServer)->linkToDeath(mDeathObserver);

        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.
+2 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ private:
    void sensorManagerDied();
    static status_t waitForSensorService(sp<ISensorServer> *server);

    explicit SensorManager(const String16& opPackageName);
    explicit SensorManager(const String16& opPackageName, int deviceId);
    status_t assertStateLocked();

private:
@@ -94,6 +94,7 @@ private:
    Vector<Sensor> mDynamicSensors;
    sp<IBinder::DeathRecipient> mDeathObserver;
    const String16 mOpPackageName;
    const int mDeviceId;
    std::unordered_map<int, sp<ISensorEventConnection>> mDirectConnection;
    int32_t mDirectConnectionHandle;
};
+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ cc_fuzz {
    static_libs: [
        "libsensorserviceaidl",
        "libpermission",
        "android.companion.virtual.virtualdevice_aidl-cpp",
        "android.frameworks.sensorservice-V1-ndk",
        "android.hardware.sensors-V1-convert",
        "android.hardware.sensors-V2-ndk",