FileDocCategorySizeDatePackage
HapticFeedbackController.javaAPI DocAndroid 5.1 API2419Thu Mar 12 22:22:50 GMT 2015com.android.datetimepicker

HapticFeedbackController

public class HapticFeedbackController extends Object
A simple utility class to handle haptic feedback.

Fields Summary
private static final int
VIBRATE_DELAY_MS
private static final int
VIBRATE_LENGTH_MS
private final android.content.Context
mContext
private final android.database.ContentObserver
mContentObserver
private android.os.Vibrator
mVibrator
private boolean
mIsGloballyEnabled
private long
mLastVibrate
Constructors Summary
public HapticFeedbackController(android.content.Context context)

        mContext = context;
        mContentObserver = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                mIsGloballyEnabled = checkGlobalSetting(mContext);
            }
        };
    
Methods Summary
private static booleancheckGlobalSetting(android.content.Context context)


         
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
    
public voidstart()
Call to setup the controller.

        mVibrator = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);

        // Setup a listener for changes in haptic feedback settings
        mIsGloballyEnabled = checkGlobalSetting(mContext);
        Uri uri = Settings.System.getUriFor(Settings.System.HAPTIC_FEEDBACK_ENABLED);
        mContext.getContentResolver().registerContentObserver(uri, false, mContentObserver);
    
public voidstop()
Call this when you don't need the controller anymore.

        mVibrator = null;
        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
    
public voidtryVibrate()
Try to vibrate. To prevent this becoming a single continuous vibration, nothing will happen if we have vibrated very recently.

        if (mVibrator != null && mIsGloballyEnabled) {
            long now = SystemClock.uptimeMillis();
            // We want to try to vibrate each individual tick discretely.
            if (now - mLastVibrate >= VIBRATE_DELAY_MS) {
                mVibrator.vibrate(VIBRATE_LENGTH_MS);
                mLastVibrate = now;
            }
        }