Replace LF and CRLF. Add warning about internet connection when online rouing is using

This commit is contained in:
Victor Shcherb 2011-06-22 00:30:50 +02:00
parent 1dc78bb32a
commit afd3fde12e
9 changed files with 622 additions and 614 deletions

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources> <resources>
<string name="internet_connection_required_for_online_route">You are about to use online routing but you do not have internet connection. You can try experimental offline routing (switch in settings).</string>
<string name="tts_language_not_supported_title">Language unsupported</string> <string name="tts_language_not_supported_title">Language unsupported</string>
<string name="tts_language_not_supported">The selected language is not supported by the installed TTS engine. Do you want to go to market and search for other TTS engine? Else preset TTS language will be used.</string> <string name="tts_language_not_supported">The selected language is not supported by the installed TTS engine. Do you want to go to market and search for other TTS engine? Else preset TTS language will be used.</string>
<string name="tts_missing_language_data_title">Missing data</string> <string name="tts_missing_language_data_title">Missing data</string>

View file

@ -411,8 +411,9 @@ public class OsmandSettings {
// this value string is synchronized with settings_pref.xml preference name // this value string is synchronized with settings_pref.xml preference name
// make cloudmade by default why osmand is not stable enough
public final OsmandPreference<RouteService> ROUTER_SERVICE = public final OsmandPreference<RouteService> ROUTER_SERVICE =
new EnumIntPreference<RouteService>("router_service", RouteService.OSMAND, false, RouteService.values()); new EnumIntPreference<RouteService>("router_service", RouteService.CLOUDMADE, false, RouteService.values());
// this value string is synchronized with settings_pref.xml preference name // this value string is synchronized with settings_pref.xml preference name

View file

@ -446,6 +446,9 @@ public class RoutingHelper {
currentRunningJob = new Thread(new Runnable() { currentRunningJob = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
if(service != RouteService.OSMAND && !settings.isInternetConnectionAvailable()){
showMessage(context.getString(R.string.internet_connection_required_for_online_route), Toast.LENGTH_LONG);
}
RouteCalculationResult res = provider.calculateRouteImpl(start, end, mode, service, context, currentGPXRoute, fastRouteMode); RouteCalculationResult res = provider.calculateRouteImpl(start, end, mode, service, context, currentGPXRoute, fastRouteMode);
synchronized (RoutingHelper.this) { synchronized (RoutingHelper.this) {
if (res.isCalculated()) { if (res.isCalculated()) {
@ -492,18 +495,21 @@ public class RoutingHelper {
return currentRunningJob != null; return currentRunningJob != null;
} }
private void showMessage(final String msg){ private void showMessage(final String msg, final int length){
if (uiActivity != null) { if (uiActivity != null) {
uiActivity.runOnUiThread(new Runnable() { uiActivity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
if(uiActivity != null){ if(uiActivity != null){
Toast.makeText(uiActivity, msg, Toast.LENGTH_SHORT).show(); Toast.makeText(uiActivity, msg, length).show();
} }
} }
}); });
} }
} }
private void showMessage(final String msg){
showMessage(msg, Toast.LENGTH_SHORT);
}
public boolean hasPointsToShow(){ public boolean hasPointsToShow(){
return finalLocation != null && !routeNodes.isEmpty(); return finalLocation != null && !routeNodes.isEmpty();

View file

@ -1,179 +1,179 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.ResourceManager; import net.osmand.plus.ResourceManager;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import alice.tuprolog.InvalidLibraryException; import alice.tuprolog.InvalidLibraryException;
import alice.tuprolog.InvalidTheoryException; import alice.tuprolog.InvalidTheoryException;
import alice.tuprolog.NoSolutionException; import alice.tuprolog.NoSolutionException;
import alice.tuprolog.Number; import alice.tuprolog.Number;
import alice.tuprolog.Prolog; import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo; import alice.tuprolog.SolveInfo;
import alice.tuprolog.Struct; import alice.tuprolog.Struct;
import alice.tuprolog.Term; import alice.tuprolog.Term;
import alice.tuprolog.Theory; import alice.tuprolog.Theory;
import alice.tuprolog.Var; import alice.tuprolog.Var;
import android.content.Context; import android.content.Context;
public abstract class AbstractPrologCommandPlayer implements CommandPlayer { public abstract class AbstractPrologCommandPlayer implements CommandPlayer {
private static final Log log = LogUtil private static final Log log = LogUtil
.getLog(AbstractPrologCommandPlayer.class); .getLog(AbstractPrologCommandPlayer.class);
protected Context ctx; protected Context ctx;
protected File voiceDir; protected File voiceDir;
protected Prolog prologSystem; protected Prolog prologSystem;
protected static final String P_VERSION = "version"; protected static final String P_VERSION = "version";
protected static final String P_RESOLVE = "resolve"; protected static final String P_RESOLVE = "resolve";
public static final String A_LEFT = "left"; public static final String A_LEFT = "left";
public static final String A_LEFT_SH = "left_sh"; public static final String A_LEFT_SH = "left_sh";
public static final String A_LEFT_SL = "left_sl"; public static final String A_LEFT_SL = "left_sl";
public static final String A_RIGHT = "right"; public static final String A_RIGHT = "right";
public static final String A_RIGHT_SH = "right_sh"; public static final String A_RIGHT_SH = "right_sh";
public static final String A_RIGHT_SL = "right_sl"; public static final String A_RIGHT_SL = "right_sl";
protected static final String DELAY_CONST = "delay_"; protected static final String DELAY_CONST = "delay_";
private final int voiceVersion; private final int voiceVersion;
protected AbstractPrologCommandPlayer(Context ctx, String voiceProvider, String configFile, int voiceVersion) protected AbstractPrologCommandPlayer(Context ctx, String voiceProvider, String configFile, int voiceVersion)
throws CommandPlayerException throws CommandPlayerException
{ {
this.voiceVersion = voiceVersion; this.voiceVersion = voiceVersion;
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
try { try {
this.ctx = ctx; this.ctx = ctx;
prologSystem = new Prolog( prologSystem = new Prolog(
new String[] { "alice.tuprolog.lib.BasicLibrary" }); //$NON-NLS-1$ new String[] { "alice.tuprolog.lib.BasicLibrary" }); //$NON-NLS-1$
} catch (InvalidLibraryException e) { } catch (InvalidLibraryException e) {
log.error("Initializing error", e); //$NON-NLS-1$ log.error("Initializing error", e); //$NON-NLS-1$
throw new RuntimeException(e); throw new RuntimeException(e);
} }
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Initializing prolog system : " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ log.info("Initializing prolog system : " + (System.currentTimeMillis() - time)); //$NON-NLS-1$
} }
init(voiceProvider, configFile); init(voiceProvider, configFile);
} }
private void init(String voiceProvider, String configFile) throws CommandPlayerException { private void init(String voiceProvider, String configFile) throws CommandPlayerException {
prologSystem.clearTheory(); prologSystem.clearTheory();
voiceDir = null; voiceDir = null;
if (voiceProvider != null) { if (voiceProvider != null) {
File parent = OsmandSettings.getOsmandSettings(ctx).extendOsmandPath(ResourceManager.VOICE_PATH); File parent = OsmandSettings.getOsmandSettings(ctx).extendOsmandPath(ResourceManager.VOICE_PATH);
voiceDir = new File(parent, voiceProvider); voiceDir = new File(parent, voiceProvider);
if (!voiceDir.exists()) { if (!voiceDir.exists()) {
voiceDir = null; voiceDir = null;
throw new CommandPlayerException( throw new CommandPlayerException(
ctx.getString(R.string.voice_data_unavailable)); ctx.getString(R.string.voice_data_unavailable));
} }
} }
// see comments below why it is impossible to read from zip (don't know // see comments below why it is impossible to read from zip (don't know
// how to play file from zip) // how to play file from zip)
// voiceZipFile = null; // voiceZipFile = null;
if (voiceDir != null) { if (voiceDir != null) {
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
boolean wrong = false; boolean wrong = false;
try { try {
InputStream config; InputStream config;
// if (voiceDir.getName().endsWith(".zip")) { //$NON-NLS-1$ // if (voiceDir.getName().endsWith(".zip")) { //$NON-NLS-1$
// voiceZipFile = new ZipFile(voiceDir); // voiceZipFile = new ZipFile(voiceDir);
// config = voiceZipFile.getInputStream(voiceZipFile.getEntry("_config.p")); //$NON-NLS-1$ // config = voiceZipFile.getInputStream(voiceZipFile.getEntry("_config.p")); //$NON-NLS-1$
// } else { // } else {
config = new FileInputStream(new File(voiceDir, configFile)); //$NON-NLS-1$ config = new FileInputStream(new File(voiceDir, configFile)); //$NON-NLS-1$
// } // }
if (!wrong) { if (!wrong) {
prologSystem.setTheory(new Theory(config)); prologSystem.setTheory(new Theory(config));
} }
} catch (InvalidTheoryException e) { } catch (InvalidTheoryException e) {
log.error("Loading voice config exception " + voiceProvider, e); //$NON-NLS-1$ log.error("Loading voice config exception " + voiceProvider, e); //$NON-NLS-1$
wrong = true; wrong = true;
} catch (IOException e) { } catch (IOException e) {
log.error("Loading voice config exception " + voiceProvider, e); //$NON-NLS-1$ log.error("Loading voice config exception " + voiceProvider, e); //$NON-NLS-1$
wrong = true; wrong = true;
} }
if (wrong) { if (wrong) {
throw new CommandPlayerException(ctx.getString(R.string.voice_data_corrupted)); throw new CommandPlayerException(ctx.getString(R.string.voice_data_corrupted));
} else { } else {
Term val = solveSimplePredicate(P_VERSION); Term val = solveSimplePredicate(P_VERSION);
if (!(val instanceof Number) || ((Number)val).intValue() != voiceVersion) { if (!(val instanceof Number) || ((Number)val).intValue() != voiceVersion) {
throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported)); throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported));
} }
} }
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("Initializing voice subsystem " + voiceProvider + " : " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$ log.info("Initializing voice subsystem " + voiceProvider + " : " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$
} }
} }
} }
protected Term solveSimplePredicate(String predicate) { protected Term solveSimplePredicate(String predicate) {
Term val = null; Term val = null;
Var v = new Var("MyVariable"); //$NON-NLS-1$ Var v = new Var("MyVariable"); //$NON-NLS-1$
SolveInfo s = prologSystem.solve(new Struct(predicate, v)); SolveInfo s = prologSystem.solve(new Struct(predicate, v));
if (s.isSuccess()) { if (s.isSuccess()) {
prologSystem.solveEnd(); prologSystem.solveEnd();
try { try {
val = s.getVarValue(v.getName()); val = s.getVarValue(v.getName());
} catch (NoSolutionException e) { } catch (NoSolutionException e) {
} }
} }
return val; return val;
} }
@Override @Override
public List<String> execute(List<Struct> listCmd){ public List<String> execute(List<Struct> listCmd){
Struct list = new Struct(listCmd.toArray(new Term[listCmd.size()])); Struct list = new Struct(listCmd.toArray(new Term[listCmd.size()]));
Var result = new Var("RESULT"); //$NON-NLS-1$ Var result = new Var("RESULT"); //$NON-NLS-1$
List<String> files = new ArrayList<String>(); List<String> files = new ArrayList<String>();
SolveInfo res = prologSystem.solve(new Struct(P_RESOLVE, list, result)); SolveInfo res = prologSystem.solve(new Struct(P_RESOLVE, list, result));
if (res.isSuccess()) { if (res.isSuccess()) {
try { try {
prologSystem.solveEnd(); prologSystem.solveEnd();
Term solution = res.getVarValue(result.getName()); Term solution = res.getVarValue(result.getName());
Iterator<?> listIterator = ((Struct) solution).listIterator(); Iterator<?> listIterator = ((Struct) solution).listIterator();
while(listIterator.hasNext()){ while(listIterator.hasNext()){
Object term = listIterator.next(); Object term = listIterator.next();
if(term instanceof Struct){ if(term instanceof Struct){
files.add(((Struct) term).getName()); files.add(((Struct) term).getName());
} }
} }
} catch (NoSolutionException e) { } catch (NoSolutionException e) {
} }
} }
return files; return files;
} }
@Override @Override
public String getCurrentVoice() { public String getCurrentVoice() {
if (voiceDir == null) { if (voiceDir == null) {
return null; return null;
} }
return voiceDir.getName(); return voiceDir.getName();
} }
@Override @Override
public CommandBuilder newCommandBuilder() { public CommandBuilder newCommandBuilder() {
return new CommandBuilder(this); return new CommandBuilder(this);
} }
@Override @Override
public void clear() { public void clear() {
ctx = null; ctx = null;
prologSystem = null; prologSystem = null;
} }
} }

View file

@ -1,179 +1,179 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import net.osmand.LogUtil; import net.osmand.LogUtil;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import alice.tuprolog.Struct; import alice.tuprolog.Struct;
import alice.tuprolog.Term; import alice.tuprolog.Term;
public class CommandBuilder { public class CommandBuilder {
private static final Log log = LogUtil.getLog(CommandBuilder.class); private static final Log log = LogUtil.getLog(CommandBuilder.class);
protected static final String C_PREPARE_TURN = "prepare_turn"; //$NON-NLS-1$ protected static final String C_PREPARE_TURN = "prepare_turn"; //$NON-NLS-1$
protected static final String C_PREPARE_ROUNDABOUT = "prepare_roundabout"; //$NON-NLS-1$ protected static final String C_PREPARE_ROUNDABOUT = "prepare_roundabout"; //$NON-NLS-1$
protected static final String C_PREPARE_MAKE_UT = "prepare_make_ut"; //$NON-NLS-1$ protected static final String C_PREPARE_MAKE_UT = "prepare_make_ut"; //$NON-NLS-1$
protected static final String C_ROUNDABOUT = "roundabout"; //$NON-NLS-1$ protected static final String C_ROUNDABOUT = "roundabout"; //$NON-NLS-1$
protected static final String C_GO_AHEAD = "go_ahead"; //$NON-NLS-1$ protected static final String C_GO_AHEAD = "go_ahead"; //$NON-NLS-1$
protected static final String C_TURN = "turn"; //$NON-NLS-1$ protected static final String C_TURN = "turn"; //$NON-NLS-1$
protected static final String C_MAKE_UT = "make_ut"; //$NON-NLS-1$ protected static final String C_MAKE_UT = "make_ut"; //$NON-NLS-1$
protected static final String C_PREAMBLE = "preamble"; //$NON-NLS-1$ protected static final String C_PREAMBLE = "preamble"; //$NON-NLS-1$
protected static final String C_AND_ARRIVE_DESTINATION = "and_arrive_destination"; //$NON-NLS-1$ protected static final String C_AND_ARRIVE_DESTINATION = "and_arrive_destination"; //$NON-NLS-1$
protected static final String C_THEN = "then"; //$NON-NLS-1$ protected static final String C_THEN = "then"; //$NON-NLS-1$
protected static final String C_REACHED_DESTINATION = "reached_destination"; //$NON-NLS-1$ protected static final String C_REACHED_DESTINATION = "reached_destination"; //$NON-NLS-1$
protected static final String C_BEAR_LEFT = "bear_left"; //$NON-NLS-1$ protected static final String C_BEAR_LEFT = "bear_left"; //$NON-NLS-1$
protected static final String C_BEAR_RIGHT = "bear_right"; //$NON-NLS-1$ protected static final String C_BEAR_RIGHT = "bear_right"; //$NON-NLS-1$
protected static final String C_ROUTE_RECALC = "route_recalc"; //$NON-NLS-1$ protected static final String C_ROUTE_RECALC = "route_recalc"; //$NON-NLS-1$
protected static final String C_ROUTE_NEW_CALC = "route_new_calc"; //$NON-NLS-1$ protected static final String C_ROUTE_NEW_CALC = "route_new_calc"; //$NON-NLS-1$
/** /**
* *
*/ */
private final CommandPlayer commandPlayer; private final CommandPlayer commandPlayer;
private boolean alreadyExecuted = false; private boolean alreadyExecuted = false;
private List<Struct> listStruct = new ArrayList<Struct>(); private List<Struct> listStruct = new ArrayList<Struct>();
public CommandBuilder(CommandPlayer commandPlayer){ public CommandBuilder(CommandPlayer commandPlayer){
this(commandPlayer, true); this(commandPlayer, true);
} }
public CommandBuilder(CommandPlayer commandPlayer, boolean preamble) { public CommandBuilder(CommandPlayer commandPlayer, boolean preamble) {
this.commandPlayer = commandPlayer; this.commandPlayer = commandPlayer;
if (preamble) { if (preamble) {
addCommand(C_PREAMBLE); addCommand(C_PREAMBLE);
} }
} }
private void checkState(){ private void checkState(){
if(alreadyExecuted){ if(alreadyExecuted){
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
private CommandBuilder addCommand(String name, Object... args){ private CommandBuilder addCommand(String name, Object... args){
checkState(); checkState();
Term[] list = new Term[args.length]; Term[] list = new Term[args.length];
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
Object o = args[i]; Object o = args[i];
if(o instanceof java.lang.Number){ if(o instanceof java.lang.Number){
if(o instanceof java.lang.Double){ if(o instanceof java.lang.Double){
list[i] = new alice.tuprolog.Double((Double) o); list[i] = new alice.tuprolog.Double((Double) o);
} else if(o instanceof java.lang.Float){ } else if(o instanceof java.lang.Float){
list[i] = new alice.tuprolog.Float((Float) o); list[i] = new alice.tuprolog.Float((Float) o);
} else if(o instanceof java.lang.Long){ } else if(o instanceof java.lang.Long){
list[i] = new alice.tuprolog.Long((Long) o); list[i] = new alice.tuprolog.Long((Long) o);
} else { } else {
list[i] = new alice.tuprolog.Int(((java.lang.Number)o).intValue()); list[i] = new alice.tuprolog.Int(((java.lang.Number)o).intValue());
} }
} else if(o instanceof String){ } else if(o instanceof String){
list[i] = new Struct((String) o); list[i] = new Struct((String) o);
} }
if(list[i]== null){ if(list[i]== null){
throw new NullPointerException(name +" " + o); //$NON-NLS-1$ throw new NullPointerException(name +" " + o); //$NON-NLS-1$
} }
} }
Struct struct = new Struct(name, list); Struct struct = new Struct(name, list);
if(log.isDebugEnabled()){ if(log.isDebugEnabled()){
log.debug("Adding command : " + name + " " + Arrays.toString(args)); //$NON-NLS-1$ //$NON-NLS-2$ log.debug("Adding command : " + name + " " + Arrays.toString(args)); //$NON-NLS-1$ //$NON-NLS-2$
} }
listStruct.add(struct); listStruct.add(struct);
return this; return this;
} }
public CommandBuilder goAhead(){ public CommandBuilder goAhead(){
return addCommand(C_GO_AHEAD); return addCommand(C_GO_AHEAD);
} }
public CommandBuilder goAhead(double dist){ public CommandBuilder goAhead(double dist){
return addCommand(C_GO_AHEAD, dist); return addCommand(C_GO_AHEAD, dist);
} }
public CommandBuilder makeUT(){ public CommandBuilder makeUT(){
return addCommand(C_MAKE_UT); return addCommand(C_MAKE_UT);
} }
public CommandBuilder makeUT(double dist){ public CommandBuilder makeUT(double dist){
return addCommand(C_MAKE_UT, dist); return addCommand(C_MAKE_UT, dist);
} }
public CommandBuilder prepareMakeUT(double dist){ public CommandBuilder prepareMakeUT(double dist){
return addCommand(C_PREPARE_MAKE_UT, dist); return addCommand(C_PREPARE_MAKE_UT, dist);
} }
public CommandBuilder turn(String param){ public CommandBuilder turn(String param){
return addCommand(C_TURN, param); return addCommand(C_TURN, param);
} }
public CommandBuilder turn(String param, double dist){ public CommandBuilder turn(String param, double dist){
return addCommand(C_TURN, param, dist); return addCommand(C_TURN, param, dist);
} }
/** /**
* *
* @param param A_LEFT, A_RIGHT, ... * @param param A_LEFT, A_RIGHT, ...
* @param dist * @param dist
* @return * @return
*/ */
public CommandBuilder prepareTurn(String param, double dist){ public CommandBuilder prepareTurn(String param, double dist){
return addCommand(C_PREPARE_TURN, param, dist); return addCommand(C_PREPARE_TURN, param, dist);
} }
public CommandBuilder prepareRoundAbout(double dist){ public CommandBuilder prepareRoundAbout(double dist){
return addCommand(C_PREPARE_ROUNDABOUT, dist); return addCommand(C_PREPARE_ROUNDABOUT, dist);
} }
public CommandBuilder roundAbout(double dist, double angle, int exit){ public CommandBuilder roundAbout(double dist, double angle, int exit){
return addCommand(C_ROUNDABOUT, dist, angle, exit); return addCommand(C_ROUNDABOUT, dist, angle, exit);
} }
public CommandBuilder roundAbout(double angle, int exit){ public CommandBuilder roundAbout(double angle, int exit){
return addCommand(C_ROUNDABOUT, angle, exit); return addCommand(C_ROUNDABOUT, angle, exit);
} }
public CommandBuilder andArriveAtDestination(){ public CommandBuilder andArriveAtDestination(){
return addCommand(C_AND_ARRIVE_DESTINATION); return addCommand(C_AND_ARRIVE_DESTINATION);
} }
public CommandBuilder arrivedAtDestination(){ public CommandBuilder arrivedAtDestination(){
return addCommand(C_REACHED_DESTINATION); return addCommand(C_REACHED_DESTINATION);
} }
public CommandBuilder bearLeft(){ public CommandBuilder bearLeft(){
return addCommand(C_BEAR_LEFT); return addCommand(C_BEAR_LEFT);
} }
public CommandBuilder bearRight(){ public CommandBuilder bearRight(){
return addCommand(C_BEAR_RIGHT); return addCommand(C_BEAR_RIGHT);
} }
public CommandBuilder then(){ public CommandBuilder then(){
return addCommand(C_THEN); return addCommand(C_THEN);
} }
public CommandBuilder newRouteCalculated(double dist){ public CommandBuilder newRouteCalculated(double dist){
return addCommand(C_ROUTE_NEW_CALC, dist); return addCommand(C_ROUTE_NEW_CALC, dist);
} }
public CommandBuilder routeRecalculated(double dist){ public CommandBuilder routeRecalculated(double dist){
return addCommand(C_ROUTE_RECALC, dist); return addCommand(C_ROUTE_RECALC, dist);
} }
public void play(){ public void play(){
this.commandPlayer.playCommands(this); this.commandPlayer.playCommands(this);
} }
protected List<String> execute(){ protected List<String> execute(){
alreadyExecuted = true; alreadyExecuted = true;
return this.commandPlayer.execute(listStruct); return this.commandPlayer.execute(listStruct);
} }
} }

View file

@ -1,24 +1,24 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
import java.util.List; import java.util.List;
import alice.tuprolog.Struct; import alice.tuprolog.Struct;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
public interface CommandPlayer { public interface CommandPlayer {
public abstract String getCurrentVoice(); public abstract String getCurrentVoice();
public abstract CommandBuilder newCommandBuilder(); public abstract CommandBuilder newCommandBuilder();
public abstract void playCommands(CommandBuilder builder); public abstract void playCommands(CommandBuilder builder);
public abstract void clear(); public abstract void clear();
public abstract List<String> execute(List<Struct> listStruct); public abstract List<String> execute(List<Struct> listStruct);
public void onActivityInit(Activity ctx); public void onActivityInit(Activity ctx);
public void onActvitiyStop(Context ctx); public void onActvitiyStop(Context ctx);
} }

View file

@ -1,22 +1,22 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
/** /**
* Exception on CommandPlayer * Exception on CommandPlayer
* *
* @author Pavol Zibrita <pavol.zibrita@gmail.com> * @author Pavol Zibrita <pavol.zibrita@gmail.com>
*/ */
public class CommandPlayerException extends Exception { public class CommandPlayerException extends Exception {
private static final long serialVersionUID = 8413902962574061832L; private static final long serialVersionUID = 8413902962574061832L;
private final String error; private final String error;
public CommandPlayerException(String error) { public CommandPlayerException(String error) {
this.error = error; this.error = error;
} }
public String getError() { public String getError() {
return error; return error;
} }
} }

View file

@ -1,34 +1,34 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
import java.io.File; import java.io.File;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.ResourceManager; import net.osmand.plus.ResourceManager;
import net.osmand.plus.activities.OsmandApplication; import net.osmand.plus.activities.OsmandApplication;
import android.app.Activity; import android.app.Activity;
import android.os.Build; import android.os.Build;
public class CommandPlayerFactory public class CommandPlayerFactory
{ {
public static CommandPlayer createCommandPlayer(String voiceProvider, OsmandApplication osmandApplication, Activity ctx) public static CommandPlayer createCommandPlayer(String voiceProvider, OsmandApplication osmandApplication, Activity ctx)
throws CommandPlayerException throws CommandPlayerException
{ {
if (voiceProvider != null){ if (voiceProvider != null){
File parent = OsmandSettings.getOsmandSettings(ctx).extendOsmandPath(ResourceManager.VOICE_PATH); File parent = OsmandSettings.getOsmandSettings(ctx).extendOsmandPath(ResourceManager.VOICE_PATH);
File voiceDir = new File(parent, voiceProvider); File voiceDir = new File(parent, voiceProvider);
if(!voiceDir.exists()){ if(!voiceDir.exists()){
throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable)); throw new CommandPlayerException(ctx.getString(R.string.voice_data_unavailable));
} }
if (MediaCommandPlayerImpl.isMyData(voiceDir)) { if (MediaCommandPlayerImpl.isMyData(voiceDir)) {
return new MediaCommandPlayerImpl(osmandApplication, voiceProvider); return new MediaCommandPlayerImpl(osmandApplication, voiceProvider);
} else if (Integer.parseInt(Build.VERSION.SDK) >= 4) { } else if (Integer.parseInt(Build.VERSION.SDK) >= 4) {
if (TTSCommandPlayerImpl.isMyData(voiceDir)) { if (TTSCommandPlayerImpl.isMyData(voiceDir)) {
return new TTSCommandPlayerImpl(ctx, voiceProvider); return new TTSCommandPlayerImpl(ctx, voiceProvider);
} }
} }
throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported)); throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported));
} }
return null; return null;
} }
} }

View file

@ -1,175 +1,175 @@
package net.osmand.plus.voice; package net.osmand.plus.voice;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import net.osmand.Algoritms; import net.osmand.Algoritms;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.SettingsActivity; import net.osmand.plus.activities.SettingsActivity;
import alice.tuprolog.Struct; import alice.tuprolog.Struct;
import alice.tuprolog.Term; import alice.tuprolog.Term;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener; import android.speech.tts.TextToSpeech.OnInitListener;
public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer {
private final class IntentStarter implements private final class IntentStarter implements
DialogInterface.OnClickListener { DialogInterface.OnClickListener {
private final Activity ctx; private final Activity ctx;
private final String intentAction; private final String intentAction;
private final Uri intentData; private final Uri intentData;
private IntentStarter(Activity ctx, String intentAction) { private IntentStarter(Activity ctx, String intentAction) {
this(ctx,intentAction, null); this(ctx,intentAction, null);
} }
private IntentStarter(Activity ctx, String intentAction, Uri intentData) { private IntentStarter(Activity ctx, String intentAction, Uri intentData) {
this.ctx = ctx; this.ctx = ctx;
this.intentAction = intentAction; this.intentAction = intentAction;
this.intentData = intentData; this.intentData = intentData;
} }
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
Intent installIntent = new Intent(); Intent installIntent = new Intent();
installIntent.setAction(intentAction); installIntent.setAction(intentAction);
if (intentData != null) { if (intentData != null) {
installIntent.setData(intentData); installIntent.setData(intentData);
} }
ctx.startActivity(installIntent); ctx.startActivity(installIntent);
} }
} }
private static final String CONFIG_FILE = "_ttsconfig.p"; private static final String CONFIG_FILE = "_ttsconfig.p";
private static final int TTS_VOICE_VERSION = 100; private static final int TTS_VOICE_VERSION = 100;
private TextToSpeech mTts; private TextToSpeech mTts;
private Context mTtsContext; private Context mTtsContext;
private String language; private String language;
protected TTSCommandPlayerImpl(Activity ctx, String voiceProvider) protected TTSCommandPlayerImpl(Activity ctx, String voiceProvider)
throws CommandPlayerException { throws CommandPlayerException {
super(ctx, voiceProvider, CONFIG_FILE, TTS_VOICE_VERSION); super(ctx, voiceProvider, CONFIG_FILE, TTS_VOICE_VERSION);
final Term langVal = solveSimplePredicate("language"); final Term langVal = solveSimplePredicate("language");
if (langVal instanceof Struct) { if (langVal instanceof Struct) {
language = ((Struct) langVal).getName(); language = ((Struct) langVal).getName();
} }
if (Algoritms.isEmpty(language)) { if (Algoritms.isEmpty(language)) {
throw new CommandPlayerException( throw new CommandPlayerException(
ctx.getString(R.string.voice_data_corrupted)); ctx.getString(R.string.voice_data_corrupted));
} }
onActivityInit(ctx); onActivityInit(ctx);
} }
@Override @Override
public void playCommands(CommandBuilder builder) { public void playCommands(CommandBuilder builder) {
if (mTts != null) { if (mTts != null) {
final List<String> execute = builder.execute(); //list of strings, the speech text, play it final List<String> execute = builder.execute(); //list of strings, the speech text, play it
StringBuilder bld = new StringBuilder(); StringBuilder bld = new StringBuilder();
for (String s : execute) { for (String s : execute) {
bld.append(s).append(' '); bld.append(s).append(' ');
} }
mTts.speak(bld.toString(), TextToSpeech.QUEUE_ADD, null); mTts.speak(bld.toString(), TextToSpeech.QUEUE_ADD, null);
} }
} }
@Override @Override
public void onActivityInit(final Activity ctx) { public void onActivityInit(final Activity ctx) {
if (mTts != null && mTtsContext != ctx) { if (mTts != null && mTtsContext != ctx) {
//clear only, if the mTts was initialized in another context. //clear only, if the mTts was initialized in another context.
//Unfortunately, for example from settings to map first the map is initialized than //Unfortunately, for example from settings to map first the map is initialized than
//the settingsactivity is destroyed... //the settingsactivity is destroyed...
internalClear(); internalClear();
} }
if (mTts == null) { if (mTts == null) {
mTtsContext = ctx; mTtsContext = ctx;
mTts = new TextToSpeech(ctx, new OnInitListener() { mTts = new TextToSpeech(ctx, new OnInitListener() {
@Override @Override
public void onInit(int status) { public void onInit(int status) {
if (status != TextToSpeech.SUCCESS) { if (status != TextToSpeech.SUCCESS) {
internalClear(); internalClear();
} else { } else {
switch (mTts.isLanguageAvailable(new Locale(language))) switch (mTts.isLanguageAvailable(new Locale(language)))
{ {
case TextToSpeech.LANG_MISSING_DATA: case TextToSpeech.LANG_MISSING_DATA:
internalClear(); internalClear();
Builder builder = createAlertDialog( Builder builder = createAlertDialog(
R.string.tts_missing_language_data_title, R.string.tts_missing_language_data_title,
R.string.tts_missing_language_data, R.string.tts_missing_language_data,
new IntentStarter( new IntentStarter(
ctx, ctx,
TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA), TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA),
ctx); ctx);
builder.show(); builder.show();
break; break;
case TextToSpeech.LANG_AVAILABLE: case TextToSpeech.LANG_AVAILABLE:
mTts.setLanguage(new Locale(language)); mTts.setLanguage(new Locale(language));
break; break;
case TextToSpeech.LANG_NOT_SUPPORTED: case TextToSpeech.LANG_NOT_SUPPORTED:
//maybe weird, but I didn't want to introduce parameter in around 5 methods just to do //maybe weird, but I didn't want to introduce parameter in around 5 methods just to do
//this if condition //this if condition
if (ctx instanceof SettingsActivity) { if (ctx instanceof SettingsActivity) {
builder = createAlertDialog( builder = createAlertDialog(
R.string.tts_language_not_supported_title, R.string.tts_language_not_supported_title,
R.string.tts_language_not_supported, R.string.tts_language_not_supported,
new IntentStarter( new IntentStarter(
ctx, ctx,
Intent.ACTION_VIEW, Uri.parse("market://search?q=text to speech engine" Intent.ACTION_VIEW, Uri.parse("market://search?q=text to speech engine"
)), )),
ctx); ctx);
builder.show(); builder.show();
} }
break; break;
} }
} }
} }
}); });
} }
} }
private Builder createAlertDialog(int titleResID, int messageResID, IntentStarter intentStarter, final Activity ctx) { private Builder createAlertDialog(int titleResID, int messageResID, IntentStarter intentStarter, final Activity ctx) {
Builder builder = new AlertDialog.Builder(ctx); Builder builder = new AlertDialog.Builder(ctx);
builder.setCancelable(true); builder.setCancelable(true);
builder.setNegativeButton(R.string.default_buttons_no, null); builder.setNegativeButton(R.string.default_buttons_no, null);
builder.setPositiveButton(R.string.default_buttons_yes, intentStarter); builder.setPositiveButton(R.string.default_buttons_yes, intentStarter);
builder.setTitle(titleResID); builder.setTitle(titleResID);
builder.setMessage(messageResID); builder.setMessage(messageResID);
return builder; return builder;
} }
@Override @Override
public void onActvitiyStop(Context ctx) { public void onActvitiyStop(Context ctx) {
//stop only when the context is the same //stop only when the context is the same
if (mTtsContext == ctx) { if (mTtsContext == ctx) {
internalClear(); internalClear();
} }
} }
private void internalClear() { private void internalClear() {
if (mTts != null) { if (mTts != null) {
mTts.shutdown(); mTts.shutdown();
mTtsContext = null; mTtsContext = null;
mTts = null; mTts = null;
} }
} }
@Override @Override
public void clear() { public void clear() {
super.clear(); super.clear();
internalClear(); internalClear();
} }
public static boolean isMyData(File voiceDir) throws CommandPlayerException { public static boolean isMyData(File voiceDir) throws CommandPlayerException {
return new File(voiceDir, CONFIG_FILE).exists(); return new File(voiceDir, CONFIG_FILE).exists();
} }
} }