// Path: app/src/main/java/com/mikifus/padland/Utils/WhiteListMatcher.java
// public class WhiteListMatcher {
//     /**
//      * Performs a wildcard matching for the text and pattern
//      * provided.
//      *
//      * @param text the text to be tested for matches.
//      *
//      * @param pattern the pattern to be matched for.
//      * This can contain the wildcard character '*' (asterisk).
//      *
//      * @return <tt>true</tt> if a match is found, <tt>false</tt>
//      * otherwise.
//      *
//      * @url http://www.adarshr.com/simple-implementation-of-wildcard-text-matching-using-java
//      */
//     public static boolean wildCardMatch(String text, String pattern) {
//         // Create the cards by splitting using a RegEx. If more speed
//         // is desired, a simpler character based splitting can be done.
//         String [] cards = pattern.split("\\*");
// 
//         // Iterate over the cards.
//         for (String card : cards) {
//             int idx = text.indexOf(card);
// 
//             // Card not detected in the text.
//             if(idx == -1) {
//                 return false;
//             }
// 
//             // Move ahead, towards the right of the text.
//             text = text.substring(idx + card.length());
//         }
// 
//         return true;
//     }
// 
//     /**
//      * Checks if the url parameter has a valid host
//      * among a list of hosts.
//      *
//      * @param url
//      * @param hostsWhitelist
//      * @return
//      */
//     public static boolean isValidHost(String url, String[] hostsWhitelist){
//         if (!TextUtils.isEmpty(url)) {
//             final String host = Uri.parse(url).getHost();
//             if( host == null ) {
//                 return false;
//             }
//             for (String whitelistedHost: hostsWhitelist){
//                 if(wildCardMatch(host, whitelistedHost)) {
//                     return true;
//                 }
//             }
//         }
//         return false;
//     }
// 
//     /**
//      * Checks if the URL can be parsed.
//      *
//      * @param padUrl
//      * @return
//      */
//     public static boolean checkValidUrl(String padUrl) {
//         // Check if it is a valid url
//         try {
//             new URL(padUrl);
//         } catch (MalformedURLException e) {
//             e.printStackTrace();
//             return false;
//         }
//         return true;
//     }
// }


import android.util.Log;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import com.mikifus.padland.Utils.WhiteListMatcher;
import java.io.ByteArrayInputStream;
package com.mikifus.padland.SaferWebView;




/**
 * Extended class, I
 * Created by mikifus on 23/04/16.
 */
public class PadLandSaferWebViewClient extends SaferWebViewClient {
    public PadLandSaferWebViewClient(String[] hostsWhitelist) {
        super(hostsWhitelist);
    }


    @Override
    protected WebResourceResponse getWebResourceResponseFromString() {
        Log.w("SaferWebViewClient", "Blocked a JS request to an external domains.");
        return getUtf8EncodedWebResourceResponse(new ByteArrayInputStream("".getBytes()));
    }

    /**
     * Returning false we allow http to https redirects
     * @param view
     * @param url
     * @return
     */
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//        return isValidHost(url);
        return false;
    }

    protected boolean isValidHost(String url){
// Path: app/src/main/java/com/mikifus/padland/Utils/WhiteListMatcher.java
// public class WhiteListMatcher {
//     /**
//      * Performs a wildcard matching for the text and pattern
//      * provided.
//      *
//      * @param text the text to be tested for matches.
//      *
//      * @param pattern the pattern to be matched for.
//      * This can contain the wildcard character '*' (asterisk).
//      *
//      * @return <tt>true</tt> if a match is found, <tt>false</tt>
//      * otherwise.
//      *
//      * @url http://www.adarshr.com/simple-implementation-of-wildcard-text-matching-using-java
//      */
//     public static boolean wildCardMatch(String text, String pattern) {
//         // Create the cards by splitting using a RegEx. If more speed
//         // is desired, a simpler character based splitting can be done.
//         String [] cards = pattern.split("\\*");
// 
//         // Iterate over the cards.
//         for (String card : cards) {
//             int idx = text.indexOf(card);
// 
//             // Card not detected in the text.
//             if(idx == -1) {
//                 return false;
//             }
// 
//             // Move ahead, towards the right of the text.
//             text = text.substring(idx + card.length());
//         }
// 
//         return true;
//     }
// 
//     /**
//      * Checks if the url parameter has a valid host
//      * among a list of hosts.
//      *
//      * @param url
//      * @param hostsWhitelist
//      * @return
//      */
//     public static boolean isValidHost(String url, String[] hostsWhitelist){
//         if (!TextUtils.isEmpty(url)) {
//             final String host = Uri.parse(url).getHost();
//             if( host == null ) {
//                 return false;
//             }
//             for (String whitelistedHost: hostsWhitelist){
//                 if(wildCardMatch(host, whitelistedHost)) {
//                     return true;
//                 }
//             }
//         }
//         return false;
//     }
// 
//     /**
//      * Checks if the URL can be parsed.
//      *
//      * @param padUrl
//      * @return
//      */
//     public static boolean checkValidUrl(String padUrl) {
//         // Check if it is a valid url
//         try {
//             new URL(padUrl);
//         } catch (MalformedURLException e) {
//             e.printStackTrace();
//             return false;
//         }
//         return true;
//     }
// }


// Path: app/src/main/java/com/mikifus/padland/SaferWebView/PadLandSaferWebViewClient.java
import android.util.Log;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import com.mikifus.padland.Utils.WhiteListMatcher;
import java.io.ByteArrayInputStream;

package com.mikifus.padland.SaferWebView;




/**
 * Extended class, I
 * Created by mikifus on 23/04/16.
 */
public class PadLandSaferWebViewClient extends SaferWebViewClient {
    public PadLandSaferWebViewClient(String[] hostsWhitelist) {
        super(hostsWhitelist);
    }


    @Override
    protected WebResourceResponse getWebResourceResponseFromString() {
        Log.w("SaferWebViewClient", "Blocked a JS request to an external domains.");
        return getUtf8EncodedWebResourceResponse(new ByteArrayInputStream("".getBytes()));
    }

    /**
     * Returning false we allow http to https redirects
     * @param view
     * @param url
     * @return
     */
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//        return isValidHost(url);
        return false;
    }

    protected boolean isValidHost(String url){
        return WhiteListMatcher.isValidHost(url, hostsWhitelist);