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

Commit 159a3655 authored by William Escande's avatar William Escande
Browse files

Fast bind to app implementation

OnCreate method is intended to return almost immediately in order for
the system Server to be notified in case of a crash.
By moving the initialization from OnCreate to "enable" we can be
notified of any crash happening during initialization and restart it a
few second later (instead of 90 min later by the system).

Bug: 328698375
Bug: 323672160
Test: atest CtsBluetoothTestCase
Test: atest pts-bot
Test: atest ServiceBluetoothTest
Change-Id: I34da95f878879c14e26b3989e676f71617a324ec
parent 90489b7d
Loading
Loading
Loading
Loading
+44 −13
Original line number Diff line number Diff line
@@ -525,7 +525,7 @@ public class AdapterService extends Service {
                    }
                    mRunningProfiles.add(profile);
                    // 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())) {
                        mNativeInterface.enable();
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
@@ -619,32 +619,59 @@ public class AdapterService extends Service {
    public void 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);
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            if (mLooper == null) {
                mLooper = Looper.getMainLooper();
            }
            mHandler = new AdapterServiceHandler(mLooper);
        }
        initMetricsLogger();
        mDeviceConfigListener.start();

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

        mRemoteDevices = new RemoteDevices(this, mLooper);
        mRemoteDevices.init();
        clearDiscoveringPackages();
        if (!Flags.fastBindToApp()) {
            mBinder = new AdapterServiceBinder(this);
        }
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            mAdapterProperties = new AdapterProperties(this);
            mAdapterStateMachine = new AdapterState(this, mLooper);
        }
        mBluetoothKeystoreService =
                new BluetoothKeystoreService(
                        BluetoothKeystoreNativeInterface.getInstance(), isCommonCriteriaMode());
@@ -5886,6 +5913,10 @@ public class AdapterService extends Service {
            debugLog("enable() called when Bluetooth was disallowed");
            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);
        mQuietmode = quietMode;
+6 −1
Original line number Diff line number Diff line
@@ -1803,7 +1803,9 @@ class BluetoothManagerService {
                            Log.e(TAG, "Unable to register BluetoothCallback", e);
                        }
                        // Inform BluetoothAdapter instances that service is up
                        if (!Flags.fastBindToApp()) {
                            sendBluetoothServiceUpCallback();
                        }

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