1/*
2 * Copyright (C) 2009 The Android Open Source Project
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.android.phone.common;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.res.Resources;
22import android.os.Vibrator;
23import android.provider.Settings;
24import android.provider.Settings.System;
25import android.util.Log;
26
27/**
28 * Handles the haptic feedback: a light buzz happening when the user
29 * presses a soft key (UI button or capacitive key).  The haptic
30 * feedback is controlled by:
31 * - a system resource for the pattern
32 *   The pattern used is tuned per device and stored in an internal
33 *   resource (config_virtualKeyVibePattern.)
34 * - a system setting HAPTIC_FEEDBACK_ENABLED.
35 *   HAPTIC_FEEDBACK_ENABLED can be changed by the user using the
36 *   system Settings activity. It must be rechecked each time the
37 *   activity comes in the foreground (onResume).
38 *
39 * This class is not thread safe. It assumes it'll be called from the
40 * UI thead.
41 *
42 * Typical usage:
43 * --------------
44 *   static private final boolean HAPTIC_ENABLED = true;
45 *   private HapticFeedback mHaptic = new HapticFeedback();
46 *
47 *   protected void onCreate(Bundle icicle) {
48 *     mHaptic.init((Context)this, HAPTIC_ENABLED);
49 *   }
50 *
51 *   protected void onResume() {
52 *     // Refresh the system setting.
53 *     mHaptic.checkSystemSetting();
54 *   }
55 *
56 *   public void foo() {
57 *     mHaptic.vibrate();
58 *   }
59 *
60 */
61
62public class HapticFeedback {
63    /** If no pattern was found, vibrate for a small amount of time. */
64    private static final long DURATION = 10;  // millisec.
65    /** Play the haptic pattern only once. */
66    private static final int NO_REPEAT = -1;
67
68    private static final String TAG = "HapticFeedback";
69    private Context mContext;
70    private long[] mHapticPattern;
71    private Vibrator mVibrator;
72
73    private boolean mEnabled;
74    private Settings.System mSystemSettings;
75    private ContentResolver mContentResolver;
76    private boolean mSettingEnabled;
77
78    /**
79     * Initialize this instance using the app and system
80     * configs. Since these don't change, init is typically called
81     * once in 'onCreate'.
82     * checkSettings is not called during init.
83     * @param context To look up the resources and system settings.
84     * @param enabled If false, vibrate will be a no-op regardless of
85     * the system settings.
86     */
87    public void init(Context context, boolean enabled) {
88        mEnabled = enabled;
89        if (enabled) {
90            mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
91            mHapticPattern = new long[] {0, DURATION, 2 * DURATION, 3 * DURATION};
92            mSystemSettings = new Settings.System();
93            mContentResolver = context.getContentResolver();
94        }
95    }
96
97
98    /**
99     * Reload the system settings to check if the user enabled the
100     * haptic feedback.
101     */
102    public void checkSystemSetting() {
103        if (!mEnabled) {
104            return;
105        }
106        try {
107            int val = mSystemSettings.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 0);
108            mSettingEnabled = val != 0;
109        } catch (Resources.NotFoundException nfe) {
110            Log.e(TAG, "Could not retrieve system setting.", nfe);
111            mSettingEnabled = false;
112        }
113
114    }
115
116
117    /**
118     * Generate the haptic feedback vibration. Only one thread can
119     * request it. If the phone is already in a middle of an haptic
120     * feedback sequence, the request is ignored.
121     */
122    public void vibrate() {
123        if (!mEnabled || !mSettingEnabled) {
124            return;
125        }
126        // System-wide configuration may return different styles of haptic feedback pattern.
127        // - an array with one value implies "one-shot vibration"
128        // - an array with multiple values implies "pattern vibration"
129        // We need to switch methods to call depending on the difference.
130        // See also PhoneWindowManager#performHapticFeedbackLw() for another example.
131        if (mHapticPattern != null && mHapticPattern.length == 1) {
132            mVibrator.vibrate(mHapticPattern[0]);
133        } else {
134            mVibrator.vibrate(mHapticPattern, NO_REPEAT);
135        }
136    }
137}
138