From 386c120eaa0adb7ec10631fdc84f428832f158a2 Mon Sep 17 00:00:00 2001 From: srowen Date: Mon, 11 Oct 2010 07:27:22 +0000 Subject: [PATCH] Farm out beep/vibrate stuff to class to try to reduce CaptureActivity complexity git-svn-id: http://zxing.googlecode.com/svn/trunk@1621 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/client/android/BeepManager.java | 109 ++++++++++++++++++ .../zxing/client/android/CaptureActivity.java | 75 +----------- 2 files changed, 115 insertions(+), 69 deletions(-) create mode 100644 android/src/com/google/zxing/client/android/BeepManager.java diff --git a/android/src/com/google/zxing/client/android/BeepManager.java b/android/src/com/google/zxing/client/android/BeepManager.java new file mode 100644 index 00000000..0e8ee88b --- /dev/null +++ b/android/src/com/google/zxing/client/android/BeepManager.java @@ -0,0 +1,109 @@ +/* + * 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.app.Activity; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.AssetFileDescriptor; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.os.Vibrator; +import android.preference.PreferenceManager; +import android.util.Log; + +import java.io.IOException; + +/** + * Manages beeps and vibrations for {@link CaptureActivity}. + */ +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 Activity activity; + private MediaPlayer mediaPlayer; + private boolean playBeep; + private boolean vibrate; + + BeepManager(Activity activity) { + this.activity = activity; + this.mediaPlayer = null; + updatePrefs(); + } + + void updatePrefs() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); + playBeep = shouldBeep(prefs, activity); + vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false); + if (playBeep && mediaPlayer == null) { + // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud, + // so we now play on the music stream. + activity.setVolumeControlStream(AudioManager.STREAM_MUSIC); + mediaPlayer = buildMediaPlayer(activity); + } + } + + void playBeepSoundAndVibrate() { + if (playBeep && mediaPlayer != null) { + mediaPlayer.start(); + } + if (vibrate) { + Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); + vibrator.vibrate(VIBRATE_DURATION); + } + } + + private static boolean shouldBeep(SharedPreferences prefs, Context activity) { + boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true); + if (shouldPlayBeep) { + // See if sound settings overrides this + AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); + if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { + shouldPlayBeep = false; + } + } + return shouldPlayBeep; + } + + private static MediaPlayer buildMediaPlayer(Context activity) { + MediaPlayer mediaPlayer = new MediaPlayer(); + mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + // When the beep has finished playing, rewind to queue up another one. + mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + public void onCompletion(MediaPlayer player) { + player.seekTo(0); + } + }); + + AssetFileDescriptor file = activity.getResources().openRawResourceFd(R.raw.beep); + try { + mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); + file.close(); + mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); + mediaPlayer.prepare(); + } catch (IOException ioe) { + Log.w(TAG, ioe); + mediaPlayer = null; + } + return mediaPlayer; + } + +} diff --git a/android/src/com/google/zxing/client/android/CaptureActivity.java b/android/src/com/google/zxing/client/android/CaptureActivity.java index da94c95e..90b0f3f0 100755 --- a/android/src/com/google/zxing/client/android/CaptureActivity.java +++ b/android/src/com/google/zxing/client/android/CaptureActivity.java @@ -34,21 +34,16 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; -import android.content.res.AssetFileDescriptor; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; -import android.media.AudioManager; -import android.media.MediaPlayer; -import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.os.Vibrator; import android.preference.PreferenceManager; import android.text.ClipboardManager; import android.util.Log; @@ -93,8 +88,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal private static final long INTENT_RESULT_DURATION = 1500L; private static final long BULK_MODE_SCAN_DELAY_MS = 1000L; - private static final float BEEP_VOLUME = 0.10f; - private static final long VIBRATE_DURATION = 200L; private static final String PACKAGE_NAME = "com.google.zxing.client.android"; private static final String PRODUCT_SEARCH_URL_PREFIX = "http://www.google"; @@ -120,15 +113,11 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal } private CaptureActivityHandler handler; - private ViewfinderView viewfinderView; private TextView statusView; private View resultView; - private MediaPlayer mediaPlayer; private Result lastResult; private boolean hasSurface; - private boolean playBeep; - private boolean vibrate; private boolean copyToClipboard; private Source source; private String sourceUrl; @@ -138,18 +127,9 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal private String versionName; private HistoryManager historyManager; private InactivityTimer inactivityTimer; + private BeepManager beepManager; - /** - * When the beep has finished playing, rewind to queue up another one. - */ - private final OnCompletionListener beepListener = new OnCompletionListener() { - public void onCompletion(MediaPlayer mediaPlayer) { - mediaPlayer.seekTo(0); - } - }; - - private final DialogInterface.OnClickListener aboutListener = - new DialogInterface.OnClickListener() { + private final DialogInterface.OnClickListener aboutListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.zxing_url))); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); @@ -183,6 +163,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal historyManager = new HistoryManager(this); historyManager.trimHistory(); inactivityTimer = new InactivityTimer(this); + beepManager = new BeepManager(this); showHelpOnFirstLaunch(); } @@ -239,17 +220,9 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - playBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true); - if (playBeep) { - // See if sound settings overrides this - AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE); - if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { - playBeep = false; - } - } - vibrate = prefs.getBoolean(PreferencesActivity.KEY_VIBRATE, false); copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true); - initBeepSound(); + + beepManager.updatePrefs(); } @Override @@ -390,7 +363,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal // This is from history -- no saved barcode handleDecodeInternally(rawResult, null); } else { - playBeepSoundAndVibrate(); + beepManager.playBeepSoundAndVibrate(); drawResultPoints(barcode, rawResult); switch (source) { case NATIVE_APP_INTENT: @@ -609,42 +582,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal return false; } - /** - * Creates the beep MediaPlayer in advance so that the sound can be triggered with the least - * latency possible. - */ - private void initBeepSound() { - if (playBeep && mediaPlayer == null) { - // The volume on STREAM_SYSTEM is not adjustable, and users found it too loud, - // so we now play on the music stream. - setVolumeControlStream(AudioManager.STREAM_MUSIC); - mediaPlayer = new MediaPlayer(); - mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); - mediaPlayer.setOnCompletionListener(beepListener); - - AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep); - try { - mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), - file.getLength()); - file.close(); - mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); - mediaPlayer.prepare(); - } catch (IOException e) { - mediaPlayer = null; - } - } - } - - private void playBeepSoundAndVibrate() { - if (playBeep && mediaPlayer != null) { - mediaPlayer.start(); - } - if (vibrate) { - Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); - vibrator.vibrate(VIBRATE_DURATION); - } - } - private void initCamera(SurfaceHolder surfaceHolder) { try { CameraManager.get().openDriver(surfaceHolder); -- 2.20.1