From f9a200a7004b74cb2d64e37129e6234427c84c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 4 Dec 2014 16:38:21 +0100 Subject: Renderers and dagger, refactoring CW. --- .../main/java/se/leap/bitmaskclient/Provider.java | 116 ++++++++++----------- 1 file changed, 56 insertions(+), 60 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/Provider.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java index bb30905c..5dd54175 100644 --- a/app/src/main/java/se/leap/bitmaskclient/Provider.java +++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java @@ -19,12 +19,16 @@ package se.leap.bitmaskclient; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; +import android.os.Parcel; +import android.os.Parcelable; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.Serializable; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Arrays; import java.util.Locale; @@ -32,16 +36,11 @@ import java.util.Locale; * @author Sean Leonard * */ -public final class Provider implements Serializable { +public final class Provider implements Parcelable { - private static final long serialVersionUID = 6003835972151761353L; - - private static Provider instance = null; - - // We'll access our preferences here - private static SharedPreferences preferences = null; - // Represents our Provider's provider.json - private static JSONObject definition = null; + private JSONObject definition; // Represents our Provider's provider.json + private URL main_url; + private boolean is_custom = false; final public static String API_URL = "api_uri", @@ -69,54 +68,39 @@ public final class Provider implements Serializable { private static final String API_TERM_DEFAULT_LANGUAGE = "default_language"; protected static final String[] API_EIP_TYPES = {"openvpn"}; - private static final String PREFS_EIP_NAME = null; + public Provider(URL main_url) { + this.main_url = main_url; + } + public static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + public Provider createFromParcel(Parcel in) { + return new Provider(in); + } - - // What, no individual fields?! We're going to gamble on org.json.JSONObject and JSONArray - // Supporting multiple API versions will probably break this paradigm, - // Forcing me to write a real constructor and rewrite getters/setters - // Also will refactor if i'm instantiating the same local variables all the time - - /** - * - */ - private Provider() {} - - protected static Provider getInstance(){ - if(instance==null){ - instance = new Provider(); - } - return instance; - } + public Provider[] newArray(int size) { + return new Provider[size]; + } + }; - protected void init(Activity activity) { - - // Load our preferences from SharedPreferences - // If there's nothing there, we will end up returning a rather empty object - // to whoever called getInstance() and they can run the First Run Wizard - //preferences = context.getgetPreferences(0); // 0 == MODE_PRIVATE, but we don't extend Android's classes... - - // Load SharedPreferences - preferences = activity.getSharedPreferences(Dashboard.SHARED_PREFERENCES,Context.MODE_PRIVATE); - // Inflate our provider.json data - try { - definition = new JSONObject( preferences.getString(Provider.KEY, "") ); - } catch (JSONException e) { - // TODO: handle exception - - // FIXME!! We want "real" data!! - } - } + private Provider(Parcel in) { + try { + main_url = new URL(in.readString()); + definition = new JSONObject((in.readString())); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + + protected void define(JSONObject provider_json) { + definition = provider_json; + } protected String getDomain(){ - String domain = ""; - try { - domain = definition.getString(API_TERM_DOMAIN); - } catch (JSONException e) { - e.printStackTrace(); - } - return domain; + return main_url.getHost(); } protected String getName(){ @@ -124,15 +108,12 @@ public final class Provider implements Serializable { String lang = Locale.getDefault().getLanguage(); String name = "Null"; // Should it actually /be/ null, for error conditions? try { - name = definition.getJSONObject(API_TERM_NAME).getString(lang); + if(definition != null) + name = definition.getJSONObject(API_TERM_NAME).getString(lang); + else throw new JSONException("Provider not defined"); } catch (JSONException e) { - // TODO: Nesting try/catch blocks? Crazy - // Maybe you should actually handle exception? - try { - name = definition.getJSONObject(API_TERM_NAME).getString( definition.getString(API_TERM_DEFAULT_LANGUAGE) ); - } catch (JSONException e2) { - // TODO: Will you handle the exception already? - } + String host = main_url.getHost(); + name = host.substring(0, host.indexOf(".")); } return name; @@ -177,4 +158,19 @@ public final class Provider implements Serializable { } return false; } + + protected boolean isCustom() { + return is_custom; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(main_url.toString()); + parcel.writeString(definition.toString()); + } } -- cgit v1.2.3