From fd07e8075addefd193b0365afc8900de93a58d3b Mon Sep 17 00:00:00 2001 From: Rupesh Tatiya <rtatiya@codeaurora.org> Date: Wed, 4 Nov 2015 17:51:44 +0530 Subject: [PATCH] Change FM off button to release media and audio resources. When FM application is launched, no other Media consuming application can receive key events (play, pause etc.) because FM service is the owner of MediaSession and AudioFocus. This does not bode well for user experience. Change the behaviour of FM off button so that we release MediaSession and AudioFocus so that other applications can work. The same behaviour is enabled when antenna is pulled out. We differentiate FM off from remote bluetooth headset start/stop i.e. when remote headset does start/stop, it starts/stops FM. This means that if user presses FM off button, user has to explicitly turn it on again. Also remove old FM Media Button Intent receiver as it is deprecated. CRs-Fixed: 936190 Change-Id: I32fef8e43b226a260d5483b40f5825926c023e62 --- fmapp2/AndroidManifest.xml | 1 - fmapp2/src/com/caf/fmradio/FMRadio.java | 2 +- .../src/com/caf/fmradio/FMRadioService.java | 93 +++++-------------- 3 files changed, 24 insertions(+), 72 deletions(-) diff --git a/fmapp2/AndroidManifest.xml b/fmapp2/AndroidManifest.xml index 92af7e3..bc383fe 100644 --- a/fmapp2/AndroidManifest.xml +++ b/fmapp2/AndroidManifest.xml @@ -73,7 +73,6 @@ <receiver android:name="com.caf.fmradio.FMMediaButtonIntentReceiver"> <intent-filter> - <action android:name="android.intent.action.MEDIA_BUTTON" /> <action android:name="android.media.AUDIO_BECOMING_NOISY" /> </intent-filter> </receiver> diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java index 3537137..5cfd5a6 100644 --- a/fmapp2/src/com/caf/fmradio/FMRadio.java +++ b/fmapp2/src/com/caf/fmradio/FMRadio.java @@ -2622,7 +2622,7 @@ public class FMRadio extends Activity @Override public boolean onKeyDown(int keyCode, KeyEvent event) { - Log.d(LOGTAG, "KEY event received" + keyCode); + Log.d(LOGTAG, "KEY event received " + keyCode); switch (keyCode) { case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: case 126: //KeyEvent.KEYCODE_MEDIA_PLAY: diff --git a/fmapp2/src/com/caf/fmradio/FMRadioService.java b/fmapp2/src/com/caf/fmradio/FMRadioService.java index 98fac7a..149c248 100644 --- a/fmapp2/src/com/caf/fmradio/FMRadioService.java +++ b/fmapp2/src/com/caf/fmradio/FMRadioService.java @@ -213,6 +213,9 @@ public class FMRadioService extends Service private Object mRecordSinkLock = new Object(); private boolean mIsFMDeviceLoopbackActive = false; + private static final int FM_OFF_FROM_APPLICATION = 1; + private static final int FM_OFF_FROM_ANTENNA = 2; + public FMRadioService() { } @@ -238,9 +241,7 @@ public class FMRadioService extends Service registerDelayedServiceStop(); registerExternalStorageListener(); registerAirplaneModeStatusChanged(); - // registering media button receiver seperately as we need to set - // different priority for receiving media events - registerFmMediaButtonReceiver(); + mSession = new MediaSession(getApplicationContext(), this.getClass().getName()); mSession.setCallback(mSessionCallback); mSession.setFlags(MediaSession.FLAG_EXCLUSIVE_GLOBAL_PRIORITY | @@ -665,71 +666,6 @@ public class FMRadioService extends Service } } - public void registerFmMediaButtonReceiver() { - if (mFmMediaButtonListener == null) { - mFmMediaButtonListener = new BroadcastReceiver() { - public void onReceive(Context context, Intent intent) { - Log.d(LOGTAG, "FMMediaButtonIntentReceiver.FM_MEDIA_BUTTON"); - Log.d(LOGTAG, "KeyEvent = " +intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT)); - String action = intent.getAction(); - if (action.equals(FMMediaButtonIntentReceiver.FM_MEDIA_BUTTON)) { - KeyEvent event = (KeyEvent) - intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); - int keycode = event.getKeyCode(); - switch (keycode) { - case KeyEvent.KEYCODE_HEADSETHOOK : - toggleFM(); - if (isOrderedBroadcast()) { - abortBroadcast(); - } - break; - case KeyEvent.KEYCODE_MEDIA_PAUSE : - if (isFmOn()){ - //FM should be off when Headset hook pressed. - fmOff(); - if (isOrderedBroadcast()) { - abortBroadcast(); - } - 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(); - } - } - break; - case KeyEvent.KEYCODE_MEDIA_PLAY: - if (isAntennaAvailable() && mServiceInUse) { - fmOn(); - if (isOrderedBroadcast()) { - abortBroadcast(); - } - try { - /* Notify the UI/Activity, only if the service is "bound" - by an activity and if Callbacks are registered - */ - if (mCallbacks != null ) { - mCallbacks.onEnabled(); - } - } catch (RemoteException e) { - e.printStackTrace(); - } - } - break; - } // end of switch - } // end of FMMediaButtonIntentReceiver.FM_MEDIA_BUTTON - } // end of onReceive - }; - IntentFilter iFilter = new IntentFilter(); - iFilter.addAction(FMMediaButtonIntentReceiver.FM_MEDIA_BUTTON); - registerReceiver(mFmMediaButtonListener, iFilter); - } - } - public void registerAudioBecomeNoisy() { if (mAudioBecomeNoisyListener == null) { mAudioBecomeNoisyListener = new BroadcastReceiver() { @@ -742,7 +678,7 @@ public class FMRadioService extends Service if (isFmOn()) { /* Disable FM and let the UI know */ - fmOff(); + fmOff(FM_OFF_FROM_ANTENNA); try { /* Notify the UI/Activity, only if the service is "bound" @@ -1092,12 +1028,14 @@ public class FMRadioService extends Service Log.d(LOGTAG, "audio focuss couldnot be granted"); return; } + mSession.setActive(true); Log.d(LOGTAG,"FM registering for registerMediaButtonEventReceiver"); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ComponentName fmRadio = new ComponentName(this.getPackageName(), FMMediaButtonIntentReceiver.class.getName()); mAudioManager.registerMediaButtonEventReceiver(fmRadio); + mStoppedOnFocusLoss = false; if (!mA2dpDeviceState.isDeviceAvailable()) { @@ -1670,6 +1608,7 @@ public class FMRadioService extends Service private void stop() { Log.d(LOGTAG,"in stop"); + if (!mServiceInUse) { Log.d(LOGTAG,"calling unregisterMediaButtonEventReceiver in stop"); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); @@ -1726,7 +1665,7 @@ public class FMRadioService extends Service public boolean fmOff() throws RemoteException { - return(mService.get().fmOff()); + return(mService.get().fmOff(FM_OFF_FROM_APPLICATION)); } public boolean fmRadioReset() throws RemoteException @@ -2030,6 +1969,7 @@ public class FMRadioService extends Service { return(mService.get().isA2DPConnected()); } + } private final IBinder mBinder = new ServiceStub(this); @@ -2243,6 +2183,19 @@ public class FMRadioService extends Service return(bStatus); } + + private boolean fmOff(int off_from) { + if (off_from == FM_OFF_FROM_APPLICATION || off_from == FM_OFF_FROM_ANTENNA) { + Log.d(LOGTAG, "FM application close button pressed or antenna removed"); + mSession.setActive(false); + AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + if (audioManager != null) + audioManager.abandonAudioFocus(mAudioFocusListener); + else + Log.d(LOGTAG, "Failed to get Audio Service"); + } + return fmOff(); + } /* * Turn OFF FM: Disable the FM Host when hardware resets asynchronously . * . -- GitLab