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

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

Merge changes Ibad098f5,I1bd11456,I3cd52493,Ib562cbb1 into main

* changes:
  ProfileService: remove adapter and set binder as final
  Le callback: Pass gattService from app directly
  AdvertiseManager: Remove impossible null case
  BluetoothLeAdvertiser: clean callback post
parents 54cb6231 54c0a196
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ package android.bluetooth.le;
 * @hide
 */
oneway interface IAdvertisingSetCallback {
  void onAdvertisingSetStarted(in int advertiserId, in int tx_power, in int status);
  void onAdvertisingSetStarted(in IBinder gattBinder, in int advertiserId, in int tx_power, in int status);
  void onOwnAddressRead(in int advertiserId, in int addressType, in String address);
  void onAdvertisingSetStopped(in int advertiserId);
  void onAdvertisingEnabled(in int advertiserId, in boolean enable, in int status);
+6 −17
Original line number Diff line number Diff line
@@ -49,8 +49,7 @@ public abstract class ProfileService extends ContextWrapper {
        void cleanup();
    }

    private BluetoothAdapter mAdapter;
    private IProfileServiceBinder mBinder;
    private final IProfileServiceBinder mBinder;
    private final String mName;
    private AdapterService mAdapterService;
    private boolean mProfileStarted = false;
@@ -69,7 +68,7 @@ public abstract class ProfileService extends ContextWrapper {
    }

    /**
     * Called in {@link #onCreate()} to init binder interface for this profile service
     * Called in ProfileService constructor to init binder interface for this profile service
     *
     * @return initialized binder interface for this profile service
     */
@@ -107,12 +106,11 @@ public abstract class ProfileService extends ContextWrapper {
        if (DBG) {
            Log.d(mName, "Service created");
        }
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mBinder = initBinder();
        mBinder = requireNonNull(initBinder(), "Binder null is not allowed for " + mName);
    }

    IBinder getBinder() {
        requireNonNull(mBinder, "Binder is null. onCreate need to be called first");
    /** return the binder of the profile */
    public IBinder getBinder() {
        return mBinder;
    }

@@ -211,11 +209,6 @@ public abstract class ProfileService extends ContextWrapper {
    @VisibleForTesting
    public void doStart() {
        Log.v(mName, "doStart");
        if (mAdapter == null) {
            Log.w(mName, "Can't start profile service: device does not have BT");
            return;
        }

        mAdapterService = AdapterService.getAdapterService();
        if (mAdapterService == null) {
            Log.w(mName, "Could not add this profile because AdapterService is null.");
@@ -254,10 +247,6 @@ public abstract class ProfileService extends ContextWrapper {
            mAdapterService.removeProfile(this);
        }
        cleanup();
        if (mBinder != null) {
        mBinder.cleanup();
            mBinder = null;
        }
        mAdapter = null;
    }
}
+8 −5
Original line number Diff line number Diff line
@@ -184,7 +184,8 @@ public class AdvertiseManager {
            AppAdvertiseStats.recordAdvertiseErrorCount(LE_ADV_ERROR_ON_START_COUNT);
        }

        callback.onAdvertisingSetStarted(advertiserId, txPower, status);
        IBinder gattBinder = mService.getBinder();
        callback.onAdvertisingSetStarted(gattBinder, advertiserId, txPower, status);
    }

    void onAdvertisingEnabled(int advertiserId, boolean enable, int status) throws Exception {
@@ -221,8 +222,9 @@ public class AdvertiseManager {
                        != AdvertisingSetParameters.ADDRESS_TYPE_RANDOM_NON_RESOLVABLE) {
            Log.w(TAG, "Cannot advertise an isolated GATT server using a resolvable address");
            try {
                IBinder gattBinder = mService.getBinder();
                callback.onAdvertisingSetStarted(
                        0x00, 0x00, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
                        gattBinder, 0x00, 0x00, AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR);
            } catch (RemoteException exception) {
                Log.e(TAG, "Failed to callback:" + Log.getStackTraceString(exception));
            }
@@ -231,7 +233,7 @@ public class AdvertiseManager {

        int appUid = Binder.getCallingUid();
        String packageName = null;
        if (mService != null && mService.getPackageManager() != null) {
        if (mService.getPackageManager() != null) {
            packageName = mService.getPackageManager().getNameForUid(appUid);
        }
        if (packageName == null) {
@@ -279,8 +281,9 @@ public class AdvertiseManager {
        } catch (IllegalArgumentException e) {
            try {
                binder.unlinkToDeath(deathRecipient, 0);
                callback.onAdvertisingSetStarted(0x00, 0x00,
                        AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE);
                IBinder gattBinder = mService.getBinder();
                callback.onAdvertisingSetStarted(
                        gattBinder, 0x00, 0x00, AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE);
            } catch (RemoteException exception) {
                Log.e(TAG, "Failed to callback:" + Log.getStackTraceString(exception));
            }
+3 −2
Original line number Diff line number Diff line
@@ -50,13 +50,14 @@ public final class AdvertisingSet {
    private int mAdvertiserId;
    private AttributionSource mAttributionSource;

    /* package */ AdvertisingSet(
    AdvertisingSet(
            IBluetoothGatt gatt,
            int advertiserId,
            BluetoothAdapter bluetoothAdapter,
            AttributionSource attributionSource) {
        mAdvertiserId = advertiserId;
        mAttributionSource = attributionSource;
        mGatt = requireNonNull(bluetoothAdapter.getBluetoothGatt(), "Failed to get Bluetooth gatt");
        mGatt = requireNonNull(gatt, "Bluetooth gatt cannot be null");
    }

    /* package */ void setAdvertiserId(int advertiserId) {
+47 −76
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.bluetooth.annotations.RequiresBluetoothAdvertisePermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
import android.content.AttributionSource;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.ParcelUuid;
import android.os.RemoteException;
@@ -750,11 +751,10 @@ public final class BluetoothLeAdvertiser {
    IAdvertisingSetCallback wrap(AdvertisingSetCallback callback, Handler handler) {
        return new IAdvertisingSetCallback.Stub() {
            @Override
            public void onAdvertisingSetStarted(int advertiserId, int txPower, int status) {
            public void onAdvertisingSetStarted(
                    IBinder gattBinder, int advertiserId, int txPower, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            if (status != AdvertisingSetCallback.ADVERTISE_SUCCESS) {
                                callback.onAdvertisingSetStarted(null, 0, status);
                                mCallbackWrappers.remove(callback);
@@ -763,125 +763,96 @@ public final class BluetoothLeAdvertiser {

                            AdvertisingSet advertisingSet =
                                    new AdvertisingSet(
                                            IBluetoothGatt.Stub.asInterface(gattBinder),
                                            advertiserId,
                                            mBluetoothAdapter,
                                            mAttributionSource);
                            mAdvertisingSets.put(advertiserId, advertisingSet);
                            callback.onAdvertisingSetStarted(advertisingSet, txPower, status);
                            }
                        });
            }

            @Override
            public void onOwnAddressRead(int advertiserId, int addressType, String address) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onOwnAddressRead(advertisingSet, addressType, address);
                            }
                        });
            }

            @Override
            public void onAdvertisingSetStopped(int advertiserId) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onAdvertisingSetStopped(advertisingSet);
                            mAdvertisingSets.remove(advertiserId);
                            mCallbackWrappers.remove(callback);
                            }
                        });
            }

            @Override
            public void onAdvertisingEnabled(int advertiserId, boolean enabled, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onAdvertisingEnabled(advertisingSet, enabled, status);
                            }
                        });
            }

            @Override
            public void onAdvertisingDataSet(int advertiserId, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onAdvertisingDataSet(advertisingSet, status);
                            }
                        });
            }

            @Override
            public void onScanResponseDataSet(int advertiserId, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onScanResponseDataSet(advertisingSet, status);
                            }
                        });
            }

            @Override
            public void onAdvertisingParametersUpdated(int advertiserId, int txPower, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onAdvertisingParametersUpdated(
                                    advertisingSet, txPower, status);
                            }
                        });
            }

            @Override
            public void onPeriodicAdvertisingParametersUpdated(int advertiserId, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                                callback.onPeriodicAdvertisingParametersUpdated(
                                        advertisingSet, status);
                            }
                            callback.onPeriodicAdvertisingParametersUpdated(advertisingSet, status);
                        });
            }

            @Override
            public void onPeriodicAdvertisingDataSet(int advertiserId, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                            callback.onPeriodicAdvertisingDataSet(advertisingSet, status);
                            }
                        });
            }

            @Override
            public void onPeriodicAdvertisingEnabled(int advertiserId, boolean enable, int status) {
                handler.post(
                        new Runnable() {
                            @Override
                            public void run() {
                        () -> {
                            AdvertisingSet advertisingSet = mAdvertisingSets.get(advertiserId);
                                callback.onPeriodicAdvertisingEnabled(
                                        advertisingSet, enable, status);
                            }
                            callback.onPeriodicAdvertisingEnabled(advertisingSet, enable, status);
                        });
            }
        };