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

Commit e6b242f0 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Fix NPE in SapService

An intent with ACTION_CONNECTION_ACCESS_REPLY can be recevied after
the USER_TIMEOUT then mRemoteDevice would become null, and NPE happens.

Bug: 258140884
Test: atest BluetoothInstrumentationTests:SapServiceTest
Change-Id: Ia622cef8508001fd7baee7b362cdba393a774169
parent 5e12b233
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -811,9 +811,10 @@ public class SapService extends ProfileService {
        } // Can only be null during shutdown
    }

    private SapBroadcastReceiver mSapReceiver = new SapBroadcastReceiver();
    @VisibleForTesting SapBroadcastReceiver mSapReceiver = new SapBroadcastReceiver();

    private class SapBroadcastReceiver extends BroadcastReceiver {
    @VisibleForTesting
    class SapBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

@@ -844,7 +845,8 @@ public class SapService extends ProfileService {
                Log.v(TAG, " - Received BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY");

                int requestType = intent.getIntExtra(BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, -1);
                if (requestType != BluetoothDevice.REQUEST_TYPE_SIM_ACCESS) {
                if (requestType != BluetoothDevice.REQUEST_TYPE_SIM_ACCESS
                        || !mIsWaitingAuthorization) {
                    return;
                }

+9 −3
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
@@ -37,7 +38,6 @@ import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -151,6 +151,12 @@ public class SapServiceTest {
    public void testGetRemoteDeviceName() {
        assertThat(SapService.getRemoteDeviceName()).isNull();
    }
}


    @Test
    public void testReceiver_ConnectionAccessReplyIntent_shouldNotCrash() {
        Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY);
        intent.putExtra(
                BluetoothDevice.EXTRA_ACCESS_REQUEST_TYPE, BluetoothDevice.REQUEST_TYPE_SIM_ACCESS);
        mService.mSapReceiver.onReceive(mTargetContext, intent);
    }
}