/talkingdialer/src/com/google/marvin/talkingdialer/FeedbackUtil.java

http://eyes-free.googlecode.com/ · Java · 119 lines · 49 code · 17 blank · 53 comment · 5 complexity · 1dd9907baac0a27251590a49d705566a MD5 · raw file

  1. /*
  2. * Copyright (C) 2011 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.marvin.talkingdialer;
  17. import android.content.Context;
  18. import android.media.AudioManager;
  19. import android.media.SoundPool;
  20. import android.os.Vibrator;
  21. import java.util.TreeMap;
  22. /**
  23. * This class provides haptic and auditory feedback.
  24. *
  25. * @author alanv@google.com (Alan Viverette)
  26. */
  27. public class FeedbackUtil {
  28. private boolean mVibrationEnabled = true;
  29. private boolean mSoundEnabled = true;
  30. private final Context mContext;
  31. private final SoundPool mSoundPool;
  32. private final Vibrator mVibrator;
  33. private final TreeMap<Integer, Integer> mSoundMap;
  34. /**
  35. * Constructs a new FeedbackUtil based on the specified parent
  36. * {@link Context}.
  37. *
  38. * @param context The parent {@link Context}.
  39. */
  40. public FeedbackUtil(Context context) {
  41. mContext = context;
  42. mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 1);
  43. mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
  44. mSoundMap = new TreeMap<Integer, Integer>();
  45. }
  46. /**
  47. * Releases resources associated with this FeedbackUtil. It is good practice
  48. * to call this method when you're done using the FeedbackUtil.
  49. */
  50. public void release() {
  51. mSoundPool.release();
  52. }
  53. /**
  54. * Plays a sound file identified by the given resource id.
  55. *
  56. * @param resId The resource id of the sound file.
  57. */
  58. public void playSound(int resId) {
  59. if (!mSoundEnabled)
  60. return;
  61. Integer soundId = mSoundMap.get(resId);
  62. if (soundId == null) {
  63. soundId = mSoundPool.load(mContext, resId, 1);
  64. mSoundMap.put(resId, soundId);
  65. }
  66. mSoundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
  67. }
  68. /**
  69. * Vibrates for the specified duration.
  70. *
  71. * @param milliseconds How long to vibrate for.
  72. */
  73. public void vibrate(long milliseconds) {
  74. if (!mVibrationEnabled)
  75. return;
  76. mVibrator.vibrate(milliseconds);
  77. }
  78. /**
  79. * Vibrates with a given pattern.
  80. *
  81. * @param pattern The vibration pattern as defined in
  82. * {@link Vibrator#vibrate(long[], int)}.
  83. * @see Vibrator#vibrate(long[], int)
  84. */
  85. public void vibrate(long[] pattern) {
  86. if (!mVibrationEnabled)
  87. return;
  88. mVibrator.vibrate(pattern, -1);
  89. }
  90. /**
  91. * @param enabled {@code true} to enable vibration.
  92. */
  93. public void setVibrationEnabled(boolean enabled) {
  94. mVibrationEnabled = enabled;
  95. }
  96. /**
  97. * @param enabled {@code true} to enable sound.
  98. */
  99. public void setSoundEnabled(boolean enabled) {
  100. mSoundEnabled = enabled;
  101. }
  102. }