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

Commit 94c3aee0 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Fix race condition that is causing a NPE by instantiating the instance...

Merge "Fix race condition that is causing a NPE by instantiating the instance in OnCreate instead of the constructor" into main
parents 97e1b883 eeafcce9
Loading
Loading
Loading
Loading
+17 −21
Original line number Diff line number Diff line
@@ -148,8 +148,6 @@ public class BluetoothInCallService extends InCallService {

    private final CallInfo mCallInfo;

    protected boolean mOnCreateCalled = false;

    private int mMaxNumberOfCalls = 0;

    private BluetoothAdapter mAdapter = null;
@@ -367,7 +365,6 @@ public class BluetoothInCallService extends InCallService {
    private BluetoothInCallService(CallInfo callInfo) {
        Log.i(TAG, "BluetoothInCallService is created");
        mCallInfo = Objects.requireNonNullElseGet(callInfo, () -> new CallInfo());
        sInstance = this;
        mExecutor = Executors.newSingleThreadExecutor();
    }

@@ -546,10 +543,6 @@ public class BluetoothInCallService extends InCallService {
    @RequiresPermission(allOf = {BLUETOOTH_CONNECT, MODIFY_PHONE_STATE})
    public boolean listCurrentCalls() {
        synchronized (LOCK) {
            if (!mOnCreateCalled) {
                Log.w(TAG, "listcurrentCalls() is called before onCreate()");
                return false;
            }
            enforceModifyPermission();
            // only log if it is after we recently updated the headset state or else it can
            // clog the android log since this can be queried every second.
@@ -780,8 +773,9 @@ public class BluetoothInCallService extends InCallService {

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        super.onCreate();
        synchronized (LOCK) {
            Log.d(TAG, "onCreate");
            mAdapter = requireNonNull(getSystemService(BluetoothManager.class)).getAdapter();
            mTelephonyManager = requireNonNull(getSystemService(TelephonyManager.class));
            mTelecomManager = requireNonNull(getSystemService(TelecomManager.class));
@@ -791,14 +785,16 @@ public class BluetoothInCallService extends InCallService {
            IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
            intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
            registerReceiver(mBluetoothAdapterReceiver, intentFilter);
        mOnCreateCalled = true;
            sInstance = this;
        }
    }

    @Override
    public void onDestroy() {
        synchronized (LOCK) {
            Log.d(TAG, "onDestroy");
            clear();
        mOnCreateCalled = false;
        }
        super.onDestroy();
    }

+2 −2
Original line number Diff line number Diff line
@@ -1813,11 +1813,11 @@ public class BluetoothInCallServiceTest {

    @Test
    public void onDestroy() {
        assertThat(mBluetoothInCallService.mOnCreateCalled).isTrue();
        assertThat(BluetoothInCallService.getInstance()).isNotNull();

        mBluetoothInCallService.onDestroy();

        assertThat(mBluetoothInCallService.mOnCreateCalled).isFalse();
        assertThat(BluetoothInCallService.getInstance()).isNull();
    }

    @Test