You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bo...@apache.org on 2012/04/13 22:00:52 UTC

[5/5] android commit: Updated tweaks to get up and running

Updated tweaks to get up and running


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/f2526bbc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/f2526bbc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/f2526bbc

Branch: refs/heads/CordovaWebView
Commit: f2526bbc78394f2b3c350b10a7a3e67de7b2d464
Parents: 1794f2e
Author: Joe Bowser <bo...@apache.org>
Authored: Thu Apr 12 14:18:06 2012 -0700
Committer: Joe Bowser <bo...@apache.org>
Committed: Thu Apr 12 14:18:06 2012 -0700

----------------------------------------------------------------------
 .../org/apache/cordova/CordovaChromeClient.java    |   17 +++--
 .../src/org/apache/cordova/CordovaWebView.java     |   45 ++++++++++++++-
 .../org/apache/cordova/CordovaWebViewClient.java   |   37 ++++++------
 framework/src/org/apache/cordova/DroidGap.java     |   47 +-------------
 .../org/apache/cordova/api/CordovaInterface.java   |   17 +++++-
 .../org/apache/cordova/test/ActivityPlugin.java    |    2 +-
 6 files changed, 94 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/framework/src/org/apache/cordova/CordovaChromeClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java
index c3c7e21..f5cebb9 100755
--- a/framework/src/org/apache/cordova/CordovaChromeClient.java
+++ b/framework/src/org/apache/cordova/CordovaChromeClient.java
@@ -22,6 +22,7 @@ import org.json.JSONArray;
 import org.json.JSONException;
 
 
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
@@ -44,7 +45,7 @@ public class CordovaChromeClient extends WebChromeClient {
 
     private String TAG = "CordovaLog";
     private long MAX_QUOTA = 100 * 1024 * 1024;
-    private DroidGap ctx;
+    private Activity ctx;
     private CordovaWebView appView;
     
     /**
@@ -53,16 +54,18 @@ public class CordovaChromeClient extends WebChromeClient {
      * @param ctx
      */
     public CordovaChromeClient(Context ctx) {
-        this.ctx = (DroidGap) ctx;
-        appView = this.ctx.appView;
+        this.ctx = (Activity) ctx;
+        //appView = this.ctx.appView;
     }
     
     public CordovaChromeClient(Context ctx, CordovaWebView app)
     {
-      this.ctx = (DroidGap) ctx;
+      this.ctx = (Activity) ctx;
       appView = app;
     }
 
+    
+    
     /**
      * Tell the client to display a javascript alert dialog.
      * 
@@ -176,7 +179,7 @@ public class CordovaChromeClient extends WebChromeClient {
         // 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.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) {
+        if (url.startsWith("file://") || url.indexOf(appView.baseUrl) == 0 || appView.isUrlWhiteListed(url)) {
             reqOk = true;
         }
         
@@ -224,8 +227,8 @@ public class CordovaChromeClient extends WebChromeClient {
         // Cordova JS has initialized, so show webview
         // (This solves white flash seen when rendering HTML)
         else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) {
-            ctx.appView.setVisibility(View.VISIBLE);
-            ctx.spinnerStop();
+            appView.setVisibility(View.VISIBLE);
+            //ctx.spinnerStop();
             result.confirm("OK");
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index 4519262..055fed7 100644
--- a/framework/src/org/apache/cordova/CordovaWebView.java
+++ b/framework/src/org/apache/cordova/CordovaWebView.java
@@ -42,7 +42,7 @@ public class CordovaWebView extends WebView {
 
   //This is for the polyfil history 
   private String url;
-  private String baseUrl;
+  String baseUrl;
   private Stack<String> urls = new Stack<String>();
 
   protected int loadUrlTimeout;
@@ -274,9 +274,32 @@ public class CordovaWebView extends WebView {
   public void loadUrl(String url)
   {
     if (!url.startsWith("javascript:")) {
+      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 + "/";
+          }
+      }
+      
+
+      // Create callback server and plugin manager
+      if (callbackServer == null) {
+          callbackServer = new CallbackServer();
+          callbackServer.init(url);
+      }
+      else {
+          callbackServer.reinit(url);
+      }
+      pluginManager.init();
+      
      this.urls.push(url);
     }
     
+    
     super.loadUrl(url);
   }
 
@@ -287,4 +310,24 @@ public class CordovaWebView extends WebView {
   public void postMessage(String id, String data) {
     pluginManager.postMessage(id, data);
   }
+
+  /** 
+   * Returns the top url on the stack without removing it from 
+   * the stack.
+   */
+  public String peekAtUrlStack() {
+      if (urls.size() > 0) {
+          return urls.peek();
+      }
+      return "";
+  }
+  
+  /**
+   * Add a url to the stack
+   * 
+   * @param url
+   */
+  public void pushUrl(String url) {
+      urls.push(url);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/framework/src/org/apache/cordova/CordovaWebViewClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java
index 943003b..20dbcbc 100755
--- a/framework/src/org/apache/cordova/CordovaWebViewClient.java
+++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java
@@ -20,6 +20,7 @@ package org.apache.cordova;
 
 import org.apache.cordova.api.LOG;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
@@ -41,7 +42,7 @@ import android.webkit.WebViewClient;
 public class CordovaWebViewClient extends WebViewClient {
     
     private static final String TAG = "Cordova";
-    DroidGap ctx;
+    Activity ctx;
     CordovaWebView appView;
     private boolean doClearHistory = false;
 
@@ -50,14 +51,13 @@ public class CordovaWebViewClient extends WebViewClient {
      * 
      * @param ctx
      */
-    public CordovaWebViewClient(DroidGap ctx) {
+    public CordovaWebViewClient(Activity ctx) {
         this.ctx = ctx;
-        appView = ctx.appView;
     }
     
     public CordovaWebViewClient(Context ctx, CordovaWebView view)
     {
-      this.ctx = (DroidGap) ctx;
+      this.ctx = (Activity) ctx;
       appView = view;
     }
     
@@ -146,8 +146,8 @@ public class CordovaWebViewClient extends WebViewClient {
 
             // If our app or file:, then load into a new Cordova webview container by starting a new instance of our activity.
             // Our app continues to run.  When BACK is pressed, our app is redisplayed.
-            if (url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) {
-                this.ctx.loadUrl(url);
+            if (url.startsWith("file://") || url.indexOf(appView.baseUrl) == 0 || appView.isUrlWhiteListed(url)) {
+                appView.loadUrl(url);
             }
 
             // If not our application, let default viewer handle
@@ -221,26 +221,26 @@ public class CordovaWebViewClient extends WebViewClient {
         }
 
         // Clear timeout flag
-        this.ctx.loadUrlTimeout++;
+        appView.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")) {
-            ctx.appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
-            this.ctx.postMessage("onNativeReady", null);
+            appView.loadUrl("javascript:try{ cordova.require('cordova/channel').onNativeReady.fire();}catch(e){_nativeReady = true;}");
+            appView.postMessage("onNativeReady", null);
         }
 
         // Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly
-        if (ctx.appView.getVisibility() == View.INVISIBLE) {
+        if (appView.getVisibility() == View.INVISIBLE) {
             Thread t = new Thread(new Runnable() {
                 public void run() {
                     try {
                         Thread.sleep(2000);
                         ctx.runOnUiThread(new Runnable() {
                             public void run() {
-                                ctx.appView.setVisibility(View.VISIBLE);
-                                ctx.spinnerStop();
+                                appView.setVisibility(View.VISIBLE);
+                                //ctx.spinnerStop();
                             }
                         });
                     } catch (InterruptedException e) {
@@ -256,7 +256,8 @@ public class CordovaWebViewClient extends WebViewClient {
             if (appView.callbackServer != null) {
                 appView.callbackServer.destroy();
             }
-            this.ctx.endActivity();
+            //this.ctx.endActivity();
+            this.ctx.finish();
         }
     }
     
@@ -274,13 +275,13 @@ public class CordovaWebViewClient extends WebViewClient {
         LOG.d(TAG, "DroidGap: GapViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl);
 
         // Clear timeout flag
-        this.ctx.loadUrlTimeout++;
+        //this.ctx.loadUrlTimeout++;
 
         // Stop "app loading" spinner if showing
-        this.ctx.spinnerStop();
+        //this.ctx.spinnerStop();
 
         // Handle error
-        this.ctx.onReceivedError(errorCode, description, failingUrl);
+        //this.ctx.onReceivedError(errorCode, description, failingUrl);
     }
     
     public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
@@ -310,8 +311,8 @@ public class CordovaWebViewClient extends WebViewClient {
          * If you do a document.location.href the url does not get pushed on the stack
          * so we do a check here to see if the url should be pushed.
          */
-        if (!this.ctx.peekAtUrlStack().equals(url)) {
-            this.ctx.pushUrl(url);
+        if (!appView.peekAtUrlStack().equals(url)) {
+            appView.pushUrl(url);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/framework/src/org/apache/cordova/DroidGap.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/DroidGap.java b/framework/src/org/apache/cordova/DroidGap.java
index e6474bd..2bb1cdd 100755
--- a/framework/src/org/apache/cordova/DroidGap.java
+++ b/framework/src/org/apache/cordova/DroidGap.java
@@ -346,16 +346,6 @@ public class DroidGap extends Activity implements CordovaInterface {
             LOG.d(TAG, "DroidGap.loadUrl(%s)", 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 + "/";
-            }
-        }
         if (!url.startsWith("javascript:")) {
             LOG.d(TAG, "DroidGap: url=%s baseUrl=%s", url, baseUrl);
         }
@@ -370,24 +360,12 @@ public class DroidGap extends Activity implements CordovaInterface {
                     me.init();
                 }
 
-                // Handle activity parameters
+                // Handle activity parameters (TODO: Somehow abstract this)
                 me.handleActivityParameters();
 
-                // Track URLs loaded instead of using appView history
-                me.urls.push(url);
-                me.appView.clearHistory();
-            
-                // Create callback server and plugin manager
-                if (appView.callbackServer == null) {
-                    appView.callbackServer = new CallbackServer();
-                    appView.callbackServer.init(url);
-                }
-                else {
-                    appView.callbackServer.reinit(url);
-                }
-                appView.pluginManager.init();
-                
                 // If loadingDialog property, then show the App loading dialog for first page of app
+                // (This doesn't seem to actually do anything here)
+                /*
                 String loading = null;
                 if (me.urls.size() == 1) {
                     loading = me.getStringProperty("loadingDialog", null);
@@ -413,6 +391,7 @@ public class DroidGap extends Activity implements CordovaInterface {
                     }
                     me.spinnerStart(title, message);
                 }
+                */
 
                 // Create a timeout timer for loadUrl
                 final int currentLoadUrlTimeout = me.loadUrlTimeout;
@@ -1184,25 +1163,7 @@ public class DroidGap extends Activity implements CordovaInterface {
      * URL stack manipulators
      */
     
-    /** 
-     * Returns the top url on the stack without removing it from 
-     * the stack.
-     */
-    public String peekAtUrlStack() {
-        if (urls.size() > 0) {
-            return urls.peek();
-        }
-        return "";
-    }
     
-    /**
-     * Add a url to the stack
-     * 
-     * @param url
-     */
-    public void pushUrl(String url) {
-        urls.push(url);
-    }
     
     /* 
      * Hook in DroidGap for menu plugins

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/framework/src/org/apache/cordova/api/CordovaInterface.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/CordovaInterface.java b/framework/src/org/apache/cordova/api/CordovaInterface.java
index 5af9fa8..b25e972 100755
--- a/framework/src/org/apache/cordova/api/CordovaInterface.java
+++ b/framework/src/org/apache/cordova/api/CordovaInterface.java
@@ -32,6 +32,8 @@ import android.content.res.Resources;
 import android.database.Cursor;
 import android.hardware.SensorManager;
 import android.net.Uri;
+import android.view.Menu;
+import android.view.MenuItem;
 
 
 /**
@@ -64,13 +66,26 @@ public interface CordovaInterface {
      */
     public abstract void bindBackButton(boolean override);
 
-
     /**
      * A hook required to check if the Back Button is bound
      * @return
      */
     public abstract boolean isBackButtonBound();
 
+    /* 
+     * Hook in DroidGap for menu plugins
+     * (This is in the Android SDK, do we need this on the Interface?)
+     */
+    
+    public abstract boolean onCreateOptionsMenu(Menu menu);
+    
+    public abstract boolean onPrepareOptionsMenu(Menu menu);
+    
+    public abstract boolean onOptionsItemSelected(MenuItem item);
+    
+    
+    
+    
     /**
      * @deprecated
      * Add services to res/xml/plugins.xml instead.

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/f2526bbc/test/src/org/apache/cordova/test/ActivityPlugin.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/ActivityPlugin.java b/test/src/org/apache/cordova/test/ActivityPlugin.java
index 9c26047..019d2ec 100755
--- a/test/src/org/apache/cordova/test/ActivityPlugin.java
+++ b/test/src/org/apache/cordova/test/ActivityPlugin.java
@@ -69,7 +69,7 @@ public class ActivityPlugin extends Plugin {
 
     public void startActivity(String className) {
         try {
-            Intent intent = new Intent().setClass(this.ctx.getContext(), Class.forName(className));
+            Intent intent = new Intent().setClass(this.ctx, Class.forName(className));
             LOG.d(TAG, "Starting activity %s", className);
             this.ctx.startActivity(intent);
         } catch (ClassNotFoundException e) {