android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Extensions for RemoteViews

Open HeyAlex opened this issue 8 years ago • 6 comments

Add extension for RemoteViews. PR with tests.

inline fun RemoteViews.setEnabled(@IdRes viewId: Int, enabled: Boolean) =
    setBoolean(viewId, "setEnabled", enabled)

Usage:

remoteView.setEnabled(R.id.test_imageView, false)
remoteView.setBackgroundResource(R.id.test_textView, R.drawable.box)
remoteView.setTextViewPaintFlags(R.id.test_textView, Paint.UNDERLINE_TEXT_FLAG)
remoteView.setBackgroundColor(R.id.test_imageView, Color.RED)
remoteView.setImageViewMaxHeight(R.id.test_imageView, testDimension)
remoteView.setImageViewMaxWidth(R.id.test_imageView, testDimension)
remoteView.setImageViewAlpha(R.id.test_imageView, 70)
remoteView.setTextViewTextSize(R.id.test_textView, testTextSize)
remoteView.setTextViewMinLines(R.id.test_textView, 8)
remoteView.setTextViewMaxLines(R.id.test_textView, 6)

HeyAlex avatar Feb 09 '18 12:02 HeyAlex

I don't know anything about this. So i'll have to learn before merging...

JakeWharton avatar Feb 27 '18 23:02 JakeWharton

@JakeWharton RemoteViews is intended for custom notifications and widgets. So view's methods that have annotation (@RemotableViewMethod) can be invoked in RemoteViews, but there is no any methods for that. So the only way to invoke these methods is to use setBooleans/setInt/setDouble and name of the method. Kotlin extensions is perfectly solve this problem.

HeyAlex avatar Feb 28 '18 08:02 HeyAlex

Yeah I've used remote views with widgets a bunch. Where's the documentation for these strings though? How do I know this is the complete subset?

JakeWharton avatar Feb 28 '18 13:02 JakeWharton

@JakeWharton As i understand where is no documentation for this thing. So if you want to set imageView resource, RemoteViews contains:

    /**
     * Equivalent to calling ImageView.setImageResource
     *
     * @param viewId The id of the view whose drawable should change
     * @param srcId The new resource id for the drawable
     */
    public void setImageViewResource(int viewId, int srcId) {
        setInt(viewId, "setImageResource", srcId);
    }

But if you want to set alpha for that imageView, you need to make:

setInt(viewId, "setImageAlpha", alpha)

The way is to grep code by annotation (@android.view.RemotableViewMethod) (something like that here. Don't forget that some methods already contains RemoteViews like setImageResource ). I just add the most popular methods (that i used a lot aswell), so it's not a complete subset (don't know, should i add them all, cuz it isn't that popular case).

HeyAlex avatar Feb 28 '18 14:02 HeyAlex

Is there an existing ticket to get these methods added to the Android SDK itself? Does this belong in a different support library like v4 compat?

NightlyNexus avatar Mar 01 '18 00:03 NightlyNexus

Actually i didnt find any related issues on google tracker. Seems like it might be in support v4 compat as you said. @JakeWharton Should i close this PR, cuz i still don't know what the best approach (since it works like a charm and i'm using the same extensions on work). Is it still suitable for ktx?.

HeyAlex avatar Mar 02 '18 12:03 HeyAlex