package com.example.android.io2014;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Outline;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.transition.Transition;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewOutlineProvider;
import android.view.WindowInsets;
import android.widget.ImageView;

import com.example.android.io2014.ui.TransitionListener;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class DetailActivityL extends AbstractDetailActivity {

    @Override
    public void postCreate() {
        setOutlines(R.id.star_button, R.id.info_button);
        applySystemWindowsBottomInset();
    }

    @Override
    public void mapLoaded(Bitmap bitmap) {

    }

    private void setOutlines(int star, int info) {
        final int size = getResources().getDimensionPixelSize(R.dimen.floating_button_size);

        final ViewOutlineProvider vop = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, size, size);
            }
        };

        findViewById(star).setOutlineProvider(vop);
        findViewById(info).setOutlineProvider(vop);
    }

    private void applySystemWindowsBottomInset() {
        container.setFitsSystemWindows(true);
        container.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
            @Override
            public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets) {
                DisplayMetrics metrics = getResources().getDisplayMetrics();
                if (metrics.widthPixels < metrics.heightPixels) {
                    view.setPadding(0, 0, 0, windowInsets.getSystemWindowInsetBottom());
                } else {
                    view.setPadding(0, 0, windowInsets.getSystemWindowInsetRight(), 0);
                }
                return windowInsets.consumeSystemWindowInsets();
            }
        });
    }

    @Override
    public void colorButton(int id, int bgColor, int tintColor) {
        View buttonView = findViewById(id);

        RippleDrawable ripple = (RippleDrawable) buttonView.getBackground();
        GradientDrawable rippleBackground = (GradientDrawable) ripple.getDrawable(0);
        rippleBackground.setColor(bgColor);

        ripple.setColor(ColorStateList.valueOf(tintColor));
    }

    public void toggleInformationView(View view) {
        final View infoContainer = findViewById(R.id.information_container);

        int cx = (view.getLeft() + view.getRight()) / 2;
        int cy = (view.getTop() + view.getBottom()) / 2;
        float radius = Math.max(infoContainer.getWidth(), infoContainer.getHeight()) * 2.0f;

        if (infoContainer.getVisibility() == View.INVISIBLE) {
            infoContainer.setVisibility(View.VISIBLE);
            ViewAnimationUtils.createCircularReveal(infoContainer, cx, cy, 0, radius).start();
        } else {
            Animator reveal = ViewAnimationUtils.createCircularReveal(
                    infoContainer, cx, cy, radius, 0);
            reveal.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    infoContainer.setVisibility(View.INVISIBLE);
                }
            });
            reveal.start();
        }
    }

    @Override
    public void setupEnterAnimation() {
        getWindow().getEnterTransition().addListener(new TransitionListener() {
            @Override
            public void onTransitionEnd(Transition transition) {
                ImageView hero = (ImageView) findViewById(R.id.photo);
                ObjectAnimator color = ObjectAnimator.ofArgb(hero.getColorFilter(), "color", 0);
                color.addUpdateListener(new ColorFilterListener(hero));
                color.start();

                findViewById(R.id.info_button).animate().alpha(1.0f);
                findViewById(R.id.star_button).animate().alpha(1.0f);

                getWindow().getEnterTransition().removeListener(this);
            }
        });
    }

    @Override
    public void setupExitAnimation() {
        ObjectAnimator color = ObjectAnimator.ofArgb(hero.getColorFilter(), "color",
                getResources().getColor(R.color.photo_tint));
        color.addUpdateListener(new ColorFilterListener(hero));
        color.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                finishAfterTransition();
            }
        });
        color.start();

        findViewById(R.id.info_button).animate().alpha(0.0f);
        findViewById(R.id.star_button).animate().alpha(0.0f);
    }

    private static class ColorFilterListener implements ValueAnimator.AnimatorUpdateListener {
        private final ImageView mHero;

        ColorFilterListener(ImageView hero) {
            mHero = hero;
        }

        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            mHero.getDrawable().setColorFilter(mHero.getColorFilter());
        }
    }
}
@@ -140,26 +140,25 @@
 imation.
-ObjectAnim
+ArgbEvalu
 ator;%0Aim
@@ -4032,38 +4032,118 @@
                 
-Object
+Integer colorFrom = getResources().getColor(R.color.photo_tint);%0A                Value
 Animator color =
@@ -4135,38 +4135,37 @@
 nimator color = 
-Object
+Value
 Animator.ofArgb(
@@ -4163,43 +4163,45 @@
 r.of
-Argb(hero.getColorFilte
+Object(new ArgbEvaluato
 r(), 
-%22
 color
-%22
+From
 , 0)
@@ -4242,35 +4242,28 @@
 istener(new 
-ColorFilter
+Tint
 Listener(her
@@ -4609,144 +4609,155 @@
     
-ObjectAnimato
+Intege
 r color
+To
  = 
-ObjectAnimator.ofArgb(hero.getColorFilter(), %22color%22,%0A                getResources().getColor(R.color.photo_tint)
+getResources().getColor(R.color.photo_tint);%0A        ValueAnimator color = ValueAnimator.ofObject(new ArgbEvaluator(), 0, colorTo
 );%0A 
@@ -4791,27 +4791,20 @@
 ner(new 
-ColorFilter
+Tint
 Listener
@@ -5200,27 +5200,20 @@
 c class 
-ColorFilter
+Tint
 Listener
@@ -5311,27 +5311,20 @@
         
-ColorFilter
+Tint
 Listener
@@ -5505,40 +5505,53 @@
 .set
-ColorFilter(mHero.getColorFilter
+Tint((Integer) valueAnimator.getAnimatedValue
 ());

Fix tint animation