package com.sebnarware.avalanche;

import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        // get parameters from the intent
        Intent intent = getIntent();
        String title = intent.getStringExtra(MainActivity.INTENT_EXTRA_WEB_VIEW_TITLE);
        String url = intent.getStringExtra(MainActivity.INTENT_EXTRA_WEB_VIEW_URL);
    	
    	// get access to the activity indicator
       	// NOTE must happen before content is added
    	requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    	// show our icon
    	requestWindowFeature(Window.FEATURE_LEFT_ICON);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.logo);

        // set the title
        this.setTitle(title);

        WebView webView = (WebView) findViewById(R.id.webview);
        
        // show full page width by default
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        // enable zooming
        webView.getSettings().setBuiltInZoomControls(true);
        
        // make link navigation stay within this webview, vs. launching the browser
        webView.setWebViewClient(new WebViewClient());
        
        // enable javascript
        webView.getSettings().setJavaScriptEnabled(true);

		// enable activity indicator during loading
		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				if (progress == 100) {
					// all done
			        setProgressBarIndeterminateVisibility(false);
				} else {
					// in progress
					setProgressBarIndeterminateVisibility(true);
				}
			}
		});
		
	    // NOTE set our user agent string to something benign and non-mobile looking, to work around website
	    // popups from nwac.us asking if you would like to be redirected to the mobile version of the site
		webView.getSettings().setUserAgentString("Mozilla/5.0");

        
        // set cache mode, depending on current network availability
        if (NetworkEngine.isNetworkAvailable(this)){
            webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        } else {
            webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }
        
        webView.loadUrl(url);
    }

}

@@ -104,31 +104,17 @@
 .webkit.
-WebChromeClient
+*
 ;%0Aimport
@@ -126,94 +126,25 @@
 oid.
-webkit.WebSettings;%0Aimport android.webkit.WebView;%0Aimport android.webkit.WebViewClient
+net.http.SslError
 ;%0Aim
@@ -1676,16 +1676,373 @@
 Client()
+ %7B%0A        %09public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) %7B%0A        %09%09// NOTE ignore SSL errors (as some websites, like CAIC as of 2012-11-23, have them, in the sense %0A        %09%09// that Android does not trust GoDaddy as a root domain authority, and so hits the brakes%0A        %09%09handler.proceed();%0A        %09%7D%0A        %7D
 );%0A     
@@ -2847,16 +2847,229 @@
 ability%0A
+%09%09// NOTE see http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/webkit/CacheManager.java%0A%09%09// for some of the inner workings of the android webkit cache manager%0A
         
@@ -3278,20 +3278,12 @@
 CHE_
-ELSE_NETWORK
+ONLY
 );%0A 

handle (ignore) SSL errors; be more aggresive in using cache when offline, to hopefully not allow cache to fail if content is present but has expired