2012-03-06 10:00:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.view.HapticFeedbackConstants;
|
2012-03-07 04:11:58 +00:00
|
|
|
import android.view.View;
|
2012-03-06 10:00:23 +00:00
|
|
|
|
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
2012-03-30 09:47:24 +00:00
|
|
|
import com.android.inputmethod.latin.VibratorUtils;
|
2012-03-06 10:00:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class gathers audio feedback and haptic feedback functions.
|
|
|
|
*
|
|
|
|
* It offers a consistent and simple interface that allows LatinIME to forget about the
|
|
|
|
* complexity of settings and the like.
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class AudioAndHapticFeedbackManager {
|
2012-03-06 10:00:23 +00:00
|
|
|
final private SettingsValues mSettingsValues;
|
2012-03-07 03:39:09 +00:00
|
|
|
final private AudioManager mAudioManager;
|
2012-03-30 09:47:24 +00:00
|
|
|
final private VibratorUtils mVibratorUtils;
|
2012-03-07 03:40:20 +00:00
|
|
|
private boolean mSoundOn;
|
2012-03-06 10:00:23 +00:00
|
|
|
|
|
|
|
public AudioAndHapticFeedbackManager(final LatinIME latinIme,
|
2012-03-07 04:11:58 +00:00
|
|
|
final SettingsValues settingsValues) {
|
2012-03-06 10:00:23 +00:00
|
|
|
mSettingsValues = settingsValues;
|
2012-03-30 09:47:24 +00:00
|
|
|
mVibratorUtils = VibratorUtils.getInstance(latinIme);
|
2012-03-07 03:44:31 +00:00
|
|
|
mAudioManager = (AudioManager) latinIme.getSystemService(Context.AUDIO_SERVICE);
|
2012-03-07 03:43:20 +00:00
|
|
|
mSoundOn = reevaluateIfSoundIsOn();
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 04:11:58 +00:00
|
|
|
public void hapticAndAudioFeedback(final int primaryCode,
|
|
|
|
final View viewToPerformHapticFeedbackOn) {
|
|
|
|
vibrate(viewToPerformHapticFeedbackOn);
|
2012-03-06 10:00:23 +00:00
|
|
|
playKeyClick(primaryCode);
|
|
|
|
}
|
|
|
|
|
2012-03-07 03:43:20 +00:00
|
|
|
private boolean reevaluateIfSoundIsOn() {
|
2012-03-07 03:39:09 +00:00
|
|
|
if (!mSettingsValues.mSoundOn || mAudioManager == null) {
|
2012-03-07 03:43:20 +00:00
|
|
|
return false;
|
2012-03-07 03:39:09 +00:00
|
|
|
} else {
|
2012-03-07 03:43:20 +00:00
|
|
|
return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void playKeyClick(int primaryCode) {
|
2012-03-07 03:39:09 +00:00
|
|
|
// if mAudioManager is null, we can't play a sound anyway, so return
|
|
|
|
if (mAudioManager == null) return;
|
2012-03-07 03:43:20 +00:00
|
|
|
if (mSoundOn) {
|
2012-03-06 10:00:23 +00:00
|
|
|
final int sound;
|
|
|
|
switch (primaryCode) {
|
|
|
|
case Keyboard.CODE_DELETE:
|
|
|
|
sound = AudioManager.FX_KEYPRESS_DELETE;
|
|
|
|
break;
|
|
|
|
case Keyboard.CODE_ENTER:
|
|
|
|
sound = AudioManager.FX_KEYPRESS_RETURN;
|
|
|
|
break;
|
|
|
|
case Keyboard.CODE_SPACE:
|
|
|
|
sound = AudioManager.FX_KEYPRESS_SPACEBAR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sound = AudioManager.FX_KEYPRESS_STANDARD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this private when LatinIME does not call it any more
|
2012-03-07 04:11:58 +00:00
|
|
|
public void vibrate(final View viewToPerformHapticFeedbackOn) {
|
2012-03-06 10:00:23 +00:00
|
|
|
if (!mSettingsValues.mVibrateOn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mSettingsValues.mKeypressVibrationDuration < 0) {
|
|
|
|
// Go ahead with the system default
|
2012-03-07 04:11:58 +00:00
|
|
|
if (viewToPerformHapticFeedbackOn != null) {
|
|
|
|
viewToPerformHapticFeedbackOn.performHapticFeedback(
|
2012-03-06 10:00:23 +00:00
|
|
|
HapticFeedbackConstants.KEYBOARD_TAP,
|
|
|
|
HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
|
|
|
|
}
|
2012-03-30 09:47:24 +00:00
|
|
|
} else if (mVibratorUtils != null) {
|
|
|
|
mVibratorUtils.vibrate(mSettingsValues.mKeypressVibrationDuration);
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:00:23 +00:00
|
|
|
public void onRingerModeChanged() {
|
|
|
|
mSoundOn = reevaluateIfSoundIsOn();
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
}
|