During the development of the new Chromium powered Android WebView, Internet celebrity Matt Gaunt showed me some rather interesting demonstrations. This included integrating with Google Play services, in the form of letting the user natively sign-in from the WebView. Now all the changes are publicly available I had to try such a thing myself, and found it surprisingly straightforward!
Two things are of note here. First, we define a callback to be triggered after a sign-in. Second, we make two calls to MainActivity” one to indicate the document is ready to be interacted with, and one to trigger a sign-in process. This is what we’ll need to support in our Android activity.
The first step to do that is to initialise our WebView. Its included in the activity by adding it to the layout file:
Next we need to configure it. We do this by finding the view, and setting some key parameters.
Half the magic then happens within this SignInInterface, which we define as a private class:
When that happens, we’ll get the normal onConnected or onConnectionFailed callbacks. The onConnected is the one we really care about, and as in the documentReady case, we’ll just call the signalWebview method we’re about to define on our activity.
In this case we evaluate a call to the handleSignIn method, which passes the display name of the connected user. Its worth noting there’s absolute no escaping on this, so if the user had a ‘ in their name, things would go wrong. Since a real app is likely to want multiple pieces of data, it would probably make sense to JSON encode the lot before passing it to the WebView.