11package com.nlinterface.activities
22
33import android.content.Context
4- import android.content.Intent
54import android.os.Bundle
6- import android.util.Log
75import android.view.View
86import android.widget.Button
97import android.widget.ImageButton
@@ -17,6 +15,7 @@ import com.nlinterface.utility.GlobalParameters
1715import com.nlinterface.utility.GlobalParameters.ThemeChoice
1816import com.nlinterface.utility.GlobalParameters.KeepScreenOn
1917import com.nlinterface.utility.STTInputType
18+ import com.nlinterface.utility.navToActivity
2019import com.nlinterface.utility.setViewRelativeSize
2120import com.nlinterface.viewmodels.SettingsViewModel
2221import kotlinx.coroutines.CoroutineScope
@@ -130,8 +129,8 @@ class SettingsActivity : AppCompatActivity() {
130129 }
131130
132131 /* *
133- * Cycle through the options for the Screen On settings, when the button is clicked. Narrate the
134- * action.
132+ * Cycles through the options for the Screen On settings, when the button is clicked. Narrates
133+ * the action.
135134 */
136135 private fun onKeepScreenOnButtonClick () {
137136
@@ -180,10 +179,10 @@ class SettingsActivity : AppCompatActivity() {
180179 */
181180 private fun onVoiceActivationButtonClick () {
182181 if (viewModel.isListening.value == false ) {
183- viewModel.setSpeechRecognitionListener (STTInputType .COMMAND )
184- viewModel.handleSpeechBegin ()
182+ viewModel.setSTTSpeechRecognitionListener (STTInputType .COMMAND )
183+ viewModel.handleSTTSpeechBegin ()
185184 } else {
186- viewModel.cancelListening ()
185+ viewModel.cancelSTTListening ()
187186 }
188187 }
189188
@@ -210,7 +209,8 @@ class SettingsActivity : AppCompatActivity() {
210209 * Called by the onCreate function and calls upon the ViewModel to initialize the STT system.
211210 * The voiceActivationButton is configured to change it microphone color to green, if the STT
212211 * system is active and to change back to white, if it is not. Also retrieves the text output
213- * of the voice input to the STT system, aka the 'command'
212+ * of the voice input to the STT system, aka the 'command', as well as a 'response', if a
213+ * question was asked by the system.
214214 */
215215 private fun configureSTT () {
216216
@@ -231,16 +231,22 @@ class SettingsActivity : AppCompatActivity() {
231231 // if a command is successfully generated, process and execute it
232232 val commandObserver = Observer <String > { command ->
233233 lastCommand = command
234- executeCommand (command)
234+ handleSTTCommand (command)
235235 }
236236
237237 // observe LiveData change to be notified when the STT returns a command
238238 viewModel.command.observe(this , commandObserver)
239239
240240 // if a response is successfully generated, process and execute it
241241 val responseObserver = Observer <String > { response ->
242+
242243 lastResponse = response
243- executeSettingsCommand(lastCommand, lastResponse)
244+
245+ // no need to handle cancelled responses
246+ if (response != resources.getString(R .string.cancel)) {
247+ handleSTTResponse(lastCommand, lastResponse)
248+ }
249+
244250 }
245251
246252 // observe LiveData change to be notified when the STT returns a response
@@ -250,14 +256,15 @@ class SettingsActivity : AppCompatActivity() {
250256
251257 /* *
252258 * Called once the STT system returns a command. It is then processed and, if valid,
253- * finally executed by navigating to the next activity
259+ * executed by further methods.
254260 *
255- * @param command: ArrayList< String>? containing the deconstructed command
261+ * @param command: String containing the deconstructed command
256262 *
257263 * TODO: streamline processing and command structure
258264 */
259- private fun executeCommand (command : String ) {
265+ private fun handleSTTCommand (command : String ) {
260266
267+ // any attempted navigation commands are handled are passed on
261268 if (command.contains(" go to" )) {
262269 executeNavigationCommand(command)
263270
@@ -297,29 +304,37 @@ class SettingsActivity : AppCompatActivity() {
297304
298305 }
299306
300- private fun executeSettingsCommand (command : String , response : String ) {
301-
302- if (response != resources.getString(R .string.cancel)) {
307+ /* *
308+ * Called when a response to a system question is registered. The response is then processed
309+ * and executed dependent on the system question/last command.
310+ *
311+ * @param command: String, the last command, which triggered the response request
312+ * @param response: String, the registered response
313+ */
314+ private fun handleSTTResponse (command : String , response : String ) {
303315
304- when (command) {
316+ when (command) {
305317
306- resources.getString(R .string.change_theme) -> {
307- changeTheme (response)
308- }
318+ resources.getString(R .string.change_theme) -> {
319+ executeChangeThemeCommand (response)
320+ }
309321
310- resources.getString(R .string.change_screen_settings) -> {
311- changeScreenSettings(response)
312- }
313-
322+ resources.getString(R .string.change_screen_settings) -> {
323+ executeChangScreenSettingsCommand(response)
314324 }
315-
325+
316326 }
317327
318328 }
319329
320- private fun changeTheme (response : String ) {
330+ /* *
331+ * Executes the change theme command according to the theme choice made.
332+ *
333+ * @param choice: String, the requested new theme
334+ */
335+ private fun executeChangeThemeCommand (choice : String ) {
321336
322- when (response ) {
337+ when (choice ) {
323338
324339 resources.getString(R .string.default_theme) -> {
325340 viewModel.setTheme(ThemeChoice .SYSTEM_DEFAULT )
@@ -342,13 +357,20 @@ class SettingsActivity : AppCompatActivity() {
342357 )
343358 }
344359
360+ else -> viewModel.say(resources.getString(R .string.invalid_command))
361+
345362 }
346363
347364 }
348365
349- private fun changeScreenSettings (response : String ) {
366+ /* *
367+ * Executes the change screen settings command according to the screen settings choice made.
368+ *
369+ * @param choice: String, the requested new theme
370+ */
371+ private fun executeChangScreenSettingsCommand (choice : String ) {
350372
351- when (response ) {
373+ when (choice ) {
352374
353375 resources.getString(R .string.keep_screen_always_on) -> {
354376 viewModel.setScreenSettings(KeepScreenOn .YES )
@@ -366,6 +388,8 @@ class SettingsActivity : AppCompatActivity() {
366388 )
367389 }
368390
391+ else -> viewModel.say(resources.getString(R .string.invalid_command))
392+
369393 }
370394
371395 }
@@ -378,57 +402,34 @@ class SettingsActivity : AppCompatActivity() {
378402 */
379403 private fun executeNavigationCommand (command : String ) {
380404
381- if ((command == resources.getString(R .string.navigate_to_grocery_list))) {
382- navToActivity(ActivityType .GROCERYLIST )
383- } else if ((command == resources.getString(R .string.navigate_to_place_details))) {
384- navToActivity(ActivityType .PLACEDETAILS )
385- } else if ((command == resources.getString(R .string.navigate_to_settings))) {
386- navToActivity(ActivityType .SETTINGS )
387- } else if ((command == resources.getString(R .string.navigate_to_main_menu))) {
388- navToActivity(ActivityType .MAIN )
389- } else {
390- viewModel.say(resources.getString(R .string.invalid_command))
405+ when (command) {
406+ resources.getString(R .string.navigate_to_grocery_list) ->
407+ navToActivity(this , ActivityType .GROCERYLIST )
408+
409+ resources.getString(R .string.navigate_to_place_details) ->
410+ navToActivity(this , ActivityType .GROCERYLIST )
411+
412+ resources.getString(R .string.navigate_to_settings) ->
413+ navToActivity(this , ActivityType .GROCERYLIST )
414+
415+ resources.getString(R .string.navigate_to_main_menu) ->
416+ navToActivity(this , ActivityType .GROCERYLIST )
417+
418+ else -> viewModel.say(resources.getString(R .string.invalid_command))
391419 }
392420
393421 }
394422
395- private suspend fun requestResponse (question : String ) {
396- viewModel.sayAndAwait(question)
397- viewModel.setSpeechRecognitionListener(STTInputType .ANSWER )
398- viewModel.handleSpeechBegin()
399- }
400-
401423 /* *
402- * Handles navigation to next activity. Called either by button click or by execution of the
403- * voice command. If the called for activity is the current one, read out the activity name .
424+ * Requests a vocal response from the user by reading a passed question out loud. Once the TTS
425+ * process is completed, the STT process is activated so that a response can be made directly .
404426 *
405- * @param activity: ActivityType, Enum specifying the activity
427+ * @param question: String, the system question to which a response is requested
406428 */
407- private fun navToActivity (activity : ActivityType ) {
408-
409- Log .println (Log .DEBUG , " navToActivity" , activity.toString())
410-
411- when (activity) {
412-
413- ActivityType .SETTINGS -> {
414- viewModel.say(resources.getString(R .string.settings))
415- }
416-
417- ActivityType .MAIN -> {
418- val intent = Intent (this , MainActivity ::class .java)
419- this .startActivity(intent)
420- }
421-
422- ActivityType .GROCERYLIST -> {
423- val intent = Intent (this , GroceryListActivity ::class .java)
424- this .startActivity(intent)
425- }
426-
427- ActivityType .PLACEDETAILS -> {
428- val intent = Intent (this , PlaceDetailsActivity ::class .java)
429- this .startActivity(intent)
430- }
431-
432- }
429+ private suspend fun requestResponse (question : String ) {
430+ viewModel.sayAndAwait(question)
431+ viewModel.setSTTSpeechRecognitionListener(STTInputType .ANSWER )
432+ viewModel.handleSTTSpeechBegin()
433433 }
434+
434435}
0 commit comments