1 /* 2 * Copyright (C) 2012 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.eyesfree.braille.display; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.util.SparseArray; 22 23 import java.util.HashMap; 24 25 /** 26 * An input event, originating from a braille display. 27 * 28 * An event contains a command that is a high-level representation of the 29 * key or key combination that was pressed on the display such as a 30 * navigation key or braille keyboard combination. For some commands, there is 31 * also an integer argument that contains additional information. 32 */ 33 public class BrailleInputEvent implements Parcelable { 34 35 // Movement commands. 36 37 /** Keyboard command: Used when there is no actual command. */ 38 public static final int CMD_NONE = -1; 39 40 /** Keyboard command: Navigate upwards. */ 41 public static final int CMD_NAV_LINE_PREVIOUS = 1; 42 /** Keyboard command: Navigate downwards. */ 43 public static final int CMD_NAV_LINE_NEXT = 2; 44 /** Keyboard command: Navigate left one item. */ 45 public static final int CMD_NAV_ITEM_PREVIOUS = 3; 46 /** Keyboard command: Navigate right one item. */ 47 public static final int CMD_NAV_ITEM_NEXT = 4; 48 /** Keyboard command: Navigate one display window to the left. */ 49 public static final int CMD_NAV_PAN_LEFT = 5; 50 /** Keyboard command: Navigate one display window to the right. */ 51 public static final int CMD_NAV_PAN_RIGHT = 6; 52 /** Keyboard command: Navigate to the top or beginning. */ 53 public static final int CMD_NAV_TOP = 7; 54 /** Keyboard command: Navigate to the bottom or end. */ 55 public static final int CMD_NAV_BOTTOM = 8; 56 57 // Activation commands. 58 59 /** Keyboard command: Activate the currently selected/focused item. */ 60 public static final int CMD_ACTIVATE_CURRENT = 20; 61 62 // Scrolling. 63 64 /** Keyboard command: Scroll backward. */ 65 public static final int CMD_SCROLL_BACKWARD = 30; 66 /** Keyboard command: Scroll forward. */ 67 public static final int CMD_SCROLL_FORWARD = 31; 68 69 // Selection commands. 70 71 /** Keyboard command: Set the start ot the selection. */ 72 public static final int CMD_SELECTION_START = 40; 73 /** Keyboard command: Set the end of the selection. */ 74 public static final int CMD_SELECTION_END = 41; 75 /** Keyboard command: Select all content of the current field. */ 76 public static final int CMD_SELECTION_SELECT_ALL = 42; 77 /** Keyboard command: Cut the content of the selection. */ 78 public static final int CMD_SELECTION_CUT = 43; 79 /** Keyboard command: Copy the current selection. */ 80 public static final int CMD_SELECTION_COPY = 44; 81 /** 82 * Keyboard command: Paste the content of the clipboard at the current 83 * insertion point. 84 */ 85 public static final int CMD_SELECTION_PASTE = 45; 86 87 /** 88 * Keyboard command: Primary routing key pressed, typically 89 * used to move the insertion point or click/tap on the item 90 * under the key. 91 * The argument is the zero-based position, relative to the first cell 92 * on the display, of the cell that is closed to the key that 93 * was pressed. 94 */ 95 public static final int CMD_ROUTE = 50; 96 97 // Braille keyboard input. 98 99 /** 100 * Keyboard command: A key combination was pressed on the braille 101 * keyboard. 102 * The argument contains the dots that were pressed as a bitmask. 103 */ 104 public static final int CMD_BRAILLE_KEY = 60; 105 106 // Editing keys. 107 108 /** Keyboard command: Enter key. */ 109 public static final int CMD_KEY_ENTER = 70; 110 /** Keyboard command: Delete backward. */ 111 public static final int CMD_KEY_DEL = 71; 112 /** Keyboard command: Delete forward. */ 113 public static final int CMD_KEY_FORWARD_DEL = 72; 114 115 // Glboal navigation keys. 116 117 /** Keyboard command: Back button. */ 118 public static final int CMD_GLOBAL_BACK = 90; 119 /** Keyboard command: Home button. */ 120 public static final int CMD_GLOBAL_HOME = 91; 121 /** Keyboard command: Recent apps button. */ 122 public static final int CMD_GLOBAL_RECENTS = 92; 123 /** Keyboard command: Show notificaitons. */ 124 public static final int CMD_GLOBAL_NOTIFICATIONS = 93; 125 126 // Miscelanous commands. 127 128 /** Keyboard command: Invoke keyboard help. */ 129 public static final int CMD_HELP = 100; 130 131 // Meanings of the argument to a command. 132 133 /** This command doesn't have an argument. */ 134 public static final int ARGUMENT_NONE = 0; 135 /** 136 * The lower order bits of the arguemnt to this command represent braille 137 * dots. Dot 1 is represented by the rightmost bit and so on until dot 8, 138 * which is represented by bit 7, counted from the right. 139 */ 140 public static final int ARGUMENT_DOTS = 1; 141 /** 142 * The argument represents a 0-based position on the display counted from 143 * the leftmost cell. 144 */ 145 public static final int ARGUMENT_POSITION = 2; 146 147 private static final SparseArray<String> CMD_NAMES = 148 new SparseArray<String>(); 149 private static final HashMap<String, Integer> NAMES_TO_CMDS 150 = new HashMap<String, Integer>(); 151 static { 152 CMD_NAMES.append(CMD_NAV_LINE_PREVIOUS, "CMD_NAV_LINE_PREVIOUS"); 153 CMD_NAMES.append(CMD_NAV_LINE_NEXT, "CMD_NAV_LINE_NEXT"); 154 CMD_NAMES.append(CMD_NAV_ITEM_PREVIOUS, "CMD_NAV_ITEM_PREVIOUS"); 155 CMD_NAMES.append(CMD_NAV_ITEM_NEXT, "CMD_NAV_ITEM_NEXT"); 156 CMD_NAMES.append(CMD_NAV_PAN_LEFT, "CMD_NAV_PAN_LEFT"); 157 CMD_NAMES.append(CMD_NAV_PAN_RIGHT, "CMD_NAV_PAN_RIGHT"); 158 CMD_NAMES.append(CMD_NAV_TOP, "CMD_NAV_TOP"); 159 CMD_NAMES.append(CMD_NAV_BOTTOM, "CMD_NAV_BOTTOM"); 160 CMD_NAMES.append(CMD_ACTIVATE_CURRENT, "CMD_ACTIVATE_CURRENT"); 161 CMD_NAMES.append(CMD_SCROLL_BACKWARD, "CMD_SCROLL_BACKWARD"); 162 CMD_NAMES.append(CMD_SCROLL_FORWARD, "CMD_SCROLL_FORWARD"); 163 CMD_NAMES.append(CMD_SELECTION_START, "CMD_SELECTION_START"); 164 CMD_NAMES.append(CMD_SELECTION_END, "CMD_SELECTION_END"); 165 CMD_NAMES.append(CMD_SELECTION_SELECT_ALL, "CMD_SELECTION_SELECT_ALL"); 166 CMD_NAMES.append(CMD_SELECTION_CUT, "CMD_SELECTION_CUT"); 167 CMD_NAMES.append(CMD_SELECTION_COPY, "CMD_SELECTION_COPY"); 168 CMD_NAMES.append(CMD_SELECTION_PASTE, "CMD_SELECTION_PASTE"); 169 CMD_NAMES.append(CMD_ROUTE, "CMD_ROUTE"); 170 CMD_NAMES.append(CMD_BRAILLE_KEY, "CMD_BRAILLE_KEY"); 171 CMD_NAMES.append(CMD_KEY_ENTER, "CMD_KEY_ENTER"); 172 CMD_NAMES.append(CMD_KEY_DEL, "CMD_KEY_DEL"); 173 CMD_NAMES.append(CMD_KEY_FORWARD_DEL, "CMD_KEY_FORWARD_DEL"); 174 CMD_NAMES.append(CMD_GLOBAL_BACK, "CMD_GLOBAL_BACK"); 175 CMD_NAMES.append(CMD_GLOBAL_HOME, "CMD_GLOBAL_HOME"); 176 CMD_NAMES.append(CMD_GLOBAL_RECENTS, "CMD_GLOBAL_RECENTS"); 177 CMD_NAMES.append(CMD_GLOBAL_NOTIFICATIONS, "CMD_GLOBAL_NOTIFICATIONS"); 178 CMD_NAMES.append(CMD_HELP, "CMD_HELP"); 179 for (int i = 0; i < CMD_NAMES.size(); ++i) { 180 NAMES_TO_CMDS.put(CMD_NAMES.valueAt(i), 181 CMD_NAMES.keyAt(i)); 182 } 183 } 184 185 private final int mCommand; 186 private final int mArgument; 187 private final long mEventTime; 188 189 public BrailleInputEvent(int command, int argument, long eventTime) { 190 mCommand = command; 191 mArgument = argument; 192 mEventTime = eventTime; 193 } 194 195 /** 196 * Returns the keyboard command that this event represents. 197 */ 198 public int getCommand() { 199 return mCommand; 200 } 201 202 /** 203 * Returns the command-specific argument of the event, or zero if the 204 * command doesn't have an argument. See the individual command constants 205 * for more details. 206 */ 207 public int getArgument() { 208 return mArgument; 209 } 210 211 /** 212 * Returns the approximate time when this event happened as 213 * returned by {@link android.os.SystemClock#uptimeMillis}. 214 */ 215 public long getEventTime() { 216 return mEventTime; 217 } 218 219 /** 220 * Returns a string representation of {@code command}, or the string 221 * {@code (unknown)} if the command is unknown. 222 */ 223 public static String commandToString(int command) { 224 String ret = CMD_NAMES.get(command); 225 return ret != null ? ret : "(unknown)"; 226 } 227 228 /** 229 * Returns the command corresponding to {@code commandName}, or 230 * {@link #CMD_NONE} if the name doesn't match any existing command. 231 */ 232 public static int stringToCommand(String commandName) { 233 Integer command = NAMES_TO_CMDS.get(commandName); 234 if (command == null) { 235 return CMD_NONE; 236 } 237 return command; 238 } 239 240 /** 241 * Returns the type of argument for the given {@code command}. 242 */ 243 public static int argumentType(int command) { 244 switch (command) { 245 case CMD_SELECTION_START: 246 case CMD_SELECTION_END: 247 case CMD_ROUTE: 248 return ARGUMENT_POSITION; 249 case CMD_BRAILLE_KEY: 250 return ARGUMENT_DOTS; 251 default: 252 return ARGUMENT_NONE; 253 } 254 } 255 256 @Override 257 public String toString() { 258 StringBuilder sb = new StringBuilder(); 259 sb.append("BrailleInputEvent {"); 260 sb.append("amd="); 261 sb.append(commandToString(mCommand)); 262 sb.append(", arg="); 263 sb.append(mArgument); 264 sb.append("}"); 265 return sb.toString(); 266 } 267 268 // For Parcelable support. 269 270 public static final Parcelable.Creator<BrailleInputEvent> CREATOR = 271 new Parcelable.Creator<BrailleInputEvent>() { 272 @Override 273 public BrailleInputEvent createFromParcel(Parcel in) { 274 return new BrailleInputEvent(in); 275 } 276 277 @Override 278 public BrailleInputEvent[] newArray(int size) { 279 return new BrailleInputEvent[size]; 280 } 281 }; 282 283 @Override 284 public int describeContents() { 285 return 0; 286 } 287 288 @Override 289 public void writeToParcel(Parcel out, int flags) { 290 out.writeInt(mCommand); 291 out.writeInt(mArgument); 292 out.writeLong(mEventTime); 293 } 294 295 private BrailleInputEvent(Parcel in) { 296 mCommand = in.readInt(); 297 mArgument = in.readInt(); 298 mEventTime = in.readLong(); 299 } 300 } 301