From c42edd69f93606470e13a5b508087c5ce8476359 Mon Sep 17 00:00:00 2001
From: Steven Kideckel
@@ -52,6 +53,7 @@ import android.util.AttributeSet;
* {@link android.R.styleable#View View Attributes}
*
As of API 31, the following widgets and layouts may also be used:
+ *Descendants of these classes are not supported.
*/ public class RemoteViews implements Parcelable, Filter { @@ -185,6 +192,8 @@ public class RemoteViews implements Parcelable, Filter { 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; + private static final int SET_COMPOUND_BUTTON_CHECKED_TAG = 26; + private static final int SET_RADIO_GROUP_CHECKED = 27; /** @hide **/ @IntDef(prefix = "MARGIN_", value = { @@ -2552,6 +2561,87 @@ public class RemoteViews implements Parcelable, Filter { } } + private static class SetCompoundButtonCheckedAction extends Action { + + private final boolean mChecked; + + SetCompoundButtonCheckedAction(@IdRes int viewId, boolean checked) { + this.viewId = viewId; + mChecked = checked; + } + + SetCompoundButtonCheckedAction(Parcel in) { + viewId = in.readInt(); + mChecked = in.readBoolean(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(viewId); + dest.writeBoolean(mChecked); + } + + @Override + public void apply(View root, ViewGroup rootParent, OnClickHandler handler) + throws ActionException { + final View target = root.findViewById(viewId); + if (target == null) return; + + if (!(target instanceof CompoundButton)) { + Log.w(LOG_TAG, "Cannot set checked to view " + + viewId + " because it is not a CompoundButton"); + return; + } + + ((CompoundButton) target).setChecked(mChecked); + } + + @Override + public int getActionTag() { + return SET_COMPOUND_BUTTON_CHECKED_TAG; + } + } + + private static class SetRadioGroupCheckedAction extends Action { + + @IdRes private final int mCheckedId; + + SetRadioGroupCheckedAction(@IdRes int viewId, @IdRes int checkedId) { + this.viewId = viewId; + mCheckedId = checkedId; + } + + SetRadioGroupCheckedAction(Parcel in) { + viewId = in.readInt(); + mCheckedId = in.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + dest.writeInt(viewId); + dest.writeInt(mCheckedId); + } + + @Override + public void apply(View root, ViewGroup rootParent, OnClickHandler handler) + throws ActionException { + final View target = root.findViewById(viewId); + if (target == null) return; + + if (!(target instanceof RadioGroup)) { + Log.w(LOG_TAG, "Cannot check " + viewId + " because it's not a RadioGroup"); + return; + } + + ((RadioGroup) target).check(mCheckedId); + } + + @Override + public int getActionTag() { + return SET_RADIO_GROUP_CHECKED; + } + } + /** * Create a new RemoteViews object that will display the views contained * in the specified layout file. @@ -2766,6 +2856,10 @@ public class RemoteViews implements Parcelable, Filter { return new ResourceReflectionAction(parcel); case COMPLEX_UNIT_DIMENSION_REFLECTION_ACTION_TAG: return new ComplexUnitDimensionReflectionAction(parcel); + case SET_COMPOUND_BUTTON_CHECKED_TAG: + return new SetCompoundButtonCheckedAction(parcel); + case SET_RADIO_GROUP_CHECKED: + return new SetRadioGroupCheckedAction(parcel); default: throw new ActionException("Tag " + tag + " not found"); } @@ -3845,6 +3939,26 @@ public class RemoteViews implements Parcelable, Filter { setInt(viewId, "setLabelFor", labeledId); } + /** + * Equivalent to calling {@link android.widget.CompoundButton#setChecked(boolean)}. + * + * @param viewId The id of the view whose property to set. + * @param checked true to check the button, false to uncheck it. + */ + public void setCompoundButtonChecked(@IdRes int viewId, boolean checked) { + addAction(new SetCompoundButtonCheckedAction(viewId, checked)); + } + + /** + * Equivalent to calling {@link android.widget.RadioGroup#check(int)}. + * + * @param viewId The id of the view whose property to set. + * @param checkedId The unique id of the radio button to select in the group. + */ + public void setRadioGroupChecked(@IdRes int viewId, @IdRes int checkedId) { + addAction(new SetRadioGroupCheckedAction(viewId, checkedId)); + } + /** * Provides an alternate layout ID, which can be used to inflate this view. This layout will be * used by the host when the widgets displayed on a light-background where foreground elements diff --git a/core/java/android/widget/Switch.java b/core/java/android/widget/Switch.java index 3295fd2ea1c3..d3600ef9f557 100644 --- a/core/java/android/widget/Switch.java +++ b/core/java/android/widget/Switch.java @@ -35,6 +35,7 @@ import android.graphics.Rect; import android.graphics.Region.Op; import android.graphics.Typeface; import android.graphics.drawable.Drawable; +import android.graphics.drawable.Icon; import android.os.Build; import android.os.Build.VERSION_CODES; import android.text.Layout; @@ -48,12 +49,14 @@ import android.util.FloatProperty; import android.util.MathUtils; import android.view.Gravity; import android.view.MotionEvent; +import android.view.RemotableViewMethod; import android.view.SoundEffectConstants; import android.view.VelocityTracker; import android.view.ViewConfiguration; import android.view.ViewStructure; import android.view.accessibility.AccessibilityEvent; import android.view.inspector.InspectableProperty; +import android.widget.RemoteViews.RemoteView; import com.android.internal.R; @@ -84,6 +87,7 @@ import com.android.internal.R; * @attr ref android.R.styleable#Switch_thumbTextPadding * @attr ref android.R.styleable#Switch_track */ +@RemoteView public class Switch extends CompoundButton { private static final int THUMB_ANIMATION_DURATION = 250; @@ -441,6 +445,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_switchPadding */ + @RemotableViewMethod public void setSwitchPadding(int pixels) { mSwitchPadding = pixels; requestLayout(); @@ -466,6 +471,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_switchMinWidth */ + @RemotableViewMethod public void setSwitchMinWidth(int pixels) { mSwitchMinWidth = pixels; requestLayout(); @@ -491,6 +497,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_thumbTextPadding */ + @RemotableViewMethod public void setThumbTextPadding(int pixels) { mThumbTextPadding = pixels; requestLayout(); @@ -533,10 +540,17 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_track */ + @RemotableViewMethod(asyncImpl = "setTrackResourceAsync") public void setTrackResource(@DrawableRes int resId) { setTrackDrawable(getContext().getDrawable(resId)); } + /** @hide **/ + public Runnable setTrackResourceAsync(@DrawableRes int resId) { + Drawable drawable = resId == 0 ? null : getContext().getDrawable(resId); + return () -> setTrackDrawable(drawable); + } + /** * Get the drawable used for the track that the switch slides within. * @@ -549,6 +563,23 @@ public class Switch extends CompoundButton { return mTrackDrawable; } + /** + * Set the drawable used for the track that the switch slides within to the specified Icon. + * + * @param icon an Icon holding the desired track, or {@code null} to clear + * the track + */ + @RemotableViewMethod(asyncImpl = "setTrackIconAsync") + public void setTrackIcon(@Nullable Icon icon) { + setTrackDrawable(icon == null ? null : icon.loadDrawable(getContext())); + } + + /** @hide **/ + public Runnable setTrackIconAsync(@Nullable Icon icon) { + Drawable track = icon == null ? null : icon.loadDrawable(getContext()); + return () -> setTrackDrawable(track); + } + /** * Applies a tint to the track drawable. Does not modify the current * tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. @@ -563,6 +594,7 @@ public class Switch extends CompoundButton { * @see #getTrackTintList() * @see Drawable#setTintList(ColorStateList) */ + @RemotableViewMethod public void setTrackTintList(@Nullable ColorStateList tint) { mTrackTintList = tint; mHasTrackTint = true; @@ -607,6 +639,7 @@ public class Switch extends CompoundButton { * @see #getTrackTintMode() * @see Drawable#setTintBlendMode(BlendMode) */ + @RemotableViewMethod public void setTrackTintBlendMode(@Nullable BlendMode blendMode) { mTrackBlendMode = blendMode; mHasTrackTintMode = true; @@ -686,10 +719,17 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_thumb */ + @RemotableViewMethod(asyncImpl = "setThumbResourceAsync") public void setThumbResource(@DrawableRes int resId) { setThumbDrawable(getContext().getDrawable(resId)); } + /** @hide **/ + public Runnable setThumbResourceAsync(@DrawableRes int resId) { + Drawable drawable = resId == 0 ? null : getContext().getDrawable(resId); + return () -> setThumbDrawable(drawable); + } + /** * Get the drawable used for the switch "thumb" - the piece that the user * can physically touch and drag along the track. @@ -703,6 +743,24 @@ public class Switch extends CompoundButton { return mThumbDrawable; } + /** + * Set the drawable used for the switch "thumb" - the piece that the user + * can physically touch and drag along the track - to the specified Icon. + * + * @param icon an Icon holding the desired thumb, or {@code null} to clear + * the thumb + */ + @RemotableViewMethod(asyncImpl = "setThumbIconAsync") + public void setThumbIcon(@Nullable Icon icon) { + setThumbDrawable(icon == null ? null : icon.loadDrawable(getContext())); + } + + /** @hide **/ + public Runnable setThumbIconAsync(@Nullable Icon icon) { + Drawable track = icon == null ? null : icon.loadDrawable(getContext()); + return () -> setThumbDrawable(track); + } + /** * Applies a tint to the thumb drawable. Does not modify the current * tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. @@ -717,6 +775,7 @@ public class Switch extends CompoundButton { * @see #getThumbTintList() * @see Drawable#setTintList(ColorStateList) */ + @RemotableViewMethod public void setThumbTintList(@Nullable ColorStateList tint) { mThumbTintList = tint; mHasThumbTint = true; @@ -761,6 +820,7 @@ public class Switch extends CompoundButton { * @see #getThumbTintMode() * @see Drawable#setTintBlendMode(BlendMode) */ + @RemotableViewMethod public void setThumbTintBlendMode(@Nullable BlendMode blendMode) { mThumbBlendMode = blendMode; mHasThumbTintMode = true; @@ -822,6 +882,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_splitTrack */ + @RemotableViewMethod public void setSplitTrack(boolean splitTrack) { mSplitTrack = splitTrack; invalidate(); @@ -852,6 +913,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_textOn */ + @RemotableViewMethod public void setTextOn(CharSequence textOn) { mTextOn = textOn; requestLayout(); @@ -875,6 +937,7 @@ public class Switch extends CompoundButton { * * @attr ref android.R.styleable#Switch_textOff */ + @RemotableViewMethod public void setTextOff(CharSequence textOff) { mTextOff = textOff; requestLayout(); @@ -889,6 +952,7 @@ public class Switch extends CompoundButton { * @param showText {@code true} to display on/off text * @attr ref android.R.styleable#Switch_showText */ + @RemotableViewMethod public void setShowText(boolean showText) { if (mShowText != showText) { mShowText = showText; -- cgit v1.2.3-59-g8ed1b