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 com.android.server.vibrator;
18 
19 import android.hardware.vibrator.IVibrator;
20 import android.os.VibratorInfo;
21 import android.os.vibrator.RampSegment;
22 import android.os.vibrator.StepSegment;
23 import android.os.vibrator.VibrationEffectSegment;
24 
25 import java.util.List;
26 
27 /**
28  * Adapter that converts step segments that should be handled as PWLEs to ramp segments.
29  *
30  * <p>Each replaced step will be represented by a ramp with same start and end
31  * amplitudes/frequencies, which can then be converted to PWLE compositions.
32  *
33  * <p>The segments will not be changed if the device doesn't have
34  * {@link IVibrator#CAP_COMPOSE_PWLE_EFFECTS}.
35  */
36 final class StepToRampAdapter implements VibrationSegmentsAdapter {
37 
38     @Override
adaptToVibrator(VibratorInfo info, List<VibrationEffectSegment> segments, int repeatIndex)39     public int adaptToVibrator(VibratorInfo info, List<VibrationEffectSegment> segments,
40             int repeatIndex) {
41         if (!info.hasCapability(IVibrator.CAP_COMPOSE_PWLE_EFFECTS)) {
42             // The vibrator does not have PWLE capability, so keep the segments unchanged.
43             return repeatIndex;
44         }
45         int segmentCount = segments.size();
46         // Convert steps that require frequency control to ramps.
47         for (int i = 0; i < segmentCount; i++) {
48             VibrationEffectSegment segment = segments.get(i);
49             if (isStep(segment) && ((StepSegment) segment).getFrequencyHz() != 0) {
50                 segments.set(i, convertStepToRamp(info, (StepSegment) segment));
51             }
52         }
53         // Convert steps that are next to ramps to also become ramps, so they can be composed
54         // together in the same PWLE waveform.
55         for (int i = 0; i < segmentCount; i++) {
56             if (segments.get(i) instanceof RampSegment) {
57                 for (int j = i - 1; j >= 0 && isStep(segments.get(j)); j--) {
58                     segments.set(j, convertStepToRamp(info, (StepSegment) segments.get(j)));
59                 }
60                 for (int j = i + 1; j < segmentCount && isStep(segments.get(j)); j++) {
61                     segments.set(j, convertStepToRamp(info, (StepSegment) segments.get(j)));
62                 }
63             }
64         }
65         return repeatIndex;
66     }
67 
convertStepToRamp(VibratorInfo info, StepSegment segment)68     private static RampSegment convertStepToRamp(VibratorInfo info, StepSegment segment) {
69         float frequencyHz = fillEmptyFrequency(info, segment.getFrequencyHz());
70         return new RampSegment(segment.getAmplitude(), segment.getAmplitude(),
71                 frequencyHz, frequencyHz, (int) segment.getDuration());
72     }
73 
isStep(VibrationEffectSegment segment)74     private static boolean isStep(VibrationEffectSegment segment) {
75         return segment instanceof StepSegment;
76     }
77 
fillEmptyFrequency(VibratorInfo info, float frequencyHz)78     private static float fillEmptyFrequency(VibratorInfo info, float frequencyHz) {
79         if (Float.isNaN(info.getResonantFrequencyHz())) {
80             return frequencyHz;
81         }
82         return frequencyHz == 0 ? info.getResonantFrequencyHz() : frequencyHz;
83     }
84 }
85