A2dpSink: Exception handling in below cases.

- While updating RC features to APP, there is some exception due to
  avrcpcontrollerSM didn't started.

  To handle this, added exception handling while notifying RC features
  to App.

- When onConnectionStateChanged callback comes to app, AvrcpControllerSM
  has been instantiated , but not started. So while instantiating of
  AvrcpControllerSM, immediately we register for the Volume changed
  intent with Audio manager. So, when volume changed intent comes, than
  posting the VOLUME_CHANGED_ACTION message to statemachine, which was
  not started yet, which is leading to this exception.

  To handle this, register VOLUME_CHANGED_ACTION, after AvrcpControllerSM
  is started.

CRs-Fixed: 2483370
Change-Id: I352f7fa3e5952f1b7fd5cd02fcc97f27c1c5f63b
diff --git a/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java b/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java
index d3219e2..dede1a7 100644
--- a/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java
+++ b/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerService.java
@@ -40,6 +40,9 @@
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
 
+import android.content.res.Resources;
+import android.content.res.Resources.NotFoundException;
+
 /**
  * Provides Bluetooth AVRCP Controller profile, as a service in the Bluetooth application.
  */
@@ -329,6 +332,11 @@
         StackEvent event =
                 StackEvent.connectionStateChanged(remoteControlConnected, browsingConnected);
         AvrcpControllerStateMachine stateMachine = getOrCreateStateMachine(device);
+        if (stateMachine == null) {
+            Log.e(TAG, "onConnectionStateChanged: mAvrcpCtSm is null, return");
+            return;
+        }
+
         if (remoteControlConnected || browsingConnected) {
             stateMachine.connect(event);
         } else {
@@ -343,9 +351,17 @@
         mCaPsm = caPsm;
         BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
         AvrcpControllerStateMachine stateMachine = getStateMachine(device);
-        if (stateMachine != null) {
+        if (stateMachine == null) {
+            Log.e(TAG, "getRcFeatures: AvrcpControllerStateMachine is null for device: " + device);
+            return;
+        }
+
+        try {
             stateMachine.sendMessage(
                     AvrcpControllerStateMachine.MESSAGE_PROCESS_RC_FEATURES, features, caPsm, device);
+        } catch(Exception ee) {
+            Log.i(TAG, "getRcFeatures exception occured.");
+            ee.printStackTrace();
         }
     }
 
@@ -718,7 +734,10 @@
         if (stateMachine == null) {
             stateMachine = newStateMachine(device);
             mDeviceStateMap.put(device, stateMachine);
+            Log.d(TAG, "AvrcpControllerStateMachine start() called: " + device);
             stateMachine.start();
+            Log.d(TAG, "AvrcpcontrollerSM started for device: " + device);
+            stateMachine.registerReceiver(device);
         }
         return stateMachine;
     }
diff --git a/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java b/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
index 03d8f48..92a67db 100644
--- a/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
+++ b/src/com/android/bluetooth/avrcpcontroller/AvrcpControllerStateMachine.java
@@ -140,6 +140,7 @@
 
     AvrcpControllerStateMachine(BluetoothDevice device, AvrcpControllerService service) {
         super(TAG);
+        setDbg(DBG);
         mDevice = device;
         mDeviceAddress = Utils.getByteAddress(mDevice);
         mService = service;
@@ -164,12 +165,11 @@
 
         mAudioManager = (AudioManager) service.getSystemService(Context.AUDIO_SERVICE);
         mIsVolumeFixed = mAudioManager.isVolumeFixed();
-        IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
-        mService.registerReceiver(mBroadcastReceiver, filter);
 
         mCoveArtUtils = new CoverArtUtils();
         mBipStateMachine = AvrcpControllerBipStateMachine.make(this, getHandler(), service);
 
+        Log.d(TAG, "Setting initial state: Disconnected: " + mDevice);
         setInitialState(mDisconnected);
     }
 
@@ -285,7 +285,7 @@
     protected class Disconnected extends State {
         @Override
         public void enter() {
-            logD("Enter Disconnected");
+            logD("Enter Disconnected mDevice:" + mDevice);
             if (mMostRecentState != BluetoothProfile.STATE_DISCONNECTED) {
                 sendMessage(CLEANUP);
             }
@@ -817,6 +817,14 @@
         }
     };
 
+    void registerReceiver(BluetoothDevice device) {
+        if (DBG) {
+            Log.d(TAG, " Register receiver for device: " + device);
+        }
+        IntentFilter filter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION);
+        mService.registerReceiver(mBroadcastReceiver, filter);
+    }
+
     void doQuit() {
         Log.d(TAG, "doQuit()");
         try {