summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorParménides GV <parmegv@sdf.org>2014-12-09 15:58:09 +0100
committerParménides GV <parmegv@sdf.org>2014-12-09 15:58:09 +0100
commit7bbb5b69717393d87869ae6f16d6aff0578a81bf (patch)
tree20cd58c73ff92f0d0a8b75e214ab2a7ccf88fbcf
parent5978db9bf758b7b229616d0575e327c58ad8cf94 (diff)
Save custom providers to sdcard.
-rw-r--r--app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java14
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Provider.java19
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java5
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/ProviderManager.java98
-rw-r--r--app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java20
5 files changed, 104 insertions, 52 deletions
diff --git a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java
index 9af31fd8..2e3fb8f1 100644
--- a/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java
+++ b/app/src/debug/java/se/leap/bitmaskclient/ConfigurationWizard.java
@@ -108,7 +108,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
super.onCreate(savedInstanceState);
preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE);
fragment_manager = new FragmentManagerEnhanced(getFragmentManager());
- provider_manager = new ProviderManager(getAssets());
+ provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null));
setUpInitialUI();
@@ -158,15 +158,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
private void setUpProviderList() {
initProviderList();
- // provider_list_fragment = ProviderListFragment.newInstance();
-
- // Bundle arguments = new Bundle();
- // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1);
- // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER)
- // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true);
-
- // provider_list_fragment.setArguments(arguments);
-
}
@Override
@@ -390,7 +381,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
public void showAndSelectProvider(String provider_main_url, boolean danger_on) {
try {
selected_provider = new Provider(new URL((provider_main_url)));
- provider_manager.add(selected_provider);
+ adapter.add(selected_provider);
+ adapter.saveProviders();
autoSelectProvider(selected_provider, danger_on);
} catch (MalformedURLException e) {
e.printStackTrace();
diff --git a/app/src/main/java/se/leap/bitmaskclient/Provider.java b/app/src/main/java/se/leap/bitmaskclient/Provider.java
index 0b021b84..a7ede2d1 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Provider.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Provider.java
@@ -26,6 +26,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import java.io.File;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
@@ -40,7 +41,6 @@ public final class Provider implements Parcelable {
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",
@@ -72,6 +72,9 @@ public final class Provider implements Parcelable {
this.main_url = main_url;
}
+ public Provider(File provider_file) {
+
+ }
public static final Parcelable.Creator<Provider> CREATOR
= new Parcelable.Creator<Provider>() {
public Provider createFromParcel(Parcel in) {
@@ -168,10 +171,6 @@ public final class Provider implements Parcelable {
return false;
}
- protected boolean isCustom() {
- return is_custom;
- }
-
@Override
public int describeContents() {
return 0;
@@ -191,4 +190,14 @@ public final class Provider implements Parcelable {
return p.mainUrl().equals(mainUrl());
} else return false;
}
+
+ public JSONObject toJson() {
+ JSONObject json = new JSONObject();
+ try {
+ json.put(Provider.MAIN_URL, main_url);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ return json;
+ }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java
index 8360954b..72ca15c7 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java
+++ b/app/src/main/java/se/leap/bitmaskclient/ProviderListAdapter.java
@@ -120,4 +120,9 @@ public class ProviderListAdapter extends RendererAdapter<Provider> {
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(getRealPosition(position), convertView, parent);
}
+
+ public void saveProviders() {
+ ProviderManager provider_manager = (ProviderManager) getCollection();
+ provider_manager.saveCustomProvidersToFile();
+ }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
index 59507d9c..911144f7 100644
--- a/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/ProviderManager.java
@@ -7,6 +7,11 @@ import com.pedrogomez.renderers.AdapteeCollection;
import org.json.JSONException;
import org.json.JSONObject;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@@ -22,51 +27,89 @@ import java.util.Set;
public class ProviderManager implements AdapteeCollection<Provider> {
private AssetManager assets_manager;
+ private File external_files_dir;
private Set<Provider> default_providers;
private Set<Provider> custom_providers;
+ private static ProviderManager instance;
+
final protected static String URLS = "urls";
+ public static ProviderManager getInstance(AssetManager assets_manager, File external_files_dir) {
+ if(instance == null)
+ instance = new ProviderManager(assets_manager);
+
+ instance.addCustomProviders(external_files_dir);
+ return instance;
+ }
+
public ProviderManager(AssetManager assets_manager) {
this.assets_manager = assets_manager;
- default_providers = default_providers();
- custom_providers = new HashSet<Provider>();
+ addDefaultProviders(assets_manager);
}
- /**
- * Loads providers data from url files contained in the assets folder
- * @return true if the files were correctly read
- */
- private Set<Provider> default_providers() {
+ private void addDefaultProviders(AssetManager assets_manager) {
+ try {
+ default_providers = providersFromAssets(URLS, assets_manager.list(URLS));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private Set<Provider> providersFromAssets(String directory, String[] relative_file_paths) {
Set<Provider> providers = new HashSet<Provider>();
try {
- for(String file : assets_manager.list(URLS)) {
- String main_url = extractProviderMainUrlFromAssetsFile(URLS + "/" + file);
+ for(String file : relative_file_paths) {
+ String main_url = extractMainUrlFromInputStream(assets_manager.open(directory + "/" + file));
providers.add(new Provider(new URL(main_url)));
- }
+ }
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
+ return providers;
+ }
+
+
+ private void addCustomProviders(File external_files_dir) {
+ this.external_files_dir = external_files_dir;
+ custom_providers = external_files_dir.isDirectory() ?
+ providersFromFiles(external_files_dir.list()) :
+ new HashSet<Provider>();
+ }
+
+ private Set<Provider> providersFromFiles(String[] files) {
+ Set<Provider> providers = new HashSet<Provider>();
+ try {
+ for(String file : files) {
+ String main_url = extractMainUrlFromInputStream(new FileInputStream(external_files_dir.getAbsolutePath() + "/" + file));
+ providers.add(new Provider(new URL(main_url)));
+ }
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
return providers;
}
- private String extractProviderMainUrlFromAssetsFile(String file_path) {
- String provider_main_url = "";
+ private String extractMainUrlFromInputStream(InputStream input_stream_file_contents) {
+ String main_url = "";
+ byte[] bytes = new byte[0];
try {
- InputStream input_stream_file_contents = assets_manager.open(file_path);
- byte[] urls_file_bytes = new byte[input_stream_file_contents.available()];
- if(input_stream_file_contents.read(urls_file_bytes) > 0) {
- String urls_file_content = new String(urls_file_bytes);
- JSONObject file_contents = new JSONObject(urls_file_content);
- provider_main_url = file_contents.getString(Provider.MAIN_URL);
+ bytes = new byte[input_stream_file_contents.available()];
+ if(input_stream_file_contents.read(bytes) > 0) {
+ JSONObject file_contents = new JSONObject(new String(bytes));
+ main_url = file_contents.getString(Provider.MAIN_URL);
}
- } catch (JSONException e) {
} catch (IOException e) {
+ e.printStackTrace();
+ } catch (JSONException e) {
+ e.printStackTrace();
}
- return provider_main_url;
+ return main_url;
}
public Set<Provider> providers() {
@@ -117,4 +160,19 @@ public class ProviderManager implements AdapteeCollection<Provider> {
default_providers.clear();
custom_providers.clear();
}
+
+ protected void saveCustomProvidersToFile() {
+ try {
+ for (Provider provider : custom_providers) {
+ File provider_file = new File(external_files_dir, provider.getName() + ".json");
+ if(!provider_file.exists()) {
+ FileWriter writer = new FileWriter(provider_file);
+ writer.write(provider.toJson().toString());
+ writer.close();
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
diff --git a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
index ef7986f1..42736444 100644
--- a/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
+++ b/app/src/release/java/se/leap/bitmaskclient/ConfigurationWizard.java
@@ -24,12 +24,11 @@ import android.widget.*;
import com.pedrogomez.renderers.*;
-import java.io.*;
import java.net.*;
import java.util.*;
import butterknife.*;
-import org.jetbrains.annotations.NotNull;
+
import org.json.*;
import javax.inject.Inject;
@@ -38,8 +37,6 @@ import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface;
import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface;
import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver;
import se.leap.bitmaskclient.ProviderDetailFragment.ProviderDetailFragmentInterface;
-import se.leap.bitmaskclient.ProviderListContent.ProviderItem;
-import se.leap.bitmaskclient.FragmentManagerEnhanced;
import se.leap.bitmaskclient.eip.Constants;
/**
@@ -112,7 +109,7 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
super.onCreate(savedInstanceState);
preferences = getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE);
fragment_manager = new FragmentManagerEnhanced(getFragmentManager());
- provider_manager = new ProviderManager(getAssets());
+ provider_manager = ProviderManager.getInstance(getAssets(), getExternalFilesDir(null));
setUpInitialUI();
@@ -163,16 +160,6 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
private void setUpProviderList() {
initProviderList();
- // provider_list_fragment = ProviderListFragment.newInstance();
-
- // Bundle arguments = new Bundle();
- // int configuration_wizard_request_code = getIntent().getIntExtra(Dashboard.REQUEST_CODE, -1);
- // if(configuration_wizard_request_code == Dashboard.SWITCH_PROVIDER)
- // arguments.putBoolean(ProviderListFragment.SHOW_ALL_PROVIDERS, true);
-
- // provider_list_fragment.setArguments(arguments);
-
- // putProviderListFragment();
}
private void putProviderListFragment() {
@@ -397,7 +384,8 @@ implements NewProviderDialogInterface, ProviderDetailFragmentInterface, Download
public void showAndSelectProvider(String provider_main_url) {
try {
selected_provider = new Provider(new URL((provider_main_url)));
- provider_manager.add(selected_provider);
+ adapter.add(selected_provider);
+ adapter.saveProviders();
autoSelectProvider(selected_provider);
} catch (MalformedURLException e) {
e.printStackTrace();