From 349e03a43f64ce145f5e16956d5c6de8fdd27bc9 Mon Sep 17 00:00:00 2001 From: sonora Date: Sun, 3 Jul 2016 16:23:33 +0200 Subject: [PATCH] introduce delay and trigger shift capability --- .../src/net/osmand/plus/routing/VoiceRouter.java | 16 ++++++++++++---- .../plus/voice/AbstractPrologCommandPlayer.java | 12 ++++++------ .../osmand/plus/voice/TTSCommandPlayerImpl.java | 10 ++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java index 41e7232085..704c20951f 100644 --- a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java +++ b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java @@ -170,7 +170,9 @@ public class VoiceRouter { TURN_DISTANCE = (int) (DEFAULT_SPEED * 7); } } - + + private double btScoDelayDistance = 0; + public boolean isDistanceLess(float currentSpeed, double dist, double etalon, float defSpeed){ if(defSpeed <= 0) { defSpeed = DEFAULT_SPEED; @@ -178,7 +180,13 @@ public class VoiceRouter { if(currentSpeed <= 0) { currentSpeed = DEFAULT_SPEED; } - if(dist < etalon || ((dist / currentSpeed) < (etalon / defSpeed))){ + + // Trigger close prompts earlier to allow for BT SCO connection delay + if (settings.AUDIO_STREAM_GUIDANCE.get() == 0) { + btScoDelayDistance = currentSpeed * (double) AbstractPrologCommandPlayer.BT_SCO_DELAY / 1000; + } + + if((dist < etalon + btScoDelayDistance) || ((dist / currentSpeed) < ((etalon + btScoDelayDistance) / defSpeed))){ return true; } return false; @@ -480,9 +488,9 @@ public class VoiceRouter { if (repeat || dist >= TURN_IN_DISTANCE_END) { if ((isDistanceLess(speed, nextNextInfo.distanceTo, TURN_DISTANCE, 0f) || nextNextInfo.distanceTo < TURN_IN_DISTANCE_END) && nextNextInfo != null) { - playMakeTurnIn(currentSegment, next, dist, nextNextInfo.directionInfo); + playMakeTurnIn(currentSegment, next, dist - (int) btScoDelayDistance, nextNextInfo.directionInfo); } else { - playMakeTurnIn(currentSegment, next, dist, null); + playMakeTurnIn(currentSegment, next, dist - (int) btScoDelayDistance, null); } playAndArriveAtDestination(repeat, nextInfo, currentSegment); } diff --git a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java index c46a9b4457..16c3358f24 100644 --- a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java +++ b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java @@ -283,7 +283,7 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat } // If AudioManager.STREAM_VOICE_CALL try using BT SCO: if (ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) { - startBtSco(); + startBtSco(ctx); } } @@ -299,7 +299,7 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat protected void abandonAudioFocus() { log.debug("abandonAudioFocus"); if ((ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) || (btScoStatus == true)) { - stopBtSco(); + stopBtSco(ctx); } if (mAudioFocusHelper != null) { mAudioFocusHelper.abandonFocus(ctx, streamType); @@ -311,11 +311,11 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat public static final int BT_SCO_DELAY = 1500; - private boolean startBtSco() { + 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) ctx.getSystemService(Context.AUDIO_SERVICE); + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //if (mAudioManager == null || !mAudioManager.isBluetoothScoAvailableOffCall()) { if (mAudioManager == null) { return false; @@ -333,9 +333,9 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat return btScoStatus; } - private boolean stopBtSco() { + private boolean stopBtSco(Context context) { btScoStatus = false; - AudioManager mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE); + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); if (mAudioManager == null) { return false; } diff --git a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java index 615ab88cdd..78b912dc86 100644 --- a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java @@ -108,6 +108,16 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { if (ttsRequests++ == 0) requestAudioFocus(); log.debug("ttsRequests="+ttsRequests); + // Delay prompts to allow BT SCO connection being established + if (ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) { + ttsRequests++; + if (android.os.Build.VERSION.SDK_INT <= 21) { + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis()); + mTts.playSilence(BT_SCO_DELAY, TextToSpeech.QUEUE_ADD, params); + } else { + mTts.playSilentUtterance(BT_SCO_DELAY, TextToSpeech.QUEUE_ADD, ""+System.currentTimeMillis()); + } + } params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis()); mTts.speak(bld.toString(), TextToSpeech.QUEUE_ADD, params); // Audio focus will be released when onUtteranceCompleted() completed is called by the TTS engine.