2012-05-19 Santiago Munin <burning1@gmail.com>

* AlarmReceiver.java: Removed. Now it's the service which will retrieve data.
	* Core.java: The method of setting periodically data retrieval was changed. 
				 Now, the AlarmManager will launch the Service instead of BroadcastReceiver.
	* PandroidEventviewerService.java: Every time it receives a call from the AlarmManager, will search for new events.
	* Most of files have little changes to adapt the new data retrieval subsystem.


git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6322 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
santimunin 2012-05-19 00:18:05 +00:00
parent 9437651954
commit da297ec58e
9 changed files with 425 additions and 466 deletions

View File

@ -1,347 +0,0 @@
/*
Pandora FMS - http://pandorafms.com
==================================================
Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
Please see http://pandorafms.org for full contribution list
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; version 2
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
package pandroid_event_viewer.pandorafms;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
/**
* It will receive new events and launch notifications.
*
* @author Miguel de Dios Matías
*
*/
public class AlarmReceiver extends BroadcastReceiver {
private static String TAG = "ALARM RECEIVER";
private static final int NOTIFICATION_PANDROID_EVENT_VIEWER = 666;
public String url;
public String user;
public String password;
public long count_events;
public int more_criticity;
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive");
checkNewEvents(context);
}
/**
* Checks if there are new events and, in that case, throw a notification.
*
* @param context
*/
public void checkNewEvents(Context context) {
if (this.url == null) {
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
this.url = preferences.getString("url", "");
this.user = preferences.getString("user", "");
this.password = preferences.getString("password", "");
Calendar c = Calendar.getInstance();
long now = (c.getTimeInMillis() / 1000);
long old_previous_filterTimestamp = preferences.getLong(
"previous_filterTimestamp", now);
if ((user.length() == 0) && (password.length() == 0)
&& (url.length() == 0)) {
return;
}
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
UrlEncodedFormEntity entity;
HttpPost httpPost;
List<NameValuePair> parameters;
HttpResponse response;
HttpEntity entityResponse;
String return_api;
httpPost = new HttpPost(this.url + "/include/api.php");
String parametersAPI = serializeParams2Api(context);
// Get total count.
parameters = new ArrayList<NameValuePair>();
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", parametersAPI
+ "|total"));
entity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
entityResponse = response.getEntity();
return_api = Core.convertStreamToString(entityResponse
.getContent());
return_api = return_api.replace("\n", "");
Log.i(TAG + " checkNewEvents", return_api);
this.count_events = new Long(return_api).longValue();
// Check the event more critical
if (this.count_events != 0) {
parameters = new ArrayList<NameValuePair>();
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",
parametersAPI + "|more_criticity"));
entity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
entityResponse = response.getEntity();
return_api = Core.convertStreamToString(entityResponse
.getContent());
return_api = return_api.replace("\n", "");
this.more_criticity = new Integer(return_api).intValue();
notificationEvent(context);
} else {
this.more_criticity = -1;
// Restore timestamp
SharedPreferences.Editor editorPreferences = preferences
.edit();
editorPreferences.putLong("previous_filterTimestamp",
old_previous_filterTimestamp);
editorPreferences.commit();
}
} catch (Exception e) {
Log.e(TAG + " EXCEPTION checkNewEvents", e.getMessage());
return;
}
}
}
/**
* Builds an api call from all filter parameters
*
* @param context
* @return Api call.
*/
public String serializeParams2Api(Context context) {
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
String filterAgentName = preferences.getString("filterAgentName", "");
// TODO no api parameter, waiting for it
// int filterIDGroup = preferences.getInt("filterIDGroup", 0);
int filterSeverity = preferences.getInt("filterSeverity", -1);
int filterStatus = preferences.getInt("filterStatus", 3);
String filterEventSearch = preferences.getString("filterEventSearch",
"");
Calendar c = Calendar.getInstance();
long now = (c.getTimeInMillis() / 1000);
long filterTimestamp = preferences.getLong("filterTimestamp", now);
SharedPreferences.Editor editorPreferences = preferences.edit();
// Save for the next execution
editorPreferences.putLong("filterTimestamp", now);
// Save the previous for the list.
editorPreferences.putLong("previous_filterTimestamp", filterTimestamp);
if (editorPreferences.commit()) {
Log.i(TAG + " (filter options)",
"Configuration changes commited (timestamp)");
} else {
Log.e(TAG + " (filter options)",
"Configuration changes not commited");
}
String return_var = "";
return_var += ';'; // Separator for the csv
return_var += "|";
return_var += Integer.toString(filterSeverity); // Criticity or severity
return_var += "|";
return_var += filterAgentName; // 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(filterTimestamp); // The minimun timestamp
return_var += "|";
return_var += ""; // The maximum timestamp
return_var += "|";
return_var += filterStatus; // The status
return_var += "|";
return_var += filterEventSearch; // The free search in the text event
// description.
return_var += "|";
return_var += Integer.toString(0); // The pagination of list events
return_var += "|";
return_var += Long.toString(0); // The offset of list events
Log.i(TAG + " serializeParams2Api", return_var);
return return_var;
}
/**
* Launchs a notification
*
* @param context
*/
public void notificationEvent(Context context) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_PANDROID_EVENT_VIEWER);
int icon;
CharSequence tickerText;
switch (this.more_criticity) {
case 0:
icon = R.drawable.criticity_0;
tickerText = context.getString(
R.string.notification_criticity_0_str).replace("%s",
new Long(this.count_events).toString());
break;
case 1:
icon = R.drawable.criticity_1;
tickerText = context.getString(
R.string.notification_criticity_1_str).replace("%s",
new Long(this.count_events).toString());
break;
case 2:
icon = R.drawable.criticity_2;
tickerText = context.getString(
R.string.notification_criticity_2_str).replace("%s",
new Long(this.count_events).toString());
break;
case 3:
icon = R.drawable.criticity_3;
tickerText = context.getString(
R.string.notification_criticity_3_str).replace("%s",
new Long(this.count_events).toString());
break;
case 4:
icon = R.drawable.criticity_4;
tickerText = context.getString(
R.string.notification_criticity_4_str).replace("%s",
new Long(this.count_events).toString());
break;
default:
icon = R.drawable.criticity_default;
tickerText = context.getString(
R.string.notification_criticity_2_str).replace("%s",
new Long(this.count_events).toString());
break;
}
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// notification.defaults |= Notification.DEFAULT_ALL;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Notification options
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
if (preferences.getBoolean("vibration", true)) {
Log.d(TAG, "Vibration");
notification.defaults |= Notification.DEFAULT_VIBRATE;
} else {
Log.d(TAG, "No vibration");
notification.vibrate = new long[] { 0, 0, 0, 0 };
}
if (preferences.getBoolean("led", false)) {
Log.d(TAG, "Led flash");
notification.defaults |= Notification.DEFAULT_LIGHTS;
/*
* notification.ledARGB = 0xff00ff00; notification.ledOnMS = 300;
* notification.ledOffMS = 1000; notification.flags |=
* Notification.FLAG_SHOW_LIGHTS;
*/
}
Uri notificationSoundUri = Uri.parse(preferences.getString(
"notification_sound_uri",
RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
.toString()));
if (notificationSoundUri != null) {
Log.i(TAG, "Setting sound: " + notificationSoundUri.toString());
notification.sound = notificationSoundUri;
} else {
Log.e(TAG, "Ringtone's uri problem (NULL)");
}
Intent notificationIntent = new Intent(context,
PandroidEventviewerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("count_events", this.count_events);
notificationIntent.putExtra("more_criticity", this.more_criticity);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
CharSequence title = context
.getString(R.string.pandroid_event_viewer_str);
notification.setLatestEventInfo(context, title, tickerText,
contentIntent);
Log.i(TAG, "Launching notification");
mNotificationManager.notify(NOTIFICATION_PANDROID_EVENT_VIEWER,
notification);
}
}

View File

@ -22,8 +22,13 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
/**
* This class provides basic functions to manage services and some received
@ -33,28 +38,7 @@ import android.content.Intent;
*
*/
public class Core {
/**
* Starts PandroidEventviewerService.
*
* @param context
*/
public static void startServiceEventWatcher(Context context) {
context.startService(new Intent(context,
PandroidEventviewerService.class));
}
/**
* Stops PandroidEventviewerService.
*
* @param context
*/
public static void stopServiceEventWatcher(Context context) {
context.stopService(new Intent(context,
PandroidEventviewerService.class));
}
private static String TAG = "Core";
/**
* Reads from the input stream and returns a string.
@ -85,6 +69,31 @@ public class Core {
return sb.toString();
}
/**
* Sets fetch frequency.
*
* @param ctx
* Application context.
*/
public static void setFetchFrequency(Context ctx) {
Log.i(TAG, "Setting events fetching frequency");
// Stops the service (if it's running)
ctx.stopService(new Intent(ctx, PandroidEventviewerService.class));
// Sets the launch frequency
AlarmManager alarmM = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
PendingIntent pandroidService = PendingIntent.getService(ctx, 0,
new Intent(ctx, PandroidEventviewerService.class), 0);
int sleepTimeAlarm = convertRefreshTimeKeyToTime(ctx);
Log.i(TAG, "sleepTimeAlarm = " + sleepTimeAlarm);
alarmM.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), sleepTimeAlarm, pandroidService);
}
/**
* Converts the maximum time setted to filter events to a timestamp.
*
@ -92,7 +101,8 @@ public class Core {
* @param arrayKey
* @return Time in milliseconds.
*/
public static long convertMaxTimeOldEventValuesToTimestamp(long time, int arrayKey) {
public static long convertMaxTimeOldEventValuesToTimestamp(long time,
int arrayKey) {
long return_var = 0;
if (time == 0) {
@ -156,4 +166,81 @@ public class Core {
return return_var;
}
/**
* Converts chosen time from spinner to seconds (either are seconds or not)
*
* @return
*/
private static int convertRefreshTimeKeyToTime(Context ctx) {
int returnvar = 60 * 10;
SharedPreferences preferences = ctx.getSharedPreferences(
ctx.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
int refreshTimeKey = preferences.getInt("refreshTimeKey", 3);
switch (refreshTimeKey) {
case 0:
returnvar = 30; // 30 seconds
break;
case 1:
returnvar = 60; // 1 minute
break;
case 2:
returnvar = 60 * 5; // 5 minutes
break;
case 3:
returnvar = 60 * 10; // 10 minutes
break;
case 4:
returnvar = 60 * 15; // 15 minutes
break;
case 5:
returnvar = 60 * 30; // 30 minutes
break;
case 6:
returnvar = 60 * 45; // 45 minutes
break;
case 7:
returnvar = 3600; // 1 hour
break;
case 8:
returnvar = 3600 + (60 * 30); // 1 hour and 30 minutes
break;
case 9:
returnvar = 3600 * 2; // 2 hours
break;
case 10:
returnvar = 3600 * 3; // 3 hours
break;
case 11:
returnvar = 3600 * 4; // 4 hours
break;
case 12:
returnvar = 3600 * 6; // 6 hours
break;
case 13:
returnvar = 3600 * 8; // 8 hours
break;
case 14:
returnvar = 3600 * 10; // 10 hours
break;
case 15:
returnvar = 3600 * 12; // 12 hours
break;
case 16:
returnvar = 3600 * 24; // 24 hours
break;
case 17:
returnvar = 3600 * 36; // 36 hours
break;
case 18:
returnvar = 3600 * 48; // 48 hours
break;
}
return returnvar * 1000;
}
}

View File

@ -19,7 +19,6 @@ package pandroid_event_viewer.pandorafms;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@ -38,6 +37,7 @@ import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -60,6 +60,7 @@ import android.widget.TextView;
*
*/
public class EventList extends ListActivity {
private static String TAG = "EventList";
private ListView lv;
private MyAdapter la;
@ -228,12 +229,9 @@ public class EventList extends ListActivity {
*/
private Bitmap downloadImage(String fileUrl) {
URL myFileUrl = null;
try {
myFileUrl = new URL(fileUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
@ -241,7 +239,7 @@ public class EventList extends ListActivity {
InputStream is = conn.getInputStream();
return BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Downloading image");
}
return null;
}

View File

@ -16,13 +16,17 @@ GNU General Public License for more details.
*/
package pandroid_event_viewer.pandorafms;
import java.io.Serializable;
/**
* This class represents an event.
*
* @author Miguel de Dios Matías
*
*/
public class EventListItem {
public class EventListItem implements Serializable {
private static final long serialVersionUID = 5923680782563861300L;
public int id_event;
public int id_agent;
public String id_user;

View File

@ -219,7 +219,7 @@ public class Main extends Activity {
array.add(groups[1]);
}
} catch (Exception e) {
Log.e(TAG +": getting groups", e.getMessage());
Log.e(TAG + ": getting groups", e.getMessage());
}
return array;
@ -302,8 +302,8 @@ public class Main extends Activity {
Spinner combo = (Spinner) findViewById(R.id.max_time_old_event_combo);
timeKey = combo.getSelectedItemPosition();
this.object.timestamp = Core
.convertMaxTimeOldEventValuesToTimestamp(0, timeKey);
this.object.timestamp = Core.convertMaxTimeOldEventValuesToTimestamp(0,
timeKey);
EditText text = (EditText) findViewById(R.id.agent_name);
this.object.agentNameStr = text.getText().toString();
@ -397,9 +397,7 @@ public class Main extends Activity {
editorPreferences.putInt("filterLastTime", filterLastTime);
if (editorPreferences.commit()) {
Core.stopServiceEventWatcher(getApplicationContext());
Core.startServiceEventWatcher(getApplicationContext());
Core.setFetchFrequency(getApplicationContext());
Toast toast = Toast.makeText(getApplicationContext(),
this.getString(R.string.filter_update_succesful_str),
Toast.LENGTH_SHORT);

View File

@ -190,6 +190,7 @@ public class Options extends Activity {
Context context = this.getApplicationContext();
if (editorPreferences.commit()) {
Core.setFetchFrequency(getApplicationContext());
Log.i(TAG, "Settings saved");
Toast toast = Toast.makeText(context,
this.getString(R.string.config_update_succesful_str),

View File

@ -120,12 +120,11 @@ public class PandroidEventviewerActivity extends TabActivity implements
if (!this.showOptionsFirstTime) {
// Start the background service for the notifications
Core.startServiceEventWatcher(getApplicationContext());
Core.setFetchFrequency(getApplicationContext());
}
Intent i_main = new Intent(this, Main.class);
i_main.putExtra("object", this);
// TODO corei_main.putExtra("core", this.core);
tabHost.addTab(tabHost
.newTabSpec(

View File

@ -13,124 +13,343 @@ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
*/
package pandroid_event_viewer.pandorafms;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;
/**
* This service will launch AlarmReceiver periodically.
*
* @author Miguel de Dios Matías
*
*/
public class PandroidEventviewerService extends Service {
public class PandroidEventviewerService extends IntentService {
private static String TAG = "PandroidEventviewerService";
public AlarmManager alarmM;
PendingIntent pendingI;
private static final int NOTIFICATION_PANDROID_EVENT_VIEWER = 666;
public String url;
public String user;
public String password;
public long count_events;
public int more_criticity;
public PandroidEventviewerService() {
super(TAG);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
alarmM = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
@Override
protected void onHandleIntent(Intent intent) {
checkNewEvents(getApplicationContext());
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
this.pendingI = PendingIntent.getBroadcast(this, 0, intentAlarm, 0);
int sleepTimeAlarm = convertRefreshTimeKeyToTime();
Log.i(TAG, "sleepTimeAlarm = " + sleepTimeAlarm);
alarmM.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), sleepTimeAlarm, this.pendingI);
}
/**
* Converts chosen time from spinner to seconds (either are seconds or not)
* @return
*/
private int convertRefreshTimeKeyToTime() {
int returnvar = 60 * 10;
SharedPreferences preferences = getSharedPreferences(
this.getString(R.string.const_string_preferences),
/**
* Checks if there are new events and, in that case, throw a notification.
*
* @param context
*/
public void checkNewEvents(Context context) {
Log.d(TAG, "Checking events at "
+ Calendar.getInstance().getTime().toGMTString());
if (this.url == null) {
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
this.url = preferences.getString("url", "");
this.user = preferences.getString("user", "");
this.password = preferences.getString("password", "");
Calendar c = Calendar.getInstance();
long now = (c.getTimeInMillis() / 1000);
long old_previous_filterTimestamp = preferences.getLong(
"previous_filterTimestamp", now);
if ((user.length() == 0) && (password.length() == 0)
&& (url.length() == 0)) {
return;
}
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
UrlEncodedFormEntity entity;
HttpPost httpPost;
List<NameValuePair> parameters;
HttpResponse response;
HttpEntity entityResponse;
String return_api;
httpPost = new HttpPost(this.url + "/include/api.php");
String parametersAPI = serializeParams2Api(context);
// Get total count.
parameters = new ArrayList<NameValuePair>();
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", parametersAPI
+ "|total"));
entity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
entityResponse = response.getEntity();
return_api = Core.convertStreamToString(entityResponse
.getContent());
return_api = return_api.replace("\n", "");
Log.i(TAG + " checkNewEvents", return_api);
this.count_events = new Long(return_api).longValue();
// Check the event more critical
if (this.count_events != 0) {
parameters = new ArrayList<NameValuePair>();
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",
parametersAPI + "|more_criticity"));
entity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
entityResponse = response.getEntity();
return_api = Core.convertStreamToString(entityResponse
.getContent());
return_api = return_api.replace("\n", "");
this.more_criticity = new Integer(return_api).intValue();
notificationEvent(context);
} else {
this.more_criticity = -1;
// Restore timestamp
SharedPreferences.Editor editorPreferences = preferences
.edit();
editorPreferences.putLong("previous_filterTimestamp",
old_previous_filterTimestamp);
editorPreferences.commit();
}
} catch (Exception e) {
Log.e(TAG + " EXCEPTION checkNewEvents", e.getMessage());
return;
}
}
Log.d(TAG, "Check finished at "
+ Calendar.getInstance().getTime().toGMTString());
}
/**
* Builds an api call from all filter parameters
*
* @param context
* @return Api call.
*/
public String serializeParams2Api(Context context) {
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
int refreshTimeKey = preferences.getInt("refreshTimeKey", 3);
String filterAgentName = preferences.getString("filterAgentName", "");
/*
* TODO no api parameter, waiting for it int filterIDGroup =
* preferences.getInt("filterIDGroup", 0);
*/
int filterSeverity = preferences.getInt("filterSeverity", -1);
int filterStatus = preferences.getInt("filterStatus", 3);
String filterEventSearch = preferences.getString("filterEventSearch",
"");
switch (refreshTimeKey) {
Calendar c = Calendar.getInstance();
long now = (c.getTimeInMillis() / 1000);
long filterTimestamp = preferences.getLong("filterTimestamp", now);
SharedPreferences.Editor editorPreferences = preferences.edit();
// Save for the next execution
editorPreferences.putLong("filterTimestamp", now);
// Save the previous for the list.
editorPreferences.putLong("previous_filterTimestamp", filterTimestamp);
if (editorPreferences.commit()) {
Log.i(TAG + " (filter options)",
"Configuration changes commited (timestamp)");
} else {
Log.e(TAG + " (filter options)",
"Configuration changes not commited");
}
String return_var = "";
return_var += ';'; // Separator for the csv
return_var += "|";
return_var += Integer.toString(filterSeverity); // Criticity or severity
return_var += "|";
return_var += filterAgentName; // 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(filterTimestamp); // The minimun timestamp
return_var += "|";
return_var += ""; // The maximum timestamp
return_var += "|";
return_var += filterStatus; // The status
return_var += "|";
return_var += filterEventSearch; // The free search in the text event
// description.
return_var += "|";
return_var += Integer.toString(0); // The pagination of list events
return_var += "|";
return_var += Long.toString(0); // The offset of list events
Log.i(TAG + " serializeParams2Api", return_var);
return return_var;
}
/**
* Launches a notification
*
* @param context
*/
public void notificationEvent(Context context) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_PANDROID_EVENT_VIEWER);
int icon;
CharSequence tickerText;
switch (this.more_criticity) {
case 0:
returnvar = 30; // 30 seconds
icon = R.drawable.criticity_0;
tickerText = context.getString(
R.string.notification_criticity_0_str).replace("%s",
new Long(this.count_events).toString());
break;
case 1:
returnvar = 60; // 1 minute
icon = R.drawable.criticity_1;
tickerText = context.getString(
R.string.notification_criticity_1_str).replace("%s",
new Long(this.count_events).toString());
break;
case 2:
returnvar = 60 * 5; // 5 minutes
icon = R.drawable.criticity_2;
tickerText = context.getString(
R.string.notification_criticity_2_str).replace("%s",
new Long(this.count_events).toString());
break;
case 3:
returnvar = 60 * 10; // 10 minutes
icon = R.drawable.criticity_3;
tickerText = context.getString(
R.string.notification_criticity_3_str).replace("%s",
new Long(this.count_events).toString());
break;
case 4:
returnvar = 60 * 15; // 15 minutes
icon = R.drawable.criticity_4;
tickerText = context.getString(
R.string.notification_criticity_4_str).replace("%s",
new Long(this.count_events).toString());
break;
case 5:
returnvar = 60 * 30; // 30 minutes
break;
case 6:
returnvar = 60 * 45; // 45 minutes
break;
case 7:
returnvar = 3600; // 1 hour
break;
case 8:
returnvar = 3600 + (60 * 30); // 1 hour and 30 minutes
break;
case 9:
returnvar = 3600 * 2; // 2 hours
break;
case 10:
returnvar = 3600 * 3; // 3 hours
break;
case 11:
returnvar = 3600 * 4; // 4 hours
break;
case 12:
returnvar = 3600 * 6; // 6 hours
break;
case 13:
returnvar = 3600 * 8; // 8 hours
break;
case 14:
returnvar = 3600 * 10; // 10 hours
break;
case 15:
returnvar = 3600 * 12; // 12 hours
break;
case 16:
returnvar = 3600 * 24; // 24 hours
break;
case 17:
returnvar = 3600 * 36; // 36 hours
break;
case 18:
returnvar = 3600 * 48; // 48 hours
default:
icon = R.drawable.criticity_default;
tickerText = context.getString(
R.string.notification_criticity_2_str).replace("%s",
new Long(this.count_events).toString());
break;
}
return returnvar * 1000;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Notification options
SharedPreferences preferences = context.getSharedPreferences(
context.getString(R.string.const_string_preferences),
Activity.MODE_PRIVATE);
if (preferences.getBoolean("vibration", true)) {
Log.d(TAG, "Vibration");
notification.defaults |= Notification.DEFAULT_VIBRATE;
} else {
Log.d(TAG, "No vibration");
notification.vibrate = new long[] { 0, 0, 0, 0 };
}
if (preferences.getBoolean("led", false)) {
Log.d(TAG, "Led flash");
notification.defaults |= Notification.DEFAULT_LIGHTS;
}
Uri notificationSoundUri = Uri.parse(preferences.getString(
"notification_sound_uri",
RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
.toString()));
if (notificationSoundUri != null) {
Log.i(TAG, "Setting sound: " + notificationSoundUri.toString());
notification.sound = notificationSoundUri;
} else {
Log.e(TAG, "Ringtone's uri problem (NULL)");
}
Intent notificationIntent = new Intent(context,
PandroidEventviewerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("count_events", this.count_events);
notificationIntent.putExtra("more_criticity", this.more_criticity);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
CharSequence title = context
.getString(R.string.pandroid_event_viewer_str);
notification.setLatestEventInfo(context, title, tickerText,
contentIntent);
Log.i(TAG, "Launching notification");
mNotificationManager.notify(NOTIFICATION_PANDROID_EVENT_VIEWER,
notification);
}
public void onDestroy() {
alarmM.cancel(this.pendingI);
}
}