From 3e2191e871490cabffbfafacb699c12c884af625 Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Wed, 21 Sep 2011 20:50:34 +0000 Subject: [PATCH] 2011-09-21 Miguel de Dios * src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java, src/pandroid_event_viewer/pandorafms/Main.java, src/pandroid_event_viewer/pandorafms/EventListItem.java, src/pandroid_event_viewer/pandorafms/EventList.java: wip, now the list events run fine, and get asynchronous events and added some checks. * res/values/strings.xml, res/values-en/strings.xml, res/values-es/strings.xml: added more strings. * res/layout/list_view_layout.xml, res/layout/item_list_event_layout.xml: changes for to show the buttons and messages. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@4978 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- extras/pandroid_event_viewer/ChangeLog | 15 + .../res/layout/item_list_event_layout.xml | 34 ++- .../res/layout/list_view_layout.xml | 15 + .../res/values-en/strings.xml | 2 + .../res/values-es/strings.xml | 2 + .../res/values/strings.xml | 2 + .../pandorafms/EventList.java | 281 +++++++++++------- .../pandorafms/EventListItem.java | 2 + .../pandorafms/Main.java | 39 ++- .../PandroidEventviewerActivity.java | 114 ++++++- 10 files changed, 381 insertions(+), 125 deletions(-) diff --git a/extras/pandroid_event_viewer/ChangeLog b/extras/pandroid_event_viewer/ChangeLog index 922c1d7c70..bfc614eae8 100644 --- a/extras/pandroid_event_viewer/ChangeLog +++ b/extras/pandroid_event_viewer/ChangeLog @@ -1,3 +1,18 @@ +2011-09-21 Miguel de Dios + + * src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java, + src/pandroid_event_viewer/pandorafms/Main.java, + src/pandroid_event_viewer/pandorafms/EventListItem.java, + src/pandroid_event_viewer/pandorafms/EventList.java: wip, now the list + events run fine, and get asynchronous events and added some checks. + + * res/values/strings.xml, res/values-en/strings.xml, + res/values-es/strings.xml: added more strings. + + * res/layout/list_view_layout.xml, + res/layout/item_list_event_layout.xml: changes for to show the buttons and + messages. + 2011-09-20 Miguel de Dios * res/values/strings.xml, res/values-en/strings.xml, diff --git a/extras/pandroid_event_viewer/res/layout/item_list_event_layout.xml b/extras/pandroid_event_viewer/res/layout/item_list_event_layout.xml index 0f8f71c0ad..00d7770866 100644 --- a/extras/pandroid_event_viewer/res/layout/item_list_event_layout.xml +++ b/extras/pandroid_event_viewer/res/layout/item_list_event_layout.xml @@ -22,7 +22,7 @@ android:id="@+id/item_linear_layout" > - + android:visibility="gone" + > + + + diff --git a/extras/pandroid_event_viewer/res/layout/list_view_layout.xml b/extras/pandroid_event_viewer/res/layout/list_view_layout.xml index 2dc062c8b2..29ffa78ee0 100644 --- a/extras/pandroid_event_viewer/res/layout/list_view_layout.xml +++ b/extras/pandroid_event_viewer/res/layout/list_view_layout.xml @@ -40,6 +40,21 @@ android:textStyle="bold" /> + + + Password Update Options + Load more events + Empty list events Update configuration successful. Update configuration is failed. diff --git a/extras/pandroid_event_viewer/res/values-es/strings.xml b/extras/pandroid_event_viewer/res/values-es/strings.xml index fa47e55465..a3403435d3 100644 --- a/extras/pandroid_event_viewer/res/values-es/strings.xml +++ b/extras/pandroid_event_viewer/res/values-es/strings.xml @@ -43,6 +43,8 @@ Contraseña Actualizar Opciones + Cargar mas eventos + Lista vacia de eventos Configuración guardada correctamente. Configuración no ha podido guardarse.. diff --git a/extras/pandroid_event_viewer/res/values/strings.xml b/extras/pandroid_event_viewer/res/values/strings.xml index 77f92378c2..ebc82b5f97 100644 --- a/extras/pandroid_event_viewer/res/values/strings.xml +++ b/extras/pandroid_event_viewer/res/values/strings.xml @@ -46,6 +46,8 @@ Password Update Options + Load more events + Empty list events Update configuration successful. Update configuration is failed. 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 2623a44356..897537a3b7 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 @@ -8,8 +8,10 @@ import java.net.URL; import java.util.HashMap; import android.app.ListActivity; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; @@ -21,9 +23,11 @@ import android.view.MenuInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; +import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; +import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; @@ -35,13 +39,16 @@ public class EventList extends ListActivity { public HashMap imgGroups; public HashMap imgSeverity; public HashMap imgType; + public String test; + + private BroadcastReceiver onBroadcast; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.openedItem = new HashMap(); + // this.openedItem = new HashMap(); this.imgGroups = new HashMap(); this.imgSeverity = new HashMap(); this.imgType = new HashMap(); @@ -58,11 +65,39 @@ public class EventList extends ListActivity { la = new MyAdapter(getBaseContext(), object); lv.setAdapter(la); + + test = "666"; + + onBroadcast = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + Log.e("onReceive", "onReceive"); + + int load_more = intent.getIntExtra("load_more", 0); + + if (load_more == 1) { + la.showLoadingEvents = false; + la.notifyDataSetChanged(); + } + else { + LinearLayout layout = (LinearLayout) findViewById(R.id.loading_layout); + layout.setVisibility(LinearLayout.GONE); + + if (object.count_events == 0) { + layout = (LinearLayout) findViewById(R.id.empty_list_layout); + layout.setVisibility(LinearLayout.VISIBLE); + } + } + } + }; } public void onResume() { super.onResume(); + registerReceiver(onBroadcast, new IntentFilter("eventlist.java")); + this.toggleLoadingLayout(); } @@ -174,83 +209,39 @@ public class EventList extends ListActivity { { super.onListItemClick(l, v, position, id); - Boolean opened = new Boolean(false); - if (this.openedItem.containsKey(new Integer(position))) { - opened = this.openedItem.get(new Integer(position)); - } - - LinearLayout itemLinearLayout = (LinearLayout)l.findViewById(R.id.item_linear_layout); - - if (!opened.booleanValue()) { - EventListItem item = this.object.eventList.get(position); - View view; - - LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - view = inflater.inflate(R.layout.item_list_event_extended, null); - - TextView text; - if (item.tags.length() != 0) { - text = (TextView)view.findViewById(R.id.tags_text); - text.setText(item.tags); - } - - if (item.user_comment.length() != 0) { - text = (TextView)view.findViewById(R.id.comments_text); - text.setText(item.user_comment); - } - - if (item.group_name.length() != 0) { - text = (TextView)view.findViewById(R.id.group_text); - text.setText(item.group_name); - this.setImageGroup(view, item.group_icon, R.id.img_group); - } - else { - //ALL - this.setImageGroup(view, "world", R.id.img_group); - } - - if (item.agent_name.length() != 0) { - View row = view.findViewById(R.id.row_agent); - row.setVisibility(View.VISIBLE); - - text = (TextView)view.findViewById(R.id.type_text); - text.setText(item.description_event); - - this.setImageType(view, item.description_image, R.id.img_type); - } - - if (item.criticity_name.length() != 0) { - text = (TextView)view.findViewById(R.id.severity_text); - text.setText(item.criticity_name); - - this.setImageType(view, item.criticity_image, R.id.img_severity); - } - Log.e("item", item.criticity_name); - - itemLinearLayout.addView(view); - } - else { - itemLinearLayout.removeViewAt(1); - } - opened = new Boolean(!opened.booleanValue()); - this.openedItem.put(new Integer(position), opened); + Log.e("onListItemClick", new Integer(position).toString()); + EventListItem item = this.object.eventList.get(position); + item.opened = !item.opened; + this.object.eventList.set(position, item); + la.notifyDataSetChanged(); } + + public void loadMoreEvents(View v) { + la.showLoadingEvents = true; + la.notifyDataSetChanged(); + + object.executeBackgroundGetEvents(); + } public class MyAdapter extends BaseAdapter { private Context mContext; public PandroidEventviewerActivity object; + public boolean showLoadingEvents; + public MyAdapter(Context c, PandroidEventviewerActivity object) { mContext = c; this.object = object; + + showLoadingEvents = false; } @Override public int getCount() { - return this.object.eventList.size(); + return this.object.eventList.size() + 1; } @Override @@ -264,60 +255,142 @@ public class EventList extends ListActivity { // TODO Auto-generated method stub return 0; } - + @Override public View getView(int position, View convertView, ViewGroup parent) { View view; - EventListItem item = this.object.eventList.get(position); + //The comment lines don't run fine, opened wrong the rows. - //OPTIMIZACIÓN PARA NO CREAR convertView - if (convertView == null) - { + //Optimization for not create convertView all times + //if (convertView == null) + //{ LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.item_list_event_layout, null); - } + /*} else { view = convertView; - } + }*/ - switch (item.criticity) { - - case 0://Blue - view.setBackgroundColor(Color.parseColor("#CDE2EA")); - break; - case 1://Grey - view.setBackgroundColor(Color.parseColor("#E4E4E4")); - break; - case 2://Green - view.setBackgroundColor(Color.parseColor("#BBFFA4")); - break; - case 3://Yellow - view.setBackgroundColor(Color.parseColor("#F4FFBF")); - break; - case 4://Red - view.setBackgroundColor(Color.parseColor("#FFC0B5")); - break; - default://Grey - view.setBackgroundColor(Color.parseColor("#E4E4E4")); - break; + //If the end of the list. + if (this.object.eventList.size() == position) { + //Show button to get more events + if ((!object.loadInProgress) && (object.count_events != 0)) { + if (showLoadingEvents) { + LinearLayout layout = (LinearLayout) view.findViewById(R.id.loading_more_events); + layout.setVisibility(LinearLayout.VISIBLE); + + RelativeLayout layout2 = (RelativeLayout) view.findViewById(R.id.content_event_item); + layout2.setVisibility(RelativeLayout.GONE); + + Button button = (Button)view.findViewById(R.id.button_load_more_events); + button.setVisibility(Button.GONE); + } + else { + Button button = (Button)view.findViewById(R.id.button_load_more_events); + button.setVisibility(Button.VISIBLE); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + object.offset += object.pagination; + loadMoreEvents(v); + } + }); + + RelativeLayout content_event_item = (RelativeLayout)view.findViewById(R.id.content_event_item); + content_event_item.setVisibility(RelativeLayout.GONE); + } + } + } + else { + EventListItem item = this.object.eventList.get(position); + + switch (item.criticity) { + + case 0://Blue + view.setBackgroundColor(Color.parseColor("#CDE2EA")); + break; + case 1://Grey + view.setBackgroundColor(Color.parseColor("#E4E4E4")); + break; + case 2://Green + view.setBackgroundColor(Color.parseColor("#BBFFA4")); + break; + case 3://Yellow + view.setBackgroundColor(Color.parseColor("#F4FFBF")); + break; + case 4://Red + view.setBackgroundColor(Color.parseColor("#FFC0B5")); + break; + default://Grey + view.setBackgroundColor(Color.parseColor("#E4E4E4")); + break; + } + + TextView titulo = (TextView)view.findViewById(R.id.agent_name); + + if (item.event_type.equals("system")) { + titulo.setText(R.string.system_str); + } + else { + titulo.setText(item.agent_name); + } + + TextView descripcion = (TextView)view.findViewById(R.id.event_name); + descripcion.setText(item.event); + + TextView timestamp = (TextView)view.findViewById(R.id.timestamp); + timestamp.setText(item.timestamp); + + //Show extended info + if (item.opened) { + View viewEventExtended; + viewEventExtended = inflater.inflate(R.layout.item_list_event_extended, null); + + TextView text; + if (item.tags.length() != 0) { + text = (TextView)viewEventExtended.findViewById(R.id.tags_text); + text.setText(item.tags); + } + + if (item.user_comment.length() != 0) { + text = (TextView)viewEventExtended.findViewById(R.id.comments_text); + text.setText(item.user_comment); + } + + if (item.group_name.length() != 0) { + text = (TextView)viewEventExtended.findViewById(R.id.group_text); + text.setText(item.group_name); + setImageGroup(viewEventExtended, item.group_icon, R.id.img_group); + } + else { + //ALL + setImageGroup(viewEventExtended, "world", R.id.img_group); + } + + if (item.agent_name.length() != 0) { + View row = viewEventExtended.findViewById(R.id.row_agent); + row.setVisibility(View.VISIBLE); + + text = (TextView)viewEventExtended.findViewById(R.id.type_text); + text.setText(item.description_event); + + setImageType(viewEventExtended, item.description_image, R.id.img_type); + } + + if (item.criticity_name.length() != 0) { + text = (TextView)viewEventExtended.findViewById(R.id.severity_text); + text.setText(item.criticity_name); + + setImageType(viewEventExtended, item.criticity_image, R.id.img_severity); + } + + LinearLayout itemLinearLayout = (LinearLayout)view.findViewById(R.id.item_linear_layout); + itemLinearLayout.addView(viewEventExtended); + } } - - TextView titulo = (TextView)view.findViewById(R.id.agent_name); - - if (item.event_type.equals("system")) { - titulo.setText(R.string.system_str); - } - else { - titulo.setText(item.agent_name); - } - - TextView descripcion = (TextView)view.findViewById(R.id.event_name); - descripcion.setText(item.event); - - TextView timestamp = (TextView)view.findViewById(R.id.timestamp); - timestamp.setText(item.timestamp); return view; } diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventListItem.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventListItem.java index 25dc1955d8..c344388b97 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventListItem.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/EventListItem.java @@ -22,4 +22,6 @@ public class EventListItem { public String description_image; public String criticity_name; public String criticity_image; + + public boolean opened; } diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java index 7b8463a1a3..23c6f8779a 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/Main.java @@ -2,8 +2,11 @@ package pandroid_event_viewer.pandorafms; import java.util.ArrayList; import java.util.Calendar; +import java.util.Date; import java.util.HashMap; +import java.util.Iterator; import java.util.List; +import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -16,8 +19,6 @@ import org.apache.http.message.BasicNameValuePair; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; -import android.content.SharedPreferences; -import android.graphics.Region.Op; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; @@ -197,6 +198,40 @@ public class Main extends Activity { this.object.eventList = new ArrayList(); this.object.loadInProgress = true; + //Get form data + DatePicker datePicker = (DatePicker)findViewById(R.id.date); + TimePicker timePicker = (TimePicker)findViewById(R.id.time); + int day = datePicker.getDayOfMonth(); + int month = datePicker.getMonth() + 1; + int year = datePicker.getYear(); + int hour = timePicker.getCurrentHour(); + int minute = timePicker.getCurrentMinute(); + Date date = new Date(year, month, day, minute, hour); + this.object.timestamp = date.getTime() / 1000; + + EditText agentName = (EditText) findViewById(R.id.agent_name); + String agentNameStr = agentName.getText().toString(); + + this.object.id_group = 0; + + Spinner combo; + int sel; + combo = (Spinner) findViewById(R.id.group_combo); + String selectedGroup = combo.getSelectedItem().toString(); + + Iterator it = pandoraGroups.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry e = (Map.Entry)it.next(); + + if (e.getValue().equals(selectedGroup)) { + this.object.id_group = e.getKey(); + } + } + + combo = (Spinner) findViewById(R.id.severity_combo); + this.object.severity = combo.getSelectedItemPosition(); + + this.object.executeBackgroundGetEvents(); TabActivity ta = (TabActivity) this.getParent(); diff --git a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java index 80fb639946..2675621a06 100644 --- a/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java +++ b/extras/pandroid_event_viewer/src/pandroid_event_viewer/pandorafms/PandroidEventviewerActivity.java @@ -20,6 +20,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Serializable; import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.apache.http.HttpEntity; @@ -41,12 +42,26 @@ import android.util.Log; import android.widget.TabHost; public class PandroidEventviewerActivity extends TabActivity implements Serializable { + //Data aplication public ArrayList eventList; + public long count_events; + + //Flags public boolean loadInProgress; public boolean getNewListEvents; + + //Configuration public String url; public String user; public String password; + + //Parameters to search in the API + public String agentNameStr; + public int id_group; + public long timestamp; + public int severity; + public int pagination; + public long offset; /** Called when the activity is first created. */ @Override @@ -62,6 +77,12 @@ public class PandroidEventviewerActivity extends TabActivity implements Serializ this.user = preferences.getString("user", ""); this.password = preferences.getString("password", ""); + this.timestamp = 1315015715; //= new Date().getTime(); + this.pagination = 20; + this.offset = 0; + this.agentNameStr = ""; + this.severity = -1; + this.eventList = new ArrayList(); this.loadInProgress = false; this.getNewListEvents = true; @@ -75,7 +96,9 @@ public class PandroidEventviewerActivity extends TabActivity implements Serializ startActivity(i); } - + else { + this.loadInProgress = true; + } executeBackgroundGetEvents(); @@ -108,33 +131,89 @@ public class PandroidEventviewerActivity extends TabActivity implements Serializ tabHost.getTabWidget().getChildAt(1).getLayoutParams().height=45; } + public String serializeParams2Api() { + String return_var = ""; + + return_var += ';'; //Separator for the csv + return_var += "|"; + return_var += Integer.toString(this.severity); //Criticity or severity + return_var += "|"; + return_var += this.agentNameStr; //The agent name + return_var += "|"; + return_var += ""; //Name of module + return_var += "|"; + return_var += ""; //Name of alert template + return_var += "|"; + return_var += ""; //Id user + return_var += "|"; + return_var += Long.toString(this.timestamp); //The minimun timestamp + return_var += "|"; + return_var += ""; //The maximum timestamp + return_var += "|"; + return_var += Integer.toString(this.pagination); //The pagination of list events + return_var += "|"; + return_var += Long.toString(this.offset); //The offset of list events + + return return_var; + } + public void getEvents(boolean newEvents) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); + UrlEncodedFormEntity entity; + HttpPost httpPost; + List parameters; + HttpResponse response; + HttpEntity entityResponse; + String return_api; - HttpPost httpPost = new HttpPost(this.url); + httpPost = new HttpPost(this.url); - List parameters = new ArrayList(); + //Get total count. + parameters = new ArrayList(); parameters.add(new BasicNameValuePair("user", this.user)); parameters.add(new BasicNameValuePair("pass", this.password)); parameters.add(new BasicNameValuePair("op", "get")); parameters.add(new BasicNameValuePair("op2", "events")); parameters.add(new BasicNameValuePair("other_mode", "url_encode_separator_|")); parameters.add(new BasicNameValuePair("return_type", "csv")); - parameters.add(new BasicNameValuePair("other", ";||||||1315015715||20|1")); - - UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters); - + parameters.add(new BasicNameValuePair("other", serializeParams2Api() + "|total")); + entity = new UrlEncodedFormEntity(parameters); httpPost.setEntity(entity); + response = httpClient.execute(httpPost); + entityResponse = response.getEntity(); + return_api = convertStreamToString(entityResponse.getContent()); + return_api = return_api.replace("\n", ""); + this.count_events = new Long(return_api).longValue(); - HttpResponse response = httpClient.execute(httpPost); - HttpEntity entityResponse = response.getEntity(); + if (this.count_events == 0) { + return; + } - String return_api = convertStreamToString(entityResponse.getContent()); + //Get the list of events. + parameters = new ArrayList(); + parameters.add(new BasicNameValuePair("user", this.user)); + parameters.add(new BasicNameValuePair("pass", this.password)); + parameters.add(new BasicNameValuePair("op", "get")); + parameters.add(new BasicNameValuePair("op2", "events")); + parameters.add(new BasicNameValuePair("other_mode", "url_encode_separator_|")); + parameters.add(new BasicNameValuePair("return_type", "csv")); + parameters.add(new BasicNameValuePair("other", serializeParams2Api())); + entity = new UrlEncodedFormEntity(parameters); + httpPost.setEntity(entity); + response = httpClient.execute(httpPost); + entityResponse = response.getEntity(); + + return_api = convertStreamToString(entityResponse.getContent()); + Log.e("return_api", return_api); String[] lines = return_api.split("\n"); + if (return_api.length() == 0) { + return; + } + for (int i= 0; i < lines.length; i++) { String[] items = lines[i].split(";", 21); @@ -201,6 +280,8 @@ public class PandroidEventviewerActivity extends TabActivity implements Serializ event.criticity_name = items[19]; event.criticity_image = items[20]; + event.opened = false; + this.eventList.add(event); } } @@ -258,10 +339,19 @@ public class PandroidEventviewerActivity extends TabActivity implements Serializ @Override protected void onPostExecute(Void unused) { - if (!isFinishing()) - { + Intent i = new Intent("eventlist.java"); + + if (getNewListEvents) { + loadInProgress = false; + getNewListEvents = false; + i.putExtra("load_more", 0); } + else { + i.putExtra("load_more", 1); + } + + getApplicationContext().sendBroadcast(i); } } } \ No newline at end of file