Home | History | Annotate | Download | only in latin
      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 
     17 package com.android.inputmethod.latin;
     18 
     19 import android.content.Context;
     20 import android.media.AudioManager;
     21 import android.view.HapticFeedbackConstants;
     22 import android.view.View;
     23 
     24 import com.android.inputmethod.keyboard.Keyboard;
     25 import com.android.inputmethod.latin.VibratorUtils;
     26 
     27 /**
     28  * This class gathers audio feedback and haptic feedback functions.
     29  *
     30  * It offers a consistent and simple interface that allows LatinIME to forget about the
     31  * complexity of settings and the like.
     32  */
     33 public class AudioAndHapticFeedbackManager {
     34     final private SettingsValues mSettingsValues;
     35     final private AudioManager mAudioManager;
     36     final private VibratorUtils mVibratorUtils;
     37     private boolean mSoundOn;
     38 
     39     public AudioAndHapticFeedbackManager(final LatinIME latinIme,
     40             final SettingsValues settingsValues) {
     41         mSettingsValues = settingsValues;
     42         mVibratorUtils = VibratorUtils.getInstance(latinIme);
     43         mAudioManager = (AudioManager) latinIme.getSystemService(Context.AUDIO_SERVICE);
     44         mSoundOn = reevaluateIfSoundIsOn();
     45     }
     46 
     47     public void hapticAndAudioFeedback(final int primaryCode,
     48             final View viewToPerformHapticFeedbackOn) {
     49         vibrate(viewToPerformHapticFeedbackOn);
     50         playKeyClick(primaryCode);
     51     }
     52 
     53     private boolean reevaluateIfSoundIsOn() {
     54         if (!mSettingsValues.mSoundOn || mAudioManager == null) {
     55             return false;
     56         } else {
     57             return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
     58         }
     59     }
     60 
     61     private void playKeyClick(int primaryCode) {
     62         // if mAudioManager is null, we can't play a sound anyway, so return
     63         if (mAudioManager == null) return;
     64         if (mSoundOn) {
     65             final int sound;
     66             switch (primaryCode) {
     67             case Keyboard.CODE_DELETE:
     68                 sound = AudioManager.FX_KEYPRESS_DELETE;
     69                 break;
     70             case Keyboard.CODE_ENTER:
     71                 sound = AudioManager.FX_KEYPRESS_RETURN;
     72                 break;
     73             case Keyboard.CODE_SPACE:
     74                 sound = AudioManager.FX_KEYPRESS_SPACEBAR;
     75                 break;
     76             default:
     77                 sound = AudioManager.FX_KEYPRESS_STANDARD;
     78                 break;
     79             }
     80             mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume);
     81         }
     82     }
     83 
     84     // TODO: make this private when LatinIME does not call it any more
     85     public void vibrate(final View viewToPerformHapticFeedbackOn) {
     86         if (!mSettingsValues.mVibrateOn) {
     87             return;
     88         }
     89         if (mSettingsValues.mKeypressVibrationDuration < 0) {
     90             // Go ahead with the system default
     91             if (viewToPerformHapticFeedbackOn != null) {
     92                 viewToPerformHapticFeedbackOn.performHapticFeedback(
     93                         HapticFeedbackConstants.KEYBOARD_TAP,
     94                         HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
     95             }
     96         } else if (mVibratorUtils != null) {
     97             mVibratorUtils.vibrate(mSettingsValues.mKeypressVibrationDuration);
     98         }
     99     }
    100 
    101     public void onRingerModeChanged() {
    102         mSoundOn = reevaluateIfSoundIsOn();
    103     }
    104 }
    105