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

Commit 7ada9e56 authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge changes I34da95f8,I501bcb52,I7c4feb5d,Ic354cc9b into main

* changes:
  Fast bind to app implementation
  AdapterService: Set RemoteCallbackList final
  AdapterService: prepare fast service create
  Flag: Add fast_bind_to_app
parents f290d5ad 159a3655
Loading
Loading
Loading
Loading
+92 −75
Original line number Original line Diff line number Diff line
@@ -340,15 +340,15 @@ public class AdapterService extends Service {
            mMetadataListeners = new HashMap<>();
            mMetadataListeners = new HashMap<>();
    private final HashMap<String, Integer> mProfileServicesState = new HashMap<String, Integer>();
    private final HashMap<String, Integer> mProfileServicesState = new HashMap<String, Integer>();
    private Set<IBluetoothConnectionCallback> mBluetoothConnectionCallbacks = new HashSet<>();
    private Set<IBluetoothConnectionCallback> mBluetoothConnectionCallbacks = new HashSet<>();
    private RemoteCallbackList<IBluetoothPreferredAudioProfilesCallback>
    private final RemoteCallbackList<IBluetoothPreferredAudioProfilesCallback>
            mPreferredAudioProfilesCallbacks;
            mPreferredAudioProfilesCallbacks = new RemoteCallbackList<>();
    private RemoteCallbackList<IBluetoothQualityReportReadyCallback>
    private final RemoteCallbackList<IBluetoothQualityReportReadyCallback>
            mBluetoothQualityReportReadyCallbacks;
            mBluetoothQualityReportReadyCallbacks = new RemoteCallbackList<>();
    // Map<groupId, PendingAudioProfilePreferenceRequest>
    // Map<groupId, PendingAudioProfilePreferenceRequest>
    private final Map<Integer, PendingAudioProfilePreferenceRequest>
    private final Map<Integer, PendingAudioProfilePreferenceRequest>
            mCsipGroupsPendingAudioProfileChanges = new HashMap<>();
            mCsipGroupsPendingAudioProfileChanges = new HashMap<>();
    // Only BluetoothManagerService should be registered
    private final RemoteCallbackList<IBluetoothCallback>
    private RemoteCallbackList<IBluetoothCallback> mRemoteCallbacks;
            mRemoteCallbacks = new RemoteCallbackList<>();
    private final Map<BluetoothStateCallback, Executor> mLocalCallbacks = new ConcurrentHashMap<>();
    private final Map<BluetoothStateCallback, Executor> mLocalCallbacks = new ConcurrentHashMap<>();
    private int mCurrentRequestId;
    private int mCurrentRequestId;
    private boolean mQuietmode = false;
    private boolean mQuietmode = false;
@@ -525,7 +525,7 @@ public class AdapterService extends Service {
                    }
                    }
                    mRunningProfiles.add(profile);
                    mRunningProfiles.add(profile);
                    // TODO(b/228875190): GATT is assumed supported. GATT starting triggers hardware
                    // TODO(b/228875190): GATT is assumed supported. GATT starting triggers hardware
                    // initializtion. Configuring a device without GATT causes start up failures.
                    // initialization. Configuring a device without GATT causes start up failures.
                    if (GattService.class.getSimpleName().equals(profile.getName())) {
                    if (GattService.class.getSimpleName().equals(profile.getName())) {
                        mNativeInterface.enable();
                        mNativeInterface.enable();
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
@@ -618,28 +618,60 @@ public class AdapterService extends Service {
            })
            })
    public void onCreate() {
    public void onCreate() {
        super.onCreate();
        super.onCreate();
        debugLog("onCreate()");
        if (!Flags.fastBindToApp()) {
            init();
            return;
        }
        // OnCreate must perform the minimum of infaillible and mandatory initialization
        if (mLooper == null) {
            mLooper = Looper.getMainLooper();
        }
        mHandler = new AdapterServiceHandler(mLooper);
        mAdapterProperties = new AdapterProperties(this);
        mAdapterStateMachine = new AdapterState(this, mLooper);
        mBinder = new AdapterServiceBinder(this);
        mUserManager = getNonNullSystemService(UserManager.class);
        mAppOps = getNonNullSystemService(AppOpsManager.class);
        mPowerManager = getNonNullSystemService(PowerManager.class);
        mBatteryStatsManager = getNonNullSystemService(BatteryStatsManager.class);
        mCompanionDeviceManager = getNonNullSystemService(CompanionDeviceManager.class);
    }

    private void init() {
        debugLog("init()");
        Config.init(this);
        Config.init(this);
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            if (mLooper == null) {
            if (mLooper == null) {
                mLooper = Looper.getMainLooper();
                mLooper = Looper.getMainLooper();
            }
            }
            mHandler = new AdapterServiceHandler(mLooper);
            mHandler = new AdapterServiceHandler(mLooper);
        }
        initMetricsLogger();
        initMetricsLogger();
        debugLog("onCreate()");
        mDeviceConfigListener.start();
        mDeviceConfigListener.start();


        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            mUserManager = getNonNullSystemService(UserManager.class);
            mUserManager = getNonNullSystemService(UserManager.class);
            mAppOps = getNonNullSystemService(AppOpsManager.class);
            mAppOps = getNonNullSystemService(AppOpsManager.class);
            mPowerManager = getNonNullSystemService(PowerManager.class);
            mPowerManager = getNonNullSystemService(PowerManager.class);
            mBatteryStatsManager = getNonNullSystemService(BatteryStatsManager.class);
            mBatteryStatsManager = getNonNullSystemService(BatteryStatsManager.class);
            mCompanionDeviceManager = getNonNullSystemService(CompanionDeviceManager.class);
            mCompanionDeviceManager = getNonNullSystemService(CompanionDeviceManager.class);
        }


        mRemoteDevices = new RemoteDevices(this, mLooper);
        mRemoteDevices = new RemoteDevices(this, mLooper);
        mRemoteDevices.init();
        mRemoteDevices.init();
        clearDiscoveringPackages();
        clearDiscoveringPackages();
        if (!Flags.fastBindToApp()) {
            mBinder = new AdapterServiceBinder(this);
            mBinder = new AdapterServiceBinder(this);
        }
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            mAdapterProperties = new AdapterProperties(this);
            mAdapterProperties = new AdapterProperties(this);
            mAdapterStateMachine = new AdapterState(this, mLooper);
            mAdapterStateMachine = new AdapterState(this, mLooper);
        }
        mBluetoothKeystoreService =
        mBluetoothKeystoreService =
                new BluetoothKeystoreService(
                new BluetoothKeystoreService(
                        BluetoothKeystoreNativeInterface.getInstance(), isCommonCriteriaMode());
                        BluetoothKeystoreNativeInterface.getInstance(), isCommonCriteriaMode());
@@ -664,11 +696,6 @@ public class AdapterService extends Service {
                isAtvDevice,
                isAtvDevice,
                getApplicationInfo().dataDir);
                getApplicationInfo().dataDir);
        mNativeAvailable = true;
        mNativeAvailable = true;
        mPreferredAudioProfilesCallbacks =
                new RemoteCallbackList<IBluetoothPreferredAudioProfilesCallback>();
        mBluetoothQualityReportReadyCallbacks =
                new RemoteCallbackList<IBluetoothQualityReportReadyCallback>();
        mRemoteCallbacks = new RemoteCallbackList<IBluetoothCallback>();
        // Load the name and address
        // Load the name and address
        mNativeInterface.getAdapterProperty(AbstractionLayer.BT_PROPERTY_BDADDR);
        mNativeInterface.getAdapterProperty(AbstractionLayer.BT_PROPERTY_BDADDR);
        mNativeInterface.getAdapterProperty(AbstractionLayer.BT_PROPERTY_BDNAME);
        mNativeInterface.getAdapterProperty(AbstractionLayer.BT_PROPERTY_BDNAME);
@@ -1103,7 +1130,7 @@ public class AdapterService extends Service {
        mAdapterProperties.setState(newState);
        mAdapterProperties.setState(newState);
        invalidateBluetoothGetStateCache();
        invalidateBluetoothGetStateCache();


        if (mRemoteCallbacks != null) {
        // Only BluetoothManagerService should be registered
        int n = mRemoteCallbacks.beginBroadcast();
        int n = mRemoteCallbacks.beginBroadcast();
        debugLog(
        debugLog(
                "updateAdapterState() - Broadcasting state "
                "updateAdapterState() - Broadcasting state "
@@ -1121,7 +1148,6 @@ public class AdapterService extends Service {
            }
            }
        }
        }
        mRemoteCallbacks.finishBroadcast();
        mRemoteCallbacks.finishBroadcast();
        }


        for (Map.Entry<BluetoothStateCallback, Executor> e : mLocalCallbacks.entrySet()) {
        for (Map.Entry<BluetoothStateCallback, Executor> e : mLocalCallbacks.entrySet()) {
            e.getValue().execute(() -> e.getKey().onBluetoothStateChange(prevState, newState));
            e.getValue().execute(() -> e.getKey().onBluetoothStateChange(prevState, newState));
@@ -1243,7 +1269,6 @@ public class AdapterService extends Service {
    public int bluetoothQualityReportReadyCallback(
    public int bluetoothQualityReportReadyCallback(
            BluetoothDevice device, BluetoothQualityReport bluetoothQualityReport) {
            BluetoothDevice device, BluetoothQualityReport bluetoothQualityReport) {
        synchronized (mBluetoothQualityReportReadyCallbacks) {
        synchronized (mBluetoothQualityReportReadyCallbacks) {
            if (mBluetoothQualityReportReadyCallbacks != null) {
            int n = mBluetoothQualityReportReadyCallbacks.beginBroadcast();
            int n = mBluetoothQualityReportReadyCallbacks.beginBroadcast();
            debugLog(
            debugLog(
                    "bluetoothQualityReportReadyCallback() - "
                    "bluetoothQualityReportReadyCallback() - "
@@ -1269,7 +1294,6 @@ public class AdapterService extends Service {
            }
            }
            mBluetoothQualityReportReadyCallbacks.finishBroadcast();
            mBluetoothQualityReportReadyCallbacks.finishBroadcast();
        }
        }
        }


        return BluetoothStatusCodes.SUCCESS;
        return BluetoothStatusCodes.SUCCESS;
    }
    }
@@ -1406,18 +1430,12 @@ public class AdapterService extends Service {
            mBinder = null; // Do not remove. Otherwise Binder leak!
            mBinder = null; // Do not remove. Otherwise Binder leak!
        }
        }


        if (mPreferredAudioProfilesCallbacks != null) {
        mPreferredAudioProfilesCallbacks.kill();
        mPreferredAudioProfilesCallbacks.kill();
        }


        if (mBluetoothQualityReportReadyCallbacks != null) {
        mBluetoothQualityReportReadyCallbacks.kill();
        mBluetoothQualityReportReadyCallbacks.kill();
        }


        if (mRemoteCallbacks != null) {
        mRemoteCallbacks.kill();
        mRemoteCallbacks.kill();
    }
    }
    }


    private void invalidateBluetoothCaches() {
    private void invalidateBluetoothCaches() {
        BluetoothAdapter.invalidateGetProfileConnectionStateCache();
        BluetoothAdapter.invalidateGetProfileConnectionStateCache();
@@ -4266,7 +4284,6 @@ public class AdapterService extends Service {
        private void unregisterCallback(IBluetoothCallback callback, AttributionSource source) {
        private void unregisterCallback(IBluetoothCallback callback, AttributionSource source) {
            AdapterService service = getService();
            AdapterService service = getService();
            if (service == null
            if (service == null
                    || service.mRemoteCallbacks == null
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "unregisterCallback")
                    || !callerIsSystemOrActiveOrManagedUser(service, TAG, "unregisterCallback")
                    || !Utils.checkConnectPermissionForDataDelivery(service, source, TAG)) {
                    || !Utils.checkConnectPermissionForDataDelivery(service, source, TAG)) {
                return;
                return;
@@ -5849,10 +5866,6 @@ public class AdapterService extends Service {


    private void sendPreferredAudioProfilesCallbackToApps(
    private void sendPreferredAudioProfilesCallbackToApps(
            BluetoothDevice device, Bundle preferredAudioProfiles, int status) {
            BluetoothDevice device, Bundle preferredAudioProfiles, int status) {
        if (mPreferredAudioProfilesCallbacks == null) {
            return;
        }

        int n = mPreferredAudioProfilesCallbacks.beginBroadcast();
        int n = mPreferredAudioProfilesCallbacks.beginBroadcast();
        debugLog(
        debugLog(
                "sendPreferredAudioProfilesCallbackToApps() - Broadcasting audio profile "
                "sendPreferredAudioProfilesCallbackToApps() - Broadcasting audio profile "
@@ -5901,6 +5914,10 @@ public class AdapterService extends Service {
            debugLog("enable() called when Bluetooth was disallowed");
            debugLog("enable() called when Bluetooth was disallowed");
            return false;
            return false;
        }
        }
        if (Flags.fastBindToApp()) {
            // The call to init must be done on the main thread
            mHandler.post(() -> init());
        }


        debugLog("enable() - Enable called with quiet mode status =  " + quietMode);
        debugLog("enable() - Enable called with quiet mode status =  " + quietMode);
        mQuietmode = quietMode;
        mQuietmode = quietMode;
+7 −0
Original line number Original line Diff line number Diff line
@@ -22,6 +22,13 @@ flag {
    bug: "321804999"
    bug: "321804999"
}
}


flag {
    name: "fast_bind_to_app"
    namespace: "bluetooth"
    description: "Remove complexity and non necessary initialization when simply binding"
    bug: "328698375"
}

flag {
flag {
    name: "use_new_airplane_mode"
    name: "use_new_airplane_mode"
    namespace: "bluetooth"
    namespace: "bluetooth"
+6 −1
Original line number Original line Diff line number Diff line
@@ -1810,7 +1810,9 @@ class BluetoothManagerService {
                            Log.e(TAG, "Unable to register BluetoothCallback", e);
                            Log.e(TAG, "Unable to register BluetoothCallback", e);
                        }
                        }
                        // Inform BluetoothAdapter instances that service is up
                        // Inform BluetoothAdapter instances that service is up
                        if (!Flags.fastBindToApp()) {
                            sendBluetoothServiceUpCallback();
                            sendBluetoothServiceUpCallback();
                        }


                        // Get the supported profiles list
                        // Get the supported profiles list
                        try {
                        try {
@@ -1828,6 +1830,9 @@ class BluetoothManagerService {
                        } catch (RemoteException | TimeoutException e) {
                        } catch (RemoteException | TimeoutException e) {
                            Log.e(TAG, "Unable to call enable()", e);
                            Log.e(TAG, "Unable to call enable()", e);
                        }
                        }
                        if (Flags.fastBindToApp()) {
                            sendBluetoothServiceUpCallback();
                        }
                    } finally {
                    } finally {
                        mAdapterLock.writeLock().unlock();
                        mAdapterLock.writeLock().unlock();
                    }
                    }