Skip to content

Commit

Permalink
Experiments with 2.0 and backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Bowser committed Nov 26, 2009
1 parent 556c3a8 commit 21a8e4d
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 26 deletions.
45 changes: 19 additions & 26 deletions android/framework/src/com/phonegap/DroidGap.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
Expand All @@ -38,15 +37,17 @@
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebStorage;
import android.webkit.WebView;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.widget.LinearLayout;
import android.os.Build.*;

public class DroidGap extends Activity {

private static final String LOG_TAG = "DroidGap";
protected WebView appView;
private LinearLayout root;
private LinearLayout root;

private String uri;
private PhoneGap gap;
Expand All @@ -57,6 +58,7 @@ public class DroidGap extends Activity {
private FileUtils fs;
private NetworkManager netMan;
private CompassListener mCompass;
private WebViewReflect eclairCheck;

/** Called when the activity is first created. */
@Override
Expand All @@ -80,23 +82,29 @@ public void onCreate(Bundle savedInstanceState) {

appView = new WebView(this);
appView.setLayoutParams(webviewParams);


WebViewReflect.checkCompatibility();

/* This changes the setWebChromeClient to log alerts to LogCat! Important for Javascript Debugging */
appView.setWebChromeClient(new GapClient(this));
appView.setWebChromeClient(new PhoneGapClient(this));
appView.setInitialScale(100);

WebSettings settings = appView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);


Package pack = this.getClass().getPackage();
String appPackage = pack.getName();

WebViewReflect.setStorage(settings, true, "/data/data/" + appPackage + "/app_database/");

/* Bind the appView object to the gap class methods */
bindBrowser(appView);

root.addView(appView);
setContentView(root);


setContentView(root);
}

@Override
Expand Down Expand Up @@ -137,28 +145,13 @@ public void loadUrl(String url)
* Provides a hook for calling "alert" from javascript. Useful for
* debugging your javascript.
*/
final class GapClient extends WebChromeClient {
final class PhoneGapClient extends GapClient {

Context mCtx;
GapClient(Context ctx)
{
mCtx = ctx;
PhoneGapClient(Context ctx){
super(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);
alertBldr.setMessage(message);
alertBldr.setTitle("Alert");
alertBldr.show();
result.confirm();
return true;
}

}


// This is required to start the camera activity! It has to come from the previous activity
public void startCamera(int quality)
Expand Down
61 changes: 61 additions & 0 deletions android/framework/src/com/phonegap/GapClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.phonegap;

import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;


public class GapClient extends WebChromeClient {

private static final String LOG_TAG = "DroidGap";
private long MAX_QUOTA = 2000000;
private WebChromeClient mInstance;

/* class initialization fails when this throws an exception */
static {
try {

} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

Context mCtx;
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);
alertBldr.setMessage(message);
alertBldr.setTitle("Alert");
alertBldr.show();
result.confirm();
return true;
}

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);
}
}
}
61 changes: 61 additions & 0 deletions android/framework/src/com/phonegap/WebViewReflect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.phonegap;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.webkit.WebSettings;

public class WebViewReflect {
private static Method mWebSettings_setDatabaseEnabled;

static
{
checkCompatibility();
}

private static void setDatabaseEnabled(boolean e) throws IOException {
try
{
mWebSettings_setDatabaseEnabled.invoke(e);
}
catch (InvocationTargetException ite) {
/* unpack original exception when possible */
Throwable cause = ite.getCause();
if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
} else {
/* unexpected checked exception; wrap and re-throw */
throw new RuntimeException(ite);
}
} catch (IllegalAccessException ie) {
System.err.println("unexpected " + ie);
}
}

public static void checkCompatibility() {
try {
mWebSettings_setDatabaseEnabled = WebSettings.class.getMethod(
"setDatabaseEnabled", new Class[] { boolean.class } );
/* success, this is a newer device */
} catch (NoSuchMethodException nsme) {
/* failure, must be older device */
}
}


public static void setStorage(WebSettings setting, boolean enable, String path) {
if (mWebSettings_setDatabaseEnabled != null) {
/* feature is supported */
setting.setDatabaseEnabled(enable);
setting.setDatabasePath(path);
} else {
/* feature not supported, do something else */
System.out.println("dump not supported");
}
}
}

0 comments on commit 21a8e4d

Please sign in to comment.