From a6e16aaf565cf2ac90393096ee250a0baaabc08e Mon Sep 17 00:00:00 2001 From: michaelsandner Date: Wed, 2 Sep 2015 21:58:34 +0200 Subject: [PATCH] enable alert in productmap --- .../productmap/ProductMapFragment.java | 175 +++++++++--------- 1 file changed, 85 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/de/fau/cs/mad/fablab/android/view/fragments/productmap/ProductMapFragment.java b/app/src/main/java/de/fau/cs/mad/fablab/android/view/fragments/productmap/ProductMapFragment.java index 52333b2..6a9bbc9 100644 --- a/app/src/main/java/de/fau/cs/mad/fablab/android/view/fragments/productmap/ProductMapFragment.java +++ b/app/src/main/java/de/fau/cs/mad/fablab/android/view/fragments/productmap/ProductMapFragment.java @@ -1,13 +1,19 @@ package de.fau.cs.mad.fablab.android.view.fragments.productmap; +import android.content.Context; import android.content.pm.ActivityInfo; import android.net.http.SslError; import android.os.AsyncTask; import android.os.Bundle; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.Display; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.WindowManager; import android.webkit.SslErrorHandler; +import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -19,6 +25,7 @@ import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManagerFactory; //import butterknife.InjectView; @@ -28,6 +35,7 @@ public class ProductMapFragment extends BaseDialogFragment implements CallBackListener { + private static final String LOG_TAG = "ProductMap"; public static final String KEY_LOCATION = "key_location"; private CallBackListener callBackListener = this; @@ -35,7 +43,8 @@ public class ProductMapFragment extends BaseDialogFragment implements CallBackLi WebView webview; @Override - public void onActivityCreated(Bundle savedInstanceState) { + public void onActivityCreated(Bundle savedInstanceState) + { super.onActivityCreated(savedInstanceState); @@ -46,28 +55,22 @@ public void onActivityCreated(Bundle savedInstanceState) { String locationString = getArguments().getString(KEY_LOCATION); //something like "fau fablab / werkstadt / ..." locationString = locationString.replace("_/", "/"); - locationString = locationString.replace(",_","_"); + locationString = locationString.replace(",_", "_"); locationString = locationString.replace("ä", "ae"); locationString = locationString.replace("ö", "oe"); locationString = locationString.replace("ü", "ue"); locationString = locationString.replace("ß", "ss"); - //test with local asset - // productMapUrl = "file:///android_asset/productMap.html?id=Werkbank"; String url = ""; - if(locationString != "") + if (locationString != "") url = productMapUrl + "?id=" + locationString; else url = productMapUrl; - /*3 creating a WebViewClient and override the - shouldOverrideUrlLoading method to setup a SSLSocket and pass the - received html data to WebView.loadData. However, I think there should - be a way to tell WebView directly which keystore and truststore it - should use to setup SSL connections.*/ - webview.getSettings().setJavaScriptEnabled(true); + webview.getSettings().setBuiltInZoomControls(true); + webview.setWebChromeClient(new WebChromeClient()); final String finalUrl = url; webview.loadUrl(url); @@ -75,47 +78,17 @@ public void onActivityCreated(Bundle savedInstanceState) { { @Override - public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) + public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { + //Method 1 handler.proceed(); - // next release -// try -// { -// //Default type is BKS on android -// KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); -// //our truststore containing the public certs -// InputStream inputStream = getResources().openRawResource(R.raw.fablab_dev_truststore); -// -// //the password used here is just a dummy as it is needed by the keystore.load method -// String trustStorePass = "dummypass4dev"; -// keyStore.load(inputStream, trustStorePass.toCharArray()); -// -// SSLContext sslContext = SSLContext.getInstance("TLS"); -// TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( -// TrustManagerFactory.getDefaultAlgorithm()); -// trustManagerFactory.init(keyStore); -// sslContext.init(null, trustManagerFactory.getTrustManagers(), null); -// -// -// -// ConnectionTask connectionTask = new ConnectionTask(); -// connectionTask.setListener(callBackListener); -// -// connectionTask.execute(finalUrl); -// -//// BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); -//// -// -// } -// catch (android.os.NetworkOnMainThreadException e) -// { -// handler.proceed(); -// } -// catch (Exception e) -// { -// e.printStackTrace(); -// } + + //Method 2 + //ConnectionTask connectionTask = new ConnectionTask(); + //connectionTask.setListener(callBackListener); + + //connectionTask.execute(finalUrl); } }); @@ -123,43 +96,41 @@ public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + Bundle savedInstanceState) + { return inflater.inflate(R.layout.fragment_locationmap, container, false); } @Override - public void onPause() { + public void onPause() + { super.onPause(); getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } @Override - public void onResume() { + public void onResume() + { super.onResume(); getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); - } + } @Override - public void callback(BufferedReader bufferedReader) + public void callback(String htmlText) { try { - String htmlText = ""; - String line = ""; - while ((line = bufferedReader.readLine()) != null) - htmlText += line; - webview.loadData(htmlText, "text/html; charset=UTF-8", null); - } - catch(Exception e) + + } catch (Exception e) { e.printStackTrace(); } } // next release - private class ConnectionTask extends AsyncTask + private class ConnectionTask extends AsyncTask { private CallBackListener mListener; @@ -171,56 +142,80 @@ public void setListener(CallBackListener listener) @Override - protected BufferedReader doInBackground(String... urls) + protected String doInBackground(String... urls) { BufferedReader bufferedReader; URL myUrl; HttpsURLConnection urlConnection; - try + try { - //Default type is BKS on android - KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); - //our truststore containing the public certs - InputStream inputStream = getResources().openRawResource(R.raw.fablab_dev_truststore); - - //the password used here is just a dummy as it is needed by the keystore.load method - String trustStorePass = "dummypass4dev"; - keyStore.load(inputStream, trustStorePass.toCharArray()); - - SSLContext sslContext = SSLContext.getInstance("TLS"); - TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( - TrustManagerFactory.getDefaultAlgorithm()); - trustManagerFactory.init(keyStore); - sslContext.init(null, trustManagerFactory.getTrustManagers(), null); myUrl = new URL(urls[0]); urlConnection = (HttpsURLConnection) myUrl.openConnection(); - urlConnection.setSSLSocketFactory(sslContext.getSocketFactory()); - + urlConnection.setSSLSocketFactory(getPinnedCertSslSocketFactory()); bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); - return bufferedReader; - } - catch (Exception e) + + String htmlText = ""; + String line = ""; + while ((line = bufferedReader.readLine()) != null) + htmlText += line; + + return htmlText; + + } catch (Exception e) { e.printStackTrace(); } - finally - { - return null; - } + return null; + } @Override - protected void onPostExecute(BufferedReader bufferedReader) { - mListener.callback(bufferedReader); + protected void onPostExecute(String htmlText) + { + mListener.callback(htmlText); } } + /** + * Creates and returns a SSLSocketFactory which will trust our selfsigned certificates in + * fablab_dev_truststore. The truststore only contains the public certs. + * + * @return a SSLSocketFactory trusting our selfsigned certs. + */ + private SSLSocketFactory getPinnedCertSslSocketFactory() + { + try + { + //Default type is BKS on android + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + //our truststore containing the public certs + InputStream inputStream = getResources().openRawResource(R.raw.fablab_dev_truststore); + + //the password used here is just a dummy as it is needed by the keystore.load method + String trustStorePass = "dummypass4dev"; + keyStore.load(inputStream, trustStorePass.toCharArray()); + + SSLContext sslContext = SSLContext.getInstance("TLS"); + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( + TrustManagerFactory.getDefaultAlgorithm()); + trustManagerFactory.init(keyStore); + sslContext.init(null, trustManagerFactory.getTrustManagers(), null); + + return sslContext.getSocketFactory(); + + } catch (Exception e) + { + Log.e(LOG_TAG, e.getMessage()); + } + return null; + } } + interface CallBackListener { - public void callback(BufferedReader bufferedReader); + void callback(String htmlText); }