Skip to content

Commit b80884c

Browse files
committed
Improving error logging, especially around SpeechService based on customer feedback.
1 parent 04f9545 commit b80884c

4 files changed

Lines changed: 74 additions & 23 deletions

File tree

app/build.gradle.kts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ android {
1515
minSdk = 26
1616
//noinspection EditedTargetSdkVersion
1717
targetSdk = 34
18-
versionCode = 1
19-
versionName = "1.0"
18+
versionCode = 7
19+
versionName = "1.1"
2020

2121
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2222
}
@@ -40,20 +40,21 @@ android {
4040
buildFeatures {
4141
viewBinding = true
4242
}
43+
buildToolsVersion = "34.0.0"
4344
}
4445

4546
dependencies {
4647

4748
implementation("androidx.core:core-ktx:1.12.0")
4849
implementation("androidx.appcompat:appcompat:1.6.1")
4950
implementation("com.google.android.material:material:1.11.0-alpha03")
50-
implementation("androidx.compose.material:material-icons-extended:1.6.0-alpha06")
51-
implementation("androidx.compose.material:material-ripple:1.6.0-alpha06")
51+
implementation("androidx.compose.material:material-icons-extended:1.6.0-alpha07")
52+
implementation("androidx.compose.material:material-ripple:1.6.0-alpha07")
5253
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
5354
implementation("androidx.lifecycle:lifecycle-livedata-ktx:2.6.2")
5455
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2")
55-
implementation("androidx.navigation:navigation-fragment-ktx:2.7.3")
56-
implementation("androidx.navigation:navigation-ui-ktx:2.7.3")
56+
implementation("androidx.navigation:navigation-fragment-ktx:2.7.4")
57+
implementation("androidx.navigation:navigation-ui-ktx:2.7.4")
5758
implementation("androidx.work:work-runtime-ktx:2.8.1")
5859
implementation("com.opencsv:opencsv:4.6")
5960
implementation("androidx.preference:preference-ktx:1.2.1")
@@ -66,7 +67,9 @@ dependencies {
6667
implementation("androidx.datastore:datastore-rxjava2:1.0.0")
6768
implementation("androidx.datastore:datastore-rxjava3:1.0.0")
6869
implementation("androidx.datastore:datastore-preferences:1.0.0")
69-
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")
70+
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha13")
71+
implementation("androidx.activity:activity-ktx:1.8.0")
72+
implementation("androidx.fragment:fragment-ktx:1.7.0-alpha06")
7073

7174
// Import the BoM for the Firebase platform
7275
implementation(platform("com.google.firebase:firebase-bom:32.3.1"))

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,40 @@ class BackgroundSpeechService(val context: Context, workerParams: WorkerParamete
1818
private val word = inputData.getString("word")
1919

2020
companion object {
21+
const val FAILURE_UNKNOWN = "FAILURE_UNKNOWN"
2122
const val FAILURE_REASON = "FAILURE_REASON"
2223
const val FAILURE_MUTED = "FAILURE_MUTED"
24+
const val FAILURE_INIT_FAILED = "FAILURE_INITFAILED"
25+
const val FAILURE_LANG_UNSUPPORTED = "FAILURE_LANG_UNSUPPORTED"
2326
}
2427

2528
@SuppressLint("RestrictedApi")
2629
override fun doWork(): Result {
27-
Log.i("BackgroundSpeechService", "Starting to play ${word} out loud.")
30+
Log.i("BackgroundSpeechService", "Readying to play ${word} out loud.")
2831
if (isMuted()) {
2932
Log.i("BackgroundSpeechService", "But volume is muted. Aborting.")
3033
return Result.failure(Data(mapOf(FAILURE_REASON to FAILURE_MUTED)))
3134
}
3235

33-
while (initStatus == null) { sleep(10); }
36+
while (initStatus == null) {
37+
Log.i("BackgroundSpeechService", "Not (yet) ready to play ${word} out loud.")
38+
sleep(10)
39+
}
3440

3541
if (initStatus != TextToSpeech.SUCCESS) {
3642
Log.e("BackgroundSpeechService", "Initialization Failed!")
3743
return Result.failure()
3844
}
3945

46+
Log.i("BackgroundSpeechService", "Setting language to play ${word} out loud.")
4047
val result = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE)
4148
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
42-
Log.e("BackgroundSpeechService","Simplified_chinese not supported on this phone.")
49+
Log.e("BackgroundSpeechService", "Simplified_chinese not supported on this phone.")
4350
return Result.failure()
4451
}
4552

46-
textToSpeech.speak(word, TextToSpeech.QUEUE_FLUSH, null,"")
53+
Log.i("BackgroundSpeechService", "Starting to play ${word} out loud.")
54+
textToSpeech.speak(word, TextToSpeech.QUEUE_FLUSH, null, "")
4755
Log.i("BackgroundSpeechService", "Finishing to play ${word} out loud.")
4856

4957
return Result.success()

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,45 @@ class Utils {
7070
val workMgr = WorkManager.getInstance(context)
7171
workMgr.getWorkInfoByIdLiveData(speechRequest.id).observeForever(
7272
object: Observer<WorkInfo> {
73-
override fun onChanged(workInfo: WorkInfo) {
74-
if (workInfo.state == WorkInfo.State.SUCCEEDED
75-
|| workInfo.state == WorkInfo.State.FAILED) {
76-
77-
if (workInfo.state == WorkInfo.State.FAILED
78-
&& workInfo.outputData.getString(BackgroundSpeechService.FAILURE_REASON) == BackgroundSpeechService.FAILURE_MUTED) {
79-
Toast.makeText(context, "Unmute to hear the word", Toast.LENGTH_LONG).show()
73+
override fun onChanged(workInfo: WorkInfo) {
74+
if (workInfo.state == WorkInfo.State.SUCCEEDED
75+
|| workInfo.state == WorkInfo.State.FAILED
76+
) {
77+
78+
var errStringId = R.string.speech_failure_toast_unknown
79+
if (workInfo.state == WorkInfo.State.FAILED) {
80+
var errId =
81+
workInfo.outputData.getString(BackgroundSpeechService.FAILURE_REASON)
82+
when (errId) {
83+
BackgroundSpeechService.FAILURE_MUTED
84+
-> errStringId = R.string.speech_failure_toast_muted
85+
86+
BackgroundSpeechService.FAILURE_INIT_FAILED
87+
-> errStringId = R.string.speech_failure_toast_init
88+
89+
BackgroundSpeechService.FAILURE_LANG_UNSUPPORTED
90+
-> errStringId =
91+
R.string.speech_failure_toast_chinese_unsupported
92+
93+
else -> {
94+
errStringId = R.string.speech_failure_toast_unknown
95+
errId = BackgroundSpeechService.FAILURE_UNKNOWN
96+
}
97+
}
98+
99+
logAnalyticsError(context, "SPEECH", errId, "")
100+
Toast.makeText(
101+
context, context.getString(errStringId),
102+
Toast.LENGTH_LONG
103+
).show()
104+
}
105+
106+
workMgr.getWorkInfoByIdLiveData(speechRequest.id)
107+
.removeObserver(this)
80108
}
81-
82-
workMgr.getWorkInfoByIdLiveData(speechRequest.id)
83-
.removeObserver(this)
84109
}
85-
86110
}
87-
})
111+
)
88112

89113
workMgr.enqueue(speechRequest)
90114
}
@@ -111,6 +135,17 @@ class Utils {
111135
Firebase.analytics.logEvent(event.name, bundle)
112136
}
113137

138+
fun logAnalyticsError(context: Context, module: String, error: String, details: String) {
139+
logAnalyticsEvent(
140+
context, ANALYTICS_EVENTS.ERROR,
141+
mapOf(
142+
"MODULE" to module,
143+
"ERROR_ID" to error,
144+
"DETAILS" to details
145+
)
146+
)
147+
}
148+
114149
fun logAnalyticsWidgetAction(context: Context, event: ANALYTICS_EVENTS, widgetId: Int) {
115150
val widgets = FlashcardWidgetProvider().getWidgetIds(context)
116151
val size = WidgetSizeProvider(context).getWidgetsSize(widgetId)
@@ -144,6 +179,7 @@ class Utils {
144179
enum class ANALYTICS_EVENTS {
145180
SCREEN_VIEW,
146181
AUTO_WORD_CHANGE,
182+
ERROR,
147183
WIDGET_PLAY_WORD,
148184
WIDGET_MANUAL_WORD_CHANGE,
149185
WIDGET_RECONFIGURE,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ We recommend to choose the level your at plus the one below, or above depending
5252
\n
5353
\nLicense: GNU GPLv3. In a nutshell, you can do whatever you want with it, but must display credit for the work and use the same license downstream. This project uses the HSK list from https://github.com/plaktos/hsk_csv/.</string>
5454
<string name="about_btn_email2">Email the developer</string>
55+
<string name="speech_failure_toast_unknown">Unknown speech error</string>
56+
<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>
58+
<string name="speech_failure_toast_init">www</string>
5559
</resources>

0 commit comments

Comments
 (0)