OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
| 7 import android.content.Intent; |
7 import android.graphics.PointF; | 8 import android.graphics.PointF; |
8 import android.os.Bundle; | 9 import android.os.Bundle; |
| 10 import android.speech.RecognitionListener; |
| 11 import android.speech.RecognizerIntent; |
| 12 import android.speech.SpeechRecognizer; |
9 | 13 |
10 import com.google.vrtoolkit.cardboard.CardboardActivity; | 14 import com.google.vrtoolkit.cardboard.CardboardActivity; |
11 import com.google.vrtoolkit.cardboard.CardboardView; | 15 import com.google.vrtoolkit.cardboard.CardboardView; |
12 | 16 |
13 import org.chromium.chromoting.jni.JniInterface; | 17 import org.chromium.chromoting.jni.JniInterface; |
14 | 18 |
| 19 import java.util.ArrayList; |
| 20 |
15 /** | 21 /** |
16 * Virtual desktop activity for Cardboard. | 22 * Virtual desktop activity for Cardboard. |
17 */ | 23 */ |
18 public class CardboardDesktopActivity extends CardboardActivity { | 24 public class CardboardDesktopActivity extends CardboardActivity { |
19 // Flag to indicate whether the current activity is going to switch to norma
l | 25 // Flag to indicate whether the current activity is going to switch to norma
l |
20 // desktop activity. | 26 // desktop activity. |
21 private boolean mSwitchToDesktopActivity; | 27 private boolean mSwitchToDesktopActivity; |
22 | 28 |
23 private CardboardDesktopRenderer mRenderer; | 29 private CardboardDesktopRenderer mRenderer; |
| 30 private SpeechRecognizer mSpeechRecognizer; |
| 31 |
| 32 // Flag to indicate whether the speech recognizer is listening or not. |
| 33 private boolean mIsListening; |
24 | 34 |
25 @Override | 35 @Override |
26 public void onCreate(Bundle savedInstanceState) { | 36 public void onCreate(Bundle savedInstanceState) { |
27 super.onCreate(savedInstanceState); | 37 super.onCreate(savedInstanceState); |
28 setContentView(R.layout.cardboard_desktop); | 38 setContentView(R.layout.cardboard_desktop); |
29 mSwitchToDesktopActivity = false; | 39 mSwitchToDesktopActivity = false; |
30 CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboar
d_view); | 40 CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboar
d_view); |
31 mRenderer = new CardboardDesktopRenderer(this); | 41 mRenderer = new CardboardDesktopRenderer(this); |
| 42 mIsListening = false; |
32 | 43 |
33 // Associate a CardboardView.StereoRenderer with cardboardView. | 44 // Associate a CardboardView.StereoRenderer with cardboardView. |
34 cardboardView.setRenderer(mRenderer); | 45 cardboardView.setRenderer(mRenderer); |
35 | 46 |
36 // Associate the cardboardView with this activity. | 47 // Associate the cardboardView with this activity. |
37 setCardboardView(cardboardView); | 48 setCardboardView(cardboardView); |
38 } | 49 } |
39 | 50 |
40 @Override | 51 @Override |
41 public void onCardboardTrigger() { | 52 public void onCardboardTrigger() { |
42 if (mRenderer.isLookingAtDesktop()) { | 53 if (mRenderer.isLookingAtDesktop()) { |
43 PointF coordinates = mRenderer.getMouseCoordinates(); | 54 PointF coordinates = mRenderer.getMouseCoordinates(); |
44 JniInterface.sendMouseEvent((int) coordinates.x, (int) coordinates.y
, | 55 JniInterface.sendMouseEvent((int) coordinates.x, (int) coordinates.y
, |
45 TouchInputHandler.BUTTON_LEFT, true); | 56 TouchInputHandler.BUTTON_LEFT, true); |
46 JniInterface.sendMouseEvent((int) coordinates.x, (int) coordinates.y
, | 57 JniInterface.sendMouseEvent((int) coordinates.x, (int) coordinates.y
, |
47 TouchInputHandler.BUTTON_LEFT, false); | 58 TouchInputHandler.BUTTON_LEFT, false); |
48 } else { | 59 } else if (mRenderer.isLookingLeftOfDesktop()) { |
| 60 // TODO(shichengfeng): Give a more polished UI (including menu icons |
| 61 // and visual indicator during when speech recognizer is listening). |
49 mSwitchToDesktopActivity = true; | 62 mSwitchToDesktopActivity = true; |
50 finish(); | 63 finish(); |
| 64 } else if (mRenderer.isLookingRightOfDesktop()) { |
| 65 listenForVoiceInput(); |
51 } | 66 } |
52 } | 67 } |
53 | 68 |
54 @Override | 69 @Override |
55 protected void onStart() { | 70 protected void onStart() { |
56 super.onStart(); | 71 super.onStart(); |
57 JniInterface.enableVideoChannel(true); | 72 JniInterface.enableVideoChannel(true); |
58 mRenderer.attachRedrawCallback(); | 73 mRenderer.attachRedrawCallback(); |
59 } | 74 } |
60 | 75 |
61 @Override | 76 @Override |
62 protected void onPause() { | 77 protected void onPause() { |
63 super.onPause(); | 78 super.onPause(); |
64 if (!mSwitchToDesktopActivity) { | 79 if (!mSwitchToDesktopActivity) { |
65 JniInterface.enableVideoChannel(false); | 80 JniInterface.enableVideoChannel(false); |
66 } | 81 } |
| 82 if (mSpeechRecognizer != null) { |
| 83 mSpeechRecognizer.stopListening(); |
| 84 } |
67 } | 85 } |
68 | 86 |
69 @Override | 87 @Override |
70 protected void onResume() { | 88 protected void onResume() { |
71 super.onResume(); | 89 super.onResume(); |
72 JniInterface.enableVideoChannel(true); | 90 JniInterface.enableVideoChannel(true); |
73 } | 91 } |
74 | 92 |
75 @Override | 93 @Override |
76 protected void onStop() { | 94 protected void onStop() { |
77 super.onStop(); | 95 super.onStop(); |
78 if (mSwitchToDesktopActivity) { | 96 if (mSwitchToDesktopActivity) { |
79 mSwitchToDesktopActivity = false; | 97 mSwitchToDesktopActivity = false; |
80 } else { | 98 } else { |
81 JniInterface.enableVideoChannel(false); | 99 JniInterface.enableVideoChannel(false); |
82 } | 100 } |
| 101 if (mSpeechRecognizer != null) { |
| 102 mSpeechRecognizer.stopListening(); |
| 103 } |
| 104 } |
| 105 |
| 106 @Override |
| 107 protected void onDestroy() { |
| 108 super.onDestroy(); |
| 109 if (mSpeechRecognizer != null) { |
| 110 mSpeechRecognizer.cancel(); |
| 111 mSpeechRecognizer.destroy(); |
| 112 } |
| 113 } |
| 114 |
| 115 private void listenForVoiceInput() { |
| 116 if (mIsListening) { |
| 117 return; |
| 118 } |
| 119 |
| 120 if (mSpeechRecognizer == null) { |
| 121 if (SpeechRecognizer.isRecognitionAvailable(this)) { |
| 122 mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this
); |
| 123 mSpeechRecognizer.setRecognitionListener(new VoiceInputRecogniti
onListener()); |
| 124 } else { |
| 125 return; |
| 126 } |
| 127 } |
| 128 |
| 129 mIsListening = true; |
| 130 |
| 131 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 132 |
| 133 // LANGUAGE_MODEL_FREE_FORM is used to improve dictation accuracy |
| 134 // for the voice keyboard. |
| 135 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 136 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
| 137 |
| 138 mSpeechRecognizer.startListening(intent); |
| 139 } |
| 140 |
| 141 private class VoiceInputRecognitionListener implements RecognitionListener { |
| 142 public void onReadyForSpeech(Bundle params) { |
| 143 } |
| 144 |
| 145 public void onBeginningOfSpeech() { |
| 146 } |
| 147 |
| 148 public void onRmsChanged(float rmsdB){ |
| 149 } |
| 150 |
| 151 public void onBufferReceived(byte[] buffer) { |
| 152 } |
| 153 |
| 154 public void onEndOfSpeech() { |
| 155 mIsListening = false; |
| 156 } |
| 157 |
| 158 public void onError(int error) { |
| 159 mIsListening = false; |
| 160 } |
| 161 |
| 162 public void onResults(Bundle results) { |
| 163 // TODO(shichengfeng): If necessary, provide a list of choices for u
ser to pick. |
| 164 ArrayList<String> data = |
| 165 results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNIT
ION); |
| 166 if (!data.isEmpty()) { |
| 167 JniInterface.sendTextEvent(data.get(0)); |
| 168 } |
| 169 } |
| 170 |
| 171 public void onPartialResults(Bundle partialResults) { |
| 172 } |
| 173 |
| 174 public void onEvent(int eventType, Bundle params) { |
| 175 } |
83 } | 176 } |
84 } | 177 } |
OLD | NEW |