From a3b2dcdead2577fcb4c6e332650e93ec19cbead0 Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Mon, 28 Oct 2013 14:49:21 +0000 Subject: [PATCH] Uploaded lost changes. git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8969 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- .../res/layout/options.xml | 16 ++- .../res/values-es/strings.xml | 1 + .../res/values-ja/strings.xml | 1 + .../res/values/strings.xml | 3 +- .../pandorafms/Core.java | 49 +++++-- .../pandorafms/Main.java | 125 ++++++++++++------ .../pandorafms/Options.java | 15 ++- 7 files changed, 146 insertions(+), 64 deletions(-) diff --git a/extras/pandroid_event_viewer/res/layout/options.xml b/extras/pandroid_event_viewer/res/layout/options.xml index 60eb9c0cc2..5962c874a4 100644 --- a/extras/pandroid_event_viewer/res/layout/options.xml +++ b/extras/pandroid_event_viewer/res/layout/options.xml @@ -155,8 +155,22 @@ + + - + +

Esta aplicación se usa para ver de forma autónoma y cómoda, el estado de los eventos resportados por un servidor de monitorización Pandora FMS. Desde esta aplicación podrá ver en tiempo real los eventos, e incluso validar o filtrarlos.

La configuración por defecto apunta al servidor de demo de Pandora FMS en firefly.artica.es. Cambie la configuración para que apunte a su propio servidor de Pandora FMS.

Conexión fallida - Vacia - + Tiempo de espera de las conexiones (en segundos) \ No newline at end of file diff --git a/extras/pandroid_event_viewer/res/values-ja/strings.xml b/extras/pandroid_event_viewer/res/values-ja/strings.xml index 5de6e8635b..1bf3580fca 100644 --- a/extras/pandroid_event_viewer/res/values-ja/strings.xml +++ b/extras/pandroid_event_viewer/res/values-ja/strings.xml @@ -76,4 +76,5 @@ 承諾済イベント Pandora FMS リアルタイムイベントビューワ - 空 - + Timeout connections (in seconds) \ No newline at end of file diff --git a/extras/pandroid_event_viewer/res/values/strings.xml b/extras/pandroid_event_viewer/res/values/strings.xml index cb2c4685ba..59fee665d4 100644 --- a/extras/pandroid_event_viewer/res/values/strings.xml +++ b/extras/pandroid_event_viewer/res/values/strings.xml @@ -82,7 +82,7 @@ Event validated Realtime event viewer for Pandora FMS. Welcome to Pandora FMS Event viewer for Android -

This app is used for to see confortable and standaralone the events status from the monitoring server of Pandora FMS. From this app you can see in realtime the events and validate or filter.

The configuration as default connecting with public demo of Pandora FMS in firefly.artica.es. Please change the configuration for to use your Pandora FMS server.

+

This app is used for to see comfortable and standaralone the events status from the monitoring server of Pandora FMS. From this app you can see in realtime the events and validate or filter.

The configuration as default connecting with public demo of Pandora FMS in firefly.artica.es. Please change the configuration for to use your Pandora FMS server.

Connection status Connection Notification @@ -122,5 +122,6 @@ Check in background Default profile can not be removed Connection failed + Timeout connections (in seconds) \ 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 a99e871c61..462a0af2d5 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 @@ -77,7 +77,6 @@ import android.widget.Toast; */ public class Core { private static String TAG = "Core"; - private static int CONNECTION_TIMEOUT = 10000; private static Map imgCache = new HashMap(); // Don't use this variable, just call getSocketFactory private static SSLSocketFactory sslSocketFactory; @@ -350,6 +349,7 @@ public class Core { String user = preferences.getString("user", ""); String password = preferences.getString("password", ""); String apiPassword = preferences.getString("api_password", ""); + int timeout_connections = preferences.getInt("timeout_connections", 10) * 1000; if (url.length() == 0 || user.length() == 0) { return ""; } @@ -363,13 +363,14 @@ public class Core { Log.i(TAG, "sent: " + url); if (url.toLowerCase().contains("https")) { // Secure connection - return Core.httpsGet(url, parameters); + return Core.httpsGet(context, url, parameters); } else { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, - CONNECTION_TIMEOUT); - HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT); + timeout_connections); + HttpConnectionParams.setSoTimeout(params, timeout_connections); + DefaultHttpClient httpClient = new DefaultHttpClient(params); UrlEncodedFormEntity entity; HttpPost httpPost; @@ -395,7 +396,7 @@ public class Core { * Image's url. * @return A bitmap of that image. */ - public static Bitmap downloadImage(String fileUrl) { + public static Bitmap downloadImage(Context context, String fileUrl) { if (imgCache.containsKey(fileUrl)) { Log.i(TAG, "Fetched from cache: " + fileUrl); return imgCache.get(fileUrl); @@ -403,6 +404,11 @@ public class Core { Log.i(TAG, "Downloading image: " + fileUrl); URL myFileUrl = null; + SharedPreferences preferences = context.getSharedPreferences( + context.getString(R.string.const_string_preferences), + Activity.MODE_PRIVATE); + int timeout_connections = preferences.getInt("timeout_connections", 10) * 1000; + try { myFileUrl = new URL(fileUrl); if (fileUrl.toLowerCase().contains("https")) { @@ -422,7 +428,7 @@ public class Core { HttpURLConnection conn = (HttpURLConnection) myFileUrl .openConnection(); conn.setDoInput(true); - conn.setConnectTimeout(CONNECTION_TIMEOUT); + conn.setConnectTimeout(timeout_connections); conn.connect(); InputStream is = conn.getInputStream(); Bitmap img = BitmapFactory.decodeStream(is); @@ -457,8 +463,8 @@ public class Core { * @param url * Image's url. */ - public static void setTextViewLeftImage(TextView view, String url) { - Drawable d = new BitmapDrawable(Core.downloadImage(url)); + public static void setTextViewLeftImage(Context context, TextView view, String url) { + Drawable d = new BitmapDrawable(Core.downloadImage(context, url)); setTextViewLeftImage(view, d, 16); } @@ -489,12 +495,17 @@ public class Core { * @throws IOException * If the given url is not accessible. */ - public static boolean isValidCertificate(URL url) { + public static boolean isValidCertificate(Context context, URL url) { HttpsURLConnection con; + SharedPreferences preferences = context.getSharedPreferences( + context.getString(R.string.const_string_preferences), + Activity.MODE_PRIVATE); + int timeout_connections = preferences.getInt("timeout_connections", 10) * 1000; + try { con = (HttpsURLConnection) url.openConnection(); - con.setConnectTimeout(CONNECTION_TIMEOUT); + con.setConnectTimeout(timeout_connections); con.connect(); con.disconnect(); @@ -511,7 +522,12 @@ public class Core { * @param url * @return boolean */ - public static boolean isOnline(URL url) { + public static boolean isOnline(Context context, URL url) { + SharedPreferences preferences = context.getSharedPreferences( + context.getString(R.string.const_string_preferences), + Activity.MODE_PRIVATE); + int timeout_connections = preferences.getInt("timeout_connections", 10) * 1000; + try { HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setHostnameVerifier(new HostnameVerifier() { @@ -519,7 +535,7 @@ public class Core { return true; } }); - con.setConnectTimeout(CONNECTION_TIMEOUT); + con.setConnectTimeout(timeout_connections); con.setSSLSocketFactory(getSocketFactory()); con.setDoOutput(true); con.getInputStream(); @@ -543,9 +559,14 @@ public class Core { * If there is any problem with connection. * */ - private static String httpsGet(String url, List parameters) + private static String httpsGet(Context context, String url, List parameters) throws IOException { + SharedPreferences preferences = context.getSharedPreferences( + context.getString(R.string.const_string_preferences), + Activity.MODE_PRIVATE); + int timeout_connections = preferences.getInt("timeout_connections", 10) * 1000; + String result = ""; HttpsURLConnection con; try { @@ -557,7 +578,7 @@ public class Core { }); con.setSSLSocketFactory(getSocketFactory()); con.setDoOutput(true); - con.setConnectTimeout(CONNECTION_TIMEOUT); + con.setConnectTimeout(timeout_connections); String postData = ""; boolean first = true; for (NameValuePair nameValuePair : parameters) { 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 f688ac6051..d00878d7b6 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 @@ -53,6 +53,7 @@ import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.Spinner; +import android.widget.SpinnerAdapter; import android.widget.Toast; /** @@ -68,30 +69,34 @@ public class Main extends Activity { private static String DEFAULT_PROFILE_NAME = "Default"; private static String DEFAULT_PROFILE = "0|3||||0|6"; private static String VERSION_4_0_2_LABEL = "v4.0.2"; - private PandroidEventviewerActivity object; - private HashMap pandoraGroups; + private PandroidEventviewerActivity object = null; + public Map pandoraGroups; + public List pandoraGroups_list; private Spinner comboSeverity; private List profiles; private Context context = this; private boolean selectLastProfile = false; // If this version, there will be changes private boolean version402 = false; + + public int id_group = -1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - + Intent i = getIntent(); this.object = (PandroidEventviewerActivity) i - .getSerializableExtra("object"); - + .getSerializableExtra("object"); + this.pandoraGroups = new HashMap(); final SharedPreferences preferences = getSharedPreferences( - this.getString(R.string.const_string_preferences), - Activity.MODE_PRIVATE); + this.getString(R.string.const_string_preferences), + Activity.MODE_PRIVATE); if (preferences.getString("api_version", "") - .equals(VERSION_4_0_2_LABEL)) { + .equals(VERSION_4_0_2_LABEL)) { + version402 = true; } @@ -111,7 +116,8 @@ public class Main extends Activity { buttonSetAsFilterWatcher.setEnabled(false); buttonSearch.setEnabled(false); buttonDeleteProfile.setEnabled(false); - } else if (object.user.equals("demo") || object.password.equals("demo")) { + } + else if (object.user.equals("demo") || object.password.equals("demo")) { Toast toast = Toast.makeText(this.getApplicationContext(), this.getString(R.string.preferences_set_demo_pandora_str), Toast.LENGTH_LONG); @@ -234,7 +240,8 @@ public class Main extends Activity { // Show advanced options? if (preferences.getBoolean("show_advanced", false)) { advancedOptions.setVisibility(View.VISIBLE); - } else { + } + else { advancedOptions.setVisibility(View.GONE); setAdvancedOptionsDefaults(); clearAdvancedOptions(); @@ -250,7 +257,8 @@ public class Main extends Activity { if (isChecked) { advancedOptions.setVisibility(View.VISIBLE); preferencesEditor.putBoolean("show_advanced", true); - } else { + } + else { advancedOptions.setVisibility(View.GONE); preferencesEditor.putBoolean("show_advanced", false); setAdvancedOptionsDefaults(); @@ -269,15 +277,19 @@ public class Main extends Activity { public void onResume() { super.onResume(); + Log.i(TAG, "Getting groups and tags"); - new GetGroupsAsyncTask().execute(); + GetGroupsAsyncTask task_group = new GetGroupsAsyncTask(); + task_group.execute(); + if (version402) { ((EditText) findViewById(R.id.tag_text)) .setVisibility(View.VISIBLE); ((ProgressBar) findViewById(R.id.loading_tag)) .setVisibility(View.GONE); ((Spinner) findViewById(R.id.tag)).setVisibility(View.GONE); - } else { + } + else { new GetTagsAsyncTask().execute(); } } @@ -289,14 +301,14 @@ public class Main extends Activity { * */ private class GetGroupsAsyncTask extends AsyncTask { - private List list; - + @Override protected Boolean doInBackground(Void... params) { - list = new ArrayList(); + pandoraGroups = new HashMap(); try { - list.addAll(API.getGroups(getApplicationContext()).values()); - } catch (IOException e) { + pandoraGroups = API.getGroups(getApplicationContext()); + } + catch (IOException e) { return false; } return true; @@ -308,12 +320,18 @@ public class Main extends Activity { loadingGroup.setVisibility(ProgressBar.GONE); if (result) { Spinner combo = (Spinner) findViewById(R.id.group_combo); - + + pandoraGroups_list = new ArrayList(); + pandoraGroups_list.addAll(pandoraGroups.values()); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter( getApplicationContext(), - android.R.layout.simple_spinner_item, list); + android.R.layout.simple_spinner_item, pandoraGroups_list); combo.setAdapter(spinnerArrayAdapter); - combo.setSelection(0); + + int index_combo = pandoraGroups_list.indexOf(pandoraGroups.get(object.id_group)); + + combo.setSelection(index_combo); Button buttonSaveAsFilterWatcher = (Button) findViewById(R.id.button_set_as_filter_watcher); Button buttonSearch = (Button) findViewById(R.id.button_send); @@ -324,7 +342,8 @@ public class Main extends Activity { buttonSearch.setEnabled(true); buttonDeleteProfile.setEnabled(true); buttonSaveProfile.setEnabled(true); - } else { + } + else { // Only this task will show the toast in order to prevent a // message repeated. Core.showConnectionProblemToast(getApplicationContext(), false); @@ -346,7 +365,8 @@ public class Main extends Activity { try { list = API.getTags(getApplicationContext()); return true; - } catch (IOException e) { + } + catch (IOException e) { return false; } } @@ -360,7 +380,16 @@ public class Main extends Activity { getApplicationContext(), android.R.layout.simple_spinner_item, list); combo.setAdapter(spinnerArrayAdapter); + + SpinnerAdapter adapter = (SpinnerAdapter)combo.getAdapter(); combo.setSelection(0); + for (int position = 0; position < adapter.getCount(); position++) { + String event_text = adapter.getItem(position).toString(); + + if (event_text.equals(object.eventTag)) { + combo.setSelection(position); + } + } } ProgressBar loadingGroup = (ProgressBar) findViewById(R.id.loading_tag); loadingGroup.setVisibility(ProgressBar.GONE); @@ -378,13 +407,13 @@ public class Main extends Activity { public boolean onOptionsItemSelected(MenuItem item) { Intent i; switch (item.getItemId()) { - case R.id.options_button_menu_options: - startActivity(new Intent(this, Options.class)); - break; - case R.id.about_button_menu_options: - i = new Intent(this, About.class); - startActivity(i); - break; + case R.id.options_button_menu_options: + startActivity(new Intent(this, Options.class)); + break; + case R.id.about_button_menu_options: + i = new Intent(this, About.class); + startActivity(i); + break; } return true; } @@ -403,28 +432,31 @@ public class Main extends Activity { timeKey = combo.getSelectedItemPosition(); this.object.timestamp = Core.convertMaxTimeOldEventValuesToTimestamp(0, - timeKey); + timeKey); EditText text = (EditText) findViewById(R.id.agent_name); this.object.agentNameStr = text.getText().toString(); this.object.id_group = 0; - + combo = (Spinner) findViewById(R.id.group_combo); if (combo.getSelectedItem() != null) { String selectedGroup = combo.getSelectedItem().toString(); Iterator> it = pandoraGroups.entrySet() .iterator(); + while (it.hasNext()) { Map.Entry e = (Map.Entry) it - .next(); + .next(); if (e.getValue().equals(selectedGroup)) { this.object.id_group = e.getKey(); } } } + + this.id_group = this.object.id_group; combo = (Spinner) findViewById(R.id.severity_combo); this.object.severity = combo.getSelectedItemPosition() - 1; @@ -437,7 +469,8 @@ public class Main extends Activity { if (version402) { text = (EditText) findViewById(R.id.tag_text); this.object.eventTag = text.getText().toString(); - } else { + } + else { combo = (Spinner) findViewById(R.id.tag); if (combo.getSelectedItem() != null) { this.object.eventTag = combo.getSelectedItem().toString(); @@ -492,7 +525,8 @@ public class Main extends Activity { if (version402) { text = (EditText) findViewById(R.id.tag_text); filterTag = text.getText().toString(); - } else { + } + else { combo = (Spinner) findViewById(R.id.tag); filterTag = combo.getSelectedItem().toString(); } @@ -519,7 +553,8 @@ public class Main extends Activity { this.getString(R.string.filter_update_succesful_str), Toast.LENGTH_SHORT); toast.show(); - } else { + } + else { Toast toast = Toast.makeText(getApplicationContext(), this.getString(R.string.filter_update_fail_str), Toast.LENGTH_SHORT); @@ -575,7 +610,8 @@ public class Main extends Activity { String tag = ""; if (version402) { tag = ((EditText) findViewById(R.id.tag_text)).getText().toString(); - } else { + } + else { tag = String.valueOf(((Spinner) findViewById(R.id.tag)) .getSelectedItemPosition()); } @@ -641,7 +677,8 @@ public class Main extends Activity { String profileData = ""; if (profileName.equals(DEFAULT_PROFILE_NAME)) { profileData = DEFAULT_PROFILE; - } else { + } + else { SharedPreferences preferences = getSharedPreferences( this.getString(R.string.const_string_preferences), Activity.MODE_PRIVATE); @@ -658,11 +695,13 @@ public class Main extends Activity { if (version402) { ((EditText) findViewById(R.id.tag_text)) .setText(options[2]); - } else { + } + else { spinner = (Spinner) findViewById(R.id.tag); try { spinner.setSelection(Integer.valueOf(options[2])); - } catch (NumberFormatException nf) { + } + catch (NumberFormatException nf) { spinner.setSelection(0); } } @@ -674,10 +713,12 @@ public class Main extends Activity { spinner.setSelection(Integer.valueOf(options[5])); spinner = (Spinner) findViewById(R.id.max_time_old_event_combo); spinner.setSelection(Integer.valueOf(options[6])); - } catch (NumberFormatException ne) { + } + catch (NumberFormatException ne) { Log.e(TAG, "NumberFormatException parsing profile"); return; - } catch (IndexOutOfBoundsException ie) { + } + catch (IndexOutOfBoundsException ie) { Log.e(TAG, "IndexOutOfBoundsException (maybe groups or tags are not correctly loaded)"); return; 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 c8bee9c99d..505772b069 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 @@ -78,13 +78,16 @@ public class Options extends Activity { // Connection EditText text = (EditText) findViewById(R.id.url); text.setText(preferences.getString("url", - "http://firefly.artica.es/pandora_demo")); + "http://192.168.70.133/pandora_console")); text = (EditText) findViewById(R.id.user); - text.setText(preferences.getString("user", "demo")); + text.setText(preferences.getString("user", "admin")); text = (EditText) findViewById(R.id.password); - text.setText(preferences.getString("password", "demo")); + text.setText(preferences.getString("password", "pandora")); text = (EditText) findViewById(R.id.api_password); - text.setText(preferences.getString("api_password", "doreik0")); + text.setText(preferences.getString("api_password", "")); + text = (EditText) findViewById(R.id.timeout_connections); + text.setText(Integer.toString(preferences.getInt("timeout_connections", 10))); + Spinner combo = (Spinner) findViewById(R.id.refresh_combo); ArrayAdapter adapter = ArrayAdapter.createFromResource( @@ -344,14 +347,14 @@ public class Options extends Activity { @Override protected Boolean doInBackground(URL... arg0) { url = arg0[0]; - online = Core.isOnline(url); + online = Core.isOnline(context, url); SharedPreferences preferences = getSharedPreferences( context.getString(R.string.const_string_preferences), Activity.MODE_PRIVATE); SharedPreferences.Editor editorPreferences = preferences.edit(); editorPreferences.putBoolean("online", online); editorPreferences.commit(); - return Core.isValidCertificate(arg0[0]); + return Core.isValidCertificate(context, arg0[0]); }