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/06/21 23:05:05 UTC

[11/15] android commit: Attempt to test icecream_workaround from viafirma

Attempt to test icecream_workaround from viafirma


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/adc88f01
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/adc88f01
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/adc88f01

Branch: refs/heads/master
Commit: adc88f01b78e1abcdb4679da7ebfcc8ba5c4f2c4
Parents: 507554b dffd2de
Author: Joe Bowser <bo...@apache.org>
Authored: Wed Jun 20 12:45:51 2012 -0700
Committer: Joe Bowser <bo...@apache.org>
Committed: Wed Jun 20 12:45:51 2012 -0700

----------------------------------------------------------------------
 .../org/apache/cordova/CordovaWebViewClient.java   |   43 +++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/adc88f01/framework/src/org/apache/cordova/CordovaWebViewClient.java
----------------------------------------------------------------------
diff --cc framework/src/org/apache/cordova/CordovaWebViewClient.java
index c9a3261,63ea301..85261c3
--- a/framework/src/org/apache/cordova/CordovaWebViewClient.java
+++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java
@@@ -18,13 -18,12 +18,17 @@@
  */
  package org.apache.cordova;
  
 +import java.util.Hashtable;
 +
 +import org.apache.cordova.api.CordovaInterface;
+ import java.io.IOException;
+ import java.io.InputStream;
+ 
  import org.apache.cordova.api.LOG;
 +import org.json.JSONException;
 +import org.json.JSONObject;
  
+ import android.content.Context;
  import android.content.Intent;
  import android.content.pm.ApplicationInfo;
  import android.content.pm.PackageManager;
@@@ -239,8 -197,7 +245,9 @@@ public class CordovaWebViewClient exten
  
      /**
       * Notify the host application that a page has finished loading.
 +     * This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet.
 +     * 
+      *
       * @param view          The webview initiating the callback.
       * @param url           The url of the page.
       */
@@@ -312,38 -270,22 +319,39 @@@
       */
      @Override
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
 -        LOG.d(TAG, "DroidGap: GapViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl);
 +        LOG.d(TAG, "CordovaWebViewClient.onReceivedError: Error code=%s Description=%s URL=%s", errorCode, description, failingUrl);
  
          // Clear timeout flag
 -        this.ctx.loadUrlTimeout++;
 -
 -        // Stop "app loading" spinner if showing
 -        this.ctx.spinnerStop();
 +        this.appView.loadUrlTimeout++;
  
          // Handle error
 -        this.ctx.onReceivedError(errorCode, description, failingUrl);
 +        JSONObject data = new JSONObject();
 +        try {
 +            data.put("errorCode", errorCode);
 +            data.put("description", description);
 +            data.put("url", failingUrl);
 +        } catch (JSONException e) {
 +            e.printStackTrace();
 +        }
 +        this.appView.postMessage("onReceivedError", data);
      }
  
 +    /**
 +     * Notify the host application that an SSL error occurred while loading a resource. 
 +     * The host application must call either handler.cancel() or handler.proceed(). 
 +     * Note that the decision may be retained for use in response to future SSL errors. 
 +     * The default behavior is to cancel the load.
 +     * 
 +     * @param view          The WebView that is initiating the callback.
 +     * @param handler       An SslErrorHandler object that will handle the user's response.
 +     * @param error         The SSL error object.
 +     */
 +    @Override
      public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  
 -        final String packageName = this.ctx.getPackageName();
 -        final PackageManager pm = this.ctx.getPackageManager();
 +        final String packageName = this.cordova.getActivity().getPackageName();
 +        final PackageManager pm = this.cordova.getActivity().getPackageManager();
++
          ApplicationInfo appInfo;
          try {
              appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
@@@ -374,81 -309,44 +382,116 @@@
           * 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 (!this.appView.peekAtUrlStack().equals(url)) {
 +            this.appView.pushUrl(url);
 +        }
 +    }
 +
 +    /**
 +     * Sets the authentication token.
 +     * 
 +     * @param authenticationToken
 +     * @param host
 +     * @param realm
 +     */
 +    public void setAuthenticationToken(AuthenticationToken authenticationToken, String host, String realm) {
 +        if (host == null) {
 +            host = "";
 +        }
 +        if (realm == null) {
 +            realm = "";
          }
 +        this.authenticationTokens.put(host.concat(realm), authenticationToken);
 +    }
 +
 +    /**
 +     * Removes the authentication token.
 +     * 
 +     * @param host
 +     * @param realm
 +     * 
 +     * @return the authentication token or null if did not exist
 +     */
 +    public AuthenticationToken removeAuthenticationToken(String host, String realm) {
 +        return this.authenticationTokens.remove(host.concat(realm));
 +    }
 +
 +    /**
 +     * Gets the authentication token.
 +     * 
 +     * In order it tries:
 +     * 1- host + realm
 +     * 2- host
 +     * 3- realm
 +     * 4- no host, no realm
 +     * 
 +     * @param host
 +     * @param realm
 +     * 
 +     * @return the authentication token
 +     */
 +    public AuthenticationToken getAuthenticationToken(String host, String realm) {
 +        AuthenticationToken token = null;
 +        token = this.authenticationTokens.get(host.concat(realm));
 +
 +        if (token == null) {
 +            // try with just the host
 +            token = this.authenticationTokens.get(host);
 +
 +            // Try the realm
 +            if (token == null) {
 +                token = this.authenticationTokens.get(realm);
 +            }
 +
 +            // if no host found, just query for default
 +            if (token == null) {
 +                token = this.authenticationTokens.get("");
 +            }
 +        }
 +
 +        return token;
 +    }
 +
 +    /**
 +     * Clear all authentication tokens.
 +     */
 +    public void clearAuthenticationTokens() {
 +        this.authenticationTokens.clear();
      }
  
+     @Override
+     public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
+         if(url.contains("?")){
+             return generateWebResourceResponse(url);
+         } else {
+             return super.shouldInterceptRequest(view, url);
+         }
+     }
+ 
+     private WebResourceResponse generateWebResourceResponse(String url) {
+         final String ANDROID_ASSET = "file:///android_asset/";
+         if (url.startsWith(ANDROID_ASSET)) {
+             String niceUrl = url;
+             niceUrl = url.replaceFirst(ANDROID_ASSET, "");
+             if(niceUrl.contains("?")){
+                 niceUrl = niceUrl.split("\\?")[0];
+             }
+ 
+             String mimetype = null;
+             if(niceUrl.endsWith(".html")){
+                 mimetype = "text/html";
+             }
+ 
+             try {
 -                AssetManager assets = ctx.getAssets();
++                AssetManager assets = cordova.getActivity().getAssets();
+                 Uri uri = Uri.parse(niceUrl);
+                 InputStream stream = assets.open(uri.getPath(), AssetManager.ACCESS_STREAMING);
+                 WebResourceResponse response = new WebResourceResponse(mimetype, "UTF-8", stream);
+                 return response;
+             } catch (IOException e) {
 -                Log.e("generateWebResourceResponse", e.getMessage(), e);
++                LOG.e("generateWebResourceResponse", e.getMessage(), e);
+             }
+         }
+         return null;
+     }
  }