BeepManager.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2010 ZXing authors
  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.zxing.client.android.decode;
  17. import android.app.Activity;
  18. import android.content.Context;
  19. import android.content.res.AssetFileDescriptor;
  20. import android.media.AudioManager;
  21. import android.media.MediaPlayer;
  22. import android.os.Vibrator;
  23. import android.util.Log;
  24. import java.io.IOException;
  25. import me.yoqi.qrcode.Config;
  26. import me.yoqi.qrcode.R;
  27. /**
  28. * Manages beeps and vibrations for {@link CaptureActivity}.
  29. */
  30. public final class BeepManager
  31. {
  32. private static final String TAG = BeepManager.class.getSimpleName ();
  33. private static final float BEEP_VOLUME = 0.10f;
  34. private static final long VIBRATE_DURATION = 200L;
  35. private final Activity activity;
  36. private MediaPlayer mediaPlayer;
  37. private boolean playBeep;
  38. private boolean vibrate;
  39. public BeepManager(Activity activity)
  40. {
  41. this.activity = activity;
  42. this.mediaPlayer = null;
  43. updatePrefs ();
  44. }
  45. public void updatePrefs(){
  46. playBeep = shouldBeep (activity);
  47. vibrate =Config.KEY_VIBRATE;
  48. if (playBeep && mediaPlayer == null)
  49. {
  50. // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
  51. // so we now play on the music stream.
  52. activity.setVolumeControlStream (AudioManager.STREAM_MUSIC);
  53. mediaPlayer = buildMediaPlayer (activity);
  54. }
  55. }
  56. public void playBeepSoundAndVibrate(){
  57. if (playBeep && mediaPlayer != null)
  58. {
  59. mediaPlayer.start ();
  60. }
  61. if (vibrate)
  62. {
  63. Vibrator vibrator = (Vibrator) activity.getSystemService (Context.VIBRATOR_SERVICE);
  64. vibrator.vibrate (VIBRATE_DURATION);
  65. }
  66. }
  67. private static boolean shouldBeep(Context activity){
  68. boolean shouldPlayBeep = Config.KEY_PLAY_BEEP;
  69. if (shouldPlayBeep)
  70. {
  71. // See if sound settings overrides this
  72. AudioManager audioService = (AudioManager) activity.getSystemService (Context.AUDIO_SERVICE);
  73. if (audioService.getRingerMode () != AudioManager.RINGER_MODE_NORMAL)
  74. {
  75. shouldPlayBeep = false;
  76. }
  77. }
  78. return shouldPlayBeep;
  79. }
  80. private static MediaPlayer buildMediaPlayer(Context activity){
  81. MediaPlayer mediaPlayer = new MediaPlayer ();
  82. mediaPlayer.setAudioStreamType (AudioManager.STREAM_MUSIC);
  83. // When the beep has finished playing, rewind to queue up another one.
  84. mediaPlayer.setOnCompletionListener (new MediaPlayer.OnCompletionListener ()
  85. {
  86. @Override
  87. public void onCompletion(MediaPlayer player){
  88. player.seekTo (0);
  89. }
  90. });
  91. AssetFileDescriptor file = activity.getResources ().openRawResourceFd (R.raw.beep);
  92. try
  93. {
  94. mediaPlayer.setDataSource (file.getFileDescriptor (), file.getStartOffset (), file.getLength ());
  95. file.close ();
  96. mediaPlayer.setVolume (BEEP_VOLUME, BEEP_VOLUME);
  97. mediaPlayer.prepare ();
  98. } catch (IOException ioe)
  99. {
  100. Log.w (TAG, ioe);
  101. mediaPlayer = null;
  102. }
  103. return mediaPlayer;
  104. }
  105. }