Merge "Use SoundPool instead of Ringtone."

This commit is contained in:
Marco Nelissen
2011-09-29 13:13:17 -07:00
committed by Android (Google) Code Review

View File

@@ -31,9 +31,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.media.AudioManager; import android.media.AudioManager;
import android.media.Ringtone; import android.media.SoundPool;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.LocalPowerManager; import android.os.LocalPowerManager;
import android.os.Message; import android.os.Message;
@@ -253,6 +251,11 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
private boolean mWaitingUntilKeyguardVisible = false; private boolean mWaitingUntilKeyguardVisible = false;
private LockPatternUtils mLockPatternUtils; private LockPatternUtils mLockPatternUtils;
private SoundPool mLockSounds;
private int mLockSoundId;
private int mUnlockSoundId;
private int mLockSoundStreamId;
public KeyguardViewMediator(Context context, PhoneWindowManager callback, public KeyguardViewMediator(Context context, PhoneWindowManager callback,
LocalPowerManager powerManager) { LocalPowerManager powerManager) {
mContext = context; mContext = context;
@@ -298,6 +301,22 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = mContext.getContentResolver();
mShowLockIcon = (Settings.System.getInt(cr, "show_status_bar_lock", 0) == 1); mShowLockIcon = (Settings.System.getInt(cr, "show_status_bar_lock", 0) == 1);
mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
String soundPath = Settings.System.getString(cr, Settings.System.LOCK_SOUND);
if (soundPath != null) {
mLockSoundId = mLockSounds.load(soundPath, 1);
}
if (soundPath == null || mLockSoundId == 0) {
if (DEBUG) Log.d(TAG, "failed to load sound from " + soundPath);
}
soundPath = Settings.System.getString(cr, Settings.System.UNLOCK_SOUND);
if (soundPath != null) {
mUnlockSoundId = mLockSounds.load(soundPath, 1);
}
if (soundPath == null || mUnlockSoundId == 0) {
if (DEBUG) Log.d(TAG, "failed to load sound from " + soundPath);
}
} }
/** /**
@@ -1044,29 +1063,11 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
final ContentResolver cr = mContext.getContentResolver(); final ContentResolver cr = mContext.getContentResolver();
if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1)
{ {
final String whichSound = locked final int whichSound = locked
? Settings.System.LOCK_SOUND ? mLockSoundId
: Settings.System.UNLOCK_SOUND; : mUnlockSoundId;
final String soundPath = Settings.System.getString(cr, whichSound); mLockSounds.stop(mLockSoundStreamId);
if (soundPath != null) { mLockSoundStreamId = mLockSounds.play(whichSound, 1.0f, 1.0f, 1, 0, 1.0f);
final Uri soundUri = Uri.parse("file://" + soundPath);
if (soundUri != null) {
final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
if (sfx != null) {
sfx.setStreamType(AudioManager.STREAM_SYSTEM);
sfx.setWakeMode(mContext, PowerManager.PARTIAL_WAKE_LOCK);
sfx.play();
} else {
if (DEBUG) Log.d(TAG, "playSounds: failed to load ringtone from uri: "
+ soundUri);
}
} else {
if (DEBUG) Log.d(TAG, "playSounds: could not parse Uri: " + soundPath);
}
} else {
if (DEBUG) Log.d(TAG, "playSounds: whichSound = " + whichSound
+ "; soundPath was null");
}
} }
} }