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
|
|
|
|
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-10-15 20:59:35 +00:00
|
|
|
private final AudioManager mAudioManager;
|
|
|
|
private final VibratorUtils mVibratorUtils;
|
|
|
|
|
|
|
|
private SettingsValues mSettingsValues;
|
2012-03-07 03:40:20 +00:00
|
|
|
private boolean mSoundOn;
|
2012-03-06 10:00:23 +00:00
|
|
|
|
2012-10-15 20:59:35 +00:00
|
|
|
public AudioAndHapticFeedbackManager(final LatinIME latinIme) {
|
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-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-10-15 20:59:35 +00:00
|
|
|
if (mSettingsValues == null || !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) {
|
2012-10-29 05:46:34 +00:00
|
|
|
case Constants.CODE_DELETE:
|
2012-03-06 10:00:23 +00:00
|
|
|
sound = AudioManager.FX_KEYPRESS_DELETE;
|
|
|
|
break;
|
2012-10-29 05:46:34 +00:00
|
|
|
case Constants.CODE_ENTER:
|
2012-03-06 10:00:23 +00:00
|
|
|
sound = AudioManager.FX_KEYPRESS_RETURN;
|
|
|
|
break;
|
2012-10-29 05:46:34 +00:00
|
|
|
case Constants.CODE_SPACE:
|
2012-03-06 10:00:23 +00:00
|
|
|
sound = AudioManager.FX_KEYPRESS_SPACEBAR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sound = AudioManager.FX_KEYPRESS_STANDARD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 20:59:35 +00:00
|
|
|
private 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-10-15 20:59:35 +00:00
|
|
|
public void onSettingsChanged(final SettingsValues settingsValues) {
|
|
|
|
mSettingsValues = settingsValues;
|
|
|
|
mSoundOn = reevaluateIfSoundIsOn();
|
|
|
|
}
|
|
|
|
|
2012-03-09 02:00:23 +00:00
|
|
|
public void onRingerModeChanged() {
|
|
|
|
mSoundOn = reevaluateIfSoundIsOn();
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
}
|