Merge "Merging MultiValueAlpha with MultiPropertyFactory" into tm-qpr-dev am: 3912808183

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Launcher3/+/19944063

Change-Id: Ia9d46bc255560cd4b8801158db4510ffcae2d7bf
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Sunny Goyal authored and Automerger Merge Worker committed 2022-10-25 22:17:40 +00:00
commit 34da3ed6df
23 files changed
+253 -313

No files matched your search

@@ -15,6 +15,7 @@
*/
package com.android.launcher3.allapps;
import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y;
import static com.android.launcher3.LauncherState.ALL_APPS;
import static com.android.launcher3.LauncherState.ALL_APPS_CONTENT;
import static com.android.launcher3.LauncherState.NORMAL;
@@ -42,6 +43,7 @@ import com.android.launcher3.anim.PropertySetter;
import com.android.launcher3.statemanager.StateManager.StateHandler;
import com.android.launcher3.states.StateAnimationConfig;
import com.android.launcher3.util.MultiPropertyFactory;
import com.android.launcher3.util.MultiPropertyFactory.MultiProperty;
import com.android.launcher3.util.MultiValueAlpha;
import com.android.launcher3.views.ScrimView;
@@ -82,8 +84,7 @@ public class AllAppsTransitionController
if (controller.mIsTablet) {
return controller.mAppsView.getActiveRecyclerView().getTranslationY();
} else {
return controller.getAppsViewPullbackTranslationY().get(
controller.mAppsView);
return controller.getAppsViewPullbackTranslationY().getValue();
}
}
@@ -92,8 +93,7 @@ public class AllAppsTransitionController
if (controller.mIsTablet) {
controller.mAppsView.getActiveRecyclerView().setTranslationY(translation);
} else {
controller.getAppsViewPullbackTranslationY().set(controller.mAppsView,
translation);
controller.getAppsViewPullbackTranslationY().setValue(translation);
}
}
};
@@ -140,10 +140,8 @@ public class AllAppsTransitionController
private ScrimView mScrimView;
private final MultiPropertyFactory<View>
mAppsViewTranslationYPropertyFactory = new MultiPropertyFactory<>(
"appsViewTranslationY", View.TRANSLATION_Y, Float::sum);
private MultiValueAlpha mAppsViewAlpha;
private MultiPropertyFactory<View> mAppsViewTranslationY;
private boolean mIsTablet;
@@ -184,7 +182,7 @@ public class AllAppsTransitionController
*/
public void setProgress(float progress) {
mProgress = progress;
getAppsViewProgressTranslationY().set(mAppsView, mProgress * mShiftRange);
getAppsViewProgressTranslationY().setValue(mProgress * mShiftRange);
mLauncher.onAllAppsTransition(1 - progress);
}
@@ -192,20 +190,20 @@ public class AllAppsTransitionController
return mProgress;
}
private FloatProperty<View> getAppsViewProgressTranslationY() {
return mAppsViewTranslationYPropertyFactory.get(INDEX_APPS_VIEW_PROGRESS);
private MultiProperty getAppsViewProgressTranslationY() {
return mAppsViewTranslationY.get(INDEX_APPS_VIEW_PROGRESS);
}
private FloatProperty<View> getAppsViewPullbackTranslationY() {
return mAppsViewTranslationYPropertyFactory.get(INDEX_APPS_VIEW_PULLBACK);
private MultiProperty getAppsViewPullbackTranslationY() {
return mAppsViewTranslationY.get(INDEX_APPS_VIEW_PULLBACK);
}
private MultiValueAlpha.AlphaProperty getAppsViewProgressAlpha() {
return mAppsViewAlpha.getProperty(INDEX_APPS_VIEW_PROGRESS);
private MultiProperty getAppsViewProgressAlpha() {
return mAppsViewAlpha.get(INDEX_APPS_VIEW_PROGRESS);
}
private MultiValueAlpha.AlphaProperty getAppsViewPullbackAlpha() {
return mAppsViewAlpha.getProperty(INDEX_APPS_VIEW_PULLBACK);
private MultiProperty getAppsViewPullbackAlpha() {
return mAppsViewAlpha.get(INDEX_APPS_VIEW_PULLBACK);
}
/**
@@ -283,7 +281,7 @@ public class AllAppsTransitionController
boolean hasAllAppsContent = (visibleElements & ALL_APPS_CONTENT) != 0;
Interpolator allAppsFade = config.getInterpolator(ANIM_ALL_APPS_FADE, LINEAR);
setter.setFloat(getAppsViewProgressAlpha(), MultiValueAlpha.VALUE,
setter.setFloat(getAppsViewProgressAlpha(), MultiPropertyFactory.MULTI_PROPERTY_VALUE,
hasAllAppsContent ? 1 : 0, allAppsFade);
boolean shouldProtectHeader =
@@ -302,8 +300,11 @@ public class AllAppsTransitionController
mScrimView = scrimView;
mAppsView = appsView;
mAppsView.setScrimView(scrimView);
mAppsViewAlpha = new MultiValueAlpha(mAppsView, APPS_VIEW_INDEX_COUNT);
mAppsViewAlpha.setUpdateVisibility(true);
mAppsViewTranslationY = new MultiPropertyFactory<>(
mAppsView, VIEW_TRANSLATE_Y, APPS_VIEW_INDEX_COUNT, Float::sum);
}
/**
@@ -16,10 +16,13 @@
package com.android.launcher3.util;
import android.util.ArrayMap;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.util.FloatProperty;
import android.util.Log;
import android.util.Property;
import java.io.PrintWriter;
import java.util.Arrays;
/**
* Allows to combine multiple values set by several sources.
@@ -35,15 +38,30 @@ import android.util.Property;
*/
public class MultiPropertyFactory<T> {
public static final FloatProperty<MultiPropertyFactory<?>.MultiProperty> MULTI_PROPERTY_VALUE =
new FloatProperty<MultiPropertyFactory<?>.MultiProperty>("value") {
@Override
public Float get(MultiPropertyFactory<?>.MultiProperty property) {
return property.mValue;
}
@Override
public void setValue(MultiPropertyFactory<?>.MultiProperty property, float value) {
property.setValue(value);
}
};
private static final boolean DEBUG = false;
private static final String TAG = "MultiPropertyFactory";
private final String mName;
private final ArrayMap<Integer, MultiProperty> mProperties = new ArrayMap<>();
private final MultiPropertyFactory<?>.MultiProperty[] mProperties;
// This is an optimization for cases when set is called repeatedly with the same setterIndex.
private float mAggregationOfOthers = 0f;
private Integer mLastIndexSet = -1;
private final Property<T, Float> mProperty;
private int mLastIndexSet = -1;
protected final T mTarget;
private final FloatProperty<T> mProperty;
private final FloatBiFunction mAggregator;
/**
@@ -56,59 +74,99 @@ public class MultiPropertyFactory<T> {
float apply(float a, float b);
}
public MultiPropertyFactory(String name, Property<T, Float> property,
public MultiPropertyFactory(T target, FloatProperty<T> property, int size,
FloatBiFunction aggregator) {
mName = name;
this(target, property, size, aggregator, 0);
}
public MultiPropertyFactory(T target, FloatProperty<T> property, int size,
FloatBiFunction aggregator, float defaultPropertyValue) {
mTarget = target;
mProperty = property;
mAggregator = aggregator;
mProperties = new MultiPropertyFactory<?>.MultiProperty[size];
for (int i = 0; i < size; i++) {
mProperties[i] = new MultiProperty(i, defaultPropertyValue);
}
}
/** Returns the [MultiFloatProperty] associated with [inx], creating it if not present. */
public MultiProperty get(Integer index) {
return mProperties.computeIfAbsent(index,
(k) -> new MultiProperty(index, mName + "_" + index));
public MultiProperty get(int index) {
return (MultiProperty) mProperties[index];
}
@Override
public String toString() {
return Arrays.deepToString(mProperties);
}
/**
* Dumps the alpha channel values to the given PrintWriter
*
* @param prefix String to be used before every line
* @param pw PrintWriter where the logs should be dumped
* @param label String used to help identify this object
* @param alphaIndexLabels Strings that represent each alpha channel, these should be entered
* in the order of the indexes they represent, starting from 0.
*/
public void dump(String prefix, PrintWriter pw, String label, String... alphaIndexLabels) {
pw.println(prefix + label);
String innerPrefix = prefix + '\t';
for (int i = 0; i < alphaIndexLabels.length; i++) {
if (i >= mProperties.length) {
pw.println(innerPrefix + alphaIndexLabels[i] + " given for alpha index " + i
+ " however there are only " + mProperties.length + " alpha channels.");
continue;
}
pw.println(innerPrefix + alphaIndexLabels[i] + "=" + get(i).getValue());
}
}
/**
* Each [setValue] will be aggregated with the other properties values created by the
* corresponding factory.
*/
class MultiProperty extends FloatProperty<T> {
private final int mInx;
private float mValue = 0f;
public class MultiProperty {
MultiProperty(int inx, String name) {
super(name);
private final int mInx;
private final float mDefaultValue;
private float mValue;
MultiProperty(int inx, float defaultValue) {
mInx = inx;
mDefaultValue = defaultValue;
mValue = defaultValue;
}
@Override
public void setValue(T obj, float newValue) {
public void setValue(float newValue) {
if (mLastIndexSet != mInx) {
mAggregationOfOthers = 0f;
mProperties.forEach((key, property) -> {
if (key != mInx) {
mAggregationOfOthers = mDefaultValue;
for (MultiPropertyFactory<?>.MultiProperty other : mProperties) {
if (other.mInx != mInx) {
mAggregationOfOthers =
mAggregator.apply(mAggregationOfOthers, property.mValue);
mAggregator.apply(mAggregationOfOthers, other.mValue);
}
});
}
mLastIndexSet = mInx;
}
float lastAggregatedValue = mAggregator.apply(mAggregationOfOthers, newValue);
mValue = newValue;
apply(obj, lastAggregatedValue);
apply(lastAggregatedValue);
if (DEBUG) {
Log.d(TAG, "name=" + mName
+ " newValue=" + newValue + " mInx=" + mInx
+ " aggregated=" + lastAggregatedValue + " others= " + mProperties);
Log.d(TAG, "name=" + mProperty.getName()
+ " target=" + mTarget.getClass()
+ " newValue=" + newValue
+ " mInx=" + mInx
+ " aggregated=" + lastAggregatedValue
+ " others= " + Arrays.deepToString(mProperties));
}
}
@Override
public Float get(T object) {
// Callers of MultiProperty should only care about the sub-property that it sets. If
// the overall value is needed, mProperty.get should be called directly.
public float getValue() {
return mValue;
}
@@ -116,9 +174,19 @@ public class MultiPropertyFactory<T> {
public String toString() {
return String.valueOf(mValue);
}
/**
* Creates and returns an Animator from the current value to the given value. Future
* animator on the same target automatically cancels the previous one.
*/
public Animator animateToValue(float value) {
ObjectAnimator animator = ObjectAnimator.ofFloat(this, MULTI_PROPERTY_VALUE, value);
animator.setAutoCancel(true);
return animator;
}
}
protected void apply(T object, float value) {
mProperty.set(object, value);
protected void apply(float value) {
mProperty.set(mTarget, value);
}
}
@@ -16,62 +16,24 @@
package com.android.launcher3.util;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.util.FloatProperty;
import static com.android.launcher3.LauncherAnimUtils.VIEW_ALPHA;
import android.view.View;
import com.android.launcher3.anim.AlphaUpdateListener;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.function.Consumer;
/**
* Utility class to handle separating a single value as a factor of multiple values
*/
public class MultiValueAlpha {
public class MultiValueAlpha extends MultiPropertyFactory<View> {
public static final FloatProperty<AlphaProperty> VALUE =
new FloatProperty<AlphaProperty>("value") {
private static final FloatBiFunction ALPHA_AGGREGATOR = (a, b) -> a * b;
@Override
public Float get(AlphaProperty alphaProperty) {
return alphaProperty.mValue;
}
@Override
public void setValue(AlphaProperty object, float value) {
object.setValue(value);
}
};
private final View mView;
private final AlphaProperty[] mMyProperties;
private int mValidMask;
// Whether we should change from INVISIBLE to VISIBLE and vice versa at low alpha values.
private boolean mUpdateVisibility;
public MultiValueAlpha(View view, int size) {
mView = view;
mMyProperties = new AlphaProperty[size];
mValidMask = 0;
for (int i = 0; i < size; i++) {
int myMask = 1 << i;
mValidMask |= myMask;
mMyProperties[i] = new AlphaProperty(myMask);
}
}
@Override
public String toString() {
return Arrays.toString(mMyProperties);
}
public AlphaProperty getProperty(int index) {
return mMyProperties[index];
super(view, VIEW_ALPHA, size, ALPHA_AGGREGATOR, 1f);
}
/** Sets whether we should update between INVISIBLE and VISIBLE based on alpha. */
@@ -79,97 +41,11 @@ public class MultiValueAlpha {
mUpdateVisibility = updateVisibility;
}
/**
* Dumps the alpha channel values to the given PrintWriter
*
* @param prefix String to be used before every line
* @param pw PrintWriter where the logs should be dumped
* @param label String used to help identify this object
* @param alphaIndexLabels Strings that represent each alpha channel, these should be entered
* in the order of the indexes they represent, starting from 0.
*/
public void dump(String prefix, PrintWriter pw, String label, String... alphaIndexLabels) {
pw.println(prefix + label);
String innerPrefix = prefix + '\t';
for (int i = 0; i < alphaIndexLabels.length; i++) {
if (i >= mMyProperties.length) {
pw.println(innerPrefix + alphaIndexLabels[i] + " given for alpha index " + i
+ " however there are only " + mMyProperties.length + " alpha channels.");
continue;
}
pw.println(innerPrefix + alphaIndexLabels[i] + "=" + getProperty(i).getValue());
}
}
public class AlphaProperty {
private final int mMyMask;
private float mValue = 1;
// Factor of all other alpha channels, only valid if mMyMask is present in mValidMask.
private float mOthers = 1;
private Consumer<Float> mConsumer;
AlphaProperty(int myMask) {
mMyMask = myMask;
}
public void setValue(float value) {
if (mValue == value) {
return;
}
if ((mValidMask & mMyMask) == 0) {
// Our cache value is not correct, recompute it.
mOthers = 1;
for (AlphaProperty prop : mMyProperties) {
if (prop != this) {
mOthers *= prop.mValue;
}
}
}
// Since we have changed our value, all other caches except our own need to be
// recomputed. Change mValidMask to indicate the new valid caches (only our own).
mValidMask = mMyMask;
mValue = value;
final float alpha = mOthers * mValue;
mView.setAlpha(alpha);
if (mUpdateVisibility) {
AlphaUpdateListener.updateVisibility(mView);
}
if (mConsumer != null) {
mConsumer.accept(mValue);
}
}
public float getValue() {
return mValue;
}
public void setConsumer(Consumer<Float> consumer) {
mConsumer = consumer;
if (mConsumer != null) {
mConsumer.accept(mValue);
}
}
@Override
public String toString() {
return Float.toString(mValue);
}
/**
* Creates and returns an Animator from the current value to the given value. Future
* animator on the same target automatically cancels the previous one.
*/
public Animator animateToValue(float value) {
ObjectAnimator animator = ObjectAnimator.ofFloat(this, VALUE, value);
animator.setAutoCancel(true);
return animator;
@Override
protected void apply(float value) {
super.apply(value);
if (mUpdateVisibility) {
AlphaUpdateListener.updateVisibility(mTarget);
}
}
}
@@ -22,7 +22,6 @@ import static android.view.MotionEvent.ACTION_UP;
import static com.android.launcher3.util.window.RefreshRateTracker.getSingleFrameMs;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Insets;
import android.graphics.Rect;
@@ -41,8 +40,8 @@ import com.android.launcher3.AbstractFloatingView;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.InsettableFrameLayout;
import com.android.launcher3.Utilities;
import com.android.launcher3.util.MultiPropertyFactory.MultiProperty;
import com.android.launcher3.util.MultiValueAlpha;
import com.android.launcher3.util.MultiValueAlpha.AlphaProperty;
import com.android.launcher3.util.TouchController;
import java.io.PrintWriter;
@@ -108,7 +107,6 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
protected final T mActivity;
private final MultiValueAlpha mMultiValueAlpha;
private final WallpaperManager mWallpaperManager;
// All the touch controllers for the view
protected TouchController[] mControllers;
@@ -121,9 +119,8 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
public BaseDragLayer(Context context, AttributeSet attrs, int alphaChannelCount) {
super(context, attrs);
mActivity = (T) ActivityContext.lookupContext(context);
mActivity = ActivityContext.lookupContext(context);
mMultiValueAlpha = new MultiValueAlpha(this, alphaChannelCount);
mWallpaperManager = context.getSystemService(WallpaperManager.class);
}
/**
@@ -508,8 +505,8 @@ public abstract class BaseDragLayer<T extends Context & ActivityContext>
return new LayoutParams(p);
}
public AlphaProperty getAlphaProperty(int index) {
return mMultiValueAlpha.getProperty(index);
public MultiProperty getAlphaProperty(int index) {
return mMultiValueAlpha.get(index);
}
public void dump(String prefix, PrintWriter writer) {