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

Commit 96a52673 authored by Marie Janssen's avatar Marie Janssen Committed by android-build-merger
Browse files

Merge "AVRCP: Catch Intent.startService SecurityException"

am: f6e63494

Change-Id: I2e5500be14f37b55108aff87b483305c35d724ab
parents 530de6f5 f6e63494
Loading
Loading
Loading
Loading
+14 −11
Original line number Diff line number Diff line
@@ -256,8 +256,10 @@ public final class Avrcp {
        mAvrcpMediaRsp = new AvrcpMediaRsp();
        mMediaPlayerInfoList = new ArrayList<MediaPlayerInfo>();
        mBrowsePlayerInfoList = new ArrayList<BrowsePlayerInfo>();
        if (mMediaSessionManager != null) {
            mMediaSessionManager.addOnActiveSessionsChangedListener(mActiveSessionListener, null,
                    mHandler);
        }
        mPackageManager = mContext.getApplicationContext().getPackageManager();

        /* create object to communicate with addressed player */
@@ -1784,26 +1786,25 @@ public final class Avrcp {

    /* initializing media player info list and prepare media player response object */
    private void buildMediaPlayersList() {

        initMediaPlayersInfoList();
        mMPLObj = prepareMediaPlayerRspObj();

        if (mMPLObj.mNumItems > 0) {
            // Setting player which is on the Top (id=1) of the list as an Addressed player
            /* Set the first one as the Addressed Player */
            updateCurrentController(1, -1);
        } else {
            Log.i(TAG, "No players available in the media players list");
            /* If there are no players available in the media players list, meaning none of the
             * players are yet open, so no active players are in the list. But in this case none
             * of the AVRCP player related commands can be satisfied. So, launching first available
             * browsable player service to avail atleast one player to do AVRCP operations. */
            /* Starting media player service */
            /* No players have browsing started. Start one so we can handle commands. */
            if ((mBrowsePlayerInfoList != null) && (mBrowsePlayerInfoList.size()!=0)) {
                BrowsePlayerInfo player = mBrowsePlayerInfoList.get(0);
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(player.packageName, player.serviceClass));
                Log.i(TAG, "Starting service:" + player.packageName + ", " + player.serviceClass);
                try {
                    mContext.startService(intent);
                } catch (SecurityException ex) {
                    Log.e(TAG, "Can't start " + player.serviceClass + ": " + ex.getMessage());
                }
            } else {
                Log.e(TAG, "Opening player to support AVRCP operations failed, " +
                        "No browsable players available!");
@@ -1832,7 +1833,9 @@ public final class Avrcp {
    private List<android.media.session.MediaController> getActiveControllersList() {
        List<android.media.session.MediaController> controllersList =
            new ArrayList<android.media.session.MediaController>();
        if (mMediaSessionManager != null) {
            controllersList = mMediaSessionManager.getActiveSessions(null);
        }
        Log.i(TAG, "getActiveControllersList: " + controllersList.size() + " controllers");
        return controllersList;
    }
+72 −0
Original line number Diff line number Diff line
package com.android.bluetooth.avrcp;

import android.bluetooth.BluetoothAvrcp;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.media.session.MediaSession;
import android.media.session.MediaSession.QueueItem;
import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.AudioManager;
import android.media.session.MediaSessionManager;
import android.os.Bundle;
import android.test.AndroidTestCase;
import android.util.Log;

import java.nio.ByteBuffer;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

import static org.mockito.Mockito.isA;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AvrcpTest extends AndroidTestCase {

    public void testCanStart() {
        Avrcp a = Avrcp.make(getContext());
    }

    public void testFailedBrowseStart() {
        Context mockContext = mock(Context.class);
        AudioManager mockAudioManager = mock(AudioManager.class);
        PackageManager mockPackageManager = mock(PackageManager.class);

        when(mockAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)).thenReturn(100);

        when(mockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mockAudioManager);

        when(mockContext.getApplicationContext()).thenReturn(mockContext);
        when(mockContext.getPackageManager()).thenReturn(mockPackageManager);


        // Call to get the BrowsableMediaPlayers
        // We must return at least one to try to startService
        List<ResolveInfo> resInfos = new ArrayList<ResolveInfo>();

        ServiceInfo fakeService = new ServiceInfo();
        fakeService.name = ".browse.MediaBrowserService";
        fakeService.packageName = "com.test.android.fake";

        ResolveInfo fakePackage = new ResolveInfo();
        fakePackage.serviceInfo = fakeService;
        fakePackage.nonLocalizedLabel = "Fake Package";
        resInfos.add(fakePackage);
        when(mockPackageManager.queryIntentServices(isA(Intent.class), anyInt())).thenReturn(resInfos);

        when(mockContext.startService(isA(Intent.class))).thenThrow(new SecurityException("test"));

        // Make calls start() which calls buildMediaPlayersList() which should
        // try to start the service?
        try {
            Avrcp a = Avrcp.make(mockContext);
        } catch (SecurityException e) {
            fail("Threw SecurityException instead of protecting against it: " + e.toString());
        }
    }
}