Add dark mode apis for setting colors and icons

Bug: 180494787
Test: sample app
Test: atest RemoteViewsTest
Change-Id: I685804c5d0fe51b75508ec5ba7c122e75ad750da
This commit is contained in:
Stevie Kideckel
2021-02-17 00:54:13 +00:00
parent 2441a6bb3a
commit 1f01cf90c5
2 changed files with 145 additions and 0 deletions

View File

@@ -55055,6 +55055,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);
@@ -55065,6 +55067,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);

View File

@@ -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.
@@ -4145,6 +4239,30 @@ public class RemoteViews implements Parcelable, Filter {
value));
}
/**
* 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.
*
@@ -4355,6 +4473,30 @@ public class RemoteViews implements Parcelable, Filter {
addAction(new ReflectionAction(viewId, methodName, BaseReflectionAction.ICON, value));
}
/**
* 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).
*