2010-08-27 03:15:34 +08:00
|
|
|
package com.phonegap;
|
2009-04-02 07:56:43 +08:00
|
|
|
/* License (MIT)
|
|
|
|
* Copyright (c) 2008 Nitobi
|
|
|
|
* website: http://phonegap.com
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* Software), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-06-05 05:54:16 +08:00
|
|
|
|
2010-07-29 08:26:23 +08:00
|
|
|
import com.phonegap.api.Command;
|
2010-09-04 03:14:28 +08:00
|
|
|
import java.util.HashMap;
|
2010-09-04 03:27:06 +08:00
|
|
|
import java.util.Map.Entry;
|
2010-07-29 08:26:23 +08:00
|
|
|
import com.phonegap.api.CommandManager;
|
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
2009-12-17 03:09:32 +08:00
|
|
|
import android.content.DialogInterface;
|
2009-07-18 07:23:18 +08:00
|
|
|
import android.content.Intent;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.content.res.Configuration;
|
2010-06-05 05:54:16 +08:00
|
|
|
import android.graphics.Bitmap;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.graphics.Color;
|
2010-04-24 07:18:11 +08:00
|
|
|
import android.net.Uri;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2009-12-17 04:50:08 +08:00
|
|
|
import android.view.KeyEvent;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.view.ViewGroup;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.view.Window;
|
|
|
|
import android.view.WindowManager;
|
|
|
|
import android.webkit.JsResult;
|
|
|
|
import android.webkit.WebChromeClient;
|
2009-11-10 09:22:36 +08:00
|
|
|
import android.webkit.WebSettings;
|
2009-11-26 09:25:16 +08:00
|
|
|
import android.webkit.WebStorage;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.webkit.WebView;
|
2010-04-24 07:18:11 +08:00
|
|
|
import android.webkit.WebViewClient;
|
2010-07-08 06:18:14 +08:00
|
|
|
import android.webkit.GeolocationPermissions.Callback;
|
2009-11-13 02:51:22 +08:00
|
|
|
import android.webkit.WebSettings.LayoutAlgorithm;
|
2010-07-31 03:23:55 +08:00
|
|
|
import android.widget.ImageView;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.widget.LinearLayout;
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2010-08-27 22:31:57 +08:00
|
|
|
/**
|
|
|
|
* This class is the main Android activity that represents the PhoneGap
|
|
|
|
* application. It should be extended by the user to load the specific
|
|
|
|
* html file that contains the application.
|
|
|
|
*
|
|
|
|
* As an example:
|
|
|
|
*
|
|
|
|
* package com.phonegap.examples;
|
|
|
|
* import android.app.Activity;
|
|
|
|
* import android.os.Bundle;
|
|
|
|
* import com.phonegap.*;
|
|
|
|
*
|
|
|
|
* public class Examples extends DroidGap {
|
|
|
|
* @Override
|
|
|
|
* public void onCreate(Bundle savedInstanceState) {
|
|
|
|
* super.onCreate(savedInstanceState);
|
2010-09-04 05:00:21 +08:00
|
|
|
* super.addModule("com.phonegap.examples.MyModule", "MyModule");
|
2010-08-27 22:31:57 +08:00
|
|
|
* super.loadUrl("file:///android_asset/www/index.html");
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2009-04-02 07:56:43 +08:00
|
|
|
public class DroidGap extends Activity {
|
2009-11-26 09:25:16 +08:00
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
private static final String LOG_TAG = "DroidGap";
|
2010-09-03 00:27:48 +08:00
|
|
|
|
|
|
|
protected WebView appView; // The webview for our app
|
2010-07-31 03:23:55 +08:00
|
|
|
protected ImageView splashScreen;
|
2010-09-03 03:54:35 +08:00
|
|
|
protected Boolean loadInWebView = false;
|
2009-11-26 09:25:16 +08:00
|
|
|
private LinearLayout root;
|
2009-11-25 08:51:23 +08:00
|
|
|
|
2010-06-09 07:30:20 +08:00
|
|
|
private Device gap;
|
2010-06-09 06:55:57 +08:00
|
|
|
private BrowserKey mKey;
|
2010-09-04 03:01:24 +08:00
|
|
|
public CallbackServer callbackServer;
|
2010-07-29 08:26:23 +08:00
|
|
|
private CommandManager commandManager;
|
2010-07-31 03:23:55 +08:00
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
private String url; // The initial URL for our app
|
|
|
|
private String baseUrl; // The base of the initial URL for our app
|
2010-09-04 03:01:24 +08:00
|
|
|
|
|
|
|
// Variables to manage ActivityResultCallbacks
|
|
|
|
private int activityResultCallbackCounter = 1000;
|
|
|
|
private HashMap<Integer,ActivityResultModule> activityResultCallbacks = new HashMap<Integer,ActivityResultModule>();
|
|
|
|
|
2010-09-04 03:14:28 +08:00
|
|
|
// List of modules started and managed
|
|
|
|
private HashMap<String,Module>modules = new HashMap<String,Module>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*
|
|
|
|
* @param savedInstanceState
|
|
|
|
*/
|
2009-04-02 07:56:43 +08:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2010-07-31 03:23:55 +08:00
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
|
|
|
|
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
2009-11-25 08:51:23 +08:00
|
|
|
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
|
2010-07-31 03:23:55 +08:00
|
|
|
|
2009-11-25 08:51:23 +08:00
|
|
|
root = new LinearLayout(this);
|
|
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
root.setBackgroundColor(Color.BLACK);
|
2010-07-31 03:23:55 +08:00
|
|
|
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT, 0.0F));
|
|
|
|
|
2010-08-26 13:52:37 +08:00
|
|
|
/*
|
2010-07-31 03:23:55 +08:00
|
|
|
splashScreen = new ImageView(this);
|
|
|
|
splashScreen.setLayoutParams(new LinearLayout.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
1.0F));
|
|
|
|
splashScreen.setImageResource(R.drawable.splash);
|
|
|
|
|
2010-08-26 13:52:37 +08:00
|
|
|
root.addView(splashScreen);
|
|
|
|
*/
|
|
|
|
|
2010-07-31 03:23:55 +08:00
|
|
|
initWebView();
|
2009-11-26 09:25:16 +08:00
|
|
|
|
2010-08-18 02:26:39 +08:00
|
|
|
root.addView(appView);
|
2010-07-31 03:23:55 +08:00
|
|
|
|
|
|
|
setContentView(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initWebView() {
|
|
|
|
appView = new WebView(DroidGap.this);
|
|
|
|
|
|
|
|
appView.setLayoutParams(new LinearLayout.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
1.0F));
|
|
|
|
|
2009-11-26 09:25:16 +08:00
|
|
|
WebViewReflect.checkCompatibility();
|
2010-07-31 03:23:55 +08:00
|
|
|
|
|
|
|
if (android.os.Build.VERSION.RELEASE.startsWith("2.")) {
|
|
|
|
appView.setWebChromeClient(new EclairClient(DroidGap.this));
|
2010-08-27 22:31:57 +08:00
|
|
|
}
|
|
|
|
else {
|
2010-07-31 03:23:55 +08:00
|
|
|
appView.setWebChromeClient(new GapClient(DroidGap.this));
|
2009-12-09 06:08:48 +08:00
|
|
|
}
|
2010-07-07 02:43:25 +08:00
|
|
|
|
2010-08-25 02:19:22 +08:00
|
|
|
appView.setWebViewClient(new GapViewClient(this));
|
|
|
|
|
2009-11-13 07:48:58 +08:00
|
|
|
appView.setInitialScale(100);
|
2009-12-04 02:47:41 +08:00
|
|
|
appView.setVerticalScrollBarEnabled(false);
|
2010-08-18 02:26:39 +08:00
|
|
|
appView.requestFocusFromTouch();
|
|
|
|
|
2009-11-10 09:22:36 +08:00
|
|
|
WebSettings settings = appView.getSettings();
|
|
|
|
settings.setJavaScriptEnabled(true);
|
|
|
|
settings.setJavaScriptCanOpenWindowsAutomatically(true);
|
2009-11-13 02:51:22 +08:00
|
|
|
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
|
2009-11-26 09:25:16 +08:00
|
|
|
|
|
|
|
Package pack = this.getClass().getPackage();
|
|
|
|
String appPackage = pack.getName();
|
|
|
|
|
|
|
|
WebViewReflect.setStorage(settings, true, "/data/data/" + appPackage + "/app_database/");
|
|
|
|
|
2010-03-03 03:14:20 +08:00
|
|
|
// Turn on DOM storage!
|
|
|
|
WebViewReflect.setDomStorage(settings);
|
2010-03-24 07:37:30 +08:00
|
|
|
// Turn off native geolocation object in browser - we use our own :)
|
2010-07-08 06:18:14 +08:00
|
|
|
WebViewReflect.setGeolocationEnabled(settings, true);
|
2010-07-31 03:23:55 +08:00
|
|
|
// Bind the appView object to the gap class methods
|
2009-04-02 07:56:43 +08:00
|
|
|
bindBrowser(appView);
|
2010-09-04 06:24:55 +08:00
|
|
|
if (this.getModule("com.phonegap.Storage") != null) {
|
|
|
|
Storage cupcakeStorage = (Storage)this.getModule("com.phonegap.Storage");
|
|
|
|
cupcakeStorage.setStorage(appPackage);
|
2010-08-27 22:31:57 +08:00
|
|
|
}
|
2010-07-08 06:18:14 +08:00
|
|
|
}
|
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
@Override
|
2010-08-12 23:51:12 +08:00
|
|
|
/**
|
|
|
|
* Called by the system when the device configuration changes while your activity is running.
|
|
|
|
*
|
|
|
|
* @param Configuration newConfig
|
|
|
|
*/
|
2009-04-02 07:56:43 +08:00
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
2010-08-20 04:41:06 +08:00
|
|
|
//don't reload the current page when the orientation is changed
|
|
|
|
super.onConfigurationChanged(newConfig);
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|
2010-08-12 23:51:12 +08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when the system is about to start resuming a previous activity.
|
|
|
|
*/
|
|
|
|
protected void onPause(){
|
|
|
|
super.onPause();
|
2010-08-20 04:41:06 +08:00
|
|
|
|
2010-09-04 03:14:28 +08:00
|
|
|
// Forward to modules
|
|
|
|
java.util.Set<Entry<String,Module>> s = this.modules.entrySet();
|
|
|
|
java.util.Iterator<Entry<String,Module>> it = s.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Entry<String,Module> entry = it.next();
|
|
|
|
Module module = entry.getValue();
|
|
|
|
module.onPause();
|
|
|
|
}
|
|
|
|
|
2010-08-20 04:41:06 +08:00
|
|
|
// Send pause event to JavaScript
|
2010-08-27 22:31:57 +08:00
|
|
|
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
|
2010-08-12 23:51:12 +08:00
|
|
|
|
|
|
|
// Pause JavaScript timers (including setInterval)
|
|
|
|
appView.pauseTimers();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when the activity will start interacting with the user.
|
|
|
|
*/
|
|
|
|
protected void onResume(){
|
|
|
|
super.onResume();
|
2010-08-20 04:41:06 +08:00
|
|
|
|
2010-09-04 03:14:28 +08:00
|
|
|
// Forward to modules
|
|
|
|
java.util.Set<Entry<String,Module>> s = this.modules.entrySet();
|
|
|
|
java.util.Iterator<Entry<String,Module>> it = s.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Entry<String,Module> entry = it.next();
|
|
|
|
Module module = entry.getValue();
|
|
|
|
module.onResume();
|
|
|
|
}
|
|
|
|
|
2010-08-20 04:41:06 +08:00
|
|
|
// Send resume event to JavaScript
|
2010-08-27 22:31:57 +08:00
|
|
|
appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
|
2010-08-12 23:51:12 +08:00
|
|
|
|
|
|
|
// Resume JavaScript timers (including setInterval)
|
|
|
|
appView.resumeTimers();
|
|
|
|
}
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2010-08-12 23:51:12 +08:00
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* The final call you receive before your activity is destroyed.
|
|
|
|
*/
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
// Make sure pause event is sent if onPause hasn't been called before onDestroy
|
2010-08-27 22:31:57 +08:00
|
|
|
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
|
2010-08-26 23:39:02 +08:00
|
|
|
|
2010-08-12 23:51:12 +08:00
|
|
|
// Load blank page so that JavaScript onunload is called
|
|
|
|
appView.loadUrl("about:blank");
|
|
|
|
|
|
|
|
// Clean up objects
|
2010-08-19 02:12:53 +08:00
|
|
|
if (mKey != null) {
|
|
|
|
|
|
|
|
}
|
2010-09-04 03:14:28 +08:00
|
|
|
|
|
|
|
// Clean up modules
|
|
|
|
java.util.Set<Entry<String,Module>> s = this.modules.entrySet();
|
|
|
|
java.util.Iterator<Entry<String,Module>> it = s.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Entry<String,Module> entry = it.next();
|
|
|
|
Module module = entry.getValue();
|
|
|
|
module.onDestroy();
|
|
|
|
}
|
|
|
|
|
2010-08-19 02:12:53 +08:00
|
|
|
if (callbackServer != null) {
|
|
|
|
callbackServer.destroy();
|
|
|
|
}
|
2010-08-12 23:51:12 +08:00
|
|
|
}
|
|
|
|
|
2010-09-04 03:14:28 +08:00
|
|
|
private void bindBrowser(WebView appView) {
|
2010-08-19 02:12:53 +08:00
|
|
|
callbackServer = new CallbackServer();
|
2010-07-29 08:26:23 +08:00
|
|
|
commandManager = new CommandManager(appView, this);
|
2010-06-09 07:30:20 +08:00
|
|
|
gap = new Device(appView, this);
|
2010-06-09 06:55:57 +08:00
|
|
|
mKey = new BrowserKey(appView, this);
|
2009-09-12 05:30:24 +08:00
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
// This creates the new javascript interfaces for PhoneGap
|
2010-07-29 08:26:23 +08:00
|
|
|
appView.addJavascriptInterface(commandManager, "CommandManager");
|
2009-08-01 06:17:36 +08:00
|
|
|
appView.addJavascriptInterface(gap, "DroidGap");
|
2010-09-04 06:24:55 +08:00
|
|
|
this.addModule("com.phonegap.AccelListener", "Accel");
|
2010-09-04 03:27:06 +08:00
|
|
|
this.addModule("com.phonegap.CameraLauncher", "GapCam");
|
2010-09-04 06:24:55 +08:00
|
|
|
this.addModule("com.phonegap.ContactManager", "ContactHook");
|
|
|
|
this.addModule("com.phonegap.FileUtils", "FileUtil");
|
|
|
|
this.addModule("com.phonegap.NetworkManager", "NetworkManager");
|
|
|
|
this.addModule("com.phonegap.CompassListener", "CompassHook");
|
|
|
|
this.addModule("com.phonegap.CryptoHandler", "GapCrypto");
|
2010-06-09 06:55:57 +08:00
|
|
|
appView.addJavascriptInterface(mKey, "BackButton");
|
2010-09-04 06:24:55 +08:00
|
|
|
this.addModule("com.phonegap.AudioHandler", "GapAudio");
|
2010-08-19 02:12:53 +08:00
|
|
|
appView.addJavascriptInterface(callbackServer, "CallbackServer");
|
2010-08-26 11:37:25 +08:00
|
|
|
appView.addJavascriptInterface(new SplashScreen(this), "SplashScreen");
|
2010-03-04 09:07:15 +08:00
|
|
|
|
|
|
|
if (android.os.Build.VERSION.RELEASE.startsWith("1."))
|
2009-12-09 06:08:48 +08:00
|
|
|
{
|
2010-09-04 06:24:55 +08:00
|
|
|
this.addModule("com.phonegap.Storage", "droidStorage");
|
|
|
|
this.addModule("com.phonegap.GeoBroker", "Geo");
|
2010-09-04 03:14:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
/**
|
|
|
|
* Add module to be loaded and made available from JavaScript.
|
|
|
|
*
|
|
|
|
* @param className The class to load
|
|
|
|
* @param javascriptInterface Bind the object to Javascript so that the methods can be
|
|
|
|
* accessed from Javascript using this variable name.
|
|
|
|
*/
|
2010-09-04 06:24:55 +08:00
|
|
|
public Object addModule(String className, String javascriptInterface) {
|
2010-09-04 03:14:28 +08:00
|
|
|
System.out.println("DroidGap.addModule("+className+", "+javascriptInterface+")");
|
|
|
|
try {
|
|
|
|
Class cl = Class.forName(className);
|
|
|
|
Class partypes[] = new Class[2];
|
|
|
|
partypes[0] = android.webkit.WebView.class;
|
|
|
|
partypes[1] = com.phonegap.DroidGap.class;
|
|
|
|
java.lang.reflect.Constructor<Module> ct = cl.getConstructor(partypes);
|
|
|
|
Object arglist[] = new Object[2];
|
|
|
|
arglist[0] = this.appView;
|
|
|
|
arglist[1] = this;
|
|
|
|
Module module = ct.newInstance(arglist);
|
|
|
|
this.modules.put(className, module);
|
|
|
|
if (javascriptInterface != null) {
|
|
|
|
this.appView.addJavascriptInterface(module, javascriptInterface);
|
|
|
|
}
|
2010-09-04 06:24:55 +08:00
|
|
|
return module;
|
2010-09-04 03:14:28 +08:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.out.println("Error adding module "+className+".");
|
2009-12-09 06:08:48 +08:00
|
|
|
}
|
2010-09-04 06:24:55 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the loaded module.
|
|
|
|
*
|
|
|
|
* @param className The class of the loaded module.
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Object getModule(String className) {
|
|
|
|
Object module = this.modules.get(className);
|
|
|
|
return module;
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|
2009-11-26 05:27:57 +08:00
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
/**
|
|
|
|
* Load the url into the webview.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
*/
|
|
|
|
public void loadUrl(final String url) {
|
|
|
|
this.url = url;
|
|
|
|
int i = url.lastIndexOf('/');
|
|
|
|
if (i > 0) {
|
|
|
|
this.baseUrl = url.substring(0, i);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.baseUrl = this.url;
|
|
|
|
}
|
|
|
|
|
2010-08-08 03:19:04 +08:00
|
|
|
this.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
DroidGap.this.appView.loadUrl(url);
|
|
|
|
}
|
|
|
|
});
|
2009-11-25 08:51:23 +08:00
|
|
|
}
|
2010-08-19 02:12:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send JavaScript statement back to JavaScript.
|
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
*/
|
|
|
|
public void sendJavascript(String statement) {
|
|
|
|
this.callbackServer.sendJavascript(statement);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the port that the callback server is listening on.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getPort() {
|
|
|
|
return this.callbackServer.getPort();
|
|
|
|
}
|
2010-04-24 07:18:11 +08:00
|
|
|
|
2009-12-01 07:41:24 +08:00
|
|
|
/**
|
|
|
|
* Provides a hook for calling "alert" from javascript. Useful for
|
|
|
|
* debugging your javascript.
|
|
|
|
*/
|
|
|
|
public class GapClient extends WebChromeClient {
|
|
|
|
|
|
|
|
Context mCtx;
|
|
|
|
public GapClient(Context ctx)
|
|
|
|
{
|
|
|
|
mCtx = ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
|
|
|
|
Log.d(LOG_TAG, message);
|
|
|
|
// This shows the dialog box. This can be commented out for dev
|
|
|
|
AlertDialog.Builder alertBldr = new AlertDialog.Builder(mCtx);
|
2009-12-17 03:09:32 +08:00
|
|
|
GapOKDialog okHook = new GapOKDialog();
|
|
|
|
GapCancelDialog cancelHook = new GapCancelDialog();
|
2009-12-01 07:41:24 +08:00
|
|
|
alertBldr.setMessage(message);
|
|
|
|
alertBldr.setTitle("Alert");
|
2009-12-17 03:09:32 +08:00
|
|
|
alertBldr.setCancelable(true);
|
|
|
|
alertBldr.setPositiveButton("OK", okHook);
|
|
|
|
alertBldr.setNegativeButton("Cancel", cancelHook);
|
2009-12-01 07:41:24 +08:00
|
|
|
alertBldr.show();
|
|
|
|
result.confirm();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-17 03:09:32 +08:00
|
|
|
/*
|
|
|
|
* This is the Code for the OK Button
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class GapOKDialog implements DialogInterface.OnClickListener {
|
|
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public class GapCancelDialog implements DialogInterface.OnClickListener {
|
|
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
dialog.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-12-01 07:41:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public final class EclairClient extends GapClient
|
|
|
|
{
|
2010-02-27 03:25:28 +08:00
|
|
|
private String TAG = "PhoneGapLog";
|
2010-04-23 18:12:40 +08:00
|
|
|
private long MAX_QUOTA = 100 * 1024 * 1024;
|
2009-12-01 07:41:24 +08:00
|
|
|
|
|
|
|
public EclairClient(Context ctx) {
|
|
|
|
super(ctx);
|
|
|
|
// TODO Auto-generated constructor stub
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
|
|
|
|
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater)
|
|
|
|
{
|
2010-04-23 18:12:40 +08:00
|
|
|
Log.d(TAG, "event raised onExceededDatabaseQuota estimatedSize: " + Long.toString(estimatedSize) + " currentQuota: " + Long.toString(currentQuota) + " totalUsedQuota: " + Long.toString(totalUsedQuota));
|
2010-08-07 04:59:28 +08:00
|
|
|
|
2009-12-01 07:41:24 +08:00
|
|
|
if( estimatedSize < MAX_QUOTA)
|
2010-04-23 18:12:40 +08:00
|
|
|
{
|
|
|
|
//increase for 1Mb
|
2010-08-07 04:59:28 +08:00
|
|
|
long newQuota = estimatedSize;
|
2010-04-23 18:12:40 +08:00
|
|
|
Log.d(TAG, "calling quotaUpdater.updateQuota newQuota: " + Long.toString(newQuota) );
|
2009-12-01 07:41:24 +08:00
|
|
|
quotaUpdater.updateQuota(newQuota);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Set the quota to whatever it is and force an error
|
|
|
|
// TODO: get docs on how to handle this properly
|
|
|
|
quotaUpdater.updateQuota(currentQuota);
|
2010-04-23 18:12:40 +08:00
|
|
|
}
|
2009-12-01 07:41:24 +08:00
|
|
|
}
|
2010-04-23 16:38:02 +08:00
|
|
|
|
|
|
|
// console.log in api level 7: http://developer.android.com/guide/developing/debug-tasks.html
|
2010-06-09 06:55:57 +08:00
|
|
|
public void onConsoleMessage(String message, int lineNumber, String sourceID)
|
|
|
|
{
|
|
|
|
// This is a kludgy hack!!!!
|
|
|
|
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
|
|
|
|
}
|
2010-02-27 03:25:28 +08:00
|
|
|
|
2010-07-08 06:18:14 +08:00
|
|
|
@Override
|
|
|
|
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
super.onGeolocationPermissionsShowPrompt(origin, callback);
|
|
|
|
callback.invoke(origin, true, false);
|
|
|
|
}
|
|
|
|
|
2009-12-01 07:41:24 +08:00
|
|
|
}
|
|
|
|
|
2010-08-27 22:31:57 +08:00
|
|
|
/**
|
|
|
|
* The webview client receives notifications about appView
|
|
|
|
*/
|
2010-08-25 02:19:22 +08:00
|
|
|
public class GapViewClient extends WebViewClient {
|
|
|
|
|
2010-08-27 03:15:34 +08:00
|
|
|
// TODO: hide splash screen here
|
|
|
|
|
|
|
|
DroidGap mCtx;
|
2010-08-25 02:19:22 +08:00
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ctx
|
|
|
|
*/
|
|
|
|
public GapViewClient(DroidGap ctx) {
|
2010-08-25 02:19:22 +08:00
|
|
|
mCtx = ctx;
|
|
|
|
}
|
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
/**
|
|
|
|
* Give the host application a chance to take over the control when a new url
|
|
|
|
* is about to be loaded in the current WebView.
|
|
|
|
*
|
|
|
|
* @param view The WebView that is initiating the callback.
|
|
|
|
* @param url The url to be loaded.
|
|
|
|
* @return true to override, false for default behavior
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
|
|
|
|
|
|
|
// If dialing phone (tel:5551212)
|
|
|
|
if (url.startsWith(WebView.SCHEME_TEL)) {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_DIAL);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error dialing "+url+": "+ e.toString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If displaying map (geo:0,0?q=address)
|
|
|
|
else if (url.startsWith(WebView.SCHEME_GEO)) {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error showing map "+url+": "+ e.toString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If sending email (mailto:abc@corp.com)
|
|
|
|
else if (url.startsWith(WebView.SCHEME_MAILTO)) {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error sending email "+url+": "+ e.toString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If sms:5551212
|
|
|
|
else if (url.startsWith("sms:")) {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
intent.putExtra("address", url.substring(4));
|
|
|
|
intent.setType("vnd.android-dir/mms-sms");
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error sending sms "+url+":"+ e.toString());
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If http, https or file
|
|
|
|
else if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) {
|
|
|
|
|
|
|
|
int i = url.lastIndexOf('/');
|
|
|
|
String newBaseUrl = url;
|
|
|
|
if (i > 0) {
|
|
|
|
newBaseUrl = url.substring(0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If our app or file:, then load into our webview
|
2010-09-03 03:54:35 +08:00
|
|
|
if (loadInWebView || url.startsWith("file://") || mCtx.baseUrl.equals(newBaseUrl)) {
|
2010-08-26 23:39:02 +08:00
|
|
|
appView.loadUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not our application, let default viewer handle
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
startActivity(intent);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error loading url "+url+":"+ e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify the host application that a page has finished loading.
|
|
|
|
*
|
|
|
|
* @param view The webview initiating the callback.
|
|
|
|
* @param url The url of the page.
|
|
|
|
*/
|
|
|
|
@Override
|
2010-08-25 02:19:22 +08:00
|
|
|
public void onPageFinished (WebView view, String url) {
|
2010-08-26 23:39:02 +08:00
|
|
|
super.onPageFinished(view, url);
|
2010-08-25 02:19:22 +08:00
|
|
|
// Try firing the onNativeReady event in JS. If it fails because the JS is
|
|
|
|
// not loaded yet then just set a flag so that the onNativeReady can be fired
|
|
|
|
// from the JS side when the JS gets to that code.
|
2010-08-26 23:39:02 +08:00
|
|
|
appView.loadUrl("javascript:try{ PhoneGap.onNativeReady.fire();}catch(e){_nativeReady = true;}");
|
2010-08-25 02:19:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 04:50:08 +08:00
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event)
|
2010-08-26 13:32:31 +08:00
|
|
|
{
|
2010-06-09 06:55:57 +08:00
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
if (mKey.isBound())
|
|
|
|
{
|
|
|
|
//We fire an event here!
|
|
|
|
appView.loadUrl("javascript:document.keyEvent.backTrigger()");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-23 21:35:56 +08:00
|
|
|
// only go back if the webview tells you that it is possible to go back
|
|
|
|
if(appView.canGoBack())
|
|
|
|
{
|
|
|
|
appView.goBack();
|
|
|
|
}
|
|
|
|
else // if you can't go back, invoke behavior of super class
|
2010-06-09 06:55:57 +08:00
|
|
|
{
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
}
|
2009-12-17 04:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_MENU)
|
|
|
|
{
|
2010-06-09 06:55:57 +08:00
|
|
|
// This is where we launch the menu
|
2009-12-18 05:21:13 +08:00
|
|
|
appView.loadUrl("javascript:keyEvent.menuTrigger()");
|
2009-12-17 04:50:08 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-31 03:23:55 +08:00
|
|
|
/**
|
|
|
|
* Removes the splash screen from root view and adds the WebView
|
|
|
|
*/
|
|
|
|
public void hideSplashScreen() {
|
|
|
|
root.removeView(splashScreen);
|
|
|
|
root.addView(appView);
|
|
|
|
}
|
|
|
|
|
2010-09-04 03:01:24 +08:00
|
|
|
/**
|
|
|
|
* Any calls to Activity.startActivityForResult must go through ActivityResultCallback, so
|
|
|
|
* the result can be routed to them correctly.
|
|
|
|
*
|
|
|
|
* This is done to eliminate the need to modify DroidGap.java to receive activity results.
|
|
|
|
*
|
|
|
|
* @param intent The intent to start
|
|
|
|
* @param requestCode Identifies who to send the result to
|
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void startActivityForResult(Intent intent, int requestCode) throws RuntimeException {
|
2010-09-04 03:27:06 +08:00
|
|
|
if ((requestCode < 0) || this.activityResultCallbacks.containsKey(requestCode)) {
|
2010-09-04 03:01:24 +08:00
|
|
|
super.startActivityForResult(intent, requestCode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new RuntimeException("PhoneGap Exception: Do not call startActivityForResult() directly. Implement ActivityResultCallback instead.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add activity result callback to receive onActivityResult() callbacks.
|
|
|
|
*
|
|
|
|
* @param callback The callback class
|
|
|
|
* @return The request code to use for the callback
|
|
|
|
*/
|
|
|
|
public int addActivityResult(ActivityResultModule callback) {
|
|
|
|
int requestCode = this.activityResultCallbackCounter++;
|
|
|
|
this.activityResultCallbacks.put(requestCode, callback);
|
|
|
|
return requestCode;
|
|
|
|
}
|
|
|
|
|
2010-09-03 00:27:48 +08:00
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when an activity you launched exits, giving you the requestCode you started it with,
|
|
|
|
* the resultCode it returned, and any additional data from it.
|
|
|
|
*
|
|
|
|
* @param requestCode The request code originally supplied to startActivityForResult(),
|
|
|
|
* allowing you to identify who this result came from.
|
|
|
|
* @param resultCode The integer result code returned by the child activity through its setResult().
|
|
|
|
* @param data An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
|
|
|
*/
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, intent);
|
|
|
|
|
2010-09-04 03:01:24 +08:00
|
|
|
ActivityResultModule callback = this.activityResultCallbacks.get(requestCode);
|
|
|
|
if (callback != null) {
|
|
|
|
callback.onActivityResult(requestCode, resultCode, intent);
|
|
|
|
}
|
2010-07-31 03:23:55 +08:00
|
|
|
}
|
2009-09-12 05:30:24 +08:00
|
|
|
}
|