• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.TestApi;
21 import android.os.Parcel;
22 import android.os.VibrationEffect;
23 
24 import com.android.internal.util.Preconditions;
25 
26 import java.util.Objects;
27 
28 /**
29  * Representation of {@link VibrationEffectSegment} that ramps vibration amplitude and/or frequency
30  * for a specified duration.
31  *
32  * @hide
33  */
34 @TestApi
35 public final class RampSegment extends VibrationEffectSegment {
36     private final float mStartAmplitude;
37     private final float mStartFrequency;
38     private final float mEndAmplitude;
39     private final float mEndFrequency;
40     private final int mDuration;
41 
RampSegment(@onNull Parcel in)42     RampSegment(@NonNull Parcel in) {
43         this(in.readFloat(), in.readFloat(), in.readFloat(), in.readFloat(), in.readInt());
44     }
45 
46     /** @hide */
RampSegment(float startAmplitude, float endAmplitude, float startFrequency, float endFrequency, int duration)47     public RampSegment(float startAmplitude, float endAmplitude, float startFrequency,
48             float endFrequency, int duration) {
49         mStartAmplitude = startAmplitude;
50         mEndAmplitude = endAmplitude;
51         mStartFrequency = startFrequency;
52         mEndFrequency = endFrequency;
53         mDuration = duration;
54     }
55 
56     @Override
equals(Object o)57     public boolean equals(Object o) {
58         if (!(o instanceof RampSegment)) {
59             return false;
60         }
61         RampSegment other = (RampSegment) o;
62         return Float.compare(mStartAmplitude, other.mStartAmplitude) == 0
63                 && Float.compare(mEndAmplitude, other.mEndAmplitude) == 0
64                 && Float.compare(mStartFrequency, other.mStartFrequency) == 0
65                 && Float.compare(mEndFrequency, other.mEndFrequency) == 0
66                 && mDuration == other.mDuration;
67     }
68 
getStartAmplitude()69     public float getStartAmplitude() {
70         return mStartAmplitude;
71     }
72 
getEndAmplitude()73     public float getEndAmplitude() {
74         return mEndAmplitude;
75     }
76 
getStartFrequency()77     public float getStartFrequency() {
78         return mStartFrequency;
79     }
80 
getEndFrequency()81     public float getEndFrequency() {
82         return mEndFrequency;
83     }
84 
85     @Override
getDuration()86     public long getDuration() {
87         return mDuration;
88     }
89 
90     @Override
hasNonZeroAmplitude()91     public boolean hasNonZeroAmplitude() {
92         return mStartAmplitude > 0 || mEndAmplitude > 0;
93     }
94 
95     @Override
validate()96     public void validate() {
97         Preconditions.checkArgumentNonnegative(mDuration,
98                 "Durations must all be >= 0, got " + mDuration);
99         Preconditions.checkArgumentInRange(mStartAmplitude, 0f, 1f, "startAmplitude");
100         Preconditions.checkArgumentInRange(mEndAmplitude, 0f, 1f, "endAmplitude");
101     }
102 
103 
104     @NonNull
105     @Override
resolve(int defaultAmplitude)106     public RampSegment resolve(int defaultAmplitude) {
107         // Default amplitude is not supported for ramping.
108         return this;
109     }
110 
111     @NonNull
112     @Override
scale(float scaleFactor)113     public RampSegment scale(float scaleFactor) {
114         float newStartAmplitude = VibrationEffect.scale(mStartAmplitude, scaleFactor);
115         float newEndAmplitude = VibrationEffect.scale(mEndAmplitude, scaleFactor);
116         if (Float.compare(mStartAmplitude, newStartAmplitude) == 0
117                 && Float.compare(mEndAmplitude, newEndAmplitude) == 0) {
118             return this;
119         }
120         return new RampSegment(newStartAmplitude, newEndAmplitude, mStartFrequency, mEndFrequency,
121                 mDuration);
122     }
123 
124     @NonNull
125     @Override
applyEffectStrength(int effectStrength)126     public RampSegment applyEffectStrength(int effectStrength) {
127         return this;
128     }
129 
130     @Override
hashCode()131     public int hashCode() {
132         return Objects.hash(mStartAmplitude, mEndAmplitude, mStartFrequency, mEndFrequency,
133                 mDuration);
134     }
135 
136     @Override
toString()137     public String toString() {
138         return "Ramp{startAmplitude=" + mStartAmplitude
139                 + ", endAmplitude=" + mEndAmplitude
140                 + ", startFrequency=" + mStartFrequency
141                 + ", endFrequency=" + mEndFrequency
142                 + ", duration=" + mDuration
143                 + "}";
144     }
145 
146     @Override
describeContents()147     public int describeContents() {
148         return 0;
149     }
150 
151     @Override
writeToParcel(@onNull Parcel out, int flags)152     public void writeToParcel(@NonNull Parcel out, int flags) {
153         out.writeInt(PARCEL_TOKEN_RAMP);
154         out.writeFloat(mStartAmplitude);
155         out.writeFloat(mEndAmplitude);
156         out.writeFloat(mStartFrequency);
157         out.writeFloat(mEndFrequency);
158         out.writeInt(mDuration);
159     }
160 
161     @NonNull
162     public static final Creator<RampSegment> CREATOR =
163             new Creator<RampSegment>() {
164                 @Override
165                 public RampSegment createFromParcel(Parcel in) {
166                     // Skip the type token
167                     in.readInt();
168                     return new RampSegment(in);
169                 }
170 
171                 @Override
172                 public RampSegment[] newArray(int size) {
173                     return new RampSegment[size];
174                 }
175             };
176 }
177