
package com.nasageek.utexasutilities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.nasageek.utexasutilities.activities.UTilitiesActivity;

import static com.nasageek.utexasutilities.UTilitiesApplication.PNA_AUTH_COOKIE_KEY;
import static com.nasageek.utexasutilities.UTilitiesApplication.UTD_AUTH_COOKIE_KEY;

public class LoginWebViewClient extends WebViewClient {

    private Context context;
    private String nextActivity;
    private char service;

    public LoginWebViewClient(Context con, String nextActivity, char service) {
        super();
        this.context = con;
        this.nextActivity = nextActivity;
        this.service = service;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        String authCookie = "";
        String cookies = "";
        switch (service) {
            case 'z':
                ((Activity) context).finish();
                break;
            case 'p':
                if (url.contains("pna.utexas.edu")) {
                    cookies = CookieManager.getInstance().getCookie("https://pna.utexas.edu");
                    if (cookies != null && cookies.contains("AUTHCOOKIE=")) {
                        for (String s : cookies.split("; ")) {
                            if (s.startsWith("AUTHCOOKIE=")) {
                                authCookie = s.substring(11);
                                break;
                            }
                        }
                    }
                    if (!authCookie.equals("")) {
                        AuthCookie pnaAuthCookie = ((UTilitiesApplication) ((Activity) context)
                                .getApplication()).getAuthCookie(PNA_AUTH_COOKIE_KEY);
                        pnaAuthCookie.setAuthCookieVal(authCookie);
                        continueToActivity("UT PNA");
                        return;
                    }
                }
                break;

            case 'u':
                if (url.contains("utexas.edu")) {
                    cookies = CookieManager.getInstance().getCookie("https://login.utexas.edu");
                    if (cookies != null) {
                        for (String s : cookies.split("; ")) {
                            if (s.startsWith("utlogin-prod=")) {
                                authCookie = s.substring(13);
                                break;
                            }
                        }
                    }
                    if (!authCookie.equals("")
                            && url.equals("https://www.utexas.edu/")) {
                        AuthCookie utdAuthCookie = ((UTilitiesApplication) ((Activity) context)
                                .getApplication()).getAuthCookie(UTD_AUTH_COOKIE_KEY);
                        utdAuthCookie.setAuthCookieVal(authCookie);
                        continueToActivity("UTLogin");
                        return;
                    }
                }
                break;
        }
    }

    private void continueToActivity(String service) {
        Intent intent = null;
        try {
            intent = new Intent(context, Class.forName(nextActivity));
            Toast.makeText(context, "You're now logged in to " + service, Toast.LENGTH_SHORT)
                    .show();
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
            intent = new Intent(context, UTilitiesActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Toast.makeText(context, "Your attempt to log in went terribly wrong",
                    Toast.LENGTH_SHORT).show();
        }
        context.startActivity(intent);
        CookieManager.getInstance().removeAllCookie();
        return;
    }
}

@@ -144,26 +144,99 @@
 oid.
-webkit.CookieManag
+net.http.SslError;%0Aimport android.webkit.CookieManager;%0Aimport android.webkit.SslErrorHandl
 er;%0A
@@ -3389,24 +3389,423 @@
    %7D%0A    %7D%0A%0A
+    @Override%0A    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) %7B%0A        // Ignore SSL certificate errors for PNA, can't use my client cert :(%0A        if (view.getUrl().equals(%22https://management.pna.utexas.edu/server/graph.cgi%22)) %7B%0A            handler.proceed();%0A        %7D else %7B%0A            super.onReceivedSslError(view, handler, error);%0A        %7D%0A    %7D%0A%0A
     private 

Fix loading UT PNA login page