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

Commit 2fa8d330 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

AudioService: log metric for audio focus hardening skipped due to SDK

Bug: 365434894
Flag: android.media.audio.ro_foreground_audio_control
Test: go/tex-metric/media_audio.value_audio_focus_grant_hardening_waived_by_sdk
Change-Id: I83b10bb2564e488ba2d77672fab0ecac9f118ffa
parent 0924ffe6
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -10809,7 +10809,8 @@ public class AudioService extends IAudioService.Stub
            //TODO move inside HardeningEnforcer after refactor that moves permission checks
            //     in the blockFocusMethod
            if (permissionOverridesCheck) {
                mHardeningEnforcer.metricsLogFocusReq(/*blocked*/false, focusReqType, uid);
                mHardeningEnforcer.metricsLogFocusReq(/*blocked*/ false, focusReqType, uid,
                        /*unblockedBySdk*/ false);
            }
            if (!permissionOverridesCheck && mHardeningEnforcer.blockFocusMethod(uid,
                    HardeningEnforcer.METHOD_AUDIO_MANAGER_REQUEST_AUDIO_FOCUS,
+20 −2
Original line number Diff line number Diff line
@@ -168,6 +168,8 @@ public class HardeningEnforcer {
        }

        boolean blocked = true;
        // indicates the focus request was not blocked because of the SDK version
        boolean unblockedBySdk = false;
        if (noteOp(AppOpsManager.OP_TAKE_AUDIO_FOCUS, callingUid, packageName, attributionTag)) {
            if (DEBUG) {
                Slog.i(TAG, "blockFocusMethod pack:" + packageName + " NOT blocking");
@@ -179,9 +181,10 @@ public class HardeningEnforcer {
                        + targetSdk);
            }
            blocked = false;
            unblockedBySdk = true;
        }

        metricsLogFocusReq(blocked, focusReqType, callingUid);
        metricsLogFocusReq(blocked, focusReqType, callingUid, unblockedBySdk);

        if (!blocked) {
            return false;
@@ -195,7 +198,16 @@ public class HardeningEnforcer {
        return true;
    }

    /*package*/ void metricsLogFocusReq(boolean blocked, int focusReq, int callingUid) {
    /**
     * Log metrics for the focus request
     * @param blocked true if the call blocked
     * @param focusReq the type of focus request
     * @param callingUid the UID of the caller
     * @param unblockedBySdk if blocked is false,
     *                       true indicates it was unblocked thanks to an older SDK
     */
    /*package*/ void metricsLogFocusReq(boolean blocked, int focusReq, int callingUid,
            boolean unblockedBySdk) {
        final String metricId = blocked ? METRIC_COUNTERS_FOCUS_DENIAL.get(focusReq)
                : METRIC_COUNTERS_FOCUS_GRANT.get(focusReq);
        if (TextUtils.isEmpty(metricId)) {
@@ -204,6 +216,12 @@ public class HardeningEnforcer {
        }
        try {
            Counter.logIncrementWithUid(metricId, callingUid);
            if (!blocked && unblockedBySdk) {
                // additional metric to capture focus requests that are currently granted
                // because the app is on an older SDK, but would have been blocked otherwise
                Counter.logIncrementWithUid(
                        "media_audio.value_audio_focus_grant_hardening_waived_by_sdk", callingUid);
            }
        } catch (Exception e) {
            Slog.e(TAG, "Counter error metricId:" + metricId + " for focus req:" + focusReq
                    + " from uid:" + callingUid, e);