From 576bb02d7a087a32863d310363968d304b3af52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 6 Sep 2013 12:54:24 +0200 Subject: Removing unnecessary fields from ProviderItem. I'm targeting to refactor the whole ProviderItem class because I've not used the Provider class so far and I should not duplicate information from "Provider" into "ProviderItem". --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 27 ++++---- src/se/leap/bitmaskclient/ProviderListContent.java | 77 ++++++---------------- 2 files changed, 35 insertions(+), 69 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 4a3bb12b..532debfd 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -175,13 +175,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn //Toast.makeText(this, getResources().getString(R.string.config_error_parsing), Toast.LENGTH_LONG); setResult(RESULT_CANCELED, mConfigState); } - } + } else if(resultCode == ProviderAPI.INCORRECTLY_UPDATED_PROVIDER_DOT_JSON) { String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); showDownloadFailedDialog(getCurrentFocus(), reason_to_fail); refreshProviderList(0); mProgressBar.setVisibility(ProgressBar.GONE); progressbar_description.setVisibility(TextView.GONE); + ConfigHelper.removeFromSharedPref(Provider.KEY); setResult(RESULT_CANCELED, mConfigState); } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_JSON_FILES) { @@ -237,8 +238,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn int provider_index = getProviderIndex(id); startProgressBar(provider_index); mSelectedProvider = selected_provider; - - saveProviderJson(mSelectedProvider); + updateProviderDotJson(mSelectedProvider.name(), mSelectedProvider.providerJsonUrl(), mSelectedProvider.completelyTrusted()); } @Override @@ -265,7 +265,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); while(providers_iterator.hasNext()) { ProviderItem provider = providers_iterator.next(); - if(provider.id.equalsIgnoreCase(id)) { + if(provider.name().equalsIgnoreCase(id)) { return provider; } } @@ -343,7 +343,7 @@ 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. @@ -353,18 +353,19 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn private void saveProviderJson(ProviderItem provider) { JSONObject provider_json = new JSONObject(); try { - if(!provider.custom) { - updateProviderDotJson(provider.name, provider.provider_json_url, provider.danger_on); + 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 = provider.provider_json; + 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.danger_on); + ConfigHelper.saveSharedPref(ProviderItem.DANGER_ON, provider.completelyTrusted()); mProgressBar.incrementProgressBy(1); + downloadJSONFiles(mSelectedProvider); } } catch (JSONException e) { @@ -373,7 +374,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn } } - /** + /** * Asks ProviderAPI to download provider site's certificate and eip-service.json * * URLs are fetched from the provider parameter @@ -382,12 +383,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn private void downloadJSONFiles(ProviderItem provider) { Intent provider_API_command = new Intent(this, ProviderAPI.class); - Bundle parameters = new Bundle(); + Bundle parameters = new Bundle(); - parameters.putString(Provider.KEY, provider.name); + parameters.putString(Provider.KEY, provider.name()); parameters.putString(Provider.CA_CERT, provider.cert_json_url); parameters.putString(EIP.KEY, provider.eip_service_json_url); - parameters.putBoolean(ProviderItem.DANGER_ON, provider.danger_on); + parameters.putBoolean(ProviderItem.DANGER_ON, provider.completelyTrusted()); provider_API_command.setAction(ProviderAPI.DOWNLOAD_JSON_FILES_BUNDLE_EXTRA); provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index 75d91733..42cac792 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -55,23 +55,14 @@ public class ProviderListContent { /** * A provider item. - */ + */ public static class ProviderItem { - - final public static String CUSTOM = "custom"; final public static String DANGER_ON = "danger_on"; - - public boolean custom = false; - public String id; - public String name; - public String domain; - public String provider_json_url; - public JSONObject provider_json; - public String provider_json_filename; - public String eip_service_json_url; - public String cert_json_url; - public boolean danger_on = false; + private boolean custom = false; + private String provider_json_url; + private String name; + private boolean danger_on = false; /** * @param name of the provider @@ -85,19 +76,11 @@ public class ProviderListContent { byte[] urls_file_bytes = new byte[urls_file_input_stream.available()]; 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); - id = name; - this.name = name; + JSONObject file_contents = new JSONObject(urls_file_content); provider_json_url = file_contents.getString(Provider.DOT_JSON_URL); - domain = new URL(provider_json_url).getHost(); - //provider_json_filename = file_contents.getString("assets_json_provider"); - eip_service_json_url = file_contents.getString("json_eip_service"); - cert_json_url = file_contents.getString(EIP.CERTIFICATE); + this.name = name; this.custom = custom; this.danger_on = danger_on; - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -113,38 +96,20 @@ public class ProviderListContent { * @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, JSONObject provider_json, boolean custom, boolean danger_on) { - - try { - id = name; - //this.name = name; - this.provider_json_url = provider_json_url; - this.provider_json = provider_json; - this.name = provider_json.getJSONObject("name").getString("en"); - domain = new URL(provider_json_url).getHost(); - eip_service_json_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; - cert_json_url = provider_json.getString("ca_cert_uri"); - this.custom = custom; - this.danger_on = danger_on; - if(custom) - provider_json_filename = name + "_provider.json".replaceFirst("__", "_"); - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - @Override - public String toString() { - return name; - } - - public String getName() { - return id; + */ + public ProviderItem(String name, String provider_json_url, boolean custom, boolean danger_on) { + this.name = name; + this.provider_json_url = provider_json_url; + this.custom = custom; + this.danger_on = danger_on; } + + public String name() { return name; } + + public boolean custom() { return custom; } + + public String providerJsonUrl() { return provider_json_url; } + + public boolean completelyTrusted() { return danger_on; } } } -- cgit v1.2.3 From 50ad1c04cee7fe3409a1c2958d4dba98882cd45d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 9 Sep 2013 20:08:00 +0200 Subject: Preseeded providers only have provider.json url I've refactored everything to be able to remove all but provider.json url at urls files. --- assets/urls/bitmask.url | 6 +-- src/se/leap/bitmaskclient/AboutFragment.java | 3 -- src/se/leap/bitmaskclient/ConfigurationWizard.java | 43 ++++++++++++---------- src/se/leap/bitmaskclient/ProviderListContent.java | 8 ++++ .../leap/bitmaskclient/ProviderListFragment.java | 2 +- 5 files changed, 34 insertions(+), 28 deletions(-) diff --git a/assets/urls/bitmask.url b/assets/urls/bitmask.url index a17a5ff8..9e4c1317 100644 --- a/assets/urls/bitmask.url +++ b/assets/urls/bitmask.url @@ -1,7 +1,3 @@ { - "name" : "bitmask", - "assets_json_provider" : "providers/bitmask.net_provider.json", - "provider_json_url" : "https://bitmask.net/provider.json", - "cert" : "https://bitmask.net/ca.crt", - "json_eip_service" : "https://api.bitmask.net:4430/1/config/eip-service.json" + "provider_json_url" : "https://bitmask.net/provider.json" } \ No newline at end of file diff --git a/src/se/leap/bitmaskclient/AboutFragment.java b/src/se/leap/bitmaskclient/AboutFragment.java index d751dc2f..4d03a44b 100644 --- a/src/se/leap/bitmaskclient/AboutFragment.java +++ b/src/se/leap/bitmaskclient/AboutFragment.java @@ -4,9 +4,6 @@ import android.app.Fragment; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; -import android.text.Html; -import android.text.Spanned; -import android.text.method.LinkMovementMethod; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 532debfd..6d088a85 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -165,7 +165,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressBar.incrementProgressBy(1); } - downloadJSONFiles(mSelectedProvider); + downloadJSONFiles(provider_json, danger_on); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -366,7 +366,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressBar.incrementProgressBy(1); - downloadJSONFiles(mSelectedProvider); + downloadJSONFiles(provider_json, provider.completelyTrusted()); } } catch (JSONException e) { setResult(RESULT_CANCELED); @@ -378,23 +378,28 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn * Asks ProviderAPI to download provider site's certificate and eip-service.json * * URLs are fetched from the provider parameter - * @param provider from which certificate and eip-service.json files are going to be downloaded - */ - private void downloadJSONFiles(ProviderItem provider) { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - - Bundle parameters = new Bundle(); - - parameters.putString(Provider.KEY, provider.name()); - parameters.putString(Provider.CA_CERT, provider.cert_json_url); - parameters.putString(EIP.KEY, provider.eip_service_json_url); - parameters.putBoolean(ProviderItem.DANGER_ON, provider.completelyTrusted()); - - provider_API_command.setAction(ProviderAPI.DOWNLOAD_JSON_FILES_BUNDLE_EXTRA); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - - startService(provider_API_command); + * @param provider_json describing the provider from which certificate and eip-service.json files are going to be downloaded + */ + private void downloadJSONFiles(JSONObject provider_json, boolean danger_on) { + try { + Intent provider_API_command = new Intent(this, ProviderAPI.class); + + Bundle parameters = new Bundle(); + + parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); + parameters.putString(Provider.CA_CERT, provider_json.getString("ca_cert_uri")); + String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/config/eip-service.json"; + parameters.putString(EIP.KEY, eip_service_url); + + provider_API_command.setAction(ProviderAPI.DOWNLOAD_JSON_FILES_BUNDLE_EXTRA); + provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + + startService(provider_API_command); + } catch (JSONException e) { + //TODO Show error to the user. This will eventually be transformed to "unselectProvider()", which will show the graphical notice itself. + ConfigHelper.removeFromSharedPref(Provider.KEY); + } } /** diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index 42cac792..a6da5620 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -110,6 +110,14 @@ public class ProviderListContent { public String providerJsonUrl() { return provider_json_url; } + public String domain() { + try { + return new URL(provider_json_url).getHost(); + } catch (MalformedURLException e) { + return provider_json_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); + } + } + public boolean completelyTrusted() { return danger_on; } } } diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 84d46a11..ca66d295 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -155,7 +155,7 @@ public class ProviderListFragment extends ListFragment { // Notify the active callbacks interface (the activity, if the // fragment is attached to one) that an item has been selected. - mCallbacks.onItemSelected(ProviderListContent.ITEMS.get(position).id); + mCallbacks.onItemSelected(ProviderListContent.ITEMS.get(position).name()); for(int item_position = 0; item_position < listView.getCount(); item_position++) { if(item_position != position) -- cgit v1.2.3 From 3c1a4d2da2531b510fd01f921093f8c007bb4d5a 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. --- assets/urls/bitmask.url | 2 +- src/se/leap/bitmaskclient/ConfigurationWizard.java | 74 +++++++--------------- 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 + 6 files changed, 43 insertions(+), 122 deletions(-) diff --git a/assets/urls/bitmask.url b/assets/urls/bitmask.url index 9e4c1317..f924301e 100644 --- a/assets/urls/bitmask.url +++ b/assets/urls/bitmask.url @@ -1,3 +1,3 @@ { - "provider_json_url" : "https://bitmask.net/provider.json" + "main_url" : "https://bitmask.net/" } \ No newline at end of file diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 6d088a85..43ac8dd8 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -238,7 +238,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 @@ -343,37 +343,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 * @@ -471,34 +440,37 @@ 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 a0b8a763..98686bd2 100644 --- a/src/se/leap/bitmaskclient/NewProviderDialog.java +++ b/src/se/leap/bitmaskclient/NewProviderDialog.java @@ -41,7 +41,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; @@ -85,7 +85,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 871a20dd..268275e2 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)) { @@ -494,10 +487,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); @@ -512,56 +503,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 From 66a147efac201c8088fee98f26e02e3bd8979fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 14 Oct 2013 13:08:22 +0200 Subject: Successfully rebased onto progress-bars. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 32 ++++++++++++---------- src/se/leap/bitmaskclient/ProviderAPI.java | 5 +++- src/se/leap/bitmaskclient/ProviderListAdapter.java | 20 ++++++-------- src/se/leap/bitmaskclient/ProviderListContent.java | 9 ++---- .../leap/bitmaskclient/ProviderListFragment.java | 1 + 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 43ac8dd8..36d90318 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -149,16 +149,14 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn if(resultData.containsKey(Provider.NAME)) { String provider_id = resultData.getString(Provider.NAME); mSelectedProvider = getProvider(provider_id); - provider_list_fragment.addItem(mSelectedProvider); - ProviderListContent.removeItem(mSelectedProvider); + //provider_list_fragment.addItem(mSelectedProvider); + //ProviderListContent.removeItem(mSelectedProvider); - if(mSelectedProvider.custom) - refreshProviderList(0); + //refreshProviderList(0); if(!mProgressBar.isShown()) { int provider_index = getProviderIndex(provider_id); startProgressBar(provider_index); - provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); provider_list_fragment.hide(provider_index-2); //setProviderList(provider_list_fragment); } @@ -171,7 +169,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn e.printStackTrace(); mProgressBar.setVisibility(ProgressBar.GONE); progressbar_description.setVisibility(TextView.GONE); - refreshProviderList(0); + //refreshProviderList(0); //Toast.makeText(this, getResources().getString(R.string.config_error_parsing), Toast.LENGTH_LONG); setResult(RESULT_CANCELED, mConfigState); } @@ -194,7 +192,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressBar.incrementProgressBy(1); mProgressBar.setVisibility(ProgressBar.GONE); progressbar_description.setVisibility(TextView.GONE); - refreshProviderList(0); + //refreshProviderList(0); //Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); setResult(RESULT_OK); showProviderDetails(getCurrentFocus()); @@ -288,7 +286,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn while(providers_iterator.hasNext()) { ProviderItem provider = providers_iterator.next(); index++; - if(provider.id.equalsIgnoreCase(id)) { + if(provider.name().equalsIgnoreCase(id)) { break; } } @@ -447,18 +445,22 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn 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); + final ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url, danger_on); + + //ProviderListContent.addItem(added_provider); + provider_list_fragment.addItem(added_provider); + /*ProviderListFragment provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); + //provider_list_fragment.notifyAdapter(); + provider_list_fragment.addItem(added_provider); + refreshProviderList(0); + 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(); + provider_list_fragment.addItem(added_provider); } - }); + });*/ } /** diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 268275e2..8542aa92 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -488,7 +488,9 @@ public class ProviderAPI extends IntentService { private Bundle updateProviderDotJSON(Bundle task) { Bundle result = new Bundle(); boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); - String provider_json_url = task.getString(Provider.MAIN_URL) + "/provider.json"; + String provider_main_url = task.getString(Provider.MAIN_URL); + String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); + String provider_json_url = provider_main_url + "/provider.json"; try { String provider_dot_json_string = downloadWithCommercialCA(provider_json_url, danger_on); @@ -503,6 +505,7 @@ public class ProviderAPI extends IntentService { } else { ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); + result.putString(Provider.NAME, added_provider.name()); result.putBoolean(RESULT_KEY, true); result.putString(Provider.KEY, provider_json.toString()); result.putBoolean(ProviderItem.DANGER_ON, danger_on); diff --git a/src/se/leap/bitmaskclient/ProviderListAdapter.java b/src/se/leap/bitmaskclient/ProviderListAdapter.java index 19c4f72c..bb1318fc 100644 --- a/src/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/src/se/leap/bitmaskclient/ProviderListAdapter.java @@ -1,5 +1,6 @@ package se.leap.bitmaskclient; +import java.lang.reflect.Array; import java.util.List; import android.content.Context; @@ -10,7 +11,6 @@ import android.widget.ArrayAdapter; import android.widget.TwoLineListItem; public class ProviderListAdapter extends ArrayAdapter { - private T[] items = null; private static boolean[] hidden = null; public void hide(int position) { @@ -42,7 +42,7 @@ public class ProviderListAdapter extends ArrayAdapter { } private int getHiddenCount() { int count = 0; - for(int i=0;i extends ArrayAdapter { @Override public int getCount() { - return (items.length - getHiddenCount()); + return (hidden.length - getHiddenCount()); } public ProviderListAdapter(Context mContext, int layout, List objects) { super(mContext, layout, objects); - items = objects.toArray((T[])new Object[0]); if(hidden == null) { - hidden = new boolean[items.length]; - for (int i = 0; i < items.length; i++) + hidden = new boolean[objects.size()]; + for (int i = 0; i < objects.size(); i++) hidden[i] = false; } } public ProviderListAdapter(Context mContext, int layout, List objects, boolean show_all_providers) { super(mContext, layout, objects); - items = objects.toArray((T[])new Object[0]); if(show_all_providers) { - hidden = new boolean[items.length]; - for (int i = 0; i < items.length; i++) + hidden = new boolean[objects.size()]; + for (int i = 0; i < objects.size(); i++) hidden[i] = false; } } @@ -101,8 +99,8 @@ public class ProviderListAdapter extends ArrayAdapter { row = (TwoLineListItem)convertView; } ProviderListContent.ProviderItem data = ProviderListContent.ITEMS.get(position); - row.getText1().setText(data.domain); - row.getText2().setText(data.name); + row.getText1().setText(data.domain()); + row.getText2().setText(data.name()); return row; } diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index 3695066c..af61dbd6 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -61,7 +61,6 @@ public class ProviderListContent { public static class ProviderItem { final public static String CUSTOM = "custom"; final public static String DANGER_ON = "danger_on"; - private boolean custom = false; private String provider_main_url; private String name; private boolean danger_on = false; @@ -80,8 +79,7 @@ public class ProviderListContent { 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); - this.name = name; - this.custom = custom; + this.name = name; this.danger_on = danger_on; } catch (JSONException e) { // TODO Auto-generated catch block @@ -99,17 +97,14 @@ public class ProviderListContent { * @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_main_url, boolean custom, boolean danger_on) { + public ProviderItem(String name, String provider_main_url, boolean danger_on) { this.name = name; this.provider_main_url = provider_main_url; - this.custom = custom; this.danger_on = danger_on; } public String name() { return name; } - public boolean custom() { return custom; } - public String providerMainUrl() { return provider_main_url; } public String domain() { diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 62dcef7a..6a0f2a05 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -199,6 +199,7 @@ public class ProviderListFragment extends ListFragment { public void addItem(ProviderItem provider) { content_adapter.add(provider); + content_adapter.notifyDataSetChanged(); } public void hide(int position) { -- cgit v1.2.3 From 718857cc244a778ba56f96b0bcf05dc22407f414 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 14 Oct 2013 15:52:38 +0200 Subject: Provider setup: One method downloads everything. This method uses another 3 to download each file. Next step: broadcast progress and remove unnecessary methods. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 21 ++++- src/se/leap/bitmaskclient/Dashboard.java | 1 + src/se/leap/bitmaskclient/Provider.java | 2 +- src/se/leap/bitmaskclient/ProviderAPI.java | 95 +++++++++++++++++++--- src/se/leap/bitmaskclient/ProviderListContent.java | 14 +--- 5 files changed, 105 insertions(+), 28 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 36d90318..6c02d612 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -163,7 +163,20 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressBar.incrementProgressBy(1); } - downloadJSONFiles(provider_json, danger_on); + + if (ConfigHelper.getBoolFromSharedPref(EIP.ALLOWED_ANON)){ + mConfigState.putExtra(SERVICES_RETRIEVED, true); + downloadAnonCert(); + } else { + mProgressBar.incrementProgressBy(1); + mProgressBar.setVisibility(ProgressBar.GONE); + progressbar_description.setVisibility(TextView.GONE); + //refreshProviderList(0); + //Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); + setResult(RESULT_OK); + showProviderDetails(getCurrentFocus()); + } + //downloadJSONFiles(provider_json, danger_on); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -236,7 +249,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn int provider_index = getProviderIndex(id); startProgressBar(provider_index); mSelectedProvider = selected_provider; - updateProviderDotJson(mSelectedProvider.providerMainUrl(), mSelectedProvider.completelyTrusted()); + updateProviderDotJson(mSelectedProvider.providerMainUrl(), true); } @Override @@ -331,7 +344,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn boolean custom = false; provider_name = url_filepath.subSequence(0, url_filepath.indexOf(".")).toString(); if(ProviderListContent.ITEMS.isEmpty()) //TODO I have to implement a way of checking if a provider new or is already present in that ITEMS list - ProviderListContent.addItem(new ProviderItem(provider_name, asset_manager.open(url_files_folder + "/" + url_filepath), custom, false)); + ProviderListContent.addItem(new ProviderItem(provider_name, asset_manager.open(url_files_folder + "/" + url_filepath))); loaded_preseeded_providers = true; } } catch (IOException e) { @@ -445,7 +458,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn private void showProvider(final String provider_main_url, final boolean danger_on) { String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); - final ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url, danger_on); + final ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url); //ProviderListContent.addItem(added_provider); provider_list_fragment.addItem(added_provider); diff --git a/src/se/leap/bitmaskclient/Dashboard.java b/src/se/leap/bitmaskclient/Dashboard.java index 65ff2800..0ae7085a 100644 --- a/src/se/leap/bitmaskclient/Dashboard.java +++ b/src/se/leap/bitmaskclient/Dashboard.java @@ -243,6 +243,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf } eipStop(); } + ConfigHelper.removeFromSharedPref(Provider.KEY); startActivityForResult(new Intent(this,ConfigurationWizard.class), SWITCH_PROVIDER); return true; case R.id.login_button: diff --git a/src/se/leap/bitmaskclient/Provider.java b/src/se/leap/bitmaskclient/Provider.java index f36e5946..598999fd 100644 --- a/src/se/leap/bitmaskclient/Provider.java +++ b/src/se/leap/bitmaskclient/Provider.java @@ -99,7 +99,7 @@ public final class Provider implements Serializable { preferences = activity.getSharedPreferences(Dashboard.SHARED_PREFERENCES,Context.MODE_PRIVATE); // Inflate our provider.json data try { - definition = new JSONObject( preferences.getString("provider", "") ); + definition = new JSONObject( preferences.getString(Provider.KEY, "") ); } catch (JSONException e) { // TODO: handle exception diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 8542aa92..6003fa6c 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -36,6 +36,7 @@ import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.security.interfaces.RSAPrivateKey; @@ -68,7 +69,6 @@ import android.os.Handler; import android.os.ResultReceiver; import android.util.Base64; import android.util.Log; -import android.widget.ProgressBar; import android.widget.Toast; /** @@ -99,7 +99,8 @@ public class ProviderAPI extends IntentService { SESSION_ID_KEY = "session_id", ERRORS = "errors", UPDATE_ACTION = "update_action", - UPDATE_DATA = "update data" + UPDATE_DATA = "update data", + TAG = "provider_api_tag" ; final public static int @@ -489,9 +490,13 @@ public class ProviderAPI extends IntentService { Bundle result = new Bundle(); boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); String provider_main_url = task.getString(Provider.MAIN_URL); - String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); - String provider_json_url = provider_main_url + "/provider.json"; - + if(downloadCACert(provider_main_url, danger_on)) { + result.putBoolean(RESULT_KEY, true); + if(getAndSetProviderJson(provider_main_url)) + if(getAndSetEipServiceJson()) + ; + } + /* try { String provider_dot_json_string = downloadWithCommercialCA(provider_json_url, danger_on); if(provider_dot_json_string.isEmpty()) { @@ -514,10 +519,72 @@ public class ProviderAPI extends IntentService { result.putBoolean(RESULT_KEY, false); result.putString(ERRORS, "Corrupt download"); } + */ + return result; + } + + private boolean downloadCACert(String provider_main_url, boolean danger_on) { + String cert_string = downloadWithCommercialCA(provider_main_url + "/ca.crt", danger_on); + if(validCertificate(cert_string)) + ConfigHelper.saveSharedPref(Provider.CA_CERT, "-----BEGIN CERTIFICATE-----\n"+cert_string+"-----END CERTIFICATE-----"); + else + return false; + return true; + } + + private boolean validCertificate(String cert_string) { + boolean result = false; + if(!cert_string.isEmpty()) { + X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(cert_string); + try { + Base64.encodeToString( certCert.getEncoded(), Base64.DEFAULT); + result = true; + } catch (CertificateEncodingException e) { + Log.d(TAG, e.getLocalizedMessage()); + } + } + + return result; + } + + private boolean getAndSetProviderJson(String provider_main_url) { + boolean result = false; + + String provider_dot_json_string = downloadWithProviderCA(provider_main_url + "/provider.json", true); + + try { + JSONObject provider_json = new JSONObject(provider_dot_json_string); + String name = provider_json.getString(Provider.NAME); + //TODO setProviderName(name); + + ConfigHelper.saveSharedPref(Provider.KEY, provider_json); + ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); + ProviderItem added_provider = new ProviderItem(name, provider_main_url); + + result = true; + } catch (JSONException e) { + } return result; } + + private boolean getAndSetEipServiceJson() { + boolean result = false; + + try { + JSONObject provider_json = ConfigHelper.getJsonFromSharedPref(Provider.KEY); + String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; + String eip_service_json_string = downloadWithProviderCA(eip_service_url, true); + JSONObject eip_service_json = new JSONObject(eip_service_json_string); + eip_service_json.getInt(Provider.API_RETURN_SERIAL); + + ConfigHelper.saveSharedPref(EIP.KEY, eip_service_json); + result = true; + } catch (JSONException e) { + } + return result; + } /** * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. * @@ -543,13 +610,13 @@ public class ProviderAPI extends IntentService { json_file_content = formatErrorMessage(R.string.server_is_down_message); } catch (IOException e) { if(provider_url != null) { - json_file_content = downloadWithProviderCA(provider_url, danger_on); + json_file_content = downloadWithProviderCA(string_url, danger_on); } else { json_file_content = formatErrorMessage(R.string.certificate_error); } } catch (Exception e) { if(provider_url != null && danger_on) { - json_file_content = downloadWithProviderCA(provider_url, danger_on); + json_file_content = downloadWithProviderCA(string_url, danger_on); } } @@ -557,15 +624,16 @@ public class ProviderAPI extends IntentService { } /** - * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. - * @param url + * Tries to download the contents of the provided url using not commercially validated CA certificate from chosen provider. + * @param url as a string * @param danger_on true to download CA certificate in case it has not been downloaded. * @return an empty string if it fails, the url content if not. */ - private String downloadWithProviderCA(URL url, boolean danger_on) { + private String downloadWithProviderCA(String url_string, boolean danger_on) { String json_file_content = ""; try { + URL url = new URL(url_string); // Tell the URLConnection to use a SocketFactory from our SSLContext HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); @@ -579,7 +647,7 @@ public class ProviderAPI extends IntentService { } catch (IOException e) { // The downloaded certificate doesn't validate our https connection. if(danger_on) { - json_file_content = downloadWithoutCA(url); + json_file_content = downloadWithoutCA(url_string); } else { json_file_content = formatErrorMessage(R.string.certificate_error); } @@ -622,7 +690,7 @@ public class ProviderAPI extends IntentService { /** * Downloads the string that's in the url with any certificate. */ - private String downloadWithoutCA(URL url) { + private String downloadWithoutCA(String url_string) { String string = ""; try { @@ -650,6 +718,7 @@ public class ProviderAPI extends IntentService { SSLContext context = SSLContext.getInstance("TLS"); context.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); + URL url = new URL(url_string); HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); urlConnection.setSSLSocketFactory(context.getSocketFactory()); urlConnection.setHostnameVerifier(hostnameVerifier); @@ -741,7 +810,7 @@ public class ProviderAPI extends IntentService { String new_cert_string_url = provider_main_url.toString() + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.CERTIFICATE; boolean danger_on = ConfigHelper.getBoolFromSharedPref(ProviderItem.DANGER_ON); - String cert_string = downloadWithCommercialCA(new_cert_string_url, danger_on); + String cert_string = downloadWithProviderCA(new_cert_string_url, true); if(!cert_string.isEmpty()) { if(ConfigHelper.checkErroneousDownload(cert_string)) { String reason_to_fail = provider_json.getString(ERRORS); diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index af61dbd6..b13c5db4 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -62,8 +62,7 @@ public class ProviderListContent { final public static String CUSTOM = "custom"; final public static String DANGER_ON = "danger_on"; private String provider_main_url; - private String name; - private boolean danger_on = false; + private String name; /** * @param name of the provider @@ -71,7 +70,7 @@ public class ProviderListContent { * @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, InputStream urls_file_input_stream, boolean custom, boolean danger_on) { + public ProviderItem(String name, InputStream urls_file_input_stream) { try { byte[] urls_file_bytes = new byte[urls_file_input_stream.available()]; @@ -80,7 +79,6 @@ public class ProviderListContent { JSONObject file_contents = new JSONObject(urls_file_content); provider_main_url = file_contents.getString(Provider.MAIN_URL); this.name = name; - this.danger_on = danger_on; } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -95,12 +93,10 @@ public class ProviderListContent { * @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_main_url, boolean danger_on) { + public ProviderItem(String name, String provider_main_url) { this.name = name; - this.provider_main_url = provider_main_url; - this.danger_on = danger_on; + this.provider_main_url = provider_main_url; } public String name() { return name; } @@ -114,7 +110,5 @@ public class ProviderListContent { return provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("/.*", ""); } } - - public boolean completelyTrusted() { return danger_on; } } } -- cgit v1.2.3 From 19cc4e83eb26374620c5f712a4f7f10d79eeb217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 14 Oct 2013 16:04:56 +0200 Subject: A bit of cleaning. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 96 +++------------------- src/se/leap/bitmaskclient/ProviderAPI.java | 92 ++------------------- src/se/leap/bitmaskclient/ProviderListContent.java | 2 - 3 files changed, 18 insertions(+), 172 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 6c02d612..ee42bae9 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -30,7 +30,6 @@ import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; -import android.app.ListFragment; import android.content.Context; import android.content.Intent; import android.content.res.AssetManager; @@ -126,17 +125,10 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn .replace(R.id.configuration_wizard_layout, new_provider_list_fragment, ProviderListFragment.TAG) .commit(); } - - private void setProviderList(ProviderListFragment new_provider_list_fragment) { - FragmentManager fragmentManager = getFragmentManager(); - fragmentManager.beginTransaction() - .replace(R.id.configuration_wizard_layout, new_provider_list_fragment, ProviderListFragment.TAG) - .commit(); - } @Override public void onReceiveResult(int resultCode, Bundle resultData) { - if(resultCode == ProviderAPI.CORRECTLY_UPDATED_PROVIDER_DOT_JSON) { + if(resultCode == ProviderAPI.PROVIDER_OK) { JSONObject provider_json; try { provider_json = new JSONObject(resultData.getString(Provider.KEY)); @@ -185,42 +177,16 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn //refreshProviderList(0); //Toast.makeText(this, getResources().getString(R.string.config_error_parsing), Toast.LENGTH_LONG); setResult(RESULT_CANCELED, mConfigState); - } - } - else if(resultCode == ProviderAPI.INCORRECTLY_UPDATED_PROVIDER_DOT_JSON) { + } + } else if(resultCode == ProviderAPI.PROVIDER_NOK) { + //refreshProviderList(0); String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); showDownloadFailedDialog(getCurrentFocus(), reason_to_fail); - refreshProviderList(0); mProgressBar.setVisibility(ProgressBar.GONE); - progressbar_description.setVisibility(TextView.GONE); + progressbar_description.setVisibility(TextView.GONE); ConfigHelper.removeFromSharedPref(Provider.KEY); setResult(RESULT_CANCELED, mConfigState); } - else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_JSON_FILES) { - mProgressBar.incrementProgressBy(1); - if (ConfigHelper.getBoolFromSharedPref(EIP.ALLOWED_ANON)){ - mConfigState.putExtra(SERVICES_RETRIEVED, true); - downloadAnonCert(); - } else { - mProgressBar.incrementProgressBy(1); - mProgressBar.setVisibility(ProgressBar.GONE); - progressbar_description.setVisibility(TextView.GONE); - //refreshProviderList(0); - //Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); - setResult(RESULT_OK); - showProviderDetails(getCurrentFocus()); - } - } - else if(resultCode == ProviderAPI.INCORRECTLY_DOWNLOADED_JSON_FILES) { - //Toast.makeText(getApplicationContext(), R.string.incorrectly_downloaded_json_files_message, Toast.LENGTH_LONG).show(); - String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); - showDownloadFailedDialog(getCurrentFocus(), reason_to_fail); - refreshProviderList(0); - //Toast.makeText(getApplicationContext(), R.string.incorrectly_downloaded_json_files_message, - mProgressBar.setVisibility(ProgressBar.GONE); - progressbar_description.setVisibility(TextView.GONE); - setResult(RESULT_CANCELED, mConfigState); - } else if(resultCode == ProviderAPI.CORRECTLY_DOWNLOADED_CERTIFICATE) { mProgressBar.incrementProgressBy(1); mProgressBar.setVisibility(ProgressBar.GONE); @@ -249,7 +215,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn int provider_index = getProviderIndex(id); startProgressBar(provider_index); mSelectedProvider = selected_provider; - updateProviderDotJson(mSelectedProvider.providerMainUrl(), true); + setUpProvider(mSelectedProvider.providerMainUrl(), true); } @Override @@ -353,34 +319,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn return loaded_preseeded_providers; } - - /** - * Asks ProviderAPI to download provider site's certificate and eip-service.json - * - * URLs are fetched from the provider parameter - * @param provider_json describing the provider from which certificate and eip-service.json files are going to be downloaded - */ - private void downloadJSONFiles(JSONObject provider_json, boolean danger_on) { - try { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - - Bundle parameters = new Bundle(); - - parameters.putBoolean(ProviderItem.DANGER_ON, danger_on); - parameters.putString(Provider.CA_CERT, provider_json.getString("ca_cert_uri")); - String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/config/eip-service.json"; - parameters.putString(EIP.KEY, eip_service_url); - - provider_API_command.setAction(ProviderAPI.DOWNLOAD_JSON_FILES_BUNDLE_EXTRA); - provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - - startService(provider_API_command); - } catch (JSONException e) { - //TODO Show error to the user. This will eventually be transformed to "unselectProvider()", which will show the graphical notice itself. - ConfigHelper.removeFromSharedPref(Provider.KEY); - } - } /** * Asks ProviderAPI to download an anonymous (anon) VPN certificate. @@ -453,27 +391,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn public void showAndSelectProvider(String provider_main_url, boolean danger_on) { showProvider(provider_main_url, danger_on); - updateProviderDotJson(provider_main_url, danger_on); + setUpProvider(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("\\/", "_"); - final ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url); - - //ProviderListContent.addItem(added_provider); - provider_list_fragment.addItem(added_provider); - /*ProviderListFragment provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); - //provider_list_fragment.notifyAdapter(); + ProviderItem added_provider = new ProviderItem(provider_name, provider_main_url); provider_list_fragment.addItem(added_provider); - refreshProviderList(0); - - runOnUiThread(new Runnable() { - - @Override - public void run() { - provider_list_fragment.addItem(added_provider); - } - });*/ } /** @@ -482,13 +406,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn * @param provider_main_url * @param danger_on tells if HTTPS client should bypass certificate errors */ - public void updateProviderDotJson(String provider_main_url, boolean danger_on) { + public void setUpProvider(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.UPDATE_PROVIDER_DOTJSON); + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 6003fa6c..60ed1e1f 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -85,8 +85,7 @@ public class ProviderAPI extends IntentService { private Handler mHandler; final public static String - DOWNLOAD_JSON_FILES_BUNDLE_EXTRA = "downloadJSONFiles", - UPDATE_PROVIDER_DOTJSON = "updateProviderDotJSON", + SET_UP_PROVIDER = "setUpProvider", DOWNLOAD_NEW_PROVIDER_DOTJSON = "downloadNewProviderDotJSON", SRP_REGISTER = "srpRegister", SRP_AUTH = "srpAuth", @@ -105,8 +104,6 @@ public class ProviderAPI extends IntentService { final public static int CUSTOM_PROVIDER_ADDED = 0, - CORRECTLY_DOWNLOADED_JSON_FILES = 1, - INCORRECTLY_DOWNLOADED_JSON_FILES = 2, SRP_AUTHENTICATION_SUCCESSFUL = 3, SRP_AUTHENTICATION_FAILED = 4, SRP_REGISTRATION_SUCCESSFUL = 5, @@ -115,8 +112,8 @@ public class ProviderAPI extends IntentService { LOGOUT_FAILED = 8, CORRECTLY_DOWNLOADED_CERTIFICATE = 9, INCORRECTLY_DOWNLOADED_CERTIFICATE = 10, - CORRECTLY_UPDATED_PROVIDER_DOT_JSON = 11, - INCORRECTLY_UPDATED_PROVIDER_DOT_JSON = 12, + PROVIDER_OK = 11, + PROVIDER_NOK = 12, CORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 13, INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14 ; @@ -143,19 +140,12 @@ public class ProviderAPI extends IntentService { String action = command.getAction(); Bundle parameters = command.getBundleExtra(PARAMETERS); - if(action.equalsIgnoreCase(DOWNLOAD_JSON_FILES_BUNDLE_EXTRA)) { - Bundle result = downloadJsonFiles(parameters); + if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { + Bundle result = setUpProvider(parameters); if(result.getBoolean(RESULT_KEY)) { - receiver.send(CORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY); - } else { - receiver.send(INCORRECTLY_DOWNLOADED_JSON_FILES, result); - } - } else if(action.equalsIgnoreCase(UPDATE_PROVIDER_DOTJSON)) { - Bundle result = updateProviderDotJSON(parameters); - if(result.getBoolean(RESULT_KEY)) { - receiver.send(CORRECTLY_UPDATED_PROVIDER_DOT_JSON, result); + receiver.send(PROVIDER_OK, result); } else { - receiver.send(INCORRECTLY_UPDATED_PROVIDER_DOT_JSON, Bundle.EMPTY); + receiver.send(PROVIDER_NOK, Bundle.EMPTY); } } else if (action.equalsIgnoreCase(SRP_AUTH)) { Bundle session_id_bundle = authenticateBySRP(parameters); @@ -178,63 +168,6 @@ public class ProviderAPI extends IntentService { } } } - - /** - * Downloads the main cert and the eip-service.json files given through the task parameter - * @param task - * @return true if eip-service.json was parsed as a JSON object correctly. - */ - private Bundle downloadJsonFiles(Bundle task) { - Bundle result = new Bundle(); - String cert_url = task.getString(Provider.CA_CERT); - String eip_service_json_url = task.getString(EIP.KEY); - boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); - try { - String cert_string = downloadWithCommercialCA(cert_url, danger_on); - - if(ConfigHelper.checkErroneousDownload(cert_string)) { - JSONObject possible_errors = new JSONObject(cert_string); - String reason_to_fail = ""; - if(cert_string.isEmpty()) - reason_to_fail = "Empty certificate downloaded"; - else - reason_to_fail = possible_errors.getString(ERRORS); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } else { - X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(cert_string); - cert_string = Base64.encodeToString( certCert.getEncoded(), Base64.DEFAULT); - ConfigHelper.saveSharedPref(Provider.CA_CERT, "-----BEGIN CERTIFICATE-----\n"+cert_string+"-----END CERTIFICATE-----"); - } - } catch (JSONException e) { - e.printStackTrace(); - result.putBoolean(RESULT_KEY, false); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - result.putBoolean(RESULT_KEY, false); - } - - try { - String eip_service_string = downloadWithCommercialCA(eip_service_json_url, danger_on); - JSONObject eip_service_json = new JSONObject(eip_service_string); - if(eip_service_json.has(ERRORS)) { - String reason_to_fail = eip_service_json.getString(ERRORS); - result.putString(ERRORS, reason_to_fail); - result.putBoolean(RESULT_KEY, false); - } - else { - ConfigHelper.saveSharedPref(EIP.KEY, eip_service_json); - ConfigHelper.saveSharedPref(EIP.PARSED_SERIAL, 0); - } - - result.putBoolean(RESULT_KEY, true); - } catch (JSONException e) { - result.putBoolean(RESULT_KEY, false); - } - - return result; - } /** * Starts the authentication process using SRP protocol. @@ -486,7 +419,7 @@ public class ProviderAPI extends IntentService { * @param task containing a boolean meaning if the provider is custom or not, another boolean meaning if the user completely trusts this provider, the provider name and its provider.json url. * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. */ - private Bundle updateProviderDotJSON(Bundle task) { + private Bundle setUpProvider(Bundle task) { Bundle result = new Bundle(); boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); String provider_main_url = task.getString(Provider.MAIN_URL); @@ -740,15 +673,6 @@ public class ProviderAPI extends IntentService { } return string; } - - /** - * Tries to guess the provider.json url given the main provider url. - * @param provider_main_url - * @return the guessed provider.json url - */ - private String guessProviderDotJsonURL(String provider_main_url) { - return provider_main_url + "/provider.json"; - } /** * Logs out from the api url retrieved from the task. diff --git a/src/se/leap/bitmaskclient/ProviderListContent.java b/src/se/leap/bitmaskclient/ProviderListContent.java index b13c5db4..e1ca4f9a 100644 --- a/src/se/leap/bitmaskclient/ProviderListContent.java +++ b/src/se/leap/bitmaskclient/ProviderListContent.java @@ -28,8 +28,6 @@ import java.net.MalformedURLException; import org.json.JSONException; import org.json.JSONObject; -import android.app.Activity; - /** * Models the provider list shown in the ConfigurationWizard. * -- cgit v1.2.3 From 50c88ad133c6bac39cb68332f1f56cd27f568205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 14 Oct 2013 17:05:06 +0200 Subject: ProviderAPI broadcasts progress to CW. Setting up a provider makes the progressbar move according to the number of files downloaded (1/3, 2/3, 3/3). Next step: recover from download errors individually. That means that if a download fails but the others went OK, the user will be prompted to retry that individual failing download. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 78 +++++++++++++--------- src/se/leap/bitmaskclient/Dashboard.java | 21 +----- src/se/leap/bitmaskclient/ProviderAPI.java | 38 +++-------- src/se/leap/bitmaskclient/ProviderListAdapter.java | 1 - .../leap/bitmaskclient/ProviderListFragment.java | 8 ++- 5 files changed, 61 insertions(+), 85 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index ee42bae9..b35123b0 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -30,8 +30,10 @@ import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.res.AssetManager; import android.os.Bundle; import android.os.Handler; @@ -61,7 +63,6 @@ import android.widget.TextView; public class ConfigurationWizard extends Activity implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogInterface, ProviderDetailFragment.ProviderDetailFragmentInterface, Receiver { - private ProviderItem mSelectedProvider; private ProgressBar mProgressBar; private TextView progressbar_description; private ProviderListFragment provider_list_fragment; @@ -76,6 +77,8 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn final protected static String SERVICES_RETRIEVED = "SERVICES RETRIEVED"; public ProviderAPIResultReceiver providerAPI_result_receiver; + private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -88,7 +91,11 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn progressbar_description.setVisibility(TextView.INVISIBLE); providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); - + providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); + IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_PROGRESSBAR); + update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); + registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); + ConfigHelper.setSharedPreferences(getSharedPreferences(Dashboard.SHARED_PREFERENCES, MODE_PRIVATE)); loadPreseededProviders(); @@ -114,6 +121,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn // TODO: If exposing deep links into your app, handle intents here. } + @Override + protected void onDestroy() { + super.onDestroy(); + unregisterReceiver(providerAPI_broadcast_receiver_update); + } + public void refreshProviderList(int top_padding) { ProviderListFragment new_provider_list_fragment = new ProviderListFragment(); Bundle top_padding_bundle = new Bundle(); @@ -133,29 +146,8 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn try { provider_json = new JSONObject(resultData.getString(Provider.KEY)); boolean danger_on = resultData.getBoolean(ProviderItem.DANGER_ON); - 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)); mConfigState.setAction(PROVIDER_SET); - if(resultData.containsKey(Provider.NAME)) { - String provider_id = resultData.getString(Provider.NAME); - mSelectedProvider = getProvider(provider_id); - //provider_list_fragment.addItem(mSelectedProvider); - //ProviderListContent.removeItem(mSelectedProvider); - - //refreshProviderList(0); - - if(!mProgressBar.isShown()) { - int provider_index = getProviderIndex(provider_id); - startProgressBar(provider_index); - provider_list_fragment.hide(provider_index-2); - //setProviderList(provider_list_fragment); - } - mProgressBar.incrementProgressBy(1); - } - - if (ConfigHelper.getBoolFromSharedPref(EIP.ALLOWED_ANON)){ mConfigState.putExtra(SERVICES_RETRIEVED, true); downloadAnonCert(); @@ -163,14 +155,10 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressBar.incrementProgressBy(1); mProgressBar.setVisibility(ProgressBar.GONE); progressbar_description.setVisibility(TextView.GONE); - //refreshProviderList(0); - //Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); setResult(RESULT_OK); showProviderDetails(getCurrentFocus()); } - //downloadJSONFiles(provider_json, danger_on); } catch (JSONException e) { - // TODO Auto-generated catch block e.printStackTrace(); mProgressBar.setVisibility(ProgressBar.GONE); progressbar_description.setVisibility(TextView.GONE); @@ -214,8 +202,8 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn ProviderItem selected_provider = getProvider(id); int provider_index = getProviderIndex(id); startProgressBar(provider_index); - mSelectedProvider = selected_provider; - setUpProvider(mSelectedProvider.providerMainUrl(), true); + provider_list_fragment.hideAllBut(provider_index); + setUpProvider(selected_provider.providerMainUrl(), true); } @Override @@ -238,16 +226,27 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn setResult(RESULT_CANCELED, ask_quit); } - private ProviderItem getProvider(String id) { + private ProviderItem getProvider(String name) { Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); while(providers_iterator.hasNext()) { ProviderItem provider = providers_iterator.next(); - if(provider.name().equalsIgnoreCase(id)) { + if(provider.name().equalsIgnoreCase(name)) { return provider; } } return null; } + + private String getId(String provider_main_url) { + Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); + while(providers_iterator.hasNext()) { + ProviderItem provider = providers_iterator.next(); + if(provider.providerMainUrl().equalsIgnoreCase(provider_main_url)) { + return provider.name(); + } + } + return ""; + } private void startProgressBar(int list_item_index) { mProgressBar.setVisibility(ProgressBar.VISIBLE); @@ -264,10 +263,10 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); while(providers_iterator.hasNext()) { ProviderItem provider = providers_iterator.next(); - index++; if(provider.name().equalsIgnoreCase(id)) { break; } + index++; } return index; } @@ -391,7 +390,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn public void showAndSelectProvider(String provider_main_url, boolean danger_on) { showProvider(provider_main_url, danger_on); - setUpProvider(provider_main_url, danger_on); + autoSelectProvider(provider_main_url, danger_on); } private void showProvider(final String provider_main_url, final boolean danger_on) { @@ -400,6 +399,10 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn provider_list_fragment.addItem(added_provider); } + private void autoSelectProvider(String provider_main_url, boolean danger_on) { + onItemSelected(getId(provider_main_url)); + } + /** * Asks ProviderAPI to download a new provider.json file * @param provider_name @@ -475,4 +478,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn setResult(RESULT_OK); finish(); } + + public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + int update = intent.getIntExtra(ProviderAPI.CURRENT_PROGRESS, 0); + mProgressBar.setProgress(update); + } + } } diff --git a/src/se/leap/bitmaskclient/Dashboard.java b/src/se/leap/bitmaskclient/Dashboard.java index 0ae7085a..8c7c495d 100644 --- a/src/se/leap/bitmaskclient/Dashboard.java +++ b/src/se/leap/bitmaskclient/Dashboard.java @@ -16,8 +16,6 @@ */ package se.leap.bitmaskclient; -import org.apache.http.cookie.Cookie; -import org.apache.http.impl.cookie.BasicClientCookie; import org.json.JSONException; import org.json.JSONObject; @@ -30,12 +28,9 @@ import android.app.DialogFragment; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; -import android.app.ProgressDialog; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; -import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; @@ -45,11 +40,7 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.view.ViewStub; -import android.widget.CompoundButton; import android.widget.ProgressBar; -import android.widget.RelativeLayout; -import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; @@ -76,7 +67,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf private ProgressBar mProgressBar; private TextView eipStatus; - private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; private static Context app; private static SharedPreferences preferences; private static Provider provider; @@ -98,10 +88,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf // mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); // eipStatus = (TextView) findViewById(R.id.eipStatus); - providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); - IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_ACTION); - update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); + mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); ConfigHelper.setSharedPreferences(getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE)); preferences = ConfigHelper.shared_preferences; @@ -116,7 +103,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf @Override protected void onDestroy() { super.onDestroy(); - unregisterReceiver(providerAPI_broadcast_receiver_update); } @Override @@ -176,11 +162,6 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf provider.init( this ); setContentView(R.layout.client_dashboard); - - providerAPI_broadcast_receiver_update = new ProviderAPIBroadcastReceiver_Update(); - IntentFilter update_intent_filter = new IntentFilter(ProviderAPI.UPDATE_ACTION); - update_intent_filter.addCategory(Intent.CATEGORY_DEFAULT); - registerReceiver(providerAPI_broadcast_receiver_update, update_intent_filter); providerNameTV = (TextView) findViewById(R.id.providerName); providerNameTV.setText(provider.getDomain()); diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 60ed1e1f..27081df2 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -97,8 +97,8 @@ public class ProviderAPI extends IntentService { SESSION_ID_COOKIE_KEY = "session_id_cookie_key", SESSION_ID_KEY = "session_id", ERRORS = "errors", - UPDATE_ACTION = "update_action", - UPDATE_DATA = "update data", + UPDATE_PROGRESSBAR = "update_action", + CURRENT_PROGRESS = "update data", TAG = "provider_api_tag" ; @@ -266,9 +266,9 @@ public class ProviderAPI extends IntentService { */ private void broadcast_progress(int progress) { Intent intentUpdate = new Intent(); - intentUpdate.setAction(UPDATE_ACTION); + intentUpdate.setAction(UPDATE_PROGRESSBAR); intentUpdate.addCategory(Intent.CATEGORY_DEFAULT); - intentUpdate.putExtra(UPDATE_DATA, progress); + intentUpdate.putExtra(CURRENT_PROGRESS, progress); sendBroadcast(intentUpdate); } @@ -421,38 +421,18 @@ public class ProviderAPI extends IntentService { */ private Bundle setUpProvider(Bundle task) { Bundle result = new Bundle(); + int progress = 0; boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); String provider_main_url = task.getString(Provider.MAIN_URL); if(downloadCACert(provider_main_url, danger_on)) { + broadcast_progress(progress++); result.putBoolean(RESULT_KEY, true); - if(getAndSetProviderJson(provider_main_url)) + if(getAndSetProviderJson(provider_main_url)) { + broadcast_progress(progress++); if(getAndSetEipServiceJson()) - ; - } - /* - try { - String provider_dot_json_string = downloadWithCommercialCA(provider_json_url, danger_on); - if(provider_dot_json_string.isEmpty()) { - result.putBoolean(RESULT_KEY, false); - } else { - JSONObject provider_json = new JSONObject(provider_dot_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(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); - - result.putString(Provider.NAME, added_provider.name()); - result.putBoolean(RESULT_KEY, true); - result.putString(Provider.KEY, provider_json.toString()); - result.putBoolean(ProviderItem.DANGER_ON, danger_on); + broadcast_progress(progress++); } - } catch (JSONException e) { - result.putBoolean(RESULT_KEY, false); - result.putString(ERRORS, "Corrupt download"); } - */ return result; } diff --git a/src/se/leap/bitmaskclient/ProviderListAdapter.java b/src/se/leap/bitmaskclient/ProviderListAdapter.java index bb1318fc..4abf15a2 100644 --- a/src/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/src/se/leap/bitmaskclient/ProviderListAdapter.java @@ -1,6 +1,5 @@ package se.leap.bitmaskclient; -import java.lang.reflect.Array; import java.util.List; import android.content.Context; diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 6a0f2a05..862177a2 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -202,8 +202,12 @@ public class ProviderListFragment extends ListFragment { content_adapter.notifyDataSetChanged(); } - public void hide(int position) { - content_adapter.hide(position); + public void hideAllBut(int position) { + for(int i = 0; i < content_adapter.getCount(); i++) + if(i != position) + content_adapter.hide(i); + else + content_adapter.unHide(i); } public void unhideAll() { -- cgit v1.2.3 From 6c46af29a115f930e5efa2411c2dbdc08a0702fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Fri, 18 Oct 2013 08:56:00 +0200 Subject: CA cert saved correctly in SharedPreferences. I/O error at login phase fixed. --- src/se/leap/bitmaskclient/ProviderAPI.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 27081df2..b23bdc7f 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -439,7 +439,7 @@ public class ProviderAPI extends IntentService { private boolean downloadCACert(String provider_main_url, boolean danger_on) { String cert_string = downloadWithCommercialCA(provider_main_url + "/ca.crt", danger_on); if(validCertificate(cert_string)) - ConfigHelper.saveSharedPref(Provider.CA_CERT, "-----BEGIN CERTIFICATE-----\n"+cert_string+"-----END CERTIFICATE-----"); + ConfigHelper.saveSharedPref(Provider.CA_CERT, cert_string); else return false; -- cgit v1.2.3 From 6c586abf63ea033fbc24451748f7103f54816625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 19 Nov 2013 18:02:43 +0100 Subject: Successfully rebased onto 0.2.2 --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index b35123b0..a4d8599f 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -142,10 +142,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn @Override public void onReceiveResult(int resultCode, Bundle resultData) { if(resultCode == ProviderAPI.PROVIDER_OK) { - JSONObject provider_json; - try { - provider_json = new JSONObject(resultData.getString(Provider.KEY)); - boolean danger_on = resultData.getBoolean(ProviderItem.DANGER_ON); mConfigState.setAction(PROVIDER_SET); if (ConfigHelper.getBoolFromSharedPref(EIP.ALLOWED_ANON)){ @@ -157,15 +153,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn progressbar_description.setVisibility(TextView.GONE); setResult(RESULT_OK); showProviderDetails(getCurrentFocus()); - } - } catch (JSONException e) { - e.printStackTrace(); - mProgressBar.setVisibility(ProgressBar.GONE); - progressbar_description.setVisibility(TextView.GONE); - //refreshProviderList(0); - //Toast.makeText(this, getResources().getString(R.string.config_error_parsing), Toast.LENGTH_LONG); - setResult(RESULT_CANCELED, mConfigState); - } + } } else if(resultCode == ProviderAPI.PROVIDER_NOK) { //refreshProviderList(0); String reason_to_fail = resultData.getString(ProviderAPI.ERRORS); -- cgit v1.2.3 From 4460a3cb0dfe10a32ce6697047c7331536233d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 15 Oct 2013 12:26:49 +0200 Subject: Download results stored in static variables. ProviderAPI has now 3 different static variables, each one for the 3 files that have to be downloaded to set up a provider. --- src/se/leap/bitmaskclient/ProviderAPI.java | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index b23bdc7f..bf3cf662 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -118,6 +118,11 @@ public class ProviderAPI extends IntentService { INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14 ; + public static boolean + CA_CERT_DOWNLOADED = false, + PROVIDER_JSON_DOWNLOADED = false, + EIP_SERVICE_JSON_DOWNLOADED = false + ; public ProviderAPI() { super("ProviderAPI"); Log.v("ClassName", "Provider API"); @@ -136,14 +141,14 @@ public class ProviderAPI extends IntentService { @Override protected void onHandleIntent(Intent command) { - final ResultReceiver receiver = command.getParcelableExtra("receiver"); + final ResultReceiver receiver = command.getParcelableExtra(RECEIVER_KEY); String action = command.getAction(); Bundle parameters = command.getBundleExtra(PARAMETERS); if(action.equalsIgnoreCase(SET_UP_PROVIDER)) { Bundle result = setUpProvider(parameters); if(result.getBoolean(RESULT_KEY)) { - receiver.send(PROVIDER_OK, result); + receiver.send(PROVIDER_OK, Bundle.EMPTY); } else { receiver.send(PROVIDER_NOK, Bundle.EMPTY); } @@ -424,15 +429,24 @@ public class ProviderAPI extends IntentService { int progress = 0; boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); String provider_main_url = task.getString(Provider.MAIN_URL); + + result.putBoolean(RESULT_KEY, false); if(downloadCACert(provider_main_url, danger_on)) { broadcast_progress(progress++); - result.putBoolean(RESULT_KEY, true); + CA_CERT_DOWNLOADED = true; if(getAndSetProviderJson(provider_main_url)) { broadcast_progress(progress++); - if(getAndSetEipServiceJson()) + PROVIDER_JSON_DOWNLOADED = true; + if(getAndSetEipServiceJson()) { broadcast_progress(progress++); + EIP_SERVICE_JSON_DOWNLOADED = true; + } } } + + if(CA_CERT_DOWNLOADED && PROVIDER_JSON_DOWNLOADED && EIP_SERVICE_JSON_DOWNLOADED) + result.putBoolean(RESULT_KEY, true); + return result; } -- cgit v1.2.3 From e89dc12bb93da0a638d9a199af4d91a54aa3556e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 22 Oct 2013 17:43:28 +0200 Subject: Save my work until now --- res/layout/client_dashboard.xml | 4 +- src/se/leap/bitmaskclient/ConfigurationWizard.java | 12 +++- src/se/leap/bitmaskclient/Dashboard.java | 3 + src/se/leap/bitmaskclient/ProviderAPI.java | 70 ++++++++++++---------- .../leap/bitmaskclient/ProviderListFragment.java | 1 - 5 files changed, 52 insertions(+), 38 deletions(-) diff --git a/res/layout/client_dashboard.xml b/res/layout/client_dashboard.xml index 16e152be..9bda6d2e 100644 --- a/res/layout/client_dashboard.xml +++ b/res/layout/client_dashboard.xml @@ -5,7 +5,7 @@ android:layout_height="match_parent" android:orientation="vertical" tools:context=".Dashboard" > - + - \ No newline at end of file + diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index a4d8599f..0cdd4561 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -189,7 +189,8 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn // resetOldConnection(); ProviderItem selected_provider = getProvider(id); int provider_index = getProviderIndex(id); - startProgressBar(provider_index); + + startProgressBar(provider_index+1); provider_list_fragment.hideAllBut(provider_index); setUpProvider(selected_provider.providerMainUrl(), true); } @@ -236,6 +237,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn return ""; } + private void startProgressBar() { + mProgressBar.setVisibility(ProgressBar.VISIBLE); + mProgressBar.setProgress(0); + mProgressBar.setMax(3); + } + private void startProgressBar(int list_item_index) { mProgressBar.setVisibility(ProgressBar.VISIBLE); progressbar_description.setVisibility(TextView.VISIBLE); @@ -253,8 +260,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn ProviderItem provider = providers_iterator.next(); if(provider.name().equalsIgnoreCase(id)) { break; - } - index++; + } else index++; } return index; } diff --git a/src/se/leap/bitmaskclient/Dashboard.java b/src/se/leap/bitmaskclient/Dashboard.java index 8c7c495d..ecac500e 100644 --- a/src/se/leap/bitmaskclient/Dashboard.java +++ b/src/se/leap/bitmaskclient/Dashboard.java @@ -166,6 +166,8 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf providerNameTV = (TextView) findViewById(R.id.providerName); providerNameTV.setText(provider.getDomain()); providerNameTV.setTextSize(28); + + mProgressBar = (ProgressBar) findViewById(R.id.eipProgress); FragmentManager fragMan = getFragmentManager(); if ( provider.hasEIP()){ @@ -224,6 +226,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf } eipStop(); } + //unregisterReceiver(providerAPI_broadcast_receiver_update); ConfigHelper.removeFromSharedPref(Provider.KEY); startActivityForResult(new Intent(this,ConfigurationWizard.class), SWITCH_PROVIDER); return true; diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index bf3cf662..0070b307 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -58,7 +58,6 @@ import org.apache.http.client.ClientProtocolException; import org.jboss.security.srp.SRPParameters; import org.json.JSONException; import org.json.JSONObject; -import org.json.JSONStringer; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; @@ -69,7 +68,6 @@ import android.os.Handler; import android.os.ResultReceiver; import android.util.Base64; import android.util.Log; -import android.widget.Toast; /** * Implements HTTP api methods used to manage communications with the provider server. @@ -97,8 +95,8 @@ public class ProviderAPI extends IntentService { SESSION_ID_COOKIE_KEY = "session_id_cookie_key", SESSION_ID_KEY = "session_id", ERRORS = "errors", - UPDATE_PROGRESSBAR = "update_action", - CURRENT_PROGRESS = "update data", + UPDATE_PROGRESSBAR = "update_progressbar", + CURRENT_PROGRESS = "current_progress", TAG = "provider_api_tag" ; @@ -123,6 +121,7 @@ public class ProviderAPI extends IntentService { PROVIDER_JSON_DOWNLOADED = false, EIP_SERVICE_JSON_DOWNLOADED = false ; + public ProviderAPI() { super("ProviderAPI"); Log.v("ClassName", "Provider API"); @@ -150,7 +149,7 @@ public class ProviderAPI extends IntentService { if(result.getBoolean(RESULT_KEY)) { receiver.send(PROVIDER_OK, Bundle.EMPTY); } else { - receiver.send(PROVIDER_NOK, Bundle.EMPTY); + receiver.send(PROVIDER_NOK, result); } } else if (action.equalsIgnoreCase(SRP_AUTH)) { Bundle session_id_bundle = authenticateBySRP(parameters); @@ -425,39 +424,43 @@ public class ProviderAPI extends IntentService { * @return a bundle with a boolean value mapped to a key named RESULT_KEY, and which is true if the update was successful. */ private Bundle setUpProvider(Bundle task) { - Bundle result = new Bundle(); int progress = 0; boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); String provider_main_url = task.getString(Provider.MAIN_URL); - result.putBoolean(RESULT_KEY, false); - if(downloadCACert(provider_main_url, danger_on)) { + Bundle current_download = downloadCACert(provider_main_url, danger_on); + if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { broadcast_progress(progress++); - CA_CERT_DOWNLOADED = true; - if(getAndSetProviderJson(provider_main_url)) { + //CA_CERT_DOWNLOADED = true; + current_download = getAndSetProviderJson(provider_main_url); + if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { broadcast_progress(progress++); - PROVIDER_JSON_DOWNLOADED = true; - if(getAndSetEipServiceJson()) { + //PROVIDER_JSON_DOWNLOADED = true; + current_download = getAndSetEipServiceJson(); + if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { broadcast_progress(progress++); - EIP_SERVICE_JSON_DOWNLOADED = true; + //EIP_SERVICE_JSON_DOWNLOADED = true; } } } - if(CA_CERT_DOWNLOADED && PROVIDER_JSON_DOWNLOADED && EIP_SERVICE_JSON_DOWNLOADED) - result.putBoolean(RESULT_KEY, true); - - return result; + return current_download; } - private boolean downloadCACert(String provider_main_url, boolean danger_on) { + private Bundle downloadCACert(String provider_main_url, boolean danger_on) { + Bundle result = new Bundle(); String cert_string = downloadWithCommercialCA(provider_main_url + "/ca.crt", danger_on); - if(validCertificate(cert_string)) + if(validCertificate(cert_string)) { ConfigHelper.saveSharedPref(Provider.CA_CERT, cert_string); - else - return false; + result.putBoolean(RESULT_KEY, true); + } else { + String reason_to_fail = cert_string; + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); + + } - return true; + return result; } private boolean validCertificate(String cert_string) { @@ -475,8 +478,8 @@ public class ProviderAPI extends IntentService { return result; } - private boolean getAndSetProviderJson(String provider_main_url) { - boolean result = false; + private Bundle getAndSetProviderJson(String provider_main_url) { + Bundle result = new Bundle(); String provider_dot_json_string = downloadWithProviderCA(provider_main_url + "/provider.json", true); @@ -487,28 +490,31 @@ public class ProviderAPI extends IntentService { ConfigHelper.saveSharedPref(Provider.KEY, provider_json); ConfigHelper.saveSharedPref(EIP.ALLOWED_ANON, provider_json.getJSONObject(Provider.SERVICE).getBoolean(EIP.ALLOWED_ANON)); - ProviderItem added_provider = new ProviderItem(name, provider_main_url); - - result = true; + + result.putBoolean(RESULT_KEY, true); } catch (JSONException e) { } return result; } - private boolean getAndSetEipServiceJson() { - boolean result = false; - + private Bundle getAndSetEipServiceJson() { + Bundle result = new Bundle(); + String eip_service_json_string = ""; try { JSONObject provider_json = ConfigHelper.getJsonFromSharedPref(Provider.KEY); String eip_service_url = provider_json.getString(Provider.API_URL) + "/" + provider_json.getString(Provider.API_VERSION) + "/" + EIP.SERVICE_API_PATH; - String eip_service_json_string = downloadWithProviderCA(eip_service_url, true); + eip_service_json_string = downloadWithProviderCA(eip_service_url, true); JSONObject eip_service_json = new JSONObject(eip_service_json_string); eip_service_json.getInt(Provider.API_RETURN_SERIAL); ConfigHelper.saveSharedPref(EIP.KEY, eip_service_json); - result = true; + + result.putBoolean(RESULT_KEY, true); } catch (JSONException e) { + String reason_to_fail = eip_service_json_string; + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); } return result; } diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 862177a2..76628f8c 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -19,7 +19,6 @@ import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import android.app.Activity; -import android.app.DialogFragment; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; -- cgit v1.2.3 From 0bcf72490f726825121e355afbeaf5b14636eb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 7 Nov 2013 10:43:03 +0100 Subject: Retry button on failed downloads. Next step: remove the provider from the list (big security hole, since all providers present on the list are assumed to be secure). --- res/values/strings.xml | 1 + src/se/leap/bitmaskclient/ConfigHelper.java | 3 ++ src/se/leap/bitmaskclient/ConfigurationWizard.java | 25 ++++++++++-- .../leap/bitmaskclient/DownloadFailedDialog.java | 27 ++++++++++++- src/se/leap/bitmaskclient/ProviderAPI.java | 46 ++++++++++++++++------ 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2598d452..747ce85e 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -11,6 +11,7 @@ Select Cancel OK + Retry No Data LZO Compression No Certificate diff --git a/src/se/leap/bitmaskclient/ConfigHelper.java b/src/se/leap/bitmaskclient/ConfigHelper.java index dd7049a7..6b49d7d7 100644 --- a/src/se/leap/bitmaskclient/ConfigHelper.java +++ b/src/se/leap/bitmaskclient/ConfigHelper.java @@ -168,6 +168,9 @@ public class ConfigHelper { return shared_preferences.getInt(shared_preferences_key, 0); } + protected static boolean sharedPrefContainsKey(String shared_preferences_key) { + return shared_preferences.contains(shared_preferences_key); + } /* * This method defaults to false. * If you use this method, be sure to fail-closed on false! diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 0cdd4561..f4409056 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -25,6 +25,11 @@ import org.json.JSONObject; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; import se.leap.bitmaskclient.ProviderListContent.ProviderItem; + +import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; +import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; +import se.leap.bitmaskclient.ProviderDetailFragment.ProviderDetailFragmentInterface; + import android.app.Activity; import android.app.DialogFragment; import android.app.Fragment; @@ -61,7 +66,7 @@ import android.widget.TextView; * */ public class ConfigurationWizard extends Activity -implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogInterface, ProviderDetailFragment.ProviderDetailFragmentInterface, Receiver { +implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderDetailFragmentInterface, DownloadFailedDialogInterface, Receiver { private ProgressBar mProgressBar; private TextView progressbar_description; @@ -192,7 +197,11 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn startProgressBar(provider_index+1); provider_list_fragment.hideAllBut(provider_index); - setUpProvider(selected_provider.providerMainUrl(), true); + + boolean danger_on = true; + if(ConfigHelper.sharedPrefContainsKey(ProviderItem.DANGER_ON)) + danger_on = ConfigHelper.getBoolFromSharedPref(ProviderItem.DANGER_ON); + setUpProvider(selected_provider.providerMainUrl(), danger_on); } @Override @@ -394,6 +403,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn } private void autoSelectProvider(String provider_main_url, boolean danger_on) { + ConfigHelper.saveSharedPref(ProviderItem.DANGER_ON, danger_on); onItemSelected(getId(provider_main_url)); } @@ -415,7 +425,16 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn startService(provider_API_command); } - + + public void retrySetUpProvider() { + Intent provider_API_command = new Intent(this, ProviderAPI.class); + + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + + startService(provider_API_command); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); diff --git a/src/se/leap/bitmaskclient/DownloadFailedDialog.java b/src/se/leap/bitmaskclient/DownloadFailedDialog.java index 3ce101a6..72455b2f 100644 --- a/src/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/src/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -17,6 +17,8 @@ package se.leap.bitmaskclient; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; +import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; @@ -47,7 +49,13 @@ public class DownloadFailedDialog extends DialogFragment { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(reason_to_fail) - .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { + .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.dismiss(); + interface_with_ConfigurationWizard.retrySetUpProvider(); + } + }) + .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } @@ -56,4 +64,21 @@ public class DownloadFailedDialog extends DialogFragment { // Create the AlertDialog object and return it return builder.create(); } + + public interface DownloadFailedDialogInterface { + public void retrySetUpProvider(); + } + + DownloadFailedDialogInterface interface_with_ConfigurationWizard; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_ConfigurationWizard = (DownloadFailedDialogInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement NoticeDialogListener"); + } + } } diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 0070b307..5d049d42 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -116,12 +116,15 @@ public class ProviderAPI extends IntentService { INCORRECTLY_DOWNLOADED_ANON_CERTIFICATE = 14 ; - public static boolean + private static boolean CA_CERT_DOWNLOADED = false, PROVIDER_JSON_DOWNLOADED = false, EIP_SERVICE_JSON_DOWNLOADED = false ; + static private String last_provider_main_url; + static private boolean last_danger_on = false; + public ProviderAPI() { super("ProviderAPI"); Log.v("ClassName", "Provider API"); @@ -425,21 +428,28 @@ public class ProviderAPI extends IntentService { */ private Bundle setUpProvider(Bundle task) { int progress = 0; - boolean danger_on = task.getBoolean(ProviderItem.DANGER_ON); - String provider_main_url = task.getString(Provider.MAIN_URL); + Bundle current_download = new Bundle(); + + if(task != null && task.containsKey(ProviderItem.DANGER_ON) && task.containsKey(Provider.MAIN_URL)) { + last_danger_on = task.getBoolean(ProviderItem.DANGER_ON); + last_provider_main_url = task.getString(Provider.MAIN_URL); + CA_CERT_DOWNLOADED = PROVIDER_JSON_DOWNLOADED = EIP_SERVICE_JSON_DOWNLOADED = false; + } - Bundle current_download = downloadCACert(provider_main_url, danger_on); - if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { + if(!CA_CERT_DOWNLOADED) + current_download = downloadCACert(last_provider_main_url, last_danger_on); + if(CA_CERT_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { broadcast_progress(progress++); - //CA_CERT_DOWNLOADED = true; - current_download = getAndSetProviderJson(provider_main_url); - if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { + CA_CERT_DOWNLOADED = true; + if(!PROVIDER_JSON_DOWNLOADED) + current_download = getAndSetProviderJson(last_provider_main_url); + if(PROVIDER_JSON_DOWNLOADED || (current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY))) { broadcast_progress(progress++); - //PROVIDER_JSON_DOWNLOADED = true; + PROVIDER_JSON_DOWNLOADED = true; current_download = getAndSetEipServiceJson(); if(current_download.containsKey(RESULT_KEY) && current_download.getBoolean(RESULT_KEY)) { broadcast_progress(progress++); - //EIP_SERVICE_JSON_DOWNLOADED = true; + EIP_SERVICE_JSON_DOWNLOADED = true; } } } @@ -457,15 +467,18 @@ public class ProviderAPI extends IntentService { String reason_to_fail = cert_string; result.putString(ERRORS, reason_to_fail); result.putBoolean(RESULT_KEY, false); - } return result; } + public static boolean caCertDownloaded() { + return CA_CERT_DOWNLOADED; + } + private boolean validCertificate(String cert_string) { boolean result = false; - if(!cert_string.isEmpty()) { + if(!ConfigHelper.checkErrorenousDownload(cert_string)) { X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(cert_string); try { Base64.encodeToString( certCert.getEncoded(), Base64.DEFAULT); @@ -498,6 +511,10 @@ public class ProviderAPI extends IntentService { } + public static boolean providerJsonDownloaded() { + return PROVIDER_JSON_DOWNLOADED; + } + private Bundle getAndSetEipServiceJson() { Bundle result = new Bundle(); String eip_service_json_string = ""; @@ -518,6 +535,11 @@ public class ProviderAPI extends IntentService { } return result; } + + public static boolean eipServiceDownloaded() { + return EIP_SERVICE_JSON_DOWNLOADED; + } + /** * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. * -- cgit v1.2.3 From 8abbfb678433c9033674db2c53070a01aa3a16a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 7 Nov 2013 11:09:40 +0100 Subject: Not added item is removed after canceling dialog. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 7 +++++++ src/se/leap/bitmaskclient/DownloadFailedDialog.java | 7 +++++++ src/se/leap/bitmaskclient/ProviderListAdapter.java | 8 ++++++++ src/se/leap/bitmaskclient/ProviderListFragment.java | 6 ++++++ 4 files changed, 28 insertions(+) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index f4409056..012a444c 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -477,6 +477,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD if(provider_list_fragment != null) provider_list_fragment.unhideAll(); } + + public void removeLastProviderItem() { + provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); + if(provider_list_fragment != null) { + provider_list_fragment.removeLastItem(); + } + } @Override public void login() { diff --git a/src/se/leap/bitmaskclient/DownloadFailedDialog.java b/src/se/leap/bitmaskclient/DownloadFailedDialog.java index 72455b2f..b759b1a3 100644 --- a/src/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/src/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -18,6 +18,7 @@ import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; +import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -57,6 +58,11 @@ public class DownloadFailedDialog extends DialogFragment { }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { + ConfigHelper.removeFromSharedPref(Provider.KEY); + ConfigHelper.removeFromSharedPref(ProviderItem.DANGER_ON); + ConfigHelper.removeFromSharedPref(EIP.ALLOWED_ANON); + ConfigHelper.removeFromSharedPref(EIP.KEY); + interface_with_ConfigurationWizard.removeLastProviderItem(); dialog.dismiss(); } }); @@ -67,6 +73,7 @@ public class DownloadFailedDialog extends DialogFragment { public interface DownloadFailedDialogInterface { public void retrySetUpProvider(); + public void removeLastProviderItem(); } DownloadFailedDialogInterface interface_with_ConfigurationWizard; diff --git a/src/se/leap/bitmaskclient/ProviderListAdapter.java b/src/se/leap/bitmaskclient/ProviderListAdapter.java index 4abf15a2..c5e8b64d 100644 --- a/src/se/leap/bitmaskclient/ProviderListAdapter.java +++ b/src/se/leap/bitmaskclient/ProviderListAdapter.java @@ -86,6 +86,14 @@ public class ProviderListAdapter extends ArrayAdapter { new_hidden[hidden.length] = false; hidden = new_hidden; } + + @Override + public void remove(T item) { + super.remove(item); + boolean[] new_hidden = new boolean[hidden.length-1]; + System.arraycopy(hidden, 0, new_hidden, 0, hidden.length-1); + hidden = new_hidden; + } @Override public View getView(int index, View convertView, ViewGroup parent) { diff --git a/src/se/leap/bitmaskclient/ProviderListFragment.java b/src/se/leap/bitmaskclient/ProviderListFragment.java index 76628f8c..53b6cb6d 100644 --- a/src/se/leap/bitmaskclient/ProviderListFragment.java +++ b/src/se/leap/bitmaskclient/ProviderListFragment.java @@ -201,6 +201,12 @@ public class ProviderListFragment extends ListFragment { content_adapter.notifyDataSetChanged(); } + public void removeLastItem() { + unhideAll(); + content_adapter.remove(content_adapter.getItem(content_adapter.getCount()-1)); + content_adapter.notifyDataSetChanged(); + } + public void hideAllBut(int position) { for(int i = 0; i < content_adapter.getCount(); i++) if(i != position) -- cgit v1.2.3 From 803134b94029160a5c885a2d93d78080b0aa8013 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 7 Nov 2013 11:26:44 +0100 Subject: Cleaner error message. --- src/se/leap/bitmaskclient/ProviderAPI.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 5d049d42..4dc1844a 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -464,7 +464,7 @@ public class ProviderAPI extends IntentService { ConfigHelper.saveSharedPref(Provider.CA_CERT, cert_string); result.putBoolean(RESULT_KEY, true); } else { - String reason_to_fail = cert_string; + String reason_to_fail = pickErrorMessage(cert_string); result.putString(ERRORS, reason_to_fail); result.putBoolean(RESULT_KEY, false); } @@ -506,6 +506,10 @@ public class ProviderAPI extends IntentService { result.putBoolean(RESULT_KEY, true); } catch (JSONException e) { + //TODO Error message should be contained in that provider_dot_json_string + String reason_to_fail = pickErrorMessage(provider_dot_json_string); + result.putString(ERRORS, reason_to_fail); + result.putBoolean(RESULT_KEY, false); } return result; } @@ -529,7 +533,7 @@ public class ProviderAPI extends IntentService { result.putBoolean(RESULT_KEY, true); } catch (JSONException e) { - String reason_to_fail = eip_service_json_string; + String reason_to_fail = pickErrorMessage(eip_service_json_string); result.putString(ERRORS, reason_to_fail); result.putBoolean(RESULT_KEY, false); } @@ -540,6 +544,18 @@ public class ProviderAPI extends IntentService { return EIP_SERVICE_JSON_DOWNLOADED; } + private String pickErrorMessage(String string_json_error_message) { + String error_message = ""; + try { + JSONObject json_error_message = new JSONObject(string_json_error_message); + error_message = json_error_message.getString(ERRORS); + } catch (JSONException e) { + // TODO Auto-generated catch block + error_message = string_json_error_message; + } + + return error_message; + } /** * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. * -- cgit v1.2.3 From 9ff9dc75f267bd6e339c026c4f3e73478210b0b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Thu, 7 Nov 2013 11:28:58 +0100 Subject: JavaDoc for pickErrorMessage --- src/se/leap/bitmaskclient/ProviderAPI.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 4dc1844a..e8f37c8a 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -544,6 +544,12 @@ public class ProviderAPI extends IntentService { return EIP_SERVICE_JSON_DOWNLOADED; } + /** + * Interprets the error message as a JSON object and extract the "errors" keyword pair. + * If the error message is not a JSON object, then it is returned untouched. + * @param string_json_error_message + * @return final error message + */ private String pickErrorMessage(String string_json_error_message) { String error_message = ""; try { @@ -556,6 +562,7 @@ public class ProviderAPI extends IntentService { return error_message; } + /** * Tries to download the contents of the provided url using commercially validated CA certificate from chosen provider. * -- cgit v1.2.3 From 6f8a6b145f2e699258963a8a1b8f555fc96f4ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Mon, 11 Nov 2013 18:57:02 +0100 Subject: Retry button prompts new provider dialog. The user can change the new provider's data entered before failing. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 36 +++++++++++++++++++--- .../leap/bitmaskclient/DownloadFailedDialog.java | 3 +- src/se/leap/bitmaskclient/NewProviderDialog.java | 10 +++++- src/se/leap/bitmaskclient/ProviderAPI.java | 12 ++++++-- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 012a444c..44d35370 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -83,7 +83,6 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD public ProviderAPIResultReceiver providerAPI_result_receiver; private ProviderAPIBroadcastReceiver_Update providerAPI_broadcast_receiver_update; - @Override protected void onCreate(Bundle savedInstanceState) { @@ -354,6 +353,24 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD newFragment.show(fragment_transaction, NewProviderDialog.TAG); } + /** + * Open the new provider dialog with data + */ + public void addAndSelectNewProvider(String main_url, boolean danger_on) { + FragmentTransaction fragment_transaction = getFragmentManager().beginTransaction(); + Fragment previous_new_provider_dialog = getFragmentManager().findFragmentByTag(NewProviderDialog.TAG); + if (previous_new_provider_dialog != null) { + fragment_transaction.remove(previous_new_provider_dialog); + } + + DialogFragment newFragment = NewProviderDialog.newInstance(); + Bundle data = new Bundle(); + data.putString(Provider.MAIN_URL, main_url); + data.putBoolean(ProviderItem.DANGER_ON, danger_on); + newFragment.setArguments(data); + newFragment.show(fragment_transaction, NewProviderDialog.TAG); + } + /** * Once selected a provider, this fragment offers the user to log in, * use it anonymously (if possible) @@ -422,9 +439,18 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); provider_API_command.putExtra(ProviderAPI.PARAMETERS, parameters); provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - - startService(provider_API_command); - } + + startService(provider_API_command); + } + + public void retrySetUpProvider() { + removeLastProviderItem(); + addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl(), ProviderAPI.lastDangerOn()); + /* + Intent provider_API_command = new Intent(this, ProviderAPI.class); + startService(provider_API_command); + */ + } public void retrySetUpProvider() { Intent provider_API_command = new Intent(this, ProviderAPI.class); @@ -478,7 +504,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD provider_list_fragment.unhideAll(); } - public void removeLastProviderItem() { + private void removeLastProviderItem() { provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); if(provider_list_fragment != null) { provider_list_fragment.removeLastItem(); diff --git a/src/se/leap/bitmaskclient/DownloadFailedDialog.java b/src/se/leap/bitmaskclient/DownloadFailedDialog.java index b759b1a3..4930da95 100644 --- a/src/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/src/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -62,7 +62,7 @@ public class DownloadFailedDialog extends DialogFragment { ConfigHelper.removeFromSharedPref(ProviderItem.DANGER_ON); ConfigHelper.removeFromSharedPref(EIP.ALLOWED_ANON); ConfigHelper.removeFromSharedPref(EIP.KEY); - interface_with_ConfigurationWizard.removeLastProviderItem(); + interface_with_ConfigurationWizard.retrySetUpProvider(); dialog.dismiss(); } }); @@ -73,7 +73,6 @@ public class DownloadFailedDialog extends DialogFragment { public interface DownloadFailedDialogInterface { public void retrySetUpProvider(); - public void removeLastProviderItem(); } DownloadFailedDialogInterface interface_with_ConfigurationWizard; diff --git a/src/se/leap/bitmaskclient/NewProviderDialog.java b/src/se/leap/bitmaskclient/NewProviderDialog.java index 98686bd2..5a20a355 100644 --- a/src/se/leap/bitmaskclient/NewProviderDialog.java +++ b/src/se/leap/bitmaskclient/NewProviderDialog.java @@ -16,7 +16,8 @@ */ package se.leap.bitmaskclient; -import se.leap.bitmaskclient.R; +import se.leap.leapclient.ProviderListContent.ProviderItem; +import se.leap.leapclient.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -71,7 +72,14 @@ public class NewProviderDialog extends DialogFragment { LayoutInflater inflater = getActivity().getLayoutInflater(); View new_provider_dialog_view = inflater.inflate(R.layout.new_provider_dialog, null); final EditText url_input_field = (EditText)new_provider_dialog_view.findViewById(R.id.new_provider_url); + if(getArguments() != null && getArguments().containsKey(Provider.MAIN_URL)) { + url_input_field.setText(getArguments().getString(Provider.MAIN_URL)); + } final CheckBox danger_checkbox = (CheckBox)new_provider_dialog_view.findViewById(R.id.danger_checkbox); + if(getArguments() != null && getArguments().containsKey(ProviderItem.DANGER_ON)) { + danger_checkbox.setActivated(getArguments().getBoolean(ProviderItem.DANGER_ON)); + } + builder.setView(new_provider_dialog_view) .setMessage(R.string.introduce_new_provider) .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() { diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index e8f37c8a..39dd978a 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -122,8 +122,8 @@ public class ProviderAPI extends IntentService { EIP_SERVICE_JSON_DOWNLOADED = false ; - static private String last_provider_main_url; - static private boolean last_danger_on = false; + private static String last_provider_main_url; + private static boolean last_danger_on = false; public ProviderAPI() { super("ProviderAPI"); @@ -137,6 +137,14 @@ public class ProviderAPI extends IntentService { CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER) ); } + public static String lastProviderMainUrl() { + return last_provider_main_url; + } + + public static boolean lastDangerOn() { + return last_danger_on; + } + private String formatErrorMessage(final int toast_string_id) { return "{ \"" + ERRORS + "\" : \""+getResources().getString(toast_string_id)+"\" }"; } -- cgit v1.2.3 From 39d1a866d042441bf5c05520203973cb3ff5d357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 12 Nov 2013 17:23:30 +0100 Subject: New provider dialog remembers previous data. It also (supposedly, I didn't test because Bitmask is working ok from our client point of view) distinguishes between cancelling a new custom provider and cancelling a preseeded provider. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 35 +++++++-------- .../leap/bitmaskclient/DownloadFailedDialog.java | 50 ++++++++++++---------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index 44d35370..e0a0d799 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -444,23 +444,19 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD } public void retrySetUpProvider() { - removeLastProviderItem(); - addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl(), ProviderAPI.lastDangerOn()); - /* - Intent provider_API_command = new Intent(this, ProviderAPI.class); - startService(provider_API_command); - */ - } + cancelSettingUpProvider(); + if(!ProviderAPI.caCertDownloaded()) { + addAndSelectNewProvider(ProviderAPI.lastProviderMainUrl(), ProviderAPI.lastDangerOn()); + } else { + Intent provider_API_command = new Intent(this, ProviderAPI.class); - public void retrySetUpProvider() { - Intent provider_API_command = new Intent(this, ProviderAPI.class); - - provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); - provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); - - startService(provider_API_command); - } + provider_API_command.setAction(ProviderAPI.SET_UP_PROVIDER); + provider_API_command.putExtra(ProviderAPI.RECEIVER_KEY, providerAPI_result_receiver); + startService(provider_API_command); + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.configuration_wizard_activity, menu); @@ -504,11 +500,16 @@ implements ProviderListFragment.Callbacks, NewProviderDialogInterface, ProviderD provider_list_fragment.unhideAll(); } - private void removeLastProviderItem() { + public void cancelSettingUpProvider() { provider_list_fragment = (ProviderListFragment) getFragmentManager().findFragmentByTag(ProviderListFragment.TAG); - if(provider_list_fragment != null) { + if(provider_list_fragment != null && ConfigHelper.sharedPrefContainsKey(ProviderItem.DANGER_ON)) { provider_list_fragment.removeLastItem(); } + + ConfigHelper.removeFromSharedPref(Provider.KEY); + ConfigHelper.removeFromSharedPref(ProviderItem.DANGER_ON); + ConfigHelper.removeFromSharedPref(EIP.ALLOWED_ANON); + ConfigHelper.removeFromSharedPref(EIP.KEY); } @Override diff --git a/src/se/leap/bitmaskclient/DownloadFailedDialog.java b/src/se/leap/bitmaskclient/DownloadFailedDialog.java index 4930da95..f78002b0 100644 --- a/src/se/leap/bitmaskclient/DownloadFailedDialog.java +++ b/src/se/leap/bitmaskclient/DownloadFailedDialog.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ - package se.leap.bitmaskclient; +package se.leap.bitmaskclient; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; @@ -45,46 +45,50 @@ public class DownloadFailedDialog extends DialogFragment { return dialog_fragment; } - @Override + @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - + builder.setMessage(reason_to_fail) .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); + dismiss(); interface_with_ConfigurationWizard.retrySetUpProvider(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - ConfigHelper.removeFromSharedPref(Provider.KEY); - ConfigHelper.removeFromSharedPref(ProviderItem.DANGER_ON); - ConfigHelper.removeFromSharedPref(EIP.ALLOWED_ANON); - ConfigHelper.removeFromSharedPref(EIP.KEY); - interface_with_ConfigurationWizard.retrySetUpProvider(); + interface_with_ConfigurationWizard.cancelSettingUpProvider(); dialog.dismiss(); } }); - + // Create the AlertDialog object and return it return builder.create(); } - + public interface DownloadFailedDialogInterface { - public void retrySetUpProvider(); - } + public void retrySetUpProvider(); + public void cancelSettingUpProvider(); + } DownloadFailedDialogInterface interface_with_ConfigurationWizard; - @Override - public void onAttach(Activity activity) { - super.onAttach(activity); - try { - interface_with_ConfigurationWizard = (DownloadFailedDialogInterface) activity; - } catch (ClassCastException e) { - throw new ClassCastException(activity.toString() - + " must implement NoticeDialogListener"); - } - } + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + interface_with_ConfigurationWizard = (DownloadFailedDialogInterface) activity; + } catch (ClassCastException e) { + throw new ClassCastException(activity.toString() + + " must implement NoticeDialogListener"); + } + } + + @Override + public void onCancel(DialogInterface dialog) { + interface_with_ConfigurationWizard.cancelSettingUpProvider(); + dialog.dismiss(); + } + } -- cgit v1.2.3 From 734758d983ed1dfd791ffed460bef988a0bcf09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 12 Nov 2013 19:33:22 +0100 Subject: Rebased onto current develop. --- src/se/leap/bitmaskclient/ConfigurationWizard.java | 7 ++++--- src/se/leap/bitmaskclient/EIP.java | 2 +- src/se/leap/bitmaskclient/NewProviderDialog.java | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/se/leap/bitmaskclient/ConfigurationWizard.java b/src/se/leap/bitmaskclient/ConfigurationWizard.java index e0a0d799..f6b93db4 100644 --- a/src/se/leap/bitmaskclient/ConfigurationWizard.java +++ b/src/se/leap/bitmaskclient/ConfigurationWizard.java @@ -22,9 +22,10 @@ import java.util.Iterator; import org.json.JSONException; import org.json.JSONObject; -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; -import se.leap.bitmaskclient.ProviderListContent.ProviderItem; + +import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.ProviderAPIResultReceiver.Receiver; +import se.leap.bitmaskclient.ProviderListContent.ProviderItem; import se.leap.bitmaskclient.DownloadFailedDialog.DownloadFailedDialogInterface; import se.leap.bitmaskclient.NewProviderDialog.NewProviderDialogInterface; diff --git a/src/se/leap/bitmaskclient/EIP.java b/src/se/leap/bitmaskclient/EIP.java index ec2f6972..6717aeae 100644 --- a/src/se/leap/bitmaskclient/EIP.java +++ b/src/se/leap/bitmaskclient/EIP.java @@ -58,10 +58,10 @@ import android.util.Log; */ public final class EIP extends IntentService { + public final static String AUTHED = "authed eip"; public final static String ACTION_START_EIP = "se.leap.bitmaskclient.START_EIP"; public final static String ACTION_STOP_EIP = "se.leap.bitmaskclient.STOP_EIP"; public final static String ACTION_UPDATE_EIP_SERVICE = "se.leap.bitmaskclient.UPDATE_EIP_SERVICE"; - public final static String AUTHED_EIP = "authed_eip"; public final static String ACTION_IS_EIP_RUNNING = "se.leap.bitmaskclient.IS_RUNNING"; public final static String EIP_NOTIFICATION = "EIP_NOTIFICATION"; public final static String ALLOWED_ANON = "allow_anonymous"; diff --git a/src/se/leap/bitmaskclient/NewProviderDialog.java b/src/se/leap/bitmaskclient/NewProviderDialog.java index 5a20a355..cf09c64b 100644 --- a/src/se/leap/bitmaskclient/NewProviderDialog.java +++ b/src/se/leap/bitmaskclient/NewProviderDialog.java @@ -16,8 +16,8 @@ */ package se.leap.bitmaskclient; -import se.leap.leapclient.ProviderListContent.ProviderItem; -import se.leap.leapclient.R; +import se.leap.bitmaskclient.ProviderListContent.ProviderItem; +import se.leap.bitmaskclient.R; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; -- cgit v1.2.3 From c26ba8d28bc4070c1945b228221d3e6b2158c485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 19 Nov 2013 19:27:23 +0100 Subject: Rebased onto 0.2.2 --- src/se/leap/bitmaskclient/Dashboard.java | 5 +++-- src/se/leap/bitmaskclient/ProviderAPI.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/se/leap/bitmaskclient/Dashboard.java b/src/se/leap/bitmaskclient/Dashboard.java index ecac500e..1307a232 100644 --- a/src/se/leap/bitmaskclient/Dashboard.java +++ b/src/se/leap/bitmaskclient/Dashboard.java @@ -112,6 +112,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf if ( resultCode == RESULT_OK ){ ConfigHelper.saveSharedPref(EIP.PARSED_SERIAL, 0); ConfigHelper.saveSharedPref(EIP.AUTHED_EIP, authed_eip); + startService( new Intent(EIP.ACTION_UPDATE_EIP_SERVICE) ); buildDashboard(); @@ -357,7 +358,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf setResult(RESULT_OK); authed_eip = true; - ConfigHelper.saveSharedPref(EIP.AUTHED_EIP, authed_eip); + ConfigHelper.saveSharedPref(EIP.AUTHED, authed_eip); invalidateOptionsMenu(); mProgressBar.setVisibility(ProgressBar.GONE); @@ -370,7 +371,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf mProgressBar.setVisibility(ProgressBar.GONE); } else if(resultCode == ProviderAPI.LOGOUT_SUCCESSFUL) { authed_eip = false; - ConfigHelper.saveSharedPref(EIP.AUTHED_EIP, authed_eip); + ConfigHelper.saveSharedPref(EIP.AUTHED, authed_eip); changeStatusMessage(resultCode); mProgressBar.setVisibility(ProgressBar.GONE); diff --git a/src/se/leap/bitmaskclient/ProviderAPI.java b/src/se/leap/bitmaskclient/ProviderAPI.java index 39dd978a..62a51c32 100644 --- a/src/se/leap/bitmaskclient/ProviderAPI.java +++ b/src/se/leap/bitmaskclient/ProviderAPI.java @@ -486,7 +486,7 @@ public class ProviderAPI extends IntentService { private boolean validCertificate(String cert_string) { boolean result = false; - if(!ConfigHelper.checkErrorenousDownload(cert_string)) { + if(!ConfigHelper.checkErroneousDownload(cert_string)) { X509Certificate certCert = ConfigHelper.parseX509CertificateFromString(cert_string); try { Base64.encodeToString( certCert.getEncoded(), Base64.DEFAULT); -- cgit v1.2.3 From 16ac5c300e21ce4c8b380013735f2c17d2e34768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 10 Dec 2013 18:49:44 +0100 Subject: Rebased onto 0.2.3. --- src/se/leap/bitmaskclient/Dashboard.java | 13 ++----------- src/se/leap/bitmaskclient/EIP.java | 2 +- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/se/leap/bitmaskclient/Dashboard.java b/src/se/leap/bitmaskclient/Dashboard.java index 1307a232..7880880c 100644 --- a/src/se/leap/bitmaskclient/Dashboard.java +++ b/src/se/leap/bitmaskclient/Dashboard.java @@ -358,7 +358,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf setResult(RESULT_OK); authed_eip = true; - ConfigHelper.saveSharedPref(EIP.AUTHED, authed_eip); + ConfigHelper.saveSharedPref(EIP.AUTHED_EIP, authed_eip); invalidateOptionsMenu(); mProgressBar.setVisibility(ProgressBar.GONE); @@ -371,7 +371,7 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf mProgressBar.setVisibility(ProgressBar.GONE); } else if(resultCode == ProviderAPI.LOGOUT_SUCCESSFUL) { authed_eip = false; - ConfigHelper.saveSharedPref(EIP.AUTHED, authed_eip); + ConfigHelper.saveSharedPref(EIP.AUTHED_EIP, authed_eip); changeStatusMessage(resultCode); mProgressBar.setVisibility(ProgressBar.GONE); @@ -469,13 +469,4 @@ public class Dashboard extends Activity implements LogInDialog.LogInDialogInterf startService(eip_intent); } - - public class ProviderAPIBroadcastReceiver_Update extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - int update = intent.getIntExtra(ProviderAPI.UPDATE_DATA, 0); - //mProgressBar.setProgress(update); - } - } } diff --git a/src/se/leap/bitmaskclient/EIP.java b/src/se/leap/bitmaskclient/EIP.java index 6717aeae..561f71af 100644 --- a/src/se/leap/bitmaskclient/EIP.java +++ b/src/se/leap/bitmaskclient/EIP.java @@ -58,7 +58,7 @@ import android.util.Log; */ public final class EIP extends IntentService { - public final static String AUTHED = "authed eip"; + public final static String AUTHED_EIP = "authed eip"; public final static String ACTION_START_EIP = "se.leap.bitmaskclient.START_EIP"; public final static String ACTION_STOP_EIP = "se.leap.bitmaskclient.STOP_EIP"; public final static String ACTION_UPDATE_EIP_SERVICE = "se.leap.bitmaskclient.UPDATE_EIP_SERVICE"; -- cgit v1.2.3