| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.media.AudioManager; |
| 21 |
|
| 22 |
import com.android.internal.annotations.VisibleForTesting; |
| 23 |
import com.android.internal.annotations.VisibleForTesting.Visibility; |
| 24 |
|
| 25 |
import java.lang.annotation.Retention; |
| 26 |
import java.lang.annotation.RetentionPolicy; |
| 27 |
import java.util.Random; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class SoundEffectConstants { |
| 33 |
|
| 34 |
private SoundEffectConstants() {} |
| 35 |
private static final Random NAVIGATION_REPEAT_RANDOMIZER = new Random(); |
| 36 |
private static int sLastNavigationRepeatSoundEffectId = -1; |
| 37 |
|
| 38 |
public static final int CLICK = 0; |
| 39 |
|
| 40 |
|
| 41 |
public static final int NAVIGATION_LEFT = 1; |
| 42 |
|
| 43 |
public static final int NAVIGATION_UP = 2; |
| 44 |
|
| 45 |
public static final int NAVIGATION_RIGHT = 3; |
| 46 |
|
| 47 |
public static final int NAVIGATION_DOWN = 4; |
| 48 |
|
| 49 |
public static final int NAVIGATION_REPEAT_LEFT = 5; |
| 50 |
|
| 51 |
public static final int NAVIGATION_REPEAT_UP = 6; |
| 52 |
|
| 53 |
public static final int NAVIGATION_REPEAT_RIGHT = 7; |
| 54 |
|
| 55 |
public static final int NAVIGATION_REPEAT_DOWN = 8; |
| 56 |
|
| 57 |
|
| 58 |
@IntDef(value = { |
| 59 |
CLICK, |
| 60 |
NAVIGATION_LEFT, |
| 61 |
NAVIGATION_UP, |
| 62 |
NAVIGATION_RIGHT, |
| 63 |
NAVIGATION_DOWN, |
| 64 |
NAVIGATION_REPEAT_LEFT, |
| 65 |
NAVIGATION_REPEAT_UP, |
| 66 |
NAVIGATION_REPEAT_RIGHT, |
| 67 |
NAVIGATION_REPEAT_DOWN |
| 68 |
}) |
| 69 |
@Retention(RetentionPolicy.SOURCE) |
| 70 |
public @interface SoundEffect {} |
| 71 |
|
| 72 |
|
| 73 |
@IntDef(prefix = { "NAVIGATION_" }, value = { |
| 74 |
NAVIGATION_LEFT, |
| 75 |
NAVIGATION_UP, |
| 76 |
NAVIGATION_RIGHT, |
| 77 |
NAVIGATION_DOWN, |
| 78 |
NAVIGATION_REPEAT_LEFT, |
| 79 |
NAVIGATION_REPEAT_UP, |
| 80 |
NAVIGATION_REPEAT_RIGHT, |
| 81 |
NAVIGATION_REPEAT_DOWN |
| 82 |
}) |
| 83 |
@Retention(RetentionPolicy.SOURCE) |
| 84 |
public @interface NavigationSoundEffect {} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static int getContantForFocusDirection(@View.FocusDirection int direction) { |
| 94 |
switch (direction) { |
| 95 |
case View.FOCUS_RIGHT: |
| 96 |
return SoundEffectConstants.NAVIGATION_RIGHT; |
| 97 |
case View.FOCUS_FORWARD: |
| 98 |
case View.FOCUS_DOWN: |
| 99 |
return SoundEffectConstants.NAVIGATION_DOWN; |
| 100 |
case View.FOCUS_LEFT: |
| 101 |
return SoundEffectConstants.NAVIGATION_LEFT; |
| 102 |
case View.FOCUS_BACKWARD: |
| 103 |
case View.FOCUS_UP: |
| 104 |
return SoundEffectConstants.NAVIGATION_UP; |
| 105 |
} |
| 106 |
throw new IllegalArgumentException("direction must be one of " |
| 107 |
+ "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, FOCUS_BACKWARD}."); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static @NavigationSoundEffect int getConstantForFocusDirection( |
| 119 |
@View.FocusDirection int direction, boolean repeating) { |
| 120 |
if (repeating) { |
| 121 |
switch (direction) { |
| 122 |
case View.FOCUS_RIGHT: |
| 123 |
return SoundEffectConstants.NAVIGATION_REPEAT_RIGHT; |
| 124 |
case View.FOCUS_FORWARD: |
| 125 |
case View.FOCUS_DOWN: |
| 126 |
return SoundEffectConstants.NAVIGATION_REPEAT_DOWN; |
| 127 |
case View.FOCUS_LEFT: |
| 128 |
return SoundEffectConstants.NAVIGATION_REPEAT_LEFT; |
| 129 |
case View.FOCUS_BACKWARD: |
| 130 |
case View.FOCUS_UP: |
| 131 |
return SoundEffectConstants.NAVIGATION_REPEAT_UP; |
| 132 |
} |
| 133 |
throw new IllegalArgumentException("direction must be one of {FOCUS_UP, FOCUS_DOWN, " |
| 134 |
+ "FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, FOCUS_BACKWARD}."); |
| 135 |
} else { |
| 136 |
return getContantForFocusDirection(direction); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@VisibleForTesting(visibility = Visibility.PACKAGE) |
| 146 |
public static boolean isNavigationRepeat(@NavigationSoundEffect int effectId) { |
| 147 |
return effectId == SoundEffectConstants.NAVIGATION_REPEAT_DOWN |
| 148 |
|| effectId == SoundEffectConstants.NAVIGATION_REPEAT_LEFT |
| 149 |
|| effectId == SoundEffectConstants.NAVIGATION_REPEAT_RIGHT |
| 150 |
|| effectId == SoundEffectConstants.NAVIGATION_REPEAT_UP; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@VisibleForTesting(visibility = Visibility.PACKAGE) |
| 159 |
public static int nextNavigationRepeatSoundEffectId() { |
| 160 |
int next = NAVIGATION_REPEAT_RANDOMIZER.nextInt( |
| 161 |
AudioManager.NUM_NAVIGATION_REPEAT_SOUND_EFFECTS - 1); |
| 162 |
if (next >= sLastNavigationRepeatSoundEffectId) { |
| 163 |
next++; |
| 164 |
} |
| 165 |
sLastNavigationRepeatSoundEffectId = next; |
| 166 |
return AudioManager.getNthNavigationRepeatSoundEffect(next); |
| 167 |
} |
| 168 |
} |