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

Commit 23c4572a authored by David Duarte's avatar David Duarte
Browse files

Do not use registerStateChangeCallback from the app

This method is made for apps/framework to get Bluetooth state update.
The expected flow is: app -> service -> framework.

Calling this in the app means that we are doing: app -> service -> app.

This commit instead remove going to the service by registering a local
callback in the app instead.

Bug: 262605980
Test: m com.android.btservices
Change-Id: I74dceab111a66bbb202953a93aa028dac225b087
parent 1d50e247
Loading
Loading
Loading
Loading
+12 −16
Original line number Original line Diff line number Diff line
@@ -37,13 +37,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();
                    }
                    }
                }
                }