App Development

Commit to Excellence: Commit Content API for Android

blog-featured-image commit to excellence

They say a picture is worth a thousand words. If that is true, an animated gif is worth a few books at least. If you could talk in images and gifs it would bring message threads to a new level. How do you leverage that information inside an app to make your conversations richer and more engaging? Why with Android 7.1’s new Commit Content API, of course.

With the Commit Content API you can now pull rich content into Messengers and the like, through the keyboard included in Android or even other keyboards that support rich content. This can be a variety of formats as long as both the keyboard and app support them. Supporting the Commit Content API in your app is fairly straightforward, so we’ll go through a simple setup in this post. For this demo, we’ll be using the Google Keyboard, which supports sending animated gifs to apps that support this new API.

The Commit Content API is new on API 25, but Google has added compat classes for using the Commit Content API down to API 13 in the support-v13 lib. First off, to keep things tidy we’ll make our own EditText to handle Commit Content with a callback that can be added using a simple interface. All we need to do is add a setter for the callback we’ll be using and override the onCreateInputConnection method to call said callback when needed:

public class CommitEditText extends AppCompatEditText {
    private CommitListener commitListener;

    // Put your usual View Constructors here

    @Override
    public InputConnection onCreateInputConnection(final EditorInfo info){
        final InputConnection ic = super.onCreateInputConnection(info); 
        EditorInfoCompat.setContentMimeTypes(info, new String[]{"image/gif"});
        final InputConnectionCompat.OnCommitContentListener callback = new InputConnectionCompat.OnCommitContentListener() {
                    @Override
                    public boolean onCommitContent(InputContentInfoCompat info, int flags, Bundle opts) {
                        if (BuildCompat.isAtLeastNMR1() && (flags & InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
                            try {
                                info.requestPermission();
                            } catch (Exception e) {
                                return false; // return false if failed
                            }
                        }
                        if(commitListener != null){
                            commitListener.onCommitContent(info.getLinkUri());
                        }
                        return true;  // return true if succeeded
                    }
                };
        return InputConnectionCompat.createWrapper(ic, info, callback);
    }

    public void setCommitListener(CommitListener listener) {
        this.commitListener = listener;
    }

    public interface CommitListener {
        void onCommitContent(Uri uri);
    }
}

The InputContentInfoCompat has methods to retrieve Uri’s for content either through a local ContentProvider Uri with .getContentUri() or via an optional http(s) Uri with .getLinkUri(). Once you have the Uri you can load it as usual with an image loading library such as Glide (which happens to have excellent animated gif support):

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ((CommitEditText) findViewById(R.id.edit)).setCommitListener(new CommitEditText.CommitListener() {
            @Override
            public void onCommitContent(Uri uri) {
                ImageView imageView = (ImageView) findViewById(R.id.image);
                GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
                Glide.with(MainActivity.this).load(uri).into(imageViewTarget);
            }
        });
    }
}

Usually this would be added to a Message thread or some other more useful form of UI, but for the sake of simplicity we’re just populating an ImageView to show off this new API. Pretend this is useful for a bit, but this what you end up with: Embedded content: http://i.giphy.com/l0MYrlE9SiVS8hhdu.gif Beautiful. Simple. Mind blowing.

So now that you’ve seen how easy it is to get content from keyboards, get some animated goodness into your apps. Let your users express themselves through a stream of endless gifs and give their typing fingers a break. If you want the full demo, visit this random github page, and if you want to learn more about the Commit Content API, see the Android documentation. Embedded content: https://media.giphy.com/media/GCLlQnV7wzKLu/giphy.gif

Quickstart-Guide-to-Kotlin-Multiplatform

A Quick Start Guide to Kotlin Multiplatform

Kotlin Multiplatform, though still experimental, is a great up-and-coming solution...

Read the article