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

Commit 78f6e04c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "[audio] Log playback hardening in enforcer" into main

parents 26626108 7a0cfddf
Loading
Loading
Loading
Loading
+48 −2
Original line number Original line Diff line number Diff line
@@ -256,6 +256,7 @@ import com.android.internal.os.BackgroundThread;
import com.android.internal.os.SomeArgs;
import com.android.internal.os.SomeArgs;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;
import com.android.modules.expresslog.Counter;
import com.android.server.EventLogTags;
import com.android.server.EventLogTags;
import com.android.server.LocalManagerRegistry;
import com.android.server.LocalManagerRegistry;
import com.android.server.LocalServices;
import com.android.server.LocalServices;
@@ -838,9 +839,49 @@ public class AudioService extends IAudioService.Stub
            new AudioServiceUserRestrictionsListener();
            new AudioServiceUserRestrictionsListener();
    private final IAudioManagerNative mNativeShim = new IAudioManagerNative.Stub() {
    private final IAudioManagerNative mNativeShim = new IAudioManagerNative.Stub() {
        static final String METRIC_COUNTERS_PLAYBACK_PARTIAL =
                "media_audio.value_audio_playback_hardening_partial_restriction";
        static final String METRIC_COUNTERS_PLAYBACK_STRICT =
                "media_audio.value_audio_playback_hardening_strict_would_restrict";
        String getPackNameForUid(int uid) {
            final long token = Binder.clearCallingIdentity();
            try {
                final String[] names = AudioService.this.mContext.
                                            getPackageManager().getPackagesForUid(uid);
                if (names == null
                        || names.length == 0
                        || TextUtils.isEmpty(names[0])) {
                    return "[" + uid + "]";
                }
                return names[0];
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }
        // oneway
        // oneway
        @Override
        @Override
        public void playbackHardeningEvent(int uid, byte type, boolean bypassed) {
        public void playbackHardeningEvent(int uid, byte type, boolean bypassed) {
            if (Binder.getCallingUid() != Process.AUDIOSERVER_UID) {
                return;
            }
            if (type == HardeningType.PARTIAL) {
                Counter.logIncrementWithUid(METRIC_COUNTERS_PLAYBACK_PARTIAL, uid);
            } else if (type == HardeningType.FULL) {
                Counter.logIncrementWithUid(METRIC_COUNTERS_PLAYBACK_STRICT, uid);
            } else {
                Slog.wtf(TAG, "Unexpected hardening type" + type);
                return;
            }
            String msg = "AudioHardening background playback "
                    + (bypassed ? "would be " : "")
                    + "muted for "
                    + getPackNameForUid(uid) + " (" + uid + "), "
                    + "level: " + (type == HardeningType.PARTIAL ? "partial" : "full");
            AudioService.this.mHardeningLogger.enqueueAndSlog(msg,
                    bypassed ? EventLogger.Event.ALOGI : EventLogger.Event.ALOGW, TAG);
        }
        }
        @Override
        @Override
@@ -1544,7 +1585,8 @@ public class AudioService extends IAudioService.Stub
        mMusicFxHelper = new MusicFxHelper(mContext, mAudioHandler);
        mMusicFxHelper = new MusicFxHelper(mContext, mAudioHandler);
        mHardeningEnforcer = new HardeningEnforcer(mContext, isPlatformAutomotive(), mAppOps,
        mHardeningEnforcer = new HardeningEnforcer(mContext, isPlatformAutomotive(), mAppOps,
                context.getPackageManager());
                context.getPackageManager(),
                mHardeningLogger);
    }
    }
    private void initVolumeStreamStates() {
    private void initVolumeStreamStates() {
@@ -12691,6 +12733,7 @@ public class AudioService extends IAudioService.Stub
    static final int LOG_NB_EVENTS_DYN_POLICY = 10;
    static final int LOG_NB_EVENTS_DYN_POLICY = 10;
    static final int LOG_NB_EVENTS_SPATIAL = 30;
    static final int LOG_NB_EVENTS_SPATIAL = 30;
    static final int LOG_NB_EVENTS_SOUND_DOSE = 50;
    static final int LOG_NB_EVENTS_SOUND_DOSE = 50;
    static final int LOG_NB_EVENTS_HARDENING = 50;
    static final int LOG_NB_EVENTS_LOUDNESS_CODEC = 30;
    static final int LOG_NB_EVENTS_LOUDNESS_CODEC = 30;
@@ -12729,6 +12772,9 @@ public class AudioService extends IAudioService.Stub
            mDynPolicyLogger = new EventLogger(LOG_NB_EVENTS_DYN_POLICY,
            mDynPolicyLogger = new EventLogger(LOG_NB_EVENTS_DYN_POLICY,
            "dynamic policy events (logged when command received by AudioService)");
            "dynamic policy events (logged when command received by AudioService)");
    private final EventLogger mHardeningLogger = new EventLogger(
            LOG_NB_EVENTS_HARDENING, "Hardening enforcement");
    private static final String[] RINGER_MODE_NAMES = new String[] {
    private static final String[] RINGER_MODE_NAMES = new String[] {
            "SILENT",
            "SILENT",
            "VIBRATE",
            "VIBRATE",
@@ -12803,7 +12849,7 @@ public class AudioService extends IAudioService.Stub
            pw.println("\nMessage handler is null");
            pw.println("\nMessage handler is null");
        }
        }
        dumpFlags(pw);
        dumpFlags(pw);
        mHardeningEnforcer.dump(pw);
        mHardeningLogger.dump(pw);
        mMediaFocusControl.dump(pw);
        mMediaFocusControl.dump(pw);
        dumpStreamStates(pw);
        dumpStreamStates(pw);
        dumpVolumeGroups(pw);
        dumpVolumeGroups(pw);
+3 −8
Original line number Original line Diff line number Diff line
@@ -54,8 +54,7 @@ public class HardeningEnforcer {
    final ActivityManager mActivityManager;
    final ActivityManager mActivityManager;
    final PackageManager mPackageManager;
    final PackageManager mPackageManager;


    final EventLogger mEventLogger = new EventLogger(LOG_NB_EVENTS,
    final EventLogger mEventLogger;
            "Hardening enforcement");


    // capacity = 4 for each of the focus request types
    // capacity = 4 for each of the focus request types
    static final SparseArray<String> METRIC_COUNTERS_FOCUS_DENIAL = new SparseArray<>(4);
    static final SparseArray<String> METRIC_COUNTERS_FOCUS_DENIAL = new SparseArray<>(4);
@@ -108,17 +107,13 @@ public class HardeningEnforcer {
    public static final int METHOD_AUDIO_MANAGER_REQUEST_AUDIO_FOCUS = 300;
    public static final int METHOD_AUDIO_MANAGER_REQUEST_AUDIO_FOCUS = 300;


    public HardeningEnforcer(Context ctxt, boolean isAutomotive, AppOpsManager appOps,
    public HardeningEnforcer(Context ctxt, boolean isAutomotive, AppOpsManager appOps,
            PackageManager pm) {
            PackageManager pm, EventLogger logger) {
        mContext = ctxt;
        mContext = ctxt;
        mIsAutomotive = isAutomotive;
        mIsAutomotive = isAutomotive;
        mAppOps = appOps;
        mAppOps = appOps;
        mActivityManager = ctxt.getSystemService(ActivityManager.class);
        mActivityManager = ctxt.getSystemService(ActivityManager.class);
        mPackageManager = pm;
        mPackageManager = pm;
    }
        mEventLogger = logger;

    protected void dump(PrintWriter pw) {
        // log
        mEventLogger.dump(pw);
    }
    }


    /**
    /**
+0 −1
Original line number Original line Diff line number Diff line
@@ -1745,7 +1745,6 @@ public final class PlaybackActivityMonitor
                            eventValues[0] = eventValue;
                            eventValues[0] = eventValue;
                            sEventLogger.enqueue(
                            sEventLogger.enqueue(
                                    new PlayerEvent(piid, PLAYER_UPDATE_MUTED, eventValues));
                                    new PlayerEvent(piid, PLAYER_UPDATE_MUTED, eventValues));

                            final AudioPlaybackConfiguration apc = mPlayers.get(piid);
                            final AudioPlaybackConfiguration apc = mPlayers.get(piid);
                            if (apc == null || !apc.handleMutedEvent(eventValue)) {
                            if (apc == null || !apc.handleMutedEvent(eventValue)) {
                                break;  // do not dispatch
                                break;  // do not dispatch