Simplify audio management (A2)

There is little point in deferring loading for this. Getting the
service at startup time is not very expensive anyway, plus we
can avoid getting it in a later change if sound is off in settings.

Change-Id: I08d65467f245fa2f9f617eb23257c327b3115673
This commit is contained in:
Jean Chalard 2012-03-07 12:39:09 +09:00
parent 564496bad6
commit 793791cdf0

View file

@ -37,7 +37,7 @@ public class AudioAndHapticFeedbackManager extends BroadcastReceiver {
final private LatinIME mLatinIme; final private LatinIME mLatinIme;
final private SettingsValues mSettingsValues; final private SettingsValues mSettingsValues;
final private KeyboardSwitcher mKeyboardSwitcher; final private KeyboardSwitcher mKeyboardSwitcher;
private AudioManager mAudioManager; final private AudioManager mAudioManager;
final private VibratorCompatWrapper mVibrator; final private VibratorCompatWrapper mVibrator;
private boolean mSilentModeOn; private boolean mSilentModeOn;
@ -48,6 +48,8 @@ public class AudioAndHapticFeedbackManager extends BroadcastReceiver {
mKeyboardSwitcher = keyboardSwitcher; mKeyboardSwitcher = keyboardSwitcher;
mSilentModeOn = true; mSilentModeOn = true;
mVibrator = VibratorCompatWrapper.getInstance(mLatinIme); mVibrator = VibratorCompatWrapper.getInstance(mLatinIme);
mAudioManager = (AudioManager) mLatinIme.getSystemService(Context.AUDIO_SERVICE);
updateRingerMode();
} }
public void hapticAndAudioFeedback(final int primaryCode) { public void hapticAndAudioFeedback(final int primaryCode) {
@ -61,21 +63,16 @@ public class AudioAndHapticFeedbackManager extends BroadcastReceiver {
// update flags for silent mode // update flags for silent mode
private void updateRingerMode() { private void updateRingerMode() {
if (mAudioManager == null) { if (!mSettingsValues.mSoundOn || mAudioManager == null) {
mAudioManager = (AudioManager) mLatinIme.getSystemService(Context.AUDIO_SERVICE); mSilentModeOn = true;
if (mAudioManager == null) return; } else {
mSilentModeOn = (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL);
} }
mSilentModeOn = (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL);
} }
private void playKeyClick(int primaryCode) { private void playKeyClick(int primaryCode) {
// if mAudioManager is null, we don't have the ringer state yet // if mAudioManager is null, we can't play a sound anyway, so return
// mAudioManager will be set by updateRingerMode if (mAudioManager == null) return;
if (mAudioManager == null) {
if (mKeyboardSwitcher.getKeyboardView() != null) {
updateRingerMode();
}
}
if (isSoundOn()) { if (isSoundOn()) {
final int sound; final int sound;
switch (primaryCode) { switch (primaryCode) {