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 2014/06/19 22:21:43 UTC

android commit: Rethinking the URI helper

Repository: cordova-android
Updated Branches:
  refs/heads/4.0.x 0ffb5d253 -> 8ac067da8


Rethinking the URI helper


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

Branch: refs/heads/4.0.x
Commit: 8ac067da894a90d5429f8d939ecdc9976966efe2
Parents: 0ffb5d2
Author: Joe Bowser <bo...@apache.org>
Authored: Wed Jun 18 16:46:22 2014 -0700
Committer: Joe Bowser <bo...@apache.org>
Committed: Thu Jun 19 13:20:44 2014 -0700

----------------------------------------------------------------------
 .../apache/cordova/AndroidWebViewClient.java    |   8 +-
 .../org/apache/cordova/CordovaUriHelper.java    | 113 +++++++++++++++++++
 .../cordova/test/junit/CordovaActivityTest.java |   4 +-
 .../apache/cordova/test/junit/CordovaTest.java  |   6 +-
 .../cordova/test/junit/GapClientTest.java       |   4 +-
 test/src/org/apache/cordova/test/whitelist.java |   1 -
 6 files changed, 127 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/framework/src/org/apache/cordova/AndroidWebViewClient.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AndroidWebViewClient.java b/framework/src/org/apache/cordova/AndroidWebViewClient.java
index b4166ec..044b2c4 100755
--- a/framework/src/org/apache/cordova/AndroidWebViewClient.java
+++ b/framework/src/org/apache/cordova/AndroidWebViewClient.java
@@ -56,10 +56,11 @@ import android.webkit.WebViewClient;
  */
 public class AndroidWebViewClient extends WebViewClient implements CordovaWebViewClient{
 
-	private static final String TAG = "CordovaWebViewClient";
+	private static final String TAG = "AndroidWebViewClient";
 	private static final String CORDOVA_EXEC_URL_PREFIX = "http://cdv_exec/";
     CordovaInterface cordova;
     AndroidWebView appView;
+    CordovaUriHelper helper;
     private boolean doClearHistory = false;
     boolean isCurrentlyLoading;
 
@@ -84,6 +85,7 @@ public class AndroidWebViewClient extends WebViewClient implements CordovaWebVie
     public AndroidWebViewClient(CordovaInterface cordova, AndroidWebView view) {
         this.cordova = cordova;
         this.appView = view;
+        helper = new CordovaUriHelper(cordova, view);
     }
 
     /**
@@ -93,6 +95,7 @@ public class AndroidWebViewClient extends WebViewClient implements CordovaWebVie
      */
     public void setWebView(AndroidWebView view) {
         this.appView = view;
+        helper = new CordovaUriHelper(cordova, view);
     }
 
 
@@ -130,6 +133,7 @@ public class AndroidWebViewClient extends WebViewClient implements CordovaWebVie
 	@Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
     	// Check if it's an exec() bridge command message.
+	    /*
     	if (NativeToJsMessageQueue.ENABLE_LOCATION_CHANGE_EXEC_MODE && url.startsWith(CORDOVA_EXEC_URL_PREFIX)) {
     		handleExecUrl(url);
     	}
@@ -235,6 +239,8 @@ public class AndroidWebViewClient extends WebViewClient implements CordovaWebVie
             }
         }
         return true;
+        */
+	    return helper.shouldOverrideUrlLoading(view, url);
     }
     
     /**

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/framework/src/org/apache/cordova/CordovaUriHelper.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaUriHelper.java b/framework/src/org/apache/cordova/CordovaUriHelper.java
new file mode 100644
index 0000000..5e9aa84
--- /dev/null
+++ b/framework/src/org/apache/cordova/CordovaUriHelper.java
@@ -0,0 +1,113 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+*/
+
+package org.apache.cordova;
+
+import org.json.JSONException;
+
+import android.content.Intent;
+import android.net.Uri;
+import android.util.Log;
+import android.webkit.WebView;
+
+public class CordovaUriHelper {
+    
+    private static final String TAG = "CordovaUriHelper";
+    private static final String CORDOVA_EXEC_URL_PREFIX = "http://cdv_exec/";
+    
+    private CordovaWebView appView;
+    private CordovaInterface cordova;
+    
+    CordovaUriHelper(CordovaInterface cdv, CordovaWebView webView)
+    {
+        appView = webView;
+        cordova = cdv;
+    }
+    
+    // Parses commands sent by setting the webView's URL to:
+    // cdvbrg:service/action/callbackId#jsonArgs
+    void handleExecUrl(String url) {
+        int idx1 = CORDOVA_EXEC_URL_PREFIX.length();
+        int idx2 = url.indexOf('#', idx1 + 1);
+        int idx3 = url.indexOf('#', idx2 + 1);
+        int idx4 = url.indexOf('#', idx3 + 1);
+        if (idx1 == -1 || idx2 == -1 || idx3 == -1 || idx4 == -1) {
+            Log.e(TAG, "Could not decode URL command: " + url);
+            return;
+        }
+        String service    = url.substring(idx1, idx2);
+        String action     = url.substring(idx2 + 1, idx3);
+        String callbackId = url.substring(idx3 + 1, idx4);
+        String jsonArgs   = url.substring(idx4 + 1);
+        try {
+            appView.exec(service, action, callbackId, jsonArgs);
+            //There is no reason to not send this directly to the pluginManager
+            
+        } catch (JSONException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+    
+
+    /**
+     * Give the host application a chance to take over the control when a new url
+     * is about to be loaded in the current WebView.
+     *
+     * @param view          The WebView that is initiating the callback.
+     * @param url           The url to be loaded.
+     * @return              true to override, false for default behavior
+     */
+    public boolean shouldOverrideUrlLoading(WebView view, String url) {
+        // The WebView should support http and https when going on the Internet
+        if(url.startsWith("http"))
+        {
+            // Check if it's an exec() bridge command message.
+            if (NativeToJsMessageQueue.ENABLE_LOCATION_CHANGE_EXEC_MODE && url.startsWith(CORDOVA_EXEC_URL_PREFIX)) {
+                handleExecUrl(url);
+            }
+            // We only need to whitelist sites on the Internet! 
+            else if(Config.isUrlWhiteListed(url))
+            {
+                return false;
+            }
+        }
+        // Give plugins the chance to handle the url
+        else if (this.appView.onOverrideUrlLoading(url)) {
+            
+        }
+        else if(url.startsWith("file://") | url.startsWith("data:"))
+        {
+            return false;
+        }
+        else
+        {
+            try {
+                Intent intent = new Intent(Intent.ACTION_VIEW);
+                intent.setData(Uri.parse(url));
+                this.cordova.getActivity().startActivity(intent);
+            } catch (android.content.ActivityNotFoundException e) {
+                LOG.e(TAG, "Error loading url " + url, e);
+            }
+        }
+        //Default behaviour should be to load the default intent, let's see what happens! 
+        return true;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java b/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java
index f9d70b2..8679f8c 100644
--- a/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java
+++ b/test/src/org/apache/cordova/test/junit/CordovaActivityTest.java
@@ -59,9 +59,9 @@ public class CordovaActivityTest extends ActivityInstrumentationTestCase2<Cordov
     }
     
 
-    public void testForCordovaView() {
+    public void testForAndroidWebView() {
         String className = testView.getClass().getSimpleName();
-        assertTrue(className.equals("CordovaWebView"));
+        assertTrue(className.equals("AndroidWebView"));
     }
     
     public void testForLinearLayout() {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/test/src/org/apache/cordova/test/junit/CordovaTest.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/junit/CordovaTest.java b/test/src/org/apache/cordova/test/junit/CordovaTest.java
index 02f4ee6..2e09611 100644
--- a/test/src/org/apache/cordova/test/junit/CordovaTest.java
+++ b/test/src/org/apache/cordova/test/junit/CordovaTest.java
@@ -49,11 +49,11 @@ public class CordovaTest extends
     assertNotNull(testView);
   }
 
-  public void testForCordovaView() {
+  public void testForAndroidWebView() {
     //Sleep for no reason!!!!
-    sleep();
+    sleep();        
     String className = testView.getClass().getSimpleName();
-    assertTrue(className.equals("CordovaWebView"));
+    assertTrue(className.equals("AndroidWebView"));
   }
 
   /*

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/test/src/org/apache/cordova/test/junit/GapClientTest.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/junit/GapClientTest.java b/test/src/org/apache/cordova/test/junit/GapClientTest.java
index 4540d92..2203ef8 100644
--- a/test/src/org/apache/cordova/test/junit/GapClientTest.java
+++ b/test/src/org/apache/cordova/test/junit/GapClientTest.java
@@ -59,9 +59,9 @@ public class GapClientTest extends ActivityInstrumentationTestCase2<CordovaWebVi
 		assertNotNull(testView);
 	}
 	
-	public void testForCordovaView() {
+	public void testForAndroidWebView() {
 	    String className = testView.getClass().getSimpleName();
-	    assertTrue(className.equals("CordovaWebView"));
+	    assertTrue(className.equals("AndroidWebView"));
 	}
 	
 	

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/8ac067da/test/src/org/apache/cordova/test/whitelist.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/whitelist.java b/test/src/org/apache/cordova/test/whitelist.java
index 879c2f8..5dbb645 100755
--- a/test/src/org/apache/cordova/test/whitelist.java
+++ b/test/src/org/apache/cordova/test/whitelist.java
@@ -22,7 +22,6 @@ import android.os.Bundle;
 import android.webkit.WebView;
 
 import org.apache.cordova.*;
-import org.apache.cordova.LOG;
 
 public class whitelist extends CordovaActivity {
     @Override