Chromium Code Reviews| 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 | |
|
Lambros
2015/08/05 23:59:37
nit: space after "UI"
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 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() && !mIsListening) { | |
|
Lambros
2015/08/05 23:59:37
Maybe move the !listening check inside sendVoiceIn
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 65 mIsListening = true; | |
|
Lambros
2015/08/05 23:59:37
Move this inside sendVoiceInput() ?
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 66 sendVoiceInput(); | |
| 51 } | 67 } |
| 52 } | 68 } |
| 53 | 69 |
| 54 @Override | 70 @Override |
| 55 protected void onStart() { | 71 protected void onStart() { |
| 56 super.onStart(); | 72 super.onStart(); |
| 57 JniInterface.enableVideoChannel(true); | 73 JniInterface.enableVideoChannel(true); |
| 58 mRenderer.attachRedrawCallback(); | 74 mRenderer.attachRedrawCallback(); |
| 59 } | 75 } |
| 60 | 76 |
| 61 @Override | 77 @Override |
| 62 protected void onPause() { | 78 protected void onPause() { |
| 63 super.onPause(); | 79 super.onPause(); |
| 64 if (!mSwitchToDesktopActivity) { | 80 if (!mSwitchToDesktopActivity) { |
| 65 JniInterface.enableVideoChannel(false); | 81 JniInterface.enableVideoChannel(false); |
| 66 } | 82 } |
| 83 if (mSpeechRecognizer != null) { | |
| 84 mSpeechRecognizer.stopListening(); | |
| 85 } | |
| 67 } | 86 } |
| 68 | 87 |
| 69 @Override | 88 @Override |
| 70 protected void onResume() { | 89 protected void onResume() { |
| 71 super.onResume(); | 90 super.onResume(); |
| 72 JniInterface.enableVideoChannel(true); | 91 JniInterface.enableVideoChannel(true); |
| 73 } | 92 } |
| 74 | 93 |
| 75 @Override | 94 @Override |
| 76 protected void onStop() { | 95 protected void onStop() { |
| 77 super.onStop(); | 96 super.onStop(); |
| 78 if (mSwitchToDesktopActivity) { | 97 if (mSwitchToDesktopActivity) { |
| 79 mSwitchToDesktopActivity = false; | 98 mSwitchToDesktopActivity = false; |
| 80 } else { | 99 } else { |
| 81 JniInterface.enableVideoChannel(false); | 100 JniInterface.enableVideoChannel(false); |
| 82 } | 101 } |
| 102 if (mSpeechRecognizer != null) { | |
| 103 mSpeechRecognizer.stopListening(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 @Override | |
| 108 protected void onDestroy() { | |
| 109 super.onDestroy(); | |
| 110 if (mSpeechRecognizer != null) { | |
| 111 mSpeechRecognizer.cancel(); | |
| 112 mSpeechRecognizer.destroy(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 private void sendVoiceInput() { | |
|
Lambros
2015/08/05 23:59:37
This doesn't really "send" anything, it just start
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 117 if (mSpeechRecognizer == null) { | |
| 118 mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this); | |
|
Lambros
2015/08/05 23:59:37
Check to see if the service is available before cr
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 119 mSpeechRecognizer.setRecognitionListener(new VoiceInputRecognitionLi stener()); | |
| 120 } | |
| 121 | |
| 122 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); | |
| 123 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, | |
| 124 RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); | |
| 125 | |
| 126 mSpeechRecognizer.startListening(intent); | |
| 127 } | |
| 128 | |
| 129 private class VoiceInputRecognitionListener implements RecognitionListener { | |
| 130 public void onReadyForSpeech(Bundle params) { | |
| 131 } | |
| 132 | |
| 133 public void onBeginningOfSpeech() { | |
| 134 } | |
| 135 | |
| 136 public void onRmsChanged(float rmsdB){ | |
| 137 } | |
| 138 | |
| 139 public void onBufferReceived(byte[] buffer) { | |
| 140 } | |
| 141 | |
| 142 public void onEndOfSpeech() { | |
|
Lambros
2015/08/05 23:59:37
Set mIsListening = false here? The recognizer stop
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 143 } | |
| 144 | |
| 145 public void onError(int error) { | |
| 146 } | |
| 147 | |
| 148 public void onResults(Bundle results) { | |
| 149 // TODO(shichengfeng): If necessary, provide a list of choices for u ser to pick. | |
| 150 ArrayList<String> data = | |
| 151 results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNIT ION); | |
| 152 if (!data.isEmpty()) { | |
| 153 JniInterface.sendTextEvent(data.get(0)); | |
| 154 } | |
| 155 mIsListening = false; | |
|
Lambros
2015/08/05 23:59:37
I wonder if this belongs in onEndOfSpeech() ?
shichengfeng
2015/08/06 19:13:41
Done.
| |
| 156 } | |
| 157 | |
| 158 public void onPartialResults(Bundle partialResults) { | |
| 159 } | |
| 160 | |
| 161 public void onEvent(int eventType, Bundle params) { | |
| 162 } | |
| 83 } | 163 } |
| 84 } | 164 } |
| OLD | NEW |