Cleanup code and add comments.

This commit is contained in:
Bryce Curtis 2010-09-09 11:01:56 -05:00
parent 77801de1ae
commit 1fa41df3f1

View File

@ -128,16 +128,19 @@ public class DroidGap extends Activity {
*/ */
initWebView(); initWebView();
root.addView(this.appView);
root.addView(appView);
setContentView(root); setContentView(root);
} }
/**
* Create and initialize web container.
*/
private void initWebView() { private void initWebView() {
appView = new WebView(DroidGap.this);
appView.setLayoutParams(new LinearLayout.LayoutParams( // Create web container
this.appView = new WebView(DroidGap.this);
this.appView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT,
1.0F)); 1.0F));
@ -145,40 +148,40 @@ public class DroidGap extends Activity {
WebViewReflect.checkCompatibility(); WebViewReflect.checkCompatibility();
if (android.os.Build.VERSION.RELEASE.startsWith("2.")) { if (android.os.Build.VERSION.RELEASE.startsWith("2.")) {
appView.setWebChromeClient(new EclairClient(DroidGap.this)); this.appView.setWebChromeClient(new EclairClient(DroidGap.this));
} }
else { else {
appView.setWebChromeClient(new GapClient(DroidGap.this)); this.appView.setWebChromeClient(new GapClient(DroidGap.this));
} }
appView.setWebViewClient(new GapViewClient(this)); this.appView.setWebViewClient(new GapViewClient(this));
appView.setInitialScale(100); this.appView.setInitialScale(100);
appView.setVerticalScrollBarEnabled(false); this.appView.setVerticalScrollBarEnabled(false);
appView.requestFocusFromTouch(); this.appView.requestFocusFromTouch();
WebSettings settings = appView.getSettings(); // Enable JavaScript
WebSettings settings = this.appView.getSettings();
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL); settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
// Enable database
Package pack = this.getClass().getPackage(); Package pack = this.getClass().getPackage();
String appPackage = pack.getName(); String appPackage = pack.getName();
WebViewReflect.setStorage(settings, true, "/data/data/" + appPackage + "/app_database/"); WebViewReflect.setStorage(settings, true, "/data/data/" + appPackage + "/app_database/");
// Turn on DOM storage! // Enable DOM storage
WebViewReflect.setDomStorage(settings); WebViewReflect.setDomStorage(settings);
// Turn off native geolocation object in browser - we use our own :)
// Enable built-in geolocation
WebViewReflect.setGeolocationEnabled(settings, true); WebViewReflect.setGeolocationEnabled(settings, true);
// Bind the appView object to the gap class methods
bindBrowser(appView); // Bind PhoneGap objects to JavaScript
if (this.pluginManager.getPlugin("com.phonegap.Storage") != null) { this.bindBrowser(this.appView);
Storage cupcakeStorage = (Storage)this.pluginManager.getPlugin("com.phonegap.Storage");
cupcakeStorage.setStorage(appPackage);
}
} }
@Override @Override
/** /**
* Called by the system when the device configuration changes while your activity is running. * Called by the system when the device configuration changes while your activity is running.
@ -201,10 +204,10 @@ public class DroidGap extends Activity {
this.pluginManager.onPause(); this.pluginManager.onPause();
// Send pause event to JavaScript // Send pause event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
// Pause JavaScript timers (including setInterval) // Pause JavaScript timers (including setInterval)
appView.pauseTimers(); this.appView.pauseTimers();
} }
@Override @Override
@ -218,10 +221,10 @@ public class DroidGap extends Activity {
this.pluginManager.onResume(); this.pluginManager.onResume();
// Send resume event to JavaScript // Send resume event to JavaScript
appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};"); this.appView.loadUrl("javascript:try{PhoneGap.onResume.fire();}catch(e){};");
// Resume JavaScript timers (including setInterval) // Resume JavaScript timers (including setInterval)
appView.resumeTimers(); this.appView.resumeTimers();
} }
@Override @Override
@ -232,44 +235,52 @@ public class DroidGap extends Activity {
super.onDestroy(); super.onDestroy();
// Make sure pause event is sent if onPause hasn't been called before onDestroy // Make sure pause event is sent if onPause hasn't been called before onDestroy
appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};"); this.appView.loadUrl("javascript:try{PhoneGap.onPause.fire();}catch(e){};");
// Load blank page so that JavaScript onunload is called // Load blank page so that JavaScript onunload is called
appView.loadUrl("about:blank"); this.appView.loadUrl("about:blank");
// Clean up objects // Clean up objects
if (mKey != null) { if (this.mKey != null) {
} }
// Forward to plugins // Forward to plugins
this.pluginManager.onDestroy(); this.pluginManager.onDestroy();
if (callbackServer != null) { if (this.callbackServer != null) {
callbackServer.destroy(); this.callbackServer.destroy();
} }
} }
/**
* Bind PhoneGap objects to JavaScript.
*
* @param appView
*/
private void bindBrowser(WebView appView) { private void bindBrowser(WebView appView) {
callbackServer = new CallbackServer(); this.callbackServer = new CallbackServer();
pluginManager = new PluginManager(appView, this); this.pluginManager = new PluginManager(appView, this);
gap = new Device(appView, this); this.gap = new Device(appView, this);
fs = new FileUtils(appView, this); this.fs = new FileUtils(appView, this);
mKey = new BrowserKey(appView, this); this.mKey = new BrowserKey(appView, this);
// This creates the new javascript interfaces for PhoneGap // This creates the new javascript interfaces for PhoneGap
appView.addJavascriptInterface(pluginManager, "PluginManager"); appView.addJavascriptInterface(this.pluginManager, "PluginManager");
appView.addJavascriptInterface(gap, "DroidGap"); appView.addJavascriptInterface(this.gap, "DroidGap");
appView.addJavascriptInterface(fs, "FileUtil"); appView.addJavascriptInterface(this.fs, "FileUtil");
appView.addJavascriptInterface(mKey, "BackButton"); appView.addJavascriptInterface(this.mKey, "BackButton");
appView.addJavascriptInterface(callbackServer, "CallbackServer"); appView.addJavascriptInterface(this.callbackServer, "CallbackServer");
appView.addJavascriptInterface(new SplashScreen(this), "SplashScreen"); appView.addJavascriptInterface(new SplashScreen(this), "SplashScreen");
if (android.os.Build.VERSION.RELEASE.startsWith("1.")) // Add in support for storage and location for Android 1.X devices
{ if (android.os.Build.VERSION.RELEASE.startsWith("1.")) {
Log.d(LOG_TAG, "bindBrowser: Adding droidStorage"); //@ibm Package pack = this.getClass().getPackage();
this.pluginManager.addPlugin("com.phonegap.Storage"); String appPackage = pack.getName();
Storage cupcakeStorage = (Storage)this.pluginManager.addPlugin("com.phonegap.Storage");
cupcakeStorage.setStorage(appPackage);
this.pluginManager.addPlugin("com.phonegap.GeoBroker"); this.pluginManager.addPlugin("com.phonegap.GeoBroker");
} }
@ -321,9 +332,15 @@ public class DroidGap extends Activity {
*/ */
public class GapClient extends WebChromeClient { public class GapClient extends WebChromeClient {
Context mCtx; private Context ctx;
/**
* Constructor.
*
* @param ctx
*/
public GapClient(Context ctx) { public GapClient(Context ctx) {
mCtx = ctx; this.ctx = ctx;
} }
/** /**
@ -337,7 +354,7 @@ public class DroidGap extends Activity {
@Override @Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
Log.d(LOG_TAG, message); Log.d(LOG_TAG, message);
AlertDialog.Builder dlg = new AlertDialog.Builder(mCtx); AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
dlg.setMessage(message); dlg.setMessage(message);
dlg.setTitle("Alert"); dlg.setTitle("Alert");
dlg.setCancelable(false); dlg.setCancelable(false);
@ -362,7 +379,7 @@ public class DroidGap extends Activity {
*/ */
@Override @Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
AlertDialog.Builder dlg = new AlertDialog.Builder(mCtx); AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
dlg.setMessage(message); dlg.setMessage(message);
dlg.setTitle("Confirm"); dlg.setTitle("Confirm");
dlg.setCancelable(false); dlg.setCancelable(false);
@ -385,16 +402,34 @@ public class DroidGap extends Activity {
} }
public final class EclairClient extends GapClient /**
{ * WebChromeClient that extends GapClient with additional support for Android 2.X
*/
public final class EclairClient extends GapClient {
private String TAG = "PhoneGapLog"; private String TAG = "PhoneGapLog";
private long MAX_QUOTA = 100 * 1024 * 1024; private long MAX_QUOTA = 100 * 1024 * 1024;
/**
* Constructor.
*
* @param ctx
*/
public EclairClient(Context ctx) { public EclairClient(Context ctx) {
super(ctx); super(ctx);
// TODO Auto-generated constructor stub
} }
/**
* 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, public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize,
long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater)
{ {
@ -416,6 +451,7 @@ public class DroidGap extends Activity {
} }
// console.log in api level 7: http://developer.android.com/guide/developing/debug-tasks.html // 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) public void onConsoleMessage(String message, int lineNumber, String sourceID)
{ {
// This is a kludgy hack!!!! // This is a kludgy hack!!!!
@ -438,7 +474,7 @@ public class DroidGap extends Activity {
// TODO: hide splash screen here // TODO: hide splash screen here
DroidGap mCtx; DroidGap ctx;
/** /**
* Constructor. * Constructor.
@ -446,7 +482,7 @@ public class DroidGap extends Activity {
* @param ctx * @param ctx
*/ */
public GapViewClient(DroidGap ctx) { public GapViewClient(DroidGap ctx) {
mCtx = ctx; this.ctx = ctx;
} }
/** /**
@ -520,8 +556,8 @@ public class DroidGap extends Activity {
} }
// If our app or file:, then load into our webview // If our app or file:, then load into our webview
if (loadInWebView || url.startsWith("file://") || mCtx.baseUrl.equals(newBaseUrl)) { if (this.ctx.loadInWebView || url.startsWith("file://") || this.ctx.baseUrl.equals(newBaseUrl)) {
appView.loadUrl(url); this.ctx.appView.loadUrl(url);
} }
// If not our application, let default viewer handle // If not our application, let default viewer handle
@ -591,7 +627,7 @@ public class DroidGap extends Activity {
*/ */
public void hideSplashScreen() { public void hideSplashScreen() {
root.removeView(splashScreen); root.removeView(splashScreen);
root.addView(appView); root.addView(this.appView);
} }
/** /**