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

Commit 3e69d73e authored by weichinweng's avatar weichinweng
Browse files

Excludes unexpected set service state intent

If the Bluetooth crashes when disabling Bluetooth service, the set
service state intent will be kept and be sent when the Bluetooth service
is enabling next time. The patch lists the profile service we want to
enable, then checks and excludes the unexpected intent when doing
doStart() and doStop().

Bug: 161092588
Test: atest BluetoothInstrumentationTests
Tag: #stability

Change-Id: I044149f149ae105e47a8e6ce6b10db7ab1e4a65b
parent 3cb0089c
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -138,6 +138,7 @@ public class AdapterService extends Service {
    private long mEnergyUsedTotalVoltAmpSecMicro;
    private final SparseArray<UidTraffic> mUidTraffic = new SparseArray<>();

    private final ArrayList<String> mStartedProfiles = new ArrayList<>();
    private final ArrayList<ProfileService> mRegisteredProfiles = new ArrayList<>();
    private final ArrayList<ProfileService> mRunningProfiles = new ArrayList<>();

@@ -305,6 +306,16 @@ public class AdapterService extends Service {
        mHandler.sendMessage(m);
    }

    /**
     * Confirm whether the ProfileService is started expectedly.
     *
     * @param string the service simple name.
     * @return true if the service is started expectedly, false otherwise.
     */
    public boolean isStartedProfile(String serviceSampleName) {
        return mStartedProfiles.contains(serviceSampleName);
    }

    private static final int MESSAGE_PROFILE_SERVICE_STATE_CHANGED = 1;
    private static final int MESSAGE_PROFILE_SERVICE_REGISTERED = 2;
    private static final int MESSAGE_PROFILE_SERVICE_UNREGISTERED = 3;
@@ -878,6 +889,11 @@ public class AdapterService extends Service {
    }

    private void setProfileServiceState(Class service, int state) {
        if (state == BluetoothAdapter.STATE_ON) {
            mStartedProfiles.add(service.getSimpleName());
        } else if (state == BluetoothAdapter.STATE_OFF) {
            mStartedProfiles.remove(service.getSimpleName());
        }
        Intent intent = new Intent(this, service);
        intent.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, state);
+8 −0
Original line number Diff line number Diff line
@@ -228,6 +228,10 @@ public abstract class ProfileService extends Service {
            Log.w(mName, "Could not add this profile because AdapterService is null.");
            return;
        }
        if (!mAdapterService.isStartedProfile(mName)) {
            Log.w(mName, "Unexpectedly do Start, don't start");
            return;
        }
        mAdapterService.addProfile(this);

        IntentFilter filter = new IntentFilter();
@@ -269,6 +273,10 @@ public abstract class ProfileService extends Service {
    }

    private void doStop() {
        if (mAdapterService == null || mAdapterService.isStartedProfile(mName)) {
            Log.w(mName, "Unexpectedly do Stop, don't stop.");
            return;
        }
        if (!mProfileStarted) {
            Log.w(mName, "doStop() called, but the profile is not running.");
        }
+2 −0
Original line number Diff line number Diff line
@@ -93,6 +93,7 @@ public class A2dpServiceTest {

        TestUtils.setAdapterService(mAdapterService);
        doReturn(MAX_CONNECTED_AUDIO_DEVICES).when(mAdapterService).getMaxConnectedAudioDevices();
        doReturn(true).when(mAdapterService).isStartedProfile(anyString());
        doReturn(false).when(mAdapterService).isQuietModeEnabled();
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();

@@ -125,6 +126,7 @@ public class A2dpServiceTest {
        if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_a2dp)) {
            return;
        }
        doReturn(false).when(mAdapterService).isStartedProfile(anyString());
        stopService();
        mTargetContext.unregisterReceiver(mA2dpIntentReceiver);
        mConnectionStateChangedQueue.clear();
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class A2dpSinkServiceTest {
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(true).when(mAdapterService).isStartedProfile(anyString());
        setMaxConnectedAudioDevices(1);
        TestUtils.startService(mServiceRule, A2dpSinkService.class);
        mService = A2dpSinkService.getA2dpSinkService();
@@ -76,6 +77,7 @@ public class A2dpSinkServiceTest {
        if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_a2dp_sink)) {
            return;
        }
        doReturn(false).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.stopService(mServiceRule, A2dpSinkService.class);
        mService = A2dpSinkService.getA2dpSinkService();
        Assert.assertNull(mService);
+5 −0
Original line number Diff line number Diff line
@@ -15,6 +15,9 @@
 */
package com.android.bluetooth.avrcpcontroller;

import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;

@@ -56,6 +59,7 @@ public class AvrcpControllerServiceTest {
                        .getBoolean(R.bool.profile_supported_avrcp_controller));
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        doReturn(true).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.startService(mServiceRule, AvrcpControllerService.class);
        mService = AvrcpControllerService.getAvrcpControllerService();
        Assert.assertNotNull(mService);
@@ -69,6 +73,7 @@ public class AvrcpControllerServiceTest {
        if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_avrcp_controller)) {
            return;
        }
        doReturn(false).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.stopService(mServiceRule, AvrcpControllerService.class);
        mService = AvrcpControllerService.getAvrcpControllerService();
        Assert.assertNull(mService);
Loading