Skip to content

Commit 803ce0f

Browse files
marconeAmith Yamasani
authored andcommitted
Use SoundPool instead of Ringtone.
The lock screen was using Ringtone for the lock/unlock sounds, which meant two new MediaPlayers were created every time a sound needed to be played. In addition, the Ringtone was assigned to a local variable, which means it could go be garbage collected and finalized while it was still playing. For short sounds that need to be played repeatedly, SoundPool is a better option anyway, so use that instead. b/5382634 Change-Id: I8794cbb24604fa7c03032bd5e32ceab37a858054
1 parent a0c39e2 commit 803ce0f

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

policy/src/com/android/internal/policy/impl/KeyguardViewMediator.java

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
import android.content.Intent;
3232
import android.content.IntentFilter;
3333
import android.media.AudioManager;
34-
import android.media.Ringtone;
35-
import android.media.RingtoneManager;
36-
import android.net.Uri;
34+
import android.media.SoundPool;
3735
import android.os.Handler;
3836
import android.os.LocalPowerManager;
3937
import android.os.Message;
@@ -253,6 +251,11 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
253251
private boolean mWaitingUntilKeyguardVisible = false;
254252
private LockPatternUtils mLockPatternUtils;
255253

254+
private SoundPool mLockSounds;
255+
private int mLockSoundId;
256+
private int mUnlockSoundId;
257+
private int mLockSoundStreamId;
258+
256259
public KeyguardViewMediator(Context context, PhoneWindowManager callback,
257260
LocalPowerManager powerManager) {
258261
mContext = context;
@@ -298,6 +301,22 @@ public KeyguardViewMediator(Context context, PhoneWindowManager callback,
298301

299302
final ContentResolver cr = mContext.getContentResolver();
300303
mShowLockIcon = (Settings.System.getInt(cr, "show_status_bar_lock", 0) == 1);
304+
305+
mLockSounds = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
306+
String soundPath = Settings.System.getString(cr, Settings.System.LOCK_SOUND);
307+
if (soundPath != null) {
308+
mLockSoundId = mLockSounds.load(soundPath, 1);
309+
}
310+
if (soundPath == null || mLockSoundId == 0) {
311+
if (DEBUG) Log.d(TAG, "failed to load sound from " + soundPath);
312+
}
313+
soundPath = Settings.System.getString(cr, Settings.System.UNLOCK_SOUND);
314+
if (soundPath != null) {
315+
mUnlockSoundId = mLockSounds.load(soundPath, 1);
316+
}
317+
if (soundPath == null || mUnlockSoundId == 0) {
318+
if (DEBUG) Log.d(TAG, "failed to load sound from " + soundPath);
319+
}
301320
}
302321

303322
/**
@@ -1044,28 +1063,11 @@ private void playSounds(boolean locked) {
10441063
final ContentResolver cr = mContext.getContentResolver();
10451064
if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1)
10461065
{
1047-
final String whichSound = locked
1048-
? Settings.System.LOCK_SOUND
1049-
: Settings.System.UNLOCK_SOUND;
1050-
final String soundPath = Settings.System.getString(cr, whichSound);
1051-
if (soundPath != null) {
1052-
final Uri soundUri = Uri.parse("file://" + soundPath);
1053-
if (soundUri != null) {
1054-
final Ringtone sfx = RingtoneManager.getRingtone(mContext, soundUri);
1055-
if (sfx != null) {
1056-
sfx.setStreamType(AudioManager.STREAM_SYSTEM);
1057-
sfx.play();
1058-
} else {
1059-
if (DEBUG) Log.d(TAG, "playSounds: failed to load ringtone from uri: "
1060-
+ soundUri);
1061-
}
1062-
} else {
1063-
if (DEBUG) Log.d(TAG, "playSounds: could not parse Uri: " + soundPath);
1064-
}
1065-
} else {
1066-
if (DEBUG) Log.d(TAG, "playSounds: whichSound = " + whichSound
1067-
+ "; soundPath was null");
1068-
}
1066+
final int whichSound = locked
1067+
? mLockSoundId
1068+
: mUnlockSoundId;
1069+
mLockSounds.stop(mLockSoundStreamId);
1070+
mLockSoundStreamId = mLockSounds.play(whichSound, 1.0f, 1.0f, 1, 0, 1.0f);
10691071
}
10701072
}
10711073

0 commit comments

Comments
 (0)