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

Commit 573cc15c authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files

SystemServer: implement getState for general purpose am: e8f6decd

parents 06c3e8a8 e8f6decd
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -1153,6 +1153,10 @@ public class AdapterService extends Service {
    }

    private void invalidateBluetoothGetStateCache() {
        if (Flags.getStateFromSystemServer()) {
            // State is managed by the system server
            return;
        }
        BluetoothAdapter.invalidateBluetoothGetStateCache();
    }

@@ -1482,7 +1486,9 @@ public class AdapterService extends Service {
        BluetoothAdapter.invalidateGetProfileConnectionStateCache();
        BluetoothAdapter.invalidateIsOffloadedFilteringSupportedCache();
        BluetoothDevice.invalidateBluetoothGetBondStateCache();
        if (!Flags.getStateFromSystemServer()) {
            BluetoothAdapter.invalidateBluetoothGetStateCache();
        }
        BluetoothAdapter.invalidateGetAdapterConnectionStateCache();
        BluetoothMap.invalidateBluetoothGetConnectionStateCache();
        BluetoothSap.invalidateBluetoothGetConnectionStateCache();
@@ -2240,6 +2246,9 @@ public class AdapterService extends Service {

        AdapterServiceBinder(AdapterService svc) {
            mService = svc;
            if (Flags.getStateFromSystemServer()) {
                return;
            }
            mService.invalidateBluetoothGetStateCache();
            BluetoothAdapter.getDefaultAdapter().disableBluetoothGetStateCache();
        }
+36 −1
Original line number Diff line number Diff line
@@ -1468,7 +1468,6 @@ public final class BluetoothAdapter {
            super(8, IpcDataCache.MODULE_BLUETOOTH, api, api, query);
        }
    }
    ;

    /**
     * Invalidate a bluetooth cache. This method is just a short-hand wrapper that enforces the
@@ -1492,24 +1491,60 @@ public final class BluetoothAdapter {
                }
            };

    private static final IpcDataCache.QueryHandler<IBluetoothManager, Integer>
            sBluetoothGetSystemStateQuery =
                    new IpcDataCache.QueryHandler<>() {
                        @RequiresNoPermission
                        @Override
                        public @InternalAdapterState Integer apply(IBluetoothManager serviceQuery) {
                            try {
                                return serviceQuery.getState();
                            } catch (RemoteException e) {
                                throw e.rethrowAsRuntimeException();
                            }
                        }
                    };

    private static final String GET_STATE_API = "BluetoothAdapter_getState";

    /** @hide */
    public static final String GET_SYSTEM_STATE_API = IBluetoothManager.GET_SYSTEM_STATE_API;

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

    private static final IpcDataCache<IBluetoothManager, Integer> sBluetoothGetSystemStateCache =
            new BluetoothCache<>(GET_SYSTEM_STATE_API, sBluetoothGetSystemStateQuery);

    /** @hide */
    @RequiresNoPermission
    public void disableBluetoothGetStateCache() {
        if (Flags.getStateFromSystemServer()) {
            throw new IllegalStateException("getStateFromSystemServer is enabled");
        }
        sBluetoothGetStateCache.disableForCurrentProcess();
    }

    /** @hide */
    public static void invalidateBluetoothGetStateCache() {
        if (Flags.getStateFromSystemServer()) {
            throw new IllegalStateException("getStateFromSystemServer is enabled");
        }
        invalidateCache(GET_STATE_API);
    }

    /** Fetch the current bluetooth state. If the service is down, return OFF. */
    private @InternalAdapterState int getStateInternal() {
        if (Flags.getStateFromSystemServer()) {
            try {
                return sBluetoothGetSystemStateCache.query(mManagerService);
            } catch (RuntimeException runtime) {
                if (runtime.getCause() instanceof RemoteException e) {
                    throw e.rethrowFromSystemServer();
                }
                throw runtime;
            }
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
+1 −0
Original line number Diff line number Diff line
@@ -221,6 +221,7 @@ android_robolectric_test {
        "mockito-robolectric-prebuilt",
        "modules-utils-expresslog",
        "platform-test-annotations",
        "service-bluetooth-binder-aidl",
        "testng",
        "truth",
    ],
+3 −0
Original line number Diff line number Diff line
@@ -36,6 +36,9 @@ interface IBluetoothManager
    boolean enableNoAutoConnect(in AttributionSource attributionSource);
    @JavaPassthrough(annotation="@android.annotation.RequiresPermission(allOf={android.Manifest.permission.BLUETOOTH_CONNECT,android.Manifest.permission.BLUETOOTH_PRIVILEGED}, conditional=true)")
    boolean disable(in AttributionSource attributionSource, boolean persist);

    const String GET_SYSTEM_STATE_API = "BluetoothAdapter_getSystemState";

    @JavaPassthrough(annotation="@android.annotation.RequiresNoPermission")
    int getState();

+9 −1
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@ package com.android.server.bluetooth

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothAdapter.STATE_OFF
import android.bluetooth.IBluetoothManager.GET_SYSTEM_STATE_API
import android.os.IpcDataCache
import com.android.bluetooth.flags.Flags
import kotlin.time.Duration
import kotlin.time.toKotlinDuration
import kotlinx.coroutines.flow.MutableSharedFlow
@@ -34,7 +37,12 @@ class BluetoothAdapterState {
        set(STATE_OFF)
    }

    fun set(s: Int) = runBlocking { _uiState.emit(s) }
    fun set(s: Int) = runBlocking {
        _uiState.emit(s)
        if (Flags.getStateFromSystemServer()) {
            IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, GET_SYSTEM_STATE_API)
        }
    }

    fun get(): Int = _uiState.replayCache.get(0)

Loading