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

Commit 78c6cefa authored by Scott Mertz's avatar Scott Mertz Committed by Ethan Chen
Browse files

AudioManager: Add Bluetooth A2DP profile trigger

    - Introduce a new profile trigger for Bluetooth A2DP
      connections.  We can't use the default ACTION_ACL_CONNECTED
      because it would apply the volume settings before routing
      has been established to the A2DP device.
    - Modify AudioService to send an intent after the audio
      route has changed to A2DP to ensure our volume change
      will apply to the correct device.
    - Allows the user to disable the A2DP profile and still
      trigger the profile change.

Fixes JIRA issue: BACON-47

Change-Id: Ib51dac6d79abbaf494b86af4480e293162814459
parent 5bccd054
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -106,6 +106,8 @@ public final class Profile implements Parcelable, Comparable {
        public static final int ON_CONNECT = 0;
        public static final int ON_DISCONNECT = 1;
        public static final int DISABLED = 2;
        public static final int ON_A2DP_CONNECT = 3;
        public static final int ON_A2DP_DISCONNECT = 4;
    }

    public static class ProfileTrigger implements Parcelable {
@@ -264,7 +266,7 @@ public final class Profile implements Parcelable, Comparable {
    public void setTrigger(int type, String id, int state, String name) {
        if (id == null
                || type < TriggerType.WIFI || type > TriggerType.BLUETOOTH
                || state < TriggerState.ON_CONNECT || state > TriggerState.DISABLED) {
                || state < TriggerState.ON_CONNECT || state > TriggerState.ON_A2DP_DISCONNECT) {
            return;
        }

+7 −0
Original line number Diff line number Diff line
@@ -144,6 +144,13 @@ public class AudioManager {
    public static final String MASTER_MUTE_CHANGED_ACTION =
        "android.media.MASTER_MUTE_CHANGED_ACTION";

    /**
     * @hide Broadcast intent when a2dp routing changes
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String A2DP_ROUTE_CHANGED_ACTION =
        "android.media.A2DP_ROUTE_CHANGED_ACTION";

    /**
     * The new vibrate setting for a particular type.
     *
+10 −0
Original line number Diff line number Diff line
@@ -3903,6 +3903,7 @@ public class AudioService extends IAudioService.Stub {
        AudioSystem.setParameters("A2dpSuspended=false");
        mConnectedDevices.put( new Integer(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP),
                address);
        sendA2dpRouteChangedIntent(address, BluetoothProfile.STATE_CONNECTED);
    }

    private void onSendBecomingNoisyIntent() {
@@ -3918,6 +3919,15 @@ public class AudioService extends IAudioService.Stub {
                AudioSystem.DEVICE_STATE_UNAVAILABLE,
                address);
        mConnectedDevices.remove(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP);
        sendA2dpRouteChangedIntent(address, BluetoothProfile.STATE_DISCONNECTED);
    }

    private void sendA2dpRouteChangedIntent(String address, int state) {
        Intent intent = new Intent(AudioManager.A2DP_ROUTE_CHANGED_ACTION);
        BluetoothDevice btDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
        intent.putExtra(BluetoothDevice.EXTRA_DEVICE, btDevice);
        intent.putExtra(BluetoothProfile.EXTRA_STATE, state);
        mContext.sendBroadcast(intent);
    }

    // must be called synchronized on mConnectedDevices
+12 −0
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ package com.android.server;

import android.app.Profile;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiSsid;
import android.net.wifi.WifiInfo;
@@ -62,6 +64,7 @@ public class ProfileTriggerHelper extends BroadcastReceiver {
        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        mIntentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        mIntentFilter.addAction(AudioManager.A2DP_ROUTE_CHANGED_ACTION);
        updateEnabled();

        mContext.getContentResolver().registerContentObserver(
@@ -104,6 +107,15 @@ public class ProfileTriggerHelper extends BroadcastReceiver {
                    ? Profile.TriggerState.ON_CONNECT : Profile.TriggerState.ON_DISCONNECT;
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            checkTriggers(Profile.TriggerType.BLUETOOTH, device.getAddress(), triggerState);
        } else if (action.equals(AudioManager.A2DP_ROUTE_CHANGED_ACTION)) {
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0);
            int triggerState = (state == BluetoothProfile.STATE_CONNECTED)
                    ? Profile.TriggerState.ON_A2DP_CONNECT :
                    Profile.TriggerState.ON_A2DP_DISCONNECT;

            checkTriggers(Profile.TriggerType.BLUETOOTH, device.getAddress(), triggerState);
        }
    }