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.annotation.SuppressLint; 20 import android.hardware.vibrator.IVibrator; 21 import android.os.VibratorInfo; 22 import android.os.vibrator.Flags; 23 import android.os.vibrator.PrimitiveSegment; 24 import android.os.vibrator.VibrationEffectSegment; 25 26 import java.util.List; 27 28 /** 29 * Validates primitive segments to ensure they are compatible with the device's capabilities. 30 * 31 * <p>The segments will be considered invalid if the device does not have 32 * {@link IVibrator#CAP_COMPOSE_EFFECTS} or if one of the primitives is not supported. 33 */ 34 final class PrimitiveSegmentsValidator implements VibrationSegmentsValidator { 35 36 @SuppressLint("WrongConstant") // using primitive id from validated segment 37 @Override hasValidSegments(VibratorInfo info, List<VibrationEffectSegment> segments)38 public boolean hasValidSegments(VibratorInfo info, List<VibrationEffectSegment> segments) { 39 int segmentCount = segments.size(); 40 for (int i = 0; i < segmentCount; i++) { 41 if (!(segments.get(i) instanceof PrimitiveSegment primitive)) { 42 continue; 43 } 44 if (Flags.primitiveCompositionAbsoluteDelay()) { 45 // Primitive support checks introduced by this feature 46 if (!info.isPrimitiveSupported(primitive.getPrimitiveId())) { 47 return false; 48 } 49 } else { 50 // Delay type support not available without this feature 51 if ((primitive.getDelayType() != PrimitiveSegment.DEFAULT_DELAY_TYPE)) { 52 return false; 53 } 54 } 55 } 56 57 return true; 58 } 59 } 60