package es.flakiness.hiccup.play;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;

public class PlayInteractionPreso implements Subscription {
    private final CompositeSubscription subscriptions = new CompositeSubscription();
    private final int originalColor;

    static private int darken(int color, float level) {
        float hsv[] = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] = hsv[2]* level;
        return Color.HSVToColor(hsv);
    }

    public PlayInteractionPreso(final View view, Observable<GestureEvent> gestures) {
        originalColor = ((ColorDrawable) view.getBackground()).getColor();
        subscriptions.add(gestures.subscribe(new Action1<GestureEvent>() {
            @Override
            public void call(GestureEvent gestureEvent) {
                switch (gestureEvent.getType()) {
                    case DOWN:
                        Log.d(getClass().getSimpleName(), view.getBackground().toString());
                        view.setBackgroundColor(darken(originalColor, 0.9f));
                        break;
                    case HOLD:
                        ((Vibrator)view.getContext().getSystemService(Context.VIBRATOR_SERVICE)).vibrate(100);
                        break;
                    case UP:
                        view.setBackgroundColor(originalColor);
                        break;
                    default:
                        break;
                }
            }
        }));
    }

    @Override
    public void unsubscribe() {
        subscriptions.unsubscribe();
    }

    @Override
    public boolean isUnsubscribed() {
        return subscriptions.isUnsubscribed();
    }
}

@@ -28,16 +28,96 @@
 .play;%0A%0A
+import android.animation.ArgbEvaluator;%0Aimport android.animation.ValueAnimator;%0A
 import a
@@ -589,23 +589,108 @@
 nal 
-int originalCol
+View backgroundView;%0A    private final int originalColor;%0A    private ValueAnimator backgroundAnimat
 or;%0A
@@ -893,24 +893,740 @@
 sv);%0A    %7D%0A%0A
+    private int getBackgroundColor() %7B%0A        return ((ColorDrawable) backgroundView.getBackground()).getColor();%0A    %7D%0A%0A    private void animateBackgroundTo(int toColor) %7B%0A        if (backgroundAnimator != null)%0A            backgroundAnimator.cancel();%0A        backgroundAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), getBackgroundColor(), toColor).setDuration(50);%0A        backgroundAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() %7B%0A            @Override%0A            public void onAnimationUpdate(ValueAnimator animation) %7B%0A                backgroundView.setBackgroundColor((Integer)animation.getAnimatedValue());%0A            %7D%0A        %7D);%0A        backgroundAnimator.start();%0A    %7D%0A%0A
     public P
@@ -1711,46 +1711,55 @@
     
-originalColor = ((ColorDrawable) view.
+backgroundView = view;%0A        originalColor = 
 getB
@@ -1759,39 +1759,32 @@
  = getBackground
-()).get
 Color();%0A       
@@ -2124,32 +2124,31 @@
             
-view.set
+animate
 BackgroundCo
@@ -2145,21 +2145,18 @@
 ckground
-Color
+To
 (darken(
@@ -2439,16 +2439,15 @@
     
-view.set
+animate
 Back
@@ -2452,21 +2452,18 @@
 ckground
-Color
+To
 (origina

Make background color change animated.