/TalkBack/src/com/google/android/marvin/talkback/FeedbackController.java

http://eyes-free.googlecode.com/ · Java · 234 lines · 97 code · 42 blank · 95 comment · 14 complexity · 02e6f6ac4ade2f7265f4c65d4c00a17e MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.android.marvin.talkback;
  17. import android.content.Context;
  18. import android.content.res.Resources;
  19. import android.media.AudioManager;
  20. import android.media.SoundPool;
  21. import android.os.Vibrator;
  22. import java.util.Map;
  23. import java.util.TreeMap;
  24. /**
  25. * Provides auditory and haptic feedback.
  26. *
  27. * @author alanv@google.com (Alan Viverette)
  28. */
  29. class FeedbackController {
  30. private static FeedbackController sInstance;
  31. /**
  32. * @return The shared instance of FeedbackController.
  33. */
  34. public static FeedbackController getInstance(Context context) {
  35. if (sInstance == null) {
  36. sInstance = new FeedbackController(context);
  37. }
  38. return sInstance;
  39. }
  40. /**
  41. * Default stream for audio feedback.
  42. */
  43. private static final int DEFAULT_STREAM = AudioManager.STREAM_MUSIC;
  44. /**
  45. * Default volume for sound playback. Use 1.0f to match the current stream
  46. * volume.
  47. */
  48. private static final float DEFAULT_VOLUME = 1.0f;
  49. /**
  50. * Default rate for sound playback.
  51. */
  52. private static final float DEFAULT_RATE = 1.0f;
  53. /**
  54. * Number of channels to use in SoundPool for auditory icon feedback.
  55. */
  56. private static final int NUMBER_OF_CHANNELS = 10;
  57. /**
  58. * The parent context. Required for mapping resource identifiers to
  59. * resources.
  60. */
  61. private final Context mContext;
  62. /**
  63. * Vibration service used to play vibration patterns.
  64. */
  65. private final Vibrator mVibrator;
  66. /**
  67. * Sound pool used to play auditory icons.
  68. */
  69. private final SoundPool mSoundPool;
  70. /**
  71. * Whether haptic feedback is enabled.
  72. */
  73. private boolean mHapticEnabled = true;
  74. /**
  75. * Whether auditory feedback is enabled.
  76. */
  77. private boolean mAuditoryEnabled = true;
  78. /** Current volume (range 0..1). */
  79. private float mVolume = DEFAULT_VOLUME;
  80. private final Map<Integer, long[]> mResourceIdToVibrationPatternMap;
  81. private final Map<Integer, Integer> mResourceIdToSoundMap;
  82. /**
  83. * Constructs a new feedback controller.
  84. *
  85. * @param context The parent context.
  86. */
  87. private FeedbackController(Context context) {
  88. mContext = context;
  89. mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  90. mSoundPool = new SoundPool(NUMBER_OF_CHANNELS, DEFAULT_STREAM, 1);
  91. mResourceIdToSoundMap = new TreeMap<Integer, Integer>();
  92. mResourceIdToVibrationPatternMap = new TreeMap<Integer, long[]>();
  93. // Since the sound pool loads resources asynchronously, we need to play
  94. // resources immediately after they load.
  95. mSoundPool.setOnLoadCompleteListener(mOnLoadCompleteListener);
  96. }
  97. /**
  98. * Sets the current volume for auditory feedback.
  99. *
  100. * @param volume Volume value (range 0..100).
  101. */
  102. public void setVolume(int volume) {
  103. mVolume = (Math.min(100, Math.max(0, volume)) / 100.0f);
  104. }
  105. /**
  106. * @param enabled Whether haptic feedback should be enabled.
  107. */
  108. public void setHapticEnabled(boolean enabled) {
  109. mHapticEnabled = enabled;
  110. }
  111. /**
  112. * @param enabled Whether auditory feedback should be enabled.
  113. */
  114. public void setAuditoryEnabled(boolean enabled) {
  115. mAuditoryEnabled = enabled;
  116. }
  117. /**
  118. * Stops all active feedback.
  119. */
  120. public void interrupt() {
  121. mVibrator.cancel();
  122. mSoundPool.autoPause();
  123. }
  124. /**
  125. * Releases resources associated with this feedback controller. It is good
  126. * practice to call this method when you're done using the controller.
  127. */
  128. public void shutdown() {
  129. mVibrator.cancel();
  130. mSoundPool.release();
  131. }
  132. /**
  133. * Plays the sound file specified by the given resource identifier at the
  134. * specified rate.
  135. *
  136. * @param resId The sound file's resource identifier.
  137. * @param rate The rate at which to play back the sound, where 1.0 is normal
  138. * speed and 0.5 is half-speed.
  139. * @return {@code true} if successful
  140. */
  141. public boolean playSound(int resId, float rate) {
  142. if (!mAuditoryEnabled) {
  143. return false;
  144. }
  145. Integer soundId = mResourceIdToSoundMap.get(resId);
  146. if (soundId == null) {
  147. soundId = mSoundPool.load(mContext, resId, 1);
  148. mResourceIdToSoundMap.put(resId, soundId);
  149. // Since we need to play the sound immediately after it loads, we
  150. // should just return true.
  151. return true;
  152. }
  153. final int stream = mSoundPool.play(soundId, mVolume, mVolume, 1, 0, rate);
  154. return (stream != 0);
  155. }
  156. /**
  157. * Plays the vibration pattern specified by the given resource identifier.
  158. *
  159. * @param resId The vibration pattern's resource identifier.
  160. * @return {@code true} if successful
  161. */
  162. public boolean playVibration(int resId) {
  163. if (!mHapticEnabled) {
  164. return false;
  165. }
  166. long[] pattern = mResourceIdToVibrationPatternMap.get(resId);
  167. if (pattern == null) {
  168. final Resources resources = mContext.getResources();
  169. final int[] intPattern = resources.getIntArray(resId);
  170. if (intPattern == null) {
  171. return false;
  172. }
  173. pattern = new long[intPattern.length];
  174. for (int i = 0; i < pattern.length; i++) {
  175. pattern[i] = intPattern[i];
  176. }
  177. mResourceIdToVibrationPatternMap.put(resId, pattern);
  178. }
  179. mVibrator.vibrate(pattern, -1);
  180. return true;
  181. }
  182. private final SoundPool.OnLoadCompleteListener mOnLoadCompleteListener =
  183. new SoundPool.OnLoadCompleteListener() {
  184. @Override
  185. public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
  186. if (status != 0) {
  187. return;
  188. }
  189. soundPool.play(sampleId, mVolume, mVolume, 1, 0, DEFAULT_RATE);
  190. }
  191. };
  192. }