Skip to content

Commit

Permalink
enable alert in productmap
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsandner committed Sep 2, 2015
1 parent bc97994 commit a6e16aa
Showing 1 changed file with 85 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -28,14 +35,16 @@

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;

@Bind(R.id.location_webView)
WebView webview;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);


Expand All @@ -46,120 +55,82 @@ 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);
webview.setWebViewClient(new WebViewClient()
{

@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);
}
});

}

@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<String, Integer , BufferedReader>
private class ConnectionTask extends AsyncTask<String, Integer, String>
{
private CallBackListener mListener;

Expand All @@ -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);
}

0 comments on commit a6e16aa

Please sign in to comment.