PageRenderTime 20ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.ideal.webreader;
 18
 19import android.app.Activity;
 20import android.os.Bundle;
 21import android.os.Environment;
 22import android.speech.tts.TextToSpeech;
 23import android.speech.tts.TextToSpeech.OnInitListener;
 24import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
 25import android.view.View;
 26import android.view.MotionEvent;
 27import android.gesture.GestureLibraries;
 28import android.gesture.GestureOverlayView;
 29import android.gesture.Gesture;
 30import android.gesture.GestureLibrary;
 31import java.io.File;
 32import java.util.HashMap;
 33
 34/**
 35 * This wizard walks the user step-by-step in creating their own set of
 36 * gestures.
 37 */
 38public class CreateGestureWizardActivity extends Activity {
 39    private static final float LENGTH_THRESHOLD = 120.0f;
 40
 41    private Gesture mGesture;
 42
 43    private String mCurrentGesture = "";
 44
 45    private TextToSpeech mTts;
 46
 47    private GestureLibrary mStore;
 48
 49    @Override
 50    protected void onCreate(Bundle savedInstanceState) {
 51        super.onCreate(savedInstanceState);
 52
 53        mCurrentGesture = "";
 54        final File storeFile = new File(Environment.getExternalStorageDirectory()
 55                + "/ideal-webaccess/gestures");
 56        mStore = GestureLibraries.fromFile(storeFile);
 57
 58        GestureOverlayView overlay = new GestureOverlayView(this);
 59        overlay.addOnGestureListener(new GesturesProcessor());
 60        setContentView(overlay);
 61
 62        mTts = new TextToSpeech(this, new OnInitListener() {
 63            @Override
 64            public void onInit(int status) {
 65                drawNextGesture();
 66            }
 67        });
 68    }
 69
 70    public void addGesture(View v) {
 71        if (mGesture != null) {
 72            final CharSequence name = mCurrentGesture;
 73            mStore.addGesture(name.toString(), mGesture);
 74        }
 75    }
 76
 77    public void cancelGesture(View v) {
 78        setResult(RESULT_CANCELED);
 79        finish();
 80    }
 81
 82    private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
 83        public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
 84            mGesture = null;
 85        }
 86
 87        public void onGesture(GestureOverlayView overlay, MotionEvent event) {
 88        }
 89
 90        public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
 91            mGesture = overlay.getGesture();
 92            if (mGesture.getLength() < LENGTH_THRESHOLD) {
 93                overlay.clear(false);
 94            }
 95            addGesture(null);
 96            drawNextGesture();
 97        }
 98
 99        public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
100        }
101    }
102
103    private void drawNextGesture() {
104        if (mCurrentGesture.equals("")) {
105            mCurrentGesture = "next";
106        } else if (mCurrentGesture.equals("next")) {
107            mCurrentGesture = "previous";
108        } else if (mCurrentGesture.equals("previous")) {
109            mCurrentGesture = "up";
110        } else if (mCurrentGesture.equals("up")) {
111            mCurrentGesture = "down";
112        } else if (mCurrentGesture.equals("down")) {
113            mCurrentGesture = "action";
114        } else if (mCurrentGesture.equals("action")) {
115            mCurrentGesture = "read all";
116        } else if (mCurrentGesture.equals("read all")) {
117            mCurrentGesture = "switch web reader mode";
118        } else if (mCurrentGesture.equals("switch web reader mode")) {
119            mCurrentGesture = "add bookmark";
120        } else if (mCurrentGesture.equals("add bookmark")) {
121            mCurrentGesture = "get definition";
122        } else if (mCurrentGesture.equals("get definition")) {
123            HashMap<String, String> ttsParams = new HashMap<String, String>();
124            // The utterance ID doesn't matter; we don't really care what was
125            // said, just that the TTS has finished speaking.
126            ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "done");
127            mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
128                @Override
129                public void onUtteranceCompleted(String utteranceId) {
130                    mStore.save();
131                    finish();
132                }
133            });
134            mTts.speak("All gestures have been defined.", 2, ttsParams);
135            return;
136        }
137        mTts.speak("Draw the gesture for " + mCurrentGesture, 2, null);
138    }
139
140}