Skip to content

Commit

Permalink
Select voice in correct language. Issue #140
Browse files Browse the repository at this point in the history
Translate voice text.
  • Loading branch information
Starcommander committed Mar 30, 2020
1 parent 1321400 commit 3805a17
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.junjunguo.pocketmaps.fragments.Dialog;
import com.junjunguo.pocketmaps.fragments.MessageDialog;
import com.junjunguo.pocketmaps.fragments.MyMapAdapter;
import com.junjunguo.pocketmaps.navigator.NaviText;
import com.junjunguo.pocketmaps.navigator.NaviVoice;
import com.junjunguo.pocketmaps.util.IO;
import com.junjunguo.pocketmaps.util.SetStatusBarColor;
import com.junjunguo.pocketmaps.util.Variable;
Expand Down Expand Up @@ -69,6 +71,7 @@ public class MainActivity extends AppCompatActivity implements OnClickMapListene
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NaviText.initTextList(this);
continueActivity();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.junjunguo.pocketmaps.R;
import com.junjunguo.pocketmaps.model.listeners.NavigatorListener;
import com.junjunguo.pocketmaps.navigator.NaviEngine;
import com.junjunguo.pocketmaps.navigator.NaviText;
import com.junjunguo.pocketmaps.util.UnitCalculator;
import com.junjunguo.pocketmaps.util.Variable;

Expand Down Expand Up @@ -325,8 +326,63 @@ public int getDirectionSignHuge(Instruction itemData) {
* @return direction
*/
public String getDirectionDescription(Instruction instruction, boolean longText) {
if (instruction.getSign() == 4) return "Navigation End";//4
String str; // TODO: Translate all this instructions to Language?
if (instruction.getSign() == 4) return NaviText.sNavEnd;
String str;
String streetName = instruction.getName();
int sign = instruction.getSign();
String dir = "";
String dirTo = NaviText.sOnto;
switch (sign) {
case Instruction.CONTINUE_ON_STREET:
dir = (NaviText.sContinue);
dirTo = NaviText.sOn;
break;
case Instruction.LEAVE_ROUNDABOUT:
dir = (NaviText.sLeaveRound);
break;
case Instruction.TURN_SHARP_LEFT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sSharpL));
break;
case Instruction.TURN_LEFT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sLeft));
break;
case Instruction.TURN_SLIGHT_LEFT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sSlightL));
break;
case Instruction.TURN_SLIGHT_RIGHT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sSlightR));
break;
case Instruction.TURN_RIGHT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sRight));
break;
case Instruction.TURN_SHARP_RIGHT:
dir = (NaviText.sTurnXXX.replace("xxx", NaviText.sSharpR));
break;
case Instruction.REACHED_VIA:
dir = ("Reached via");
break;
case Instruction.USE_ROUNDABOUT:
dir = (NaviText.sUseRound);
break;
case Instruction.KEEP_LEFT:
dir = (NaviText.sKeepXXX.replace("xxx", NaviText.sLeft));
break;
case Instruction.KEEP_RIGHT:
dir = (NaviText.sKeepXXX.replace("xxx", NaviText.sRight));
break;
}
if (!longText) { return dir; }
str = Helper.isEmpty(streetName) ? dir : (dir + " " + dirTo + " " + streetName);
return str;
}

/**
* @param instruction
* @return direction
*/
public String getDirectionDescriptionFallback(Instruction instruction, boolean longText) {
if (instruction.getSign() == 4) return "Navigation End";
String str;
String streetName = instruction.getName();
int sign = instruction.getSign();
String dir = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ private NaviInstruction getNewInstruction()
NaviInstruction nIn = new NaviInstruction(in, nextIn, fullTime);
if (speakDistanceCheck(in.getDistance()) && nearestP.isDirectionOk())
{
naviVoice.speak(nIn.getVoiceText());
naviVoice.speak(nIn.getVoiceTextFallback(), nIn.getVoiceText());
naviVoiceSpoken = true;
}
else
Expand Down Expand Up @@ -495,7 +495,7 @@ private NaviInstruction getUpdatedInstruction(GeoPoint curPos, PointPosData near
newIn.updateDist(partDistance);
if (!naviVoiceSpoken && nearestP.isDirectionOk() && speakDistanceCheck(partDistance))
{
naviVoice.speak(newIn.getVoiceText());
naviVoice.speak(newIn.getVoiceTextFallback(), newIn.getVoiceText());
naviVoiceSpoken = true;
}
return newIn;
Expand Down Expand Up @@ -606,7 +606,7 @@ public void checkDirectionOk(Location pos, Instruction in, NaviVoice v)
{
if (wrongDirHint) { return; }
wrongDirHint = true;
v.speak("Wrong direction");
v.speak("Wrong direction", NaviText.sWrongDir);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class NaviInstruction
String curStreet;
String nextInstruction;
String nextInstructionShort;
String nextInstructionShortFallback;
long fullTime;
String fullTimeString;
double nextDistance;
Expand All @@ -25,13 +26,15 @@ public NaviInstruction(Instruction in, Instruction nextIn, long fullTime)
nextSignResource = Navigator.getNavigator().getDirectionSignHuge(nextIn);
nextInstruction = Navigator.getNavigator().getDirectionDescription(nextIn, true);
nextInstructionShort = Navigator.getNavigator().getDirectionDescription(nextIn, false);
nextInstructionShortFallback = Navigator.getNavigator().getDirectionDescriptionFallback(nextIn, false);
}
else
{
nextSign = in.getSign(); // Finished?
nextSignResource = Navigator.getNavigator().getDirectionSignHuge(in);
nextInstruction = Navigator.getNavigator().getDirectionDescription(in, true);
nextInstructionShort = Navigator.getNavigator().getDirectionDescription(in, false);
nextInstructionShortFallback = Navigator.getNavigator().getDirectionDescriptionFallback(in, false);
}
if (nextSignResource == 0) { nextSignResource = R.drawable.ic_2x_continue_on_street; }
nextDistance = in.getDistance();
Expand Down Expand Up @@ -68,6 +71,20 @@ public void updateDist(double partDistance)


public String getVoiceText()
{
String unit = " " + NaviText.sMeters + ". ";
int roundetDistance = (int)nextDistance;
if (Variable.getVariable().isImperalUnit())
{
unit = " " + NaviText.sFeets + ". ";
roundetDistance = (int)(nextDistance / UnitCalculator.METERS_OF_FEET);
}
roundetDistance = roundetDistance/10;
roundetDistance = roundetDistance * 10;
return NaviText.sIn + " " + roundetDistance + unit + nextInstructionShort;
}

public String getVoiceTextFallback()
{
String unit = " meters. ";
int roundetDistance = (int)nextDistance;
Expand All @@ -78,6 +95,6 @@ public String getVoiceText()
}
roundetDistance = roundetDistance/10;
roundetDistance = roundetDistance * 10;
return "In " + roundetDistance + unit + nextInstructionShort;
return "In " + roundetDistance + unit + nextInstructionShortFallback;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.junjunguo.pocketmaps.navigator;

import android.app.Activity;
import com.junjunguo.pocketmaps.R;

/**
*
* @author ppp
*/
public class NaviText
{
public static String sIn;
public static String sMeters;
public static String sFeets;
public static String sUseRound;
public static String sLeaveRound;
public static String sTurnXXX;
public static String sKeepXXX;
public static String sLeft;
public static String sRight;
public static String sSlightL;
public static String sSlightR;
public static String sSharpL;
public static String sSharpR;
public static String sWrongDir;
public static String sOn;
public static String sOnto;
public static String sContinue;
public static String sNavEnd;

public static void initTextList(Activity activity)
{
if (sIn != null) { return; }
sIn = activity.getResources().getString(R.string.navivoice_in);
sFeets = activity.getResources().getString(R.string.navivoice_feets);
sMeters = activity.getResources().getString(R.string.navivoice_meters);
sUseRound = activity.getResources().getString(R.string.navivoice_useround);
sLeaveRound = activity.getResources().getString(R.string.navivoice_leaveround);
sTurnXXX = activity.getResources().getString(R.string.navivoice_turnxxx);
sKeepXXX = activity.getResources().getString(R.string.navivoice_keepxxx);
sLeft = activity.getResources().getString(R.string.navivoice_left);
sRight = activity.getResources().getString(R.string.navivoice_right);
sSlightL = activity.getResources().getString(R.string.navivoice_slightl);
sSlightR = activity.getResources().getString(R.string.navivoice_slightr);
sSharpL = activity.getResources().getString(R.string.navivoice_sharpl);
sSharpR = activity.getResources().getString(R.string.navivoice_sharpr);
sWrongDir = activity.getResources().getString(R.string.navivoice_wrongdir);
sOn = activity.getResources().getString(R.string.navivoice_on);
sOnto = activity.getResources().getString(R.string.navivoice_onto);
sContinue = activity.getResources().getString(R.string.navivoice_continue);
sNavEnd = activity.getResources().getString(R.string.navivoice_navend);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
import java.util.HashSet;

import com.junjunguo.pocketmaps.util.Variable;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.speech.tts.TextToSpeech;
import android.speech.tts.Voice;
import android.speech.tts.TextToSpeech.OnInitListener;
import java.util.ArrayList;

public class NaviVoice
{
public static final String DISPLAY_LANG = Locale.getDefault().getDisplayLanguage();
TextToSpeech tts;
Voice curVoice;
Locale wantedLang = Locale.ENGLISH;
Locale wantedLang = new Locale(DISPLAY_LANG);
Locale fallbackLang = Locale.ENGLISH;
String wantedName;
boolean ttsReady;
boolean ttsMute = false;

public NaviVoice(Context context)
{
tts = new TextToSpeech(context, createInitListener());
updateVoiceCompat();
}

public boolean isTtsReady()
Expand All @@ -34,13 +41,14 @@ public boolean isTtsReady()

public void setTtsMute(boolean ttsMute) { this.ttsMute = ttsMute; }

public void speak(String txt)
public void speak(String fallbackTxt, String txt)
{
if (!ttsReady) { return; }
if (!Variable.getVariable().isVoiceON()) { return; }
if (ttsMute) { return; }
updateVoiceCompat();
speakCompat(txt);
if (wantedLang == fallbackLang) { speakCompat(fallbackTxt); }
else { speakCompat(txt); }
}

/** Hint from https://stackoverflow.com/questions/27968146/texttospeech-with-api-21#29777304 **/
Expand Down Expand Up @@ -87,8 +95,10 @@ private void updateVoiceCompat()
private void updateVoiceUnder20()
{
Locale l = tts.getLanguage();
if (l.equals(wantedLang)) { return; }
tts.setLanguage(l);
if (l.equals(wantedLang) || l.equals(fallbackLang)) { return; }
tts.setLanguage(wantedLang);
if (!tts.getLanguage().equals(wantedLang))
tts.setLanguage(fallbackLang);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Expand All @@ -97,7 +107,12 @@ private void updateVoiceGreater21()
if (!ttsReady) { return; }
if (curVoice == null)
{
curVoice = searchWantedVoiceGreater21();
curVoice = searchWantedVoiceGreater21(wantedLang);
if (curVoice == null)
{
wantedLang = fallbackLang;
curVoice = searchWantedVoiceGreater21(fallbackLang);
}
if (curVoice == null) { return; }
}
else
Expand All @@ -108,9 +123,9 @@ private void updateVoiceGreater21()
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Voice searchWantedVoiceGreater21()
private Voice searchWantedVoiceGreater21(Locale selLang)
{
Set<Voice> curVoices = getVoiceListGreater21(wantedLang);
Set<Voice> curVoices = getVoiceListGreater21(selLang);
if (curVoices.size() == 0) { return null; }
if (wantedName != null)
{
Expand All @@ -126,7 +141,13 @@ private Voice searchWantedVoiceGreater21()
public Set<Voice> getVoiceListGreater21(Locale lang)
{
if (!ttsReady) { return null; }
return tts.getVoices();
Set<Voice> allV = tts.getVoices();
Set<Voice> selV = new HashSet<>();
for (Voice curV : allV)
{
if (curV.getLocale().getLanguage().equals(lang.getLanguage())) { selV.add(curV); }
}
return selV;
}

/** Can be set before androids TTS is ready. **/
Expand Down
18 changes: 18 additions & 0 deletions PocketMaps/app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@
<string name="search_hint">Texteingabe</string>
<string name="edit">Bearbeiten</string>
<string name="help">Hilfe</string>
<string name="navivoice_wrongdir">Falsche Richtung</string>
<string name="navivoice_in">Nach</string>
<string name="navivoice_meters">meter</string>
<string name="navivoice_feets">Fuß</string>
<string name="navivoice_useround">benutzen sie den Kreisverkehr</string>
<string name="navivoice_turnxxx">biegen sie xxx ab</string>
<string name="navivoice_keepxxx">halten sie sich xxx</string>
<string name="navivoice_left">links</string>
<string name="navivoice_right">rechts</string>
<string name="navivoice_slightl">leicht links</string>
<string name="navivoice_slightr">leicht rechts</string>
<string name="navivoice_sharpl">scharf links</string>
<string name="navivoice_sharpr">scharf rechts</string>
<string name="navivoice_on">auf</string>
<string name="navivoice_onto">auf</string>
<string name="navivoice_continue">weiter fahren</string>
<string name="navivoice_leaveround">verlassen sie den Kreisverkehr</string>
<string name="navivoice_navend">Navigation beendet</string>
</resources>
18 changes: 18 additions & 0 deletions PocketMaps/app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,22 @@
<string name="search_hint">inserire il testo</string>
<string name="edit">Modifica</string>
<string name="help">Aiuto</string>
<string name="navivoice_wrongdir">Direzione sbagliata</string>
<string name="navivoice_in">In</string>
<string name="navivoice_meters">metri</string>
<string name="navivoice_feets">piedi</string>
<string name="navivoice_useround">lasciare la rotonda</string>
<string name="navivoice_turnxxx">gira a xxx</string>
<string name="navivoice_keepxxx">tieni la xxx</string>
<string name="navivoice_left">sinistra</string>
<string name="navivoice_right">destra</string>
<string name="navivoice_slightl">slight left</string>
<string name="navivoice_slightr">slight right</string>
<string name="navivoice_sharpl">forte a sinistra</string>
<string name="navivoice_sharpr">forte a destra</string>
<string name="navivoice_on">su</string>
<string name="navivoice_onto">onto</string>
<string name="navivoice_continue">continuare</string>
<string name="navivoice_leaveround">lasciare la rotonda</string>
<string name="navivoice_navend">fine della navigazione</string>
</resources>
Loading

0 comments on commit 3805a17

Please sign in to comment.