From 97e77486027cc47fcf1ff86506102917447f1a8c Mon Sep 17 00:00:00 2001 From: santimunin Date: Thu, 21 Jun 2012 22:11:22 +0000 Subject: [PATCH] 2012-06-21 Santiago Munin * src/pandroid_event_viewer/pandorafms/Core.java: Added a new method which checks if a website is online. * src/pandroid_event_viewer/pandorafms/Options.java: Fixed a bug. The app didn't check if the url were online before checking the certificate. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6673 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- extras/pandroid_event_viewer/ChangeLog | 4 ++ .../pandorafms/Core.java | 28 +++++++-- .../pandorafms/Options.java | 59 +++++++++++-------- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/extras/pandroid_event_viewer/ChangeLog b/extras/pandroid_event_viewer/ChangeLog index d101c72812..ed694ae653 100644 --- a/extras/pandroid_event_viewer/ChangeLog +++ b/extras/pandroid_event_viewer/ChangeLog @@ -1,3 +1,7 @@ +2012-06-21 Santiago Munín + * src/pandroid_event_viewer/pandorafms/Core.java: Added a new method which checks if a website is online. + * src/pandroid_event_viewer/pandorafms/Options.java: Fixed a bug. The app didn't check if the url were online before checking the certificate. + 2012-06-21 Santiago Munín * src/pandroid_event_viewer/pandorafms/EventList.java: Restored to the previous version. * src/pandroid_event_viewer/pandorafms/Core.java: Added a line which checks if the connection is secure. diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java index 70ed74a795..85ac708501 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java @@ -386,7 +386,7 @@ public class Core { return img; } } catch (IOException e) { - Log.e(TAG, "Downloading image "+fileUrl+": error"); + Log.e(TAG, "Downloading image " + fileUrl + ": error"); } return null; } @@ -437,13 +437,15 @@ public class Core { * Finds out if the given url has a CA signed certificate. * * @param url - * @return + * @return boolean + * @throws IOException + * If the given url is not accessible. */ public static boolean isValidCertificate(URL url) { - HttpsURLConnection con; try { con = (HttpsURLConnection) url.openConnection(); + con.getResponseCode(); con.connect(); con.disconnect(); return true; @@ -452,6 +454,24 @@ public class Core { } } + public static boolean isOnline(URL url) { + HttpsURLConnection con; + try { + con = (HttpsURLConnection) url.openConnection(); + con.setHostnameVerifier(new HostnameVerifier() { + public boolean verify(String hostname, SSLSession session) { + return true; + } + }); + con.setSSLSocketFactory(getSocketFactory()); + if (con.getResponseCode()!=400) { + return false; + } else return true; + } catch (IOException e) { + return true; + } + } + public static String httpsGet(String url, List additionalParameters) { String result = ""; @@ -487,7 +507,7 @@ public class Core { String temp; while ((temp = bufferedReader.readLine()) != null) { Log.d("CONTENT", temp); - result += temp+"\n"; + result += temp + "\n"; } } catch (IOException e) { return ""; diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java index 931160f878..917cdff565 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java @@ -315,41 +315,48 @@ public class Options extends Activity { private class CheckCertificateAsyncTask extends AsyncTask { - + private URL url; @Override protected Boolean doInBackground(URL... arg0) { + url = arg0[0]; return Core.isValidCertificate(arg0[0]); + } @Override protected void onPostExecute(Boolean result) { retrievingCertificate.dismiss(); - if (!result) { - DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - switch (which) { - case DialogInterface.BUTTON_NEGATIVE: - Toast.makeText(getApplicationContext(), - R.string.options_not_saved, - Toast.LENGTH_SHORT).show(); - return; - case DialogInterface.BUTTON_POSITIVE: - writeChanges(); - return; - } - } - }; - - AlertDialog.Builder builder = new AlertDialog.Builder( - context); - builder.setMessage(getString(R.string.certificate_not_valid)) - .setPositiveButton(getString(android.R.string.yes), - dialogClickListener) - .setNegativeButton(getString(android.R.string.no), - dialogClickListener).show(); - } else { + if (!Core.isOnline(url)) { writeChanges(); + } else { + if (!result) { + DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_NEGATIVE: + Toast.makeText(getApplicationContext(), + R.string.options_not_saved, + Toast.LENGTH_SHORT).show(); + return; + case DialogInterface.BUTTON_POSITIVE: + writeChanges(); + return; + } + } + }; + + AlertDialog.Builder builder = new AlertDialog.Builder( + context); + builder.setMessage( + getString(R.string.certificate_not_valid)) + .setPositiveButton(getString(android.R.string.yes), + dialogClickListener) + .setNegativeButton(getString(android.R.string.no), + dialogClickListener).show(); + } else { + writeChanges(); + } } } }