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

Commit 24ffac5c authored by Pierre Barbier de Reuille's avatar Pierre Barbier de Reuille
Browse files

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
parent 2a98cf0f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -54469,6 +54469,7 @@ package android.widget {
    method public void setEmptyView(@IdRes int, @IdRes int);
    method public void setFloat(@IdRes int, String, float);
    method public void setFloatDimen(@IdRes int, @NonNull String, @DimenRes int);
    method public void setFloatDimen(@IdRes int, @NonNull String, float, int);
    method public void setIcon(@IdRes int, String, android.graphics.drawable.Icon);
    method public void setImageViewBitmap(@IdRes int, android.graphics.Bitmap);
    method public void setImageViewIcon(@IdRes int, android.graphics.drawable.Icon);
@@ -54476,6 +54477,7 @@ package android.widget {
    method public void setImageViewUri(@IdRes int, android.net.Uri);
    method public void setInt(@IdRes int, String, int);
    method public void setIntDimen(@IdRes int, @NonNull String, @DimenRes int);
    method public void setIntDimen(@IdRes int, @NonNull String, float, int);
    method public void setIntent(@IdRes int, String, android.content.Intent);
    method public void setLabelFor(@IdRes int, @IdRes int);
    method public void setLightBackgroundLayoutId(@LayoutRes int);
+92 −0
Original line number Diff line number Diff line
@@ -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");
        }
@@ -3503,6 +3560,23 @@ public class RemoteViews implements Parcelable, Filter {
                ResourceReflectionAction.DIMEN_RESOURCE, dimenResource));
    }

    /**
     * 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.
     *
@@ -3587,6 +3661,24 @@ public class RemoteViews implements Parcelable, Filter {
                ResourceReflectionAction.DIMEN_RESOURCE, dimenResource));
    }

    /**
     * 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.
     *