Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5fb3fa76 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add isSupportedMimeType method to VibrationXmlParser"

parents c95f3b53 f8388011
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -2682,7 +2682,7 @@ package android.os.vibrator.persistence {
    method public static void serialize(@NonNull android.os.VibrationEffect, @NonNull java.io.Writer) throws java.io.IOException, android.os.vibrator.persistence.VibrationXmlSerializer.SerializationFailedException;
  }

  public static final class VibrationXmlSerializer.SerializationFailedException extends java.lang.IllegalStateException {
  public static final class VibrationXmlSerializer.SerializationFailedException extends java.lang.RuntimeException {
  }

}
+28 −0
Original line number Diff line number Diff line
@@ -92,6 +92,16 @@ import java.lang.annotation.RetentionPolicy;
public final class VibrationXmlParser {
    private static final String TAG = "VibrationXmlParser";

    /**
     * The MIME type for a xml holding a vibration.
     *
     * <p>This should match the type registered at android.mime.types.
     *
     * @hide
     */
    public static final String APPLICATION_VIBRATION_XML_MIME_TYPE =
            "application/vnd.android.haptics.vibration+xml";

    /**
     * Allows {@link VibrationEffect} instances created via non-public APIs to be parsed/serialized.
     *
@@ -110,6 +120,24 @@ public final class VibrationXmlParser {
    @Retention(RetentionPolicy.SOURCE)
    public @interface Flags {}

    /**
     * Returns whether this parser supports parsing files of the given MIME type.
     *
     * <p>Returns false for {@code null} value.
     *
     * <p><em>Note: MIME type matching in the Android framework is case-sensitive, unlike the formal
     * RFC definitions. As a result, you should always write these elements with lower case letters,
     * or use {@link android.content.Intent#normalizeMimeType} to ensure that they are converted to
     * lower case.</em>
     *
     * @hide
     */
    public static boolean isSupportedMimeType(@Nullable String mimeType) {
        // NOTE: prefer using MimeTypeFilter.matches() if MIME_TYPE_VIBRATION_XML becomes a filter
        // or if more than one MIME type is supported by this parser.
        return APPLICATION_VIBRATION_XML_MIME_TYPE.equals(mimeType);
    }

    /**
     * Parses XML content from given input stream into a {@link VibrationEffect}.
     *
+8 −5
Original line number Diff line number Diff line
@@ -81,12 +81,11 @@ public final class VibrationXmlSerializer {
     * Serializes a {@link VibrationEffect} to XML and writes output to given {@link Writer}.
     *
     * <p>This method will only write into the {@link Writer} if the effect can successfully
     * be represented by the XML serialization. It will return {@code false} otherwise, and not
     * write any data.
     * be represented by the XML serialization. It will throw an exception otherwise.
     *
     * @throws SerializationFailedException serialization of input effect failed, no data was
     *                                      written into given {@link Writer}
     * @throws IOException error writing to given {@link Writer}
     *                                      written into given {@link Writer}.
     * @throws IOException error writing to given {@link Writer}.
     *
     * @hide
     */
@@ -139,10 +138,14 @@ public final class VibrationXmlSerializer {
    /**
     * Exception thrown when a {@link VibrationEffect} instance serialization fails.
     *
     * <p>The serialization can fail if a given vibration cannot be represented using the public
     * format, or if it uses hidden APIs that are not supported for serialization (e.g.
     * {@link VibrationEffect.WaveformBuilder}).
     *
     * @hide
     */
    @TestApi
    public static final class SerializationFailedException extends IllegalStateException {
    public static final class SerializationFailedException extends RuntimeException {
        SerializationFailedException(VibrationEffect effect, Throwable cause) {
            super("Serialization failed for vibration effect " + effect, cause);
        }
+15 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.os.VibrationEffect.Composition.PRIMITIVE_CLICK;
import static android.os.VibrationEffect.Composition.PRIMITIVE_LOW_TICK;
import static android.os.VibrationEffect.Composition.PRIMITIVE_SPIN;
import static android.os.VibrationEffect.Composition.PRIMITIVE_TICK;
import static android.os.vibrator.persistence.VibrationXmlParser.isSupportedMimeType;

import static com.google.common.truth.Truth.assertThat;

@@ -49,6 +50,20 @@ import java.util.Map;
@RunWith(JUnit4.class)
public class VibrationEffectXmlSerializationTest {

    @Test
    public void isSupportedMimeType_onlySupportsVibrationXmlMimeType() {
        // Single MIME type supported
        assertThat(isSupportedMimeType(
                VibrationXmlParser.APPLICATION_VIBRATION_XML_MIME_TYPE)).isTrue();
        assertThat(isSupportedMimeType("application/vnd.android.haptics.vibration+xml")).isTrue();
        // without xml suffix not supported
        assertThat(isSupportedMimeType("application/vnd.android.haptics.vibration")).isFalse();
        // different top-level not supported
        assertThat(isSupportedMimeType("haptics/vnd.android.haptics.vibration+xml")).isFalse();
        // different type not supported
        assertThat(isSupportedMimeType("application/vnd.android.vibration+xml")).isFalse();
    }

    @Test
    public void testPrimitives_allSucceed() throws IOException {
        VibrationEffect effect = VibrationEffect.startComposition()