OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chromoting.cardboard; | |
6 | |
7 import android.content.Intent; | |
8 import android.graphics.PointF; | |
9 import android.os.Bundle; | |
10 import android.speech.RecognitionListener; | |
11 import android.speech.RecognizerIntent; | |
12 import android.speech.SpeechRecognizer; | |
13 | |
14 import com.google.vrtoolkit.cardboard.CardboardActivity; | |
15 import com.google.vrtoolkit.cardboard.CardboardView; | |
16 | |
17 import org.chromium.chromoting.InputStub; | |
18 import org.chromium.chromoting.R; | |
19 import org.chromium.chromoting.jni.Client; | |
20 | |
21 import java.util.ArrayList; | |
22 | |
23 /** | |
24 * Virtual desktop activity for Cardboard. | |
25 */ | |
26 public class DesktopActivity extends CardboardActivity { | |
27 // Flag to indicate whether the current activity is going to switch to norma
l | |
28 // desktop activity. | |
29 private boolean mSwitchToDesktopActivity; | |
30 | |
31 private Client mClient; | |
32 private CardboardRenderer mRenderer; | |
33 private SpeechRecognizer mSpeechRecognizer; | |
34 | |
35 // Flag to indicate whether the speech recognizer is listening or not. | |
36 private boolean mIsListening; | |
37 | |
38 @Override | |
39 public void onCreate(Bundle savedInstanceState) { | |
40 super.onCreate(savedInstanceState); | |
41 setContentView(R.layout.cardboard_desktop); | |
42 | |
43 mClient = Client.getInstance(); | |
44 | |
45 mSwitchToDesktopActivity = false; | |
46 CardboardView cardboardView = (CardboardView) findViewById(R.id.cardboar
d_view); | |
47 | |
48 // THE CODE BELOW IS BROKEN. | |
49 // To make it work, you have to somehow get the reference to the Display
object and pass | |
50 // it into the constructor. | |
51 mRenderer = new CardboardRenderer(this, mClient, null); | |
52 mIsListening = false; | |
53 | |
54 // Associate a CardboardView.StereoRenderer with cardboard view. | |
55 cardboardView.setRenderer(mRenderer); | |
56 | |
57 // Associate the cardboard view with this activity. | |
58 setCardboardView(cardboardView); | |
59 } | |
60 | |
61 @Override | |
62 public void onCardboardTrigger() { | |
63 if (mRenderer.isMenuBarVisible()) { | |
64 if (mRenderer.isLookingAtMenuBar()) { | |
65 switch (mRenderer.getMenuItem().getType()) { | |
66 case BACK: | |
67 mSwitchToDesktopActivity = true; | |
68 finish(); | |
69 break; | |
70 case VOICE_INPUT: | |
71 listenForVoiceInput(); | |
72 break; | |
73 case ZOOM_IN: | |
74 mRenderer.moveTowardsDesktop(); | |
75 break; | |
76 case ZOOM_OUT: | |
77 mRenderer.moveAwayFromDesktop(); | |
78 break; | |
79 } | |
80 } else { | |
81 mRenderer.setMenuBarVisible(false); | |
82 } | |
83 } else { | |
84 if (mRenderer.isLookingAtDesktop()) { | |
85 PointF coordinates = mRenderer.getMouseCoordinates(); | |
86 mClient.sendMouseEvent((int) coordinates.x, (int) coordinates.y, | |
87 InputStub.BUTTON_LEFT, true); | |
88 mClient.sendMouseEvent((int) coordinates.x, (int) coordinates.y, | |
89 InputStub.BUTTON_LEFT, false); | |
90 } else { | |
91 if (mRenderer.isLookingFarawayFromDesktop()) { | |
92 getCardboardView().resetHeadTracker(); | |
93 } else { | |
94 mRenderer.setMenuBarVisible(true); | |
95 } | |
96 } | |
97 } | |
98 } | |
99 | |
100 @Override | |
101 protected void onStart() { | |
102 super.onStart(); | |
103 mClient.enableVideoChannel(true); | |
104 } | |
105 | |
106 @Override | |
107 protected void onPause() { | |
108 super.onPause(); | |
109 if (!mSwitchToDesktopActivity) { | |
110 mClient.enableVideoChannel(false); | |
111 } | |
112 if (mSpeechRecognizer != null) { | |
113 mSpeechRecognizer.stopListening(); | |
114 } | |
115 } | |
116 | |
117 @Override | |
118 protected void onResume() { | |
119 super.onResume(); | |
120 mClient.enableVideoChannel(true); | |
121 } | |
122 | |
123 @Override | |
124 protected void onStop() { | |
125 super.onStop(); | |
126 if (mSwitchToDesktopActivity) { | |
127 mSwitchToDesktopActivity = false; | |
128 } else { | |
129 mClient.enableVideoChannel(false); | |
130 } | |
131 if (mSpeechRecognizer != null) { | |
132 mSpeechRecognizer.stopListening(); | |
133 } | |
134 } | |
135 | |
136 @Override | |
137 protected void onDestroy() { | |
138 super.onDestroy(); | |
139 if (mSpeechRecognizer != null) { | |
140 mSpeechRecognizer.cancel(); | |
141 mSpeechRecognizer.destroy(); | |
142 } | |
143 } | |
144 | |
145 private void listenForVoiceInput() { | |
146 if (mIsListening) { | |
147 return; | |
148 } | |
149 | |
150 if (mSpeechRecognizer == null) { | |
151 if (SpeechRecognizer.isRecognitionAvailable(this)) { | |
152 mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this
); | |
153 mSpeechRecognizer.setRecognitionListener(new VoiceInputRecogniti
onListener()); | |
154 } else { | |
155 return; | |
156 } | |
157 } | |
158 | |
159 mIsListening = true; | |
160 | |
161 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | |
162 | |
163 // LANGUAGE_MODEL_FREE_FORM is used to improve dictation accuracy | |
164 // for the voice keyboard. | |
165 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, | |
166 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); | |
167 | |
168 mSpeechRecognizer.startListening(intent); | |
169 } | |
170 | |
171 private class VoiceInputRecognitionListener implements RecognitionListener { | |
172 public void onReadyForSpeech(Bundle params) { | |
173 } | |
174 | |
175 public void onBeginningOfSpeech() { | |
176 } | |
177 | |
178 public void onRmsChanged(float rmsdB){ | |
179 } | |
180 | |
181 public void onBufferReceived(byte[] buffer) { | |
182 } | |
183 | |
184 public void onEndOfSpeech() { | |
185 mIsListening = false; | |
186 } | |
187 | |
188 public void onError(int error) { | |
189 mIsListening = false; | |
190 } | |
191 | |
192 public void onResults(Bundle results) { | |
193 // TODO(shichengfeng): If necessary, provide a list of choices for u
ser to pick. | |
194 ArrayList<String> data = | |
195 results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNIT
ION); | |
196 if (!data.isEmpty()) { | |
197 mClient.sendTextEvent(data.get(0)); | |
198 } | |
199 } | |
200 | |
201 public void onPartialResults(Bundle partialResults) { | |
202 } | |
203 | |
204 public void onEvent(int eventType, Bundle params) { | |
205 } | |
206 } | |
207 } | |
OLD | NEW |