summaryrefslogtreecommitdiff
path: root/core/java/android/os/vibrator/PrebakedSegment.java
blob: 30f5a5ca86c590c818898ab7ae97ab426070fe0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os.vibrator;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.VibrationEffect;

import java.util.Objects;

/**
 * Representation of {@link VibrationEffectSegment} that plays a prebaked vibration effect.
 *
 * @hide
 */
@TestApi
public final class PrebakedSegment extends VibrationEffectSegment {
    private final int mEffectId;
    private final boolean mFallback;
    private final int mEffectStrength;

    PrebakedSegment(@NonNull Parcel in) {
        mEffectId = in.readInt();
        mFallback = in.readByte() != 0;
        mEffectStrength = in.readInt();
    }

    /** @hide */
    public PrebakedSegment(int effectId, boolean shouldFallback, int effectStrength) {
        mEffectId = effectId;
        mFallback = shouldFallback;
        mEffectStrength = effectStrength;
    }

    public int getEffectId() {
        return mEffectId;
    }

    public int getEffectStrength() {
        return mEffectStrength;
    }

    /** Return true if a fallback effect should be played if this effect is not supported. */
    public boolean shouldFallback() {
        return mFallback;
    }

    @Override
    public long getDuration() {
        return -1;
    }

    /** @hide */
    @Override
    public boolean isHapticFeedbackCandidate() {
        switch (mEffectId) {
            case VibrationEffect.EFFECT_CLICK:
            case VibrationEffect.EFFECT_DOUBLE_CLICK:
            case VibrationEffect.EFFECT_HEAVY_CLICK:
            case VibrationEffect.EFFECT_POP:
            case VibrationEffect.EFFECT_TEXTURE_TICK:
            case VibrationEffect.EFFECT_THUD:
            case VibrationEffect.EFFECT_TICK:
                return true;
            default:
                // VibrationEffect.RINGTONES are not segments that could represent a haptic feedback
                return false;
        }
    }

    /** @hide */
    @Override
    public boolean hasNonZeroAmplitude() {
        return true;
    }

    /** @hide */
    @NonNull
    @Override
    public PrebakedSegment resolve(int defaultAmplitude) {
        return this;
    }

    /** @hide */
    @NonNull
    @Override
    public PrebakedSegment scale(float scaleFactor) {
        // Prebaked effect strength cannot be scaled with this method.
        return this;
    }

    /** @hide */
    @NonNull
    @Override
    public PrebakedSegment applyEffectStrength(int effectStrength) {
        if (effectStrength != mEffectStrength && isValidEffectStrength(effectStrength)) {
            return new PrebakedSegment(mEffectId, mFallback, effectStrength);
        }
        return this;
    }

    private static boolean isValidEffectStrength(int strength) {
        switch (strength) {
            case VibrationEffect.EFFECT_STRENGTH_LIGHT:
            case VibrationEffect.EFFECT_STRENGTH_MEDIUM:
            case VibrationEffect.EFFECT_STRENGTH_STRONG:
                return true;
            default:
                return false;
        }
    }

    /** @hide */
    @Override
    public void validate() {
        switch (mEffectId) {
            case VibrationEffect.EFFECT_CLICK:
            case VibrationEffect.EFFECT_DOUBLE_CLICK:
            case VibrationEffect.EFFECT_HEAVY_CLICK:
            case VibrationEffect.EFFECT_POP:
            case VibrationEffect.EFFECT_TEXTURE_TICK:
            case VibrationEffect.EFFECT_THUD:
            case VibrationEffect.EFFECT_TICK:
                break;
            default:
                int[] ringtones = VibrationEffect.RINGTONES;
                if (mEffectId < ringtones[0] || mEffectId > ringtones[ringtones.length - 1]) {
                    throw new IllegalArgumentException(
                            "Unknown prebaked effect type (value=" + mEffectId + ")");
                }
        }
        if (!isValidEffectStrength(mEffectStrength)) {
            throw new IllegalArgumentException(
                    "Unknown prebaked effect strength (value=" + mEffectStrength + ")");
        }
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (!(o instanceof PrebakedSegment)) {
            return false;
        }
        PrebakedSegment other = (PrebakedSegment) o;
        return mEffectId == other.mEffectId
                && mFallback == other.mFallback
                && mEffectStrength == other.mEffectStrength;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mEffectId, mFallback, mEffectStrength);
    }

    @Override
    public String toString() {
        return "Prebaked{effect=" + VibrationEffect.effectIdToString(mEffectId)
                + ", strength=" + VibrationEffect.effectStrengthToString(mEffectStrength)
                + ", fallback=" + mFallback
                + "}";
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull Parcel out, int flags) {
        out.writeInt(PARCEL_TOKEN_PREBAKED);
        out.writeInt(mEffectId);
        out.writeByte((byte) (mFallback ? 1 : 0));
        out.writeInt(mEffectStrength);
    }

    @NonNull
    public static final Parcelable.Creator<PrebakedSegment> CREATOR =
            new Parcelable.Creator<PrebakedSegment>() {
                @Override
                public PrebakedSegment createFromParcel(Parcel in) {
                    // Skip the type token
                    in.readInt();
                    return new PrebakedSegment(in);
                }

                @Override
                public PrebakedSegment[] newArray(int size) {
                    return new PrebakedSegment[size];
                }
            };
}