• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.phone;
18 
19 import android.content.Context;
20 import android.content.ContentResolver;
21 import android.content.res.Resources;
22 import android.os.Vibrator;
23 import android.util.Log;
24 import android.provider.Settings;
25 import 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 
62 public 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      */
init(Context context, boolean enabled)89     public void init(Context context, boolean enabled) {
90         mEnabled = enabled;
91         if (enabled) {
92             mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
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      */
checkSystemSetting()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      */
vibrate()126     public void vibrate() {
127         if (!mEnabled || !mSettingEnabled) {
128             return;
129         }
130         // System-wide configuration may return different styles of haptic feedback pattern.
131         // - an array with one value implies "one-shot vibration"
132         // - an array with multiple values implies "pattern vibration"
133         // We need to switch methods to call depending on the difference.
134         // See also PhoneWindowManager#performHapticFeedbackLw() for another example.
135         if (mHapticPattern != null && mHapticPattern.length == 1) {
136             mVibrator.vibrate(mHapticPattern[0]);
137         } else {
138             mVibrator.vibrate(mHapticPattern, NO_REPEAT);
139         }
140     }
141 
142     /**
143      * @return true If the system haptic pattern was found.
144      */
loadHapticSystemPattern(Resources r)145     private boolean loadHapticSystemPattern(Resources r) {
146         int[] pattern;
147 
148         mHapticPattern = null;
149         try {
150             pattern = r.getIntArray(VIBRATION_PATTERN_ID);
151         } catch (Resources.NotFoundException nfe) {
152             Log.e(TAG, "Vibrate pattern missing.", nfe);
153             return false;
154         }
155 
156         if (null == pattern || pattern.length == 0) {
157             Log.e(TAG, "Haptic pattern is null or empty.");
158             return false;
159         }
160 
161         // int[] to long[] conversion.
162         mHapticPattern = new long[pattern.length];
163         for (int i = 0; i < pattern.length; i++) {
164             mHapticPattern[i] = pattern[i];
165         }
166         return true;
167     }
168 }
169