diff --git a/core/api/test-current.txt b/core/api/test-current.txt index d25c3f5bf5381..9d74b363ad461 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -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 diff --git a/core/java/android/view/inputmethod/HandwritingGesture.java b/core/java/android/view/inputmethod/HandwritingGesture.java index e0fc426e14679..1f4a7af433039 100644 --- a/core/java/android/view/inputmethod/HandwritingGesture.java +++ b/core/java/android/view/inputmethod/HandwritingGesture.java @@ -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(); + } + } + } }