AudioAndHapticFeedbackManager.java revision bdbb22bb3315bc22e1641fb8ecd150fc2de4a19d
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.latin; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.media.AudioManager; 23import android.view.HapticFeedbackConstants; 24 25import com.android.inputmethod.compat.VibratorCompatWrapper; 26import com.android.inputmethod.keyboard.Keyboard; 27import com.android.inputmethod.keyboard.KeyboardSwitcher; 28import com.android.inputmethod.keyboard.LatinKeyboardView; 29 30/** 31 * This class gathers audio feedback and haptic feedback functions. 32 * 33 * It offers a consistent and simple interface that allows LatinIME to forget about the 34 * complexity of settings and the like. 35 */ 36public class AudioAndHapticFeedbackManager extends BroadcastReceiver { 37 final private LatinIME mLatinIme; 38 final private SettingsValues mSettingsValues; 39 final private KeyboardSwitcher mKeyboardSwitcher; 40 final private AudioManager mAudioManager; 41 final private VibratorCompatWrapper mVibrator; 42 private boolean mSoundOn; 43 44 public AudioAndHapticFeedbackManager(final LatinIME latinIme, 45 final SettingsValues settingsValues, final KeyboardSwitcher keyboardSwitcher) { 46 mLatinIme = latinIme; 47 mSettingsValues = settingsValues; 48 mKeyboardSwitcher = keyboardSwitcher; 49 mVibrator = VibratorCompatWrapper.getInstance(mLatinIme); 50 mAudioManager = (AudioManager) mLatinIme.getSystemService(Context.AUDIO_SERVICE); 51 mSoundOn = reevaluateIfSoundIsOn(); 52 } 53 54 public void hapticAndAudioFeedback(final int primaryCode) { 55 vibrate(); 56 playKeyClick(primaryCode); 57 } 58 59 private boolean reevaluateIfSoundIsOn() { 60 if (!mSettingsValues.mSoundOn || mAudioManager == null) { 61 return false; 62 } else { 63 return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL; 64 } 65 } 66 67 private void playKeyClick(int primaryCode) { 68 // if mAudioManager is null, we can't play a sound anyway, so return 69 if (mAudioManager == null) return; 70 if (mSoundOn) { 71 final int sound; 72 switch (primaryCode) { 73 case Keyboard.CODE_DELETE: 74 sound = AudioManager.FX_KEYPRESS_DELETE; 75 break; 76 case Keyboard.CODE_ENTER: 77 sound = AudioManager.FX_KEYPRESS_RETURN; 78 break; 79 case Keyboard.CODE_SPACE: 80 sound = AudioManager.FX_KEYPRESS_SPACEBAR; 81 break; 82 default: 83 sound = AudioManager.FX_KEYPRESS_STANDARD; 84 break; 85 } 86 mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume); 87 } 88 } 89 90 // TODO: make this private when LatinIME does not call it any more 91 public void vibrate() { 92 if (!mSettingsValues.mVibrateOn) { 93 return; 94 } 95 if (mSettingsValues.mKeypressVibrationDuration < 0) { 96 // Go ahead with the system default 97 LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); 98 if (inputView != null) { 99 inputView.performHapticFeedback( 100 HapticFeedbackConstants.KEYBOARD_TAP, 101 HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING); 102 } 103 } else if (mVibrator != null) { 104 mVibrator.vibrate(mSettingsValues.mKeypressVibrationDuration); 105 } 106 } 107 108 @Override 109 public void onReceive(Context context, Intent intent) { 110 final String action = intent.getAction(); 111 // The following test is supposedly useless since we only listen for the ringer event. 112 // Still, it's a good safety measure. 113 if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) { 114 mSoundOn = reevaluateIfSoundIsOn(); 115 } 116 } 117} 118