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

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

Merge "BluetoothInCallService: register broadcast receiver at onCreate and...

Merge "BluetoothInCallService: register broadcast receiver at onCreate and unregister it at onDestroy"
parents 303721b4 9df4cb92
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -138,16 +138,14 @@ public class BluetoothInCallService extends InCallService {
                }
            };

    /**
     * Receives events for global state changes of the bluetooth adapter.
     */
    // TODO: The code is moved from Telecom stack. Since we're running in the BT process itself,
    // we may be able to simplify this in a future patch.
    @VisibleForTesting
    public final BroadcastReceiver mBluetoothAdapterReceiver = new BroadcastReceiver() {
    public class BluetoothAdapterReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (LOCK) {
                if (intent.getAction() != BluetoothAdapter.ACTION_STATE_CHANGED) {
                    Log.w(TAG, "BluetoothAdapterReceiver: Intent action " + intent.getAction());
                    return;
                }
                int state = intent
                        .getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                Log.d(TAG, "Bluetooth Adapter state: " + state);
@@ -158,6 +156,14 @@ public class BluetoothInCallService extends InCallService {
        }
    };

    /**
     * Receives events for global state changes of the bluetooth adapter.
     */
    // TODO: The code is moved from Telecom stack. Since we're running in the BT process itself,
    // we may be able to simplify this in a future patch.
    @VisibleForTesting
    public BluetoothAdapterReceiver mBluetoothAdapterReceiver;

    @VisibleForTesting
    public class CallStateCallback extends Call.Callback {
        public int mLastState;
@@ -290,8 +296,6 @@ public class BluetoothInCallService extends InCallService {
            return null;
        }
        IBinder binder = super.onBind(intent);
        IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothAdapterReceiver, intentFilter);
        mTelephonyManager = getSystemService(TelephonyManager.class);
        mTelecomManager = getSystemService(TelecomManager.class);
        return binder;
@@ -300,12 +304,11 @@ public class BluetoothInCallService extends InCallService {
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(TAG, "onUnbind. Intent: " + intent);
        unregisterReceiver(mBluetoothAdapterReceiver);
        return super.onUnbind(intent);
    }

    public BluetoothInCallService() {
        Log.i(TAG, "onCreate");
        Log.i(TAG, "BluetoothInCallService is created");
        BluetoothAdapter.getDefaultAdapter()
                .getProfileProxy(this, mProfileListener, BluetoothProfile.HEADSET);
        sInstance = this;
@@ -490,11 +493,18 @@ public class BluetoothInCallService extends InCallService {
    public void onCreate() {
        Log.d(TAG, "onCreate");
        super.onCreate();
        mBluetoothAdapterReceiver = new BluetoothAdapterReceiver();
        IntentFilter intentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mBluetoothAdapterReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        if (mBluetoothAdapterReceiver != null) {
            unregisterReceiver(mBluetoothAdapterReceiver);
            mBluetoothAdapterReceiver = null;
        }
        super.onDestroy();
    }

+3 −1
Original line number Diff line number Diff line
@@ -1096,9 +1096,11 @@ public class BluetoothInCallServiceTest {
        BluetoothCall ringingCall = createRingingCall();
        when(ringingCall.getHandle()).thenReturn(Uri.parse("tel:5550000"));

        Intent intent = new Intent();
        Intent intent = new Intent(BluetoothAdapter.ACTION_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_ON);
        clearInvocations(mMockBluetoothHeadset);
        mBluetoothInCallService.mBluetoothAdapterReceiver
                = mBluetoothInCallService.new BluetoothAdapterReceiver();
        mBluetoothInCallService.mBluetoothAdapterReceiver
                .onReceive(mBluetoothInCallService, intent);