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

Commit 3ec19943 authored by Simon Dai's avatar Simon Dai Committed by Joseph Pirozzo
Browse files

Add bluetooth prefs for Android Automotive

Add extra to BluetoothMediaBrowserService to redirect to settings when
Bluetooth is not connected

Fixes: 134514401
Test: verify extra in the media session state at startup and after
disconnection.

Change-Id: Iffbd6704eb82e02233b738c7ee06f5c4d8e3c0b5
parent 55bab4f4
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -317,6 +317,16 @@
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
        </service>

        <activity
            android:name=".BluetoothPrefs"
            android:exported="@bool/profile_supported_a2dp_sink"
            android:enabled="@bool/profile_supported_a2dp_sink">
            <intent-filter>
                <action android:name="android.intent.action.APPLICATION_PREFERENCES"/>
            </intent-filter>
        </activity>

        <service
            android:process="@string/process"
            android:name = ".avrcp.AvrcpTargetService"
+1 −0
Original line number Diff line number Diff line
@@ -249,4 +249,5 @@
    <string name="bluetooth_disconnected">Bluetooth audio disconnected"</string>
    <string name="a2dp_sink_mbs_label">Bluetooth Audio</string>
    <string name="bluetooth_opp_file_limit_exceeded">Files bigger than 4GB cannot be transferred</string>
    <string name="bluetooth_connect_action">Connect to Bluetooth</string>
</resources>
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.bluetooth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

/**
 * Activity that routes to Bluetooth settings when launched
 */
public class BluetoothPrefs extends Activity {

    public static final String BLUETOOTH_SETTING_ACTION = "android.settings.BLUETOOTH_SETTINGS";
    public static final String BLUETOOTH_SETTING_CATEGORY = "android.intent.category.DEFAULT";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent launchIntent = new Intent();
        launchIntent.setAction(BLUETOOTH_SETTING_ACTION);
        launchIntent.addCategory(BLUETOOTH_SETTING_CATEGORY);
        startActivity(launchIntent);
        finish();
    }
}
+1 −5
Original line number Diff line number Diff line
@@ -233,11 +233,7 @@ class AvrcpControllerStateMachine extends StateMachine {
        mAddressedPlayer.updateCurrentTrack(null);
        mBrowseTree.mNowPlayingNode.setCached(false);
        BluetoothMediaBrowserService.notifyChanged(mBrowseTree.mNowPlayingNode);
        PlaybackStateCompat.Builder pbb = new PlaybackStateCompat.Builder();
        pbb.setState(PlaybackStateCompat.STATE_ERROR, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN,
                1.0f).setActions(0);
        pbb.setErrorMessage(mService.getString(R.string.bluetooth_disconnected));
        BluetoothMediaBrowserService.notifyChanged(pbb.build());
        BluetoothMediaBrowserService.addressedPlayerChanged(null);
        mService.sBrowseTree.mRootNode.removeChild(
                mBrowseTree.mRootNode);
        BluetoothMediaBrowserService.notifyChanged(mService
+31 −5
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.bluetooth.avrcpcontroller;

import android.app.PendingIntent;
import android.content.Intent;
import android.media.MediaMetadata;
import android.media.browse.MediaBrowser.MediaItem;
import android.os.Bundle;
@@ -29,6 +31,7 @@ import android.util.Log;

import androidx.media.MediaBrowserServiceCompat;

import com.android.bluetooth.BluetoothPrefs;
import com.android.bluetooth.R;

import java.util.ArrayList;
@@ -60,6 +63,12 @@ public class BluetoothMediaBrowserService extends MediaBrowserServiceCompat {
    // Browsing related structures.
    private List<MediaSessionCompat.QueueItem> mMediaQueue = new ArrayList<>();

    // Error messaging extras
    public static final String ERROR_RESOLUTION_ACTION_INTENT =
            "android.media.extras.ERROR_RESOLUTION_ACTION_INTENT";
    public static final String ERROR_RESOLUTION_ACTION_LABEL =
            "android.media.extras.ERROR_RESOLUTION_ACTION_LABEL";

    /**
     * Initialize this BluetoothMediaBrowserService, creating our MediaSessionCompat, MediaPlayer
     * and MediaMetaData, and setting up mechanisms to talk with the AvrcpControllerService.
@@ -76,11 +85,7 @@ public class BluetoothMediaBrowserService extends MediaBrowserServiceCompat {
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mSession.setQueueTitle(getString(R.string.bluetooth_a2dp_sink_queue_name));
        mSession.setQueue(mMediaQueue);
        PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_ERROR,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f).setActions(0);
        playbackStateBuilder.setErrorMessage(getString(R.string.bluetooth_disconnected));
        mSession.setPlaybackState(playbackStateBuilder.build());
        setErrorPlaybackState();
        sBluetoothMediaBrowserService = this;
    }

@@ -94,6 +99,24 @@ public class BluetoothMediaBrowserService extends MediaBrowserServiceCompat {
        }
    }

    private void setErrorPlaybackState() {
        Bundle extras = new Bundle();
        extras.putString(ERROR_RESOLUTION_ACTION_LABEL,
                getString(R.string.bluetooth_connect_action));
        Intent launchIntent = new Intent();
        launchIntent.setAction(BluetoothPrefs.BLUETOOTH_SETTING_ACTION);
        launchIntent.addCategory(BluetoothPrefs.BLUETOOTH_SETTING_CATEGORY);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        extras.putParcelable(ERROR_RESOLUTION_ACTION_INTENT, pendingIntent);
        PlaybackStateCompat errorState = new PlaybackStateCompat.Builder()
                .setErrorMessage(getString(R.string.bluetooth_disconnected))
                .setExtras(extras)
                .setState(PlaybackStateCompat.STATE_ERROR, 0, 0)
                .build();
        mSession.setPlaybackState(errorState);
    }

    @Override
    public synchronized void onLoadChildren(final String parentMediaId,
            final Result<List<MediaBrowserCompat.MediaItem>> result) {
@@ -138,6 +161,9 @@ public class BluetoothMediaBrowserService extends MediaBrowserServiceCompat {

    static synchronized void addressedPlayerChanged(MediaSessionCompat.Callback callback) {
        if (sBluetoothMediaBrowserService != null) {
            if (callback == null) {
                sBluetoothMediaBrowserService.setErrorPlaybackState();
            }
            sBluetoothMediaBrowserService.mSession.setCallback(callback);
        } else {
            Log.w(TAG, "addressedPlayerChanged Unavailable");