Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0228a255 authored by Selim Cinek's avatar Selim Cinek
Browse files

Refactored PropertyAnimator for easier usability

This allows us to use a PropertyAnimator much easier, by
just passing a few values, without having to write that
much boilerplate anymore.

Bug: 69168591
Test: runtest -x packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/PropertyAnimatorTest.java
Change-Id: I3783fa928bdc9d3fe7a92b2c0d04a369950fa5bd
parent 0fe07395
Loading
Loading
Loading
Loading
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.systemui.statusbar.notification;

import android.util.FloatProperty;
import android.util.Property;
import android.view.View;

import com.android.systemui.statusbar.stack.AnimationProperties;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * An animatable property of a view. Used with {@link PropertyAnimator}
 */
public interface AnimatableProperty {
    int getAnimationStartTag();

    int getAnimationEndTag();

    int getAnimatorTag();

    Property getProperty();

    static <T extends View> AnimatableProperty from(String name, BiConsumer<T, Float> setter,
            Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag) {
        Property<T, Float> property = new FloatProperty<T>(name) {

            @Override
            public Float get(T object) {
                return getter.apply(object);
            }

            @Override
            public void setValue(T object, float value) {
                setter.accept(object, value);
            }
        };
        return new AnimatableProperty() {
            @Override
            public int getAnimationStartTag() {
                return startValueTag;
            }

            @Override
            public int getAnimationEndTag() {
                return endValueTag;
            }

            @Override
            public int getAnimatorTag() {
                return animatorTag;
            }

            @Override
            public Property getProperty() {
                return property;
            }
        };
    }
}
+13 −6
Original line number Diff line number Diff line
@@ -34,6 +34,19 @@ import com.android.systemui.statusbar.stack.ViewState;
 */
public class PropertyAnimator {

    public static <T extends View> void setProperty(final T view,
            AnimatableProperty animatableProperty, float newEndValue,
            AnimationProperties properties, boolean animated) {
        int animatorTag = animatableProperty.getAnimatorTag();
        ValueAnimator previousAnimator = ViewState.getChildTag(view, animatorTag);
        if (previousAnimator != null || animated) {
            startAnimation(view, animatableProperty, newEndValue, properties);
        } else {
            // no new animation needed, let's just apply the value
            animatableProperty.getProperty().set(view, newEndValue);
        }
    }

    public static <T extends View> void startAnimation(final T view,
            AnimatableProperty animatableProperty, float newEndValue,
            AnimationProperties properties) {
@@ -102,10 +115,4 @@ public class PropertyAnimator {
        view.setTag(animationEndTag, newEndValue);
    }

    public interface AnimatableProperty {
        int getAnimationStartTag();
        int getAnimationEndTag();
        int getAnimatorTag();
        Property getProperty();
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -36,7 +36,12 @@ public class AnimationProperties {
     * @return an animation filter for this animation.
     */
    public AnimationFilter getAnimationFilter() {
        return new AnimationFilter();
        return new AnimationFilter() {
            @Override
            public boolean shouldAnimateProperty(Property property) {
                return true;
            }
        };
    }

    /**
+7 −8
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.app.Notification;
import android.util.Property;
import android.view.View;
import android.view.animation.Interpolator;
@@ -29,7 +28,7 @@ import android.view.animation.Interpolator;
import com.android.systemui.Interpolators;
import com.android.systemui.R;
import com.android.systemui.statusbar.ExpandableView;
import com.android.systemui.statusbar.NotificationShelf;
import com.android.systemui.statusbar.notification.AnimatableProperty;
import com.android.systemui.statusbar.notification.PropertyAnimator;
import com.android.systemui.statusbar.policy.HeadsUpManager;

@@ -64,8 +63,8 @@ public class ViewState {
    private static final int TAG_START_TRANSLATION_Z = R.id.translation_z_animator_start_value_tag;
    private static final int TAG_START_ALPHA = R.id.alpha_animator_start_value_tag;

    private static final PropertyAnimator.AnimatableProperty SCALE_X_PROPERTY
            = new PropertyAnimator.AnimatableProperty() {
    private static final AnimatableProperty SCALE_X_PROPERTY
            = new AnimatableProperty() {

        @Override
        public int getAnimationStartTag() {
@@ -88,8 +87,8 @@ public class ViewState {
        }
    };

    private static final PropertyAnimator.AnimatableProperty SCALE_Y_PROPERTY
            = new PropertyAnimator.AnimatableProperty() {
    private static final AnimatableProperty SCALE_Y_PROPERTY
            = new AnimatableProperty() {

        @Override
        public int getAnimationStartTag() {
@@ -251,7 +250,7 @@ public class ViewState {
        return getChildTag(view, tag) != null;
    }

    public static boolean isAnimating(View view, PropertyAnimator.AnimatableProperty property) {
    public static boolean isAnimating(View view, AnimatableProperty property) {
        return getChildTag(view, property.getAnimatorTag()) != null;
    }

@@ -403,7 +402,7 @@ public class ViewState {
        startZTranslationAnimation(view, NO_NEW_ANIMATIONS);
    }

    private void updateAnimation(View view, PropertyAnimator.AnimatableProperty property,
    private void updateAnimation(View view, AnimatableProperty property,
            float endValue) {
        PropertyAnimator.startAnimation(view, property, endValue, NO_NEW_ANIMATIONS);
    }
+2 −3
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ import android.view.animation.Interpolator;
import com.android.systemui.Interpolators;
import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.statusbar.notification.PropertyAnimator;
import com.android.systemui.statusbar.stack.AnimationFilter;
import com.android.systemui.statusbar.stack.AnimationProperties;
import com.android.systemui.statusbar.stack.ViewState;
@@ -63,8 +62,8 @@ public class PropertyAnimatorTest extends SysuiTestCase {
            return mValue;
        }
    };
    private PropertyAnimator.AnimatableProperty mProperty
            = new PropertyAnimator.AnimatableProperty() {
    private AnimatableProperty mProperty
            = new AnimatableProperty() {

        @Override
        public int getAnimationStartTag() {