Farm out beep/vibrate stuff to class to try to reduce CaptureActivity complexity
[zxing.git] / android / src / com / google / zxing / client / android / BeepManager.java
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
17 package com.google.zxing.client.android;
18
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;
28
29 import java.io.IOException;
30
31 /**
32  * Manages beeps and vibrations for {@link CaptureActivity}.
33  */
34 public final class BeepManager {
35
36   private static final String TAG = BeepManager.class.getSimpleName();
37
38   private static final float BEEP_VOLUME = 0.10f;
39   private static final long VIBRATE_DURATION = 200L;
40
41   private final Activity activity;
42   private MediaPlayer mediaPlayer;
43   private boolean playBeep;
44   private boolean vibrate;
45
46   BeepManager(Activity activity) {
47     this.activity = activity;
48     this.mediaPlayer = null;
49     updatePrefs();
50   }
51
52   void updatePrefs() {
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);
61     }
62   }
63
64   void playBeepSoundAndVibrate() {
65     if (playBeep && mediaPlayer != null) {
66       mediaPlayer.start();
67     }
68     if (vibrate) {
69       Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE);
70       vibrator.vibrate(VIBRATE_DURATION);
71     }
72   }
73
74   private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
75     boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true);
76     if (shouldPlayBeep) {
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;
81       }
82     }
83     return shouldPlayBeep;
84   }
85
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) {
92         player.seekTo(0);
93       }
94     });
95
96     AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep);
97     try {
98       mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
99       file.close();
100       mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
101       mediaPlayer.prepare();
102     } catch (IOException ioe) {
103       Log.w(TAG, ioe);
104       mediaPlayer = null;
105     }
106     return mediaPlayer;
107   }
108
109 }