3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 package org.appspot.apprtc;
30 import android.annotation.SuppressLint;
31 import android.app.Activity;
32 import android.util.Log;
33 import android.webkit.ConsoleMessage;
34 import android.webkit.JavascriptInterface;
35 import android.webkit.WebChromeClient;
36 import android.webkit.WebView;
37 import android.webkit.WebViewClient;
40 * Java-land version of Google AppEngine's JavaScript Channel API:
41 * https://developers.google.com/appengine/docs/python/channel/javascript
43 * Requires a hosted HTML page that opens the desired channel and dispatches JS
44 * on{Open,Message,Close,Error}() events to a global object named
45 * "androidMessageHandler".
47 public class GAEChannelClient {
48 private static final String TAG = "GAERTCClient";
49 private WebView webView;
50 private final ProxyingMessageHandler proxyingMessageHandler;
53 * Callback interface for messages delivered on the Google AppEngine channel.
55 public interface GAEMessageHandler {
57 public void onMessage(final String data);
58 public void onClose();
59 public void onError(final int code, final String description);
62 /** Asynchronously open an AppEngine channel. */
63 @SuppressLint("SetJavaScriptEnabled")
64 public GAEChannelClient(
65 Activity activity, String token, GAEMessageHandler handler) {
66 webView = new WebView(activity);
67 webView.getSettings().setJavaScriptEnabled(true);
68 webView.setWebChromeClient(new WebChromeClient() { // Purely for debugging.
69 public boolean onConsoleMessage (ConsoleMessage msg) {
70 Log.d(TAG, "console: " + msg.message() + " at " +
71 msg.sourceId() + ":" + msg.lineNumber());
75 webView.setWebViewClient(new WebViewClient() { // Purely for debugging.
76 public void onReceivedError(
77 WebView view, int errorCode, String description,
79 Log.e(TAG, "JS error: " + errorCode + " in " + failingUrl +
80 ", desc: " + description);
83 proxyingMessageHandler = new ProxyingMessageHandler(handler, token);
84 webView.addJavascriptInterface(
85 proxyingMessageHandler, "androidMessageHandler");
86 webView.loadUrl("file:///android_asset/channel.html");
89 /** Close the connection to the AppEngine channel. */
91 if (webView == null) {
94 proxyingMessageHandler.disconnect();
95 webView.removeJavascriptInterface("androidMessageHandler");
96 webView.loadUrl("about:blank");
100 // Helper class for proxying callbacks from the Java<->JS interaction
101 // (private, background) thread.
102 private static class ProxyingMessageHandler {
103 private final GAEMessageHandler handler;
104 private boolean disconnected = false;
105 private final String token;
107 public ProxyingMessageHandler(GAEMessageHandler handler, String token) {
108 this.handler = handler;
112 public void disconnect() {
117 public String getToken() {
122 public void onOpen() {
129 public void onMessage(final String data) {
131 handler.onMessage(data);
136 public void onClose() {
143 public void onError(final int code, final String description) {
145 handler.onError(code, description);