PageRenderTime 41ms CodeModel.GetById 11ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/brailler/src/com/google/marvin/brailler/BraillerView.java

http://eyes-free.googlecode.com/
Java | 245 lines | 219 code | 15 blank | 11 comment | 191 complexity | a3291b39d0a1526181c5a40ecfbcd8b3 MD5 | raw file
  1package com.google.marvin.brailler;
  2
  3import android.content.Context;
  4import android.os.Vibrator;
  5import android.view.MotionEvent;
  6import android.view.View;
  7
  8/**
  9 * Custom View that implements the Brailler
 10 * 
 11 * @author clchen@google.com (Charles L. Chen)
 12 */
 13public class BraillerView extends View {
 14  private static final long[] PATTERN = {0, 1, 40, 41};
 15  private boolean dot1;
 16  private boolean dot2;
 17  private boolean dot3;
 18  private boolean dot4;
 19  private boolean dot5;
 20  private boolean dot6;
 21  private Brailler parent;
 22  private Vibrator vibe;
 23
 24  public BraillerView(Context context) {
 25    super(context);
 26    parent = (Brailler) context;
 27    vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
 28
 29    dot1 = false;
 30    dot2 = false;
 31    dot3 = false;
 32    dot4 = false;
 33    dot5 = false;
 34    dot6 = false;
 35
 36    setClickable(true);
 37    setFocusable(true);
 38    setFocusableInTouchMode(true);
 39    requestFocus();
 40  }
 41
 42  @Override
 43  public boolean onTouchEvent(MotionEvent event) {
 44    // Ignore any events that involve the user going off the edge of the screen
 45    int edgeFlags = event.getEdgeFlags();
 46    if ((edgeFlags == MotionEvent.EDGE_BOTTOM) || (edgeFlags == MotionEvent.EDGE_LEFT)
 47        || (edgeFlags == MotionEvent.EDGE_RIGHT) || (edgeFlags == MotionEvent.EDGE_TOP)) {
 48      vibe.vibrate(PATTERN, -1);
 49      return true;
 50    }
 51    int action = event.getAction();
 52    switch (action) {
 53      case MotionEvent.ACTION_UP:
 54        processInput();
 55        break;
 56      default:
 57        float x = event.getX();
 58        float y = event.getY();
 59        if (x < (getWidth() * .4)) {
 60          if (y < (getHeight() * .25)) {
 61            if (!dot1) {
 62              parent.tts.speak("[tock]", 0, null);
 63              vibe.vibrate(PATTERN, -1);
 64            }
 65            dot1 = true;
 66          } else if (y < (getHeight() * .375)) {
 67            //Nothing - alley
 68          } else if (y < (getHeight() * .625)) {
 69            if (!dot2) {
 70              parent.tts.speak("[tock]", 0, null);
 71              vibe.vibrate(PATTERN, -1);
 72            }
 73            dot2 = true;
 74          } else if (y < (getHeight() * .75)) {
 75            //Nothing - alley
 76          } else {
 77            if (!dot3) {
 78              parent.tts.speak("[tock]", 0, null);
 79              vibe.vibrate(PATTERN, -1);
 80            }
 81            dot3 = true;
 82          }
 83        } else if (x < (getWidth() * .6)) {
 84        //Nothing - alley
 85        } else {
 86          if (y < (getHeight() * .25)) {
 87            if (!dot4) {
 88              parent.tts.speak("[tock]", 0, null);
 89              vibe.vibrate(PATTERN, -1);
 90            }
 91            dot4 = true;
 92          } else if (y < (getHeight() * .375)) {
 93            //Nothing - alley
 94          } else if (y < (getHeight() * .625)) {
 95            if (!dot5) {
 96              parent.tts.speak("[tock]", 0, null);
 97              vibe.vibrate(PATTERN, -1);
 98            }
 99            dot5 = true;
100          } else if (y < (getHeight() * .75)) {
101            //Nothing - alley
102          } else {
103            if (!dot6) {
104              parent.tts.speak("[tock]", 0, null);
105              vibe.vibrate(PATTERN, -1);
106            }
107            dot6 = true;
108          }
109        }
110        break;
111    }
112    return true;
113  }
114
115
116  private void processInput() { 
117    parent.tts.speak(letterFromBraille(), 0, null);
118    dot1 = false;
119    dot2 = false;
120    dot3 = false;
121    dot4 = false;
122    dot5 = false;
123    dot6 = false;
124  }
125
126  private String letterFromBraille() {
127    if ( dot1 && !dot4 && 
128        !dot2 && !dot5 && 
129        !dot3 && !dot6 ) {
130      return "a";
131    }
132    if (dot1 && !dot4 && dot2 && !dot5 && !dot3 && !dot6) {
133      return "b";
134    }
135    if (dot1 && dot4 && !dot2 && !dot5 && !dot3 && !dot6) {
136      return "c";
137    }
138    if (dot1 && dot4 && !dot2 && dot5 && !dot3 && !dot6) {
139      return "d";
140    }
141    if (dot1 && !dot4 && !dot2 && dot5 && !dot3 && !dot6) {
142      return "e";
143    }
144    if (dot1 && dot4 && dot2 && !dot5 && !dot3 && !dot6) {
145      return "f";
146    }
147    if (dot1 && dot4 && dot2 && dot5 && !dot3 && !dot6) {
148      return "g";
149    }
150    if (dot1 && !dot4 && dot2 && dot5 && !dot3 && !dot6) {
151      return "h";
152    }
153    if (!dot1 && dot4 && dot2 && !dot5 && !dot3 && !dot6) {
154      return "i";
155    }
156    if (!dot1 && dot4 && dot2 && dot5 && !dot3 && !dot6) {
157      return "j";
158    }
159    if (dot1 && !dot4 && !dot2 && !dot5 && dot3 && !dot6) {
160      return "k";
161    }
162    
163    if ( dot1 && !dot4 && 
164         dot2 && !dot5 && 
165         dot3 && !dot6 ) {
166      return "l";
167    }
168    if ( dot1 &&  dot4 && 
169        !dot2 && !dot5 && 
170         dot3 && !dot6 ) {
171      return "m";
172    }
173    if ( dot1 &&  dot4 && 
174        !dot2 &&  dot5 && 
175         dot3 && !dot6 ) {
176      return "n";
177    }
178    if ( dot1 && !dot4 && 
179        !dot2 &&  dot5 && 
180         dot3 && !dot6 ) {
181      return "o";
182    }
183    if ( dot1 &&  dot4 && 
184         dot2 && !dot5 && 
185         dot3 && !dot6 ) {
186      return "p";
187    }    
188    if ( dot1 &&  dot4 && 
189         dot2 &&  dot5 && 
190         dot3 && !dot6 ) {
191      return "q";
192    }
193    if ( dot1 && !dot4 && 
194         dot2 &&  dot5 && 
195         dot3 && !dot6 ) {
196      return "r";
197    }
198    if (!dot1 &&  dot4 && 
199         dot2 && !dot5 && 
200         dot3 && !dot6 ) {
201      return "s";
202    }
203    if (!dot1 &&  dot4 && 
204         dot2 &&  dot5 && 
205         dot3 && !dot6 ) {
206      return "t";
207    }
208    
209    
210    
211    if ( dot1 && !dot4 && 
212        !dot2 && !dot5 && 
213         dot3 &&  dot6 ) {
214      return "u";
215    }    
216    if ( dot1 && !dot4 && 
217         dot2 && !dot5 && 
218         dot3 &&  dot6 ) {
219      return "v";
220    }   
221    if (!dot1 &&  dot4 && 
222         dot2 &&  dot5 && 
223        !dot3 &&  dot6 ) {
224      return "w";
225    }   
226    if ( dot1 &&  dot4 && 
227        !dot2 && !dot5 && 
228         dot3 &&  dot6 ) {
229      return "x";
230    }   
231    if ( dot1 &&  dot4 && 
232        !dot2 &&  dot5 && 
233         dot3 &&  dot6 ) {
234      return "y";
235    }   
236    if ( dot1 && !dot4 && 
237        !dot2 &&  dot5 && 
238         dot3 &&  dot6 ) {
239      return "z";
240    }   
241    
242    return "";
243  }
244
245}