diff options
author | Martijn Coenen <maco@google.com> | 2011-11-11 13:42:31 -0800 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2011-11-11 13:42:31 -0800 |
commit | 1d9c95c9d31f71519d72b60e5af9cd64822ed87b (patch) | |
tree | 479a64aeb1226d20cb4e3d4d7122dc21b4c3a8a7 /src/com | |
parent | b0928eaa500b3cccbf6fbd53377e0f6f16ef8574 (diff) | |
parent | d92037714c289cffb9ed1e6e6df36cd3b7292a21 (diff) | |
download | packages_apps_nfc-1d9c95c9d31f71519d72b60e5af9cd64822ed87b.zip packages_apps_nfc-1d9c95c9d31f71519d72b60e5af9cd64822ed87b.tar.gz packages_apps_nfc-1d9c95c9d31f71519d72b60e5af9cd64822ed87b.tar.bz2 |
am d9203771: Optimize soundpool use.
* commit 'd92037714c289cffb9ed1e6e6df36cd3b7292a21':
Optimize soundpool use.
Diffstat (limited to 'src/com')
-rwxr-xr-x | src/com/android/nfc/NfcService.java | 64 | ||||
-rw-r--r-- | src/com/android/nfc/P2pEventManager.java | 25 |
2 files changed, 58 insertions, 31 deletions
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java index f46f144..68e7598 100755 --- a/src/com/android/nfc/NfcService.java +++ b/src/com/android/nfc/NfcService.java @@ -119,6 +119,11 @@ public class NfcService extends Application implements DeviceHostListener { static final int ROUTE_OFF = 1; static final int ROUTE_ON_WHEN_SCREEN_ON = 2; + // for use with playSound() + public static final int SOUND_START = 0; + public static final int SOUND_END = 1; + public static final int SOUND_ERROR = 2; + public static final String ACTION_RF_FIELD_ON_DETECTED = "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED"; public static final String ACTION_RF_FIELD_OFF_DETECTED = @@ -286,11 +291,6 @@ public class NfcService extends Application implements DeviceHostListener { sService = this; - mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); - mStartSound = mSoundPool.load(this, R.raw.start, 1); - mEndSound = mSoundPool.load(this, R.raw.end, 1); - mErrorSound = mSoundPool.load(this, R.raw.error, 1); - mContext = this; mDeviceHost = new NativeNfcManager(this, this); @@ -331,6 +331,26 @@ public class NfcService extends Application implements DeviceHostListener { new EnableDisableTask().execute(TASK_BOOT); // do blocking boot tasks } + void initSoundPool() { + synchronized(this) { + if (mSoundPool == null) { + mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); + mStartSound = mSoundPool.load(this, R.raw.start, 1); + mEndSound = mSoundPool.load(this, R.raw.end, 1); + mErrorSound = mSoundPool.load(this, R.raw.error, 1); + } + } + } + + void releaseSoundPool() { + synchronized(this) { + if (mSoundPool != null) { + mSoundPool.release(); + mSoundPool = null; + } + } + } + void registerForAirplaneMode(IntentFilter filter) { final ContentResolver resolver = mContext.getContentResolver(); final String airplaneModeRadios = Settings.System.getString(resolver, @@ -456,6 +476,8 @@ public class NfcService extends Application implements DeviceHostListener { updateState(NfcAdapter.STATE_ON); } + initSoundPool(); + /* Start polling loop */ applyRouting(); return true; @@ -497,6 +519,8 @@ public class NfcService extends Application implements DeviceHostListener { updateState(NfcAdapter.STATE_OFF); + releaseSoundPool(); + return result; } @@ -560,9 +584,23 @@ public class NfcService extends Application implements DeviceHostListener { } } - void playSound(int sound) { + public void playSound(int sound) { synchronized (this) { - mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f); + if (mSoundPool == null) { + Log.w(TAG, "Not playing sound when NFC is disabled"); + return; + } + switch (sound) { + case SOUND_START: + mSoundPool.play(mStartSound, 1.0f, 1.0f, 0, 0, 1.0f); + break; + case SOUND_END: + mSoundPool.play(mEndSound, 1.0f, 1.0f, 0, 0, 1.0f); + break; + case SOUND_ERROR: + mSoundPool.play(mErrorSound, 1.0f, 1.0f, 0, 0, 1.0f); + break; + } } } @@ -1392,9 +1430,9 @@ public class NfcService extends Application implements DeviceHostListener { boolean delivered = mNfcDispatcher.dispatchTag(tag, new NdefMessage[] { ndefMsg }); if (delivered) { - playSound(mEndSound); + playSound(SOUND_END); } else { - playSound(mErrorSound); + playSound(SOUND_ERROR); } break; } @@ -1402,7 +1440,7 @@ public class NfcService extends Application implements DeviceHostListener { case MSG_NDEF_TAG: if (DBG) Log.d(TAG, "Tag detected, notifying applications"); TagEndpoint tag = (TagEndpoint) msg.obj; - playSound(mStartSound); + playSound(SOUND_START); NdefMessage[] ndefMsgs = tag.findAndReadNdef(); if (ndefMsgs != null) { @@ -1414,7 +1452,7 @@ public class NfcService extends Application implements DeviceHostListener { dispatchTagEndpoint(tag, null); } else { tag.disconnect(); - playSound(mErrorSound); + playSound(SOUND_ERROR); } } break; @@ -1595,9 +1633,9 @@ public class NfcService extends Application implements DeviceHostListener { registerTagObject(tagEndpoint); if (!mNfcDispatcher.dispatchTag(tag, msgs)) { unregisterObject(tagEndpoint.getHandle()); - playSound(mErrorSound); + playSound(SOUND_ERROR); } else { - playSound(mEndSound); + playSound(SOUND_END); } } } diff --git a/src/com/android/nfc/P2pEventManager.java b/src/com/android/nfc/P2pEventManager.java index 9273afa..6a00b43 100644 --- a/src/com/android/nfc/P2pEventManager.java +++ b/src/com/android/nfc/P2pEventManager.java @@ -19,11 +19,10 @@ package com.android.nfc; import android.app.NotificationManager; import android.content.Context; import android.content.SharedPreferences; -import android.media.AudioManager; -import android.media.SoundPool; import android.os.Handler; import android.os.Message; import android.os.Vibrator; + import com.android.nfc3.R; /** @@ -36,11 +35,8 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { static final long[] VIBRATION_PATTERN = {0, 100, 10000}; final Context mContext; + final NfcService mNfcService; final P2pEventListener.Callback mCallback; - final int mStartSound; - final int mEndSound; - final int mErrorSound; - final SoundPool mSoundPool; // playback synchronized on this final Vibrator mVibrator; final NotificationManager mNotificationManager; final SendUi mSendUi; @@ -51,12 +47,9 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { boolean mNdefReceived; public P2pEventManager(Context context, P2pEventListener.Callback callback) { + mNfcService = NfcService.getInstance(); mContext = context; mCallback = callback; - mSoundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0); - mStartSound = mSoundPool.load(mContext, R.raw.start, 1); - mEndSound = mSoundPool.load(mContext, R.raw.end, 1); - mErrorSound = mSoundPool.load(mContext, R.raw.error, 1); mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); mNotificationManager = (NotificationManager) mContext.getSystemService( Context.NOTIFICATION_SERVICE); @@ -67,7 +60,7 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { @Override public void onP2pInRange() { - playSound(mStartSound); + mNfcService.playSound(NfcService.SOUND_START); mNdefSent = false; mNdefReceived = false; @@ -82,7 +75,7 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { @Override public void onP2pSendComplete() { - playSound(mEndSound); + mNfcService.playSound(NfcService.SOUND_END); mVibrator.vibrate(VIBRATION_PATTERN, -1); mSendUi.showPostSend(); mSending = false; @@ -92,7 +85,7 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { @Override public void onP2pReceiveComplete() { mVibrator.vibrate(VIBRATION_PATTERN, -1); - playSound(mEndSound); + mNfcService.playSound(NfcService.SOUND_END); // TODO we still don't have a nice receive solution // The sanest solution right now is just to scale back up what we had // and start the new activity. It is not perfect, but at least it is @@ -107,7 +100,7 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { @Override public void onP2pOutOfRange() { if (mSending) { - playSound(mErrorSound); + mNfcService.playSound(NfcService.SOUND_ERROR); mSending = false; } if (!mNdefSent && !mNdefReceived) { @@ -124,8 +117,4 @@ public class P2pEventManager implements P2pEventListener, SendUi.Callback { mSending = true; } - - void playSound(int sound) { - mSoundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f); - } } |