From c01aa98a1d43ae0d6b56f0d13a8782dbb6a3e35e Mon Sep 17 00:00:00 2001 From: parmegv Date: Tue, 12 Feb 2013 19:33:19 +0100 Subject: New custom provider dialog effectively adds the new provider to the ProviderListFragment, and then the user can choose it. --- project.properties | 2 +- src/se/leap/leapclient/ConfigHelper.java | 29 ++++++++++++++---- src/se/leap/leapclient/ConfigurationWizard.java | 34 +++++++++++++++++----- src/se/leap/leapclient/ProviderAPI.java | 13 +++++++-- .../leap/leapclient/ProviderAPIResultReceiver.java | 30 +++++++++++++++++++ src/se/leap/leapclient/ProviderListContent.java | 27 +++++++++++++++-- src/se/leap/leapclient/ProviderListFragment.java | 10 ++++--- 7 files changed, 122 insertions(+), 23 deletions(-) create mode 100644 src/se/leap/leapclient/ProviderAPIResultReceiver.java diff --git a/project.properties b/project.properties index 895c9ce..c4f09d2 100644 --- a/project.properties +++ b/project.properties @@ -8,4 +8,4 @@ # project structure. # Project target. -target=android-16 +target=android-17 diff --git a/src/se/leap/leapclient/ConfigHelper.java b/src/se/leap/leapclient/ConfigHelper.java index c724a31..5301209 100644 --- a/src/se/leap/leapclient/ConfigHelper.java +++ b/src/se/leap/leapclient/ConfigHelper.java @@ -3,8 +3,11 @@ package se.leap.leapclient; import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import org.json.JSONException; import org.json.JSONObject; @@ -23,6 +26,9 @@ public class ConfigHelper { public static final String PREFERENCES_KEY = "LEAPPreferences"; public static final String user_directory = "leap_android"; public static String provider_key_url = "provider_main_url"; + final public static String eip_service_api_path = "/config/eip-service.json"; + + final public static int CUSTOM_PROVIDER_ADDED = 0; static void saveSharedPref(String shared_preferences_key, JSONObject content) { @@ -45,16 +51,16 @@ public class ConfigHelper { static void saveFile(String filename, String content) { File root = Environment.getExternalStorageDirectory(); - File outDir = new File(root.getAbsolutePath() + File.separator + user_directory); - if (!outDir.isDirectory()) { - outDir.mkdir(); + File leap_dir = new File(root.getAbsolutePath() + File.separator + user_directory); + if (!leap_dir.isDirectory()) { + leap_dir.mkdir(); } try { - if (!outDir.isDirectory()) { + if (!leap_dir.isDirectory()) { throw new IOException( "Unable to create directory " + user_directory + ". Maybe the SD card is mounted?"); } - File outputFile = new File(outDir, filename); + File outputFile = new File(leap_dir, filename); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile)); writer.write(content); writer.close(); @@ -62,4 +68,17 @@ public class ConfigHelper { Log.w("leap_android", e.getMessage(), e); } } + + static FileInputStream openFileInputStream(String filename) { + FileInputStream input_stream = null; + File root = Environment.getExternalStorageDirectory(); + File leap_dir = new File(root.getAbsolutePath() + File.separator + user_directory); + try { + input_stream = new FileInputStream(leap_dir + File.separator + filename); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return input_stream; + } } diff --git a/src/se/leap/leapclient/ConfigurationWizard.java b/src/se/leap/leapclient/ConfigurationWizard.java index 39ada31..422f996 100644 --- a/src/se/leap/leapclient/ConfigurationWizard.java +++ b/src/se/leap/leapclient/ConfigurationWizard.java @@ -8,7 +8,7 @@ import java.util.Scanner; import org.json.JSONException; import org.json.JSONObject; -import se.leap.leapclient.NewProviderDialog.NewProviderDialogInterface; +import se.leap.leapclient.ProviderAPIResultReceiver.Receiver; import se.leap.leapclient.ProviderListContent.ProviderItem; import android.app.Activity; import android.app.DialogFragment; @@ -19,6 +19,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.res.AssetManager; import android.os.Bundle; +import android.os.Handler; import android.view.View; @@ -39,18 +40,19 @@ import android.view.View; * to listen for item selections. */ public class ConfigurationWizard extends Activity - implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogInterface{ + implements ProviderListFragment.Callbacks, NewProviderDialog.NewProviderDialogInterface, Receiver { + /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. */ private boolean mTwoPane; - - private int mStackLevel; static SharedPreferences shared_preferences; + public ProviderAPIResultReceiver providerAPI_result_receiver; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -86,9 +88,10 @@ public class ConfigurationWizard extends Activity String provider_name = ""; for(String url_filepath : urls_filepaths) { + 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))); + ProviderListContent.addItem(new ProviderItem(provider_name, asset_manager.open(url_files_folder + "/" + url_filepath), custom)); } } catch (IOException e) { // TODO Auto-generated catch block @@ -115,7 +118,8 @@ public class ConfigurationWizard extends Activity if(current_provider_item.id.equalsIgnoreCase(id)) { try { - processAssetsFiles(current_provider_item); + if(current_provider_item.custom) + processAssetsFiles(current_provider_item); // TODO ask Provider class to save provider.json, setResult(OK), finish() to ConfigurationWizard downloadJSONFiles(current_provider_item); } catch (IOException e) { @@ -174,13 +178,29 @@ public class ConfigurationWizard extends Activity @Override public void saveProvider(String provider_url) { + providerAPI_result_receiver = new ProviderAPIResultReceiver(new Handler()); + providerAPI_result_receiver.setReceiver(this); + Intent provider_API_command = new Intent(this, ProviderAPI.class); Bundle method_and_parameters = new Bundle(); method_and_parameters.putString(ConfigHelper.provider_key_url, provider_url); provider_API_command.putExtra(ConfigHelper.downloadNewProviderDotJSON, method_and_parameters); - + provider_API_command.putExtra("receiver", providerAPI_result_receiver); startService(provider_API_command); } + + @Override + public void onReceiveResult(int resultCode, Bundle resultData) { + // TODO Auto-generated method stub + if(resultCode == ConfigHelper.CUSTOM_PROVIDER_ADDED){ + ProviderListFragment providerList = new ProviderListFragment(); + + FragmentManager fragmentManager = getFragmentManager(); + fragmentManager.beginTransaction() + .replace(R.id.configuration_wizard_layout, providerList, "providerlist") + .commit(); + } + } } diff --git a/src/se/leap/leapclient/ProviderAPI.java b/src/se/leap/leapclient/ProviderAPI.java index c21e17a..583eea0 100644 --- a/src/se/leap/leapclient/ProviderAPI.java +++ b/src/se/leap/leapclient/ProviderAPI.java @@ -10,13 +10,16 @@ import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONException; import org.json.JSONObject; +import se.leap.leapclient.ProviderListContent.ProviderItem; + import android.app.IntentService; import android.content.Intent; import android.os.Bundle; +import android.os.ResultReceiver; import android.util.Log; public class ProviderAPI extends IntentService { - + public ProviderAPI() { super("ProviderAPI"); Log.v("ClassName", "Provider API"); @@ -25,6 +28,7 @@ public class ProviderAPI extends IntentService { @Override protected void onHandleIntent(Intent task_for) { + final ResultReceiver receiver = task_for.getParcelableExtra("receiver"); Bundle task; System.out.println("onHandleIntent called"); if((task = task_for.getBundleExtra(ConfigHelper.downloadJsonFilesBundleExtra)) != null) { @@ -46,12 +50,16 @@ public class ProviderAPI extends IntentService { } } else if ((task = task_for.getBundleExtra(ConfigHelper.downloadNewProviderDotJSON)) != null) { + boolean custom = true; String provider_main_url = (String) task.get(ConfigHelper.provider_key_url); + String provider_name = provider_main_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_"); String provider_json_url = guessURL(provider_main_url); try { JSONObject provider_json = getJSONFromProvider(provider_json_url); - String filename = provider_json_url.replaceFirst("http[s]?://", "").replaceFirst("\\/", "_") + "_provider.json".replaceFirst("__", "_"); + String filename = provider_name + "_provider.json".replaceFirst("__", "_"); ConfigHelper.saveFile(filename, provider_json.toString()); + ProviderListContent.addItem(new ProviderItem(provider_name, ConfigHelper.openFileInputStream(filename), custom)); + receiver.send(ConfigHelper.CUSTOM_PROVIDER_ADDED, Bundle.EMPTY); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -84,5 +92,4 @@ public class ProviderAPI extends IntentService { String json_file_content = getStringFromProvider(json_url); return new JSONObject(json_file_content); } - } diff --git a/src/se/leap/leapclient/ProviderAPIResultReceiver.java b/src/se/leap/leapclient/ProviderAPIResultReceiver.java new file mode 100644 index 0000000..a6a8d9d --- /dev/null +++ b/src/se/leap/leapclient/ProviderAPIResultReceiver.java @@ -0,0 +1,30 @@ +package se.leap.leapclient; + +import android.os.Bundle; +import android.os.Handler; +import android.os.ResultReceiver; + +public class ProviderAPIResultReceiver extends ResultReceiver { + private Receiver mReceiver; + + public ProviderAPIResultReceiver(Handler handler) { + super(handler); + // TODO Auto-generated constructor stub + } + + public void setReceiver(Receiver receiver) { + mReceiver = receiver; + } + + public interface Receiver { + public void onReceiveResult(int resultCode, Bundle resultData); + } + + @Override + protected void onReceiveResult(int resultCode, Bundle resultData) { + if (mReceiver != null) { + mReceiver.onReceiveResult(resultCode, resultData); + } + } + +} diff --git a/src/se/leap/leapclient/ProviderListContent.java b/src/se/leap/leapclient/ProviderListContent.java index bf8bfa8..5fe206e 100644 --- a/src/se/leap/leapclient/ProviderListContent.java +++ b/src/se/leap/leapclient/ProviderListContent.java @@ -1,5 +1,6 @@ package se.leap.leapclient; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -35,7 +36,8 @@ public class ProviderListContent { /** * A dummy item representing a piece of content. */ - public static class ProviderItem { + public static class ProviderItem { + public boolean custom = false; public String id; public String name; public String provider_json_url; @@ -43,7 +45,6 @@ public class ProviderListContent { public String eip_service_json_url; public String cert_json_url; - public ProviderItem(String id, String name, String provider_json_url, String eip_service_json_url, String cert_json_url) { this.id = id; this.name = name; @@ -52,7 +53,7 @@ public class ProviderListContent { this.cert_json_url = cert_json_url; } - public ProviderItem(String name, InputStream urls_file_input_stream) { + public ProviderItem(String name, InputStream urls_file_input_stream, boolean custom) { try { byte[] urls_file_bytes = new byte[urls_file_input_stream.available()]; @@ -74,6 +75,26 @@ public class ProviderListContent { } } + public ProviderItem(String name, FileInputStream provider_json, boolean custom) { + + try { + byte[] urls_file_bytes = new byte[provider_json.available()]; + provider_json.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; + eip_service_json_url = (String) file_contents.get("api_uri") + ConfigHelper.eip_service_api_path; + cert_json_url = (String) file_contents.get("ca_cert_uri"); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + @Override public String toString() { return name; diff --git a/src/se/leap/leapclient/ProviderListFragment.java b/src/se/leap/leapclient/ProviderListFragment.java index bde3baa..4316e9f 100644 --- a/src/se/leap/leapclient/ProviderListFragment.java +++ b/src/se/leap/leapclient/ProviderListFragment.java @@ -1,5 +1,6 @@ package se.leap.leapclient; +import se.leap.leapclient.ProviderListContent.ProviderItem; import android.app.Activity; import android.app.DialogFragment; import android.app.Fragment; @@ -23,6 +24,8 @@ import android.widget.ListView; */ public class ProviderListFragment extends ListFragment { + private ArrayAdapter content_adapter; + /** * The serialization (saved instance state) Bundle key representing the * activated item position. Only used on tablets. @@ -72,13 +75,12 @@ public class ProviderListFragment extends ListFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - setListAdapter(new ArrayAdapter( + content_adapter = new ArrayAdapter( getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, - ProviderListContent.ITEMS)); - + ProviderListContent.ITEMS); + setListAdapter(content_adapter); } @Override -- cgit v1.2.3