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

Commit 8a62852a authored by Arthur Ishiguro's avatar Arthur Ishiguro
Browse files

Only check high sampling permission when requested (native)

Instead of checking for all packages, only check the permission
when it is required to avoid spammy warnings.

Bug: 199125995
Test: Verify warning logs are reduced
Change-Id: I98ca9ffd513aadbabf484a1522def00d9f7102b2
parent 5841f5ce
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ SensorService::SensorDirectConnection::SensorDirectConnection(const sp<SensorSer
        : mService(service), mUid(uid), mMem(*mem),
        mHalChannelHandle(halChannelHandle),
        mOpPackageName(opPackageName), mDestroyed(false) {
    mIsRateCappedBasedOnPermission = mService->isRateCappedBasedOnPermission(mOpPackageName);
    mUserId = multiuser_get_user_id(mUid);
    ALOGD_IF(DEBUG_CONNECTIONS, "Created SensorDirectConnection");
}
@@ -197,8 +196,8 @@ int32_t SensorService::SensorDirectConnection::configureChannel(int handle, int
            if (mService->isSensorInCappedSet(s.getType())) {
                // Back up the rates that the app is allowed to have if the mic toggle is off
                // This is used in the uncapRates() function.
                if (!mIsRateCappedBasedOnPermission ||
                            requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
                if ((requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) ||
                    !isRateCappedBasedOnPermission()) {
                    mMicRateBackup[handle] = requestedRateLevel;
                } else {
                    mMicRateBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
+11 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#ifndef ANDROID_SENSOR_DIRECT_CONNECTION_H
#define ANDROID_SENSOR_DIRECT_CONNECTION_H

#include <optional>
#include <stdint.h>
#include <sys/types.h>

@@ -100,10 +101,19 @@ private:
    std::unordered_map<int, int> mActivatedBackup;
    std::unordered_map<int, int> mMicRateBackup;

    std::atomic_bool mIsRateCappedBasedOnPermission;
    mutable Mutex mDestroyLock;
    bool mDestroyed;
    userid_t mUserId;

    std::optional<bool> mIsRateCappedBasedOnPermission;

    bool isRateCappedBasedOnPermission() {
      if (!mIsRateCappedBasedOnPermission.has_value()) {
        mIsRateCappedBasedOnPermission =
            mService->isRateCappedBasedOnPermission(mOpPackageName);
      }
      return mIsRateCappedBasedOnPermission.value();
    }
};

} // namepsace android
+4 −5
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ SensorService::SensorEventConnection::SensorEventConnection(
      mCacheSize(0), mMaxCacheSize(0), mTimeOfLastEventDrop(0), mEventsDropped(0),
      mPackageName(packageName), mOpPackageName(opPackageName), mAttributionTag(attributionTag),
      mTargetSdk(kTargetSdkUnknown), mDestroyed(false) {
    mIsRateCappedBasedOnPermission = mService->isRateCappedBasedOnPermission(mOpPackageName);
    mUserId = multiuser_get_user_id(mUid);
    mChannel = new BitTube(mService->mSocketBufferSize);
#if DEBUG_CONNECTIONS
@@ -706,8 +705,8 @@ status_t SensorService::SensorEventConnection::enableDisable(
        err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs,
                               reservedFlags, mOpPackageName);
        if (err == OK && isSensorCapped) {
            if (!mIsRateCappedBasedOnPermission ||
                        requestedSamplingPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) {
            if ((requestedSamplingPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) ||
                !isRateCappedBasedOnPermission()) {
                mMicSamplingPeriodBackup[handle] = requestedSamplingPeriodNs;
            } else {
                mMicSamplingPeriodBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS;
@@ -745,8 +744,8 @@ status_t SensorService::SensorEventConnection::setEventRate(int handle, nsecs_t
    }
    status_t ret = mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName);
    if (ret == OK && isSensorCapped) {
        if (!mIsRateCappedBasedOnPermission ||
                    requestedSamplingPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) {
        if ((requestedSamplingPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) ||
            !isRateCappedBasedOnPermission()) {
            mMicSamplingPeriodBackup[handle] = requestedSamplingPeriodNs;
        } else {
            mMicSamplingPeriodBackup[handle] = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS;
+11 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
#define ANDROID_SENSOR_EVENT_CONNECTION_H

#include <atomic>
#include <optional>
#include <stdint.h>
#include <sys/types.h>
#include <unordered_map>
@@ -148,7 +149,6 @@ private:
    sp<SensorService> const mService;
    sp<BitTube> mChannel;
    uid_t mUid;
    std::atomic_bool mIsRateCappedBasedOnPermission;
    mutable Mutex mConnectionLock;
    // Number of events from wake up sensors which are still pending and haven't been delivered to
    // the corresponding application. It is incremented by one unit for each write to the socket.
@@ -201,6 +201,16 @@ private:
    // Mapping of sensor handles to its rate before being capped by the mic toggle.
    std::unordered_map<int, nsecs_t> mMicSamplingPeriodBackup;
    userid_t mUserId;

    std::optional<bool> mIsRateCappedBasedOnPermission;

    bool isRateCappedBasedOnPermission() {
      if (!mIsRateCappedBasedOnPermission.has_value()) {
        mIsRateCappedBasedOnPermission
            = mService->isRateCappedBasedOnPermission(mOpPackageName);
      }
      return mIsRateCappedBasedOnPermission.value();
    }
};

} // namepsace android
+2 −3
Original line number Diff line number Diff line
@@ -2190,10 +2190,10 @@ bool SensorService::isSensorInCappedSet(int sensorType) {
status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* requestedPeriodNs,
        const String16& opPackageName) {
    uid_t uid = IPCThreadState::self()->getCallingUid();
    bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName);
    if (*requestedPeriodNs >= SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS) {
        return OK;
    }
    bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName);
    if (shouldCapBasedOnPermission) {
        *requestedPeriodNs = SENSOR_SERVICE_CAPPED_SAMPLING_PERIOD_NS;
        if (isPackageDebuggable(opPackageName)) {
@@ -2211,11 +2211,10 @@ status_t SensorService::adjustSamplingPeriodBasedOnMicAndPermission(nsecs_t* req
status_t SensorService::adjustRateLevelBasedOnMicAndPermission(int* requestedRateLevel,
        const String16& opPackageName) {
    uid_t uid = IPCThreadState::self()->getCallingUid();
    bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName);

    if (*requestedRateLevel <= SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL) {
        return OK;
    }
    bool shouldCapBasedOnPermission = isRateCappedBasedOnPermission(opPackageName);
    if (shouldCapBasedOnPermission) {
        *requestedRateLevel = SENSOR_SERVICE_CAPPED_SAMPLING_RATE_LEVEL;
        if (isPackageDebuggable(opPackageName)) {