From 8377ec4771623484a4cc972efb452f1b5b004639 Mon Sep 17 00:00:00 2001
From: Kamal Negi <kamaln@codeaurora.org>
Date: Tue, 5 Jul 2016 10:42:45 +0530
Subject: [PATCH] Toggle FM on receiving AVRCP play command

While FM is playing over BT and press the play/pause button from BT
headset, receiving the AVRCP play command from BT headset. On receving AVRCP
play command from BT headset, if FM is already on, we don't turn off the FM and
FM keeps playing.
Toggle the FM to on/off on receving the play command.

Change-Id: Iee34ca9d40929a0f5fd04fcfba0c42f29a0dd388
---
 .../src/com/caf/fmradio/FMRadioService.java   | 27 ++++++++++++++-----
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java
index 002224d..762a969 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadioService.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java
@@ -976,13 +976,28 @@ public class FMRadioService extends Service
                        && (key_action == KeyEvent.ACTION_DOWN)) {
                 Log.d(LOGTAG, "SessionCallback: MEDIA_PLAY");
                 if (isAntennaAvailable() && mServiceInUse) {
-                    fmOn();
-                    try {
-                        if (mCallbacks != null ) {
-                            mCallbacks.onEnabled();
+                    if (isFmOn()){
+                        //FM should be off when Headset hook pressed.
+                        fmOff();
+                        try {
+                            /* Notify the UI/Activity, only if the service is "bound"
+                             * by an activity and if Callbacks are registered
+                             * */
+                            if ((mServiceInUse) && (mCallbacks != null) ) {
+                                mCallbacks.onDisabled();
+                            }
+                        } catch (RemoteException e) {
+                            e.printStackTrace();
+                        }
+                    } else {
+                        fmOn();
+                        try {
+                            if (mCallbacks != null ) {
+                                mCallbacks.onEnabled();
+                            }
+                        } catch (RemoteException e) {
+                            e.printStackTrace();
                         }
-                    } catch (RemoteException e) {
-                        e.printStackTrace();
                     }
                     return true;
                 }
-- 
GitLab