2012-09-13 Miguel de Dios <miguel.dedios@artica.es>

* res/layout/main.xml: removed the typo bug.
	
	* src/pandroid_event_viewer/pandorafms/Core.java, 
	src/pandroid_event_viewer/pandorafms/API.java,
	src/pandroid_event_viewer/pandorafms/About.java,
	src/pandroid_event_viewer/pandorafms/EventList.java: fixed the code
	style.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8942 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
mdtrooper 2013-10-21 12:00:42 +00:00
parent 1f4b99eaa9
commit fef54fd731
6 changed files with 329 additions and 284 deletions

View File

@ -1,3 +1,13 @@
2012-09-13 Miguel de Dios <miguel.dedios@artica.es>
* res/layout/main.xml: removed the typo bug.
* src/pandroid_event_viewer/pandorafms/Core.java,
src/pandroid_event_viewer/pandorafms/API.java,
src/pandroid_event_viewer/pandorafms/About.java,
src/pandroid_event_viewer/pandorafms/EventList.java: fixed the code
style.
2012-09-13 Miguel de Dios <miguel.dedios@artica.es> 2012-09-13 Miguel de Dios <miguel.dedios@artica.es>
* .classpath: removed the entry for ".svn" folder * .classpath: removed the entry for ".svn" folder

View File

@ -232,7 +232,6 @@
android:stretchColumns="0,1" > android:stretchColumns="0,1" >
<TableRow <TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" > android:layout_height="wrap_content" >
@ -250,7 +249,6 @@
</TableRow> </TableRow>
<TableRow <TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" > android:layout_height="wrap_content" >

View File

@ -15,9 +15,9 @@ import android.util.Log;
@SuppressLint("UseSparseArrays") @SuppressLint("UseSparseArrays")
public class API { public class API {
private static String TAG = "API"; private static String TAG = "API";
/** /**
* Get groups through an api call. * Get groups through an api call.
* *
@ -34,10 +34,10 @@ public class API {
parameters.add(new BasicNameValuePair("op", "get")); parameters.add(new BasicNameValuePair("op", "get"));
parameters.add(new BasicNameValuePair("op2", "groups")); parameters.add(new BasicNameValuePair("op2", "groups"));
parameters.add(new BasicNameValuePair("other_mode", parameters.add(new BasicNameValuePair("other_mode",
"url_encode_separator_|")); "url_encode_separator_|"));
parameters.add(new BasicNameValuePair("return_type", "csv")); parameters.add(new BasicNameValuePair("return_type", "csv"));
parameters.add(new BasicNameValuePair("other", ";")); parameters.add(new BasicNameValuePair("other", ";"));
String return_api = Core.httpGet(context, parameters); String return_api = Core.httpGet(context, parameters);
String[] lines = return_api.split("\n"); String[] lines = return_api.split("\n");
try { try {
@ -45,12 +45,13 @@ public class API {
String[] groups = lines[i].split(";", 21); String[] groups = lines[i].split(";", 21);
result.put(Integer.valueOf(groups[0]), groups[1]); result.put(Integer.valueOf(groups[0]), groups[1]);
} }
} catch (NumberFormatException e) { }
catch (NumberFormatException e) {
Log.e(TAG, "Problem parsing number in response"); Log.e(TAG, "Problem parsing number in response");
} }
return result; return result;
} }
/** /**
* Get agents through an api call. * Get agents through an api call.
* *
@ -60,23 +61,24 @@ public class API {
* If there is a problem with the connection. * If there is a problem with the connection.
*/ */
public static Map<Integer, String> getAgents(Context context) public static Map<Integer, String> getAgents(Context context)
throws IOException { throws IOException {
Map<Integer, String> result = new HashMap<Integer, String>(); Map<Integer, String> result = new HashMap<Integer, String>();
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("op", "get")); parameters.add(new BasicNameValuePair("op", "get"));
parameters.add(new BasicNameValuePair("op2", "all_agents")); parameters.add(new BasicNameValuePair("op2", "all_agents"));
parameters.add(new BasicNameValuePair("return_type", "csv")); parameters.add(new BasicNameValuePair("return_type", "csv"));
String return_api = Core.httpGet(context, parameters); String return_api = Core.httpGet(context, parameters);
String[] lines = return_api.split("\n"); String[] lines = return_api.split("\n");
for (int i = 0; i < lines.length; i++) { for (int i = 0; i < lines.length; i++) {
String[] agents = lines[i].split(";"); String[] agents = lines[i].split(";");
result.put(Integer.valueOf(agents[0]), agents[1]); result.put(Integer.valueOf(agents[0]), agents[1]);
} }
return result; return result;
} }
/** /**
* Get API version. * Get API version.
* *
@ -96,14 +98,16 @@ public class API {
String[] lines = return_api.split(","); String[] lines = return_api.split(",");
if (lines.length == 3) { if (lines.length == 3) {
return lines[1]; return lines[1];
} else { }
else {
return context.getString(R.string.unknown_version); return context.getString(R.string.unknown_version);
} }
} else { }
else {
return ""; return "";
} }
} }
/** /**
* Performs a get_events API call. * Performs a get_events API call.
* *
@ -144,16 +148,16 @@ public class API {
parameters.add(new BasicNameValuePair("op", "get")); parameters.add(new BasicNameValuePair("op", "get"));
parameters.add(new BasicNameValuePair("op2", "events")); parameters.add(new BasicNameValuePair("op2", "events"));
parameters.add(new BasicNameValuePair("other_mode", parameters.add(new BasicNameValuePair("other_mode",
"url_encode_separator_|")); "url_encode_separator_|"));
parameters.add(new BasicNameValuePair("return_type", "csv")); parameters.add(new BasicNameValuePair("return_type", "csv"));
parameters.add(new BasicNameValuePair("other", parameters.add(new BasicNameValuePair("other",
serializeEventsParamsToAPI(filterAgentName, idGroup, serializeEventsParamsToAPI(filterAgentName, idGroup,
filterSeverity, filterStatus, filterEventSearch, filterSeverity, filterStatus, filterEventSearch,
filterTag, filterTimestamp, itemsPerPage, offset, filterTag, filterTimestamp, itemsPerPage, offset, total,
total, more_criticity))); more_criticity)));
return Core.httpGet(context, parameters); return Core.httpGet(context, parameters);
} }
/** /**
* Get tags through an api call. * Get tags through an api call.
* *
@ -176,12 +180,13 @@ public class API {
String[] tags = lines[i].split(";", 2); String[] tags = lines[i].split(";", 2);
array.add(tags[1]); array.add(tags[1]);
} }
} catch (ArrayIndexOutOfBoundsException e) { }
catch (ArrayIndexOutOfBoundsException e) {
Log.e(TAG, "There was a problem getting tags: " + e.getMessage()); Log.e(TAG, "There was a problem getting tags: " + e.getMessage());
} }
return array; return array;
} }
/** /**
* Creates new incident in console. * Creates new incident in console.
* *
@ -195,14 +200,15 @@ public class API {
*/ */
public static void createNewIncident(Context context, public static void createNewIncident(Context context,
String[] incidentParameters) throws IOException { String[] incidentParameters) throws IOException {
Log.i(TAG, "Sending new incident"); Log.i(TAG, "Sending new incident");
List<NameValuePair> parameters = new ArrayList<NameValuePair>(); List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("op", "set")); parameters.add(new BasicNameValuePair("op", "set"));
parameters.add(new BasicNameValuePair("op2", "new_incident")); parameters.add(new BasicNameValuePair("op2", "new_incident"));
parameters.add(new BasicNameValuePair("other_mode", parameters.add(new BasicNameValuePair("other_mode",
"url_encode_separator_|")); "url_encode_separator_|"));
parameters.add(new BasicNameValuePair("other", Core parameters.add(new BasicNameValuePair("other", Core
.serializeParams2Api(incidentParameters))); .serializeParams2Api(incidentParameters)));
Core.httpGet(context, parameters); Core.httpGet(context, parameters);
} }
@ -221,23 +227,25 @@ public class API {
*/ */
public static boolean validateEvent(Context context, int idEvent, public static boolean validateEvent(Context context, int idEvent,
String comment) throws IOException { String comment) throws IOException {
List<NameValuePair> parameters; List<NameValuePair> parameters;
// Set event validation. // Set event validation.
parameters = new ArrayList<NameValuePair>(); parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("op", "set")); parameters.add(new BasicNameValuePair("op", "set"));
parameters.add(new BasicNameValuePair("op2", "validate_events")); parameters.add(new BasicNameValuePair("op2", "validate_events"));
parameters.add(new BasicNameValuePair("id", Integer.valueOf(idEvent) parameters.add(new BasicNameValuePair("id", Integer.valueOf(idEvent)
.toString())); .toString()));
parameters.add(new BasicNameValuePair("other", comment)); parameters.add(new BasicNameValuePair("other", comment));
String return_api = Core.httpGet(context, parameters); String return_api = Core.httpGet(context, parameters);
if (return_api.startsWith("Correct validation")) { if (return_api.startsWith("Correct validation")) {
return true; return true;
} else { }
else {
return false; return false;
} }
} }
/** /**
* Serialize parameters to api. * Serialize parameters to api.
* *
@ -266,17 +274,17 @@ public class API {
* @return Serialized parameters. * @return Serialized parameters.
*/ */
private static String serializeEventsParamsToAPI(String filterAgentName, private static String serializeEventsParamsToAPI(String filterAgentName,
int idGroup, int filterSeverity, int filterStatus, int idGroup, int filterSeverity, int filterStatus,
String filterEventSearch, String filterTag, long filterTimestamp, String filterEventSearch, String filterTag, long filterTimestamp,
long itemsPerPage, long offset, boolean total, long itemsPerPage, long offset, boolean total,
boolean more_criticity) { boolean more_criticity) {
String totalStr = (total) ? "total" : "-1"; String totalStr = (total) ? "total" : "-1";
if (more_criticity) { if (more_criticity) {
totalStr = "more_criticity"; totalStr = "more_criticity";
} }
return Core.serializeParams2Api(new String[] { ";", // Separator for the return Core.serializeParams2Api(new String[] {
// csv ";", // Separator for the csv
Integer.toString(filterSeverity), // criticity or severity Integer.toString(filterSeverity), // criticity or severity
filterAgentName, // The agent name filterAgentName, // The agent name
"", // Name of module "", // Name of module

View File

@ -32,37 +32,38 @@ import android.widget.TextView;
* *
*/ */
public class About extends Activity { public class About extends Activity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.info); setContentView(R.layout.info);
TextView text = (TextView) findViewById(R.id.url_pandora); TextView text = (TextView) findViewById(R.id.url_pandora);
text.setText(Html text.setText(Html
.fromHtml("<a href='http://pandorafms.org/'>PandoraFMS.org</a>")); .fromHtml("<a href='http://pandorafms.org/'>PandoraFMS.org</a>"));
text.setMovementMethod(LinkMovementMethod.getInstance()); text.setMovementMethod(LinkMovementMethod.getInstance());
CheckBox check_show = (CheckBox) findViewById(R.id.dont_show_again_extended); CheckBox check_show = (CheckBox) findViewById(R.id.dont_show_again_extended);
check_show.setOnClickListener(new View.OnClickListener() { check_show.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
CheckBox check_show = (CheckBox) v; CheckBox check_show = (CheckBox) v;
if (check_show.isChecked()) { if (check_show.isChecked()) {
SharedPreferences preferences = getSharedPreferences( SharedPreferences preferences = getSharedPreferences(
getString(R.string.const_string_preferences), getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE); Activity.MODE_PRIVATE);
SharedPreferences.Editor editorPreferences = preferences SharedPreferences.Editor editorPreferences = preferences
.edit(); .edit();
editorPreferences.putBoolean("show_popup_info", false); editorPreferences.putBoolean("show_popup_info", false);
editorPreferences.commit(); editorPreferences.commit();
} else { }
else {
SharedPreferences preferences = getSharedPreferences( SharedPreferences preferences = getSharedPreferences(
getString(R.string.const_string_preferences), getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE); Activity.MODE_PRIVATE);
SharedPreferences.Editor editorPreferences = preferences SharedPreferences.Editor editorPreferences = preferences
.edit(); .edit();
editorPreferences.putBoolean("show_popup_info", true); editorPreferences.putBoolean("show_popup_info", true);
editorPreferences.commit(); editorPreferences.commit();
} }

View File

@ -81,7 +81,7 @@ public class Core {
private static Map<String, Bitmap> imgCache = new HashMap<String, Bitmap>(); private static Map<String, Bitmap> imgCache = new HashMap<String, Bitmap>();
// Don't use this variable, just call getSocketFactory // Don't use this variable, just call getSocketFactory
private static SSLSocketFactory sslSocketFactory; private static SSLSocketFactory sslSocketFactory;
/** /**
* Reads from the input stream and returns a string. * Reads from the input stream and returns a string.
* *
@ -89,28 +89,31 @@ public class Core {
* @return A string with all data read. * @return A string with all data read.
*/ */
private static String convertStreamToString(InputStream is) { private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is), BufferedReader reader = new BufferedReader(
8 * 1024); new InputStreamReader(is), 8 * 1024);
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String line = null; String line = null;
try { try {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
sb.append(line + "\n"); sb.append(line + "\n");
} }
} catch (IOException e) { }
catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} finally { }
finally {
try { try {
is.close(); is.close();
} catch (IOException e) { }
catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
return sb.toString(); return sb.toString();
} }
/** /**
* Sets fetch frequency. * Sets fetch frequency.
* *
@ -127,33 +130,34 @@ public class Core {
ctx.stopService(new Intent(ctx, PandroidEventviewerService.class)); ctx.stopService(new Intent(ctx, PandroidEventviewerService.class));
// Sets the launch frequency // Sets the launch frequency
AlarmManager alarmM = (AlarmManager) ctx AlarmManager alarmM = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE); .getSystemService(Context.ALARM_SERVICE);
PendingIntent pandroidService = PendingIntent.getService(ctx, 0, PendingIntent pandroidService = PendingIntent.getService(ctx, 0,
new Intent(ctx, PandroidEventviewerService.class), 0); new Intent(ctx, PandroidEventviewerService.class), 0);
int sleepTimeAlarm = convertRefreshTimeKeyToTime(ctx); int sleepTimeAlarm = convertRefreshTimeKeyToTime(ctx);
Log.i(TAG, "sleepTimeAlarm = " + sleepTimeAlarm); Log.i(TAG, "sleepTimeAlarm = " + sleepTimeAlarm);
alarmM.setRepeating(AlarmManager.RTC_WAKEUP, alarmM.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), sleepTimeAlarm, pandroidService); System.currentTimeMillis(), sleepTimeAlarm, pandroidService);
} else { }
else {
Log.i(TAG, Log.i(TAG,
"Service wasn't started because the options aren't configured yet"); "Service wasn't started because the options aren't configured yet");
} }
} }
public static void cancelBackgroundService(Context ctx) { public static void cancelBackgroundService(Context ctx) {
AlarmManager alarmM = (AlarmManager) ctx AlarmManager alarmM = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE); .getSystemService(Context.ALARM_SERVICE);
PendingIntent pandroidService = PendingIntent.getService(ctx, 0, PendingIntent pandroidService = PendingIntent.getService(ctx, 0,
new Intent(ctx, PandroidEventviewerService.class), 0); new Intent(ctx, PandroidEventviewerService.class), 0);
Log.i(TAG, "Background service cancelled"); Log.i(TAG, "Background service cancelled");
alarmM.cancel(pandroidService); alarmM.cancel(pandroidService);
} }
/** /**
* Converts the maximum time setted to filter events to a timestamp. * Converts the maximum time setted to filter events to a timestamp.
* *
@ -162,71 +166,72 @@ public class Core {
* @return Time in milliseconds. * @return Time in milliseconds.
*/ */
public static long convertMaxTimeOldEventValuesToTimestamp(long time, public static long convertMaxTimeOldEventValuesToTimestamp(long time,
int arrayKey) { int arrayKey) {
long return_var = 0; long return_var = 0;
if (time == 0) { if (time == 0) {
Calendar c = Calendar.getInstance(); Calendar c = Calendar.getInstance();
time = c.getTimeInMillis() / 1000; time = c.getTimeInMillis() / 1000;
} }
switch (arrayKey) { switch (arrayKey) {
case 0: case 0:
return_var = time - 30 * 60; return_var = time - 30 * 60;
break; break;
case 1: case 1:
return_var = time - 60 * 60; return_var = time - 60 * 60;
break; break;
case 2: case 2:
return_var = time - 2 * (60 * 60); return_var = time - 2 * (60 * 60);
break; break;
case 3: case 3:
return_var = time - 3 * (60 * 60); return_var = time - 3 * (60 * 60);
break; break;
case 4: case 4:
return_var = time - 4 * (60 * 60); return_var = time - 4 * (60 * 60);
break; break;
case 5: case 5:
return_var = time - 5 * (60 * 60); return_var = time - 5 * (60 * 60);
break; break;
case 6: case 6:
return_var = time - 8 * (60 * 60); return_var = time - 8 * (60 * 60);
break; break;
case 7: case 7:
return_var = time - 10 * (60 * 60); return_var = time - 10 * (60 * 60);
break; break;
case 8: case 8:
return_var = time - 12 * (60 * 60); return_var = time - 12 * (60 * 60);
break; break;
case 9: case 9:
return_var = time - 24 * (60 * 60); return_var = time - 24 * (60 * 60);
break; break;
case 10: case 10:
return_var = time - 2 * (24 * 60 * 60); return_var = time - 2 * (24 * 60 * 60);
break; break;
case 11: case 11:
return_var = time - 3 * (24 * 60 * 60); return_var = time - 3 * (24 * 60 * 60);
break; break;
case 12: case 12:
return_var = time - 4 * (24 * 60 * 60); return_var = time - 4 * (24 * 60 * 60);
break; break;
case 13: case 13:
return_var = time - 5 * (24 * 60 * 60); return_var = time - 5 * (24 * 60 * 60);
break; break;
case 14: case 14:
return_var = time - 7 * (24 * 60 * 60); return_var = time - 7 * (24 * 60 * 60);
break; break;
case 15: case 15:
return_var = time - 2 * (7 * 24 * 60 * 60); return_var = time - 2 * (7 * 24 * 60 * 60);
break; break;
case 16: case 16:
return_var = time - 30 * (24 * 60 * 60); return_var = time - 30 * (24 * 60 * 60);
break; break;
} }
return return_var; return return_var;
} }
/** /**
* Converts max event age chosen from spinner to seconds (either are seconds * Converts max event age chosen from spinner to seconds (either are seconds
* or not) * or not)
@ -235,76 +240,76 @@ public class Core {
*/ */
private static int convertRefreshTimeKeyToTime(Context ctx) { private static int convertRefreshTimeKeyToTime(Context ctx) {
int returnvar = 60 * 10; int returnvar = 60 * 10;
SharedPreferences preferences = ctx.getSharedPreferences( SharedPreferences preferences = ctx.getSharedPreferences(
ctx.getString(R.string.const_string_preferences), ctx.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE); Activity.MODE_PRIVATE);
int refreshTimeKey = preferences.getInt("refreshTimeKey", 3); int refreshTimeKey = preferences.getInt("refreshTimeKey", 3);
switch (refreshTimeKey) { switch (refreshTimeKey) {
case 0: case 0:
returnvar = 30; // 30 seconds returnvar = 30; // 30 seconds
break; break;
case 1: case 1:
returnvar = 60; // 1 minute returnvar = 60; // 1 minute
break; break;
case 2: case 2:
returnvar = 60 * 5; // 5 minutes returnvar = 60 * 5; // 5 minutes
break; break;
case 3: case 3:
returnvar = 60 * 10; // 10 minutes returnvar = 60 * 10; // 10 minutes
break; break;
case 4: case 4:
returnvar = 60 * 15; // 15 minutes returnvar = 60 * 15; // 15 minutes
break; break;
case 5: case 5:
returnvar = 60 * 30; // 30 minutes returnvar = 60 * 30; // 30 minutes
break; break;
case 6: case 6:
returnvar = 60 * 45; // 45 minutes returnvar = 60 * 45; // 45 minutes
break; break;
case 7: case 7:
returnvar = 3600; // 1 hour returnvar = 3600; // 1 hour
break; break;
case 8: case 8:
returnvar = 3600 + (60 * 30); // 1 hour and 30 minutes returnvar = 3600 + (60 * 30); // 1 hour and 30 minutes
break; break;
case 9: case 9:
returnvar = 3600 * 2; // 2 hours returnvar = 3600 * 2; // 2 hours
break; break;
case 10: case 10:
returnvar = 3600 * 3; // 3 hours returnvar = 3600 * 3; // 3 hours
break; break;
case 11: case 11:
returnvar = 3600 * 4; // 4 hours returnvar = 3600 * 4; // 4 hours
break; break;
case 12: case 12:
returnvar = 3600 * 6; // 6 hours returnvar = 3600 * 6; // 6 hours
break; break;
case 13: case 13:
returnvar = 3600 * 8; // 8 hours returnvar = 3600 * 8; // 8 hours
break; break;
case 14: case 14:
returnvar = 3600 * 10; // 10 hours returnvar = 3600 * 10; // 10 hours
break; break;
case 15: case 15:
returnvar = 3600 * 12; // 12 hours returnvar = 3600 * 12; // 12 hours
break; break;
case 16: case 16:
returnvar = 3600 * 24; // 24 hours returnvar = 3600 * 24; // 24 hours
break; break;
case 17: case 17:
returnvar = 3600 * 36; // 36 hours returnvar = 3600 * 36; // 36 hours
break; break;
case 18: case 18:
returnvar = 3600 * 48; // 48 hours returnvar = 3600 * 48; // 48 hours
break; break;
} }
return returnvar * 1000; return returnvar * 1000;
} }
/** /**
* Converts params to string. * Converts params to string.
* *
@ -313,14 +318,16 @@ public class Core {
*/ */
public static String serializeParams2Api(String[] params) { public static String serializeParams2Api(String[] params) {
String return_var = params[0]; String return_var = params[0];
for (int i = 1; i < params.length; i++) { for (int i = 1; i < params.length; i++) {
return_var += "|" + params[i]; return_var += "|" + params[i];
} }
Log.i(TAG + " serializeParams2Api", return_var); Log.i(TAG + " serializeParams2Api", return_var);
return return_var; return return_var;
} }
/** /**
* Performs an http get petition. * Performs an http get petition.
* *
@ -333,11 +340,12 @@ public class Core {
* If there is any problem with the connection. * If there is any problem with the connection.
*/ */
public static String httpGet(Context context, public static String httpGet(Context context,
List<NameValuePair> additionalParameters) throws IOException { List<NameValuePair> additionalParameters) throws IOException {
SharedPreferences preferences = context.getSharedPreferences( SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences), context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE); Activity.MODE_PRIVATE);
String url = preferences.getString("url", "") + "/include/api.php"; String url = preferences.getString("url", "") + "/include/api.php";
String user = preferences.getString("user", ""); String user = preferences.getString("user", "");
String password = preferences.getString("password", ""); String password = preferences.getString("password", "");
@ -356,10 +364,11 @@ public class Core {
if (url.toLowerCase().contains("https")) { if (url.toLowerCase().contains("https")) {
// Secure connection // Secure connection
return Core.httpsGet(url, parameters); return Core.httpsGet(url, parameters);
} else { }
else {
HttpParams params = new BasicHttpParams(); HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, HttpConnectionParams.setConnectionTimeout(params,
CONNECTION_TIMEOUT); CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);
DefaultHttpClient httpClient = new DefaultHttpClient(params); DefaultHttpClient httpClient = new DefaultHttpClient(params);
UrlEncodedFormEntity entity; UrlEncodedFormEntity entity;
@ -373,12 +382,12 @@ public class Core {
response = httpClient.execute(httpPost); response = httpClient.execute(httpPost);
entityResponse = response.getEntity(); entityResponse = response.getEntity();
return_api = Core return_api = Core
.convertStreamToString(entityResponse.getContent()); .convertStreamToString(entityResponse.getContent());
Log.i(TAG, "received: " + return_api); Log.i(TAG, "received: " + return_api);
return return_api; return return_api;
} }
} }
/** /**
* Downloads an image * Downloads an image
* *
@ -393,12 +402,12 @@ public class Core {
} }
Log.i(TAG, "Downloading image: " + fileUrl); Log.i(TAG, "Downloading image: " + fileUrl);
URL myFileUrl = null; URL myFileUrl = null;
try { try {
myFileUrl = new URL(fileUrl); myFileUrl = new URL(fileUrl);
if (fileUrl.toLowerCase().contains("https")) { if (fileUrl.toLowerCase().contains("https")) {
HttpsURLConnection con = (HttpsURLConnection) new URL(fileUrl) HttpsURLConnection con = (HttpsURLConnection) new URL(fileUrl)
.openConnection(); .openConnection();
con.setHostnameVerifier(new HostnameVerifier() { con.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) { public boolean verify(String hostname, SSLSession session) {
return true; return true;
@ -408,9 +417,10 @@ public class Core {
Bitmap img = BitmapFactory.decodeStream(con.getInputStream()); Bitmap img = BitmapFactory.decodeStream(con.getInputStream());
imgCache.put(fileUrl, img); imgCache.put(fileUrl, img);
return img; return img;
} else { }
else {
HttpURLConnection conn = (HttpURLConnection) myFileUrl HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection(); .openConnection();
conn.setDoInput(true); conn.setDoInput(true);
conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.connect(); conn.connect();
@ -419,12 +429,13 @@ public class Core {
imgCache.put(fileUrl, img); imgCache.put(fileUrl, img);
return img; return img;
} }
} catch (IOException e) { }
catch (IOException e) {
Log.e(TAG, "Downloading image " + fileUrl + ": error"); Log.e(TAG, "Downloading image " + fileUrl + ": error");
} }
return null; return null;
} }
/** /**
* Puts the image to the left of theTextView. * Puts the image to the left of theTextView.
* *
@ -437,7 +448,7 @@ public class Core {
Drawable d = new BitmapDrawable(image); Drawable d = new BitmapDrawable(image);
setTextViewLeftImage(view, d, 16); setTextViewLeftImage(view, d, 16);
} }
/** /**
* Puts the image to the left of theTextView. * Puts the image to the left of theTextView.
* *
@ -450,7 +461,7 @@ public class Core {
Drawable d = new BitmapDrawable(Core.downloadImage(url)); Drawable d = new BitmapDrawable(Core.downloadImage(url));
setTextViewLeftImage(view, d, 16); setTextViewLeftImage(view, d, 16);
} }
/** /**
* Puts the image to the left of theTextView. * Puts the image to the left of theTextView.
* *
@ -462,13 +473,14 @@ public class Core {
* Image size * Image size
*/ */
public static void setTextViewLeftImage(TextView view, Drawable image, public static void setTextViewLeftImage(TextView view, Drawable image,
int size) { int size) {
if (image != null) { if (image != null) {
image.setBounds(0, 0, size, size); image.setBounds(0, 0, size, size);
view.setCompoundDrawables(image, null, null, null); view.setCompoundDrawables(image, null, null, null);
} }
} }
/** /**
* Finds out if the given url has a CA signed certificate. * Finds out if the given url has a CA signed certificate.
* *
@ -479,17 +491,20 @@ public class Core {
*/ */
public static boolean isValidCertificate(URL url) { public static boolean isValidCertificate(URL url) {
HttpsURLConnection con; HttpsURLConnection con;
try { try {
con = (HttpsURLConnection) url.openConnection(); con = (HttpsURLConnection) url.openConnection();
con.setConnectTimeout(CONNECTION_TIMEOUT); con.setConnectTimeout(CONNECTION_TIMEOUT);
con.connect(); con.connect();
con.disconnect(); con.disconnect();
return true; return true;
} catch (IOException e) { }
catch (IOException e) {
return false; return false;
} }
} }
/** /**
* Checks if a url is online. * Checks if a url is online.
* *
@ -508,12 +523,14 @@ public class Core {
con.setSSLSocketFactory(getSocketFactory()); con.setSSLSocketFactory(getSocketFactory());
con.setDoOutput(true); con.setDoOutput(true);
con.getInputStream(); con.getInputStream();
return true; return true;
} catch (IOException e) { }
catch (IOException e) {
return false; return false;
} }
} }
/** /**
* Performs an secure http petition * Performs an secure http petition
* *
@ -527,7 +544,8 @@ public class Core {
* *
*/ */
private static String httpsGet(String url, List<NameValuePair> parameters) private static String httpsGet(String url, List<NameValuePair> parameters)
throws IOException { throws IOException {
String result = ""; String result = "";
HttpsURLConnection con; HttpsURLConnection con;
try { try {
@ -545,32 +563,34 @@ public class Core {
for (NameValuePair nameValuePair : parameters) { for (NameValuePair nameValuePair : parameters) {
postData = first ? postData : postData + "&"; postData = first ? postData : postData + "&";
first = false; first = false;
postData += URLEncoder.encode(nameValuePair.getName()) + "=" postData += URLEncoder.encode(nameValuePair.getName())
+ URLEncoder.encode(nameValuePair.getValue()); + "=" + URLEncoder.encode(nameValuePair.getValue());
} }
if (postData.length() > 0) { if (postData.length() > 0) {
OutputStreamWriter wr = new OutputStreamWriter( OutputStreamWriter wr = new OutputStreamWriter(
con.getOutputStream()); con.getOutputStream());
wr.write(postData); wr.write(postData);
wr.flush(); wr.flush();
} }
InputStream inputStream; InputStream inputStream;
inputStream = con.getInputStream(); inputStream = con.getInputStream();
BufferedReader bufferedReader = new BufferedReader( BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream)); new InputStreamReader(inputStream));
String temp; String temp;
while ((temp = bufferedReader.readLine()) != null) { while ((temp = bufferedReader.readLine()) != null) {
Log.d("CONTENT", temp); Log.d("CONTENT", temp);
result += temp + "\n"; result += temp + "\n";
} }
} catch (MalformedURLException e) { }
catch (MalformedURLException e) {
// Can't reach here because the given url is checked when is // Can't reach here because the given url is checked when is
// inserted in Options activity. // inserted in Options activity.
} }
return result; return result;
} }
/** /**
* Returns a SSL Factory instance that accepts all server certificates. * Returns a SSL Factory instance that accepts all server certificates.
* *
@ -582,21 +602,24 @@ public class Core {
TrustManager[] tm = new TrustManager[] { new NaiveTrustManager() }; TrustManager[] tm = new TrustManager[] { new NaiveTrustManager() };
SSLContext context = SSLContext.getInstance("TLS"); SSLContext context = SSLContext.getInstance("TLS");
context.init(new KeyManager[0], tm, new SecureRandom()); context.init(new KeyManager[0], tm, new SecureRandom());
sslSocketFactory = (SSLSocketFactory) context sslSocketFactory = (SSLSocketFactory) context
.getSocketFactory(); .getSocketFactory();
} catch (KeyManagementException e) { }
catch (KeyManagementException e) {
Log.e("No SSL algorithm support: " + e.getMessage(), Log.e("No SSL algorithm support: " + e.getMessage(),
e.toString()); e.toString());
} catch (NoSuchAlgorithmException e) { }
catch (NoSuchAlgorithmException e) {
Log.e("Exception when setting up the Naive key management.", Log.e("Exception when setting up the Naive key management.",
e.toString()); e.toString());
} }
} }
return sslSocketFactory; return sslSocketFactory;
} }
/** /**
* Shows a toast which will show the connection problem message. Do not call * Shows a toast which will show the connection problem message. Do not call
* outside the UI's thread. * outside the UI's thread.
@ -608,21 +631,24 @@ public class Core {
* correctly configured. * correctly configured.
*/ */
public static void showConnectionProblemToast(Context context, public static void showConnectionProblemToast(Context context,
boolean ignoreConnectionCheck) { boolean ignoreConnectionCheck) {
SharedPreferences preferences = context.getSharedPreferences( SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences), context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE); Activity.MODE_PRIVATE);
if (preferences.getBoolean("online", false)) { if (preferences.getBoolean("online", false)) {
Toast.makeText(context, R.string.connection_problem, Toast.makeText(context, R.string.connection_problem,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} else { }
else {
if (ignoreConnectionCheck) { if (ignoreConnectionCheck) {
Toast.makeText(context, R.string.connection_settings_problem, Toast.makeText(context,
Toast.LENGTH_SHORT).show(); R.string.connection_settings_problem,
Toast.LENGTH_SHORT).show();
} }
} }
} }
/** /**
* Checks if a string is inside the list (case insensitive). * Checks if a string is inside the list (case insensitive).
* *
@ -640,7 +666,7 @@ public class Core {
} }
return false; return false;
} }
/** /**
* Returns the corresponding image to the given severity code. * Returns the corresponding image to the given severity code.
* *
@ -652,28 +678,28 @@ public class Core {
*/ */
public static Drawable getSeverityImage(Context context, int severityCode) { public static Drawable getSeverityImage(Context context, int severityCode) {
switch (severityCode) { switch (severityCode) {
case 0: case 0:
return context.getResources().getDrawable( return context.getResources().getDrawable(
R.drawable.severity_maintenance); R.drawable.severity_maintenance);
case 1: case 1:
return context.getResources().getDrawable( return context.getResources().getDrawable(
R.drawable.severity_informational); R.drawable.severity_informational);
case 2: case 2:
return context.getResources().getDrawable( return context.getResources().getDrawable(
R.drawable.severity_normal); R.drawable.severity_normal);
case 3: case 3:
return context.getResources().getDrawable( return context.getResources().getDrawable(
R.drawable.severity_warning); R.drawable.severity_warning);
case 4: case 4:
return context.getResources().getDrawable( return context.getResources().getDrawable(
R.drawable.severity_critical); R.drawable.severity_critical);
} }
return null; return null;
} }
/** /**
* Returns the corresponding image to the given event type. * Returns the corresponding image to the given event type.
* *
@ -699,11 +725,12 @@ public class Core {
images.put("recon_host_detected", R.drawable.network); images.put("recon_host_detected", R.drawable.network);
images.put("new_agent", R.drawable.wand); images.put("new_agent", R.drawable.wand);
images.put("unknown", R.drawable.err); images.put("unknown", R.drawable.err);
Integer code = images.get(eventType.toLowerCase()); Integer code = images.get(eventType.toLowerCase());
if (code != null) { if (code != null) {
return context.getResources().getDrawable(code); return context.getResources().getDrawable(code);
} else { }
else {
return null; return null;
} }
} }

View File

@ -392,42 +392,43 @@ public class EventList extends ListActivity {
content_event_item.setVisibility(RelativeLayout.GONE); content_event_item.setVisibility(RelativeLayout.GONE);
} }
} }
} else { }
else {
final EventListItem item = this.object.eventList.get(position); final EventListItem item = this.object.eventList.get(position);
switch (item.criticity) { switch (item.criticity) {
case 0:
case 0: view.setBackgroundColor(getResources().getColor(
view.setBackgroundColor(getResources().getColor( R.color.Blue));
R.color.Blue)); break;
break; case 1:
case 1: view.setBackgroundColor(getResources().getColor(
view.setBackgroundColor(getResources().getColor( R.color.Grey));
R.color.Grey)); break;
break; case 2:
case 2: view.setBackgroundColor(getResources().getColor(
view.setBackgroundColor(getResources().getColor( R.color.Green));
R.color.Green)); break;
break; case 3:
case 3: view.setBackgroundColor(getResources().getColor(
view.setBackgroundColor(getResources().getColor( R.color.Yellow));
R.color.Yellow)); break;
break; case 4:
case 4: view.setBackgroundColor(getResources()
view.setBackgroundColor(getResources() .getColor(R.color.Red));
.getColor(R.color.Red)); break;
break; default:
default: view.setBackgroundColor(getResources().getColor(
view.setBackgroundColor(getResources().getColor( R.color.Grey));
R.color.Grey)); break;
break;
} }
TextView titulo = (TextView) view.findViewById(R.id.agent_name); TextView titulo = (TextView) view.findViewById(R.id.agent_name);
if (item.event_type.equals("system")) { if (item.event_type.equals("system")) {
titulo.setText(R.string.system_str); titulo.setText(R.string.system_str);
} else { }
else {
titulo.setText(item.agent_name); titulo.setText(item.agent_name);
} }