Home | History | Annotate | Download | only in common
      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.common;
     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     /** If no pattern was found, vibrate for a small amount of time. */
     65     private static final long DURATION = 10;  // millisec.
     66     /** Play the haptic pattern only once. */
     67     private static final int NO_REPEAT = -1;
     68 
     69     private static final String TAG = "HapticFeedback";
     70     private Context mContext;
     71     private long[] mHapticPattern;
     72     private Vibrator mVibrator;
     73 
     74     private boolean mEnabled;
     75     private Settings.System mSystemSettings;
     76     private ContentResolver mContentResolver;
     77     private boolean mSettingEnabled;
     78 
     79     /**
     80      * Initialize this instance using the app and system
     81      * configs. Since these don't change, init is typically called
     82      * once in 'onCreate'.
     83      * checkSettings is not called during init.
     84      * @param context To look up the resources and system settings.
     85      * @param enabled If false, vibrate will be a no-op regardless of
     86      * the system settings.
     87      */
     88     public void init(Context context, boolean enabled) {
     89         mEnabled = enabled;
     90         if (enabled) {
     91             // We don't rely on getSystemService(Context.VIBRATOR_SERVICE) to make sure this
     92             // vibrator object will be isolated from others.
     93             mVibrator = new SystemVibrator(context);
     94             mHapticPattern = new long[] {0, DURATION, 2 * DURATION, 3 * DURATION};
     95             mSystemSettings = new Settings.System();
     96             mContentResolver = context.getContentResolver();
     97         }
     98     }
     99 
    100 
    101     /**
    102      * Reload the system settings to check if the user enabled the
    103      * haptic feedback.
    104      */
    105     public void checkSystemSetting() {
    106         if (!mEnabled) {
    107             return;
    108         }
    109         try {
    110             int val = mSystemSettings.getInt(mContentResolver, System.HAPTIC_FEEDBACK_ENABLED, 0);
    111             mSettingEnabled = val != 0;
    112         } catch (Resources.NotFoundException nfe) {
    113             Log.e(TAG, "Could not retrieve system setting.", nfe);
    114             mSettingEnabled = false;
    115         }
    116 
    117     }
    118 
    119 
    120     /**
    121      * Generate the haptic feedback vibration. Only one thread can
    122      * request it. If the phone is already in a middle of an haptic
    123      * feedback sequence, the request is ignored.
    124      */
    125     public void vibrate() {
    126         if (!mEnabled || !mSettingEnabled) {
    127             return;
    128         }
    129         // System-wide configuration may return different styles of haptic feedback pattern.
    130         // - an array with one value implies "one-shot vibration"
    131         // - an array with multiple values implies "pattern vibration"
    132         // We need to switch methods to call depending on the difference.
    133         // See also PhoneWindowManager#performHapticFeedbackLw() for another example.
    134         if (mHapticPattern != null && mHapticPattern.length == 1) {
    135             mVibrator.vibrate(mHapticPattern[0]);
    136         } else {
    137             mVibrator.vibrate(mHapticPattern, NO_REPEAT);
    138         }
    139     }
    140 }
    141