haptic feedback added

This commit is contained in:
serso
2011-10-26 23:56:45 +04:00
parent d58f0356f6
commit 311867dad7
11 changed files with 289 additions and 43 deletions

View File

@@ -27,6 +27,8 @@ import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
@@ -54,15 +56,21 @@ public class ColorButton extends Button {
private long animationStart;
private Paint feedbackPaint;
@NotNull
private final OnClickListenerVibrator onClickListener;
public ColorButton(Context context, AttributeSet attrs) {
this(context, attrs, true);
}
public ColorButton(Context context, AttributeSet attrs, boolean init) {
super(context, attrs);
if (init) {
init(context);
}
this.onClickListener = new OnClickListenerVibrator((Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE), PreferenceManager.getDefaultSharedPreferences(context));
}
protected void init(Context context) {
@@ -179,6 +187,22 @@ public class ColorButton extends Button {
invalidate();
}
@Override
public boolean performClick() {
vibrate();
return super.performClick();
}
@Override
public boolean performLongClick() {
vibrate();
return super.performLongClick();
}
private void vibrate() {
this.onClickListener.onClick(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean result = super.onTouchEvent(event);

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.view.widgets;
import android.content.SharedPreferences;
import android.os.Vibrator;
import android.view.View;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 10/26/11
* Time: 11:25 PM
*/
public class OnClickListenerVibrator implements View.OnClickListener {
private static final long VIBRATION_TIME = 100;
@NotNull
private VibratorContainer vibrator;
public OnClickListenerVibrator(@Nullable Vibrator vibrator,
@NotNull SharedPreferences preferences) {
this.vibrator = new VibratorContainer(vibrator, preferences, VIBRATION_TIME);
}
@Override
public void onClick(View v) {
vibrator.vibrate();
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.view.widgets;
import android.view.View;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 10/26/11
* Time: 10:55 PM
*/
public class OnClickListenerWrapper implements View.OnClickListener{
@NotNull
private final View.OnClickListener onClickListener;
public OnClickListenerWrapper(@NotNull View.OnClickListener onClickListener) {
this.onClickListener = onClickListener;
}
@Override
public void onClick(View v) {
this.onClick(v);
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.view.widgets;
import org.jetbrains.annotations.NotNull;
/**
* User: serso
* Date: 10/26/11
* Time: 10:37 PM
*/
public class OnDragListenerWrapper implements OnDragListener {
@NotNull
private final OnDragListener onDragListener;
public OnDragListenerWrapper(@NotNull OnDragListener onDragListener) {
this.onDragListener = onDragListener;
}
@Override
public boolean isSuppressOnClickEvent() {
return this.onDragListener.isSuppressOnClickEvent();
}
@Override
public boolean onDrag(@NotNull DragButton dragButton, @NotNull DragEvent event) {
return this.onDragListener.onDrag(dragButton, event);
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2009-2011. Created by serso aka se.solovyev.
* For more information, please, contact se.solovyev@gmail.com
* or visit http://se.solovyev.org
*/
package org.solovyev.android.view.widgets;
import android.content.SharedPreferences;
import android.os.Vibrator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* User: serso
* Date: 10/26/11
* Time: 11:40 PM
*/
public class VibratorContainer implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String HAPTIC_FEEDBACK_PREFERENCE = "org.solovyev.android.calculator.CalculatorModel_haptic_feedback";
private final long defaultVibrationTime;
@Nullable
private final Vibrator vibrator;
private long time = 0;
public VibratorContainer(@Nullable Vibrator vibrator, @NotNull SharedPreferences preferences, long defaultVibrationTime) {
this.vibrator = vibrator;
this.defaultVibrationTime = defaultVibrationTime;
preferences.registerOnSharedPreferenceChangeListener(this);
onSharedPreferenceChanged(preferences, null);
}
public void vibrate() {
if (time > 0 && vibrator != null) {
vibrator.vibrate(time);
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences preferences, @Nullable String key) {
if (preferences.getBoolean(HAPTIC_FEEDBACK_PREFERENCE, false)) {
this.time = defaultVibrationTime;
} else {
this.time = 0;
}
}
}