package com.growthbeat.link;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.LinkedHashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.growthbeat.utils.SystemServiceUtils;

public class FingerprintReceiver {

	public interface Callback {
		void onComplete(String fingerprintParameters);
	}

	public static void getFingerprintParameters(Context context, String fingerprintUrl, final Callback callback) {

		final WindowManager windowManager = SystemServiceUtils.getWindowManager(context);

		WebView webView = new WebView(context);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setDomStorageEnabled(true);
		webView.setVisibility(View.INVISIBLE);
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView argWebView, String argString) {

				String requestString = argString;
				if (requestString.startsWith("native://fingerprint?fingerprintParameters=")) {
					Log.d("request_string", requestString);
					Map<String, String> param;
					String fingerpritnParameters = null;
					try {
						param = splitQuery(new URI(requestString));
						fingerpritnParameters = param.get("fingerprintParameters");
					} catch (URISyntaxException e) {
						e.printStackTrace();
						callback.onComplete(null);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					windowManager.removeView(argWebView);
					callback.onComplete(fingerpritnParameters);
				}
				return (true);
			}
		});

		WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
				WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_TOAST,
				WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
		windowManager.addView(webView, layoutParams);

		webView.loadUrl(fingerprintUrl);

	}

	private static Map<String, String> splitQuery(URI uri) throws UnsupportedEncodingException {
		Map<String, String> query_pairs = new LinkedHashMap<String, String>();
		String query = uri.getQuery();
		String[] pairs = query.split("&");
		for (String pair : pairs) {
			int idx = pair.indexOf("=");
			query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
		}
		return query_pairs;
	}

}

@@ -27,193 +27,8 @@
 k;%0A%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URLDecoder;%0Aimport java.util.LinkedHashMap;%0Aimport java.util.Map;%0A%0A
 impo
@@ -111,16 +111,15 @@
 oid.
-util.Log
+net.Uri
 ;%0Aim
@@ -898,20 +898,17 @@
 WebView 
-argW
+w
 ebView, 
@@ -914,19 +914,19 @@
  String 
-arg
+url
 String) 
@@ -936,40 +936,38 @@
 %09%09%09%09
-String requestString = arg
+Uri uri = Uri.parse(url
 String
+)
 ;%0A%09%09
@@ -976,509 +976,225 @@
 if (
-requestString.startsWith(%22native://fingerprint?fingerprintParameters=%22)) %7B%0A%09%09%09%09%09Log.d(%22request_string%22, requestString);%0A%09%09%09%09%09Map%3CString, String%3E param;%0A%09
+uri == null)%0A%09%09%09%09%09return false;%0A%0A
 %09%09%09%09
-String fingerpritnParameters = null;%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09param = splitQuery(new URI(requestString));%0A%09%09%09%09%09%09fingerpritnParameters = param.get(%22fingerprintParameters%22);%0A%09%09%09%09%09%7D catch (URISyntaxException e) %7B%0A%09%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09%09callback.onComplete(null);%0A%09%09%09%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09%09%09%09e.printStackTrace(
+if (!uri.getScheme().equals(%22native%22))%0A%09%09%09%09%09return false;%0A%0A%09%09%09%09if (uri.getHost().equals(%22fingerprint%22)) %7B%0A%09%09%09%09%09callback.onComplete(uri.getQueryParameter(%22fingerprintParameters%22)
 );%0A
-%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1223,12 +1223,9 @@
 iew(
-argW
+w
 ebVi
@@ -1237,58 +1237,10 @@
 %09%09%09%09
-%09callback.onComplete(fingerpritnParameters);%0A%09%09%09%09
 %7D
+%0A
 %0A%09%09%09
@@ -1247,23 +1247,22 @@
 %09return 
-(
 true
-)
 ;
+%0A
 %0A%09%09%09%7D%0A%09%09
@@ -1642,460 +1642,6 @@
 %09%7D%0A%0A
-%09private static Map%3CString, String%3E splitQuery(URI uri) throws UnsupportedEncodingException %7B%0A%09%09Map%3CString, String%3E query_pairs = new LinkedHashMap%3CString, String%3E();%0A%09%09String query = uri.getQuery();%0A%09%09String%5B%5D pairs = query.split(%22&%22);%0A%09%09for (String pair : pairs) %7B%0A%09%09%09int idx = pair.indexOf(%22=%22);%0A%09%09%09query_pairs.put(URLDecoder.decode(pair.substring(0, idx), %22UTF-8%22), URLDecoder.decode(pair.substring(idx + 1), %22UTF-8%22));%0A%09%09%7D%0A%09%09return query_pairs;%0A%09%7D%0A%0A
 %7D%0A

Refactor code of FinerprintReceiver