PageRenderTime 36ms CodeModel.GetById 15ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ime/latinime/src/com/googlecode/eyesfree/inputmethod/FeedbackUtil.java

http://eyes-free.googlecode.com/
Java | 119 lines | 49 code | 17 blank | 53 comment | 5 complexity | 5816240b9813f8f2af653a3e7bf172ad MD5 | raw file
  1/*
  2 * Copyright (C) 2011 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of 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,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.googlecode.eyesfree.inputmethod;
 18
 19import android.content.Context;
 20import android.media.AudioManager;
 21import android.media.SoundPool;
 22import android.os.Vibrator;
 23
 24import java.util.TreeMap;
 25
 26/**
 27 * This class provides haptic and auditory feedback.
 28 *
 29 * @author alanv@google.com (Alan Viverette)
 30 */
 31public class FeedbackUtil {
 32    private boolean mVibrationEnabled = true;
 33    private boolean mSoundEnabled = true;
 34
 35    private final Context mContext;
 36    private final SoundPool mSoundPool;
 37    private final Vibrator mVibrator;
 38    private final TreeMap<Integer, Integer> mSoundMap;
 39
 40    /**
 41     * Constructs a new FeedbackUtil based on the specified parent
 42     * {@link Context}.
 43     *
 44     * @param context The parent {@link Context}.
 45     */
 46    public FeedbackUtil(Context context) {
 47        mContext = context;
 48        mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 1);
 49        mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
 50        mSoundMap = new TreeMap<Integer, Integer>();
 51    }
 52
 53    /**
 54     * Releases resources associated with this FeedbackUtil. It is good practice
 55     * to call this method when you're done using the FeedbackUtil.
 56     */
 57    public void release() {
 58        mSoundPool.release();
 59    }
 60
 61    /**
 62     * Plays a sound file identified by the given resource id.
 63     *
 64     * @param resId The resource id of the sound file.
 65     */
 66    public void playSound(int resId) {
 67        if (!mSoundEnabled)
 68            return;
 69
 70        Integer soundId = mSoundMap.get(resId);
 71
 72        if (soundId == null) {
 73            soundId = mSoundPool.load(mContext, resId, 1);
 74            mSoundMap.put(resId, soundId);
 75        }
 76
 77        mSoundPool.play(soundId, 1.0f, 1.0f, 1, 0, 1.0f);
 78    }
 79
 80    /**
 81     * Vibrates for the specified duration.
 82     *
 83     * @param milliseconds How long to vibrate for.
 84     */
 85    public void vibrate(long milliseconds) {
 86        if (!mVibrationEnabled)
 87            return;
 88
 89        mVibrator.vibrate(milliseconds);
 90    }
 91
 92    /**
 93     * Vibrates with a given pattern.
 94     *
 95     * @param pattern The vibration pattern as defined in
 96     *            {@link Vibrator#vibrate(long[], int)}.
 97     * @see Vibrator#vibrate(long[], int)
 98     */
 99    public void vibrate(long[] pattern) {
100        if (!mVibrationEnabled)
101            return;
102
103        mVibrator.vibrate(pattern, -1);
104    }
105
106    /**
107     * @param enabled {@code true} to enable vibration.
108     */
109    public void setVibrationEnabled(boolean enabled) {
110        mVibrationEnabled = enabled;
111    }
112
113    /**
114     * @param enabled {@code true} to enable sound.
115     */
116    public void setSoundEnabled(boolean enabled) {
117        mSoundEnabled = enabled;
118    }
119}