2 * Copyright (C) 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.content.res.AssetFileDescriptor;
23 import android.media.AudioManager;
24 import android.media.MediaPlayer;
25 import android.os.Vibrator;
26 import android.preference.PreferenceManager;
27 import android.util.Log;
29 import java.io.IOException;
32 * Manages beeps and vibrations for {@link CaptureActivity}.
34 public final class BeepManager {
36 private static final String TAG = BeepManager.class.getSimpleName();
38 private static final float BEEP_VOLUME = 0.10f;
39 private static final long VIBRATE_DURATION = 200L;
41 private final Activity activity;
42 private MediaPlayer mediaPlayer;
43 private boolean playBeep;
44 private boolean vibrate;
46 BeepManager(Activity activity) {
47 this.activity = activity;
48 this.mediaPlayer = null;
53 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
54 playBeep = shouldBeep(prefs, activity);
55 vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false);
56 if (playBeep && mediaPlayer == null) {
57 // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud,
58 // so we now play on the music stream.
59 activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
60 mediaPlayer = buildMediaPlayer(activity);
64 void playBeepSoundAndVibrate() {
65 if (playBeep && mediaPlayer != null) {
69 Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
70 vibrator.vibrate(VIBRATE_DURATION);
74 private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
75 boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
77 // See if sound settings overrides this
78 AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
79 if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
80 shouldPlayBeep = false;
83 return shouldPlayBeep;
86 private static MediaPlayer buildMediaPlayer(Context activity) {
87 MediaPlayer mediaPlayer = new MediaPlayer();
88 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
89 // When the beep has finished playing, rewind to queue up another one.
90 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
91 public void onCompletion(MediaPlayer player) {
96 AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
98 mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
100 mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
101 mediaPlayer.prepare();
102 } catch (IOException ioe) {