From f268bf573f671fdc051bf4485dc0ea972406ea38 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Wed, 7 Feb 2018 23:23:34 +0000 Subject: [PATCH] Add support for vibrator HAL 1.2 effects. Test: atest android.os.VibrationEffectTest Bug: 64184692 Bug: 64185677 Change-Id: I0b3f9caa04b3e7bdadba5c44188120ac14943f82 --- Android.bp | 5 +- core/java/android/os/VibrationEffect.java | 103 +++++++++++++++++++-- core/res/res/values/config.xml | 8 ++ core/res/res/values/symbols.xml | 2 + .../src/android/os/VibrationEffectTest.java | 90 ++++++++++++++++++ .../java/com/android/server/VibratorService.java | 2 +- services/core/jni/Android.bp | 1 + .../jni/com_android_server_VibratorService.cpp | 69 ++++++++------ 8 files changed, 244 insertions(+), 36 deletions(-) create mode 100644 core/tests/coretests/src/android/os/VibrationEffectTest.java diff --git a/Android.bp b/Android.bp index 3d56254417e1..d1970faa1edf 100644 --- a/Android.bp +++ b/Android.bp @@ -671,8 +671,9 @@ java_library { "android.hardware.tv.input-V1.0-java-constants", "android.hardware.usb-V1.0-java-constants", "android.hardware.usb-V1.1-java-constants", - "android.hardware.vibrator-V1.0-java-constants", - "android.hardware.vibrator-V1.1-java-constants", + "android.hardware.vibrator-V1.0-java", + "android.hardware.vibrator-V1.1-java", + "android.hardware.vibrator-V1.2-java", "android.hardware.wifi-V1.0-java-constants", "android.hardware.radio-V1.0-java", "android.hardware.usb.gadget-V1.0-java", diff --git a/core/java/android/os/VibrationEffect.java b/core/java/android/os/VibrationEffect.java index b6f16a7b9ff8..e9b48535a34e 100644 --- a/core/java/android/os/VibrationEffect.java +++ b/core/java/android/os/VibrationEffect.java @@ -16,10 +16,15 @@ package android.os; -import android.hardware.vibrator.V1_0.Constants.EffectStrength; -import android.hardware.vibrator.V1_1.Constants.Effect_1_1; +import android.annotation.Nullable; +import android.content.Context; +import android.hardware.vibrator.V1_0.EffectStrength; +import android.hardware.vibrator.V1_2.Effect; +import android.net.Uri; import android.util.MathUtils; +import com.android.internal.annotations.VisibleForTesting; + import java.util.Arrays; /** @@ -49,7 +54,7 @@ public abstract class VibrationEffect implements Parcelable { * @see #get(int) * @hide */ - public static final int EFFECT_CLICK = Effect_1_1.CLICK; + public static final int EFFECT_CLICK = Effect.CLICK; /** * A double click effect. @@ -57,14 +62,62 @@ public abstract class VibrationEffect implements Parcelable { * @see #get(int) * @hide */ - public static final int EFFECT_DOUBLE_CLICK = Effect_1_1.DOUBLE_CLICK; + public static final int EFFECT_DOUBLE_CLICK = Effect.DOUBLE_CLICK; /** * A tick effect. * @see #get(int) * @hide */ - public static final int EFFECT_TICK = Effect_1_1.TICK; + public static final int EFFECT_TICK = Effect.TICK; + + /** + * A thud effect. + * @see #get(int) + * @hide + */ + public static final int EFFECT_THUD = Effect.THUD; + + /** + * A pop effect. + * @see #get(int) + * @hide + */ + public static final int EFFECT_POP = Effect.POP; + + /** + * A heavy click effect. + * @see #get(int) + * @hide + */ + public static final int EFFECT_HEAVY_CLICK = Effect.HEAVY_CLICK; + + + /** + * Ringtone patterns. They may correspond with the device's ringtone audio, or may just be a + * pattern that can be played as a ringtone with any audio, depending on the device. + * + * @see #get(Uri, Context) + * @hide + */ + @VisibleForTesting + public static final int[] RINGTONES = { + Effect.RINGTONE_1, + Effect.RINGTONE_2, + Effect.RINGTONE_3, + Effect.RINGTONE_4, + Effect.RINGTONE_5, + Effect.RINGTONE_6, + Effect.RINGTONE_7, + Effect.RINGTONE_8, + Effect.RINGTONE_9, + Effect.RINGTONE_10, + Effect.RINGTONE_11, + Effect.RINGTONE_12, + Effect.RINGTONE_13, + Effect.RINGTONE_14, + Effect.RINGTONE_15 + }; /** @hide to prevent subclassing from outside of the framework */ public VibrationEffect() { } @@ -198,6 +251,37 @@ public abstract class VibrationEffect implements Parcelable { return effect; } + /** + * Get a predefined vibration effect associated with a given URI. + * + * Predefined effects are a set of common vibration effects that should be identical, regardless + * of the app they come from, in order to provide a cohesive experience for users across + * the entire device. They also may be custom tailored to the device hardware in order to + * provide a better experience than you could otherwise build using the generic building + * blocks. + * + * @param uri The URI associated with the haptic effect. + * @param context The context used to get the URI to haptic effect association. + * + * @return The desired effect, or {@code null} if there's no associated effect. + * + * @hide + */ + @Nullable + public static VibrationEffect get(Uri uri, Context context) { + String[] uris = context.getResources().getStringArray( + com.android.internal.R.array.config_ringtoneEffectUris); + for (int i = 0; i < uris.length && i < RINGTONES.length; i++) { + if (uris[i] == null) { + continue; + } + if (Uri.parse(uris[i]).equals(uri)) { + return get(RINGTONES[i]); + } + } + return null; + } + @Override public int describeContents() { return 0; @@ -548,10 +632,15 @@ public abstract class VibrationEffect implements Parcelable { case EFFECT_CLICK: case EFFECT_DOUBLE_CLICK: case EFFECT_TICK: + case EFFECT_THUD: + case EFFECT_POP: + case EFFECT_HEAVY_CLICK: break; default: - throw new IllegalArgumentException( - "Unknown prebaked effect type (value=" + mEffectId + ")"); + if (mEffectId < RINGTONES[0] || mEffectId > RINGTONES[RINGTONES.length - 1]) { + throw new IllegalArgumentException( + "Unknown prebaked effect type (value=" + mEffectId + ")"); + } } if (!isValidEffectStrength(mEffectStrength)) { throw new IllegalArgumentException( diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index c8032a276659..d99f28e8a3c4 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1075,6 +1075,14 @@ 10 + + + + false