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

Commit 3de585db authored by Atneya Nair's avatar Atneya Nair
Browse files

Condition background record restriction on Sdk

To prevent breaking existing apps, modify the checks around when
an app should have its recording silenced to retain prior behavior
unless an app has targetSdk U or greater.

Test: oboetester conditionally restricted based on targetSdk level
Bug: 268724205
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:dc4f375d570965775634d90856719b812aee9865)
Merged-In: I42b6cbca60db6ce1a073254239b48e9104c4ebfb
Change-Id: I42b6cbca60db6ce1a073254239b48e9104c4ebfb
parent 81581254
Loading
Loading
Loading
Loading
+26 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@
#include <sys/time.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <dlfcn.h>


#include <android/content/pm/IPackageManagerNative.h>
#include <audio_utils/clock.h>
#include <audio_utils/clock.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/Log.h>
@@ -192,6 +193,27 @@ static void destroyAudioPolicyManager(AudioPolicyInterface *interface)
{
{
    delete interface;
    delete interface;
}
}

namespace {
int getTargetSdkForPackageName(std::string_view packageName) {
    const auto binder = defaultServiceManager()->checkService(String16{"package_native"});
    int targetSdk = -1;
    if (binder != nullptr) {
        const auto pm = interface_cast<content::pm::IPackageManagerNative>(binder);
        if (pm != nullptr) {
            const auto status = pm->getTargetSdkVersionForPackage(
                    String16{packageName.data(), packageName.size()}, &targetSdk);
            return status.isOk() ? targetSdk : -1;
        }
    }
    return targetSdk;
}

bool doesPackageTargetAtLeastU(std::string_view packageName) {
    constexpr int ANDROID_API_U = 34;
    return getTargetSdkForPackageName(packageName) >= ANDROID_API_U;
}
} // anonymous
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


AudioPolicyService::AudioPolicyService()
AudioPolicyService::AudioPolicyService()
@@ -1873,10 +1895,14 @@ void AudioPolicyService::OpRecordAudioMonitor::onFirstRef()
    checkOp();
    checkOp();
    mOpCallback = new RecordAudioOpCallback(this);
    mOpCallback = new RecordAudioOpCallback(this);
    ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
    ALOGV("start watching op %d for %s", mAppOp, mAttributionSource.toString().c_str());
    int flags = doesPackageTargetAtLeastU(
            mAttributionSource.packageName.value_or("")) ?
            AppOpsManager::WATCH_FOREGROUND_CHANGES : 0;
    // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
    // TODO: We need to always watch AppOpsManager::OP_RECORD_AUDIO too
    // since it controls the mic permission for legacy apps.
    // since it controls the mic permission for legacy apps.
    mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
    mAppOpsManager.startWatchingMode(mAppOp, VALUE_OR_FATAL(aidl2legacy_string_view_String16(
        mAttributionSource.packageName.value_or(""))),
        mAttributionSource.packageName.value_or(""))),
        flags,
        mOpCallback);
        mOpCallback);
}
}