@@ -147,8 +147,16 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
147147 */
148148 private static final boolean ENABLE_INSECURE_STATUS_BAR_EXPAND = true ;
149149
150+ /** The stream type that the lock sounds are tied to. */
151+ private static final int MASTER_STREAM_TYPE = AudioManager .STREAM_RING ;
152+ /** Minimum volume for lock sounds, as a ratio of max MASTER_STREAM_TYPE */
153+ final float MIN_LOCK_VOLUME = 0.05f ;
154+ /** Maximum volume for lock sounds, as a ratio of max MASTER_STREAM_TYPE */
155+ final float MAX_LOCK_VOLUME = 0.4f ;
156+
150157 private Context mContext ;
151158 private AlarmManager mAlarmManager ;
159+ private AudioManager mAudioManager ;
152160 private StatusBarManager mStatusBarManager ;
153161 private boolean mShowLockIcon ;
154162 private boolean mShowingLockIcon ;
@@ -255,6 +263,7 @@ public class KeyguardViewMediator implements KeyguardViewCallback,
255263 private int mLockSoundId ;
256264 private int mUnlockSoundId ;
257265 private int mLockSoundStreamId ;
266+ private int mMasterStreamMaxVolume ;
258267
259268 public KeyguardViewMediator (Context context , PhoneWindowManager callback ,
260269 LocalPowerManager powerManager ) {
@@ -1061,13 +1070,33 @@ private void playSounds(boolean locked) {
10611070 }
10621071
10631072 final ContentResolver cr = mContext .getContentResolver ();
1064- if (Settings .System .getInt (cr , Settings .System .LOCKSCREEN_SOUNDS_ENABLED , 1 ) == 1 )
1065- {
1073+ if (Settings .System .getInt (cr , Settings .System .LOCKSCREEN_SOUNDS_ENABLED , 1 ) == 1 ) {
10661074 final int whichSound = locked
10671075 ? mLockSoundId
10681076 : mUnlockSoundId ;
10691077 mLockSounds .stop (mLockSoundStreamId );
1070- mLockSoundStreamId = mLockSounds .play (whichSound , 1.0f , 1.0f , 1 , 0 , 1.0f );
1078+ // Init mAudioManager
1079+ if (mAudioManager == null ) {
1080+ mAudioManager = (AudioManager ) mContext .getSystemService (Context .AUDIO_SERVICE );
1081+ if (mAudioManager == null ) return ;
1082+ mMasterStreamMaxVolume = mAudioManager .getStreamMaxVolume (MASTER_STREAM_TYPE );
1083+ }
1084+ // If the stream is muted, don't play the sound
1085+ if (mAudioManager .isStreamMute (MASTER_STREAM_TYPE )) return ;
1086+
1087+ // Adjust the lock sound volume from a minimum of MIN_LOCK_VOLUME to a maximum
1088+ // of MAX_LOCK_VOLUME, relative to the maximum level of the MASTER_STREAM_TYPE volume.
1089+ float lockSoundVolume ;
1090+ int masterStreamVolume = mAudioManager .getStreamVolume (MASTER_STREAM_TYPE );
1091+ if (masterStreamVolume == 0 ) {
1092+ return ;
1093+ } else {
1094+ lockSoundVolume = MIN_LOCK_VOLUME + (MAX_LOCK_VOLUME - MIN_LOCK_VOLUME )
1095+ * ((float ) masterStreamVolume / mMasterStreamMaxVolume );
1096+ }
1097+
1098+ mLockSoundStreamId = mLockSounds .play (whichSound , lockSoundVolume , lockSoundVolume , 1 ,
1099+ 0 , 1.0f );
10711100 }
10721101 }
10731102
0 commit comments