diff options
| author | 2021-02-22 14:45:03 +0000 | |
|---|---|---|
| committer | 2021-02-22 14:45:03 +0000 | |
| commit | 6bb6992fe9e0914bfbfaed49db5c3e19c4c6c70c (patch) | |
| tree | 2bf815469c68fb45d5e73263a781df7743687528 | |
| parent | 28f5700a0844e798ed35ed3a39f8b24ec75686bf (diff) | |
| parent | 1f01cf90c5288d56c845fd21d293459c20da83ac (diff) | |
Merge "Add dark mode apis for setting colors and icons" into sc-dev
| -rw-r--r-- | core/api/current.txt | 3 | ||||
| -rw-r--r-- | core/java/android/widget/RemoteViews.java | 142 |
2 files changed, 145 insertions, 0 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index 0f777b7db857..4382b3207e1e 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -55073,6 +55073,8 @@ package android.widget { method public void setChronometer(@IdRes int, long, String, boolean); method public void setChronometerCountDown(@IdRes int, boolean); method public void setColor(@IdRes int, @NonNull String, @ColorRes int); + method public void setColorInt(@IdRes int, @NonNull String, @ColorInt int, @ColorInt int); + method public void setColorStateList(@IdRes int, @NonNull String, @Nullable android.content.res.ColorStateList, @Nullable android.content.res.ColorStateList); method public void setColorStateList(@IdRes int, @NonNull String, @ColorRes int); method public void setCompoundButtonChecked(@IdRes int, boolean); method public void setContentDescription(@IdRes int, CharSequence); @@ -55083,6 +55085,7 @@ package android.widget { 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 setIcon(@IdRes int, @NonNull String, @Nullable android.graphics.drawable.Icon, @Nullable android.graphics.drawable.Icon); method public void setImageViewBitmap(@IdRes int, android.graphics.Bitmap); method public void setImageViewIcon(@IdRes int, android.graphics.drawable.Icon); method public void setImageViewResource(@IdRes int, @DrawableRes int); diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java index 2112fb1de63f..30bf546eb83e 100644 --- a/core/java/android/widget/RemoteViews.java +++ b/core/java/android/widget/RemoteViews.java @@ -211,6 +211,7 @@ public class RemoteViews implements Parcelable, Filter { private static final int SET_RADIO_GROUP_CHECKED = 27; private static final int SET_VIEW_OUTLINE_RADIUS_TAG = 28; private static final int SET_ON_CHECKED_CHANGE_RESPONSE_TAG = 29; + private static final int NIGHT_MODE_REFLECTION_ACTION_TAG = 30; /** @hide **/ @IntDef(prefix = "MARGIN_", value = { @@ -1877,6 +1878,73 @@ public class RemoteViews implements Parcelable, Filter { } } + private final class NightModeReflectionAction extends BaseReflectionAction { + + private final Object mLightValue; + private final Object mDarkValue; + + NightModeReflectionAction( + @IdRes int viewId, + String methodName, + int type, + Object lightValue, + Object darkValue) { + super(viewId, methodName, type); + mLightValue = lightValue; + mDarkValue = darkValue; + } + + NightModeReflectionAction(Parcel in) { + super(in); + switch (this.type) { + case ICON: + mLightValue = in.readTypedObject(Icon.CREATOR); + mDarkValue = in.readTypedObject(Icon.CREATOR); + break; + case COLOR_STATE_LIST: + mLightValue = in.readTypedObject(ColorStateList.CREATOR); + mDarkValue = in.readTypedObject(ColorStateList.CREATOR); + break; + case INT: + mLightValue = in.readInt(); + mDarkValue = in.readInt(); + break; + default: + throw new ActionException("Unexpected night mode action type: " + this.type); + } + } + + @Override + public void writeToParcel(Parcel out, int flags) { + super.writeToParcel(out, flags); + switch (this.type) { + case ICON: + case COLOR_STATE_LIST: + out.writeTypedObject((Parcelable) mLightValue, flags); + out.writeTypedObject((Parcelable) mDarkValue, flags); + break; + case INT: + out.writeInt((int) mLightValue); + out.writeInt((int) mDarkValue); + break; + } + } + + @Nullable + @Override + protected Object getParameterValue(@Nullable View view) throws ActionException { + if (view == null) return null; + + Configuration configuration = view.getResources().getConfiguration(); + return configuration.isNightModeActive() ? mDarkValue : mLightValue; + } + + @Override + public int getActionTag() { + return NIGHT_MODE_REFLECTION_ACTION_TAG; + } + } + /** * This is only used for async execution of actions and it not parcelable. */ @@ -3243,6 +3311,8 @@ public class RemoteViews implements Parcelable, Filter { return new SetViewOutlinePreferredRadiusAction(parcel); case SET_ON_CHECKED_CHANGE_RESPONSE_TAG: return new SetOnCheckedChangeResponse(parcel); + case NIGHT_MODE_REFLECTION_ACTION_TAG: + return new NightModeReflectionAction(parcel); default: throw new ActionException("Tag " + tag + " not found"); } @@ -4130,6 +4200,30 @@ public class RemoteViews implements Parcelable, Filter { ResourceReflectionAction.COLOR_RESOURCE, colorResource)); } + /** + * Call a method taking one int, a color, on a view in the layout for this RemoteViews. + * + * @param viewId The id of the view on which to call the method. + * @param methodName The name of the method to call. + * @param notNight The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_NO} + * @param night The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_YES} + */ + public void setColorInt( + @IdRes int viewId, + @NonNull String methodName, + @ColorInt int notNight, + @ColorInt int night) { + addAction( + new NightModeReflectionAction( + viewId, + methodName, + BaseReflectionAction.INT, + notNight, + night)); + } + /** * Call a method taking one ColorStateList on a view in the layout for this RemoteViews. @@ -4148,6 +4242,30 @@ public class RemoteViews implements Parcelable, Filter { /** * Call a method taking one ColorStateList on a view in the layout for this RemoteViews. * + * @param viewId The id of the view on which to call the method. + * @param methodName The name of the method to call. + * @param notNight The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_NO} + * @param night The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_YES} + */ + public void setColorStateList( + @IdRes int viewId, + @NonNull String methodName, + @Nullable ColorStateList notNight, + @Nullable ColorStateList night) { + addAction( + new NightModeReflectionAction( + viewId, + methodName, + BaseReflectionAction.COLOR_STATE_LIST, + notNight, + night)); + } + + /** + * Call a method taking one ColorStateList on a view in the layout for this RemoteViews. + * * The ColorStateList will be resolved from the resources at the time of inflation. * * @param viewId The id of the view on which to call the method. @@ -4356,6 +4474,30 @@ public class RemoteViews implements Parcelable, Filter { } /** + * Call a method taking one Icon on a view in the layout for this RemoteViews. + * + * @param viewId The id of the view on which to call the method. + * @param methodName The name of the method to call. + * @param notNight The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_NO} + * @param night The value to pass to the method when the view's configuration is set to + * {@link Configuration#UI_MODE_NIGHT_YES} + */ + public void setIcon( + @IdRes int viewId, + @NonNull String methodName, + @Nullable Icon notNight, + @Nullable Icon night) { + addAction( + new NightModeReflectionAction( + viewId, + methodName, + BaseReflectionAction.ICON, + notNight, + night)); + } + + /** * Equivalent to calling View.setContentDescription(CharSequence). * * @param viewId The id of the view whose content description should change. |