package org.ccci.gto.android.thekey;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebSettings;
import android.webkit.WebView;

public final class DisplayUtil {
    @SuppressLint("SetJavaScriptEnabled")
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public final static WebView createLoginWebView(final Context context, final TheKey thekey,
            final LoginWebViewClient webViewClient) {
        final WebView webView = new WebView(context);
        webView.setVisibility(View.GONE);
        webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webView.setScrollbarFadingEnabled(true);

        final WebSettings settings = webView.getSettings();
        settings.setSavePassword(false);
        settings.setJavaScriptEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setAllowFileAccess(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            settings.setAllowContentAccess(false);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                settings.setAllowFileAccessFromFileURLs(false);
                settings.setAllowUniversalAccessFromFileURLs(false);
            }
        }

        webView.setWebViewClient(webViewClient);
        webView.loadUrl(thekey.getAuthorizeUri().toString());
        return webView;
    }
}

@@ -165,16 +165,49 @@
 .Build;%0A
+import android.view.MotionEvent;%0A
 import a
@@ -224,16 +224,16 @@
 w.View;%0A
-
 import a
@@ -499,22 +499,16 @@
  public 
-final 
 static W
@@ -577,16 +577,49 @@
 thekey,%0A
+                                 
         
@@ -1707,31 +1707,1074 @@
 ));%0A
-        return webView;
+%0A        // apply any hacks to work around various bugs in previous versions of android%0A        switch (Build.VERSION.SDK_INT) %7B%0A            case Build.VERSION_CODES.FROYO:%0A            case Build.VERSION_CODES.GINGERBREAD:%0A            case Build.VERSION_CODES.GINGERBREAD_MR1:%0A                webView.setOnTouchListener(new SDK8TouchListener());%0A                break;%0A        %7D%0A%0A        return webView;%0A    %7D%0A%0A    /**%0A     * This listener is only used on SDK levels 8-10%0A     *%0A     * This will work around a focus bug affecting WebViews%0A     *%0A     * see http://stackoverflow.com/questions/3460915%0A     */%0A    private final static class SDK8TouchListener implements View.OnTouchListener %7B%0A        @Override%0A        public boolean onTouch(final View v, final MotionEvent event) %7B%0A            switch (event.getAction()) %7B%0A                case MotionEvent.ACTION_DOWN:%0A                case MotionEvent.ACTION_UP:%0A                    if (!v.hasFocus()) %7B%0A                        v.requestFocus();%0A                    %7D%0A            %7D%0A            return false;%0A        %7D
 %0A   

add in a workaround for being able to enter text into the login WebView on API level <= 10