AI2 Component  (Version nb184)
IntentBasedSpeechRecognizer.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
8 import android.app.Activity;
9 import android.content.Intent;
10 import android.speech.RecognizerIntent;
11 
12 import java.util.ArrayList;
13 
15  implements ActivityResultListener {
16 
17  private String result;
18  private ComponentContainer container;
19  private Intent recognizerIntent;
20 
21  /* Used to identify the call to startActivityForResult. Will be passed back
22  into the resultReturned() callback method. */
23  private int requestCode;
24 
25  public IntentBasedSpeechRecognizer(ComponentContainer container, Intent recognizerIntent) {
26  this.container = container;
27  this.recognizerIntent = recognizerIntent;
28  }
29 
30  @Override
31  public void start() {
32  if(requestCode == 0) {
33  requestCode = container.$form().registerForActivityResult(this);
34  }
35  container.$context().startActivityForResult(recognizerIntent, requestCode);
36  }
37 
38  @Override
39  public void stop() {
40  //implementation not available
41  }
42 
43  @Override
44  public void resultReturned(int requestCode, int resultCode, Intent data) {
45  if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
46  if (data.hasExtra(RecognizerIntent.EXTRA_RESULTS)) {
47  ArrayList<String> results = data.getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
48  result = results.get(0);
49  } else {
50  result = "";
51  }
52  speechListener.onResult(result);
53  }
54  }
55 }
com.google.appinventor.components.runtime.SpeechRecognizerController
Definition: SpeechRecognizerController.java:11
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer.IntentBasedSpeechRecognizer
IntentBasedSpeechRecognizer(ComponentContainer container, Intent recognizerIntent)
Definition: IntentBasedSpeechRecognizer.java:25
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer.resultReturned
void resultReturned(int requestCode, int resultCode, Intent data)
Definition: IntentBasedSpeechRecognizer.java:44
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer.stop
void stop()
Definition: IntentBasedSpeechRecognizer.java:39
com.google.appinventor.components.runtime.ActivityResultListener
Definition: ActivityResultListener.java:16
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer
Definition: IntentBasedSpeechRecognizer.java:14
com.google.appinventor.components.runtime.SpeechListener.onResult
void onResult(String text)
com.google.appinventor.components.runtime.IntentBasedSpeechRecognizer.start
void start()
Definition: IntentBasedSpeechRecognizer.java:31
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.Form.registerForActivityResult
int registerForActivityResult(ActivityResultListener listener)
Definition: Form.java:638