/*------------------------------------------------------------------------
* (The MIT License)
* 
* Copyright (c) 2008-2011 Rhomobile, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* 
* http://rhomobile.com
*------------------------------------------------------------------------*/

package com.rhomobile.rhodes.webview;

import java.io.File;

import com.rhomobile.rhodes.LocalFileProvider;
import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.RhoConf;
import com.rhomobile.rhodes.RhodesActivity;
import com.rhomobile.rhodes.RhodesService;
import com.rhomobile.rhodes.extmanager.IRhoExtension;
import com.rhomobile.rhodes.extmanager.RhoExtManager;

import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.view.Window;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.CookieSyncManager;

public class RhoWebViewClient extends WebViewClient
{
    private final String TAG = RhoWebViewClient.class.getSimpleName();
    private GoogleWebView mWebView;

    public RhoWebViewClient(GoogleWebView webView) {
        mWebView = webView;
    }

//    @Override
//    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
//        Logger.I(TAG, "Resource request: " + url);
//        return null;
//    }
//
//    public void onLoadResource(WebView view, String url) {
//        Logger.I(TAG, "Load resource" + url);
//    }

    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Logger.I(TAG, "Loading URL: " + url);
         if(url.contains("HTML") || url.contains("HTM"))
        {
        	if(url.contains("HTML"))
        		url=url.replace("HTML", "html");
        	else
        		url=url.replace("HTM", "htm");
        }
        
        
        
        
        boolean res = RhodesService.getInstance().handleUrlLoading(url);
        if (!res) {
            Logger.profStart("BROWSER_PAGE");
            
            RhoExtManager.getImplementationInstance().onBeforeNavigate(view, url);
            
            Uri localUri = LocalFileProvider.overrideUri(Uri.parse(url));
            if (localUri != null) {
                url = Uri.decode(localUri.toString());
                Logger.T(TAG, "Override URL: " + url);
                view.loadUrl(url);
                return true;
            }
        }
        return res;
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        
        RhoExtManager.getImplementationInstance().onNavigateStarted(view, url);

        if (mWebView.getConfig() != null && mWebView.getConfig().getBool("enablePageLoadingIndication"))
            RhodesActivity.safeGetInstance().getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 0);
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {

        Logger.profStop("BROWSER_PAGE");

        // Set title
        String title = view.getTitle();
        RhodesActivity.safeGetInstance().setTitle(title);
        if (mWebView.getConfig() != null && mWebView.getConfig().getBool("enablePageLoadingIndication"))
            RhodesActivity.safeGetInstance().getWindow().setFeatureInt(Window.FEATURE_PROGRESS, RhodesActivity.MAX_PROGRESS);

        RhoExtManager.getImplementationInstance().onNavigateComplete(view, url);
        //CookieSyncManager.getInstance().sync();

        super.onPageFinished(view, url);
    }
    
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        
        Logger.profStop("BROWSER_PAGE");
        
        StringBuilder msg = new StringBuilder(failingUrl != null ? failingUrl : "null");
        msg.append(" failed: ");
        msg.append(errorCode);
        msg.append(" - " + description);
        Logger.E(TAG, msg.toString());

        RhoExtManager.getImplementationInstance().onLoadError(view, IRhoExtension.LoadErrorReason.INTERNAL_ERROR);
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        
        if(RhoConf.getBool("no_ssl_verify_peer")) {
            Logger.D(TAG, "Skip SSL error.");
            handler.proceed();
        } else {
            StringBuilder msg = new StringBuilder();
            msg.append("SSL error - ");
            switch(error.getPrimaryError()) {
            case SslError.SSL_NOTYETVALID:
                msg.append("The certificate is not yet valid: ");
                break;
            case SslError.SSL_EXPIRED:
                msg.append("The certificate has expired: ");
                break;
            case SslError.SSL_IDMISMATCH:
                msg.append("Hostname mismatch: ");
                break;
            case SslError.SSL_UNTRUSTED:
                msg.append("The certificate authority is not trusted: ");
                break;
            }
            msg.append(error.getCertificate().toString());
            Logger.W(TAG, msg.toString());
            handler.cancel();
        }
    }
}

@@ -2714,19 +2714,28 @@
         
+%0A       
  if
+ 
 (url.con
@@ -2745,38 +2745,16 @@
 ns(%22
-HTML%22) %7C%7C url.contains(%22
+.
 HTM%22))
+ 
 %0A   
@@ -2764,52 +2764,18 @@
   %7B%0A
-    
     %09
-if(url.contains(%22HTML%22))%0A   
      
-%09%09
 url=
@@ -2791,16 +2791,17 @@
 ce(%22
+.
 HTML%22, %22
 html
@@ -2796,16 +2796,17 @@
 HTML%22, %22
+.
 html%22);%0A
@@ -2813,28 +2813,14 @@
     
- 
+%09  
    
-%09else%0A        %09%09
 url=
@@ -2836,15 +2836,17 @@
 ce(%22
+.
 HTM%22, %22
+.
 htm%22
@@ -2848,28 +2848,91 @@
 .htm%22);%0A    
+%09
     
+ Logger.I(TAG, %22Changed to lower case html, url=%22+ url);%0A    %09
 %7D%0A        %0A 

Update RhoWebViewClient.java