Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bcbf8231 authored by Taran Singh's avatar Taran Singh
Browse files

TestApi for easy HandwritingGesture serial/deserialization

Introduce TestApi for easier serialization and deserialization of
HandwritingGesture.

Bug: 217957587
Test: atest CtsInputMethodTestCases

Change-Id: I37b3c24bb427ca22b33e14aa2c40b21821cab4e4
parent 9513eb2d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -3321,7 +3321,9 @@ package android.view.displayhash {
package android.view.inputmethod {

  public abstract class HandwritingGesture {
    method @NonNull public static android.view.inputmethod.HandwritingGesture fromByteArray(@NonNull byte[]);
    method public final int getGestureType();
    method @NonNull public final byte[] toByteArray();
    field public static final int GESTURE_TYPE_DELETE = 4; // 0x4
    field public static final int GESTURE_TYPE_DELETE_RANGE = 64; // 0x40
    field public static final int GESTURE_TYPE_INSERT = 2; // 0x2
+55 −0
Original line number Diff line number Diff line
@@ -17,10 +17,13 @@
package android.view.inputmethod;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.graphics.RectF;
import android.inputmethodservice.InputMethodService;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.MotionEvent;

import java.lang.annotation.Retention;
@@ -198,4 +201,56 @@ public abstract class HandwritingGesture {
    public final String getFallbackText() {
        return mFallbackText;
    }

    /**
     * Dump data into a byte array so that you can pass the data across process boundary.
     *
     * @return byte array data.
     * @see #fromByteArray(byte[])
     * @hide
     */
    @TestApi
    @NonNull
    public final byte[] toByteArray() {
        if (!(this instanceof Parcelable)) {
            throw new UnsupportedOperationException(getClass() + " is not Parcelable");
        }
        final Parcelable self = (Parcelable) this;
        if ((self.describeContents() & Parcelable.CONTENTS_FILE_DESCRIPTOR) != 0) {
            throw new UnsupportedOperationException("Gesture that contains FD is not supported");
        }
        Parcel parcel = null;
        try {
            parcel = Parcel.obtain();
            ParcelableHandwritingGesture.of(this).writeToParcel(parcel, 0);
            return parcel.marshall();
        } finally {
            if (parcel != null) {
                parcel.recycle();
            }
        }
    }

    /**
     * Create a new instance from byte array obtained from {@link #toByteArray()}.
     *
     * @param buffer byte array obtained from {@link #toByteArray()}
     * @return A new instance of {@link HandwritingGesture} subclass.
     * @hide
     */
    @TestApi
    @NonNull
    public static HandwritingGesture fromByteArray(@NonNull byte[] buffer) {
        Parcel parcel = null;
        try {
            parcel = Parcel.obtain();
            parcel.unmarshall(buffer, 0, buffer.length);
            parcel.setDataPosition(0);
            return ParcelableHandwritingGesture.CREATOR.createFromParcel(parcel).get();
        } finally {
            if (parcel != null) {
                parcel.recycle();
            }
        }
    }
}