@@ -2,17 +2,21 @@ package fr.berliat.hskwidget.domain
22
33import android.annotation.SuppressLint
44import android.content.Context
5+ import android.content.Intent
6+ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
57import android.media.AudioManager
68import android.os.SystemClock.sleep
79import android.speech.tts.TextToSpeech
810import android.util.Log
11+ import androidx.core.content.ContextCompat
912import androidx.work.Data
1013import androidx.work.Worker
1114import androidx.work.WorkerParameters
1215import 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." )
0 commit comments