2012-03-06 10:00:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-03-06 10:00:23 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-03-06 10:00:23 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-03-06 10:00:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2013-07-22 03:43:37 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValues;
|
|
|
|
|
2012-03-06 10:00:23 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.media.AudioManager;
|
2013-01-08 05:58:08 +00:00
|
|
|
import android.os.Vibrator;
|
2012-03-06 10:00:23 +00:00
|
|
|
import android.view.HapticFeedbackConstants;
|
2012-03-07 04:11:58 +00:00
|
|
|
import android.view.View;
|
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 {
|
2013-01-08 05:58:08 +00:00
|
|
|
private AudioManager mAudioManager;
|
|
|
|
private Vibrator mVibrator;
|
2012-10-15 20:59:35 +00:00
|
|
|
|
|
|
|
private SettingsValues mSettingsValues;
|
2012-03-07 03:40:20 +00:00
|
|
|
private boolean mSoundOn;
|
2012-03-06 10:00:23 +00:00
|
|
|
|
2013-01-08 05:58:08 +00:00
|
|
|
private static final AudioAndHapticFeedbackManager sInstance =
|
|
|
|
new AudioAndHapticFeedbackManager();
|
|
|
|
|
|
|
|
public static AudioAndHapticFeedbackManager getInstance() {
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
private AudioAndHapticFeedbackManager() {
|
|
|
|
// Intentional empty constructor for singleton.
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void init(final Context context) {
|
|
|
|
sInstance.initInternal(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initInternal(final Context context) {
|
|
|
|
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
|
|
|
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 03:10:26 +00:00
|
|
|
public void performHapticAndAudioFeedback(final int code,
|
2012-03-07 04:11:58 +00:00
|
|
|
final View viewToPerformHapticFeedbackOn) {
|
2013-08-13 03:10:26 +00:00
|
|
|
performHapticFeedback(viewToPerformHapticFeedbackOn);
|
|
|
|
performAudioFeedback(code);
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 05:58:08 +00:00
|
|
|
public boolean hasVibrator() {
|
|
|
|
return mVibrator != null && mVibrator.hasVibrator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void vibrate(final long milliseconds) {
|
|
|
|
if (mVibrator == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mVibrator.vibrate(milliseconds);
|
|
|
|
}
|
|
|
|
|
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-06 10:00:23 +00:00
|
|
|
}
|
2013-01-08 05:58:08 +00:00
|
|
|
return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-13 03:10:26 +00:00
|
|
|
public void performAudioFeedback(final int code) {
|
2012-03-07 03:39:09 +00:00
|
|
|
// if mAudioManager is null, we can't play a sound anyway, so return
|
2013-01-08 05:58:08 +00:00
|
|
|
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;
|
2013-08-13 03:10:26 +00:00
|
|
|
switch (code) {
|
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;
|
|
|
|
}
|
2013-01-08 09:20:31 +00:00
|
|
|
mAudioManager.playSoundEffect(sound, mSettingsValues.mKeypressSoundVolume);
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 03:10:26 +00:00
|
|
|
public void performHapticFeedback(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);
|
|
|
|
}
|
2013-01-08 05:58:08 +00:00
|
|
|
return;
|
2012-03-06 10:00:23 +00:00
|
|
|
}
|
2013-01-08 05:58:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|