1 /* 2 * Copyright (C) 2023 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.internal.vibrator.persistence; 18 19 import static com.android.internal.util.Preconditions.checkArgument; 20 21 import static java.util.Objects.requireNonNull; 22 23 import android.annotation.NonNull; 24 import android.os.VibrationEffect; 25 26 import com.android.modules.utils.TypedXmlSerializer; 27 28 import java.io.IOException; 29 import java.util.Arrays; 30 31 /** 32 * Serialized representation of a {@link VibrationEffect}. 33 * 34 * <p>The vibration is represented by a list of serialized segments that can be added to a 35 * {@link VibrationEffect.Composition} during the {@link #deserialize()} procedure. 36 * 37 * @hide 38 */ 39 final class SerializedVibrationEffect implements XmlSerializedVibration<VibrationEffect> { 40 41 @NonNull 42 private final SerializedSegment[] mSegments; 43 SerializedVibrationEffect(@onNull SerializedSegment segment)44 SerializedVibrationEffect(@NonNull SerializedSegment segment) { 45 requireNonNull(segment); 46 mSegments = new SerializedSegment[]{ segment }; 47 } 48 SerializedVibrationEffect(@onNull SerializedSegment[] segments)49 SerializedVibrationEffect(@NonNull SerializedSegment[] segments) { 50 requireNonNull(segments); 51 checkArgument(segments.length > 0, "Unsupported empty vibration"); 52 mSegments = segments; 53 } 54 55 @NonNull 56 @Override deserialize()57 public VibrationEffect deserialize() { 58 VibrationEffect.Composition composition = VibrationEffect.startComposition(); 59 for (SerializedSegment segment : mSegments) { 60 segment.deserializeIntoComposition(composition); 61 } 62 return composition.compose(); 63 } 64 65 @Override write(@onNull TypedXmlSerializer serializer)66 public void write(@NonNull TypedXmlSerializer serializer) 67 throws IOException { 68 serializer.startTag(XmlConstants.NAMESPACE, XmlConstants.TAG_VIBRATION_EFFECT); 69 writeContent(serializer); 70 serializer.endTag(XmlConstants.NAMESPACE, XmlConstants.TAG_VIBRATION_EFFECT); 71 } 72 73 @Override writeContent(@onNull TypedXmlSerializer serializer)74 public void writeContent(@NonNull TypedXmlSerializer serializer) throws IOException { 75 for (SerializedSegment segment : mSegments) { 76 segment.write(serializer); 77 } 78 } 79 80 @Override toString()81 public String toString() { 82 return "SerializedVibrationEffect{" 83 + "segments=" + Arrays.toString(mSegments) 84 + '}'; 85 } 86 87 /** 88 * Serialized representation of a generic part of a {@link VibrationEffect}. 89 * 90 * <p>This can represent a single {@link android.os.vibrator.VibrationEffectSegment} (e.g. a 91 * single primitive or predefined effect) or a more complex effect, like a repeating 92 * amplitude-step waveform. 93 * 94 * @see XmlSerializedVibration 95 */ 96 interface SerializedSegment { 97 98 /** Writes this segment into a {@link TypedXmlSerializer}. */ write(@onNull TypedXmlSerializer serializer)99 void write(@NonNull TypedXmlSerializer serializer) throws IOException; 100 101 /** Adds this segment into a {@link VibrationEffect.Composition}. */ deserializeIntoComposition(@onNull VibrationEffect.Composition composition)102 void deserializeIntoComposition(@NonNull VibrationEffect.Composition composition); 103 } 104 } 105