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

Commit 56056198 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Use new ScanController instead of GattService for scan related...

Merge "Use new ScanController instead of GattService for scan related functionality within PeriodicAdvertisingManager" into main
parents 4aee8e95 5b838335
Loading
Loading
Loading
Loading
+69 −26
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.bluetooth.Attributable;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.IBluetoothGatt;
import android.bluetooth.IBluetoothScan;
import android.bluetooth.annotations.RequiresBluetoothLocationPermission;
import android.bluetooth.annotations.RequiresBluetoothScanPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
@@ -34,6 +35,8 @@ import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;

import com.android.bluetooth.flags.Flags;

import java.util.IdentityHashMap;
import java.util.Objects;

@@ -149,8 +152,6 @@ public final class PeriodicAdvertisingManager {
                    "timeout must be between " + TIMEOUT_MIN + " and " + TIMEOUT_MAX);
        }

        IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }
@@ -158,11 +159,22 @@ public final class PeriodicAdvertisingManager {
        IPeriodicAdvertisingCallback wrapped = wrap(callback, handler);
        mCallbackWrappers.put(callback, wrapped);

        if (Flags.scanManagerRefactor()) {
            IBluetoothScan scan = mBluetoothAdapter.getBluetoothScan();

            try {
                scan.registerSync(scanResult, skip, timeout, wrapped, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            }
        } else {
            IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

            try {
                gatt.registerSync(scanResult, skip, timeout, wrapped, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            return;
            }
        }
    }

@@ -181,18 +193,27 @@ public final class PeriodicAdvertisingManager {
            throw new IllegalArgumentException("callback can't be null");
        }

        IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

        IPeriodicAdvertisingCallback wrapper = mCallbackWrappers.remove(callback);
        if (wrapper == null) {
            throw new IllegalArgumentException("callback was not properly registered");
        }

        if (Flags.scanManagerRefactor()) {
            IBluetoothScan scan = mBluetoothAdapter.getBluetoothScan();

            try {
                scan.unregisterSync(wrapper, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to cancel sync creation - ", e);
            }
        } else {
            IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

            try {
                gatt.unregisterSync(wrapper, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to cancel sync creation - ", e);
            return;
            }
        }
    }

@@ -202,13 +223,22 @@ public final class PeriodicAdvertisingManager {
     * @hide
     */
    public void transferSync(BluetoothDevice bda, int serviceData, int syncHandle) {
        if (Flags.scanManagerRefactor()) {
            IBluetoothScan scan = mBluetoothAdapter.getBluetoothScan();

            try {
                scan.transferSync(bda, serviceData, syncHandle, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            }
        } else {
            IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

            try {
                gatt.transferSync(bda, serviceData, syncHandle, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            return;
            }
        }
    }

@@ -239,19 +269,32 @@ public final class PeriodicAdvertisingManager {
        if (callback == null) {
            throw new IllegalArgumentException("callback can't be null");
        }
        IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }

        IPeriodicAdvertisingCallback wrapper = wrap(callback, handler);
        if (wrapper == null) {
            throw new IllegalArgumentException("callback was not properly registered");
        }

        if (Flags.scanManagerRefactor()) {
            IBluetoothScan scan = mBluetoothAdapter.getBluetoothScan();

            try {
                scan.transferSetInfo(bda, serviceData, advHandle, wrapper, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            }
        } else {
            IBluetoothGatt gatt = mBluetoothAdapter.getBluetoothGatt();

            try {
                gatt.transferSetInfo(bda, serviceData, advHandle, wrapper, mAttributionSource);
            } catch (RemoteException e) {
                Log.e(TAG, "Failed to register sync - ", e);
            return;
            }
        }
    }