2022-04-14 18:36:57 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 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.launcher3.util;
|
|
|
|
|
|
|
|
|
|
import android.util.ArrayMap;
|
|
|
|
|
import android.util.FloatProperty;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.util.Property;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allows to combine multiple values set by several sources.
|
|
|
|
|
*
|
|
|
|
|
* The various sources are meant to use [set], providing different `setterIndex` params. When it is
|
|
|
|
|
* not set, 0 is used. This is meant to cover the case multiple animations are going on at the same
|
|
|
|
|
* time.
|
|
|
|
|
*
|
|
|
|
|
* This class behaves similarly to [MultiValueAlpha], but is meant to be more abstract and reusable.
|
2022-09-01 21:28:14 +01:00
|
|
|
* It aggregate all values using the provided [aggregator].
|
2022-04-14 18:36:57 +01:00
|
|
|
*
|
|
|
|
|
* @param <T> Type where to apply the property.
|
|
|
|
|
*/
|
2022-09-01 21:28:14 +01:00
|
|
|
public class MultiPropertyFactory<T> {
|
2022-04-14 18:36:57 +01:00
|
|
|
|
|
|
|
|
private static final boolean DEBUG = false;
|
2022-09-01 21:28:14 +01:00
|
|
|
private static final String TAG = "MultiPropertyFactory";
|
2022-04-14 18:36:57 +01:00
|
|
|
private final String mName;
|
2022-09-01 21:28:14 +01:00
|
|
|
private final ArrayMap<Integer, MultiProperty> mProperties = new ArrayMap<>();
|
2022-04-14 18:36:57 +01:00
|
|
|
|
|
|
|
|
// This is an optimization for cases when set is called repeatedly with the same setterIndex.
|
|
|
|
|
private float mAggregationOfOthers = 0f;
|
|
|
|
|
private Integer mLastIndexSet = -1;
|
2022-09-01 21:28:14 +01:00
|
|
|
private final Property<T, Float> mProperty;
|
|
|
|
|
private final FloatBiFunction mAggregator;
|
2022-04-14 18:36:57 +01:00
|
|
|
|
2022-09-01 21:28:14 +01:00
|
|
|
/**
|
|
|
|
|
* Represents a function that accepts two float and produces a float.
|
|
|
|
|
*/
|
|
|
|
|
public interface FloatBiFunction {
|
|
|
|
|
/**
|
|
|
|
|
* Applies this function to the given arguments.
|
|
|
|
|
*/
|
|
|
|
|
float apply(float a, float b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MultiPropertyFactory(String name, Property<T, Float> property,
|
|
|
|
|
FloatBiFunction aggregator) {
|
2022-04-14 18:36:57 +01:00
|
|
|
mName = name;
|
|
|
|
|
mProperty = property;
|
2022-09-01 21:28:14 +01:00
|
|
|
mAggregator = aggregator;
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Returns the [MultiFloatProperty] associated with [inx], creating it if not present. */
|
2022-09-01 21:28:14 +01:00
|
|
|
public MultiProperty get(Integer index) {
|
2022-04-14 18:36:57 +01:00
|
|
|
return mProperties.computeIfAbsent(index,
|
2022-09-01 21:28:14 +01:00
|
|
|
(k) -> new MultiProperty(index, mName + "_" + index));
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Each [setValue] will be aggregated with the other properties values created by the
|
|
|
|
|
* corresponding factory.
|
|
|
|
|
*/
|
2022-09-01 21:28:14 +01:00
|
|
|
class MultiProperty extends FloatProperty<T> {
|
2022-04-14 18:36:57 +01:00
|
|
|
private final int mInx;
|
|
|
|
|
private float mValue = 0f;
|
|
|
|
|
|
2022-09-01 21:28:14 +01:00
|
|
|
MultiProperty(int inx, String name) {
|
2022-04-14 18:36:57 +01:00
|
|
|
super(name);
|
|
|
|
|
mInx = inx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setValue(T obj, float newValue) {
|
|
|
|
|
if (mLastIndexSet != mInx) {
|
|
|
|
|
mAggregationOfOthers = 0f;
|
|
|
|
|
mProperties.forEach((key, property) -> {
|
|
|
|
|
if (key != mInx) {
|
2022-09-01 21:28:14 +01:00
|
|
|
mAggregationOfOthers =
|
|
|
|
|
mAggregator.apply(mAggregationOfOthers, property.mValue);
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
mLastIndexSet = mInx;
|
|
|
|
|
}
|
2022-09-01 21:28:14 +01:00
|
|
|
float lastAggregatedValue = mAggregator.apply(mAggregationOfOthers, newValue);
|
2022-04-14 18:36:57 +01:00
|
|
|
mValue = newValue;
|
|
|
|
|
apply(obj, lastAggregatedValue);
|
|
|
|
|
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "name=" + mName
|
|
|
|
|
+ " newValue=" + newValue + " mInx=" + mInx
|
|
|
|
|
+ " aggregated=" + lastAggregatedValue + " others= " + mProperties);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-09-01 21:28:14 +01:00
|
|
|
public Float get(T object) {
|
2022-04-14 18:36:57 +01:00
|
|
|
// The scale of the view should match mLastAggregatedValue. Still, if it has been
|
|
|
|
|
// changed without using this property, it can differ. As this get method is usually
|
|
|
|
|
// used to set the starting point on an animation, this would result in some jumps
|
|
|
|
|
// when the view scale is different than the last aggregated value. To stay on the
|
|
|
|
|
// safe side, let's return the real view scale.
|
2022-09-01 21:28:14 +01:00
|
|
|
return mProperty.get(object);
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String toString() {
|
|
|
|
|
return String.valueOf(mValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-01 21:28:14 +01:00
|
|
|
protected void apply(T object, float value) {
|
|
|
|
|
mProperty.set(object, value);
|
2022-04-14 18:36:57 +01:00
|
|
|
}
|
|
|
|
|
}
|