diff options
author | Parménides GV <parmegv@sdf.org> | 2013-02-04 16:35:28 +0100 |
---|---|---|
committer | Parménides GV <parmegv@sdf.org> | 2013-02-04 16:35:28 +0100 |
commit | d2bd18ef560d95974117604af899b3a9fcc16dab (patch) | |
tree | 96831726c943bcccb104584d4748c5972a43654e /src/se/leap/leapclient | |
parent | 7b9e22e765a1e5eda05ad121684e63c20ff5f049 (diff) |
Created ConfigHelper with static information such as sharedprefs keys,
the saveSharedPrefs method and an unimplemented rescueFromJSONException.
Next step: managing HttpsURLConnection for the
CertPathValidatorException.
Diffstat (limited to 'src/se/leap/leapclient')
-rw-r--r-- | src/se/leap/leapclient/ConfigHelper.java | 32 | ||||
-rw-r--r-- | src/se/leap/leapclient/Dashboard.java | 3 | ||||
-rw-r--r-- | src/se/leap/leapclient/ProviderAPI.java | 55 | ||||
-rw-r--r-- | src/se/leap/leapclient/ProviderListActivity.java | 3 |
4 files changed, 64 insertions, 29 deletions
diff --git a/src/se/leap/leapclient/ConfigHelper.java b/src/se/leap/leapclient/ConfigHelper.java new file mode 100644 index 00000000..174ff79f --- /dev/null +++ b/src/se/leap/leapclient/ConfigHelper.java @@ -0,0 +1,32 @@ +package se.leap.leapclient; + +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.SharedPreferences; + +public class ConfigHelper { + + final static String downloadJsonFilesBundleExtra = "downloadJSONFiles"; + final static String provider_key = "provider"; + final static String eip_service_key = "eip"; + + static void saveSharedPref(String shared_preferences_key, + JSONObject content) { + + SharedPreferences.Editor shared_preferences_editor = ProviderListActivity.shared_preferences + .edit(); + shared_preferences_editor.putString(shared_preferences_key, + content.toString()); + shared_preferences_editor.commit(); + System.out.println("Shared preferences updated: " + + ProviderListActivity.shared_preferences.getString( + shared_preferences_key, "Default")); + + } + + static void rescueJSONException(JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } +} diff --git a/src/se/leap/leapclient/Dashboard.java b/src/se/leap/leapclient/Dashboard.java index f81c37a7..84ddab27 100644 --- a/src/se/leap/leapclient/Dashboard.java +++ b/src/se/leap/leapclient/Dashboard.java @@ -37,8 +37,7 @@ public class Dashboard extends Activity { preferences = getPreferences(MODE_PRIVATE); // FIXME provider data!! get parmegv's work so we can stop (or lessen) faking it - //TODO In my emulator, I always get that contains TRUE. Don't know why, but I've been testing without the "!". - if (!preferences.contains("provider") ) + if (preferences.contains("provider") ) startActivity(new Intent(this, ProviderListActivity.class)); // Get our provider diff --git a/src/se/leap/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java index aefb87fc..40638180 100644 --- a/src/se/leap/leapclient/ProviderAPI.java +++ b/src/se/leap/leapclient/ProviderAPI.java @@ -5,11 +5,16 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; import java.util.Scanner; +import javax.net.ssl.HttpsURLConnection; + +import org.json.JSONException; +import org.json.JSONObject; + import android.app.IntentService; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; @@ -23,45 +28,45 @@ public class ProviderAPI extends IntentService { @Override protected void onHandleIntent(Intent task_for) { - Bundle task ; + Bundle task; System.out.println("onHandleIntent called"); - if(!(task = task_for.getBundleExtra("downloadJSONFiles")).isEmpty()) - { - String provider_key = "provider"; - String eip_service_key = "eip"; - String provider_json_url = (String) task.get(provider_key); - String eip_service_json_url = (String) task.get(eip_service_key); + if (!(task = task_for.getBundleExtra(ConfigHelper.downloadJsonFilesBundleExtra)).isEmpty()) { + String provider_json_url = (String) task.get(ConfigHelper.provider_key); + String eip_service_json_url = (String) task.get(ConfigHelper.eip_service_key); try { - getAndParseSharedPref(provider_key, provider_json_url); - getAndParseSharedPref(eip_service_key, eip_service_json_url); + JSONObject provider_json = getFromProvider(provider_json_url); + ConfigHelper.saveSharedPref(ConfigHelper.provider_key, provider_json); + JSONObject eip_service_json = getFromProvider(eip_service_json_url); + ConfigHelper.saveSharedPref(ConfigHelper.eip_service_key, eip_service_json); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); + } catch (JSONException e) { + ConfigHelper.rescueJSONException(e); } } } - private void getAndParseSharedPref(String shared_preferences_key, - String json_url) throws IOException { + private JSONObject getFromProvider(String json_url) throws IOException, JSONException { URL url = new URL(json_url); - HttpURLConnection urlConnection = (HttpURLConnection) url - .openConnection(); + String json_file_content = ""; + URLConnection urlConnection = null; + + if (url.getProtocol().equalsIgnoreCase("https")) { + urlConnection = (HttpsURLConnection) url.openConnection(); + } else if (url.getProtocol().equalsIgnoreCase("http")) { + urlConnection = (HttpURLConnection) url.openConnection(); + } + try { InputStream in = new BufferedInputStream( urlConnection.getInputStream()); - String json_file_content = new Scanner(in).useDelimiter("\\A") - .next(); - - SharedPreferences.Editor shared_preferences_editor = ProviderListActivity.shared_preferences - .edit(); - shared_preferences_editor.putString(shared_preferences_key, - json_file_content); - shared_preferences_editor.commit(); - System.out.println("Shared preferences updated: " + ProviderListActivity.shared_preferences.getString(shared_preferences_key, "Default")); + json_file_content = new Scanner(in).useDelimiter("\\A").next(); } finally { - urlConnection.disconnect(); + ((HttpURLConnection) urlConnection).disconnect(); } - + + return new JSONObject(json_file_content); } } diff --git a/src/se/leap/leapclient/ProviderListActivity.java b/src/se/leap/leapclient/ProviderListActivity.java index 9e5796da..088b464d 100644 --- a/src/se/leap/leapclient/ProviderListActivity.java +++ b/src/se/leap/leapclient/ProviderListActivity.java @@ -120,11 +120,10 @@ public class ProviderListActivity extends FragmentActivity Intent provider_API_command = new Intent(this, ProviderAPI.class);
Bundle method_and_parameters = new Bundle();
- method_and_parameters.putString("method", "getAndParseSharedPref");
method_and_parameters.putString("provider", current_provider_item.provider_json_url);
method_and_parameters.putString("eip", current_provider_item.eip_service_json_url);
- provider_API_command.putExtra("downloadJSONFiles", method_and_parameters);
+ provider_API_command.putExtra(ConfigHelper.downloadJsonFilesBundleExtra, method_and_parameters);
startService(provider_API_command);
|