Skip to content

Commit 433ffbb

Browse files
committed
Improving error logging, adding functionality to prompt the user to install missing voice data package.
1 parent b80884c commit 433ffbb

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

app/src/main/java/fr/berliat/hskwidget/domain/BackgroundSpeechService.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package fr.berliat.hskwidget.domain
22

33
import android.annotation.SuppressLint
44
import android.content.Context
5+
import android.content.Intent
6+
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
57
import android.media.AudioManager
68
import android.os.SystemClock.sleep
79
import android.speech.tts.TextToSpeech
810
import android.util.Log
11+
import androidx.core.content.ContextCompat
912
import androidx.work.Data
1013
import androidx.work.Worker
1114
import androidx.work.WorkerParameters
1215
import java.util.Locale
1316

14-
class BackgroundSpeechService(val context: Context, workerParams: WorkerParameters)
15-
: Worker(context, workerParams), TextToSpeech.OnInitListener {
17+
18+
class BackgroundSpeechService(val context: Context, workerParams: WorkerParameters) :
19+
Worker(context, workerParams), TextToSpeech.OnInitListener {
1620
private val textToSpeech = TextToSpeech(context, this)
1721
private var initStatus: Int? = null
1822
private val word = inputData.getString("word")
@@ -40,14 +44,20 @@ class BackgroundSpeechService(val context: Context, workerParams: WorkerParamete
4044

4145
if (initStatus != TextToSpeech.SUCCESS) {
4246
Log.e("BackgroundSpeechService", "Initialization Failed!")
43-
return Result.failure()
47+
return Result.failure(Data(mapOf(FAILURE_REASON to FAILURE_INIT_FAILED)))
4448
}
4549

4650
Log.i("BackgroundSpeechService", "Setting language to play ${word} out loud.")
4751
val result = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE)
4852
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
4953
Log.e("BackgroundSpeechService", "Simplified_chinese not supported on this phone.")
50-
return Result.failure()
54+
55+
val installIntent = Intent()
56+
installIntent.action = TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA
57+
installIntent.flags = FLAG_ACTIVITY_NEW_TASK
58+
ContextCompat.startActivity(context, installIntent, null)
59+
60+
return Result.failure(Data(mapOf(FAILURE_REASON to FAILURE_LANG_UNSUPPORTED)))
5161
}
5262

5363
Log.i("BackgroundSpeechService", "Starting to play ${word} out loud.")

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ We recommend to choose the level your at plus the one below, or above depending
5454
<string name="about_btn_email2">Email the developer</string>
5555
<string name="speech_failure_toast_unknown">Unknown speech error</string>
5656
<string name="speech_failure_toast_muted">Unmute to hear the word</string>
57-
<string name="speech_failure_toast_chinese_unsupported">Chinese voice not supported</string>
57+
<string name="speech_failure_toast_chinese_unsupported">Please install the "Chinese (China)" voice</string>
5858
<string name="speech_failure_toast_init">www</string>
5959
</resources>

0 commit comments

Comments
 (0)