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

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

Merge "audio: Add permission update barrier for tests" into main

parents b9fd5d02 0bae36bf
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1992,6 +1992,7 @@ package android.media {
    method @RequiresPermission(anyOf={android.Manifest.permission.MODIFY_AUDIO_ROUTING, android.Manifest.permission.QUERY_AUDIO_STATE, android.Manifest.permission.MODIFY_AUDIO_SETTINGS_PRIVILEGED}) public boolean isFullVolumeDevice();
    method @RequiresPermission(android.Manifest.permission.CALL_AUDIO_INTERCEPTION) public boolean isPstnCallAudioInterceptable();
    method @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS_PRIVILEGED) public boolean isVolumeControlUsingVolumeGroups();
    method public void permissionUpdateBarrier();
    method @RequiresPermission("android.permission.QUERY_AUDIO_STATE") public int requestAudioFocusForTest(@NonNull android.media.AudioFocusRequest, @NonNull String, int, int);
    method @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS_PRIVILEGED) public void setCsd(float);
    method @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_SETTINGS_PRIVILEGED) public void setNotifAliasRingForTest(boolean);
+18 −0
Original line number Diff line number Diff line
@@ -10126,6 +10126,24 @@ public class AudioManager {
        }
    }

    /**
     * @hide
     * Blocks until permission updates have propagated through the audio system.
     * Only useful in tests, where adoptShellPermissions can change the permission state of
     * an app without the app being killed.
     */
    @TestApi
    @SuppressWarnings("UnflaggedApi") // @TestApi without associated feature.
    public void permissionUpdateBarrier() {
        final IAudioService service = getService();
        try {
            service.permissionUpdateBarrier();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }


    /**
     * @hide
     * Return the list of independent stream types for volume control.
+2 −0
Original line number Diff line number Diff line
@@ -101,6 +101,8 @@ interface IAudioService {

    oneway void portEvent(in int portId, in int event, in @nullable PersistableBundle extras);

    void permissionUpdateBarrier();

    // Java-only methods below.
    void adjustStreamVolume(int streamType, int direction, int flags, String callingPackage);

+2 −0
Original line number Diff line number Diff line
@@ -27,9 +27,11 @@ java_library {
    name: "mediatestutils",
    srcs: [
        "java/com/android/media/mediatestutils/TestUtils.java",
        "java/com/android/media/mediatestutils/PermissionUpdateBarrierRule.java",
    ],
    static_libs: [
        "androidx.concurrent_concurrent-futures",
        "androidx.test.runner",
        "guava",
        "mediatestutils_host",
    ],
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.media.mediatestutils;

import android.content.Context;
import android.media.AudioManager;

import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * Barrier to wait for permission updates to propagate to audioserver, to avoid flakiness when using
 * {@code com.android.compatability.common.util.AdoptShellPermissionsRule}. Note, this rule should
 * <b> always </b> be placed after the adopt permission rule. Don't use rule when changing
 * permission state in {@code @Before}, since that executes after all rules.
 */
public class PermissionUpdateBarrierRule implements TestRule {

    private final Context mContext;

    /**
     * @param context the context to use
     */
    public PermissionUpdateBarrierRule(Context context) {
        mContext = context;
    }

    public PermissionUpdateBarrierRule() {
        this(InstrumentationRegistry.getInstrumentation().getContext());
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                mContext.getSystemService(AudioManager.class).permissionUpdateBarrier();
                base.evaluate();
            }
        };
    }
}
Loading