summaryrefslogtreecommitdiff
path: root/core/java/android/view/SoundEffectConstants.java
diff options
context:
space:
mode:
authorPhilip Junker <philipjunker@google.com>2020-12-04 11:07:58 +0100
committerPhilip Junker <philipjunker@google.com>2021-02-23 08:48:34 +0000
commite3a12e85c4aa75988738c6df18ffdcd4d2b2efb1 (patch)
tree4ec5a7b6fee699faff09352452b51b29e6dfe186 /core/java/android/view/SoundEffectConstants.java
parent891f69f44198c20e55823762e872dfe6ed60fcc6 (diff)
Add support for navigation repeat sound effects
NO_TYPO_CHECK=Existing typo in getContantForFocusDirection (e.g. b/3170596) Test: atest android.view.cts.SoundEffectConstantsTest Test: atest android.view.SoundEffectConstantsTest Bug: 157407957 Change-Id: I7377b1df6b151ea0d77476f0186b4bb21f1a55fe
Diffstat (limited to 'core/java/android/view/SoundEffectConstants.java')
-rw-r--r--core/java/android/view/SoundEffectConstants.java82
1 files changed, 80 insertions, 2 deletions
diff --git a/core/java/android/view/SoundEffectConstants.java b/core/java/android/view/SoundEffectConstants.java
index 8d891bbc2cfc..f177451783dc 100644
--- a/core/java/android/view/SoundEffectConstants.java
+++ b/core/java/android/view/SoundEffectConstants.java
@@ -16,12 +16,21 @@
package android.view;
+import android.media.AudioManager;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.annotations.VisibleForTesting.Visibility;
+
+import java.util.Random;
+
/**
- * Constants to be used to play sound effects via {@link View#playSoundEffect(int)}
+ * Constants to be used to play sound effects via {@link View#playSoundEffect(int)}
*/
public class SoundEffectConstants {
private SoundEffectConstants() {}
+ private static final Random NAVIGATION_REPEAT_RANDOMIZER = new Random();
+ private static int sLastNavigationRepeatSoundEffectId = -1;
public static final int CLICK = 0;
@@ -29,6 +38,14 @@ public class SoundEffectConstants {
public static final int NAVIGATION_UP = 2;
public static final int NAVIGATION_RIGHT = 3;
public static final int NAVIGATION_DOWN = 4;
+ /** Sound effect for a repeatedly triggered navigation, e.g. due to long pressing a button */
+ public static final int NAVIGATION_REPEAT_LEFT = 5;
+ /** @see #NAVIGATION_REPEAT_LEFT */
+ public static final int NAVIGATION_REPEAT_UP = 6;
+ /** @see #NAVIGATION_REPEAT_LEFT */
+ public static final int NAVIGATION_REPEAT_RIGHT = 7;
+ /** @see #NAVIGATION_REPEAT_LEFT */
+ public static final int NAVIGATION_REPEAT_DOWN = 8;
/**
* Get the sonification constant for the focus directions.
@@ -40,7 +57,7 @@ public class SoundEffectConstants {
* @throws {@link IllegalArgumentException} when the passed direction is not one of the
* documented values.
*/
- public static int getContantForFocusDirection(int direction) {
+ public static int getContantForFocusDirection(@View.FocusDirection int direction) {
switch (direction) {
case View.FOCUS_RIGHT:
return SoundEffectConstants.NAVIGATION_RIGHT;
@@ -56,4 +73,65 @@ public class SoundEffectConstants {
throw new IllegalArgumentException("direction must be one of "
+ "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, FOCUS_BACKWARD}.");
}
+
+ /**
+ * Get the sonification constant for the focus directions
+ * @param direction One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
+ * {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT}, {@link View#FOCUS_FORWARD}
+ * or {@link View#FOCUS_BACKWARD}
+ * @param repeating True if the user long-presses a direction
+ * @return The appropriate sonification constant
+ * @throws IllegalArgumentException when the passed direction is not one of the
+ * documented values.
+ */
+ public static int getConstantForFocusDirection(@View.FocusDirection int direction,
+ boolean repeating) {
+ if (repeating) {
+ switch (direction) {
+ case View.FOCUS_RIGHT:
+ return SoundEffectConstants.NAVIGATION_REPEAT_RIGHT;
+ case View.FOCUS_FORWARD:
+ case View.FOCUS_DOWN:
+ return SoundEffectConstants.NAVIGATION_REPEAT_DOWN;
+ case View.FOCUS_LEFT:
+ return SoundEffectConstants.NAVIGATION_REPEAT_LEFT;
+ case View.FOCUS_BACKWARD:
+ case View.FOCUS_UP:
+ return SoundEffectConstants.NAVIGATION_REPEAT_UP;
+ }
+ throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, "
+ + "FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, FOCUS_BACKWARD}.");
+ } else {
+ return getContantForFocusDirection(direction);
+ }
+ }
+
+ /**
+ * @param effectId any of the effect ids defined in {@link SoundEffectConstants}
+ * @return true if the given effect id is a navigation repeat one
+ * @hide
+ */
+ @VisibleForTesting(visibility = Visibility.PACKAGE)
+ public static boolean isNavigationRepeat(int effectId) {
+ return effectId == SoundEffectConstants.NAVIGATION_REPEAT_DOWN
+ || effectId == SoundEffectConstants.NAVIGATION_REPEAT_LEFT
+ || effectId == SoundEffectConstants.NAVIGATION_REPEAT_RIGHT
+ || effectId == SoundEffectConstants.NAVIGATION_REPEAT_UP;
+ }
+
+ /**
+ * @return The next navigation repeat sound effect id, chosen at random in a non-repeating
+ * fashion
+ * @hide
+ */
+ @VisibleForTesting(visibility = Visibility.PACKAGE)
+ public static int nextNavigationRepeatSoundEffectId() {
+ int next = NAVIGATION_REPEAT_RANDOMIZER.nextInt(
+ AudioManager.NUM_NAVIGATION_REPEAT_SOUND_EFFECTS - 1);
+ if (next >= sLastNavigationRepeatSoundEffectId) {
+ next++;
+ }
+ sLastNavigationRepeatSoundEffectId = next;
+ return AudioManager.getNthNavigationRepeatSoundEffect(next);
+ }
}