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

Commit 6e98a721 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Add caching for {Map,Sap}.getConnectionState" into tm-dev am: ea5dd33b

parents 3339334a ea5dd33b
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -263,6 +263,10 @@ class AdapterProperties {
    private static void invalidateIsOffloadedFilteringSupportedCache() {
        BluetoothAdapter.invalidateIsOffloadedFilteringSupportedCache();
    }
    private static void invalidateBluetoothGetConnectionStateCache() {
        BluetoothMap.invalidateBluetoothGetConnectionStateCache();
        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
    }
    private static void invalidateGetConnectionStateCache() {
        BluetoothAdapter.invalidateGetAdapterConnectionStateCache();
    }
@@ -274,6 +278,7 @@ class AdapterProperties {
        invalidateIsOffloadedFilteringSupportedCache();
        invalidateGetConnectionStateCache();
        invalidateGetBondStateCache();
        invalidateBluetoothGetConnectionStateCache();
    }

    @Override
+4 −0
Original line number Diff line number Diff line
@@ -46,8 +46,10 @@ import android.bluetooth.BluetoothAdapter.ActiveDeviceUse;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothFrameworkInitializer;
import android.bluetooth.BluetoothMap;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProtoEnums;
import android.bluetooth.BluetoothSap;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothStatusCodes;
@@ -985,6 +987,8 @@ public class AdapterService extends Service {
        BluetoothDevice.invalidateBluetoothGetBondStateCache();
        BluetoothAdapter.invalidateBluetoothGetStateCache();
        BluetoothAdapter.invalidateGetAdapterConnectionStateCache();
        BluetoothMap.invalidateBluetoothGetConnectionStateCache();
        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
    }

    private void setProfileServiceState(Class service, int state) {
+12 −4
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.bluetooth.IBluetoothMap;
import android.bluetooth.SdpMnsRecord;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -166,6 +165,7 @@ public class BluetoothMapService extends ProfileService {

    public BluetoothMapService() {
        mState = BluetoothMap.STATE_DISCONNECTED;
        BluetoothMap.invalidateBluetoothGetConnectionStateCache();
    }

    private synchronized void closeService() {
@@ -195,6 +195,7 @@ public class BluetoothMapService extends ProfileService {
        }

        sRemoteDevice = null;
        // no need to invalidate cache here because setState did it above

        if (mSessionStatusHandler == null) {
            return;
@@ -332,6 +333,7 @@ public class BluetoothMapService extends ProfileService {
            setState(BluetoothMap.STATE_DISCONNECTED);
            mPermission = BluetoothDevice.ACCESS_UNKNOWN;
            sRemoteDevice = null;
            // no need to invalidate cache here because setState did it above
            if (mAccountChanged) {
                updateMasInstances(UPDATE_MAS_INSTANCES_ACCOUNT_DISCONNECT);
            }
@@ -519,6 +521,7 @@ public class BluetoothMapService extends ProfileService {
            }
            int prevState = mState;
            mState = state;
            BluetoothMap.invalidateBluetoothGetConnectionStateCache();
            Intent intent = new Intent(BluetoothMap.ACTION_CONNECTION_STATE_CHANGED);
            intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
            intent.putExtra(BluetoothProfile.EXTRA_STATE, mState);
@@ -595,7 +598,8 @@ public class BluetoothMapService extends ProfileService {
     */
    public int getConnectionState(BluetoothDevice device) {
        synchronized (this) {
            if (getState() == BluetoothMap.STATE_CONNECTED && getRemoteDevice().equals(device)) {
            if (getState() == BluetoothMap.STATE_CONNECTED && getRemoteDevice() != null
                    && getRemoteDevice().equals(device)) {
                return BluetoothProfile.STATE_CONNECTED;
            } else {
                return BluetoothProfile.STATE_DISCONNECTED;
@@ -911,6 +915,9 @@ public class BluetoothMapService extends ProfileService {
        synchronized (this) {
            if (sRemoteDevice == null) {
                sRemoteDevice = remoteDevice;
                if (getState() == BluetoothMap.STATE_CONNECTED) {
                    BluetoothMap.invalidateBluetoothGetConnectionStateCache();
                }
                sRemoteDeviceName = Utils.getName(sRemoteDevice);
                // In case getRemoteName failed and return null
                if (TextUtils.isEmpty(sRemoteDeviceName)) {
@@ -933,6 +940,7 @@ public class BluetoothMapService extends ProfileService {
                        (remoteDevice == null) ? "unknown" : Utils.getName(remoteDevice)));
                return false;
            } // Else second connection to same device, just continue

        }

        if (sendIntent) {
@@ -1274,8 +1282,8 @@ public class BluetoothMapService extends ProfileService {
                BluetoothMapService service = getService(source);
                boolean result = false;
                if (service != null) {
                    result = service.getState() == BluetoothMap.STATE_CONNECTED
                        && BluetoothMapService.getRemoteDevice().equals(device);
                    result = service.getConnectionState(device)
                        == BluetoothProfile.STATE_CONNECTED;
                }
                receiver.send(result);
            } catch (RuntimeException e) {
+7 −3
Original line number Diff line number Diff line
@@ -109,6 +109,7 @@ public class SapService extends ProfileService {

    public SapService() {
        mState = BluetoothSap.STATE_DISCONNECTED;
        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
    }

    /***
@@ -362,6 +363,7 @@ public class SapService extends ProfileService {
                            break;
                        }
                        mRemoteDevice = mConnSocket.getRemoteDevice();
                        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
                    }
                    if (mRemoteDevice == null) {
                        Log.i(TAG, "getRemoteDevice() = null");
@@ -525,6 +527,7 @@ public class SapService extends ProfileService {
            }
            int prevState = mState;
            mState = state;
            BluetoothSap.invalidateBluetoothGetConnectionStateCache();
            Intent intent = new Intent(BluetoothSap.ACTION_CONNECTION_STATE_CHANGED);
            intent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, prevState);
            intent.putExtra(BluetoothProfile.EXTRA_STATE, mState);
@@ -596,7 +599,8 @@ public class SapService extends ProfileService {

    public int getConnectionState(BluetoothDevice device) {
        synchronized (this) {
            if (getState() == BluetoothSap.STATE_CONNECTED && getRemoteDevice().equals(device)) {
            if (getState() == BluetoothSap.STATE_CONNECTED && getRemoteDevice() != null
                    && getRemoteDevice().equals(device)) {
                return BluetoothProfile.STATE_CONNECTED;
            } else {
                return BluetoothProfile.STATE_DISCONNECTED;
@@ -982,8 +986,8 @@ public class SapService extends ProfileService {
                boolean defaultValue = false;
                SapService service = getService(source);
                if (service != null) {
                    defaultValue = service.getState() == BluetoothSap.STATE_CONNECTED
                            && service.getRemoteDevice().equals(device);
                    defaultValue = service.getConnectionState(device)
                        == BluetoothProfile.STATE_CONNECTED;
                }
                receiver.send(defaultValue);
            } catch (RuntimeException e) {
+121 −97
Original line number Diff line number Diff line
@@ -1269,36 +1269,27 @@ public final class BluetoothAdapter {
        IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
    }

    /**
     * The binder cache for getState().
     */
    private static final String GET_STATE_API = "getState";

    private final IpcDataCache.QueryHandler<Void, Integer> mBluetoothGetStateQuery =
    private final IpcDataCache.QueryHandler<IBluetooth, Integer> mBluetoothGetStateQuery =
            new IpcDataCache.QueryHandler<>() {
            @RequiresLegacyBluetoothPermission
            @RequiresNoPermission
            @Override
        public @InternalAdapterState Integer apply(Void query) {
            int state = BluetoothAdapter.STATE_OFF;
            mServiceLock.readLock().lock();
            public @InternalAdapterState Integer apply(IBluetooth serviceQuery) {
                try {
                if (mService != null) {
                    final SynchronousResultReceiver<Integer> recv =
                            new SynchronousResultReceiver();
                    mService.getState(recv);
                    return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(state);
                }
                    final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
                    serviceQuery.getState(recv);
                    return recv.awaitResultNoInterrupt(getSyncTimeout())
                        .getValue(BluetoothAdapter.STATE_OFF);
                } catch (RemoteException | TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            } finally {
                mServiceLock.readLock().unlock();
                    throw new RuntimeException(e);
                }
            return state;
        }};
            }
        };

    private final IpcDataCache<Void, Integer> mBluetoothGetStateCache =
            new BluetoothCache<Void, Integer>(GET_STATE_API, mBluetoothGetStateQuery);
    private static final String GET_STATE_API = "BluetoothAdapter_getState";

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

    /** @hide */
    @RequiresNoPermission
@@ -1316,7 +1307,21 @@ public final class BluetoothAdapter {
     * OFF.
     */
    private @InternalAdapterState int getStateInternal() {
        return mBluetoothGetStateCache.query(null);
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                return mBluetoothGetStateCache.query(mService);
            }
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
                throw e;
            }
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return STATE_OFF;
    }

    /**
@@ -2417,31 +2422,25 @@ public final class BluetoothAdapter {
        }
    }

    private final IpcDataCache.QueryHandler<Void, Boolean> mBluetoothFilteringQuery =
    private final IpcDataCache.QueryHandler<IBluetooth, Boolean> mBluetoothFilteringQuery =
            new IpcDataCache.QueryHandler<>() {
        @RequiresLegacyBluetoothPermission
        @RequiresNoPermission
        @Override
        public Boolean apply(Void query) {
            mServiceLock.readLock().lock();
        public Boolean apply(IBluetooth serviceQuery) {
            try {
                if (mService != null) {
                final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver();
                    mService.isOffloadedFilteringSupported(recv);
                serviceQuery.isOffloadedFilteringSupported(recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false);
                }
            } catch (RemoteException | TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            } finally {
                mServiceLock.readLock().unlock();
                throw new RuntimeException(e);
            }
            return false;
        }};

    private static final String FILTERING_API = "isOffloadedFilteringSupported";
    private static final String FILTERING_API = "BluetoothAdapter_isOffloadedFilteringSupported";

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

    /** @hide */
    @RequiresNoPermission
@@ -2465,7 +2464,19 @@ public final class BluetoothAdapter {
        if (!getLeAccess()) {
            return false;
        }
        return mBluetoothFilteringCache.query(null);
        mServiceLock.readLock().lock();
        try {
            if (mService != null) return mBluetoothFilteringCache.query(mService);
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
                throw e;
            }
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return false;
    }

    /**
@@ -2967,35 +2978,28 @@ public final class BluetoothAdapter {
        return supportedProfiles;
    }

    private final IpcDataCache.QueryHandler<Void, Integer> mBluetoothGetAdapterQuery =
            new IpcDataCache.QueryHandler<>() {
    private final IpcDataCache.QueryHandler<IBluetooth, Integer>
            mBluetoothGetAdapterConnectionStateQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresLegacyBluetoothPermission
                @RequiresNoPermission
                @Override
        public Integer apply(Void query) {
            mServiceLock.readLock().lock();
                public Integer apply(IBluetooth serviceQuery) {
                    try {
                if (mService != null) {
                        final SynchronousResultReceiver<Integer> recv =
                                new SynchronousResultReceiver();
                    mService.getAdapterConnectionState(recv);
                        serviceQuery.getAdapterConnectionState(recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout())
                            .getValue(STATE_DISCONNECTED);
                    } catch (RemoteException | TimeoutException e) {
                        throw new RuntimeException(e);
                    }
            } catch (TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            } catch (RemoteException e) {
                Log.e(TAG, "failed to getConnectionState, error: ", e);
            } finally {
                mServiceLock.readLock().unlock();
                }
            return BluetoothAdapter.STATE_DISCONNECTED;
        }};
            };

    private static final String GET_CONNECTION_API = "BluetoothAdapter_getConnectionState";

    private static final String GET_CONNECTION_API = "getAdapterConnectionState";
    private final IpcDataCache<Void, Integer>
            mBluetoothGetAdapterConnectionStateCache =
            new BluetoothCache<Void, Integer>(GET_CONNECTION_API, mBluetoothGetAdapterQuery);
    private final IpcDataCache<IBluetooth, Integer> mBluetoothGetAdapterConnectionStateCache =
            new BluetoothCache<>(GET_CONNECTION_API, mBluetoothGetAdapterConnectionStateQuery);

    /** @hide */
    @RequiresNoPermission
@@ -3025,36 +3029,42 @@ public final class BluetoothAdapter {
        if (getState() != STATE_ON) {
            return BluetoothAdapter.STATE_DISCONNECTED;
        }
        return mBluetoothGetAdapterConnectionStateCache.query(null);
        mServiceLock.readLock().lock();
        try {
            if (mService != null) return mBluetoothGetAdapterConnectionStateCache.query(mService);
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
                throw e;
            }
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return STATE_DISCONNECTED;
    }

    private final IpcDataCache.QueryHandler<Integer, Integer> mBluetoothProfileQuery =
            new IpcDataCache.QueryHandler<>() {
    private final IpcDataCache.QueryHandler<Pair<IBluetooth, Integer>, Integer>
            mBluetoothProfileQuery = new IpcDataCache.QueryHandler<>() {
                @RequiresNoPermission
                @Override
        public Integer apply(Integer query) {
                public Integer apply(Pair<IBluetooth, Integer> pairQuery) {
                    final int defaultValue = STATE_DISCONNECTED;
                    try {
                mServiceLock.readLock().lock();
                if (mService != null) {
                    final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
                    mService.getProfileConnectionState(query, recv);
                    return recv.awaitResultNoInterrupt(getSyncTimeout())
                        .getValue(BluetoothProfile.STATE_DISCONNECTED);
                        final SynchronousResultReceiver<Integer> recv =
                                new SynchronousResultReceiver();
                        pairQuery.first.getProfileConnectionState(pairQuery.second, recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
                    } catch (RemoteException | TimeoutException e) {
                        throw new RuntimeException(e);
                    }
            } catch (TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            } catch (RemoteException e) {
                Log.e(TAG, "failed to getProfileConnectionState, error: ", e);
            } finally {
                mServiceLock.readLock().unlock();
                }
            return BluetoothProfile.STATE_DISCONNECTED;
        }};
            };

    private static final String PROFILE_API = "BluetoothAdapter_getProfileConnectionState";

    private static final String PROFILE_API = "getProfileConnectionState";
    private final IpcDataCache<Integer, Integer>
            mGetProfileConnectionStateCache =
            new BluetoothCache<Integer, Integer>(PROFILE_API, mBluetoothProfileQuery);
    private final IpcDataCache<Pair<IBluetooth, Integer>, Integer> mGetProfileConnectionStateCache =
            new BluetoothCache<>(PROFILE_API, mBluetoothProfileQuery);

    /**
     * @hide
@@ -3085,9 +3095,23 @@ public final class BluetoothAdapter {
    @SuppressLint("AndroidFrameworkRequiresPermission")
    public @ConnectionState int getProfileConnectionState(int profile) {
        if (getState() != STATE_ON) {
            return BluetoothProfile.STATE_DISCONNECTED;
            return STATE_DISCONNECTED;
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                return mGetProfileConnectionStateCache.query(new Pair<>(mService, profile));
            }
        } catch (RuntimeException e) {
            if (!(e.getCause() instanceof TimeoutException)
                    && !(e.getCause() instanceof RemoteException)) {
                throw e;
            }
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return mGetProfileConnectionStateCache.query(profile);
        return STATE_DISCONNECTED;
    }

    /**
Loading