2010-10-18 15:31:16 -05:00
|
|
|
/*
|
|
|
|
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
|
|
|
|
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
|
|
|
* Copyright (c) 2010, IBM Corporation
|
|
|
|
*/
|
2010-08-19 13:35:31 -05:00
|
|
|
package com.phonegap;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
2011-06-01 02:54:18 +08:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2010-08-19 13:35:31 -05:00
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
2011-04-23 08:19:59 +08:00
|
|
|
import java.net.URLEncoder;
|
2010-08-19 13:35:31 -05:00
|
|
|
import java.util.LinkedList;
|
|
|
|
|
2011-06-01 02:54:18 +08:00
|
|
|
import android.util.Log;
|
|
|
|
|
2010-08-19 13:35:31 -05:00
|
|
|
/**
|
|
|
|
* This class provides a way for Java to run JavaScript in the web page that has loaded PhoneGap.
|
2010-10-26 15:09:54 -05:00
|
|
|
* The CallbackServer class implements an XHR server and a polling server with a list of JavaScript
|
|
|
|
* statements that are to be executed on the web page.
|
2010-08-19 13:35:31 -05:00
|
|
|
*
|
2010-10-26 15:09:54 -05:00
|
|
|
* The process flow for XHR is:
|
2010-08-19 13:35:31 -05:00
|
|
|
* 1. JavaScript makes an async XHR call.
|
|
|
|
* 2. The server holds the connection open until data is available.
|
|
|
|
* 3. The server writes the data to the client and closes the connection.
|
|
|
|
* 4. The server immediately starts listening for the next XHR call.
|
|
|
|
* 5. The client receives this XHR response, processes it.
|
|
|
|
* 6. The client sends a new async XHR request.
|
|
|
|
*
|
2010-10-18 15:31:16 -05:00
|
|
|
* The CallbackServer class requires the following permission in Android manifest file
|
|
|
|
* <uses-permission android:name="android.permission.INTERNET" />
|
2010-10-26 15:09:54 -05:00
|
|
|
*
|
|
|
|
* If the device has a proxy set, then XHR cannot be used, so polling must be used instead.
|
|
|
|
* This can be determined by the client by calling CallbackServer.usePolling().
|
|
|
|
*
|
|
|
|
* The process flow for polling is:
|
|
|
|
* 1. The client calls CallbackServer.getJavascript() to retrieve next statement.
|
|
|
|
* 2. If statement available, then client processes it.
|
|
|
|
* 3. The client repeats #1 in loop.
|
2010-08-19 13:35:31 -05:00
|
|
|
*/
|
|
|
|
public class CallbackServer implements Runnable {
|
|
|
|
|
2011-06-01 02:54:18 +08:00
|
|
|
private static final String LOG_TAG = "CallbackServer";
|
|
|
|
|
2010-08-19 13:35:31 -05:00
|
|
|
/**
|
|
|
|
* The list of JavaScript statements to be sent to JavaScript.
|
|
|
|
*/
|
|
|
|
private LinkedList<String> javascript;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The port to listen on.
|
|
|
|
*/
|
|
|
|
private int port;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server thread.
|
|
|
|
*/
|
|
|
|
private Thread serverThread;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates the server is running.
|
|
|
|
*/
|
|
|
|
private boolean active;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates that the JavaScript statements list is empty
|
|
|
|
*/
|
|
|
|
private boolean empty;
|
|
|
|
|
2010-10-26 15:09:54 -05:00
|
|
|
/**
|
|
|
|
* Indicates that polling should be used instead of XHR.
|
|
|
|
*/
|
2011-02-27 20:07:24 -06:00
|
|
|
private boolean usePolling = true;
|
2010-10-26 15:09:54 -05:00
|
|
|
|
2010-10-29 10:53:59 +08:00
|
|
|
/**
|
|
|
|
* Security token to prevent other apps from accessing this callback server via XHR
|
|
|
|
*/
|
|
|
|
private String token;
|
|
|
|
|
2010-08-19 13:35:31 -05:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public CallbackServer() {
|
|
|
|
//System.out.println("CallbackServer()");
|
|
|
|
this.active = false;
|
|
|
|
this.empty = true;
|
|
|
|
this.port = 0;
|
2010-11-10 14:19:17 -06:00
|
|
|
this.javascript = new LinkedList<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init callback server and start XHR if running local app.
|
|
|
|
*
|
|
|
|
* If PhoneGap app is loaded from file://, then we can use XHR
|
|
|
|
* otherwise we have to use polling due to cross-domain security restrictions.
|
|
|
|
*
|
|
|
|
* @param url The URL of the PhoneGap app being loaded
|
|
|
|
*/
|
|
|
|
public void init(String url) {
|
|
|
|
//System.out.println("CallbackServer.start("+url+")");
|
2011-10-21 16:29:55 -05:00
|
|
|
this.active = false;
|
|
|
|
this.empty = true;
|
|
|
|
this.port = 0;
|
|
|
|
this.javascript = new LinkedList<String>();
|
2010-11-10 14:19:17 -06:00
|
|
|
|
|
|
|
// Determine if XHR or polling is to be used
|
|
|
|
if ((url != null) && !url.startsWith("file://")) {
|
2010-10-26 15:09:54 -05:00
|
|
|
this.usePolling = true;
|
2010-11-10 14:19:17 -06:00
|
|
|
this.stopServer();
|
|
|
|
}
|
|
|
|
else if (android.net.Proxy.getDefaultHost() != null) {
|
|
|
|
this.usePolling = true;
|
|
|
|
this.stopServer();
|
2010-10-26 15:09:54 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.usePolling = false;
|
|
|
|
this.startServer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-21 16:29:55 -05:00
|
|
|
/**
|
|
|
|
* Re-init when loading a new HTML page into webview.
|
|
|
|
*
|
|
|
|
* @param url The URL of the PhoneGap app being loaded
|
|
|
|
*/
|
|
|
|
public void reinit(String url) {
|
|
|
|
this.stopServer();
|
|
|
|
this.init(url);
|
|
|
|
}
|
|
|
|
|
2010-10-26 15:09:54 -05:00
|
|
|
/**
|
2010-11-10 14:19:17 -06:00
|
|
|
* Return if polling is being used instead of XHR.
|
2010-10-26 15:09:54 -05:00
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean usePolling() {
|
|
|
|
return this.usePolling;
|
2010-08-19 13:35:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the port that this server is running on.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getPort() {
|
|
|
|
return this.port;
|
|
|
|
}
|
|
|
|
|
2010-10-29 10:53:59 +08:00
|
|
|
/**
|
|
|
|
* Get the security token that this server requires when calling getJavascript().
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getToken() {
|
|
|
|
return this.token;
|
|
|
|
}
|
|
|
|
|
2010-08-19 13:35:31 -05:00
|
|
|
/**
|
|
|
|
* Start the server on a new thread.
|
|
|
|
*/
|
|
|
|
public void startServer() {
|
|
|
|
//System.out.println("CallbackServer.startServer()");
|
|
|
|
this.active = false;
|
|
|
|
|
|
|
|
// Start server on new thread
|
|
|
|
this.serverThread = new Thread(this);
|
|
|
|
this.serverThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restart the server on a new thread.
|
|
|
|
*/
|
|
|
|
public void restartServer() {
|
|
|
|
|
|
|
|
// Stop server
|
|
|
|
this.stopServer();
|
|
|
|
|
|
|
|
// Start server again
|
|
|
|
this.startServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start running the server.
|
|
|
|
* This is called automatically when the server thread is started.
|
|
|
|
*/
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
// Start server
|
|
|
|
try {
|
|
|
|
this.active = true;
|
|
|
|
String request;
|
|
|
|
ServerSocket waitSocket = new ServerSocket(0);
|
|
|
|
this.port = waitSocket.getLocalPort();
|
2010-11-10 14:19:17 -06:00
|
|
|
//System.out.println("CallbackServer -- using port " +this.port);
|
2010-10-29 10:53:59 +08:00
|
|
|
this.token = java.util.UUID.randomUUID().toString();
|
2010-11-10 14:19:17 -06:00
|
|
|
//System.out.println("CallbackServer -- using token "+this.token);
|
2010-08-19 13:35:31 -05:00
|
|
|
|
|
|
|
while (this.active) {
|
|
|
|
//System.out.println("CallbackServer: Waiting for data on socket");
|
|
|
|
Socket connection = waitSocket.accept();
|
|
|
|
BufferedReader xhrReader = new BufferedReader(new InputStreamReader(connection.getInputStream()),40);
|
|
|
|
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
|
|
|
|
request = xhrReader.readLine();
|
2010-11-01 13:59:08 -05:00
|
|
|
String response = "";
|
2010-11-10 14:19:17 -06:00
|
|
|
//System.out.println("CallbackServerRequest="+request);
|
2011-01-13 14:45:15 -06:00
|
|
|
if (this.active && (request != null)) {
|
2011-01-06 00:29:17 +08:00
|
|
|
if (request.contains("GET")) {
|
2011-01-13 14:45:15 -06:00
|
|
|
|
|
|
|
// Get requested file
|
|
|
|
String[] requestParts = request.split(" ");
|
|
|
|
|
2011-01-06 00:29:17 +08:00
|
|
|
// Must have security token
|
2011-01-16 15:15:24 -06:00
|
|
|
if ((requestParts.length == 3) && (requestParts[1].substring(1).equals(this.token))) {
|
2011-01-06 00:29:17 +08:00
|
|
|
//System.out.println("CallbackServer -- Processing GET request");
|
2011-01-13 14:45:15 -06:00
|
|
|
|
2011-01-06 00:29:17 +08:00
|
|
|
// Wait until there is some data to send, or send empty data every 10 sec
|
|
|
|
// to prevent XHR timeout on the client
|
|
|
|
synchronized (this) {
|
|
|
|
while (this.empty) {
|
|
|
|
try {
|
|
|
|
this.wait(10000); // prevent timeout from happening
|
|
|
|
//System.out.println("CallbackServer>>> break <<<");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (Exception e) { }
|
2010-10-29 10:53:59 +08:00
|
|
|
}
|
2011-01-06 00:29:17 +08:00
|
|
|
}
|
2011-01-13 14:45:15 -06:00
|
|
|
|
2011-01-06 00:29:17 +08:00
|
|
|
// If server is still running
|
|
|
|
if (this.active) {
|
2011-01-13 14:45:15 -06:00
|
|
|
|
2011-01-06 00:29:17 +08:00
|
|
|
// If no data, then send 404 back to client before it times out
|
|
|
|
if (this.empty) {
|
|
|
|
//System.out.println("CallbackServer -- sending data 0");
|
|
|
|
response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//System.out.println("CallbackServer -- sending item");
|
2011-04-23 08:19:59 +08:00
|
|
|
response = "HTTP/1.1 200 OK\r\n\r\n";
|
|
|
|
String js = this.getJavascript();
|
2011-06-01 02:54:18 +08:00
|
|
|
if (js != null) {
|
2011-06-02 01:46:27 +08:00
|
|
|
response += encode(js, "UTF-8");
|
2011-06-01 02:54:18 +08:00
|
|
|
}
|
2011-01-06 00:29:17 +08:00
|
|
|
}
|
2010-10-29 10:53:59 +08:00
|
|
|
}
|
|
|
|
else {
|
2011-01-06 00:29:17 +08:00
|
|
|
response = "HTTP/1.1 503 Service Unavailable\r\n\r\n ";
|
2010-10-29 10:53:59 +08:00
|
|
|
}
|
2010-08-19 13:35:31 -05:00
|
|
|
}
|
2010-11-01 13:59:08 -05:00
|
|
|
else {
|
2011-01-06 00:29:17 +08:00
|
|
|
response = "HTTP/1.1 403 Forbidden\r\n\r\n ";
|
2010-11-01 13:59:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-01-06 00:29:17 +08:00
|
|
|
response = "HTTP/1.1 400 Bad Request\r\n\r\n ";
|
2010-10-29 10:53:59 +08:00
|
|
|
}
|
2011-01-06 00:29:17 +08:00
|
|
|
//System.out.println("CallbackServer: response="+response);
|
|
|
|
//System.out.println("CallbackServer: closing output");
|
|
|
|
output.writeBytes(response);
|
|
|
|
output.flush();
|
2011-01-13 14:45:15 -06:00
|
|
|
}
|
2011-01-06 00:29:17 +08:00
|
|
|
output.close();
|
|
|
|
xhrReader.close();
|
2010-08-19 13:35:31 -05:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
this.active = false;
|
|
|
|
//System.out.println("CallbackServer.startServer() - EXIT");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop server.
|
|
|
|
* This stops the thread that the server is running on.
|
|
|
|
*/
|
|
|
|
public void stopServer() {
|
|
|
|
//System.out.println("CallbackServer.stopServer()");
|
2010-11-10 14:19:17 -06:00
|
|
|
if (this.active) {
|
|
|
|
this.active = false;
|
2010-08-19 13:35:31 -05:00
|
|
|
|
2010-11-10 14:19:17 -06:00
|
|
|
// Break out of server wait
|
|
|
|
synchronized (this) {
|
|
|
|
this.notify();
|
|
|
|
}
|
2010-08-19 13:35:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy
|
|
|
|
*/
|
|
|
|
public void destroy() {
|
|
|
|
this.stopServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of JavaScript statements.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public int getSize() {
|
|
|
|
int size = this.javascript.size();
|
|
|
|
//System.out.println("getSize() = " + size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next JavaScript statement and remove from list.
|
|
|
|
*
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public String getJavascript() {
|
|
|
|
if (this.javascript.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String statement = this.javascript.remove(0);
|
|
|
|
//System.out.println("CallbackServer.getJavascript() = " + statement);
|
|
|
|
if (this.javascript.size() == 0) {
|
|
|
|
synchronized (this) {
|
|
|
|
this.empty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a JavaScript statement to the list.
|
|
|
|
*
|
|
|
|
* @param statement
|
|
|
|
*/
|
|
|
|
public void sendJavascript(String statement) {
|
|
|
|
//System.out.println("CallbackServer.sendJavascript("+statement+")");
|
|
|
|
this.javascript.add(statement);
|
|
|
|
synchronized (this) {
|
|
|
|
this.empty = false;
|
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-02 01:46:27 +08:00
|
|
|
/* The Following code has been modified from original implementation of URLEncoder */
|
|
|
|
|
|
|
|
/* start */
|
2011-06-01 02:54:18 +08:00
|
|
|
|
2011-06-02 01:46:27 +08:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
static final String digits = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will encode the return value to JavaScript. We revert the encoding for
|
|
|
|
* common characters that don't require encoding to reduce the size of the string
|
|
|
|
* being passed to JavaScript.
|
|
|
|
*
|
|
|
|
* @param s to be encoded
|
|
|
|
* @param enc encoding type
|
|
|
|
* @return encoded string
|
|
|
|
*/
|
|
|
|
public static String encode(String s, String enc) throws UnsupportedEncodingException {
|
|
|
|
if (s == null || enc == null) {
|
|
|
|
throw new NullPointerException();
|
|
|
|
}
|
|
|
|
// check for UnsupportedEncodingException
|
|
|
|
"".getBytes(enc);
|
|
|
|
|
|
|
|
// Guess a bit bigger for encoded form
|
|
|
|
StringBuilder buf = new StringBuilder(s.length() + 16);
|
|
|
|
int start = -1;
|
|
|
|
for (int i = 0; i < s.length(); i++) {
|
|
|
|
char ch = s.charAt(i);
|
|
|
|
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
|
|
|
|
|| (ch >= '0' && ch <= '9')
|
|
|
|
|| " .-*_'(),<>=?@[]{}:~\"\\/;!".indexOf(ch) > -1) {
|
|
|
|
if (start >= 0) {
|
|
|
|
convert(s.substring(start, i), buf, enc);
|
|
|
|
start = -1;
|
|
|
|
}
|
|
|
|
if (ch != ' ') {
|
|
|
|
buf.append(ch);
|
|
|
|
} else {
|
|
|
|
buf.append(' ');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (start < 0) {
|
|
|
|
start = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (start >= 0) {
|
|
|
|
convert(s.substring(start, s.length()), buf, enc);
|
|
|
|
}
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void convert(String s, StringBuilder buf, String enc) throws UnsupportedEncodingException {
|
|
|
|
byte[] bytes = s.getBytes(enc);
|
|
|
|
for (int j = 0; j < bytes.length; j++) {
|
|
|
|
buf.append('%');
|
|
|
|
buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
|
|
|
|
buf.append(digits.charAt(bytes[j] & 0xf));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end */
|
2010-08-19 13:35:31 -05:00
|
|
|
}
|