2009-11-10 09:45:02 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.graphics.Color;
|
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;
|
2009-11-13 02:51:22 +08:00
|
|
|
import android.webkit.WebSettings.LayoutAlgorithm;
|
2009-11-25 08:51:23 +08:00
|
|
|
import android.widget.LinearLayout;
|
2009-11-26 09:25:16 +08:00
|
|
|
import android.os.Build.*;
|
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";
|
2009-11-26 05:27:57 +08:00
|
|
|
protected WebView appView;
|
2009-11-26 09:25:16 +08:00
|
|
|
private LinearLayout root;
|
2009-11-25 08:51:23 +08:00
|
|
|
|
2009-07-14 07:13:55 +08:00
|
|
|
private PhoneGap gap;
|
|
|
|
private GeoBroker geo;
|
|
|
|
private AccelListener accel;
|
2009-07-31 07:56:07 +08:00
|
|
|
private CameraLauncher launcher;
|
2009-11-03 07:43:09 +08:00
|
|
|
private ContactManager mContacts;
|
2009-09-12 05:30:24 +08:00
|
|
|
private FileUtils fs;
|
2009-11-04 03:54:30 +08:00
|
|
|
private NetworkManager netMan;
|
2009-11-19 03:11:05 +08:00
|
|
|
private CompassListener mCompass;
|
2009-12-09 06:08:48 +08:00
|
|
|
private Storage cupcakeStorage;
|
2010-02-25 08:18:35 +08:00
|
|
|
private CryptoHandler crypto;
|
2009-04-02 07:56:43 +08:00
|
|
|
|
|
|
|
/** Called when the activity is first created. */
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
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!
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2009-11-25 08:51:23 +08:00
|
|
|
LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT, 0.0F);
|
|
|
|
|
|
|
|
LinearLayout.LayoutParams webviewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
|
|
|
|
ViewGroup.LayoutParams.FILL_PARENT, 1.0F);
|
2009-04-02 07:56:43 +08:00
|
|
|
|
2009-11-25 08:51:23 +08:00
|
|
|
root = new LinearLayout(this);
|
|
|
|
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
|
root.setBackgroundColor(Color.BLACK);
|
|
|
|
root.setLayoutParams(containerParams);
|
|
|
|
|
|
|
|
appView = new WebView(this);
|
|
|
|
appView.setLayoutParams(webviewParams);
|
2009-11-26 09:25:16 +08:00
|
|
|
|
|
|
|
WebViewReflect.checkCompatibility();
|
|
|
|
|
2009-12-01 07:41:24 +08:00
|
|
|
/* This changes the setWebChromeClient to log alerts to LogCat! Important for Javascript Debugging */
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR)
|
|
|
|
appView.setWebChromeClient(new EclairClient(this));
|
|
|
|
else
|
2009-12-09 06:08:48 +08:00
|
|
|
{
|
2009-12-01 07:41:24 +08:00
|
|
|
appView.setWebChromeClient(new GapClient(this));
|
2009-12-09 06:08:48 +08:00
|
|
|
}
|
2009-12-01 07:41:24 +08:00
|
|
|
|
2009-11-13 07:48:58 +08:00
|
|
|
appView.setInitialScale(100);
|
2009-12-04 02:47:41 +08:00
|
|
|
appView.setVerticalScrollBarEnabled(false);
|
2009-11-25 08:51:23 +08:00
|
|
|
|
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-03 02:06:23 +08:00
|
|
|
|
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/");
|
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
/* Bind the appView object to the gap class methods */
|
|
|
|
bindBrowser(appView);
|
2010-01-05 03:53:26 +08:00
|
|
|
if(cupcakeStorage != null)
|
|
|
|
cupcakeStorage.setStorage(appPackage);
|
|
|
|
|
2009-11-25 08:51:23 +08:00
|
|
|
root.addView(appView);
|
2009-11-26 09:25:16 +08:00
|
|
|
setContentView(root);
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
//don't reload the current page when the orientation is changed
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void bindBrowser(WebView appView)
|
|
|
|
{
|
2009-07-14 07:13:55 +08:00
|
|
|
gap = new PhoneGap(this, appView);
|
|
|
|
geo = new GeoBroker(appView, this);
|
|
|
|
accel = new AccelListener(this, appView);
|
2009-08-01 05:16:02 +08:00
|
|
|
launcher = new CameraLauncher(appView, this);
|
2009-11-04 08:51:40 +08:00
|
|
|
mContacts = new ContactManager(this, appView);
|
2009-10-29 04:43:39 +08:00
|
|
|
fs = new FileUtils(appView);
|
2009-11-04 03:54:30 +08:00
|
|
|
netMan = new NetworkManager(this, appView);
|
2010-02-25 08:18:35 +08:00
|
|
|
mCompass = new CompassListener(this, appView);
|
|
|
|
crypto = new CryptoHandler(appView);
|
2009-09-12 05:30:24 +08:00
|
|
|
|
2009-04-02 07:56:43 +08:00
|
|
|
// This creates the new javascript interfaces for PhoneGap
|
2009-08-01 06:17:36 +08:00
|
|
|
appView.addJavascriptInterface(gap, "DroidGap");
|
2009-04-02 07:56:43 +08:00
|
|
|
appView.addJavascriptInterface(geo, "Geo");
|
|
|
|
appView.addJavascriptInterface(accel, "Accel");
|
2009-07-31 08:12:20 +08:00
|
|
|
appView.addJavascriptInterface(launcher, "GapCam");
|
2009-11-03 07:43:09 +08:00
|
|
|
appView.addJavascriptInterface(mContacts, "ContactHook");
|
2009-09-12 05:30:24 +08:00
|
|
|
appView.addJavascriptInterface(fs, "FileUtil");
|
2009-11-04 03:54:30 +08:00
|
|
|
appView.addJavascriptInterface(netMan, "NetworkManager");
|
2009-11-19 03:11:05 +08:00
|
|
|
appView.addJavascriptInterface(mCompass, "CompassHook");
|
2010-02-25 08:18:35 +08:00
|
|
|
appView.addJavascriptInterface(crypto, "GapCrypto");
|
|
|
|
|
2009-12-09 06:08:48 +08:00
|
|
|
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.DONUT)
|
|
|
|
{
|
|
|
|
cupcakeStorage = new Storage(appView);
|
|
|
|
appView.addJavascriptInterface(cupcakeStorage, "droidStorage");
|
|
|
|
}
|
2009-04-02 07:56:43 +08:00
|
|
|
}
|
2009-11-26 05:27:57 +08:00
|
|
|
|
|
|
|
|
2009-11-25 08:51:23 +08:00
|
|
|
public void loadUrl(String url)
|
|
|
|
{
|
|
|
|
appView.loadUrl(url);
|
|
|
|
}
|
|
|
|
|
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";
|
2009-12-01 07:41:24 +08:00
|
|
|
private long MAX_QUOTA = 2000000;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
if( estimatedSize < MAX_QUOTA)
|
|
|
|
{
|
|
|
|
long newQuota = estimatedSize;
|
|
|
|
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-02-27 03:25:28 +08:00
|
|
|
|
|
|
|
// This is a test of console.log, because we don't have this in Android 2.01
|
|
|
|
public void addMessageToConsole(String message, int lineNumber, String sourceID)
|
|
|
|
{
|
|
|
|
Log.d(TAG, sourceID + ": Line " + Integer.toString(lineNumber) + " : " + message);
|
|
|
|
}
|
|
|
|
|
2009-12-01 07:41:24 +08:00
|
|
|
}
|
|
|
|
|
2009-12-17 04:50:08 +08:00
|
|
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event)
|
|
|
|
{
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
String testUrl = appView.getUrl();
|
|
|
|
appView.goBack();
|
|
|
|
if(appView.getUrl() == testUrl)
|
|
|
|
{
|
|
|
|
return super.onKeyDown(keyCode, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_MENU)
|
|
|
|
{
|
2009-12-18 05:21:13 +08:00
|
|
|
appView.loadUrl("javascript:keyEvent.menuTrigger()");
|
2009-12-17 04:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_SEARCH)
|
|
|
|
{
|
2009-12-18 05:21:13 +08:00
|
|
|
appView.loadUrl("javascript:keyEvent.searchTrigger()");
|
2009-12-17 04:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-18 07:23:18 +08:00
|
|
|
// This is required to start the camera activity! It has to come from the previous activity
|
2009-08-01 04:52:45 +08:00
|
|
|
public void startCamera(int quality)
|
2009-07-18 07:23:18 +08:00
|
|
|
{
|
2009-12-01 07:41:24 +08:00
|
|
|
Intent i = new Intent(this, CameraPreview.class);
|
2009-08-01 04:52:45 +08:00
|
|
|
i.setAction("android.intent.action.PICK");
|
|
|
|
i.putExtra("quality", quality);
|
2009-07-18 07:23:18 +08:00
|
|
|
startActivityForResult(i, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
|
|
|
|
{
|
2009-08-01 04:52:45 +08:00
|
|
|
String data;
|
2009-07-31 07:56:07 +08:00
|
|
|
super.onActivityResult(requestCode, resultCode, intent);
|
2009-08-01 04:52:45 +08:00
|
|
|
if (resultCode == RESULT_OK)
|
|
|
|
{
|
|
|
|
data = intent.getStringExtra("picture");
|
|
|
|
// Send the graphic back to the class that needs it
|
|
|
|
launcher.processPicture(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
launcher.failPicture("Did not complete!");
|
|
|
|
}
|
2009-07-18 07:23:18 +08:00
|
|
|
}
|
2009-11-26 05:27:57 +08:00
|
|
|
|
|
|
|
public WebView getView()
|
|
|
|
{
|
2009-11-26 07:06:47 +08:00
|
|
|
return this.appView;
|
2009-11-26 05:27:57 +08:00
|
|
|
}
|
|
|
|
|
2009-09-12 05:30:24 +08:00
|
|
|
}
|