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

Commit fe818b0c authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Profile Service: Make onCreate() and onDestroy() symmetric

* Move profile service unregistration from onDestroy() to doStop()
* Make the field mAdapterService in ProfileService private

Bug: 67460963
Bug: 74511352
Test: instrumentation test
Change-Id: Ic583b895b81700dcea054bc58be8ed7e786d3778
(cherry picked from commit 2f37ccbd61df9ed864cfa0607b84b6f2ae4b3560)
parent 5894ee95
Loading
Loading
Loading
Loading
+8 −9
Original line number Diff line number Diff line
@@ -54,10 +54,10 @@ public abstract class ProfileService extends Service {
    //Profile services will not be automatically restarted.
    //They must be explicitly restarted by AdapterService
    private static final int PROFILE_SERVICE_MODE = Service.START_NOT_STICKY;
    protected final String mName;
    protected BluetoothAdapter mAdapter;
    protected AdapterService mAdapterService;
    protected IProfileServiceBinder mBinder;
    private BluetoothAdapter mAdapter;
    private IProfileServiceBinder mBinder;
    private final String mName;
    private AdapterService mAdapterService;
    private BroadcastReceiver mUserSwitchedReceiver;
    private boolean mProfileStarted = false;

@@ -208,17 +208,13 @@ public abstract class ProfileService extends Service {

    @Override
    public void onDestroy() {
        if (mAdapterService != null) {
            mAdapterService.removeProfile(this);
        }

        cleanup();
        if (mBinder != null) {
            mBinder.cleanup();
            mBinder = null;
        }
        super.onDestroy();
        mAdapter = null;
        super.onDestroy();
    }

    private void doStart() {
@@ -283,6 +279,9 @@ public abstract class ProfileService extends Service {
        if (!stop()) {
            Log.e(mName, "Unable to stop profile");
        }
        if (mAdapterService != null) {
            mAdapterService.removeProfile(this);
        }
        if (mUserSwitchedReceiver != null) {
            getApplicationContext().unregisterReceiver(mUserSwitchedReceiver);
            mUserSwitchedReceiver = null;
+2 −0
Original line number Diff line number Diff line
@@ -165,6 +165,7 @@ public class GattService extends ProfileService {

    private final Map<Integer, List<BluetoothGattService>> mGattClientDatabases = new HashMap<>();

    private BluetoothAdapter mAdapter;
    private AdvertiseManager mAdvertiseManager;
    private PeriodicScanManager mPeriodicScanManager;
    private ScanManager mScanManager;
@@ -192,6 +193,7 @@ public class GattService extends ProfileService {
            Log.d(TAG, "start()");
        }
        initializeNative();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAppOps = getSystemService(AppOpsManager.class);
        mAdvertiseManager = new AdvertiseManager(this, AdapterService.getAdapterService());
        mAdvertiseManager.start();
+3 −0
Original line number Diff line number Diff line
@@ -146,6 +146,8 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti

    boolean mAcceptNewConnections;

    private BluetoothAdapter mAdapter;

    private static final String INVISIBLE =
            BluetoothShare.VISIBILITY + "=" + BluetoothShare.VISIBILITY_HIDDEN;

@@ -206,6 +208,7 @@ public class BluetoothOppService extends ProfileService implements IObexConnecti
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothReceiver, filter);

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        synchronized (BluetoothOppService.this) {
            if (mAdapter == null) {
                Log.w(TAG, "Local BT device is not enabled");
+26 −0
Original line number Diff line number Diff line
@@ -188,4 +188,30 @@ public class ProfileServiceTest {
            profileNumber += 1;
        }
    }

    /**
     * Test: Start and stop a single profile repeatedly and verify that the profile services are
     * registered and unregistered accordingly.
     */
    @Test
    public void testProfileServiceRegisterUnregister() throws TimeoutException {
        int profileNumber = 0;
        for (Class profile : mProfiles) {
            for (int i = 0; i < NUM_REPEATS; i++) {
                setProfileState(profile, BluetoothAdapter.STATE_ON);
                ArgumentCaptor<ProfileService> start =
                        ArgumentCaptor.forClass(ProfileService.class);
                verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times(
                        NUM_REPEATS * profileNumber + i + 1)).addProfile(
                        start.capture());
                setProfileState(profile, BluetoothAdapter.STATE_OFF);
                ArgumentCaptor<ProfileService> stop = ArgumentCaptor.forClass(ProfileService.class);
                verify(mMockAdapterService, timeout(PROFILE_START_MILLIS).times(
                        NUM_REPEATS * profileNumber + i + 1)).removeProfile(
                        stop.capture());
                Assert.assertEquals(start.getValue(), stop.getValue());
            }
            profileNumber += 1;
        }
    }
}