2010-11-22 07:33:13 +08: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.
|
2011-02-28 10:07:24 +08:00
|
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
2010-11-22 07:33:13 +08:00
|
|
|
*/
|
|
|
|
package com.phonegap;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import com.phonegap.api.Plugin;
|
|
|
|
import com.phonegap.api.PluginResult;
|
2011-06-30 07:23:20 +08:00
|
|
|
import java.util.HashMap;
|
2010-11-22 07:33:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class exposes methods in DroidGap that can be called from JavaScript.
|
|
|
|
*/
|
|
|
|
public class App extends Plugin {
|
2011-02-28 10:07:24 +08:00
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
/**
|
|
|
|
* Executes the request and returns PluginResult.
|
|
|
|
*
|
|
|
|
* @param action The action to execute.
|
|
|
|
* @param args JSONArry of arguments for the plugin.
|
|
|
|
* @param callbackId The callback id used when calling back into JavaScript.
|
|
|
|
* @return A PluginResult object with a status and message.
|
|
|
|
*/
|
|
|
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (action.equals("clearCache")) {
|
|
|
|
this.clearCache();
|
|
|
|
}
|
|
|
|
else if (action.equals("loadUrl")) {
|
|
|
|
this.loadUrl(args.getString(0), args.optJSONObject(1));
|
|
|
|
}
|
|
|
|
else if (action.equals("cancelLoadUrl")) {
|
|
|
|
this.cancelLoadUrl();
|
|
|
|
}
|
|
|
|
else if (action.equals("clearHistory")) {
|
|
|
|
this.clearHistory();
|
2011-02-28 10:07:24 +08:00
|
|
|
}
|
|
|
|
else if (action.equals("overrideBackbutton")) {
|
|
|
|
this.overrideBackbutton(args.getBoolean(0));
|
|
|
|
}
|
|
|
|
else if (action.equals("isBackbuttonOverridden")) {
|
|
|
|
boolean b = this.isBackbuttonOverridden();
|
|
|
|
return new PluginResult(status, b);
|
|
|
|
}
|
|
|
|
else if (action.equals("exitApp")) {
|
|
|
|
this.exitApp();
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
return new PluginResult(status, result);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// LOCAL METHODS
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the resource cache.
|
|
|
|
*/
|
|
|
|
public void clearCache() {
|
|
|
|
((DroidGap)this.ctx).clearCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the url into the webview.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @param props Properties that can be passed in to the DroidGap activity (i.e. loadingDialog, wait, ...)
|
|
|
|
* @throws JSONException
|
|
|
|
*/
|
|
|
|
public void loadUrl(String url, JSONObject props) throws JSONException {
|
|
|
|
System.out.println("App.loadUrl("+url+","+props+")");
|
|
|
|
int wait = 0;
|
2011-06-30 07:23:20 +08:00
|
|
|
boolean usePhoneGap = true;
|
|
|
|
boolean clearPrev = false;
|
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
// If there are properties, then set them on the Activity
|
2011-06-30 07:23:20 +08:00
|
|
|
HashMap<String, Object> params = new HashMap<String, Object>();
|
2010-11-22 07:33:13 +08:00
|
|
|
if (props != null) {
|
|
|
|
JSONArray keys = props.names();
|
|
|
|
for (int i=0; i<keys.length(); i++) {
|
|
|
|
String key = keys.getString(i);
|
|
|
|
if (key.equals("wait")) {
|
|
|
|
wait = props.getInt(key);
|
|
|
|
}
|
2011-06-30 07:23:20 +08:00
|
|
|
else if (key.equalsIgnoreCase("usephonegap")) {
|
|
|
|
usePhoneGap = props.getBoolean(key);
|
|
|
|
}
|
|
|
|
else if (key.equalsIgnoreCase("clearprev")) {
|
|
|
|
clearPrev = props.getBoolean(key);
|
|
|
|
}
|
2010-11-22 07:33:13 +08:00
|
|
|
else {
|
|
|
|
Object value = props.get(key);
|
|
|
|
if (value == null) {
|
2011-06-30 07:23:20 +08:00
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
else if (value.getClass().equals(String.class)) {
|
2011-06-30 07:23:20 +08:00
|
|
|
params.put(key, (String)value);
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
else if (value.getClass().equals(Boolean.class)) {
|
2011-06-30 07:23:20 +08:00
|
|
|
params.put(key, (Boolean)value);
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
else if (value.getClass().equals(Integer.class)) {
|
2011-06-30 07:23:20 +08:00
|
|
|
params.put(key, (Integer)value);
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-30 07:23:20 +08:00
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
// If wait property, then delay loading
|
2011-06-30 07:23:20 +08:00
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
if (wait > 0) {
|
2011-06-30 07:23:20 +08:00
|
|
|
try {
|
|
|
|
synchronized(this) {
|
|
|
|
this.wait(wait);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
2011-06-30 07:23:20 +08:00
|
|
|
((DroidGap)this.ctx).showWebPage(url, usePhoneGap, clearPrev, params);
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel loadUrl before it has been loaded.
|
|
|
|
*/
|
|
|
|
public void cancelLoadUrl() {
|
|
|
|
((DroidGap)this.ctx).cancelLoadUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear web history in this web view.
|
|
|
|
*/
|
|
|
|
public void clearHistory() {
|
|
|
|
((DroidGap)this.ctx).clearHistory();
|
|
|
|
}
|
|
|
|
|
2011-02-28 10:07:24 +08:00
|
|
|
/**
|
|
|
|
* Override the default behavior of the Android back button.
|
|
|
|
* If overridden, when the back button is pressed, the "backKeyDown" JavaScript event will be fired.
|
|
|
|
*
|
|
|
|
* @param override T=override, F=cancel override
|
|
|
|
*/
|
|
|
|
public void overrideBackbutton(boolean override) {
|
|
|
|
System.out.println("WARNING: Back Button Default Behaviour will be overridden. The backbutton event will be fired!");
|
|
|
|
((DroidGap)this.ctx).bound = override;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the Android back button is overridden by the user.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public boolean isBackbuttonOverridden() {
|
|
|
|
return ((DroidGap)this.ctx).bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exit the Android application.
|
|
|
|
*/
|
|
|
|
public void exitApp() {
|
|
|
|
((DroidGap)this.ctx).finish();
|
|
|
|
}
|
|
|
|
|
2010-11-22 07:33:13 +08:00
|
|
|
}
|