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

Commit e7ecdd65 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes I74dceab1,I26339bed into main

* changes:
  Do not use registerStateChangeCallback from the app
  Use a ForwardingServiceListener in BluetoothVolumeControl
parents 58271e92 23c4572a
Loading
Loading
Loading
Loading
+12 −16
Original line number Original line Diff line number Diff line
@@ -36,13 +36,10 @@ import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothStateChangeCallback;
import android.content.Context;
import android.content.Context;
import android.os.Handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;
import android.util.Log;
import android.util.Pair;
import android.util.Pair;


@@ -810,11 +807,16 @@ public class MediaControlGattService implements MediaControlGattServiceInterface
        }
        }
    }
    }


    private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
    private final AdapterService.BluetoothStateCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
            new AdapterService.BluetoothStateCallback() {
                public void onBluetoothStateChange(boolean up) {
                public void onBluetoothStateChange(int prevState, int newState) {
                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                    if (DBG) {
                    if (up) {
                        Log.d(
                                TAG,
                                "onBluetoothStateChange: state="
                                        + BluetoothAdapter.nameForState(newState));
                    }
                    if (newState == BluetoothAdapter.STATE_ON) {
                        restoreCccValuesForStoredDevices();
                        restoreCccValuesForStoredDevices();
                    }
                    }
                }
                }
@@ -1102,14 +1104,8 @@ public class MediaControlGattService implements MediaControlGattServiceInterface
        mAdapterService =  Objects.requireNonNull(AdapterService.getAdapterService(),
        mAdapterService =  Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService shouldn't be null when creating MediaControlCattService");
                "AdapterService shouldn't be null when creating MediaControlCattService");


        IBluetoothManager mgr = BluetoothAdapter.getDefaultAdapter().getBluetoothManager();
        mAdapterService.registerBluetoothStateCallback(
        if (mgr != null) {
                mContext.getMainExecutor(), mBluetoothStateChangeCallback);
            try {
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }


        mEventLogger =
        mEventLogger =
                new BluetoothEventLogger(
                new BluetoothEventLogger(
+13 −16
Original line number Original line Diff line number Diff line
@@ -27,13 +27,10 @@ import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothManager;
import android.bluetooth.IBluetoothStateChangeCallback;
import android.content.Context;
import android.content.Context;
import android.os.Handler;
import android.os.Handler;
import android.os.Looper;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.util.Log;
import android.util.Log;


import com.android.bluetooth.BluetoothEventLogger;
import com.android.bluetooth.BluetoothEventLogger;
@@ -259,14 +256,9 @@ public class TbsGatt {
        mContext = tbsService;
        mContext = tbsService;
        mAdapterService =  Objects.requireNonNull(AdapterService.getAdapterService(),
        mAdapterService =  Objects.requireNonNull(AdapterService.getAdapterService(),
                "AdapterService shouldn't be null when creating MediaControlCattService");
                "AdapterService shouldn't be null when creating MediaControlCattService");
        IBluetoothManager mgr = BluetoothAdapter.getDefaultAdapter().getBluetoothManager();

        if (mgr != null) {
        mAdapterService.registerBluetoothStateCallback(
            try {
                mContext.getMainExecutor(), mBluetoothStateChangeCallback);
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }


        mBearerProviderNameCharacteristic = new GattCharacteristic(UUID_BEARER_PROVIDER_NAME,
        mBearerProviderNameCharacteristic = new GattCharacteristic(UUID_BEARER_PROVIDER_NAME,
                BluetoothGattCharacteristic.PROPERTY_READ
                BluetoothGattCharacteristic.PROPERTY_READ
@@ -987,11 +979,16 @@ public class TbsGatt {
        }
        }
    }
    }


    private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
    private final AdapterService.BluetoothStateCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
            new AdapterService.BluetoothStateCallback() {
                public void onBluetoothStateChange(boolean up) {
                public void onBluetoothStateChange(int prevState, int newState) {
                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                    if (DBG) {
                    if (up) {
                        Log.d(
                                TAG,
                                "onBluetoothStateChange: state="
                                        + BluetoothAdapter.nameForState(newState));
                    }
                    if (newState == BluetoothAdapter.STATE_ON) {
                        restoreCccValuesForStoredDevices();
                        restoreCccValuesForStoredDevices();
                    }
                    }
                }
                }
+43 −51
Original line number Original line Diff line number Diff line
@@ -64,6 +64,48 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    private CloseGuard mCloseGuard;
    private CloseGuard mCloseGuard;
    private final Map<Callback, Executor> mCallbackExecutorMap = new HashMap<>();
    private final Map<Callback, Executor> mCallbackExecutorMap = new HashMap<>();


    private final class VolumeControlServiceListener extends ForwardingServiceListener {
        VolumeControlServiceListener(ServiceListener listener) {
            super(listener);
        }

        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            try {
                if (profile == VOLUME_CONTROL) {
                    // re-register the service-to-app callback
                    synchronized (mCallbackExecutorMap) {
                        if (!mCallbackExecutorMap.isEmpty()) {
                            try {
                                final IBluetoothVolumeControl service = getService();
                                if (service != null) {
                                    final SynchronousResultReceiver<Integer> recv =
                                            SynchronousResultReceiver.get();
                                    service.registerCallback(mCallback, mAttributionSource, recv);
                                    recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
                                }
                            } catch (RemoteException e) {
                                Log.e(
                                        TAG,
                                        "onBluetoothServiceUp: Failed to register"
                                                + "Volume Control callback",
                                        e);
                            } catch (TimeoutException e) {
                                Log.e(
                                        TAG,
                                        e.toString()
                                                + "\n"
                                                + Log.getStackTraceString(new Throwable()));
                            }
                        }
                    }
                }
            } finally {
                super.onServiceConnected(profile, proxy);
            }
        }
    }

    /**
    /**
     * This class provides a callback that is invoked when volume offset value changes on
     * This class provides a callback that is invoked when volume offset value changes on
     * the remote device.
     * the remote device.
@@ -142,37 +184,6 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
                }
                }
            };
            };


    @SuppressLint("AndroidFrameworkBluetoothPermission")
    private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
            new IBluetoothStateChangeCallback.Stub() {
                public void onBluetoothStateChange(boolean up) {
                    if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                    if (!up) {
                        return;
                    }
                    // re-register the service-to-app callback
                    synchronized (mCallbackExecutorMap) {
                        if (!mCallbackExecutorMap.isEmpty()) {
                            try {
                                final IBluetoothVolumeControl service = getService();
                                if (service != null) {
                                    final SynchronousResultReceiver<Integer> recv =
                                                    SynchronousResultReceiver.get();
                                    service.registerCallback(mCallback, mAttributionSource, recv);
                                    recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
                                }
                            } catch (RemoteException e) {
                                Log.e(TAG, "onBluetoothServiceUp: Failed to register"
                                        + "Volume Control callback", e);
                            } catch (TimeoutException e) {
                                Log.e(TAG, e.toString() + "\n"
                                        + Log.getStackTraceString(new Throwable()));
                            }
                        }
                    }
                }
            };

    /**
    /**
     * Create a BluetoothVolumeControl proxy object for interacting with the local
     * Create a BluetoothVolumeControl proxy object for interacting with the local
     * Bluetooth Volume Control service.
     * Bluetooth Volume Control service.
@@ -181,18 +192,7 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
            BluetoothAdapter adapter) {
            BluetoothAdapter adapter) {
        mAdapter = adapter;
        mAdapter = adapter;
        mAttributionSource = adapter.getAttributionSource();
        mAttributionSource = adapter.getAttributionSource();
        mProfileConnector.connect(context, listener);
        mProfileConnector.connect(context, new VolumeControlServiceListener(listener));

        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
                // IBluetoothManager run in the system server
                throw e.rethrowFromSystemServer();
            }
        }


        mCloseGuard = new CloseGuard();
        mCloseGuard = new CloseGuard();
        mCloseGuard.open("close");
        mCloseGuard.open("close");
@@ -217,14 +217,6 @@ public final class BluetoothVolumeControl implements BluetoothProfile, AutoClose
    public void close() {
    public void close() {
        if (VDBG) log("close()");
        if (VDBG) log("close()");


        IBluetoothManager mgr = mAdapter.getBluetoothManager();
        if (mgr != null) {
            try {
                mgr.unregisterStateChangeCallback(mBluetoothStateChangeCallback);
            } catch (RemoteException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            }
        }
        mProfileConnector.disconnect();
        mProfileConnector.disconnect();
    }
    }