From 5ad4fada2452645fcd60f2ff1b8dac7fddeaadc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 10 Oct 2013 21:12:32 +0200 Subject: Simplified the methods to add a new provider. Adding a new provider is the same that selecting a new one, using the same methods and following the same workflow. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 72 +++++++--------------- src/se/leap/bitmaskclient/NewProviderDialog.java | 4 +- src/se/leap/bitmaskclient/ProviderAPI.java | 64 ++----------------- src/se/leap/bitmaskclient/ProviderListContent.java | 18 +++--- .../leap/bitmaskclient/ProviderListFragment.java | 3 + 5 files changed, 42 insertions(+), 119 deletions(-) (limited to 'src') diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 016d9c52..cb1d01db 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -236,7 +236,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn int provider_index = getProviderIndex(id); startProgressBar(provider_index); mSelectedProvider = selected_provider; - updateProviderDotJson(mSelectedProvider.name(), mSelectedProvider.providerJsonUrl(), mSelectedProvider.completelyTrusted()); + updateProviderDotJson(mSelectedProvider.providerMainUrl(), mSelectedProvider.completelyTrusted()); } @Override @@ -341,37 +341,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn return loaded_preseeded_providers; } - /** - * Saves provider.json file associated with provider. - * - * If the provider is custom, the file has already been downloaded so we load it from memory. - * If not, the file is updated using the provider's URL. - * @param provider - */ - private void saveProviderJson(ProviderItem provider) { - JSONObject provider_json = new JSONObject(); - try { - if(!provider.custom()) { - updateProviderDotJson(mSelectedProvider.name(), provider.providerJsonUrl(), provider.completelyTrusted()); - } else { - // FIXME!! We should we be updating our seeded providers list at ConfigurationWizard onStart() ? - // I think yes, but if so, where does this list live? leap.se, as it's the non-profit project for the software? - // If not, we should just be getting names/urls, and fetching the provider.json like in custom entries - provider_json = ConfigHelper.getJsonFromSharedPref(Provider.KEY); - ConfigHelper.saveSharedPref(Provider.KEY, provider_json); - ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); - ConfigHelper.saveSharedPref(ProviderItem.DANGER_ON, provider.completelyTrusted()); - - mProgressBar.incrementProgressBy(1); - - downloadJSONFiles(provider_json, provider.completelyTrusted()); - } - } catch (JSONException e) { - setResult(RESULT_CANCELED); - finish(); - } - } - /** * Asks ProviderAPI to download provider site's certificate and eip-service.json * @@ -469,33 +438,38 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn newFragment.show(fragment_transaction, ProviderDetailFragment.TAG); } - @Override - public void saveAndSelectProvider(String provider_main_url, boolean danger_on) { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - - Bundle parameters = new Bundle(); - parameters.putString(Provider.MAIN_URL, provider_main_url); - parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); - - provider_API_command.setAction(ProviderAPI.DOWNLOAD_NEW_PROVIDER_DOTJSON); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - - startService(provider_API_command); + public void showAndSelectProvider(String provider_main_url, boolean danger_on) { + showProvider(provider_main_url, danger_on); + updateProviderDotJson(provider_main_url, danger_on); + } + + private void showProvider(final String provider_main_url, final boolean danger_on) { + String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); + boolean custom = true; + final ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url, custom, danger_on); + runOnUiThread(new Runnable() { + + @Override + public void run() { + // TODO Auto-generated method stub + ProviderListContent.addItem(added_provider); + ProviderListFragment provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag("providerlist"); + provider_list_fragment.notifyAdapter(); + } + }); } /** * Asks ProviderAPI to download a new provider.json file * @param provider_name - * @param provider_json_url + * @param provider_main_url * @param danger_on tells if HTTPS client should bypass certificate errors */ - public void updateProviderDotJson(String provider_name, String provider_json_url, boolean danger_on) { + public void updateProviderDotJson(String provider_main_url, boolean danger_on) { Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle parameters = new Bundle(); - parameters.putString(Provider.NAME, provider_name); - parameters.putString(Provider.DOT_JSON_URL, provider_json_url); + parameters.putString(Provider.MAIN_URL, provider_main_url); parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); provider_API_command.setAction(ProviderAPI.UPDATE_PROVIDER_DOTJSON); diff --git a/src/se/leap/bitmaskclient/NewProviderDialog.java b/src/se/leap/bitmaskclient/NewProviderDialog.java index f77cb6d4..16d46c67 100644 --- a/src/se/leap/bitmaskclient/NewProviderDialog.java +++ b/src/se/leap/bitmaskclient/NewProviderDialog.java @@ -40,7 +40,7 @@ public class NewProviderDialog extends DialogFragment { final public static String TAG = "newProviderDialog"; public interface NewProviderDialogInterface { - public void saveAndSelectProvider(String url_provider, boolean danger_on); + public void showAndSelectProvider(String url_provider, boolean danger_on); } NewProviderDialogInterface interface_with_ConfigurationWizard; @@ -82,7 +82,7 @@ public class NewProviderDialog extends DialogFragment { } boolean danger_on = danger_checkbox.isChecked(); if(validURL(entered_url)) { - interface_with_ConfigurationWizard.saveAndSelectProvider(entered_url, danger_on); + interface_with_ConfigurationWizard.showAndSelectProvider(entered_url, danger_on); Toast.makeText(getActivity().getApplicationContext(), R.string.valid_url_entered, Toast.LENGTH_LONG).show(); } else { url_input_field.setText(""); diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index a553851b..c9da5c63 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -156,13 +156,6 @@ public class ProviderAPI extends IntentService { } else { receiver.send(INCORRECTLY_UPDATED_PROVIDER_DOT_JSON, Bundle.EMPTY); } - } else if (action.equalsIgnoreCase(DOWNLOAD_NEW_PROVIDER_DOTJSON)) { - Bundle result = downloadNewProviderDotJSON(parameters); - if(result.getBoolean(RESULT_KEY)) { - receiver.send(CORRECTLY_UPDATED_PROVIDER_DOT_JSON, result); - } else { - receiver.send(INCORRECTLY_DOWNLOADED_JSON_FILES, result); - } } else if (action.equalsIgnoreCase(SRP_AUTH)) { Bundle session_id_bundle = authenticateBySRP(parameters); if(session_id_bundle.getBoolean(RESULT_KEY)) { @@ -491,10 +484,8 @@ public class ProviderAPI extends IntentService { */ private Bundle updateProviderDotJSON(Bundle task) { Bundle result = new Bundle(); - boolean custom = task.getBoolean(ProviderItem.CUSTOM); boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); - String provider_json_url = task.getString(Provider.DOT_JSON_URL); - String provider_name = task.getString(Provider.NAME); + String provider_json_url = task.getString(Provider.MAIN_URL) + "/provider.json"; try { String provider_dot_json_string = downloadWithCommercialCA(provider_json_url, danger_on); @@ -509,56 +500,9 @@ public class ProviderAPI extends IntentService { } else { ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); - //ProviderListContent.addItem(new ProviderItem(provider_name, provider_json_url, provider_json, custom, danger_on)); - result.putBoolean(RESULT_KEY, true); - result.putString(Provider.KEY, provider_json.toString()); - result.putBoolean(ProviderItem.DANGER_ON, danger_on); - } - } - } catch (JSONException e) { - result.putBoolean(RESULT_KEY, false); - } - - return result; - } - - /** - * Downloads a custom provider provider.json file - * @param task containing a boolean meaning if the user completely trusts this provider, and the provider main url entered in the new custom provider dialog. - * @return true if provider.json file was successfully parsed as a JSON object. - */ - private Bundle downloadNewProviderDotJSON(Bundle task) { - Bundle result = new Bundle(); - boolean custom = true; - boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); - - String provider_main_url = (String) task.get(Provider.MAIN_URL); - String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); - String provider_json_url = guessProviderDotJsonURL(provider_main_url); - - String provider_json_string = downloadWithCommercialCA(provider_json_url, danger_on); - try { - if(provider_json_string.isEmpty()) { - result.putBoolean(RESULT_KEY, false); - } else { - JSONObject provider_json = new JSONObject(provider_json_string); - - if(provider_json.has(ERRORS)) { - String reason_to_fail = provider_json.getString(ERRORS); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } else { - ConfigHelper.saveSharedPref(Provider.KEY, provider_json); - ConfigHelper.saveSharedPref(ProviderItem.DANGER_ON, danger_on); - ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); - ProviderItem added_provider = new ProviderItem(provider_name, provider_json_url, provider_json, custom, danger_on); - ProviderListContent.addItem(added_provider); - - result.putString(Provider.NAME, added_provider.getName()); - result.putBoolean(RESULT_KEY, true); - result.putString(Provider.KEY, provider_json.toString()); - result.putBoolean(ProviderItem.DANGER_ON, danger_on); - } + result.putBoolean(RESULT_KEY, true); + result.putString(Provider.KEY, provider_json.toString()); + result.putBoolean(ProviderItem.DANGER_ON, danger_on); } } catch (JSONException e) { result.putBoolean(RESULT_KEY, false); diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index a6da5620..3695066c 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -28,6 +28,8 @@ import java.net.MalformedURLException; import org.json.JSONException; import org.json.JSONObject; +import android.app.Activity; + /** * Models the provider list shown in the ConfigurationWizard. * @@ -60,7 +62,7 @@ public class ProviderListContent { final public static String CUSTOM = "custom"; final public static String DANGER_ON = "danger_on"; private boolean custom = false; - private String provider_json_url; + private String provider_main_url; private String name; private boolean danger_on = false; @@ -77,7 +79,7 @@ public class ProviderListContent { urls_file_input_stream.read(urls_file_bytes); String urls_file_content = new String(urls_file_bytes); JSONObject file_contents = new JSONObject(urls_file_content); - provider_json_url = file_contents.getString(Provider.DOT_JSON_URL); + provider_main_url = file_contents.getString(Provider.MAIN_URL); this.name = name; this.custom = custom; this.danger_on = danger_on; @@ -92,14 +94,14 @@ public class ProviderListContent { /** * @param name of the provider - * @param provider_json_url used to download provider.json file of the provider + * @param provider_main_url used to download provider.json file of the provider * @param provider_json already downloaded * @param custom if it's a new provider entered by the user or not * @param danger_on if the user trusts completely the new provider */ - public ProviderItem(String name, String provider_json_url, boolean custom, boolean danger_on) { + public ProviderItem(String name, String provider_main_url, boolean custom, boolean danger_on) { this.name = name; - this.provider_json_url = provider_json_url; + this.provider_main_url = provider_main_url; this.custom = custom; this.danger_on = danger_on; } @@ -108,13 +110,13 @@ public class ProviderListContent { public boolean custom() { return custom; } - public String providerJsonUrl() { return provider_json_url; } + public String providerMainUrl() { return provider_main_url; } public String domain() { try { - return new URL(provider_json_url).getHost(); + return new URL(provider_main_url).getHost(); } catch (MalformedURLException e) { - return provider_json_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); + return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); } } diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index ca66d295..62dcef7a 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -172,6 +172,9 @@ public class ProviderListFragment extends ListFragment { } } + public void notifyAdapter() { + content_adapter.notifyDataSetChanged(); + } /** * Turns on activate-on-click mode. When this mode is on, list items will be * given the 'activated' state when touched. -- cgit v1.2.3