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

Commit 6ed47691 authored by Vladimir Komsiyski's avatar Vladimir Komsiyski Committed by Android (Google) Code Review
Browse files

Merge "Respect deviceId in SensorManager cache." into main

parents b8e9d020 71db5f82
Loading
Loading
Loading
Loading
+36 −34
Original line number Diff line number Diff line
@@ -88,11 +88,16 @@ SensorManager& SensorManager::getInstanceForPackage(const String16& packageName)
    SensorManager* sensorManager;
    auto iterator = sPackageInstances.find(packageName);

    const uid_t uid = IPCThreadState::self()->getCallingUid();
    const int deviceId = getDeviceIdForUid(uid);

    // Return the cached instance if the device association of the package has not changed.
    if (iterator != sPackageInstances.end()) {
        sensorManager = iterator->second;
    } else {
        String16 opPackageName = packageName;
        const uid_t uid = IPCThreadState::self()->getCallingUid();
        if (sensorManager->mDeviceId == deviceId) {
            return *sensorManager;
        }
    }

    // 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
@@ -100,6 +105,7 @@ SensorManager& SensorManager::getInstanceForPackage(const String16& packageName)
    // runtime permissions as for legacy apps we will toggle the app op for
    // all packages in the UID. The caveat is that the operation may be attributed
    // to the wrong package and stats based on app ops may be slightly off.
    String16 opPackageName = packageName;
    if (opPackageName.size() <= 0) {
        sp<IBinder> binder = defaultServiceManager()->getService(String16("permission"));
        if (binder != nullptr) {
@@ -115,9 +121,6 @@ SensorManager& SensorManager::getInstanceForPackage(const String16& packageName)
        }
    }

        // 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
@@ -129,7 +132,6 @@ SensorManager& SensorManager::getInstanceForPackage(const String16& packageName)

    // Stash the per package sensor manager.
    sPackageInstances.insert(std::make_pair(opPackageName, sensorManager));
    }

    return *sensorManager;
}