From 7f2bbe7d0228f8b678ebe3c7d628e97e2aced457 Mon Sep 17 00:00:00 2001 From: santimunin Date: Mon, 11 Jun 2012 18:27:17 +0000 Subject: [PATCH] 2012-06-11 Santiago Munin * src/pandroid_event_viewer/pandorafms/Core.java, src/pandroid_event_viewer/pandorafms/API.java: Moved api functions to API.java * src/pandroid_event_viewer/pandorafms/Options.java: Connection status works ok now. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6495 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- extras/pandroid_event_viewer/ChangeLog | 8 +- .../pandroid_event_viewer/pandorafms/API.java | 99 +++++++++++++++++++ .../pandorafms/Core.java | 60 ----------- .../pandorafms/CreateIncidentActivity.java | 2 +- .../pandorafms/EventList.java | 1 - .../pandorafms/Options.java | 18 ++-- 6 files changed, 119 insertions(+), 69 deletions(-) create mode 100644 extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java diff --git a/extras/pandroid_event_viewer/ChangeLog b/extras/pandroid_event_viewer/ChangeLog index 6fb2c60c38..30c3c65791 100644 --- a/extras/pandroid_event_viewer/ChangeLog +++ b/extras/pandroid_event_viewer/ChangeLog @@ -1,10 +1,16 @@ +2012-06-11 Santiago Munín + + * src/pandroid_event_viewer/pandorafms/Core.java, + src/pandroid_event_viewer/pandorafms/API.java: Moved api functions to API.java + * src/pandroid_event_viewer/pandorafms/Options.java: Connection status works ok now. + 2012-06-11 Santiago Munín * res/values/arrays.xml: Removed some entries. * res/values/strings.xml: Added one entry. * res/layout/create_incident.xml: Removed form fields. * res/layout/item_list_event_extended.xml: Added a button to create an incident from the event. - * src/pandroid_event_viewer/pandorafms/EventList.java: Added crate incident button logic. + * src/pandroid_event_viewer/pandorafms/EventList.java: Added create incident button logic. * src/pandroid_event_viewer/pandorafms/CreateIncidentActivity.java: Removed fields logic. * src/pandroid_event_viewer/pandorafms/Main.java, src/pandroid_event_viewer/pandorafms/EventList.java, diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java new file mode 100644 index 0000000000..5e22c45864 --- /dev/null +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/API.java @@ -0,0 +1,99 @@ +package pandroid_event_viewer.pandorafms; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; + +import android.content.Context; +import android.util.Log; + +public class API { + + private static String TAG = "API"; + + /** + * Get groups through an api call. + * + * @param context + * Application context. + * @return Map containing id -> group. + */ + public static Map getGroups(Context context) { + Map result = new HashMap(); + try { + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("op", "get")); + parameters.add(new BasicNameValuePair("op2", "groups")); + parameters.add(new BasicNameValuePair("other_mode", + "url_encode_separator_|")); + parameters.add(new BasicNameValuePair("return_type", "csv")); + parameters.add(new BasicNameValuePair("other", ";")); + + String return_api = Core.httpGet(context, parameters); + String[] lines = return_api.split("\n"); + + for (int i = 0; i < lines.length; i++) { + String[] groups = lines[i].split(";", 21); + result.put(Integer.valueOf(groups[0]), groups[1]); + } + } catch (Exception e) { + Log.e(TAG + ": getting groups", e.getMessage()); + } + return result; + } + + /** + * Get agents through an api call. + * + * @param context + * @return Map containing id -> agent. + */ + public static Map getAgents(Context context) { + Map result = new HashMap(); + try { + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("op", "get")); + parameters.add(new BasicNameValuePair("op2", "all_agents")); + parameters.add(new BasicNameValuePair("return_type", "csv")); + + String return_api = Core.httpGet(context, parameters); + String[] lines = return_api.split("\n"); + + for (int i = 0; i < lines.length; i++) { + String[] agents = lines[i].split(";"); + result.put(Integer.valueOf(agents[0]), agents[1]); + } + } catch (Exception e) { + Log.e(TAG + ": getting groups", e.getMessage()); + } + return result; + } + + /** + * Get API version. + * + * @param context + * Application context. + * @return API version or empty string if fails. + */ + public static String getVersion(Context context) { + try { + List parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("op", "get")); + parameters.add(new BasicNameValuePair("op2", "test")); + String return_api = Core.httpGet(context, parameters); + // TODO wait version + if (return_api.equals("OK\n")) { + return "4.0.2"; + } else { + return ""; + } + } catch (Exception e) { + return ""; + } + } +} \ No newline at end of file diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java index 886d3b92dc..d09c289089 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Core.java @@ -398,64 +398,4 @@ public class Core { image.setBounds(0, 0, size, size); view.setCompoundDrawables(image, null, null, null); } - - /** - * Get groups through an api call. - * - * @param context - * Application context. - * @return Map containing id -> group. - */ - public static Map getGroups(Context context) { - Map result = new HashMap(); - try { - List parameters = new ArrayList(); - parameters.add(new BasicNameValuePair("op", "get")); - parameters.add(new BasicNameValuePair("op2", "groups")); - parameters.add(new BasicNameValuePair("other_mode", - "url_encode_separator_|")); - parameters.add(new BasicNameValuePair("return_type", "csv")); - parameters.add(new BasicNameValuePair("other", ";")); - - String return_api = Core.httpGet(context, parameters); - String[] lines = return_api.split("\n"); - - for (int i = 0; i < lines.length; i++) { - String[] groups = lines[i].split(";", 21); - - result.put(Integer.valueOf(groups[0]), groups[1]); - } - } catch (Exception e) { - Log.e(TAG + ": getting groups", e.getMessage()); - } - return result; - } - - /** - * Get agents through an api call. - * - * @param context - * @return Map containing id -> agent. - */ - public static Map getAgents(Context context) { - Map result = new HashMap(); - try { - List parameters = new ArrayList(); - parameters.add(new BasicNameValuePair("op", "get")); - parameters.add(new BasicNameValuePair("op2", "all_agents")); - parameters.add(new BasicNameValuePair("return_type", "csv")); - - String return_api = Core.httpGet(context, parameters); - String[] lines = return_api.split("\n"); - - for (int i = 0; i < lines.length; i++) { - String[] agents = lines[i].split(";"); - - result.put(Integer.valueOf(agents[0]), agents[1]); - } - } catch (Exception e) { - Log.e(TAG + ": getting groups", e.getMessage()); - } - return result; - } } diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/CreateIncidentActivity.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/CreateIncidentActivity.java index 3343309dab..7f897a4dfe 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/CreateIncidentActivity.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/CreateIncidentActivity.java @@ -101,7 +101,7 @@ public class CreateIncidentActivity extends Activity { incidentParams[3] = String.valueOf(DEFAULT_PRIORITY_CODE); incidentParams[4] = String.valueOf(DEFAULT_STATUS_CODE); int groupCode = -1; - for (Entry entry : Core.getGroups( + for (Entry entry : API.getGroups( getApplicationContext()).entrySet()) { if (entry.getValue().equals(eventGroup)) { groupCode = entry.getKey(); diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventList.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventList.java index 207f0fe150..eb962886b2 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventList.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventList.java @@ -520,7 +520,6 @@ public class EventList extends ListActivity { @Override public void onClick(View v) { - // TODO Intent intent = new Intent( getBaseContext(), CreateIncidentActivity.class); diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java index a6792de8d8..0cd58f9e83 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Options.java @@ -257,22 +257,28 @@ public class Options extends Activity { * @author Santiago Munín González * */ - private class CheckConnectionAsyncTask extends AsyncTask { + private class CheckConnectionAsyncTask extends + AsyncTask { private boolean connectionOk = false; @Override - protected Void doInBackground(Void... arg0) { - // TODO implement check - this.connectionOk = false; - return null; + protected String doInBackground(Void... arg0) { + String version = API.getVersion(getApplicationContext()); + if (version.length() > 0) { + this.connectionOk = true; + } else { + this.connectionOk = false; + } + return version; } /** * Choose an image (ok or wrong) */ - protected void onPostExecute(Void v) { + protected void onPostExecute(String v) { if (this.connectionOk) { + connectionStatus.setText(v); connectionStatus.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.ok); } else {