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

Commit ea6274ae authored by William Escande's avatar William Escande
Browse files

Change bluetooth cache to be static

Whith a non static cache, we are creating a cache every time a new
BluetoothDevice is created, and then we have to rely on the GC.
The number of cache can sometimes reach 10 000.

Fix: 235637412
Test: basic bt functionnality
Test: adb shell dumpsys cacheinfo com.android.bluetooth
Change-Id: I6659dd9050b42bb57d0640df8117150cd67cb026
parent de669212
Loading
Loading
Loading
Loading
+24 −22
Original line number Diff line number Diff line
@@ -1269,7 +1269,7 @@ public final class BluetoothAdapter {
        IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
    }

    private final IpcDataCache.QueryHandler<IBluetooth, Integer> mBluetoothGetStateQuery =
    private static final IpcDataCache.QueryHandler<IBluetooth, Integer> sBluetoothGetStateQuery =
            new IpcDataCache.QueryHandler<>() {
            @RequiresLegacyBluetoothPermission
            @RequiresNoPermission
@@ -1288,13 +1288,13 @@ public final class BluetoothAdapter {

    private static final String GET_STATE_API = "BluetoothAdapter_getState";

    private final IpcDataCache<IBluetooth, Integer> mBluetoothGetStateCache =
            new BluetoothCache<>(GET_STATE_API, mBluetoothGetStateQuery);
    private static final IpcDataCache<IBluetooth, Integer> sBluetoothGetStateCache =
            new BluetoothCache<>(GET_STATE_API, sBluetoothGetStateQuery);

    /** @hide */
    @RequiresNoPermission
    public void disableBluetoothGetStateCache() {
        mBluetoothGetStateCache.disableForCurrentProcess();
        sBluetoothGetStateCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -1310,7 +1310,7 @@ public final class BluetoothAdapter {
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                return mBluetoothGetStateCache.query(mService);
                return sBluetoothGetStateCache.query(mService);
            }
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
@@ -2422,7 +2422,7 @@ public final class BluetoothAdapter {
        }
    }

    private final IpcDataCache.QueryHandler<IBluetooth, Boolean> mBluetoothFilteringQuery =
    private static final IpcDataCache.QueryHandler<IBluetooth, Boolean> sBluetoothFilteringQuery =
            new IpcDataCache.QueryHandler<>() {
        @RequiresLegacyBluetoothPermission
        @RequiresNoPermission
@@ -2439,13 +2439,13 @@ public final class BluetoothAdapter {

    private static final String FILTERING_API = "BluetoothAdapter_isOffloadedFilteringSupported";

    private final IpcDataCache<IBluetooth, Boolean> mBluetoothFilteringCache =
            new BluetoothCache<>(FILTERING_API, mBluetoothFilteringQuery);
    private static final IpcDataCache<IBluetooth, Boolean> sBluetoothFilteringCache =
            new BluetoothCache<>(FILTERING_API, sBluetoothFilteringQuery);

    /** @hide */
    @RequiresNoPermission
    public void disableIsOffloadedFilteringSupportedCache() {
        mBluetoothFilteringCache.disableForCurrentProcess();
        sBluetoothFilteringCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -2466,7 +2466,7 @@ public final class BluetoothAdapter {
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) return mBluetoothFilteringCache.query(mService);
            if (mService != null) return sBluetoothFilteringCache.query(mService);
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
@@ -2982,8 +2982,8 @@ public final class BluetoothAdapter {
        return supportedProfiles;
    }

    private final IpcDataCache.QueryHandler<IBluetooth, Integer>
            mBluetoothGetAdapterConnectionStateQuery = new IpcDataCache.QueryHandler<>() {
    private static final IpcDataCache.QueryHandler<IBluetooth, Integer>
            sBluetoothGetAdapterConnectionStateQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresLegacyBluetoothPermission
                @RequiresNoPermission
                @Override
@@ -3002,13 +3002,14 @@ public final class BluetoothAdapter {

    private static final String GET_CONNECTION_API = "BluetoothAdapter_getConnectionState";

    private final IpcDataCache<IBluetooth, Integer> mBluetoothGetAdapterConnectionStateCache =
            new BluetoothCache<>(GET_CONNECTION_API, mBluetoothGetAdapterConnectionStateQuery);
    private static final IpcDataCache<IBluetooth, Integer>
            sBluetoothGetAdapterConnectionStateCache = new BluetoothCache<>(GET_CONNECTION_API,
                    sBluetoothGetAdapterConnectionStateQuery);

    /** @hide */
    @RequiresNoPermission
    public void disableGetAdapterConnectionStateCache() {
        mBluetoothGetAdapterConnectionStateCache.disableForCurrentProcess();
        sBluetoothGetAdapterConnectionStateCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -3035,7 +3036,7 @@ public final class BluetoothAdapter {
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) return mBluetoothGetAdapterConnectionStateCache.query(mService);
            if (mService != null) return sBluetoothGetAdapterConnectionStateCache.query(mService);
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
@@ -3048,8 +3049,8 @@ public final class BluetoothAdapter {
        return STATE_DISCONNECTED;
    }

    private final IpcDataCache.QueryHandler<Pair<IBluetooth, Integer>, Integer>
            mBluetoothProfileQuery = new IpcDataCache.QueryHandler<>() {
    private static final IpcDataCache.QueryHandler<Pair<IBluetooth, Integer>, Integer>
            sBluetoothProfileQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresNoPermission
                @Override
                public Integer apply(Pair<IBluetooth, Integer> pairQuery) {
@@ -3067,15 +3068,16 @@ public final class BluetoothAdapter {

    private static final String PROFILE_API = "BluetoothAdapter_getProfileConnectionState";

    private final IpcDataCache<Pair<IBluetooth, Integer>, Integer> mGetProfileConnectionStateCache =
            new BluetoothCache<>(PROFILE_API, mBluetoothProfileQuery);
    private static final IpcDataCache<Pair<IBluetooth, Integer>, Integer>
            sGetProfileConnectionStateCache = new BluetoothCache<>(PROFILE_API,
                    sBluetoothProfileQuery);

    /**
     * @hide
     */
    @RequiresNoPermission
    public void disableGetProfileConnectionStateCache() {
        mGetProfileConnectionStateCache.disableForCurrentProcess();
        sGetProfileConnectionStateCache.disableForCurrentProcess();
    }

    /**
@@ -3104,7 +3106,7 @@ public final class BluetoothAdapter {
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                return mGetProfileConnectionStateCache.query(new Pair<>(mService, profile));
                return sGetProfileConnectionStateCache.query(new Pair<>(mService, profile));
            }
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
+16 −10
Original line number Diff line number Diff line
@@ -1903,21 +1903,25 @@ public final class BluetoothDevice implements Parcelable, Attributable {
        IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
    }

    private final IpcDataCache.QueryHandler<Pair<IBluetooth, BluetoothDevice>, Integer>
            mBluetoothBondQuery = new IpcDataCache.QueryHandler<>() {
    private static final IpcDataCache
            .QueryHandler<Pair<IBluetooth, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothBondQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresLegacyBluetoothPermission
                @RequiresBluetoothConnectPermission
                @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
                @Override
                public Integer apply(Pair<IBluetooth, BluetoothDevice> pairQuery) {
                public Integer apply(Pair<IBluetooth,
                        Pair<AttributionSource, BluetoothDevice>> pairQuery) {
                    IBluetooth service = pairQuery.first;
                    AttributionSource source = pairQuery.second.first;
                    BluetoothDevice device = pairQuery.second.second;
                    if (DBG) {
                        log("getConnectionState(" + pairQuery.second.getAnonymizedAddress()
                                + ") uncached");
                        log("getBondState(" + device.getAnonymizedAddress() + ") uncached");
                    }
                    try {
                        final SynchronousResultReceiver<Integer> recv =
                                SynchronousResultReceiver.get();
                        pairQuery.first.getBondState(pairQuery.second, mAttributionSource, recv);
                        service.getBondState(device, source, recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(BOND_NONE);
                    } catch (RemoteException | TimeoutException e) {
                        throw new RuntimeException(e);
@@ -1927,12 +1931,13 @@ public final class BluetoothDevice implements Parcelable, Attributable {

    private static final String GET_BOND_STATE_API = "BluetoothDevice_getBondState";

    private final BluetoothCache<Pair<IBluetooth, BluetoothDevice>, Integer> mBluetoothBondCache =
            new BluetoothCache<>(GET_BOND_STATE_API, mBluetoothBondQuery);
    private static final
            BluetoothCache<Pair<IBluetooth, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothBondCache = new BluetoothCache<>(GET_BOND_STATE_API, sBluetoothBondQuery);

    /** @hide */
    public void disableBluetoothGetBondStateCache() {
        mBluetoothBondCache.disableForCurrentProcess();
        sBluetoothBondCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -1961,7 +1966,8 @@ public final class BluetoothDevice implements Parcelable, Attributable {
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else {
            try {
                return mBluetoothBondCache.query(new Pair<>(service, BluetoothDevice.this));
                return sBluetoothBondCache.query(
                        new Pair<>(service, new Pair<>(mAttributionSource, BluetoothDevice.this)));
            } catch (RuntimeException e) {
                if (!(e.getCause() instanceof TimeoutException)
                        && !(e.getCause() instanceof RemoteException)) {
+17 −12
Original line number Diff line number Diff line
@@ -367,7 +367,7 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {

    /** @hide */
    public void disableBluetoothGetConnectionStateCache() {
        mBluetoothConnectionCache.disableForCurrentProcess();
        sBluetoothConnectionCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -383,20 +383,23 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
        IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
    }

    private final IpcDataCache.QueryHandler<Pair<IBluetoothMap, BluetoothDevice>, Integer>
            mBluetoothConnectionQuery = new IpcDataCache.QueryHandler<>() {
    private static final IpcDataCache
            .QueryHandler<Pair<IBluetoothMap, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothConnectionQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresBluetoothConnectPermission
                @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
                @Override
                public Integer apply(Pair<IBluetoothMap, BluetoothDevice> pairQuery) {
                public Integer apply(Pair<IBluetoothMap,
                        Pair<AttributionSource, BluetoothDevice>> pairQuery) {
                    IBluetoothMap service = pairQuery.first;
                    AttributionSource source = pairQuery.second.first;
                    BluetoothDevice device = pairQuery.second.second;
                    if (DBG) {
                        log("getConnectionState(" + pairQuery.second.getAnonymizedAddress()
                                + ") uncached");
                        log("getConnectionState(" + device.getAnonymizedAddress() + ") uncached");
                    }
                    final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
                    try {
                        pairQuery.first
                            .getConnectionState(pairQuery.second, mAttributionSource, recv);
                        service.getConnectionState(device, source, recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout())
                            .getValue(BluetoothProfile.STATE_DISCONNECTED);
                    } catch (RemoteException | TimeoutException e) {
@@ -407,9 +410,10 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {

    private static final String GET_CONNECTION_STATE_API = "BluetoothMap_getConnectionState";

    private final BluetoothCache<Pair<IBluetoothMap, BluetoothDevice>, Integer>
            mBluetoothConnectionCache = new BluetoothCache<>(GET_CONNECTION_STATE_API,
                mBluetoothConnectionQuery);
    private static final
            BluetoothCache<Pair<IBluetoothMap, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothConnectionCache = new BluetoothCache<>(GET_CONNECTION_STATE_API,
                sBluetoothConnectionQuery);

    /**
     * Get connection state of device
@@ -427,7 +431,8 @@ public final class BluetoothMap implements BluetoothProfile, AutoCloseable {
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled() && isValidDevice(device)) {
            try {
                return mBluetoothConnectionCache.query(new Pair<>(service, device));
                return sBluetoothConnectionCache.query(
                        new Pair<>(service, new Pair<>(mAttributionSource, device)));
            } catch (RuntimeException e) {
                if (!(e.getCause() instanceof TimeoutException)
                        && !(e.getCause() instanceof RemoteException)) {
+17 −12
Original line number Diff line number Diff line
@@ -364,7 +364,7 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {

    /** @hide */
    public void disableBluetoothGetConnectionStateCache() {
        mBluetoothConnectionCache.disableForCurrentProcess();
        sBluetoothConnectionCache.disableForCurrentProcess();
    }

    /** @hide */
@@ -380,20 +380,23 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {
        IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
    }

    private final IpcDataCache.QueryHandler<Pair<IBluetoothSap, BluetoothDevice>, Integer>
            mBluetoothConnectionQuery = new IpcDataCache.QueryHandler<>() {
    private static final IpcDataCache
            .QueryHandler<Pair<IBluetoothSap, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothConnectionQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresBluetoothConnectPermission
                @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
                @Override
                public Integer apply(Pair<IBluetoothSap, BluetoothDevice> pairQuery) {
                public Integer apply(Pair<IBluetoothSap,
                        Pair<AttributionSource, BluetoothDevice>> pairQuery) {
                    IBluetoothSap service = pairQuery.first;
                    AttributionSource source = pairQuery.second.first;
                    BluetoothDevice device = pairQuery.second.second;
                    if (DBG) {
                        log("getConnectionState(" + pairQuery.second.getAnonymizedAddress()
                                + ") uncached");
                        log("getConnectionState(" + device.getAnonymizedAddress() + ") uncached");
                    }
                    final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
                    try {
                        pairQuery.first
                            .getConnectionState(pairQuery.second, mAttributionSource, recv);
                        service.getConnectionState(device, source, recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout())
                            .getValue(BluetoothProfile.STATE_DISCONNECTED);
                    } catch (RemoteException | TimeoutException e) {
@@ -404,9 +407,10 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {

    private static final String GET_CONNECTION_STATE_API = "BluetoothSap_getConnectionState";

    private final BluetoothCache<Pair<IBluetoothSap, BluetoothDevice>, Integer>
            mBluetoothConnectionCache = new BluetoothCache<>(GET_CONNECTION_STATE_API,
                mBluetoothConnectionQuery);
    private static final
            BluetoothCache<Pair<IBluetoothSap, Pair<AttributionSource, BluetoothDevice>>, Integer>
            sBluetoothConnectionCache = new BluetoothCache<>(GET_CONNECTION_STATE_API,
                sBluetoothConnectionQuery);

    /**
     * Get connection state of device
@@ -424,7 +428,8 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {
            if (DBG) log(Log.getStackTraceString(new Throwable()));
        } else if (isEnabled() && isValidDevice(device)) {
            try {
                return mBluetoothConnectionCache.query(new Pair<>(service, device));
                return sBluetoothConnectionCache.query(
                        new Pair<>(service, new Pair<>(mAttributionSource, device)));
            } catch (RuntimeException e) {
                if (!(e.getCause() instanceof TimeoutException)
                        && !(e.getCause() instanceof RemoteException)) {