Separate mute and sending voice notifications

This commit is contained in:
Victor Shcherb 2015-09-28 00:40:35 +02:00
parent 6c37768663
commit 36e18d2651
5 changed files with 57 additions and 58 deletions

View file

@ -1,12 +1,13 @@
package net.osmand.plus;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.osmand.IProgress;
import net.osmand.IndexConstants;
@ -29,23 +30,23 @@ import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.views.corenative.NativeCoreContext;
import net.osmand.plus.voice.CommandPlayer;
import net.osmand.plus.voice.CommandPlayerException;
import net.osmand.plus.voice.CommandPlayerFactory;
import net.osmand.plus.voice.MediaCommandPlayerImpl;
import net.osmand.plus.voice.TTSCommandPlayerImpl;
import net.osmand.render.RenderingRulesStorage;
import net.osmand.router.RoutingConfiguration;
import net.osmand.util.Algorithms;
import org.xmlpull.v1.XmlPullParserException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import btools.routingapp.BRouterServiceConnection;
/**
@ -319,13 +320,32 @@ public class AppInitializer implements IProgress {
final ProgressDialog dlg = showDialog ? ProgressDialog.show(uiContext, app.getString(R.string.loading_data),
app.getString(R.string.voice_data_initializing)) : null;
new Thread(new Runnable() {
public CommandPlayer createCommandPlayer(String voiceProvider, OsmandApplication osmandApplication, Activity ctx)
throws CommandPlayerException {
if (voiceProvider != null) {
File parent = osmandApplication.getAppPath(IndexConstants.VOICE_INDEX_DIR);
File voiceDir = new File(parent, voiceProvider);
if (!voiceDir.exists()) {
throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable));
}
if (MediaCommandPlayerImpl.isMyData(voiceDir)) {
return new MediaCommandPlayerImpl(osmandApplication, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider);
} else if (TTSCommandPlayerImpl.isMyData(voiceDir)) {
return new TTSCommandPlayerImpl(ctx, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider);
}
throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported));
}
return null;
}
@Override
public void run() {
try {
if (app.player != null) {
app.player.clear();
}
app.player = CommandPlayerFactory.createCommandPlayer(voiceProvider, app, uiContext);
app.player = createCommandPlayer(voiceProvider, app, uiContext);
app.getRoutingHelper().getVoiceRouter().setPlayer(app.player);
if(dlg != null) {
dlg.dismiss();

View file

@ -110,7 +110,7 @@ public class VoiceRouter {
protected CommandBuilder getNewCommandPlayerToPlay(){
if(player == null || mute){
if(player == null){
return null;
}
lastAnnouncement = System.currentTimeMillis();

View file

@ -1,29 +0,0 @@
package net.osmand.plus.voice;
import java.io.File;
import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import android.app.Activity;
public class CommandPlayerFactory {
public static CommandPlayer createCommandPlayer(String voiceProvider, OsmandApplication osmandApplication, Activity ctx)
throws CommandPlayerException {
if (voiceProvider != null) {
File parent = osmandApplication.getAppPath(IndexConstants.VOICE_INDEX_DIR);
File voiceDir = new File(parent, voiceProvider);
if (!voiceDir.exists()) {
throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable));
}
if (MediaCommandPlayerImpl.isMyData(voiceDir)) {
return new MediaCommandPlayerImpl(osmandApplication, voiceProvider);
} else if (TTSCommandPlayerImpl.isMyData(voiceDir)) {
return new TTSCommandPlayerImpl(ctx, voiceProvider);
}
throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported));
}
return null;
}
}

View file

@ -8,6 +8,7 @@ import java.util.List;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.routing.VoiceRouter;
import org.apache.commons.logging.Log;
@ -30,12 +31,14 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen
private MediaPlayer mediaPlayer;
// indicates that player is ready to play first file
private List<String> filesToPlay = Collections.synchronizedList(new ArrayList<String>());
private VoiceRouter vrt;
public MediaCommandPlayerImpl(OsmandApplication ctx, String voiceProvider)
public MediaCommandPlayerImpl(OsmandApplication ctx, VoiceRouter vrt, String voiceProvider)
throws CommandPlayerException
{
super(ctx, voiceProvider, CONFIG_FILE, MEDIA_VOICE_VERSION);
this.vrt = vrt;
}
@Override
@ -57,6 +60,9 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen
// Called from the calculating route thread.
@Override
public synchronized void playCommands(CommandBuilder builder) {
if(vrt.isMute()) {
return;
}
filesToPlay.addAll(builder.execute());
// If we have not already started to play audio, start.

View file

@ -14,6 +14,7 @@ import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsActivity;
import net.osmand.plus.routing.VoiceRouter;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -69,10 +70,12 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer {
private TextToSpeech mTts;
private Context mTtsContext;
private HashMap<String, String> params = new HashMap<String, String>();
private VoiceRouter vrt;
protected TTSCommandPlayerImpl(Activity ctx, String voiceProvider)
public TTSCommandPlayerImpl(Activity ctx, VoiceRouter vrt, String voiceProvider)
throws CommandPlayerException {
super((OsmandApplication) ctx.getApplicationContext(), voiceProvider, CONFIG_FILE, TTS_VOICE_VERSION);
this.vrt = vrt;
if (Algorithms.isEmpty(language)) {
throw new CommandPlayerException(
ctx.getString(R.string.voice_data_corrupted));
@ -100,17 +103,16 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer {
// Called from the calculating route thread.
@Override
public synchronized void playCommands(CommandBuilder builder) {
if (mTts != null) {
final List<String> execute = builder.execute(); //list of strings, the speech text, play it
StringBuilder bld = new StringBuilder();
for (String s : execute) {
bld.append(s).append(' ');
}
final List<String> execute = builder.execute(); //list of strings, the speech text, play it
StringBuilder bld = new StringBuilder();
for (String s : execute) {
bld.append(s).append(' ');
}
sendAlertToPebble(bld.toString());
if (mTts != null && !vrt.isMute()) {
if (ttsRequests++ == 0)
requestAudioFocus();
log.debug("ttsRequests="+ttsRequests);
sendAlertToPebble(bld.toString());
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.