/WebAccess/src/com/ideal/webreader/CreateGestureWizardActivity.java

http://eyes-free.googlecode.com/ · Java · 140 lines · 100 code · 19 blank · 21 comment · 22 complexity · a347d734fb8725c8e52bdef5e6f68da1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.webreader;
  17. import android.app.Activity;
  18. import android.os.Bundle;
  19. import android.os.Environment;
  20. import android.speech.tts.TextToSpeech;
  21. import android.speech.tts.TextToSpeech.OnInitListener;
  22. import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
  23. import android.view.View;
  24. import android.view.MotionEvent;
  25. import android.gesture.GestureLibraries;
  26. import android.gesture.GestureOverlayView;
  27. import android.gesture.Gesture;
  28. import android.gesture.GestureLibrary;
  29. import java.io.File;
  30. import java.util.HashMap;
  31. /**
  32. * This wizard walks the user step-by-step in creating their own set of
  33. * gestures.
  34. */
  35. public class CreateGestureWizardActivity extends Activity {
  36. private static final float LENGTH_THRESHOLD = 120.0f;
  37. private Gesture mGesture;
  38. private String mCurrentGesture = "";
  39. private TextToSpeech mTts;
  40. private GestureLibrary mStore;
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. mCurrentGesture = "";
  45. final File storeFile = new File(Environment.getExternalStorageDirectory()
  46. + "/ideal-webaccess/gestures");
  47. mStore = GestureLibraries.fromFile(storeFile);
  48. GestureOverlayView overlay = new GestureOverlayView(this);
  49. overlay.addOnGestureListener(new GesturesProcessor());
  50. setContentView(overlay);
  51. mTts = new TextToSpeech(this, new OnInitListener() {
  52. @Override
  53. public void onInit(int status) {
  54. drawNextGesture();
  55. }
  56. });
  57. }
  58. public void addGesture(View v) {
  59. if (mGesture != null) {
  60. final CharSequence name = mCurrentGesture;
  61. mStore.addGesture(name.toString(), mGesture);
  62. }
  63. }
  64. public void cancelGesture(View v) {
  65. setResult(RESULT_CANCELED);
  66. finish();
  67. }
  68. private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
  69. public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
  70. mGesture = null;
  71. }
  72. public void onGesture(GestureOverlayView overlay, MotionEvent event) {
  73. }
  74. public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
  75. mGesture = overlay.getGesture();
  76. if (mGesture.getLength() < LENGTH_THRESHOLD) {
  77. overlay.clear(false);
  78. }
  79. addGesture(null);
  80. drawNextGesture();
  81. }
  82. public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
  83. }
  84. }
  85. private void drawNextGesture() {
  86. if (mCurrentGesture.equals("")) {
  87. mCurrentGesture = "next";
  88. } else if (mCurrentGesture.equals("next")) {
  89. mCurrentGesture = "previous";
  90. } else if (mCurrentGesture.equals("previous")) {
  91. mCurrentGesture = "up";
  92. } else if (mCurrentGesture.equals("up")) {
  93. mCurrentGesture = "down";
  94. } else if (mCurrentGesture.equals("down")) {
  95. mCurrentGesture = "action";
  96. } else if (mCurrentGesture.equals("action")) {
  97. mCurrentGesture = "read all";
  98. } else if (mCurrentGesture.equals("read all")) {
  99. mCurrentGesture = "switch web reader mode";
  100. } else if (mCurrentGesture.equals("switch web reader mode")) {
  101. mCurrentGesture = "add bookmark";
  102. } else if (mCurrentGesture.equals("add bookmark")) {
  103. mCurrentGesture = "get definition";
  104. } else if (mCurrentGesture.equals("get definition")) {
  105. HashMap<String, String> ttsParams = new HashMap<String, String>();
  106. // The utterance ID doesn't matter; we don't really care what was
  107. // said, just that the TTS has finished speaking.
  108. ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");
  109. mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
  110. @Override
  111. public void onUtteranceCompleted(String utteranceId) {
  112. mStore.save();
  113. finish();
  114. }
  115. });
  116. mTts.speak("All gestures have been defined.", 2, ttsParams);
  117. return;
  118. }
  119. mTts.speak("Draw the gesture for " + mCurrentGesture, 2, null);
  120. }
  121. }