1 /* 2 * Copyright (C) 2021 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.os.vibrator; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.TestApi; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.os.VibrationEffect; 25 26 import com.android.internal.util.Preconditions; 27 28 import java.util.Objects; 29 30 /** 31 * Representation of {@link VibrationEffectSegment} that plays a primitive vibration effect after a 32 * specified delay and applying a given scale. 33 * 34 * @hide 35 */ 36 @TestApi 37 public final class PrimitiveSegment extends VibrationEffectSegment { 38 private final int mPrimitiveId; 39 private final float mScale; 40 private final int mDelay; 41 PrimitiveSegment(@onNull Parcel in)42 PrimitiveSegment(@NonNull Parcel in) { 43 this(in.readInt(), in.readFloat(), in.readInt()); 44 } 45 46 /** @hide */ PrimitiveSegment(int id, float scale, int delay)47 public PrimitiveSegment(int id, float scale, int delay) { 48 mPrimitiveId = id; 49 mScale = scale; 50 mDelay = delay; 51 } 52 getPrimitiveId()53 public int getPrimitiveId() { 54 return mPrimitiveId; 55 } 56 getScale()57 public float getScale() { 58 return mScale; 59 } 60 getDelay()61 public int getDelay() { 62 return mDelay; 63 } 64 65 @Override getDuration()66 public long getDuration() { 67 return -1; 68 } 69 70 /** @hide */ 71 @Override isHapticFeedbackCandidate()72 public boolean isHapticFeedbackCandidate() { 73 return true; 74 } 75 76 /** @hide */ 77 @Override hasNonZeroAmplitude()78 public boolean hasNonZeroAmplitude() { 79 // Every primitive plays a vibration with a non-zero amplitude, even at scale == 0. 80 return true; 81 } 82 83 /** @hide */ 84 @NonNull 85 @Override resolve(int defaultAmplitude)86 public PrimitiveSegment resolve(int defaultAmplitude) { 87 return this; 88 } 89 90 /** @hide */ 91 @NonNull 92 @Override scale(float scaleFactor)93 public PrimitiveSegment scale(float scaleFactor) { 94 return new PrimitiveSegment(mPrimitiveId, VibrationEffect.scale(mScale, scaleFactor), 95 mDelay); 96 } 97 98 /** @hide */ 99 @NonNull 100 @Override applyEffectStrength(int effectStrength)101 public PrimitiveSegment applyEffectStrength(int effectStrength) { 102 return this; 103 } 104 105 /** @hide */ 106 @Override validate()107 public void validate() { 108 Preconditions.checkArgumentInRange(mPrimitiveId, VibrationEffect.Composition.PRIMITIVE_NOOP, 109 VibrationEffect.Composition.PRIMITIVE_LOW_TICK, "primitiveId"); 110 Preconditions.checkArgumentInRange(mScale, 0f, 1f, "scale"); 111 VibrationEffectSegment.checkDurationArgument(mDelay, "delay"); 112 } 113 114 @Override writeToParcel(@onNull Parcel dest, int flags)115 public void writeToParcel(@NonNull Parcel dest, int flags) { 116 dest.writeInt(PARCEL_TOKEN_PRIMITIVE); 117 dest.writeInt(mPrimitiveId); 118 dest.writeFloat(mScale); 119 dest.writeInt(mDelay); 120 } 121 122 @Override describeContents()123 public int describeContents() { 124 return 0; 125 } 126 127 @Override toString()128 public String toString() { 129 return "Primitive{" 130 + "primitive=" + VibrationEffect.Composition.primitiveToString(mPrimitiveId) 131 + ", scale=" + mScale 132 + ", delay=" + mDelay 133 + '}'; 134 } 135 136 @Override equals(@ullable Object o)137 public boolean equals(@Nullable Object o) { 138 if (this == o) return true; 139 if (o == null || getClass() != o.getClass()) return false; 140 PrimitiveSegment that = (PrimitiveSegment) o; 141 return mPrimitiveId == that.mPrimitiveId 142 && Float.compare(that.mScale, mScale) == 0 143 && mDelay == that.mDelay; 144 } 145 146 @Override hashCode()147 public int hashCode() { 148 return Objects.hash(mPrimitiveId, mScale, mDelay); 149 } 150 151 @NonNull 152 public static final Parcelable.Creator<PrimitiveSegment> CREATOR = 153 new Parcelable.Creator<PrimitiveSegment>() { 154 @Override 155 public PrimitiveSegment createFromParcel(Parcel in) { 156 // Skip the type token 157 in.readInt(); 158 return new PrimitiveSegment(in); 159 } 160 161 @Override 162 public PrimitiveSegment[] newArray(int size) { 163 return new PrimitiveSegment[size]; 164 } 165 }; 166 } 167