From 6ada10858719786e489938b3124d1d6015a70421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Parm=C3=A9nides=20GV?= Date: Tue, 25 Jun 2013 17:55:22 +0200 Subject: The added provider is selected automatically. I've also removed a lot of finishes when things go wrong (so that it should be returning to the previous activity/fragment). --- src/se/leap/leapclient/ConfigHelper.java | 1 + src/se/leap/leapclient/ConfigurationWizard.java | 82 ++++++++++++------------- src/se/leap/leapclient/NewProviderDialog.java | 4 +- src/se/leap/leapclient/ProviderAPI.java | 30 ++++++--- src/se/leap/leapclient/ProviderListContent.java | 10 ++- 5 files changed, 71 insertions(+), 56 deletions(-) (limited to 'src/se') diff --git a/src/se/leap/leapclient/ConfigHelper.java b/src/se/leap/leapclient/ConfigHelper.java index cab5fdee..9a3bd991 100644 --- a/src/se/leap/leapclient/ConfigHelper.java +++ b/src/se/leap/leapclient/ConfigHelper.java @@ -64,6 +64,7 @@ public class ConfigHelper { PREFERENCES_KEY = "LEAPPreferences", USER_DIRECTORY = "leap_android", PROVIDER_NAME = "provider_name", + PROVIDER_ID = "provider_id", PROVIDER_MAIN_URL = "provider_main_url", PROVIDER_JSON_URL = "provider_json_url", CUSTOM = "custom", diff --git a/src/se/leap/leapclient/ConfigurationWizard.java b/src/se/leap/leapclient/ConfigurationWizard.java index ce279425..6bf94ca6 100644 --- a/src/se/leap/leapclient/ConfigurationWizard.java +++ b/src/se/leap/leapclient/ConfigurationWizard.java @@ -50,9 +50,9 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); providerAPI_result_receiver.setReceiver(this); - + ConfigHelper.setSharedPreferences(getSharedPreferences(ConfigHelper.PREFERENCES_KEY, MODE_PRIVATE)); - + loadPreseededProviders(); // Only create our fragments if we're not restoring a saved instance @@ -72,15 +72,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn @Override public void onReceiveResult(int resultCode, Bundle resultData) { - if(resultCode == ConfigHelper.CUSTOM_PROVIDER_ADDED){ - ProviderListFragment providerList = new ProviderListFragment(); - - FragmentManager fragmentManager = getFragmentManager(); - fragmentManager.beginTransaction() - .replace(R.id.configuration_wizard_layout, providerList, "providerlist") - .commit(); - } - else if(resultCode == ConfigHelper.CORRECTLY_UPDATED_PROVIDER_DOT_JSON) { + if(resultCode == ConfigHelper.CORRECTLY_UPDATED_PROVIDER_DOT_JSON) { JSONObject provider_json; try { provider_json = new JSONObject(resultData.getString(ConfigHelper.PROVIDER_KEY)); @@ -88,10 +80,13 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn ConfigHelper.saveSharedPref(ConfigHelper.PROVIDER_KEY, provider_json); ConfigHelper.saveSharedPref(ConfigHelper.DANGER_ON, danger_on); ConfigHelper.saveSharedPref(ConfigHelper.ALLOWED_ANON, provider_json.getJSONObject(ConfigHelper.SERVICE_KEY).getBoolean(ConfigHelper.ALLOWED_ANON)); - mConfigState.setAction(PROVIDER_SET); + if(mProgressDialog == null) + mProgressDialog = ProgressDialog.show(this, getResources().getString(R.string.config_wait_title), getResources().getString(R.string.config_connecting_provider), true); mProgressDialog.setMessage(getResources().getString(R.string.config_downloading_services)); + if(mSelectedProvider == null) + mSelectedProvider = getProvider(resultData.getString(ConfigHelper.PROVIDER_ID)); downloadJSONFiles(mSelectedProvider); } catch (JSONException e) { // TODO Auto-generated catch block @@ -100,14 +95,12 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn mProgressDialog.dismiss(); Toast.makeText(this, getResources().getString(R.string.config_error_parsing), Toast.LENGTH_LONG); setResult(RESULT_CANCELED, mConfigState); - finish(); } } else if(resultCode == ConfigHelper.INCORRECTLY_UPDATED_PROVIDER_DOT_JSON) { mProgressDialog.dismiss(); Toast.makeText(getApplicationContext(), R.string.incorrectly_updated_provider_dot_json_message, Toast.LENGTH_LONG).show(); setResult(RESULT_CANCELED, mConfigState); - finish(); } else if(resultCode == ConfigHelper.CORRECTLY_DOWNLOADED_JSON_FILES) { if (ConfigHelper.getBoolFromSharedPref(ConfigHelper.ALLOWED_ANON)){ @@ -122,22 +115,20 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn } } else if(resultCode == ConfigHelper.INCORRECTLY_DOWNLOADED_JSON_FILES) { - Toast.makeText(getApplicationContext(), R.string.incorrectly_downloaded_json_files_message, Toast.LENGTH_LONG).show(); - setResult(RESULT_CANCELED, mConfigState); - finish(); + Toast.makeText(getApplicationContext(), R.string.incorrectly_downloaded_json_files_message, Toast.LENGTH_LONG).show(); + setResult(RESULT_CANCELED, mConfigState); } else if(resultCode == ConfigHelper.CORRECTLY_DOWNLOADED_CERTIFICATE) { mProgressDialog.dismiss(); Toast.makeText(getApplicationContext(), R.string.correctly_downloaded_json_files_message, Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG).show(); //mConfigState.putExtra(CERTIFICATE_RETRIEVED, true); // If this isn't the last step and finish() is moved... - setResult(RESULT_OK); - finish(); + setResult(RESULT_OK); + finish(); } else if(resultCode == ConfigHelper.INCORRECTLY_DOWNLOADED_CERTIFICATE) { mProgressDialog.dismiss(); Toast.makeText(getApplicationContext(), R.string.incorrectly_downloaded_certificate_message, Toast.LENGTH_LONG).show(); setResult(RESULT_CANCELED, mConfigState); - finish(); } } @@ -147,18 +138,23 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn */ @Override public void onItemSelected(String id) { - //TODO Code 2 pane view - Iterator preseeded_providers_iterator = ProviderListContent.ITEMS.iterator(); - while(preseeded_providers_iterator.hasNext()) - { - ProviderItem provider = preseeded_providers_iterator.next(); - if(provider.id.equalsIgnoreCase(id)) - { - mProgressDialog = ProgressDialog.show(this, getResources().getString(R.string.config_wait_title), getResources().getString(R.string.config_connecting_provider), true); - mSelectedProvider = provider; - saveProviderJson(mSelectedProvider); - } - } + //TODO Code 2 pane view + ProviderItem selected_provider = getProvider(id); + if(mProgressDialog == null) + mProgressDialog = ProgressDialog.show(this, getResources().getString(R.string.config_wait_title), getResources().getString(R.string.config_connecting_provider), true); + mSelectedProvider = selected_provider; + saveProviderJson(mSelectedProvider); + } + + private ProviderItem getProvider(String id) { + Iterator providers_iterator = ProviderListContent.ITEMS.iterator(); + while(providers_iterator.hasNext()) { + ProviderItem provider = providers_iterator.next(); + if(provider.id.equalsIgnoreCase(id)) { + return provider; + } + } + return null; } /** @@ -237,7 +233,7 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn provider_API_command.putExtra(ConfigHelper.DOWNLOAD_JSON_FILES_BUNDLE_EXTRA, method_and_parameters); provider_API_command.putExtra(ConfigHelper.RECEIVER_KEY, providerAPI_result_receiver); - + startService(provider_API_command); } @@ -261,20 +257,20 @@ implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogIn * Open the new provider dialog * @param view from which the dialog is showed */ - public void addNewProvider(View view) { + public void addAndSelectNewProvider(View view) { FragmentTransaction fragment_transaction = getFragmentManager().beginTransaction(); - Fragment previous_new_provider_dialog = getFragmentManager().findFragmentByTag(ConfigHelper.NEW_PROVIDER_DIALOG); - if (previous_new_provider_dialog != null) { - fragment_transaction.remove(previous_new_provider_dialog); - } - fragment_transaction.addToBackStack(null); - - DialogFragment newFragment = NewProviderDialog.newInstance(); - newFragment.show(fragment_transaction, ConfigHelper.NEW_PROVIDER_DIALOG); + Fragment previous_new_provider_dialog = getFragmentManager().findFragmentByTag(ConfigHelper.NEW_PROVIDER_DIALOG); + if (previous_new_provider_dialog != null) { + fragment_transaction.remove(previous_new_provider_dialog); + } + fragment_transaction.addToBackStack(null); + + DialogFragment newFragment = NewProviderDialog.newInstance(); + newFragment.show(fragment_transaction, ConfigHelper.NEW_PROVIDER_DIALOG); } @Override - public void saveProvider(String provider_main_url, boolean danger_on) { + public void saveAndSelectProvider(String provider_main_url, boolean danger_on) { Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle method_and_parameters = new Bundle(); diff --git a/src/se/leap/leapclient/NewProviderDialog.java b/src/se/leap/leapclient/NewProviderDialog.java index 678e805f..cf361719 100644 --- a/src/se/leap/leapclient/NewProviderDialog.java +++ b/src/se/leap/leapclient/NewProviderDialog.java @@ -22,7 +22,7 @@ import android.widget.Toast; public class NewProviderDialog extends DialogFragment { public interface NewProviderDialogInterface { - public void saveProvider(String url_provider, boolean danger_on); + public void saveAndSelectProvider(String url_provider, boolean danger_on); } NewProviderDialogInterface interface_with_ConfigurationWizard; @@ -64,7 +64,7 @@ public class NewProviderDialog extends DialogFragment { } boolean danger_on = danger_checkbox.isChecked(); if(validURL(entered_url)) { - interface_with_ConfigurationWizard.saveProvider(entered_url, danger_on); + interface_with_ConfigurationWizard.saveAndSelectProvider(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/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java index 72cc9b62..e7e3918c 100644 --- a/src/se/leap/leapclient/ProviderAPI.java +++ b/src/se/leap/leapclient/ProviderAPI.java @@ -91,8 +91,9 @@ public class ProviderAPI extends IntentService { } } else if ((task = task_for.getBundleExtra(ConfigHelper.DOWNLOAD_NEW_PROVIDER_DOTJSON)) != null) { - if(downloadNewProviderDotJSON(task)) { - receiver.send(ConfigHelper.CUSTOM_PROVIDER_ADDED, Bundle.EMPTY); + Bundle result = downloadNewProviderDotJSON(task); + if(result.getBoolean(ConfigHelper.RESULT_KEY)) { + receiver.send(ConfigHelper.CORRECTLY_UPDATED_PROVIDER_DOT_JSON, result); } else { receiver.send(ConfigHelper.INCORRECTLY_DOWNLOADED_JSON_FILES, Bundle.EMPTY); } @@ -287,7 +288,8 @@ public class ProviderAPI extends IntentService { * @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 boolean downloadNewProviderDotJSON(Bundle task) { + private Bundle downloadNewProviderDotJSON(Bundle task) { + Bundle result = new Bundle(); boolean custom = true; boolean danger_on = task.getBoolean(ConfigHelper.DANGER_ON); @@ -298,14 +300,26 @@ public class ProviderAPI extends IntentService { JSONObject provider_json; try { provider_json = getJSONFromProvider(provider_json_url, danger_on); - ProviderListContent.addItem(new ProviderItem(provider_name, provider_json_url, provider_json, custom, danger_on)); + if(provider_json == null) { + result.putBoolean(ConfigHelper.RESULT_KEY, false); + } else { + + ConfigHelper.saveSharedPref(ConfigHelper.PROVIDER_KEY, provider_json); + ConfigHelper.saveSharedPref(ConfigHelper.DANGER_ON, danger_on); + ConfigHelper.saveSharedPref(ConfigHelper.ALLOWED_ANON, provider_json.getJSONObject(ConfigHelper.SERVICE_KEY).getBoolean(ConfigHelper.ALLOWED_ANON)); + ProviderItem added_provider = new ProviderItem(provider_name, provider_json_url, provider_json, custom, danger_on); + ProviderListContent.addItem(added_provider); + + result.putString(ConfigHelper.PROVIDER_ID, added_provider.getId()); + result.putBoolean(ConfigHelper.RESULT_KEY, true); + result.putString(ConfigHelper.PROVIDER_KEY, provider_json.toString()); + result.putBoolean(ConfigHelper.DANGER_ON, danger_on); + } } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; + result.putBoolean(ConfigHelper.RESULT_KEY, false); } - return true; + return result; } /** diff --git a/src/se/leap/leapclient/ProviderListContent.java b/src/se/leap/leapclient/ProviderListContent.java index 991034b1..dc3d6ab3 100644 --- a/src/se/leap/leapclient/ProviderListContent.java +++ b/src/se/leap/leapclient/ProviderListContent.java @@ -114,8 +114,12 @@ public class ProviderListContent { } @Override - public String toString() { - return name; - } + public String toString() { + return name; + } + + public String getId() { + return id; + } } } -- cgit v1.2.3