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

Commit f5e1a0b8 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Get bluetooth A2DP status directly from BT"

parents 148e8f41 15eb7da7
Loading
Loading
Loading
Loading
+31 −20
Original line number Original line Diff line number Diff line
@@ -23,6 +23,9 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.annotation.SystemService;
import android.annotation.SystemService;
import android.app.ActivityThread;
import android.app.ActivityThread;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHearingAid;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.BroadcastReceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Context;
@@ -99,6 +102,7 @@ public class MediaRouter {


        RouteInfo mDefaultAudioVideo;
        RouteInfo mDefaultAudioVideo;
        RouteInfo mBluetoothA2dpRoute;
        RouteInfo mBluetoothA2dpRoute;
        volatile boolean mHasActiveBluetoothDevices;


        RouteInfo mSelectedRoute;
        RouteInfo mSelectedRoute;


@@ -172,14 +176,20 @@ public class MediaRouter {
                    new IntentFilter(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED));
                    new IntentFilter(DisplayManager.ACTION_WIFI_DISPLAY_STATUS_CHANGED));
            appContext.registerReceiver(new VolumeChangeReceiver(),
            appContext.registerReceiver(new VolumeChangeReceiver(),
                    new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION));
                    new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION));
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED);
            intentFilter.addAction(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED);
            appContext.registerReceiver(new BluetoothStateChangedReceiver(), intentFilter);


            mDisplayService.registerDisplayListener(this, mHandler);
            mDisplayService.registerDisplayListener(this, mHandler);


            AudioRoutesInfo newAudioRoutes = null;
            AudioRoutesInfo newAudioRoutes = null;
            try {
            try {
                newAudioRoutes = mAudioService.startWatchingRoutes(mAudioRoutesObserver);
                newAudioRoutes = mAudioService.startWatchingRoutes(mAudioRoutesObserver);
                mHasActiveBluetoothDevices = mAudioService.isBluetoothA2dpOn();
            } catch (RemoteException e) {
            } catch (RemoteException e) {
            }
            }

            if (newAudioRoutes != null) {
            if (newAudioRoutes != null) {
                // This will select the active BT route if there is one and the current
                // This will select the active BT route if there is one and the current
                // selected route is the default system route, or if there is no selected
                // selected route is the default system route, or if there is no selected
@@ -253,7 +263,8 @@ public class MediaRouter {
            }
            }


            if (audioRoutesChanged) {
            if (audioRoutesChanged) {
                Log.v(TAG, "Audio routes updated: " + newRoutes + ", a2dp=" + isBluetoothA2dpOn());
                Log.v(TAG, "Audio routes updated: " + newRoutes + ", hasActiveBTDevices="
                        + mHasActiveBluetoothDevices);
                if (mSelectedRoute == null || mSelectedRoute == mDefaultAudioVideo
                if (mSelectedRoute == null || mSelectedRoute == mDefaultAudioVideo
                        || mSelectedRoute == mBluetoothA2dpRoute) {
                        || mSelectedRoute == mBluetoothA2dpRoute) {
                    if (forceUseDefaultRoute || mBluetoothA2dpRoute == null) {
                    if (forceUseDefaultRoute || mBluetoothA2dpRoute == null) {
@@ -277,15 +288,6 @@ public class MediaRouter {
            return mStreamVolume.get(streamType);
            return mStreamVolume.get(streamType);
        }
        }


        boolean isBluetoothA2dpOn() {
            try {
                return mBluetoothA2dpRoute != null && mAudioService.isBluetoothA2dpOn();
            } catch (RemoteException e) {
                Log.e(TAG, "Error querying Bluetooth A2DP state", e);
                return false;
            }
        }

        void updateDiscoveryRequest() {
        void updateDiscoveryRequest() {
            // What are we looking for today?
            // What are we looking for today?
            int routeTypes = 0;
            int routeTypes = 0;
@@ -394,7 +396,7 @@ public class MediaRouter {
        }
        }


        void updateSelectedRouteForId(String routeId) {
        void updateSelectedRouteForId(String routeId) {
            RouteInfo selectedRoute = isBluetoothA2dpOn()
            RouteInfo selectedRoute = sStatic.mHasActiveBluetoothDevices
                    ? mBluetoothA2dpRoute : mDefaultAudioVideo;
                    ? mBluetoothA2dpRoute : mDefaultAudioVideo;
            final int count = mRoutes.size();
            final int count = mRoutes.size();
            for (int i = 0; i < count; i++) {
            for (int i = 0; i < count; i++) {
@@ -1043,7 +1045,7 @@ public class MediaRouter {
        Log.v(TAG, "Selecting route: " + route);
        Log.v(TAG, "Selecting route: " + route);
        assert(route != null);
        assert(route != null);
        final RouteInfo oldRoute = sStatic.mSelectedRoute;
        final RouteInfo oldRoute = sStatic.mSelectedRoute;
        final RouteInfo currentSystemRoute = sStatic.isBluetoothA2dpOn()
        final RouteInfo currentSystemRoute = sStatic.mHasActiveBluetoothDevices
                ? sStatic.mBluetoothA2dpRoute : sStatic.mDefaultAudioVideo;
                ? sStatic.mBluetoothA2dpRoute : sStatic.mDefaultAudioVideo;
        boolean wasDefaultOrBluetoothRoute = (oldRoute == sStatic.mDefaultAudioVideo
        boolean wasDefaultOrBluetoothRoute = (oldRoute == sStatic.mDefaultAudioVideo
                || oldRoute == sStatic.mBluetoothA2dpRoute);
                || oldRoute == sStatic.mBluetoothA2dpRoute);
@@ -1106,7 +1108,8 @@ public class MediaRouter {


    static void selectDefaultRouteStatic() {
    static void selectDefaultRouteStatic() {
        // TODO: Be smarter about the route types here; this selects for all valid.
        // TODO: Be smarter about the route types here; this selects for all valid.
        if (sStatic.mSelectedRoute != sStatic.mBluetoothA2dpRoute && sStatic.isBluetoothA2dpOn()) {
        if (sStatic.mSelectedRoute != sStatic.mBluetoothA2dpRoute
                && sStatic.mHasActiveBluetoothDevices) {
            selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mBluetoothA2dpRoute, false);
            selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mBluetoothA2dpRoute, false);
        } else {
        } else {
            selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mDefaultAudioVideo, false);
            selectRouteStatic(ROUTE_TYPE_ANY, sStatic.mDefaultAudioVideo, false);
@@ -1443,13 +1446,8 @@ public class MediaRouter {
        if (selectedRoute == sStatic.mBluetoothA2dpRoute ||
        if (selectedRoute == sStatic.mBluetoothA2dpRoute ||
                selectedRoute == sStatic.mDefaultAudioVideo) {
                selectedRoute == sStatic.mDefaultAudioVideo) {
            dispatchRouteVolumeChanged(selectedRoute);
            dispatchRouteVolumeChanged(selectedRoute);
        } else if (sStatic.mBluetoothA2dpRoute != null) {
        } else if (sStatic.mHasActiveBluetoothDevices) {
            try {
            dispatchRouteVolumeChanged(sStatic.mBluetoothA2dpRoute);
                dispatchRouteVolumeChanged(sStatic.mAudioService.isBluetoothA2dpOn() ?
                        sStatic.mBluetoothA2dpRoute : sStatic.mDefaultAudioVideo);
            } catch (RemoteException e) {
                Log.e(TAG, "Error checking Bluetooth A2DP state to report volume change", e);
            }
        } else {
        } else {
            dispatchRouteVolumeChanged(sStatic.mDefaultAudioVideo);
            dispatchRouteVolumeChanged(sStatic.mDefaultAudioVideo);
        }
        }
@@ -3172,4 +3170,17 @@ public class MediaRouter {
            }
            }
        }
        }
    }
    }

    static class BluetoothStateChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED:
                case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED:
                    sStatic.mHasActiveBluetoothDevices =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) != null;
                    break;
            }
        }
    }
}
}
+1 −0
Original line number Original line Diff line number Diff line
@@ -439,6 +439,7 @@ class BluetoothRouteProvider {
            }
            }
        }
        }
    }
    }

    private class BluetoothBroadcastReceiver extends BroadcastReceiver {
    private class BluetoothBroadcastReceiver extends BroadcastReceiver {
        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {