FileDocCategorySizeDatePackage
GestureStore.javaAPI DocAndroid 5.1 API10536Thu Mar 12 22:22:10 GMT 2015android.gesture

GestureStore

public class GestureStore extends Object
GestureLibrary maintains gesture examples and makes predictions on a new gesture

Fields Summary
public static final int
SEQUENCE_INVARIANT
public static final int
SEQUENCE_SENSITIVE
public static final int
ORIENTATION_INVARIANT
public static final int
ORIENTATION_SENSITIVE
static final int
ORIENTATION_SENSITIVE_4
static final int
ORIENTATION_SENSITIVE_8
private static final short
FILE_FORMAT_VERSION
private static final boolean
PROFILE_LOADING_SAVING
private int
mSequenceType
private int
mOrientationStyle
private final HashMap
mNamedGestures
private Learner
mClassifier
private boolean
mChanged
Constructors Summary
public GestureStore()


      
        mClassifier = new InstanceLearner();
    
Methods Summary
public voidaddGesture(java.lang.String entryName, Gesture gesture)
Add a gesture for the entry

param
entryName entry name
param
gesture

        if (entryName == null || entryName.length() == 0) {
            return;
        }
        ArrayList<Gesture> gestures = mNamedGestures.get(entryName);
        if (gestures == null) {
            gestures = new ArrayList<Gesture>();
            mNamedGestures.put(entryName, gestures);
        }
        gestures.add(gesture);
        mClassifier.addInstance(
                Instance.createInstance(mSequenceType, mOrientationStyle, gesture, entryName));
        mChanged = true;
    
public java.util.SetgetGestureEntries()
Get all the gesture entry names in the library

return
a set of strings

        return mNamedGestures.keySet();
    
public java.util.ArrayListgetGestures(java.lang.String entryName)
Get all the gestures of an entry

param
entryName
return
the list of gestures that is under this name

        ArrayList<Gesture> gestures = mNamedGestures.get(entryName);
        if (gestures != null) {
            return new ArrayList<Gesture>(gestures);
        } else {
            return null;
        }
    
LearnergetLearner()

        return mClassifier;
    
public intgetOrientationStyle()

        return mOrientationStyle;
    
public intgetSequenceType()

return
SEQUENCE_INVARIANT or SEQUENCE_SENSITIVE

        return mSequenceType;
    
public booleanhasChanged()

        return mChanged;
    
public voidload(java.io.InputStream stream)
Load the gesture library

        load(stream, false);
    
public voidload(java.io.InputStream stream, boolean closeStream)

        DataInputStream in = null;
        try {
            in = new DataInputStream((stream instanceof BufferedInputStream) ? stream :
                    new BufferedInputStream(stream, GestureConstants.IO_BUFFER_SIZE));

            long start;
            if (PROFILE_LOADING_SAVING) {
                start = SystemClock.elapsedRealtime();
            }

            // Read file format version number
            final short versionNumber = in.readShort();
            switch (versionNumber) {
                case 1:
                    readFormatV1(in);
                    break;
            }

            if (PROFILE_LOADING_SAVING) {
                long end = SystemClock.elapsedRealtime();
                Log.d(LOG_TAG, "Loading gestures library = " + (end - start) + " ms");
            }
        } finally {
            if (closeStream) GestureUtils.closeStream(in);
        }
    
private voidreadFormatV1(java.io.DataInputStream in)

        final Learner classifier = mClassifier;
        final HashMap<String, ArrayList<Gesture>> namedGestures = mNamedGestures;
        namedGestures.clear();

        // Number of entries in the library
        final int entriesCount = in.readInt();

        for (int i = 0; i < entriesCount; i++) {
            // Entry name
            final String name = in.readUTF();
            // Number of gestures
            final int gestureCount = in.readInt();

            final ArrayList<Gesture> gestures = new ArrayList<Gesture>(gestureCount);
            for (int j = 0; j < gestureCount; j++) {
                final Gesture gesture = Gesture.deserialize(in);
                gestures.add(gesture);
                classifier.addInstance(
                        Instance.createInstance(mSequenceType, mOrientationStyle, gesture, name));
            }

            namedGestures.put(name, gestures);
        }
    
public java.util.ArrayListrecognize(Gesture gesture)
Recognize a gesture

param
gesture the query
return
a list of predictions of possible entries for a given gesture

        Instance instance = Instance.createInstance(mSequenceType,
                mOrientationStyle, gesture, null);
        return mClassifier.classify(mSequenceType, mOrientationStyle, instance.vector);
    
public voidremoveEntry(java.lang.String entryName)
Remove a entry of gestures

param
entryName the entry name

        mNamedGestures.remove(entryName);
        mClassifier.removeInstances(entryName);
        mChanged = true;
    
public voidremoveGesture(java.lang.String entryName, Gesture gesture)
Remove a gesture from the library. If there are no more gestures for the given entry, the gesture entry will be removed.

param
entryName entry name
param
gesture

        ArrayList<Gesture> gestures = mNamedGestures.get(entryName);
        if (gestures == null) {
            return;
        }

        gestures.remove(gesture);

        // if there are no more samples, remove the entry automatically
        if (gestures.isEmpty()) {
            mNamedGestures.remove(entryName);
        }

        mClassifier.removeInstance(gesture.getID());

        mChanged = true;
    
public voidsave(java.io.OutputStream stream)
Save the gesture library

        save(stream, false);
    
public voidsave(java.io.OutputStream stream, boolean closeStream)

        DataOutputStream out = null;

        try {
            long start;
            if (PROFILE_LOADING_SAVING) {
                start = SystemClock.elapsedRealtime();
            }

            final HashMap<String, ArrayList<Gesture>> maps = mNamedGestures;

            out = new DataOutputStream((stream instanceof BufferedOutputStream) ? stream :
                    new BufferedOutputStream(stream, GestureConstants.IO_BUFFER_SIZE));
            // Write version number
            out.writeShort(FILE_FORMAT_VERSION);
            // Write number of entries
            out.writeInt(maps.size());

            for (Map.Entry<String, ArrayList<Gesture>> entry : maps.entrySet()) {
                final String key = entry.getKey();
                final ArrayList<Gesture> examples = entry.getValue();
                final int count = examples.size();

                // Write entry name
                out.writeUTF(key);
                // Write number of examples for this entry
                out.writeInt(count);

                for (int i = 0; i < count; i++) {
                    examples.get(i).serialize(out);
                }
            }

            out.flush();

            if (PROFILE_LOADING_SAVING) {
                long end = SystemClock.elapsedRealtime();
                Log.d(LOG_TAG, "Saving gestures library = " + (end - start) + " ms");
            }

            mChanged = false;
        } finally {
            if (closeStream) GestureUtils.closeStream(out);
        }
    
public voidsetOrientationStyle(int style)
Specify how the gesture library will handle orientation. Use ORIENTATION_INVARIANT or ORIENTATION_SENSITIVE

param
style

        mOrientationStyle = style;
    
public voidsetSequenceType(int type)

param
type SEQUENCE_INVARIANT or SEQUENCE_SENSITIVE

        mSequenceType = type;