Android Open Source - datetimepicker-android Haptic Feedback Controller






From Project

Back to project page datetimepicker-android.

License

The source code is released under:

Apache License

If you think the Android project datetimepicker-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jp.seesaa.android.datetimepicker;
/*www.j av  a 2s  .  co m*/
import android.app.Service;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.SystemClock;
import android.os.Vibrator;
import android.provider.Settings;

/**
 * A simple utility class to handle haptic feedback.
 */
public class HapticFeedbackController {
    private static final int VIBRATE_DELAY_MS = 125;
    private static final int VIBRATE_LENGTH_MS = 5;

    private static boolean checkGlobalSetting(Context context) {
        return Settings.System.getInt(context.getContentResolver(),
                Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) == 1;
    }

    private final Context mContext;
    private final ContentObserver mContentObserver;

    private Vibrator mVibrator;
    private boolean mIsGloballyEnabled;
    private long mLastVibrate;

    public HapticFeedbackController(Context context) {
        mContext = context;
        mContentObserver = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                mIsGloballyEnabled = checkGlobalSetting(mContext);
            }
        };
    }

    /**
     * Call to setup the controller.
     */
    public void start() {
        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);
    }

    /**
     * Call this when you don't need the controller anymore.
     */
    public void stop() {
        mVibrator = null;
        mContext.getContentResolver().unregisterContentObserver(mContentObserver);
    }

    /**
     * Try to vibrate. To prevent this becoming a single continuous vibration, nothing will
     * happen if we have vibrated very recently.
     */
    public void tryVibrate() {
        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;
            }
        }
    }
}




Java Source Code List

jp.seesaa.android.datetimepicker.AccessibleLinearLayout.java
jp.seesaa.android.datetimepicker.AccessibleTextView.java
jp.seesaa.android.datetimepicker.HapticFeedbackController.java
jp.seesaa.android.datetimepicker.Utils.java
jp.seesaa.android.datetimepicker.date.AccessibleDateAnimator.java
jp.seesaa.android.datetimepicker.date.DatePickerController.java
jp.seesaa.android.datetimepicker.date.DatePickerDialog.java
jp.seesaa.android.datetimepicker.date.DayPickerView.java
jp.seesaa.android.datetimepicker.date.MonthAdapter.java
jp.seesaa.android.datetimepicker.date.MonthView.java
jp.seesaa.android.datetimepicker.date.SimpleDayPickerView.java
jp.seesaa.android.datetimepicker.date.SimpleMonthAdapter.java
jp.seesaa.android.datetimepicker.date.SimpleMonthView.java
jp.seesaa.android.datetimepicker.date.TextViewWithCircularIndicator.java
jp.seesaa.android.datetimepicker.date.YearPickerView.java
jp.seesaa.android.datetimepicker.time.AmPmCirclesView.java
jp.seesaa.android.datetimepicker.time.CircleView.java
jp.seesaa.android.datetimepicker.time.RadialPickerLayout.java
jp.seesaa.android.datetimepicker.time.RadialSelectorView.java
jp.seesaa.android.datetimepicker.time.RadialTextsView.java
jp.seesaa.android.datetimepicker.time.TimePickerDialog.java
jp.seesaa.example.datetimepicker.ApplicationTest.java
jp.seesaa.example.datetimepicker.MainActivity.java