Getting a good authentication implementation is quite difficult and tedious, and there are not so many options. We chose OAuth because of its simplicity for the user, but it has a big disadvantage: There must be a web browser to use it. Making a native Android app with this restriction could be problematic.
In Koliseo Checkpoint Charlie we support 6 OAuth providers. In Android world, this can only be achieved integrating a WebView in our app to handle all the OAuth flow. This means, as we told before, that we’ve embedded a browser inside our app.
The important thing here is, how can we take advantage of using a WebView? Where do we have to write our code to integrate it with this widget?
For those who have never used a WebView, first thing we must do is to declare it in our layout. This time, we’re going to declare it inside our xml layout file (which is the recommended way for layouts in Android, althought you can declare it on Java code too).
In just a few lines, we’ve declared an embedded browser in our app, and it’s ready to be used. Remember that this browser is WebKit based (as Safari and Chrome are). In order to use it, we must give internet connection to our app in app’s manifest.
Now, inside our Activity, we should find the WebView widget and work with it:
Here, we’re introducing a layout to show a loading image when embedded browser is working and fetching the specified URL:
The most important part when we’re trying to place our code into WebView operations is to create a class extending
WebViewClient. This class will give us some callbacks to make things easier:
onLoadResource(WebView view, String url): Notifies the app when a specified resource is going to be loaded.
onPageFinished(WebView view, String url): Notifies the app when an URL has been loaded.
onPageStarted(WebView view, String url, Bitmap favicon): Notifies the app when URL loading is about to start.
onReceivedError(WebView view, int errorCode, String description, String failingUrl): Notifies when a connection fails with a given
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error): Tells about a SSL error.
shouldOverrideUrlLoading (WebView view, String url): This gives us the chance to modify the behaviour of URL load when returning false.
This class will be nested into the main Activity in order to make access to android layout resources easier (views specially). This saves us using Intents or Handler’s to show or hide Views.
To make this work, we only have to make the request to the desired URL:
You can read this article in spanish in Extrema Sistema’s blog!