BluetoothInCallService: register broadcast receiver at onCreate and
unregister it at onDestroy

Bug: 176220236
Test: atest BluetoothInstrumentationTests
Tag: #stability

Change-Id: I8dcb230b7b6cf9e96414dad3005ac8b2a2aa0e11
diff --git a/src/com/android/bluetooth/hfp/BluetoothInCallService.java b/src/com/android/bluetooth/hfp/BluetoothInCallService.java
index c25992d..7444ffc 100644
--- a/src/com/android/bluetooth/hfp/BluetoothInCallService.java
+++ b/src/com/android/bluetooth/hfp/BluetoothInCallService.java
@@ -138,16 +138,14 @@
                 }
             };
 
-    /**
-     * 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 @@
         }
     };
 
+    /**
+     * 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 @@
             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 @@
     @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 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();
     }
 
diff --git a/tests/unit/src/com/android/bluetooth/hfp/BluetoothInCallServiceTest.java b/tests/unit/src/com/android/bluetooth/hfp/BluetoothInCallServiceTest.java
index c74bc8b..d3db9c6 100644
--- a/tests/unit/src/com/android/bluetooth/hfp/BluetoothInCallServiceTest.java
+++ b/tests/unit/src/com/android/bluetooth/hfp/BluetoothInCallServiceTest.java
@@ -1096,10 +1096,12 @@
         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);
 
         verify(mMockBluetoothHeadset).phoneStateChanged(eq(0), eq(0), eq(CALL_STATE_INCOMING),