From 21d433bf757225eea74d66ebe2895644e8814d35 Mon Sep 17 00:00:00 2001 From: sonora Date: Thu, 30 Jun 2016 14:17:22 +0200 Subject: [PATCH] Basic fix for 2590 --- OsmAnd/AndroidManifest.xml | 1 + OsmAnd/res/values/strings.xml | 2 +- .../voice/AbstractPrologCommandPlayer.java | 49 ++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/OsmAnd/AndroidManifest.xml b/OsmAnd/AndroidManifest.xml index ed269d5df8..b752900ce0 100644 --- a/OsmAnd/AndroidManifest.xml +++ b/OsmAnd/AndroidManifest.xml @@ -23,6 +23,7 @@ + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 3483352eea..19331ffff3 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -1224,7 +1224,7 @@ Auto zoom map Snap position to roads during navigation Snap to road - Interrupt music when making announcement + Interrupt music when making announcement (also as simulated phone calls to car BT stereos) Interrupt music OsmAnd Maps & Navigation Global Mobile Map Viewing & Navigation for Offline and Online OSM Maps diff --git a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java index 1e8612be2b..008d6cdd85 100644 --- a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java +++ b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java @@ -4,6 +4,8 @@ import android.content.Context; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; +import android.media.AudioManager; + import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.StateChangedListener; @@ -279,6 +281,13 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat if (mAudioFocusHelper != null) { mAudioFocusHelper.requestFocus(ctx, streamType); } + if (ctx.getSettings().INTERRUPT_MUSIC.get()) { + // TODO: Delay the prompts a bit until connection is established + startBtSco(ctx); + //if (btScoStatus == false) { + // Toast.makeText(this, R.string."BT connection error: ", Toast.LENGTH_SHORT).show(); + //} + } } private AudioFocusHelper getAudioFocus() { @@ -292,10 +301,48 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat protected void abandonAudioFocus() { log.debug("abandonAudioFocus"); + if (ctx.getSettings().INTERRUPT_MUSIC.get()) { + stopBtSco(ctx); + } if (mAudioFocusHelper != null) { mAudioFocusHelper.abandonFocus(ctx, streamType); mAudioFocusHelper = null; } } - + + private boolean btScoStatus = false; + + private boolean startBtSco(Context context) { + // Establish a low quality Synchronous Connection-Oriented link to BT to e.g. interrupt a car stereo + // http://stackoverflow.com/questions/2144694/routing-audio-to-bluetooth-headset-non-a2dp-on-android + try { + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + if (mAudioManager == null) { + return false; + } + mAudioManager.setMode(0); + mAudioManager.setBluetoothScoOn(true); + mAudioManager.startBluetoothSco(); + mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); + btScoStatus = true; + } catch (Exception e) { + System.out.println("Exception starting BT SCO " + e.getMessage() ); + btScoStatus = false; + return false; + } + return btScoStatus; + } + + private boolean stopBtSco(Context context) { + btScoStatus = false; + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + if (mAudioManager == null) { + return false; + } + mAudioManager.setBluetoothScoOn(false); + mAudioManager.stopBluetoothSco(); + mAudioManager.setMode(AudioManager.MODE_NORMAL); + return true; + } + }