package com.rhomobile.rhodes.webview;

import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.RhodesActivity;
import com.rhomobile.rhodes.RhodesService;

import android.graphics.Bitmap;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class RhoWebViewClient extends WebViewClient
{
    private final String TAG = RhoWebViewClient.class.getSimpleName();
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Logger.T(TAG, "Loading URL: " + url);
        return RhodesService.getInstance().handleUrlLoading(url);
    }
    
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        if (RhodesActivity.ENABLE_LOADING_INDICATION)
            RhodesActivity.safeGetInstance().getWindow().setFeatureInt(Window.FEATURE_PROGRESS, 0);
        super.onPageStarted(view, url, favicon);
    }
    
    @Override
    public void onPageFinished(WebView view, String url) {
        // Set title
        String title = view.getTitle();
        RhodesActivity.safeGetInstance().setTitle(title);
        if (RhodesActivity.ENABLE_LOADING_INDICATION)
            RhodesActivity.safeGetInstance().getWindow().setFeatureInt(
                    Window.FEATURE_PROGRESS, RhodesActivity.MAX_PROGRESS);
        
        super.onPageFinished(view, url);
    }
    
    public void onReceivedError(android.webkit.WebView view, int errorCode, java.lang.String description, java.lang.String failingUrl)
    {
        StringBuilder msg = new StringBuilder(failingUrl);
        msg.append(" failed: ");
        msg.append(errorCode);
        msg.append(" - " + description);
        Logger.E(TAG, msg.toString());
    }


}

@@ -68,16 +68,53 @@
 Logger;%0A
+import com.rhomobile.rhodes.RhoConf;%0A
 import c
@@ -244,19 +244,92 @@
 oid.
-view.Window
+net.http.SslError;%0Aimport android.view.Window;%0Aimport android.webkit.SslErrorHandler
 ;%0Aim
@@ -626,17 +626,17 @@
  Logger.
-T
+I
 (TAG, %22L
@@ -1841,11 +1841,1101 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) %7B%0A        %0A        if(RhoConf.getBool(%22NoSSLVerifyPeer%22)) %7B%0A            Logger.D(TAG, %22Skip SSL error.%22);// + error.toString());%0A            handler.proceed();%0A        %7D else %7B%0A            StringBuilder msg = new StringBuilder();%0A            msg.append(%22SSL error - %22);%0A            switch(error.getPrimaryError()) %7B%0A            case SslError.SSL_NOTYETVALID:%0A                msg.append(%22The certificate is not yet valid: %22);%0A                break;%0A            case SslError.SSL_EXPIRED:%0A                msg.append(%22The certificate has expired: %22);%0A                break;%0A            case SslError.SSL_IDMISMATCH:%0A                msg.append(%22Hostname mismatch: %22);%0A                break;%0A            case SslError.SSL_UNTRUSTED:%0A                msg.append(%22The certificate authority is not trusted: %22);%0A                break;%0A            %7D%0A            msg.append(error.getCertificate().toString());%0A            Logger.W(TAG, msg.toString());%0A            handler.cancel();%0A        %7D%0A    %7D
 %0A%7D%0A

introduce NoSSLVerifyPeer parameter in rhoconfig for WebView.