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;
18
19import android.content.Context;
20import android.content.ContentResolver;
21import android.content.res.Resources;
22import android.os.Vibrator;
23import android.util.Log;
24import android.provider.Settings;
25import android.provider.Settings.System;
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    private static final int VIBRATION_PATTERN_ID =
64            com.android.internal.R.array.config_virtualKeyVibePattern;
65    /** If no pattern was found, vibrate for a small amount of time. */
66    private static final long DURATION = 10;  // millisec.
67    /** Play the haptic pattern only once. */
68    private static final int NO_REPEAT = -1;
69
70    private static final String TAG = "HapticFeedback";
71    private Context mContext;
72    private long[] mHapticPattern;
73    private Vibrator mVibrator;
74
75    private boolean mEnabled;
76    private Settings.System mSystemSettings;
77    private ContentResolver mContentResolver;
78    private boolean mSettingEnabled;
79
80    /**
81     * Initialize this instance using the app and system
82     * configs. Since these don't change, init is typically called
83     * once in 'onCreate'.
84     * checkSettings is not called during init.
85     * @param context To look up the resources and system settings.
86     * @param enabled If false, vibrate will be a no-op regardless of
87     * the system settings.
88     */
89    public void init(Context context, boolean enabled) {
90        mEnabled = enabled;
91        if (enabled) {
92            mVibrator = new Vibrator();
93            if (!loadHapticSystemPattern(context.getResources())) {
94                mHapticPattern = new long[] {0, DURATION, 2 * DURATION, 3 * DURATION};
95            }
96            mSystemSettings = new Settings.System();
97            mContentResolver = context.getContentResolver();
98        }
99    }
100
101
102    /**
103     * Reload the system settings to check if the user enabled the
104     * haptic feedback.
105     */
106    public void checkSystemSetting() {
107        if (!mEnabled) {
108            return;
109        }
110        try {
111            int val = mSystemSettings.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 0);
112            mSettingEnabled = val != 0;
113        } catch (Resources.NotFoundException nfe) {
114            Log.e(TAG, "Could not retrieve system setting.", nfe);
115            mSettingEnabled = false;
116        }
117
118    }
119
120
121    /**
122     * Generate the haptic feedback vibration. Only one thread can
123     * request it. If the phone is already in a middle of an haptic
124     * feedback sequence, the request is ignored.
125     */
126    public void vibrate() {
127        if (!mEnabled || !mSettingEnabled) {
128            return;
129        }
130        mVibrator.vibrate(mHapticPattern, NO_REPEAT);
131    }
132
133    /**
134     * @return true If the system haptic pattern was found.
135     */
136    private boolean loadHapticSystemPattern(Resources r) {
137        int[] pattern;
138
139        mHapticPattern = null;
140        try {
141            pattern = r.getIntArray(VIBRATION_PATTERN_ID);
142        } catch (Resources.NotFoundException nfe) {
143            Log.e(TAG, "Vibrate pattern missing.", nfe);
144            return false;
145        }
146
147        if (null == pattern || pattern.length == 0) {
148            Log.e(TAG, "Haptic pattern is null or empty.");
149            return false;
150        }
151
152        // int[] to long[] conversion.
153        mHapticPattern = new long[pattern.length];
154        for (int i = 0; i < pattern.length; i++) {
155            mHapticPattern[i] = pattern[i];
156        }
157        return true;
158    }
159}
160