OCR Configurable  1.0
 Todo Clases Namespaces Archivos Funciones Variables
BeepManager.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright (C) 2010 ZXing authors
00003  * Copyright 2011 Robert Theis
00004  * Copyright 2012 Jaime Navarro Santapau
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00027 package edu.uoc.ocr;
00028 
00029 import android.app.Activity;
00030 import android.content.Context;
00031 import android.content.SharedPreferences;
00032 import android.content.res.AssetFileDescriptor;
00033 import android.media.AudioManager;
00034 import android.media.MediaPlayer;
00035 import android.preference.PreferenceManager;
00036 import android.util.Log;
00037 
00038 import java.io.IOException;
00039 
00040 import edu.uoc.ocr.R;
00041 
00046 public final class BeepManager {
00047 
00048         private static final String TAG = BeepManager.class.getSimpleName();
00049         private static final float BEEP_VOLUME = 0.10f;
00050         private final Activity activity;
00051         private MediaPlayer mediaPlayer;
00052         private boolean playBeep;
00053 
00054         public BeepManager(Activity activity) {
00055                 this.activity = activity;
00056                 this.mediaPlayer = null;
00057                 updatePrefs();
00058         }
00059 
00060         public void updatePrefs() {
00061                 SharedPreferences prefs = PreferenceManager
00062                                 .getDefaultSharedPreferences(activity);
00063                 playBeep = shouldBeep(prefs, activity);
00064                 if (playBeep && mediaPlayer == null) {
00065                         // The volume on STREAM_SYSTEM is not adjustable, and users found it
00066                         // too loud,
00067                         // so we now play on the music stream.
00068                         activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
00069                         mediaPlayer = buildMediaPlayer(activity);
00070                 }
00071         }
00072 
00073         public void playBeepSoundAndVibrate() {
00074                 if (playBeep && mediaPlayer != null) {
00075                         mediaPlayer.start();
00076                 }
00077         }
00078 
00079         private static boolean shouldBeep(SharedPreferences prefs, Context activity) {
00080                 boolean shouldPlayBeep = prefs.getBoolean(
00081                                 PreferencesActivity.KEY_PLAY_BEEP,
00082                                 CaptureActivityForOcr.DEFAULT_TOGGLE_BEEP);
00083                 if (shouldPlayBeep) {
00084                         // See if sound settings overrides this
00085                         AudioManager audioService = (AudioManager) activity
00086                                         .getSystemService(Context.AUDIO_SERVICE);
00087                         if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
00088                                 shouldPlayBeep = false;
00089                         }
00090                 }
00091                 return shouldPlayBeep;
00092         }
00093 
00094         private static MediaPlayer buildMediaPlayer(Context activity) {
00095                 MediaPlayer mediaPlayer = new MediaPlayer();
00096                 mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
00097                 // When the beep has finished playing, rewind to queue up another one.
00098                 mediaPlayer
00099                                 .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
00100                                         public void onCompletion(MediaPlayer player) {
00101                                                 player.seekTo(0);
00102                                         }
00103                                 });
00104 
00105                 AssetFileDescriptor file = activity.getResources().openRawResourceFd(
00106                                 R.raw.beep);
00107                 try {
00108                         mediaPlayer.setDataSource(file.getFileDescriptor(),
00109                                         file.getStartOffset(), file.getLength());
00110                         file.close();
00111                         mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
00112                         mediaPlayer.prepare();
00113                 } catch (IOException ioe) {
00114                         Log.w(TAG, ioe);
00115                         mediaPlayer = null;
00116                 }
00117                 return mediaPlayer;
00118         }
00119 
00120 }
 Todo Clases Namespaces Archivos Funciones Variables