Skip to content

Commit ee1d541

Browse files
marconeAndroid (Google) Code Review
authored andcommitted
Merge "Use SoundPool instead of Ringtone."
2 parents 2d56123 + d5545bd commit ee1d541

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

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

Lines changed: 27 additions & 26 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,29 +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.setWakeMode(mContext, PowerManager.PARTIAL_WAKE_LOCK);
1058-
sfx.play();
1059-
} else {
1060-
if (DEBUG) Log.d(TAG, "playSounds: failed to load ringtone from uri: "
1061-
+ soundUri);
1062-
}
1063-
} else {
1064-
if (DEBUG) Log.d(TAG, "playSounds: could not parse Uri: " + soundPath);
1065-
}
1066-
} else {
1067-
if (DEBUG) Log.d(TAG, "playSounds: whichSound = " + whichSound
1068-
+ "; soundPath was null");
1069-
}
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);
10701071
}
10711072
}
10721073

0 commit comments

Comments
 (0)