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

Commit 96c8d709 authored by William Escande's avatar William Escande
Browse files

InCallService: getService during onCreate

Since there is no guarantee for when onBind will be called, we cannot
use it to initialize critical systemService

Bug: 330247213
Fix: 330247213
Test: atest BluetoothInstrumentationTests:BluetoothInCallServiceTests
Test: atest BluetoothInstrumentationTests:CallInfoTest
Flag: Exempt refactor
Change-Id: I29459364d9425fef6b8b7ece8b5ff6a072f23bc3
parent bdca2885
Loading
Loading
Loading
Loading
+5 −9
Original line number Diff line number Diff line
@@ -122,9 +122,8 @@ public class BluetoothInCallService extends InCallService {
    @VisibleForTesting BluetoothLeCallControlProxy mBluetoothLeCallControl;
    private ExecutorService mExecutor;

    @VisibleForTesting public TelephonyManager mTelephonyManager;

    @VisibleForTesting public TelecomManager mTelecomManager;
    private TelephonyManager mTelephonyManager;
    private TelecomManager mTelecomManager;

    @VisibleForTesting
    public final HashMap<Integer, CallStateCallback> mCallbacks = new HashMap<>();
@@ -334,10 +333,7 @@ public class BluetoothInCallService extends InCallService {
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind. Intent: " + intent);
        IBinder binder = super.onBind(intent);
        mTelephonyManager = getSystemService(TelephonyManager.class);
        mTelecomManager = getSystemService(TelecomManager.class);
        return binder;
        return super.onBind(intent);
    }

    @Override
@@ -367,8 +363,6 @@ public class BluetoothInCallService extends InCallService {
        mBluetoothHeadset = headset;
        mBluetoothLeCallControl = leCallControl;
        attachBaseContext(context);
        mTelephonyManager = getSystemService(TelephonyManager.class);
        mTelecomManager = getSystemService(TelecomManager.class);
    }

    public static BluetoothInCallService getInstance() {
@@ -736,6 +730,8 @@ public class BluetoothInCallService extends InCallService {
        Log.d(TAG, "onCreate");
        super.onCreate();
        mAdapter = requireNonNull(getSystemService(BluetoothManager.class)).getAdapter();
        mTelephonyManager = getSystemService(TelephonyManager.class);
        mTelecomManager = getSystemService(TelecomManager.class);
        mAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
        mAdapter.getProfileProxy(this, mProfileListener, BluetoothProfile.LE_CALL_CONTROL);
        mBluetoothAdapterReceiver = new BluetoothAdapterReceiver();
+13 −4
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.net.Uri;
import android.os.Process;
import android.telecom.Call;
@@ -31,15 +33,17 @@ import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telecom.TelecomManager;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@@ -57,15 +61,21 @@ public class CallInfoTest {

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock private TelecomManager mMockTelecomManager;
    private TelecomManager mMockTelecomManager;

    private BluetoothInCallService mBluetoothInCallService;
    private BluetoothInCallService.CallInfo mMockCallInfo;

    @Before
    public void setUp() throws Exception {
        Context spiedContext = spy(new ContextWrapper(ApplicationProvider.getApplicationContext()));
        mMockTelecomManager =
                TestUtils.mockGetSystemService(
                        spiedContext, Context.TELECOM_SERVICE, TelecomManager.class);

        mBluetoothInCallService = new BluetoothInCallService(spiedContext, null, null, null);
        mBluetoothInCallService.onCreate();

        mBluetoothInCallService = new BluetoothInCallService();
        mMockCallInfo = spy(mBluetoothInCallService.new CallInfo());
    }

@@ -273,7 +283,6 @@ public class CallInfoTest {

        PhoneAccount fakePhoneAccount = makeQuickAccount(testId, TEST_ACCOUNT_INDEX);
        when(mMockTelecomManager.getPhoneAccount(testHandle)).thenReturn(fakePhoneAccount);
        mBluetoothInCallService.mTelecomManager = mMockTelecomManager;

        assertThat(mMockCallInfo.getBestPhoneAccount()).isEqualTo(fakePhoneAccount);
    }