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