Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008-2009 The Android Open Source Project |
| 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 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame^] | 17 | package android.gesture; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 18 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 19 | import android.util.Log; |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 20 | import android.os.SystemClock; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 21 | |
| 22 | import java.io.BufferedInputStream; |
| 23 | import java.io.BufferedOutputStream; |
| 24 | import java.io.File; |
| 25 | import java.io.FileInputStream; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.io.IOException; |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 28 | import java.io.DataOutputStream; |
| 29 | import java.io.DataInputStream; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.HashMap; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 32 | import java.util.Set; |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 33 | import java.util.Map; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 34 | |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame^] | 35 | import static android.gesture.GestureConstants.LOG_TAG; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 36 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 37 | /** |
| 38 | * GestureLibrary maintains gesture examples and makes predictions on a new |
| 39 | * gesture |
| 40 | */ |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 41 | // |
| 42 | // File format for GestureLibrary: |
| 43 | // |
| 44 | // Nb. bytes Java type Description |
| 45 | // ----------------------------------- |
| 46 | // Header |
| 47 | // 2 bytes short File format version number |
| 48 | // 4 bytes int Number of entries |
| 49 | // Entry |
| 50 | // X bytes UTF String Entry name |
| 51 | // 4 bytes int Number of gestures |
| 52 | // Gesture |
| 53 | // 8 bytes long Gesture ID |
| 54 | // 4 bytes int Number of strokes |
| 55 | // Stroke |
| 56 | // 4 bytes int Number of points |
| 57 | // Point |
| 58 | // 4 bytes float X coordinate of the point |
| 59 | // 4 bytes float Y coordinate of the point |
| 60 | // 8 bytes long Time stamp |
| 61 | // |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 62 | public class GestureLibrary { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 63 | public static final int SEQUENCE_INVARIANT = 1; |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 64 | // when SEQUENCE_SENSITIVE is used, only single stroke gestures are currently allowed |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 65 | public static final int SEQUENCE_SENSITIVE = 2; |
| 66 | |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 67 | // ORIENTATION_SENSITIVE and ORIENTATION_INVARIANT are only for SEQUENCE_SENSITIVE gestures |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 68 | public static final int ORIENTATION_INVARIANT = 1; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 69 | public static final int ORIENTATION_SENSITIVE = 2; |
| 70 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 71 | private static final short FILE_FORMAT_VERSION = 1; |
| 72 | |
| 73 | private static final boolean PROFILE_LOADING_SAVING = false; |
| 74 | |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 75 | private int mSequenceType = SEQUENCE_SENSITIVE; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 76 | private int mOrientationStyle = ORIENTATION_SENSITIVE; |
| 77 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 78 | private final String mGestureFileName; |
| 79 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 80 | private final HashMap<String, ArrayList<Gesture>> mNamedGestures = |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 81 | new HashMap<String, ArrayList<Gesture>>(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 82 | |
| 83 | private Learner mClassifier; |
| 84 | |
| 85 | private boolean mChanged = false; |
| 86 | |
| 87 | /** |
| 88 | * @param path where gesture data is stored |
| 89 | */ |
| 90 | public GestureLibrary(String path) { |
| 91 | mGestureFileName = path; |
| 92 | mClassifier = new InstanceLearner(); |
| 93 | } |
| 94 | |
| 95 | /** |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 96 | * Specify how the gesture library will handle orientation. |
| 97 | * Use ORIENTATION_INVARIANT or ORIENTATION_SENSITIVE |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 98 | * |
| 99 | * @param style |
| 100 | */ |
| 101 | public void setOrientationStyle(int style) { |
| 102 | mOrientationStyle = style; |
| 103 | } |
| 104 | |
| 105 | public int getOrientationStyle() { |
| 106 | return mOrientationStyle; |
| 107 | } |
| 108 | |
Yang Li | ac6a4b8 | 2009-05-21 16:08:35 -0700 | [diff] [blame] | 109 | /** |
| 110 | * @param type SEQUENCE_INVARIANT or SEQUENCE_SENSITIVE |
| 111 | */ |
| 112 | public void setSequenceType(int type) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 113 | mSequenceType = type; |
| 114 | } |
| 115 | |
Yang Li | ac6a4b8 | 2009-05-21 16:08:35 -0700 | [diff] [blame] | 116 | /** |
| 117 | * @return SEQUENCE_INVARIANT or SEQUENCE_SENSITIVE |
| 118 | */ |
| 119 | public int getSequenceType() { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 120 | return mSequenceType; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get all the gesture entry names in the library |
| 125 | * |
| 126 | * @return a set of strings |
| 127 | */ |
| 128 | public Set<String> getGestureEntries() { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 129 | return mNamedGestures.keySet(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Recognize a gesture |
| 134 | * |
| 135 | * @param gesture the query |
| 136 | * @return a list of predictions of possible entries for a given gesture |
| 137 | */ |
| 138 | public ArrayList<Prediction> recognize(Gesture gesture) { |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 139 | Instance instance = Instance.createInstance(mSequenceType, gesture, null); |
| 140 | return mClassifier.classify(mSequenceType, instance.vector); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Add a gesture for the entry |
| 145 | * |
| 146 | * @param entryName entry name |
| 147 | * @param gesture |
| 148 | */ |
| 149 | public void addGesture(String entryName, Gesture gesture) { |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 150 | if (entryName == null || entryName.length() == 0) { |
| 151 | return; |
| 152 | } |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 153 | ArrayList<Gesture> gestures = mNamedGestures.get(entryName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 154 | if (gestures == null) { |
| 155 | gestures = new ArrayList<Gesture>(); |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 156 | mNamedGestures.put(entryName, gestures); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 157 | } |
| 158 | gestures.add(gesture); |
Yang Li | e6ea003 | 2009-05-21 14:47:59 -0700 | [diff] [blame] | 159 | mClassifier.addInstance(Instance.createInstance(mSequenceType, gesture, entryName)); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 160 | mChanged = true; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Remove a gesture from the library. If there are no more gestures for the |
| 165 | * given entry, the gesture entry will be removed. |
| 166 | * |
| 167 | * @param entryName entry name |
| 168 | * @param gesture |
| 169 | */ |
| 170 | public void removeGesture(String entryName, Gesture gesture) { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 171 | ArrayList<Gesture> gestures = mNamedGestures.get(entryName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 172 | if (gestures == null) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | gestures.remove(gesture); |
| 177 | |
| 178 | // if there are no more samples, remove the entry automatically |
| 179 | if (gestures.isEmpty()) { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 180 | mNamedGestures.remove(entryName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | mClassifier.removeInstance(gesture.getID()); |
| 184 | |
| 185 | mChanged = true; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Remove a entry of gestures |
| 190 | * |
| 191 | * @param entryName the entry name |
| 192 | */ |
Romain Guy | db567c3 | 2009-05-21 16:23:21 -0700 | [diff] [blame^] | 193 | public void removeEntry(String entryName) { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 194 | mNamedGestures.remove(entryName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 195 | mClassifier.removeInstances(entryName); |
| 196 | mChanged = true; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get all the gestures of an entry |
| 201 | * |
| 202 | * @param entryName |
| 203 | * @return the list of gestures that is under this name |
| 204 | */ |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 205 | public ArrayList<Gesture> getGestures(String entryName) { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 206 | ArrayList<Gesture> gestures = mNamedGestures.get(entryName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 207 | if (gestures != null) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 208 | return new ArrayList<Gesture>(gestures); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 209 | } else { |
| 210 | return null; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Save the gesture library |
| 216 | */ |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 217 | public boolean save() { |
| 218 | if (!mChanged) { |
| 219 | return true; |
| 220 | } |
| 221 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 222 | boolean result = false; |
| 223 | DataOutputStream out = null; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 224 | |
| 225 | try { |
| 226 | File file = new File(mGestureFileName); |
| 227 | if (!file.getParentFile().exists()) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 228 | if (!file.getParentFile().mkdirs()) { |
| 229 | return false; |
| 230 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 231 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 232 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 233 | long start; |
| 234 | if (PROFILE_LOADING_SAVING) { |
| 235 | start = SystemClock.elapsedRealtime(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 236 | } |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 237 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 238 | final HashMap<String, ArrayList<Gesture>> maps = mNamedGestures; |
| 239 | |
| 240 | out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file), |
| 241 | GestureConstants.IO_BUFFER_SIZE)); |
| 242 | // Write version number |
| 243 | out.writeShort(FILE_FORMAT_VERSION); |
| 244 | // Write number of entries |
| 245 | out.writeInt(maps.size()); |
| 246 | |
| 247 | for (Map.Entry<String, ArrayList<Gesture>> entry : maps.entrySet()) { |
| 248 | final String key = entry.getKey(); |
| 249 | final ArrayList<Gesture> examples = entry.getValue(); |
| 250 | final int count = examples.size(); |
| 251 | |
| 252 | // Write entry name |
| 253 | out.writeUTF(key); |
| 254 | // Write number of examples for this entry |
| 255 | out.writeInt(count); |
| 256 | |
| 257 | for (int i = 0; i < count; i++) { |
| 258 | examples.get(i).serialize(out); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | out.flush(); |
| 263 | |
| 264 | if (PROFILE_LOADING_SAVING) { |
| 265 | long end = SystemClock.elapsedRealtime(); |
| 266 | Log.d(LOG_TAG, "Saving gestures library = " + (end - start) + " ms"); |
| 267 | } |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 268 | |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 269 | mChanged = false; |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 270 | result = true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 271 | } catch (IOException ex) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 272 | Log.d(LOG_TAG, "Failed to save gestures:", ex); |
| 273 | } finally { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 274 | GestureUtilities.closeStream(out); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 275 | } |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 276 | |
| 277 | return result; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Load the gesture library |
| 282 | */ |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 283 | public boolean load() { |
| 284 | boolean result = false; |
| 285 | |
| 286 | final File file = new File(mGestureFileName); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 287 | if (file.exists()) { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 288 | DataInputStream in = null; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 289 | try { |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 290 | in = new DataInputStream(new BufferedInputStream( |
| 291 | new FileInputStream(mGestureFileName), GestureConstants.IO_BUFFER_SIZE)); |
| 292 | |
| 293 | long start; |
| 294 | if (PROFILE_LOADING_SAVING) { |
| 295 | start = SystemClock.elapsedRealtime(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 296 | } |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 297 | |
| 298 | // Read file format version number |
| 299 | final short versionNumber = in.readShort(); |
| 300 | switch (versionNumber) { |
| 301 | case 1: |
| 302 | readFormatV1(in); |
| 303 | break; |
| 304 | } |
| 305 | |
| 306 | if (PROFILE_LOADING_SAVING) { |
| 307 | long end = SystemClock.elapsedRealtime(); |
| 308 | Log.d(LOG_TAG, "Loading gestures library = " + (end - start) + " ms"); |
| 309 | } |
| 310 | |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 311 | result = true; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 312 | } catch (IOException ex) { |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 313 | Log.d(LOG_TAG, "Failed to load gestures:", ex); |
| 314 | } finally { |
| 315 | GestureUtilities.closeStream(in); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
Romain Guy | c534727 | 2009-05-20 10:37:13 -0700 | [diff] [blame] | 318 | |
| 319 | return result; |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 322 | private void readFormatV1(DataInputStream in) throws IOException { |
| 323 | final Learner classifier = mClassifier; |
| 324 | final HashMap<String, ArrayList<Gesture>> namedGestures = mNamedGestures; |
| 325 | namedGestures.clear(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 326 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 327 | // Number of entries in the library |
| 328 | final int entriesCount = in.readInt(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 329 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 330 | for (int i = 0; i < entriesCount; i++) { |
| 331 | // Entry name |
| 332 | final String name = in.readUTF(); |
| 333 | // Number of gestures |
| 334 | final int gestureCount = in.readInt(); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 335 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 336 | final ArrayList<Gesture> gestures = new ArrayList<Gesture>(gestureCount); |
| 337 | for (int j = 0; j < gestureCount; j++) { |
| 338 | final Gesture gesture = Gesture.deserialize(in); |
| 339 | gestures.add(gesture); |
| 340 | classifier.addInstance(Instance.createInstance(mSequenceType, gesture, name)); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 341 | } |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 342 | |
Romain Guy | b6d99b7 | 2009-05-21 15:05:50 -0700 | [diff] [blame] | 343 | namedGestures.put(name, gestures); |
Yang Li | 35aa84b | 2009-05-18 18:29:05 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | } |