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

Commit 883748c5 authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Use updated AppOps API in SensorService

- Pass the "message" field to the noteOp call.
- Removes the noteOp call except when delivering sensor samples.
- Restructures code to only call checkOp when necessary.

Bug: 160153221
Test: Modify SensorLogger to log on onAsyncNoted() and verify
message gets displayed

Change-Id: I042afb5304150cf5067fdea4f8926b60e64a46e0
parent 08a8d539
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -161,7 +161,7 @@ bool SensorService::SensorEventConnection::addSensor(int32_t handle) {
    Mutex::Autolock _l(mConnectionLock);
    sp<SensorInterface> si = mService->getSensorInterfaceFromHandle(handle);
    if (si == nullptr ||
        !canAccessSensor(si->getSensor(), "Tried adding", mOpPackageName) ||
        !canAccessSensor(si->getSensor(), "Add to SensorEventConnection: ", mOpPackageName) ||
        mSensorInfo.count(handle) > 0) {
        return false;
    }
@@ -460,8 +460,12 @@ bool SensorService::SensorEventConnection::noteOpIfRequired(const sensors_event_
                mTargetSdk > 0 && mTargetSdk <= __ANDROID_API_P__) {
            success = true;
        } else {
            int32_t sensorHandle = event.sensor;
            String16 noteMsg("Sensor event (");
            noteMsg.append(String16(mService->getSensorStringType(sensorHandle)));
            noteMsg.append(String16(")"));
            int32_t appOpMode = mService->sAppOpsManager.noteOp(iter->second, mUid,
                                                                mOpPackageName);
                                                                mOpPackageName, {}, noteMsg);
            success = (appOpMode == AppOpsManager::MODE_ALLOWED);
        }
    }
+6 −0
Original line number Diff line number Diff line
@@ -57,6 +57,12 @@ String8 SensorList::getName(int handle) const {
            mNonSensor.getName());
}

String8 SensorList::getStringType(int handle) const {
    return getOne<String8>(
            handle, [] (const Entry& e) -> String8 {return e.si->getSensor().getStringType();},
            mNonSensor.getStringType());
}

sp<SensorInterface> SensorList::getInterface(int handle) const {
    return getOne<sp<SensorInterface>>(
            handle, [] (const Entry& e) -> sp<SensorInterface> {return e.si;}, nullptr);
+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ public:
    const Vector<Sensor> getVirtualSensors() const;

    String8 getName(int handle) const;
    String8 getStringType(int handle) const;

    sp<SensorInterface> getInterface(int handle) const;
    bool isNewHandle(int handle) const;

+10 −7
Original line number Diff line number Diff line
@@ -1112,6 +1112,10 @@ String8 SensorService::getSensorName(int handle) const {
    return mSensors.getName(handle);
}

String8 SensorService::getSensorStringType(int handle) const {
    return mSensors.getStringType(handle);
}

bool SensorService::isVirtualSensor(int handle) const {
    sp<SensorInterface> sensor = getSensorInterfaceFromHandle(handle);
    return sensor != nullptr && sensor->isVirtual();
@@ -1807,9 +1811,6 @@ bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
    }

    const int32_t opCode = sensor.getRequiredAppOp();
    const int32_t appOpMode = sAppOpsManager.checkOp(opCode,
            IPCThreadState::self()->getCallingUid(), opPackageName);
    bool appOpAllowed = appOpMode == AppOpsManager::MODE_ALLOWED;
    int targetSdkVersion = getTargetSdkVersion(opPackageName);

    bool canAccess = false;
@@ -1822,14 +1823,16 @@ bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation,
        canAccess = true;
    } else if (hasPermissionForSensor(sensor)) {
        // Ensure that the AppOp is allowed, or that there is no necessary app op for the sensor
        if (opCode < 0 || appOpAllowed) {
        if (opCode >= 0) {
            const int32_t appOpMode = sAppOpsManager.checkOp(opCode,
                    IPCThreadState::self()->getCallingUid(), opPackageName);
            canAccess = (appOpMode == AppOpsManager::MODE_ALLOWED);
        } else {
            canAccess = true;
        }
    }

    if (canAccess) {
        sAppOpsManager.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName);
    } else {
    if (!canAccess) {
        ALOGE("%s %s a sensor (%s) without holding %s", String8(opPackageName).string(),
              operation, sensor.getName().string(), sensor.getRequiredPermission().string());
    }
+1 −0
Original line number Diff line number Diff line
@@ -295,6 +295,7 @@ private:
    virtual status_t dump(int fd, const Vector<String16>& args);
    status_t dumpProtoLocked(int fd, ConnectionSafeAutolock* connLock) const;
    String8 getSensorName(int handle) const;
    String8 getSensorStringType(int handle) const;
    bool isVirtualSensor(int handle) const;
    sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const;
    bool isWakeUpSensor(int type) const;