|
31 | 31 | import android.content.Intent; |
32 | 32 | import android.content.IntentFilter; |
33 | 33 | import android.media.AudioManager; |
34 | | -import android.media.Ringtone; |
35 | | -import android.media.RingtoneManager; |
36 | | -import android.net.Uri; |
| 34 | +import android.media.SoundPool; |
37 | 35 | import android.os.Handler; |
38 | 36 | import android.os.LocalPowerManager; |
39 | 37 | import android.os.Message; |
@@ -253,6 +251,11 @@ public class KeyguardViewMediator implements KeyguardViewCallback, |
253 | 251 | private boolean mWaitingUntilKeyguardVisible = false; |
254 | 252 | private LockPatternUtils mLockPatternUtils; |
255 | 253 |
|
| 254 | + private SoundPool mLockSounds; |
| 255 | + private int mLockSoundId; |
| 256 | + private int mUnlockSoundId; |
| 257 | + private int mLockSoundStreamId; |
| 258 | + |
256 | 259 | public KeyguardViewMediator(Context context, PhoneWindowManager callback, |
257 | 260 | LocalPowerManager powerManager) { |
258 | 261 | mContext = context; |
@@ -298,6 +301,22 @@ public KeyguardViewMediator(Context context, PhoneWindowManager callback, |
298 | 301 |
|
299 | 302 | final ContentResolver cr = mContext.getContentResolver(); |
300 | 303 | 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 | + } |
301 | 320 | } |
302 | 321 |
|
303 | 322 | /** |
@@ -1044,28 +1063,11 @@ private void playSounds(boolean locked) { |
1044 | 1063 | final ContentResolver cr = mContext.getContentResolver(); |
1045 | 1064 | if (Settings.System.getInt(cr, Settings.System.LOCKSCREEN_SOUNDS_ENABLED, 1) == 1) |
1046 | 1065 | { |
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); |
1069 | 1071 | } |
1070 | 1072 | } |
1071 | 1073 |
|
|
0 commit comments