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

Commit 188eee04 authored by Weilin Xu's avatar Weilin Xu Committed by Automerger Merge Worker
Browse files

Merge "Disable sending tuner callback to non current user" into udc-dev am:...

Merge "Disable sending tuner callback to non current user" into udc-dev am: 4ef0d056 am: 91f2911a

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/22825096



Change-Id: I7872fb3d0de505c61ea0dca2844c9d34107d07e4
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 8b5b29c3 91f2911a
Loading
Loading
Loading
Loading
+22 −5
Original line number Original line Diff line number Diff line
@@ -58,7 +58,7 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    }
    }


    @Test
    @Test
    public void isCurrentUser_forCurrentUser_returnsFalse() {
    public void isCurrentOrSystemUser_forCurrentUser_returnsFalse() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);


        assertWithMessage("Current user")
        assertWithMessage("Current user")
@@ -66,7 +66,7 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    }
    }


    @Test
    @Test
    public void isCurrentUser_forNonCurrentUser_returnsFalse() {
    public void isCurrentOrSystemUser_forNonCurrentUser_returnsFalse() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_2);
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_2);


        assertWithMessage("Non-current user")
        assertWithMessage("Non-current user")
@@ -74,7 +74,8 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    }
    }


    @Test
    @Test
    public void isCurrentUser_forSystemUser_returnsTrue() {
    public void isCurrentOrSystemUser_forSystemUser_returnsTrue() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM);
        when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM);


        assertWithMessage("System user")
        assertWithMessage("System user")
@@ -82,10 +83,26 @@ public final class RadioServiceUserControllerTest extends ExtendedRadioMockitoTe
    }
    }


    @Test
    @Test
    public void isCurrentUser_withActivityManagerFails_returnsFalse() {
    public void isCurrentOrSystemUser_withActivityManagerFailure_returnsFalse() {
        doThrow(new RuntimeException()).when(() -> ActivityManager.getCurrentUser());
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doThrow(new RuntimeException()).when(ActivityManager::getCurrentUser);


        assertWithMessage("User when activity manager fails")
        assertWithMessage("User when activity manager fails")
                .that(RadioServiceUserController.isCurrentOrSystemUser()).isFalse();
                .that(RadioServiceUserController.isCurrentOrSystemUser()).isFalse();
    }
    }

    @Test
    public void getCurrentUser() {
        assertWithMessage("Current user")
                .that(RadioServiceUserController.getCurrentUser()).isEqualTo(USER_ID_1);
    }

    @Test
    public void getCurrentUser_withActivityManagerFailure_returnsUserNull() {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doThrow(new RuntimeException()).when(ActivityManager::getCurrentUser);

        assertWithMessage("Current user when activity manager fails")
                .that(RadioServiceUserController.getCurrentUser()).isEqualTo(UserHandle.USER_NULL);
    }
}
}
+41 −2
Original line number Original line Diff line number Diff line
@@ -47,9 +47,11 @@ import android.hardware.radio.ProgramList;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioTuner;
import android.hardware.radio.RadioTuner;
import android.os.Binder;
import android.os.ParcelableException;
import android.os.ParcelableException;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.os.ServiceSpecificException;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.ArraySet;


@@ -73,6 +75,8 @@ import java.util.Set;
 */
 */
public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {
public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {


    private static final int USER_ID_1 = 11;
    private static final int USER_ID_2 = 12;
    private static final VerificationWithTimeout CALLBACK_TIMEOUT =
    private static final VerificationWithTimeout CALLBACK_TIMEOUT =
            timeout(/* millis= */ 200);
            timeout(/* millis= */ 200);
    private static final int SIGNAL_QUALITY = 90;
    private static final int SIGNAL_QUALITY = 90;
@@ -109,7 +113,10 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {
            SIGNAL_QUALITY);
            SIGNAL_QUALITY);


    // Mocks
    // Mocks
    @Mock private IBroadcastRadio mBroadcastRadioMock;
    @Mock
    private UserHandle mUserHandleMock;
    @Mock
    private IBroadcastRadio mBroadcastRadioMock;
    private android.hardware.radio.ITunerCallback[] mAidlTunerCallbackMocks;
    private android.hardware.radio.ITunerCallback[] mAidlTunerCallbackMocks;


    // RadioModule under test
    // RadioModule under test
@@ -124,14 +131,18 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {


    @Override
    @Override
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
        builder.spyStatic(RadioServiceUserController.class).spyStatic(CompatChanges.class);
        builder.spyStatic(RadioServiceUserController.class).spyStatic(CompatChanges.class)
                .spyStatic(Binder.class);
    }
    }


    @Before
    @Before
    public void setup() throws Exception {
    public void setup() throws Exception {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doReturn(true).when(() -> CompatChanges.isChangeEnabled(
        doReturn(true).when(() -> CompatChanges.isChangeEnabled(
                eq(ConversionUtils.RADIO_U_VERSION_REQUIRED), anyInt()));
                eq(ConversionUtils.RADIO_U_VERSION_REQUIRED), anyInt()));
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        doReturn(USER_ID_1).when(() -> RadioServiceUserController.getCurrentUser());
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());


        mRadioModule = new RadioModule(mBroadcastRadioMock,
        mRadioModule = new RadioModule(mBroadcastRadioMock,
                AidlTestUtils.makeDefaultModuleProperties());
                AidlTestUtils.makeDefaultModuleProperties());
@@ -420,6 +431,21 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {
                .onCurrentProgramInfoChanged(tuneInfo);
                .onCurrentProgramInfoChanged(tuneInfo);
    }
    }


    @Test
    public void tune_forSystemUser() throws Exception {
        when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM);
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        ProgramSelector initialSel = AidlTestUtils.makeFmSelector(AM_FM_FREQUENCY_LIST[1]);
        RadioManager.ProgramInfo tuneInfo =
                AidlTestUtils.makeProgramInfo(initialSel, SIGNAL_QUALITY);
        openAidlClients(/* numClients= */ 1);

        mTunerSessions[0].tune(initialSel);

        verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT).onCurrentProgramInfoChanged(tuneInfo);
    }

    @Test
    @Test
    public void tune_withUnknownErrorFromHal_fails() throws Exception {
    public void tune_withUnknownErrorFromHal_fails() throws Exception {
        openAidlClients(/* numClients= */ 1);
        openAidlClients(/* numClients= */ 1);
@@ -1135,6 +1161,19 @@ public final class TunerSessionTest extends ExtendedRadioMockitoTestCase {
                .that(thrown).hasMessageThat().contains(exceptionMessage);
                .that(thrown).hasMessageThat().contains(exceptionMessage);
    }
    }


    @Test
    public void onCurrentProgramInfoChanged_withNoncurrentUser_doesNotInvokeCallback()
            throws Exception {
        openAidlClients(1);
        doReturn(USER_ID_2).when(() -> RadioServiceUserController.getCurrentUser());

        mHalTunerCallback.onCurrentProgramInfoChanged(AidlTestUtils.makeHalProgramInfo(
                AidlTestUtils.makeHalFmSelector(/* freq= */ 97300), SIGNAL_QUALITY));

        verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT.times(0))
                .onCurrentProgramInfoChanged(any());
    }

    @Test
    @Test
    public void onAntennaStateChange_forTunerCallback() throws Exception {
    public void onAntennaStateChange_forTunerCallback() throws Exception {
        int numSessions = 3;
        int numSessions = 3;
+41 −3
Original line number Original line Diff line number Diff line
@@ -46,8 +46,10 @@ import android.hardware.radio.ProgramList;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.ProgramSelector;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioManager;
import android.hardware.radio.RadioTuner;
import android.hardware.radio.RadioTuner;
import android.os.Binder;
import android.os.ParcelableException;
import android.os.ParcelableException;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArrayMap;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.ArraySet;


@@ -74,6 +76,8 @@ import java.util.Map;
@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.class)
public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {


    private static final int USER_ID_1 = 11;
    private static final int USER_ID_2 = 12;
    private static final VerificationWithTimeout CALLBACK_TIMEOUT =
    private static final VerificationWithTimeout CALLBACK_TIMEOUT =
            timeout(/* millis= */ 200);
            timeout(/* millis= */ 200);
    private static final int SIGNAL_QUALITY = 1;
    private static final int SIGNAL_QUALITY = 1;
@@ -94,18 +98,25 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
    private ProgramInfo mHalCurrentInfo;
    private ProgramInfo mHalCurrentInfo;
    private TunerSession[] mTunerSessions;
    private TunerSession[] mTunerSessions;


    @Mock private IBroadcastRadio mBroadcastRadioMock;
    @Mock
    @Mock ITunerSession mHalTunerSessionMock;
    private UserHandle mUserHandleMock;
    @Mock
    private IBroadcastRadio mBroadcastRadioMock;
    @Mock
    ITunerSession mHalTunerSessionMock;
    private android.hardware.radio.ITunerCallback[] mAidlTunerCallbackMocks;
    private android.hardware.radio.ITunerCallback[] mAidlTunerCallbackMocks;


    @Override
    @Override
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
    protected void initializeSession(StaticMockitoSessionBuilder builder) {
        builder.spyStatic(RadioServiceUserController.class);
        builder.spyStatic(RadioServiceUserController.class).spyStatic(Binder.class);
    }
    }


    @Before
    @Before
    public void setup() throws Exception {
    public void setup() throws Exception {
        when(mUserHandleMock.getIdentifier()).thenReturn(USER_ID_1);
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        doReturn(USER_ID_1).when(() -> RadioServiceUserController.getCurrentUser());


        mRadioModule = new RadioModule(mBroadcastRadioMock,
        mRadioModule = new RadioModule(mBroadcastRadioMock,
                TestUtils.makeDefaultModuleProperties());
                TestUtils.makeDefaultModuleProperties());
@@ -386,6 +397,20 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
                .that(thrown).hasMessageThat().contains(Result.toString(Result.UNKNOWN_ERROR));
                .that(thrown).hasMessageThat().contains(Result.toString(Result.UNKNOWN_ERROR));
    }
    }


    @Test
    public void tune_forSystemUser() throws Exception {
        when(mUserHandleMock.getIdentifier()).thenReturn(UserHandle.USER_SYSTEM);
        doReturn(mUserHandleMock).when(() -> Binder.getCallingUserHandle());
        doReturn(true).when(() -> RadioServiceUserController.isCurrentOrSystemUser());
        ProgramSelector initialSel = TestUtils.makeFmSelector(AM_FM_FREQUENCY_LIST[1]);
        RadioManager.ProgramInfo tuneInfo = TestUtils.makeProgramInfo(initialSel, SIGNAL_QUALITY);
        openAidlClients(/* numClients= */ 1);

        mTunerSessions[0].tune(initialSel);

        verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT).onCurrentProgramInfoChanged(tuneInfo);
    }

    @Test
    @Test
    public void step_withDirectionUp() throws Exception {
    public void step_withDirectionUp() throws Exception {
        long initFreq = AM_FM_FREQUENCY_LIST[1];
        long initFreq = AM_FM_FREQUENCY_LIST[1];
@@ -857,6 +882,19 @@ public final class TunerSessionHidlTest extends ExtendedRadioMockitoTestCase {
                .that(thrown).hasMessageThat().contains(exceptionMessage);
                .that(thrown).hasMessageThat().contains(exceptionMessage);
    }
    }


    @Test
    public void onCurrentProgramInfoChanged_withNoncurrentUser_doesNotInvokeCallback()
            throws Exception {
        openAidlClients(1);
        doReturn(USER_ID_2).when(() -> RadioServiceUserController.getCurrentUser());

        mHalTunerCallback.onCurrentProgramInfoChanged(TestUtils.makeHalProgramInfo(
                TestUtils.makeHalFmSelector(/* freq= */ 97300), SIGNAL_QUALITY));

        verify(mAidlTunerCallbackMocks[0], CALLBACK_TIMEOUT.times(0))
                .onCurrentProgramInfoChanged(any());
    }

    @Test
    @Test
    public void onConfigFlagUpdated_forTunerCallback() throws Exception {
    public void onConfigFlagUpdated_forTunerCallback() throws Exception {
        int numSessions = 3;
        int numSessions = 3;
+12 −6
Original line number Original line Diff line number Diff line
@@ -39,18 +39,24 @@ public final class RadioServiceUserController {
     */
     */
    public static boolean isCurrentOrSystemUser() {
    public static boolean isCurrentOrSystemUser() {
        int callingUser = Binder.getCallingUserHandle().getIdentifier();
        int callingUser = Binder.getCallingUserHandle().getIdentifier();
        return callingUser == getCurrentUser() || callingUser == UserHandle.USER_SYSTEM;
    }

    /**
     * Get current foreground user for Broadcast Radio Service
     *
     * @return foreground user id.
     */
    public static int getCurrentUser() {
        final long identity = Binder.clearCallingIdentity();
        final long identity = Binder.clearCallingIdentity();
        int userId = UserHandle.USER_NULL;
        try {
        try {
            int currentUser = ActivityManager.getCurrentUser();
            userId = ActivityManager.getCurrentUser();
            if (callingUser != currentUser && callingUser != UserHandle.USER_SYSTEM) {
                return false;
            }
            return true;
        } catch (RuntimeException e) {
        } catch (RuntimeException e) {
            // Activity manager not running, nothing we can do assume user 0.
            // Activity manager not running, nothing we can do assume user 0.
        } finally {
        } finally {
            Binder.restoreCallingIdentity(identity);
            Binder.restoreCallingIdentity(identity);
        }
        }
        return false;
        return userId;
    }
    }
}
}
+7 −0
Original line number Original line Diff line number Diff line
@@ -36,11 +36,13 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder;
import android.os.Looper;
import android.os.Looper;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.ArraySet;
import android.util.IndentingPrintWriter;
import android.util.IndentingPrintWriter;


import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.broadcastradio.RadioServiceUserController;
import com.android.server.utils.Slogf;
import com.android.server.utils.Slogf;


import java.util.ArrayList;
import java.util.ArrayList;
@@ -419,8 +421,13 @@ final class RadioModule {


    @GuardedBy("mLock")
    @GuardedBy("mLock")
    private void fanoutAidlCallbackLocked(AidlCallbackRunnable runnable) {
    private void fanoutAidlCallbackLocked(AidlCallbackRunnable runnable) {
        int currentUserId = RadioServiceUserController.getCurrentUser();
        List<TunerSession> deadSessions = null;
        List<TunerSession> deadSessions = null;
        for (int i = 0; i < mAidlTunerSessions.size(); i++) {
        for (int i = 0; i < mAidlTunerSessions.size(); i++) {
            if (mAidlTunerSessions.valueAt(i).mUserId != currentUserId
                    && mAidlTunerSessions.valueAt(i).mUserId != UserHandle.USER_SYSTEM) {
                continue;
            }
            try {
            try {
                runnable.run(mAidlTunerSessions.valueAt(i).mCallback,
                runnable.run(mAidlTunerSessions.valueAt(i).mCallback,
                        mAidlTunerSessions.valueAt(i).getUid());
                        mAidlTunerSessions.valueAt(i).getUid());
Loading