package kapil.voiceassistedweatherapp.customviews;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

import kapil.voiceassistedweatherapp.R;

/**
 * Created by kapil on 08-01-2017.
 *
 * This is a custom view for visual feedback during the Speech Recognizer is in Listening Mode.
 */
public class VoiceListeningView extends View {
    private float radius;
    private float circleAction;

    private Paint paint;
    private ValueAnimator radiusAnimator;
    private int animationDuration;

    public VoiceListeningView(Context context) {
        super(context);
        init();
    }

    public VoiceListeningView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public VoiceListeningView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        radius = pxToDp(30);
        circleAction = 20;       // px

        animationDuration = 800;        // milliseconds

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
        paint.setAlpha(99);

        radiusAnimator = new ValueAnimator();
        radiusAnimator.setFloatValues(radius, radius + circleAction, radius);
        radiusAnimator.setDuration(animationDuration);
        radiusAnimator.setRepeatMode(ValueAnimator.RESTART);
        radiusAnimator.setRepeatCount(ValueAnimator.INFINITE);
        radiusAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        radiusAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                radius = (float) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (radiusAnimator.isRunning()) {
            canvas.drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, radius, paint);
        }
    }

    public float pxToDp(float px) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, getResources().getDisplayMetrics());
    }

    public void startAnim() {
        if (!radiusAnimator.isRunning()) {
            radiusAnimator.start();
        }
    }

    public void pauseAnim() {
        if (radiusAnimator.isRunning()) {
            radiusAnimator.pause();
        }
    }

    public void endAnim() {
        if (radiusAnimator.isRunning()) {
            radiusAnimator.end();
        }
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        switch (visibility) {
            case VISIBLE:
                startAnim();
                break;
            case INVISIBLE:
                pauseAnim();
                break;
            case GONE:
                endAnim();
                break;
        }
    }
}

@@ -1199,19 +1199,36 @@
 s = 
-pxToDp(30);
+DpToPx(30);            // dp
 %0A   
@@ -1251,12 +1251,19 @@
 n = 
-20; 
+DpToPx(10);
     
@@ -1267,18 +1267,18 @@
      // 
+d
 p
-x
 %0A%0A      
@@ -2501,23 +2501,23 @@
 oat 
-pxToDp
+DpToPx
 (float 
+d
 p
-x
 ) %7B%0A
@@ -2590,10 +2590,10 @@
 IP, 
+d
 p
-x
 , ge
@@ -2947,32 +2947,32 @@
 .isRunning()) %7B%0A
+
             radi
@@ -3008,388 +3008,7 @@
    %7D
-%0A%0A    @Override%0A    public void setVisibility(int visibility) %7B%0A        super.setVisibility(visibility);%0A        switch (visibility) %7B%0A            case VISIBLE:%0A                startAnim();%0A                break;%0A            case INVISIBLE:%0A                pauseAnim();%0A                break;%0A            case GONE:%0A                endAnim();%0A                break;%0A        %7D%0A    %7D
 %0A%7D%0A

Tweak voice listening view dimensions