2010-10-19 04:31:16 +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.
|
2009-04-02 07:56:43 +08:00
|
|
|
*
|
2010-10-19 04:31:16 +08:00
|
|
|
* Copyright (c) 2005-2010, Nitobi Software Inc.
|
2011-02-28 10:07:24 +08:00
|
|
|
* Copyright (c) 2010-2011, IBM Corporation
|
2009-04-02 07:56:43 +08:00
|
|
|
*/
|
2010-10-19 04:31:16 +08:00
|
|
|
package com.phonegap;
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2011-06-30 07:23:20 +08:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map.Entry;
|
2011-07-01 04:42:40 +08:00
|
|
|
|
2010-11-12 12:24:20 +08:00
|
|
|
import org.json.JSONArray;
|
2011-02-28 10:07:24 +08:00
|
|
|
import org.json.JSONException;
|
2011-05-31 03:12:31 +08:00
|
|
|
|
|
|
|
import android.app.Activity;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.app.AlertDialog;
|
2011-07-09 12:07:22 +08:00
|
|
|
import android.app.ProgressDialog;
|
2009-04-02 07:56:43 +08:00
|
|
|
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;
|
2011-06-22 01:08:42 +08:00
|
|
|
import android.content.pm.ApplicationInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.content.res.Configuration;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.graphics.Color;
|
2011-05-31 03:12:31 +08:00
|
|
|
import android.graphics.Rect;
|
2011-02-02 23:27:58 +08:00
|
|
|
import android.media.AudioManager;
|
2010-04-24 07:18:11 +08:00
|
|
|
import android.net.Uri;
|
2011-06-22 01:08:42 +08:00
|
|
|
import android.net.http.SslError;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
2011-06-01 06:38:03 +08:00
|
|
|
import android.view.Display;
|
2009-12-17 04:50:08 +08:00
|
|
|
import android.view.KeyEvent;
|
2011-03-09 12:00:33 +08:00
|
|
|
import android.view.View;
|
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;
|
2011-06-25 05:08:26 +08:00
|
|
|
import android.webkit.GeolocationPermissions.Callback;
|
|
|
|
import android.webkit.JsPromptResult;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.webkit.JsResult;
|
2011-06-22 01:08:42 +08:00
|
|
|
import android.webkit.SslErrorHandler;
|
2009-04-02 07:56:43 +08:00
|
|
|
import android.webkit.WebChromeClient;
|
2009-11-10 09:22:36 +08:00
|
|
|
import android.webkit.WebSettings;
|
2011-06-25 05:08:26 +08:00
|
|
|
import android.webkit.WebSettings.LayoutAlgorithm;
|
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;
|
2011-06-25 05:08:26 +08:00
|
|
|
import android.widget.EditText;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.widget.LinearLayout;
|
2011-06-25 05:08:26 +08:00
|
|
|
|
|
|
|
import com.phonegap.api.PhonegapActivity;
|
2011-07-08 03:11:03 +08:00
|
|
|
import com.phonegap.api.IPlugin;
|
2010-11-06 02:10:51 +08:00
|
|
|
import com.phonegap.api.PluginManager;
|
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-11-13 12:38:27 +08:00
|
|
|
*
|
2010-11-12 01:34:12 +08:00
|
|
|
* // Set properties for activity
|
2011-02-03 04:37:09 +08:00
|
|
|
* super.setStringProperty("loadingDialog", "Title,Message"); // show loading dialog
|
|
|
|
* super.setStringProperty("errorUrl", "file:///android_asset/www/error.html"); // if error loading file in super.loadUrl().
|
2010-11-13 12:38:27 +08:00
|
|
|
*
|
|
|
|
* // Initialize activity
|
|
|
|
* super.init();
|
2010-11-12 01:34:12 +08:00
|
|
|
*
|
|
|
|
* // Add your plugins here or in JavaScript
|
|
|
|
* super.addService("MyService", "com.phonegap.examples.MyService");
|
|
|
|
*
|
|
|
|
* // Clear cache if you want
|
|
|
|
* super.appView.clearCache(true);
|
|
|
|
*
|
|
|
|
* // Load your application
|
2011-02-03 04:37:09 +08:00
|
|
|
* super.setIntegerProperty("splashscreen", R.drawable.splash); // load splash.jpg image from the resource drawable directory
|
2010-11-13 12:38:27 +08:00
|
|
|
* super.loadUrl("file:///android_asset/www/index.html", 3000); // show splash screen 3 sec before loading app
|
2010-08-27 22:31:57 +08:00
|
|
|
* }
|
|
|
|
* }
|
2010-11-13 12:38:27 +08:00
|
|
|
*
|
|
|
|
* Properties: The application can be configured using the following properties:
|
2010-11-23 23:53:43 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Display a native loading dialog. Format for value = "Title,Message".
|
|
|
|
* // (String - default=null)
|
|
|
|
* super.setStringProperty("loadingDialog", "Wait,Loading Demo...");
|
2010-11-23 23:53:43 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Display a native loading dialog when loading sub-pages. Format for value = "Title,Message".
|
|
|
|
* // (String - default=null)
|
|
|
|
* super.setStringProperty("loadingPageDialog", "Loading page...");
|
2011-08-12 05:21:22 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Cause all links on web page to be loaded into existing web view,
|
|
|
|
* // instead of being loaded into new browser. (Boolean - default=false)
|
|
|
|
* super.setBooleanProperty("loadInWebView", true);
|
2010-11-23 23:53:43 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Load a splash screen image from the resource drawable directory.
|
|
|
|
* // (Integer - default=0)
|
|
|
|
* super.setIntegerProperty("splashscreen", R.drawable.splash);
|
2011-08-12 05:21:22 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Set the background color.
|
|
|
|
* // (Integer - default=0 or BLACK)
|
|
|
|
* super.setIntegerProperty("backgroundColor", Color.WHITE);
|
2010-11-23 23:53:43 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Time in msec to wait before triggering a timeout error when loading
|
|
|
|
* // with super.loadUrl(). (Integer - default=20000)
|
|
|
|
* super.setIntegerProperty("loadUrlTimeoutValue", 60000);
|
2010-11-23 23:53:43 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // URL to load if there's an error loading specified URL with loadUrl().
|
|
|
|
* // Should be a local URL starting with file://. (String - default=null)
|
|
|
|
* super.setStringProperty("errorUrl", "file:///android_asset/www/error.html");
|
2010-11-13 12:38:27 +08:00
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* // Enable app to keep running in background. (Boolean - default=true)
|
|
|
|
* super.setBooleanProperty("keepRunning", false);
|
2010-08-27 22:31:57 +08:00
|
|
|
*/
|
2010-11-06 02:10:51 +08:00
|
|
|
public class DroidGap extends PhonegapActivity {
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// The webview for our app
|
|
|
|
protected WebView appView;
|
|
|
|
protected WebViewClient webViewClient;
|
|
|
|
|
|
|
|
protected LinearLayout root;
|
|
|
|
public boolean bound = false;
|
|
|
|
public CallbackServer callbackServer;
|
|
|
|
protected PluginManager pluginManager;
|
|
|
|
protected boolean cancelLoadUrl = false;
|
|
|
|
protected boolean clearHistory = false;
|
|
|
|
protected ProgressDialog spinnerDialog = null;
|
|
|
|
|
|
|
|
// The initial URL for our app
|
|
|
|
// ie http://server/path/index.html#abc?query
|
|
|
|
private String url;
|
|
|
|
|
|
|
|
// The base of the initial URL for our app.
|
|
|
|
// Does not include file name. Ends with /
|
|
|
|
// ie http://server/path/
|
|
|
|
private String baseUrl = null;
|
|
|
|
|
|
|
|
// Plugin to call when activity result is received
|
|
|
|
protected IPlugin activityResultCallback = null;
|
|
|
|
protected boolean activityResultKeepRunning;
|
|
|
|
|
|
|
|
// Flag indicates that a loadUrl timeout occurred
|
|
|
|
private int loadUrlTimeout = 0;
|
|
|
|
|
|
|
|
// Default background color for activity
|
|
|
|
// (this is not the color for the webview, which is set in HTML)
|
|
|
|
private int backgroundColor = Color.BLACK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The variables below are used to cache some of the activity properties.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Flag indicates that a URL navigated to from PhoneGap app should be loaded into same webview
|
|
|
|
// instead of being loaded into the web browser.
|
|
|
|
protected boolean loadInWebView = false;
|
|
|
|
|
|
|
|
// Draw a splash screen using an image located in the drawable resource directory.
|
|
|
|
// This is not the same as calling super.loadSplashscreen(url)
|
|
|
|
protected int splashscreen = 0;
|
|
|
|
|
|
|
|
// LoadUrl timeout value in msec (default of 20 sec)
|
|
|
|
protected int loadUrlTimeoutValue = 20000;
|
|
|
|
|
|
|
|
// Keep app running when pause is received. (default = true)
|
|
|
|
// If true, then the JavaScript and native code continue to run in the background
|
|
|
|
// when another application (activity) is started.
|
|
|
|
protected boolean keepRunning = true;
|
2010-11-13 12:38:27 +08:00
|
|
|
|
2010-09-04 03:14:28 +08:00
|
|
|
/**
|
|
|
|
* Called when the activity is first created.
|
|
|
|
*
|
|
|
|
* @param savedInstanceState
|
|
|
|
*/
|
2010-11-12 01:34:12 +08:00
|
|
|
@Override
|
2009-04-02 07:56:43 +08:00
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
2011-08-23 01:38:00 +08:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
|
|
|
|
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
|
|
|
|
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
|
|
|
|
// This builds the view. We could probably get away with NOT having a LinearLayout, but I like having a bucket!
|
|
|
|
|
|
|
|
Display display = getWindowManager().getDefaultDisplay();
|
|
|
|
int width = display.getWidth();
|
|
|
|
int height = display.getHeight();
|
|
|
|
|
|
|
|
root = new LinearLayoutSoftKeyboardDetect(this, width, height);
|
|
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
root.setBackgroundColor(this.backgroundColor);
|
|
|
|
root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT, 0.0F));
|
|
|
|
|
|
|
|
// If url was passed in to intent, then init webview, which will load the url
|
|
|
|
Bundle bundle = this.getIntent().getExtras();
|
|
|
|
if (bundle != null) {
|
|
|
|
String url = bundle.getString("url");
|
|
|
|
if (url != null) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Setup the hardware volume controls to handle volume control
|
|
|
|
setVolumeControlStream(AudioManager.STREAM_MUSIC);
|
2010-11-12 04:00:56 +08:00
|
|
|
}
|
|
|
|
|
2010-09-10 00:01:56 +08:00
|
|
|
/**
|
|
|
|
* Create and initialize web container.
|
|
|
|
*/
|
2011-08-23 01:38:00 +08:00
|
|
|
public void init() {
|
|
|
|
|
|
|
|
// Create web container
|
|
|
|
this.appView = new WebView(DroidGap.this);
|
|
|
|
this.appView.setId(100);
|
|
|
|
|
|
|
|
this.appView.setLayoutParams(new LinearLayout.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
1.0F));
|
2010-07-31 03:23:55 +08:00
|
|
|
|
2009-11-26 09:25:16 +08:00
|
|
|
WebViewReflect.checkCompatibility();
|
2010-07-31 03:23:55 +08:00
|
|
|
|
2011-05-11 00:44:09 +08:00
|
|
|
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.appView.setWebChromeClient(new GapClient(DroidGap.this));
|
2010-08-27 22:31:57 +08:00
|
|
|
}
|
|
|
|
else {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.appView.setWebChromeClient(new EclairClient(DroidGap.this));
|
2009-12-09 06:08:48 +08:00
|
|
|
}
|
2010-07-07 02:43:25 +08:00
|
|
|
|
2010-11-22 06:42:00 +08:00
|
|
|
this.setWebViewClient(this.appView, new GapViewClient(this));
|
2010-08-25 02:19:22 +08:00
|
|
|
|
2010-09-10 00:01:56 +08:00
|
|
|
this.appView.setInitialScale(100);
|
|
|
|
this.appView.setVerticalScrollBarEnabled(false);
|
|
|
|
this.appView.requestFocusFromTouch();
|
2010-08-18 02:26:39 +08:00
|
|
|
|
2010-09-10 00:01:56 +08:00
|
|
|
// Enable JavaScript
|
|
|
|
WebSettings settings = this.appView.getSettings();
|
2009-11-10 09:22:36 +08:00
|
|
|
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
|
|
|
|
2010-09-10 00:01:56 +08:00
|
|
|
// Enable database
|
2011-07-14 05:48:29 +08:00
|
|
|
settings.setDatabaseEnabled(true);
|
|
|
|
String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
|
|
|
|
settings.setDatabasePath(databasePath);
|
2010-09-10 00:01:56 +08:00
|
|
|
|
|
|
|
// Enable DOM storage
|
2010-03-03 03:14:20 +08:00
|
|
|
WebViewReflect.setDomStorage(settings);
|
2010-09-10 00:01:56 +08:00
|
|
|
|
|
|
|
// Enable built-in geolocation
|
2010-07-08 06:18:14 +08:00
|
|
|
WebViewReflect.setGeolocationEnabled(settings, true);
|
2010-09-10 00:01:56 +08:00
|
|
|
|
|
|
|
// Bind PhoneGap objects to JavaScript
|
|
|
|
this.bindBrowser(this.appView);
|
2010-11-12 01:34:12 +08:00
|
|
|
|
2011-03-09 12:00:33 +08:00
|
|
|
// Add web view but make it invisible while loading URL
|
|
|
|
this.appView.setVisibility(View.INVISIBLE);
|
2010-11-12 01:34:12 +08:00
|
|
|
root.addView(this.appView);
|
|
|
|
setContentView(root);
|
|
|
|
|
2010-11-22 06:42:00 +08:00
|
|
|
// Clear cancel flag
|
|
|
|
this.cancelLoadUrl = false;
|
|
|
|
|
|
|
|
// If url specified, then load it
|
|
|
|
String url = this.getStringProperty("url", null);
|
|
|
|
if (url != null) {
|
2011-08-23 01:38:00 +08:00
|
|
|
System.out.println("Loading initial URL="+url);
|
|
|
|
this.loadUrl(url);
|
2010-11-22 06:42:00 +08:00
|
|
|
}
|
2011-08-23 01:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the WebViewClient.
|
|
|
|
*
|
|
|
|
* @param appView
|
|
|
|
* @param client
|
|
|
|
*/
|
|
|
|
protected void setWebViewClient(WebView appView, WebViewClient client) {
|
|
|
|
this.webViewClient = client;
|
|
|
|
appView.setWebViewClient(client);
|
|
|
|
}
|
2010-11-22 06:42:00 +08:00
|
|
|
|
2010-11-12 04:00:56 +08:00
|
|
|
/**
|
|
|
|
* Bind PhoneGap objects to JavaScript.
|
|
|
|
*
|
|
|
|
* @param appView
|
|
|
|
*/
|
2011-08-23 01:38:00 +08:00
|
|
|
private void bindBrowser(WebView appView) {
|
|
|
|
this.callbackServer = new CallbackServer();
|
|
|
|
this.pluginManager = new PluginManager(appView, this);
|
2010-11-12 04:00:56 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
}
|
2010-11-12 04:00:56 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
/**
|
|
|
|
* Look at activity parameters and process them.
|
|
|
|
* This must be called from the main UI thread.
|
|
|
|
*/
|
|
|
|
private void handleActivityParameters() {
|
|
|
|
|
|
|
|
// Init web view if not already done
|
|
|
|
if (this.appView == null) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If backgroundColor
|
|
|
|
this.backgroundColor = this.getIntegerProperty("backgroundColor", Color.BLACK);
|
|
|
|
this.root.setBackgroundColor(this.backgroundColor);
|
|
|
|
|
|
|
|
// If spashscreen
|
|
|
|
this.splashscreen = this.getIntegerProperty("splashscreen", 0);
|
|
|
|
if (this.splashscreen != 0) {
|
|
|
|
root.setBackgroundResource(this.splashscreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If loadInWebView
|
|
|
|
this.loadInWebView = this.getBooleanProperty("loadInWebView", false);
|
|
|
|
|
|
|
|
// If loadUrlTimeoutValue
|
|
|
|
int timeout = this.getIntegerProperty("loadUrlTimeoutValue", 0);
|
|
|
|
if (timeout > 0) {
|
|
|
|
this.loadUrlTimeoutValue = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If keepRunning
|
|
|
|
this.keepRunning = this.getBooleanProperty("keepRunning", true);
|
|
|
|
}
|
|
|
|
|
2010-11-12 04:00:56 +08:00
|
|
|
/**
|
|
|
|
* Load the url into the webview.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
*/
|
2011-08-23 01:38:00 +08:00
|
|
|
public void loadUrl(final String url) {
|
|
|
|
System.out.println("loadUrl("+url+")");
|
|
|
|
this.url = url;
|
|
|
|
if (this.baseUrl == null) {
|
|
|
|
int i = url.lastIndexOf('/');
|
|
|
|
if (i > 0) {
|
|
|
|
this.baseUrl = url.substring(0, i+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.baseUrl = this.url + "/";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("url="+url+" baseUrl="+baseUrl);
|
|
|
|
|
|
|
|
// Load URL on UI thread
|
|
|
|
final DroidGap me = this;
|
|
|
|
this.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
|
|
|
|
// Handle activity parameters
|
|
|
|
me.handleActivityParameters();
|
|
|
|
|
|
|
|
// Initialize callback server
|
|
|
|
me.callbackServer.init(url);
|
|
|
|
|
|
|
|
// If loadingDialog, then show the App loading dialog
|
|
|
|
String loading = me.getStringProperty("loadingDialog", null);
|
|
|
|
if (loading != null) {
|
|
|
|
|
|
|
|
String title = "";
|
|
|
|
String message = "Loading Application...";
|
|
|
|
|
|
|
|
if (loading.length() > 0) {
|
|
|
|
int comma = loading.indexOf(',');
|
|
|
|
if (comma > 0) {
|
|
|
|
title = loading.substring(0, comma);
|
|
|
|
message = loading.substring(comma+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
title = "";
|
|
|
|
message = loading;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
me.spinnerStart(title, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a timeout timer for loadUrl
|
|
|
|
final int currentLoadUrlTimeout = me.loadUrlTimeout;
|
|
|
|
Runnable runnable = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
synchronized(this) {
|
|
|
|
wait(me.loadUrlTimeoutValue);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If timeout, then stop loading and handle error
|
|
|
|
if (me.loadUrlTimeout == currentLoadUrlTimeout) {
|
|
|
|
me.appView.stopLoading();
|
|
|
|
me.webViewClient.onReceivedError(me.appView, -6, "The connection to the server was unsuccessful.", url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Thread thread = new Thread(runnable);
|
|
|
|
thread.start();
|
|
|
|
me.appView.loadUrl(url);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the url into the webview after waiting for period of time.
|
|
|
|
* This is used to display the splashscreen for certain amount of time.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @param time The number of ms to wait before loading webview
|
|
|
|
*/
|
|
|
|
public void loadUrl(final String url, final int time) {
|
|
|
|
System.out.println("loadUrl("+url+","+time+")");
|
|
|
|
final DroidGap me = this;
|
|
|
|
|
|
|
|
// Handle activity parameters
|
|
|
|
this.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
me.handleActivityParameters();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Runnable runnable = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
synchronized(this) {
|
|
|
|
this.wait(time);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (!me.cancelLoadUrl) {
|
|
|
|
me.loadUrl(url);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
me.cancelLoadUrl = false;
|
|
|
|
System.out.println("Aborting loadUrl("+url+"): Another URL was loaded before timer expired.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Thread thread = new Thread(runnable);
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel loadUrl before it has been loaded.
|
|
|
|
*/
|
|
|
|
public void cancelLoadUrl() {
|
|
|
|
this.cancelLoadUrl = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the resource cache.
|
|
|
|
*/
|
|
|
|
public void clearCache() {
|
|
|
|
if (this.appView == null) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
this.appView.clearCache(true);
|
|
|
|
}
|
2010-11-22 06:42:00 +08:00
|
|
|
|
2010-11-13 12:38:27 +08:00
|
|
|
/**
|
2010-11-22 06:42:00 +08:00
|
|
|
* Clear web history in this web view.
|
|
|
|
*/
|
|
|
|
public void clearHistory() {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.clearHistory = true;
|
|
|
|
if (this.appView != null) {
|
|
|
|
this.appView.clearHistory();
|
|
|
|
}
|
2010-11-22 06:42:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-12 01:34:12 +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) {
|
2011-08-23 01:38:00 +08:00
|
|
|
//don't reload the current page when the orientation is changed
|
|
|
|
super.onConfigurationChanged(newConfig);
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get boolean property for activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param defaultValue
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public boolean getBooleanProperty(String name, boolean defaultValue) {
|
2011-08-23 01:38:00 +08:00
|
|
|
Bundle bundle = this.getIntent().getExtras();
|
|
|
|
if (bundle == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
Boolean p = (Boolean)bundle.get(name);
|
|
|
|
if (p == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return p.booleanValue();
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get int property for activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param defaultValue
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public int getIntegerProperty(String name, int defaultValue) {
|
2011-08-23 01:38:00 +08:00
|
|
|
Bundle bundle = this.getIntent().getExtras();
|
|
|
|
if (bundle == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
Integer p = (Integer)bundle.get(name);
|
|
|
|
if (p == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return p.intValue();
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get string property for activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param defaultValue
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public String getStringProperty(String name, String defaultValue) {
|
2011-08-23 01:38:00 +08:00
|
|
|
Bundle bundle = this.getIntent().getExtras();
|
|
|
|
if (bundle == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
String p = bundle.getString(name);
|
|
|
|
if (p == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return p;
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get double property for activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param defaultValue
|
|
|
|
* @return
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public double getDoubleProperty(String name, double defaultValue) {
|
2011-08-23 01:38:00 +08:00
|
|
|
Bundle bundle = this.getIntent().getExtras();
|
|
|
|
if (bundle == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
Double p = (Double)bundle.get(name);
|
|
|
|
if (p == null) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
return p.doubleValue();
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set boolean property on activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public void setBooleanProperty(String name, boolean value) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.getIntent().putExtra(name, value);
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set int property on activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public void setIntegerProperty(String name, int value) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.getIntent().putExtra(name, value);
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set string property on activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public void setStringProperty(String name, String value) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.getIntent().putExtra(name, value);
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
2010-11-22 06:42:00 +08:00
|
|
|
|
2010-11-12 01:34:12 +08:00
|
|
|
/**
|
|
|
|
* Set double property on activity.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @param value
|
|
|
|
*/
|
2010-11-22 06:42:00 +08:00
|
|
|
public void setDoubleProperty(String name, double value) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.getIntent().putExtra(name, value);
|
2010-11-12 01:34:12 +08:00
|
|
|
}
|
2011-07-01 04:42:40 +08:00
|
|
|
|
2010-08-12 23:51:12 +08:00
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when the system is about to start resuming a previous activity.
|
|
|
|
*/
|
2010-09-08 02:59:54 +08:00
|
|
|
protected void onPause() {
|
2010-08-12 23:51:12 +08:00
|
|
|
super.onPause();
|
2011-04-06 04:42:25 +08:00
|
|
|
if (this.appView == null) {
|
2011-08-23 01:38:00 +08:00
|
|
|
return;
|
2011-04-06 04:42:25 +08:00
|
|
|
}
|
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Send pause event to JavaScript
|
|
|
|
this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
|
2011-03-08 08:48:23 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Forward to plugins
|
|
|
|
this.pluginManager.onPause(this.keepRunning);
|
2011-06-25 05:08:26 +08:00
|
|
|
|
2010-11-15 07:33:06 +08:00
|
|
|
// If app doesn't want to run in background
|
|
|
|
if (!this.keepRunning) {
|
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Pause JavaScript timers (including setInterval)
|
|
|
|
this.appView.pauseTimers();
|
2010-11-15 07:33:06 +08:00
|
|
|
}
|
2010-08-12 23:51:12 +08:00
|
|
|
}
|
2011-06-22 23:05:33 +08:00
|
|
|
|
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when the activity receives a new intent
|
|
|
|
**/
|
|
|
|
protected void onNewIntent(Intent intent) {
|
2011-08-23 01:38:00 +08:00
|
|
|
super.onNewIntent(intent);
|
2011-06-22 23:05:33 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
//Forward to plugins
|
|
|
|
this.pluginManager.onNewIntent(intent);
|
2011-06-22 23:05:33 +08:00
|
|
|
}
|
|
|
|
|
2010-08-12 23:51:12 +08:00
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Called when the activity will start interacting with the user.
|
|
|
|
*/
|
2010-09-08 02:59:54 +08:00
|
|
|
protected void onResume() {
|
2010-08-12 23:51:12 +08:00
|
|
|
super.onResume();
|
2011-04-06 04:42:25 +08:00
|
|
|
if (this.appView == null) {
|
2011-08-23 01:38:00 +08:00
|
|
|
return;
|
2011-04-06 04:42:25 +08:00
|
|
|
}
|
2010-08-20 04:41:06 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Send resume event to JavaScript
|
|
|
|
this.appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
|
2011-03-08 08:50:10 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Forward to plugins
|
|
|
|
this.pluginManager.onResume(this.keepRunning || this.activityResultKeepRunning);
|
2011-06-25 05:08:26 +08:00
|
|
|
|
2010-11-15 07:33:06 +08:00
|
|
|
// If app doesn't want to run in background
|
2010-12-10 04:13:23 +08:00
|
|
|
if (!this.keepRunning || this.activityResultKeepRunning) {
|
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Restore multitasking state
|
|
|
|
if (this.activityResultKeepRunning) {
|
|
|
|
this.keepRunning = this.activityResultKeepRunning;
|
|
|
|
this.activityResultKeepRunning = false;
|
|
|
|
}
|
2010-11-15 07:33:06 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Resume JavaScript timers (including setInterval)
|
|
|
|
this.appView.resumeTimers();
|
2010-11-15 07:33:06 +08:00
|
|
|
}
|
2010-08-12 23:51:12 +08:00
|
|
|
}
|
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() {
|
2011-08-23 01:38:00 +08:00
|
|
|
super.onDestroy();
|
|
|
|
|
2011-04-06 04:42:25 +08:00
|
|
|
if (this.appView != null) {
|
2011-08-23 01:38:00 +08:00
|
|
|
|
|
|
|
// Make sure pause event is sent if onPause hasn't been called before onDestroy
|
|
|
|
this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
|
2011-03-19 06:27:36 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Send destroy event to JavaScript
|
|
|
|
this.appView.loadUrl("javascript:try{PhoneGap.onDestroy.fire();}catch(e){};");
|
2011-03-19 06:27:36 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Load blank page so that JavaScript onunload is called
|
|
|
|
this.appView.loadUrl("about:blank");
|
|
|
|
|
|
|
|
// Forward to plugins
|
|
|
|
this.pluginManager.onDestroy();
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2011-04-06 04:42:25 +08:00
|
|
|
}
|
2010-08-12 23:51:12 +08:00
|
|
|
}
|
|
|
|
|
2010-09-11 00:31:22 +08:00
|
|
|
/**
|
|
|
|
* Add a class that implements a service.
|
|
|
|
*
|
|
|
|
* @param serviceType
|
|
|
|
* @param className
|
|
|
|
*/
|
|
|
|
public void addService(String serviceType, String className) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.pluginManager.addService(serviceType, className);
|
2010-09-11 00:31:22 +08:00
|
|
|
}
|
2010-08-19 02:12:53 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send JavaScript statement back to JavaScript.
|
2010-11-12 01:34:12 +08:00
|
|
|
* (This is a convenience method)
|
2010-08-19 02:12:53 +08:00
|
|
|
*
|
|
|
|
* @param message
|
|
|
|
*/
|
|
|
|
public void sendJavascript(String statement) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.callbackServer.sendJavascript(statement);
|
2010-08-19 02:12:53 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 07:23:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a new browser with the specified URL.
|
|
|
|
*
|
|
|
|
* NOTE: If usePhoneGap is set, only trusted PhoneGap URLs should be loaded,
|
|
|
|
* since any PhoneGap API can be called by the loaded HTML page.
|
|
|
|
*
|
|
|
|
* @param url The url to load.
|
|
|
|
* @param usePhoneGap Load url in PhoneGap webview.
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param clearPrev Clear the activity stack, so new app becomes top of stack
|
|
|
|
* @param params DroidGap parameters for new app
|
2011-06-30 07:25:49 +08:00
|
|
|
* @throws android.content.ActivityNotFoundException
|
2011-06-30 07:23:20 +08:00
|
|
|
*/
|
2011-06-30 07:25:49 +08:00
|
|
|
public void showWebPage(String url, boolean usePhoneGap, boolean clearPrev, HashMap<String, Object> params) throws android.content.ActivityNotFoundException {
|
2011-08-23 01:38:00 +08:00
|
|
|
Intent intent = null;
|
|
|
|
if (usePhoneGap) {
|
|
|
|
try {
|
|
|
|
intent = new Intent().setClass(this, Class.forName(this.getComponentName().getClassName()));
|
|
|
|
intent.putExtra("url", url);
|
|
|
|
|
|
|
|
// Add parameters
|
|
|
|
if (params != null) {
|
|
|
|
java.util.Set<Entry<String,Object>> s = params.entrySet();
|
|
|
|
java.util.Iterator<Entry<String,Object>> it = s.iterator();
|
|
|
|
while(it.hasNext()) {
|
|
|
|
Entry<String,Object> entry = it.next();
|
|
|
|
String key = entry.getKey();
|
|
|
|
Object value = entry.getValue();
|
|
|
|
if (value == null) {
|
|
|
|
}
|
|
|
|
else if (value.getClass().equals(String.class)) {
|
|
|
|
intent.putExtra(key, (String)value);
|
|
|
|
}
|
|
|
|
else if (value.getClass().equals(Boolean.class)) {
|
|
|
|
intent.putExtra(key, (Boolean)value);
|
|
|
|
}
|
|
|
|
else if (value.getClass().equals(Integer.class)) {
|
|
|
|
intent.putExtra(key, (Integer)value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
intent.setData(Uri.parse(url));
|
|
|
|
}
|
|
|
|
this.startActivity(intent);
|
|
|
|
|
|
|
|
// Finish current activity
|
|
|
|
if (clearPrev) {
|
|
|
|
this.finish();
|
|
|
|
}
|
2011-06-30 07:23:20 +08:00
|
|
|
}
|
2011-07-09 12:07:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the spinner. Must be called from the UI thread.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param title Title of the dialog
|
|
|
|
* @param message The message of the dialog
|
2011-07-09 12:07:22 +08:00
|
|
|
*/
|
|
|
|
public void spinnerStart(final String title, final String message) {
|
2011-08-23 01:38:00 +08:00
|
|
|
if (this.spinnerDialog != null) {
|
|
|
|
this.spinnerDialog.dismiss();
|
|
|
|
this.spinnerDialog = null;
|
|
|
|
}
|
|
|
|
final DroidGap me = this;
|
|
|
|
this.spinnerDialog = ProgressDialog.show(DroidGap.this, title , message, true, true,
|
|
|
|
new DialogInterface.OnCancelListener() {
|
|
|
|
public void onCancel(DialogInterface dialog) {
|
|
|
|
me.spinnerDialog = null;
|
|
|
|
}
|
|
|
|
});
|
2011-07-09 12:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop spinner.
|
|
|
|
*/
|
|
|
|
public void spinnerStop() {
|
2011-08-23 01:38:00 +08:00
|
|
|
if (this.spinnerDialog != null) {
|
|
|
|
this.spinnerDialog.dismiss();
|
|
|
|
this.spinnerDialog = null;
|
|
|
|
}
|
2011-07-09 12:07:22 +08:00
|
|
|
}
|
2011-06-22 11:50:53 +08:00
|
|
|
|
2010-09-07 23:33:08 +08:00
|
|
|
/**
|
|
|
|
* Provides a hook for calling "alert" from javascript. Useful for
|
|
|
|
* debugging your javascript.
|
|
|
|
*/
|
|
|
|
public class GapClient extends WebChromeClient {
|
2010-09-10 00:01:56 +08:00
|
|
|
|
2011-06-22 11:50:53 +08:00
|
|
|
private DroidGap ctx;
|
2010-09-10 00:01:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ctx
|
|
|
|
*/
|
2010-09-07 23:33:08 +08:00
|
|
|
public GapClient(Context ctx) {
|
2011-06-22 11:50:53 +08:00
|
|
|
this.ctx = (DroidGap)ctx;
|
2010-09-07 23:33:08 +08:00
|
|
|
}
|
2010-09-10 00:01:56 +08:00
|
|
|
|
2010-09-07 23:33:08 +08:00
|
|
|
/**
|
|
|
|
* Tell the client to display a javascript alert dialog.
|
|
|
|
*
|
|
|
|
* @param view
|
|
|
|
* @param url
|
|
|
|
* @param message
|
|
|
|
* @param result
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
|
2010-09-10 00:01:56 +08:00
|
|
|
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
|
2010-09-07 23:33:08 +08:00
|
|
|
dlg.setMessage(message);
|
|
|
|
dlg.setTitle("Alert");
|
|
|
|
dlg.setCancelable(false);
|
|
|
|
dlg.setPositiveButton(android.R.string.ok,
|
2011-08-23 01:38:00 +08:00
|
|
|
new AlertDialog.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
result.confirm();
|
|
|
|
}
|
|
|
|
});
|
2010-09-07 23:33:08 +08:00
|
|
|
dlg.create();
|
|
|
|
dlg.show();
|
|
|
|
return true;
|
|
|
|
}
|
2009-12-17 03:09:32 +08:00
|
|
|
|
2010-09-07 23:33:08 +08:00
|
|
|
/**
|
|
|
|
* Tell the client to display a confirm dialog to the user.
|
|
|
|
*
|
|
|
|
* @param view
|
|
|
|
* @param url
|
|
|
|
* @param message
|
|
|
|
* @param result
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
|
2010-09-10 00:01:56 +08:00
|
|
|
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
|
2010-09-07 23:33:08 +08:00
|
|
|
dlg.setMessage(message);
|
|
|
|
dlg.setTitle("Confirm");
|
|
|
|
dlg.setCancelable(false);
|
|
|
|
dlg.setPositiveButton(android.R.string.ok,
|
2011-08-23 01:38:00 +08:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
result.confirm();
|
2010-09-07 23:33:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.setNegativeButton(android.R.string.cancel,
|
2011-08-23 01:38:00 +08:00
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
result.cancel();
|
2010-09-07 23:33:08 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.create();
|
|
|
|
dlg.show();
|
|
|
|
return true;
|
|
|
|
}
|
2009-12-01 07:41:24 +08:00
|
|
|
|
2011-02-28 10:07:24 +08:00
|
|
|
/**
|
|
|
|
* Tell the client to display a prompt dialog to the user.
|
|
|
|
* If the client returns true, WebView will assume that the client will
|
|
|
|
* handle the prompt dialog and call the appropriate JsPromptResult method.
|
|
|
|
*
|
|
|
|
* @param view
|
|
|
|
* @param url
|
|
|
|
* @param message
|
|
|
|
* @param defaultValue
|
|
|
|
* @param result
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
|
2011-08-23 01:38:00 +08:00
|
|
|
|
|
|
|
// Security check to make sure any requests are coming from the page initially
|
|
|
|
// loaded in webview and not another loaded in an iframe.
|
|
|
|
boolean reqOk = false;
|
|
|
|
if (url.indexOf(this.ctx.baseUrl) == 0) {
|
|
|
|
reqOk = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling PluginManager.exec() to call a native service using
|
|
|
|
// prompt(this.stringify(args), "gap:"+this.stringify([service, action, callbackId, true]));
|
|
|
|
if (reqOk && defaultValue != null && defaultValue.length() > 3 && defaultValue.substring(0, 4).equals("gap:")) {
|
|
|
|
JSONArray array;
|
|
|
|
try {
|
|
|
|
array = new JSONArray(defaultValue.substring(4));
|
|
|
|
String service = array.getString(0);
|
|
|
|
String action = array.getString(1);
|
|
|
|
String callbackId = array.getString(2);
|
|
|
|
boolean async = array.getBoolean(3);
|
|
|
|
String r = pluginManager.exec(service, action, callbackId, message, async);
|
|
|
|
result.confirm(r);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polling for JavaScript messages
|
|
|
|
else if (reqOk && defaultValue.equals("gap_poll:")) {
|
|
|
|
String r = callbackServer.getJavascript();
|
|
|
|
result.confirm(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calling into CallbackServer
|
|
|
|
else if (reqOk && defaultValue.equals("gap_callbackServer:")) {
|
|
|
|
String r = "";
|
|
|
|
if (message.equals("usePolling")) {
|
|
|
|
r = ""+callbackServer.usePolling();
|
|
|
|
}
|
|
|
|
else if (message.equals("restartServer")) {
|
|
|
|
callbackServer.restartServer();
|
|
|
|
}
|
|
|
|
else if (message.equals("getPort")) {
|
|
|
|
r = Integer.toString(callbackServer.getPort());
|
|
|
|
}
|
|
|
|
else if (message.equals("getToken")) {
|
|
|
|
r = callbackServer.getToken();
|
|
|
|
}
|
|
|
|
result.confirm(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhoneGap JS has initialized, so show webview
|
|
|
|
// (This solves white flash seen when rendering HTML)
|
|
|
|
else if (reqOk && defaultValue.equals("gap_init:")) {
|
|
|
|
appView.setVisibility(View.VISIBLE);
|
|
|
|
ctx.spinnerStop();
|
|
|
|
result.confirm("OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show dialog
|
|
|
|
else {
|
|
|
|
final JsPromptResult res = result;
|
|
|
|
AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
|
|
|
|
dlg.setMessage(message);
|
|
|
|
final EditText input = new EditText(this.ctx);
|
|
|
|
if (defaultValue != null) {
|
|
|
|
input.setText(defaultValue);
|
|
|
|
}
|
|
|
|
dlg.setView(input);
|
|
|
|
dlg.setCancelable(false);
|
|
|
|
dlg.setPositiveButton(android.R.string.ok,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
String usertext = input.getText().toString();
|
|
|
|
res.confirm(usertext);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.setNegativeButton(android.R.string.cancel,
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
res.cancel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.create();
|
|
|
|
dlg.show();
|
|
|
|
}
|
|
|
|
return true;
|
2011-02-28 10:07:24 +08:00
|
|
|
}
|
2011-05-11 00:44:09 +08:00
|
|
|
|
2010-09-07 23:33:08 +08:00
|
|
|
}
|
2010-09-10 00:01:56 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WebChromeClient that extends GapClient with additional support for Android 2.X
|
|
|
|
*/
|
2011-05-11 00:44:09 +08:00
|
|
|
public class EclairClient extends GapClient {
|
2010-09-10 00:01:56 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
private String TAG = "PhoneGapLog";
|
|
|
|
private long MAX_QUOTA = 100 * 1024 * 1024;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ctx
|
|
|
|
*/
|
|
|
|
public EclairClient(Context ctx) {
|
|
|
|
super(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle database quota exceeded notification.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* @param databaseIdentifier
|
|
|
|
* @param currentQuota
|
|
|
|
* @param estimatedSize
|
|
|
|
* @param totalUsedQuota
|
|
|
|
* @param quotaUpdater
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
|
|
|
|
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater)
|
|
|
|
{
|
|
|
|
Log.d(TAG, "event raised onExceededDatabaseQuota estimatedSize: " + Long.toString(estimatedSize) + " currentQuota: " + Long.toString(currentQuota) + " totalUsedQuota: " + Long.toString(totalUsedQuota));
|
|
|
|
|
|
|
|
if( estimatedSize < MAX_QUOTA)
|
|
|
|
{
|
|
|
|
//increase for 1Mb
|
|
|
|
long newQuota = estimatedSize;
|
|
|
|
Log.d(TAG, "calling quotaUpdater.updateQuota newQuota: " + Long.toString(newQuota) );
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log in api level 7: http://developer.android.com/guide/developing/debug-tasks.html
|
|
|
|
@Override
|
|
|
|
public void onConsoleMessage(String message, int lineNumber, String sourceID)
|
|
|
|
{
|
|
|
|
// This is a kludgy hack!!!!
|
|
|
|
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Instructs the client to show a prompt to ask the user to set the Geolocation permission state for the specified origin.
|
|
|
|
*
|
|
|
|
* @param origin
|
|
|
|
* @param callback
|
|
|
|
*/
|
|
|
|
public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
super.onGeolocationPermissionsShowPrompt(origin, callback);
|
|
|
|
callback.invoke(origin, true, false);
|
|
|
|
}
|
2010-11-12 04:00:56 +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
|
|
|
|
2010-09-10 00:01:56 +08:00
|
|
|
DroidGap ctx;
|
2010-08-25 02:19:22 +08:00
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param ctx
|
|
|
|
*/
|
|
|
|
public GapViewClient(DroidGap ctx) {
|
2010-09-10 00:01:56 +08:00
|
|
|
this.ctx = ctx;
|
2010-08-25 02:19:22 +08:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param view The WebView that is initiating the callback.
|
|
|
|
* @param url The url to be loaded.
|
|
|
|
* @return true to override, false for default behavior
|
2010-08-26 23:39:02 +08:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
2011-08-23 01:38:00 +08:00
|
|
|
// 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("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?body=This is the message
|
|
|
|
else if (url.startsWith("sms:")) {
|
|
|
|
try {
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
|
|
|
|
// Get address
|
|
|
|
String address = null;
|
|
|
|
int parmIndex = url.indexOf('?');
|
|
|
|
if (parmIndex == -1) {
|
|
|
|
address = url.substring(4);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
address = url.substring(4, parmIndex);
|
|
|
|
|
|
|
|
// If body, then set sms body
|
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
String query = uri.getQuery();
|
|
|
|
if (query != null) {
|
|
|
|
if (query.startsWith("body=")) {
|
|
|
|
intent.putExtra("sms_body", query.substring(5));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
intent.setData(Uri.parse("sms:"+address));
|
|
|
|
intent.putExtra("address", address);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All else
|
|
|
|
else {
|
|
|
|
|
|
|
|
// If our app or file:, then load into our webview
|
|
|
|
// NOTE: This replaces our app with new URL. When BACK is pressed,
|
|
|
|
// our app is reloaded and restarted. All state is lost.
|
|
|
|
if (this.ctx.loadInWebView || url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0) {
|
|
|
|
try {
|
|
|
|
// Init parameters to new DroidGap activity and propagate existing parameters
|
|
|
|
HashMap<String, Object> params = new HashMap<String, Object>();
|
|
|
|
String loadingPage = this.ctx.getStringProperty("loadingPageDialog", null);
|
|
|
|
if (loadingPage != null) {
|
|
|
|
params.put("loadingDialog", loadingPage);
|
|
|
|
params.put("loadingPageDialog", loadingPage);
|
|
|
|
}
|
|
|
|
if (this.ctx.loadInWebView) {
|
|
|
|
params.put("loadInWebView", true);
|
|
|
|
}
|
|
|
|
params.put("keepRunning", this.ctx.keepRunning);
|
|
|
|
params.put("loadUrlTimeoutValue", this.ctx.loadUrlTimeoutValue);
|
|
|
|
String errorUrl = this.ctx.getStringProperty("errorUrl", null);
|
|
|
|
if (errorUrl != null) {
|
|
|
|
params.put("errorUrl", errorUrl);
|
|
|
|
}
|
|
|
|
params.put("backgroundColor", this.ctx.backgroundColor);
|
|
|
|
|
|
|
|
this.ctx.showWebPage(url, true, false, params);
|
|
|
|
} catch (android.content.ActivityNotFoundException e) {
|
|
|
|
System.out.println("Error loading url into DroidGap - "+url+":"+ e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2010-08-26 23:39:02 +08:00
|
|
|
}
|
2011-08-23 01:38:00 +08:00
|
|
|
|
2010-08-26 23:39:02 +08:00
|
|
|
/**
|
|
|
|
* Notify the host application that a page has finished loading.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param view The webview initiating the callback.
|
|
|
|
* @param url The url of the page.
|
2010-08-26 23:39:02 +08:00
|
|
|
*/
|
|
|
|
@Override
|
2010-11-13 12:38:27 +08:00
|
|
|
public void onPageFinished(WebView view, String url) {
|
2011-08-23 01:38:00 +08:00
|
|
|
super.onPageFinished(view, url);
|
|
|
|
|
|
|
|
// Clear timeout flag
|
|
|
|
this.ctx.loadUrlTimeout++;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (!url.equals("about:blank")) {
|
|
|
|
appView.loadUrl("javascript:try{ PhoneGap.onNativeReady.fire();}catch(e){_nativeReady = true;}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make app visible after 2 sec in case there was a JS error and PhoneGap JS never initialized correctly
|
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
Thread.sleep(2000);
|
|
|
|
ctx.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
appView.setVisibility(View.VISIBLE);
|
|
|
|
ctx.spinnerStop();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
|
|
|
|
// Clear history, so that previous screen isn't there when Back button is pressed
|
|
|
|
if (this.ctx.clearHistory) {
|
|
|
|
this.ctx.clearHistory = false;
|
|
|
|
this.ctx.appView.clearHistory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown if blank loaded
|
|
|
|
if (url.equals("about:blank")) {
|
|
|
|
if (this.ctx.callbackServer != null) {
|
|
|
|
this.ctx.callbackServer.destroy();
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 02:19:22 +08:00
|
|
|
}
|
2010-11-13 12:38:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable).
|
|
|
|
* The errorCode parameter corresponds to one of the ERROR_* constants.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param view The WebView that is initiating the callback.
|
|
|
|
* @param errorCode The error code corresponding to an ERROR_* value.
|
|
|
|
* @param description A String describing the error.
|
|
|
|
* @param failingUrl The url that failed to load.
|
2010-11-13 12:38:27 +08:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
2011-08-23 01:38:00 +08:00
|
|
|
System.out.println("onReceivedError: Error code="+errorCode+" Description="+description+" URL="+failingUrl);
|
2010-08-25 02:19:22 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Clear timeout flag
|
|
|
|
this.ctx.loadUrlTimeout++;
|
2010-11-22 06:42:00 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Stop "app loading" spinner if showing
|
|
|
|
this.ctx.spinnerStop();
|
2010-11-13 12:38:27 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// Handle error
|
|
|
|
this.ctx.onReceivedError(errorCode, description, failingUrl);
|
2010-11-13 12:38:27 +08:00
|
|
|
}
|
2011-06-22 01:08:42 +08:00
|
|
|
|
|
|
|
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
|
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
final String packageName = this.ctx.getPackageName();
|
|
|
|
final PackageManager pm = this.ctx.getPackageManager();
|
|
|
|
ApplicationInfo appInfo;
|
|
|
|
try {
|
|
|
|
appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
|
|
|
|
if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
|
|
|
|
// debug = true
|
|
|
|
handler.proceed();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// debug = false
|
|
|
|
super.onReceivedSslError(view, handler, error);
|
|
|
|
}
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
// When it doubt, lock it out!
|
|
|
|
super.onReceivedSslError(view, handler, error);
|
|
|
|
}
|
2011-06-22 01:08:42 +08:00
|
|
|
}
|
2010-11-13 12:38:27 +08:00
|
|
|
}
|
2010-11-12 04:08:55 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a key is pressed.
|
|
|
|
*
|
|
|
|
* @param keyCode
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
2011-04-06 04:42:25 +08:00
|
|
|
if (this.appView == null) {
|
2011-08-23 01:38:00 +08:00
|
|
|
return super.onKeyDown(keyCode, event);
|
2011-04-06 04:42:25 +08:00
|
|
|
}
|
2010-11-12 04:08:55 +08:00
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
// If back key
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
|
|
|
|
// If back key is bound, then send event to JavaScript
|
|
|
|
if (this.bound) {
|
|
|
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('backbutton');");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not bound
|
|
|
|
else {
|
|
|
|
|
|
|
|
// Go to previous page in webview if it is possible to go back
|
|
|
|
if (this.appView.canGoBack()) {
|
|
|
|
this.appView.goBack();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, then invoke behavior of super class
|
|
|
|
else {
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If menu key
|
|
|
|
else if (keyCode == KeyEvent.KEYCODE_MENU) {
|
|
|
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('menubutton');");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If search key
|
|
|
|
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
|
|
|
|
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('searchbutton');");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-12-17 04:50:08 +08:00
|
|
|
}
|
2010-11-12 04:08:55 +08:00
|
|
|
|
2010-09-04 03:01:24 +08:00
|
|
|
/**
|
2010-09-07 02:13:09 +08:00
|
|
|
* Any calls to Activity.startActivityForResult must use method below, so
|
2010-09-04 03:01:24 +08:00
|
|
|
* the result can be routed to them correctly.
|
|
|
|
*
|
|
|
|
* This is done to eliminate the need to modify DroidGap.java to receive activity results.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param intent The intent to start
|
|
|
|
* @param requestCode Identifies who to send the result to
|
2010-09-04 03:01:24 +08:00
|
|
|
*
|
|
|
|
* @throws RuntimeException
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void startActivityForResult(Intent intent, int requestCode) throws RuntimeException {
|
2011-08-23 01:38:00 +08:00
|
|
|
System.out.println("startActivityForResult(intent,"+requestCode+")");
|
|
|
|
super.startActivityForResult(intent, requestCode);
|
2010-09-04 03:01:24 +08:00
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2010-09-04 03:01:24 +08:00
|
|
|
/**
|
2010-09-07 02:13:09 +08:00
|
|
|
* Launch an activity for which you would like a result when it finished. When this activity exits,
|
|
|
|
* your onActivityResult() method will be called.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param command The command object
|
|
|
|
* @param intent The intent to start
|
|
|
|
* @param requestCode The request code that is passed to callback to identify the activity
|
2010-09-04 03:01:24 +08:00
|
|
|
*/
|
2011-07-08 03:11:03 +08:00
|
|
|
public void startActivityForResult(IPlugin command, Intent intent, int requestCode) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.activityResultCallback = command;
|
|
|
|
this.activityResultKeepRunning = this.keepRunning;
|
|
|
|
|
|
|
|
// If multitasking turned on, then disable it for activities that return results
|
|
|
|
if (command != null) {
|
|
|
|
this.keepRunning = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start activity
|
|
|
|
super.startActivityForResult(intent, requestCode);
|
2010-09-04 03:01:24 +08:00
|
|
|
}
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
@Override
|
2010-09-03 00:27:48 +08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @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").
|
2010-09-03 00:27:48 +08:00
|
|
|
*/
|
2010-11-13 12:38:27 +08:00
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
2011-08-23 01:38:00 +08:00
|
|
|
super.onActivityResult(requestCode, resultCode, intent);
|
|
|
|
IPlugin callback = this.activityResultCallback;
|
|
|
|
if (callback != null) {
|
|
|
|
callback.onActivityResult(requestCode, resultCode, intent);
|
|
|
|
}
|
2010-11-13 12:38:27 +08:00
|
|
|
}
|
2011-07-01 04:42:40 +08:00
|
|
|
|
|
|
|
@Override
|
2011-07-08 03:11:03 +08:00
|
|
|
public void setActivityResultCallback(IPlugin plugin) {
|
2011-08-23 01:38:00 +08:00
|
|
|
this.activityResultCallback = plugin;
|
2011-07-01 04:42:40 +08:00
|
|
|
}
|
|
|
|
|
2010-11-13 12:38:27 +08:00
|
|
|
/**
|
|
|
|
* Report an error to the host application. These errors are unrecoverable (i.e. the main resource is unavailable).
|
|
|
|
* The errorCode parameter corresponds to one of the ERROR_* constants.
|
|
|
|
*
|
2011-08-23 01:38:00 +08:00
|
|
|
* @param errorCode The error code corresponding to an ERROR_* value.
|
|
|
|
* @param description A String describing the error.
|
|
|
|
* @param failingUrl The url that failed to load.
|
2010-11-13 12:38:27 +08:00
|
|
|
*/
|
|
|
|
public void onReceivedError(int errorCode, String description, String failingUrl) {
|
2011-08-23 01:38:00 +08:00
|
|
|
final DroidGap me = this;
|
|
|
|
|
|
|
|
// If errorUrl specified, then load it
|
|
|
|
final String errorUrl = me.getStringProperty("errorUrl", null);
|
|
|
|
if ((errorUrl != null) && errorUrl.startsWith("file://") && (!failingUrl.equals(errorUrl))) {
|
|
|
|
|
|
|
|
// Load URL on UI thread
|
|
|
|
me.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
me.appView.loadUrl(errorUrl);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not, then display error dialog
|
|
|
|
else {
|
|
|
|
me.appView.loadUrl("about:blank");
|
|
|
|
me.displayError("Application Error", description + " ("+failingUrl+")", "OK", true);
|
|
|
|
}
|
2010-11-13 12:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display an error dialog and optionally exit application.
|
|
|
|
*
|
|
|
|
* @param title
|
|
|
|
* @param message
|
|
|
|
* @param button
|
|
|
|
* @param exit
|
|
|
|
*/
|
|
|
|
public void displayError(final String title, final String message, final String button, final boolean exit) {
|
2011-08-23 01:38:00 +08:00
|
|
|
final DroidGap me = this;
|
|
|
|
me.runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
AlertDialog.Builder dlg = new AlertDialog.Builder(me);
|
|
|
|
dlg.setMessage(message);
|
|
|
|
dlg.setTitle(title);
|
|
|
|
dlg.setCancelable(false);
|
|
|
|
dlg.setPositiveButton(button,
|
|
|
|
new AlertDialog.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
dialog.dismiss();
|
|
|
|
if (exit) {
|
|
|
|
me.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
dlg.create();
|
|
|
|
dlg.show();
|
|
|
|
}
|
|
|
|
});
|
2010-11-13 12:38:27 +08:00
|
|
|
}
|
2011-05-31 03:12:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* We are providing this class to detect when the soft keyboard is shown
|
|
|
|
* and hidden in the web view.
|
|
|
|
*/
|
|
|
|
class LinearLayoutSoftKeyboardDetect extends LinearLayout {
|
|
|
|
|
2011-08-23 01:38:00 +08:00
|
|
|
private static final String LOG_TAG = "SoftKeyboardDetect";
|
|
|
|
|
|
|
|
private int oldHeight = 0; // Need to save the old height as not to send redundant events
|
|
|
|
private int oldWidth = 0; // Need to save old width for orientation change
|
|
|
|
private int screenWidth = 0;
|
|
|
|
private int screenHeight = 0;
|
|
|
|
|
|
|
|
public LinearLayoutSoftKeyboardDetect(Context context, int width, int height) {
|
|
|
|
super(context);
|
|
|
|
screenWidth = width;
|
|
|
|
screenHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Start listening to new measurement events. Fire events when the height
|
|
|
|
* gets smaller fire a show keyboard event and when height gets bigger fire
|
|
|
|
* a hide keyboard event.
|
|
|
|
*
|
|
|
|
* Note: We are using callbackServer.sendJavascript() instead of
|
|
|
|
* this.appView.loadUrl() as changing the URL of the app would cause the
|
|
|
|
* soft keyboard to go away.
|
|
|
|
*
|
|
|
|
* @param widthMeasureSpec
|
|
|
|
* @param heightMeasureSpec
|
|
|
|
*/
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
|
|
|
|
|
|
|
Log.d(LOG_TAG, "We are in our onMeasure method");
|
|
|
|
|
|
|
|
// Get the current height of the visible part of the screen.
|
|
|
|
// This height will not included the status bar.
|
|
|
|
int height = MeasureSpec.getSize(heightMeasureSpec);
|
|
|
|
int width = MeasureSpec.getSize(widthMeasureSpec);
|
|
|
|
|
|
|
|
Log.d(LOG_TAG, "Old Height = " + oldHeight);
|
|
|
|
Log.d(LOG_TAG, "Height = " + height);
|
|
|
|
Log.d(LOG_TAG, "Old Width = " + oldWidth);
|
|
|
|
Log.d(LOG_TAG, "Width = " + width);
|
|
|
|
|
|
|
|
|
|
|
|
// If the oldHeight = 0 then this is the first measure event as the app starts up.
|
|
|
|
// If oldHeight == height then we got a measurement change that doesn't affect us.
|
|
|
|
if (oldHeight == 0 || oldHeight == height) {
|
|
|
|
Log.d(LOG_TAG, "Ignore this event");
|
|
|
|
}
|
|
|
|
// Account for orientation change and ignore this event/Fire orientation change
|
|
|
|
else if(screenHeight == width)
|
|
|
|
{
|
|
|
|
int tmp_var = screenHeight;
|
|
|
|
screenHeight = screenWidth;
|
|
|
|
screenWidth = tmp_var;
|
|
|
|
Log.d(LOG_TAG, "Orientation Change");
|
|
|
|
}
|
|
|
|
// If the height as gotten bigger then we will assume the soft keyboard has
|
|
|
|
// gone away.
|
|
|
|
else if (height > oldHeight) {
|
|
|
|
Log.d(LOG_TAG, "Throw hide keyboard event");
|
|
|
|
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('hidekeyboard');");
|
|
|
|
}
|
|
|
|
// If the height as gotten smaller then we will assume the soft keyboard has
|
|
|
|
// been displayed.
|
|
|
|
else if (height < oldHeight) {
|
|
|
|
Log.d(LOG_TAG, "Throw show keyboard event");
|
|
|
|
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the old height for the next event
|
|
|
|
oldHeight = height;
|
|
|
|
oldWidth = width;
|
|
|
|
}
|
2011-05-31 03:12:31 +08:00
|
|
|
}
|
2011-08-23 01:38:00 +08:00
|
|
|
}
|