| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 42 |
PrimitiveSegment(@NonNull Parcel in) { |
| 43 |
this(in.readInt(), in.readFloat(), in.readInt()); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
public PrimitiveSegment(int id, float scale, int delay) { |
| 48 |
mPrimitiveId = id; |
| 49 |
mScale = scale; |
| 50 |
mDelay = delay; |
| 51 |
} |
| 52 |
|
| 53 |
public int getPrimitiveId() { |
| 54 |
return mPrimitiveId; |
| 55 |
} |
| 56 |
|
| 57 |
public float getScale() { |
| 58 |
return mScale; |
| 59 |
} |
| 60 |
|
| 61 |
public int getDelay() { |
| 62 |
return mDelay; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public long getDuration() { |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean isHapticFeedbackCandidate() { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean hasNonZeroAmplitude() { |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
@NonNull |
| 85 |
@Override |
| 86 |
public PrimitiveSegment resolve(int defaultAmplitude) { |
| 87 |
return this; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
@NonNull |
| 92 |
@Override |
| 93 |
public PrimitiveSegment scale(float scaleFactor) { |
| 94 |
return new PrimitiveSegment(mPrimitiveId, VibrationEffect.scale(mScale, scaleFactor), |
| 95 |
mDelay); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
@NonNull |
| 100 |
@Override |
| 101 |
public PrimitiveSegment applyEffectStrength(int effectStrength) { |
| 102 |
return this; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
@Override |
| 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 |
| 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 |
| 123 |
public int describeContents() { |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public String toString() { |
| 129 |
return "Primitive{" |
| 130 |
+ "primitive=" + VibrationEffect.Composition.primitiveToString(mPrimitiveId) |
| 131 |
+ ", scale=" + mScale |
| 132 |
+ ", delay=" + mDelay |
| 133 |
+ '}'; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 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 |
| 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 |
|
| 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 |
} |