1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/*
* Copyright (C) 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.client.android;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Vibrator;
import android.util.Log;
import java.io.IOException;
/**
* Manages beeps and vibrations.
*/
public final class BeepManager {
private static final String TAG = BeepManager.class.getSimpleName();
private static final float BEEP_VOLUME = 0.10f;
private static final long VIBRATE_DURATION = 200L;
private final Context context;
private boolean beepEnabled = true;
private boolean vibrateEnabled = false;
public BeepManager(Activity activity) {
activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// We do not keep a reference to the Activity itself, to prevent leaks
this.context = activity.getApplicationContext();
}
public boolean isBeepEnabled() {
return beepEnabled;
}
/**
* Call updatePrefs() after setting this.
*
* If the device is in silent mode, it will not beep.
*
* @param beepEnabled true to enable beep
*/
public void setBeepEnabled(boolean beepEnabled) {
this.beepEnabled = beepEnabled;
}
public boolean isVibrateEnabled() {
return vibrateEnabled;
}
/**
* Call updatePrefs() after setting this.
*
* @param vibrateEnabled true to enable vibrate
*/
public void setVibrateEnabled(boolean vibrateEnabled) {
this.vibrateEnabled = vibrateEnabled;
}
@SuppressLint("MissingPermission")
public synchronized void playBeepSoundAndVibrate() {
if (beepEnabled) {
playBeepSound();
}
if (vibrateEnabled) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
vibrator.vibrate(VIBRATE_DURATION);
}
}
}
public MediaPlayer playBeepSound() {
MediaPlayer mediaPlayer = new MediaPlayer();
if (Build.VERSION.SDK_INT >= 21) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(
AudioAttributes.CONTENT_TYPE_MUSIC).build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mediaPlayer.setOnCompletionListener(mp -> {
mp.stop();
mp.reset();
mp.release();
});
mediaPlayer.setOnErrorListener((mp, what, extra) -> {
Log.w(TAG, "Failed to beep " + what + ", " + extra);
// possibly media player error, so release and recreate
mp.stop();
mp.reset();
mp.release();
return true;
});
try {
AssetFileDescriptor file = context.getResources().openRawResourceFd(R.raw.zxing_beep);
try {
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
} finally {
file.close();
}
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
mediaPlayer.start();
return mediaPlayer;
} catch (IOException ioe) {
Log.w(TAG, ioe);
mediaPlayer.reset();
mediaPlayer.release();
return null;
}
}
}
|