• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.BasicPwleSegment;
22 import android.os.vibrator.PwleSegment;
23 import android.os.vibrator.VibrationEffectSegment;
24 
25 import java.util.List;
26 
27 /**
28  * Validates {@link PwleSegment} and {@link BasicPwleSegment} instances to ensure they are
29  * compatible with the device's capabilities.
30  *
31  * <p>This validator performs the following checks:
32  * <ul>
33  *   <li>For {@link PwleSegment}:
34  *     <ul>
35  *       <li>Verifies that the device supports {@link IVibrator#CAP_COMPOSE_PWLE_EFFECTS_V2}.
36  *       <li>Verifies that each segment's start and end frequencies fall within the supported range.
37  *     </ul>
38  *   </li>
39  *   <li>For {@link BasicPwleSegment}:
40  *     <ul>
41  *       <li>Verifies that the device supports {@link IVibrator#CAP_COMPOSE_PWLE_EFFECTS_V2}.
42  *     </ul>
43  *   </li>
44  * </ul>
45  */
46 final class PwleSegmentsValidator implements VibrationSegmentsValidator {
47 
48     @Override
hasValidSegments(VibratorInfo info, List<VibrationEffectSegment> segments)49     public boolean hasValidSegments(VibratorInfo info, List<VibrationEffectSegment> segments) {
50 
51         boolean hasPwleCapability = info.hasCapability(IVibrator.CAP_COMPOSE_PWLE_EFFECTS_V2);
52         float minFrequency = info.getFrequencyProfile().getMinFrequencyHz();
53         float maxFrequency = info.getFrequencyProfile().getMaxFrequencyHz();
54 
55         for (VibrationEffectSegment segment : segments) {
56             if (segment instanceof BasicPwleSegment && !hasPwleCapability) {
57                 return false;
58             }
59             if (segment instanceof PwleSegment pwleSegment) {
60                 if (!hasPwleCapability || pwleSegment.getStartFrequencyHz() < minFrequency
61                         || pwleSegment.getStartFrequencyHz() > maxFrequency
62                         || pwleSegment.getEndFrequencyHz() < minFrequency
63                         || pwleSegment.getEndFrequencyHz() > maxFrequency) {
64                     return false;
65                 }
66             }
67         }
68 
69         return true;
70     }
71 }
72