1 /* 2 * Copyright (C) 2022 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 17 package android.view.inputmethod; 18 19 import android.annotation.NonNull; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import java.util.Objects; 24 25 /** 26 * A generic container of parcelable {@link HandwritingGesture}. 27 * 28 * @hide 29 */ 30 public final class ParcelableHandwritingGesture implements Parcelable { 31 @NonNull 32 private final HandwritingGesture mGesture; 33 @NonNull 34 private final Parcelable mGestureAsParcelable; 35 ParcelableHandwritingGesture(@onNull HandwritingGesture gesture)36 private ParcelableHandwritingGesture(@NonNull HandwritingGesture gesture) { 37 mGesture = gesture; 38 // For fail-fast. 39 mGestureAsParcelable = (Parcelable) gesture; 40 } 41 42 /** 43 * Creates {@link ParcelableHandwritingGesture} from {@link HandwritingGesture}, which also 44 * implements {@link Parcelable}. 45 * 46 * @param gesture {@link HandwritingGesture} object to be stored. 47 * @return {@link ParcelableHandwritingGesture} to be stored in {@link Parcel}. 48 */ 49 @NonNull of(@onNull HandwritingGesture gesture)50 public static ParcelableHandwritingGesture of(@NonNull HandwritingGesture gesture) { 51 return new ParcelableHandwritingGesture(Objects.requireNonNull(gesture)); 52 } 53 54 /** 55 * @return {@link HandwritingGesture} object stored in this container. 56 */ 57 @NonNull get()58 public HandwritingGesture get() { 59 return mGesture; 60 } 61 createFromParcelInternal( @andwritingGesture.GestureType int gestureType, @NonNull Parcel parcel)62 private static HandwritingGesture createFromParcelInternal( 63 @HandwritingGesture.GestureType int gestureType, @NonNull Parcel parcel) { 64 switch (gestureType) { 65 case HandwritingGesture.GESTURE_TYPE_NONE: 66 throw new UnsupportedOperationException("GESTURE_TYPE_NONE is not supported"); 67 case HandwritingGesture.GESTURE_TYPE_SELECT: 68 return SelectGesture.CREATOR.createFromParcel(parcel); 69 case HandwritingGesture.GESTURE_TYPE_SELECT_RANGE: 70 return SelectRangeGesture.CREATOR.createFromParcel(parcel); 71 case HandwritingGesture.GESTURE_TYPE_INSERT: 72 return InsertGesture.CREATOR.createFromParcel(parcel); 73 case HandwritingGesture.GESTURE_TYPE_INSERT_MODE: 74 return InsertModeGesture.CREATOR.createFromParcel(parcel); 75 case HandwritingGesture.GESTURE_TYPE_DELETE: 76 return DeleteGesture.CREATOR.createFromParcel(parcel); 77 case HandwritingGesture.GESTURE_TYPE_DELETE_RANGE: 78 return DeleteRangeGesture.CREATOR.createFromParcel(parcel); 79 case HandwritingGesture.GESTURE_TYPE_JOIN_OR_SPLIT: 80 return JoinOrSplitGesture.CREATOR.createFromParcel(parcel); 81 case HandwritingGesture.GESTURE_TYPE_REMOVE_SPACE: 82 return RemoveSpaceGesture.CREATOR.createFromParcel(parcel); 83 default: 84 throw new UnsupportedOperationException("Unknown type=" + gestureType); 85 } 86 } 87 88 public static final Creator<ParcelableHandwritingGesture> CREATOR = new Parcelable.Creator<>() { 89 @Override 90 public ParcelableHandwritingGesture createFromParcel(Parcel in) { 91 final int gestureType = in.readInt(); 92 return new ParcelableHandwritingGesture(createFromParcelInternal(gestureType, in)); 93 } 94 95 @Override 96 public ParcelableHandwritingGesture[] newArray(int size) { 97 return new ParcelableHandwritingGesture[size]; 98 } 99 }; 100 101 @Override describeContents()102 public int describeContents() { 103 return mGestureAsParcelable.describeContents(); 104 } 105 106 @Override writeToParcel(@onNull Parcel dest, int flags)107 public void writeToParcel(@NonNull Parcel dest, int flags) { 108 dest.writeInt(mGesture.getGestureType()); 109 mGestureAsParcelable.writeToParcel(dest, flags); 110 } 111 } 112