diff options
| author | Pierre Barbier de Reuille <pbdr@google.com> | 2021-01-28 10:16:25 +0000 |
|---|---|---|
| committer | Pierre Barbier de Reuille <pbdr@google.com> | 2021-02-01 21:26:26 +0000 |
| commit | 24ffac5ccd66f2b3602f5dcfb5673eb1f6b8ce58 (patch) | |
| tree | ef97fbb69ce6ef3bebd99b55743a6b0c19f73b17 /core/java/android/widget/RemoteViews.java | |
| parent | 2a98cf0f640b9c4604dfe3369cb7d1d4e292a592 (diff) | |
New setters extracting values from complex unit dimensions.
These new setters allow defining configuration dependent values at
runtime (to some extend) in a way that is currently not possible.
See go/widgets-configuration-setters for details
Bug: 178591376
Test: atest android.widget.cts.RemoteViewsTest
Change-Id: I5a1582b4fa7f70ad8d922c72f773fe254ef2f9ff
Diffstat (limited to 'core/java/android/widget/RemoteViews.java')
| -rw-r--r-- | core/java/android/widget/RemoteViews.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java index 2b0dd0bcd6bd..b47a0acc4fae 100644 --- a/core/java/android/widget/RemoteViews.java +++ b/core/java/android/widget/RemoteViews.java @@ -65,6 +65,7 @@ import android.os.StrictMode; import android.os.UserHandle; import android.text.TextUtils; import android.util.ArrayMap; +import android.util.DisplayMetrics; import android.util.IntArray; import android.util.Log; import android.util.Pair; @@ -183,6 +184,7 @@ public class RemoteViews implements Parcelable, Filter { private static final int SET_INT_TAG_TAG = 22; private static final int REMOVE_FROM_PARENT_ACTION_TAG = 23; private static final int RESOURCE_REFLECTION_ACTION_TAG = 24; + private static final int COMPLEX_UNIT_DIMENSION_REFLECTION_ACTION_TAG = 25; /** @hide **/ @IntDef(prefix = "MARGIN_", value = { @@ -1684,6 +1686,59 @@ public class RemoteViews implements Parcelable, Filter { } } + private final class ComplexUnitDimensionReflectionAction extends BaseReflectionAction { + + private final float mValue; + @ComplexDimensionUnit + private final int mUnit; + + ComplexUnitDimensionReflectionAction(int viewId, String methodName, int parameterType, + float value, @ComplexDimensionUnit int unit) { + super(viewId, methodName, parameterType); + this.mValue = value; + this.mUnit = unit; + } + + ComplexUnitDimensionReflectionAction(Parcel in) { + super(in); + this.mValue = in.readFloat(); + this.mUnit = in.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeFloat(this.mValue); + dest.writeInt(this.mUnit); + } + + @Override + protected Object getParameterValue(View view) throws ActionException { + DisplayMetrics dm = view.getContext().getResources().getDisplayMetrics(); + try { + int data = TypedValue.createComplexDimension(this.mValue, this.mUnit); + switch (this.type) { + case ReflectionAction.INT: + return TypedValue.complexToDimensionPixelSize(data, dm); + case ReflectionAction.FLOAT: + return TypedValue.complexToDimension(data, dm); + default: + throw new ActionException( + "parameter type must be INT or FLOAT, not " + this.type); + } + } catch (ActionException ex) { + throw ex; + } catch (Throwable t) { + throw new ActionException(t); + } + } + + @Override + public int getActionTag() { + return COMPLEX_UNIT_DIMENSION_REFLECTION_ACTION_TAG; + } + } + /** * This is only used for async execution of actions and it not parcelable. */ @@ -2709,6 +2764,8 @@ public class RemoteViews implements Parcelable, Filter { return new RemoveFromParentAction(parcel); case RESOURCE_REFLECTION_ACTION_TAG: return new ResourceReflectionAction(parcel); + case COMPLEX_UNIT_DIMENSION_REFLECTION_ACTION_TAG: + return new ComplexUnitDimensionReflectionAction(parcel); default: throw new ActionException("Tag " + tag + " not found"); } @@ -3504,6 +3561,23 @@ public class RemoteViews implements Parcelable, Filter { } /** + * Call a method taking one int, a size in pixels, on a view in the layout for this + * RemoteViews. + * + * The dimension will be resolved from the specified dimension at the time of inflation. + * + * @param viewId The id of the view on which to call the method. + * @param methodName The name of the method to call. + * @param value The value of the dimension. + * @param unit The unit in which the value is specified. + */ + public void setIntDimen(@IdRes int viewId, @NonNull String methodName, + float value, @ComplexDimensionUnit int unit) { + addAction(new ComplexUnitDimensionReflectionAction(viewId, methodName, ReflectionAction.INT, + value, unit)); + } + + /** * Call a method taking one int, a color, on a view in the layout for this RemoteViews. * * The ColorStateList will be resolved from the resources at the time of inflation. @@ -3588,6 +3662,24 @@ public class RemoteViews implements Parcelable, Filter { } /** + * Call a method taking one float, a size in pixels, on a view in the layout for this + * RemoteViews. + * + * The dimension will be resolved from the specified dimension at the time of inflation. + * + * @param viewId The id of the view on which to call the method. + * @param methodName The name of the method to call. + * @param value The value of the dimension. + * @param unit The unit in which the value is specified. + */ + public void setFloatDimen(@IdRes int viewId, @NonNull String methodName, + float value, @ComplexDimensionUnit int unit) { + addAction( + new ComplexUnitDimensionReflectionAction(viewId, methodName, ReflectionAction.FLOAT, + value, unit)); + } + + /** * Call a method taking one double on a view in the layout for this RemoteViews. * * @param viewId The id of the view on which to call the method. |
