Skip to content

Commit

Permalink
Selectable Locale for Spoken readings
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Sep 19, 2016
1 parent 029b4a7 commit 19aae49
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
35 changes: 31 additions & 4 deletions app/src/main/java/com/eveningoutpost/dexdrip/utils/BgToSpeech.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import android.media.AudioManager;
import android.preference.PreferenceManager;
import android.speech.tts.TextToSpeech;

import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.UserError.Log;

import com.eveningoutpost.dexdrip.UtilityModels.Constants;
Expand Down Expand Up @@ -88,11 +91,23 @@ public void onInit(int status) {
if (status == TextToSpeech.SUCCESS && tts != null) {

//try local language

Locale loc = Locale.getDefault();
Log.d(TAG, "status == TextToSpeech.SUCCESS + loc" + loc);
try {
final String tts_language = Home.getPreferencesStringDefaultBlank("speak_readings_custom_language").trim();
if (tts_language.length() > 1) {
final String[] lang_components = tts_language.split("_");
String country = (lang_components.length > 1) ? lang_components[1] : "";
loc = new Locale(lang_components[0], country, "");
}
} catch (Exception e) {
Log.e(TAG, "Exception trying to use custom language: " + e);
}

Log.d(TAG, "status == TextToSpeech.SUCCESS + loc " + loc);
int result;
try {
result = tts.setLanguage(Locale.getDefault());
result = tts.setLanguage(loc);
} catch (IllegalArgumentException e) {
// can end up here with Locales like "OS"
Log.e(TAG, "Got TTS set language error: " + e.toString());
Expand Down Expand Up @@ -141,6 +156,17 @@ private void speakInternal(final double value, long timestamp) {
Log.d(TAG, "successfully spoken");
} else {
Log.d(TAG, "error " + result + ". trying again with new tts-object.");
JoH.runOnUiThreadDelayed(new Runnable() {
@Override
public void run() {
try {
tts.speak(calculateText(value, prefs), TextToSpeech.QUEUE_FLUSH, null);
} catch (Exception e) {
Log.e(TAG, "Got exception TTS delayed: " + e);
}
}
}, 2000);

}
} catch (IllegalStateException e) {
Log.e(TAG, "IllegalStateException in TTS: " + e.toString());
Expand All @@ -150,8 +176,8 @@ private void speakInternal(final double value, long timestamp) {
}

private String calculateText(double value, SharedPreferences prefs) {
boolean doMgdl = (prefs.getString("units", "mgdl").equals("mgdl"));

final boolean doMgdl = (prefs.getString("units", "mgdl").equals("mgdl"));
final boolean bg_to_speech_repeat_twice = (prefs.getBoolean("bg_to_speech_repeat_twice", false));
String text = "";

DecimalFormat df = new DecimalFormat("#");
Expand All @@ -170,6 +196,7 @@ private String calculateText(double value, SharedPreferences prefs) {
// in case the text has a comma in current locale but TTS defaults to English
text = text.replace(",", ".");
}
if (bg_to_speech_repeat_twice) text = text + " ... ... ... " + text;
} catch (NullPointerException e) {
Log.e(TAG, "Null pointer for TTS in calculateText");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.eveningoutpost.dexdrip.GcmActivity;
import com.eveningoutpost.dexdrip.Home;
import com.eveningoutpost.dexdrip.Models.BgReading;
import com.eveningoutpost.dexdrip.Models.JoH;
import com.eveningoutpost.dexdrip.Models.Profile;
import com.eveningoutpost.dexdrip.Models.UserError.Log;
Expand Down Expand Up @@ -1492,7 +1493,7 @@ private void bindTTSListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if ((Boolean) newValue) {
prefs.edit().putBoolean("bg_to_speech",true).commit(); // early write before we exit method
prefs.edit().putBoolean("bg_to_speech", true).commit(); // early write before we exit method
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Install Text-To-Speech Data?");
alertDialog.setMessage("Install Text-To-Speech Data?\n(After installation of languages you might have to press \"Restart Collector\" in System Status.)");
Expand All @@ -1506,7 +1507,15 @@ public void onClick(DialogInterface dialog, int which) {
alertDialog.setNegativeButton(R.string.no, null);
AlertDialog alert = alertDialog.create();
alert.show();
BgToSpeech.setupTTS(preference.getContext()); // try to initialize now
try {
BgToSpeech.setupTTS(preference.getContext()); // try to initialize now
BgReading bgReading = BgReading.last();
if (bgReading != null) {
BgToSpeech.speak(bgReading.calculated_value, bgReading.timestamp+1200000);
}
} catch (Exception e) {
Log.e(TAG, "Got exception with TTS: " + e);
}
} else {
BgToSpeech.tearDownTTS();
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/xml/pref_advanced_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@
android:key="bg_to_speech"
android:summary="@string/if_the_phone_has_text_to_speech"
android:title="@string/speak_readings" />
<EditTextPreference
android:key="speak_readings_custom_language"
android:dependency="bg_to_speech"
android:defaultValue=""
android:summary="Custom Locale for Spoken Readings, eg: de or en_IN"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="bg_to_speech_repeat_twice"
android:dependency="bg_to_speech"
android:summary="Speak the glucose value twice each time"/>
<CheckBoxPreference
android:defaultValue="false"
android:key="bg_to_speech_shortcut"
Expand Down

0 comments on commit 19aae49

Please sign in to comment.