Back to project page Icinga-Mobile.
The source code is released under:
GNU General Public License
If you think the Android project Icinga-Mobile listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mhst.dreamteam.IcingaService; /*from w ww . j av a 2 s.c om*/ import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import java.sql.Time; import java.util.ArrayList; import java.util.Calendar; import java.util.Map; import java.util.Stack; import mhst.dreamteam.IcingaClient.GlobalConst; import mhst.dreamteam.IcingaClient.Icinga.IcingaApi; import mhst.dreamteam.IcingaClient.Interface.OnCompleteListener; import mhst.dreamteam.IcingaClient.SessionMng.Session; /** * Background service to check notification from server * * @author MinhNN */ public class DataUpdater extends IntentService implements OnCompleteListener { private Stack<Map<String, Object>> mEventStack; private Session currentSession; private boolean isLoading = false; private NotiBuilder mNotiBuilder; private NotificationManager mNotiMng; private long lastChecked; private final String NOTIFICATION_ID = "NOTIFICATION_ID"; private final String NOTIFICATION_TYPE = "NOTIFICATION_TYPE"; private final String NOTIFICATION_CONTACT = "NOTIFICATION_CONTACT"; private final String NOTIFICATION_HOST_NAME = "HOST_NAME"; private final String NOTIFICATION_SERVICE_NAME = "SERVICE_NAME"; private final String NOTIFICATION_STATE = "NOTIFICATION_STATE"; private final String NOTIFICATION_STARTTIME = "NOTIFICATION_STARTTIME"; private final String NOTIFICATION_OUTPUT = "NOTIFICATION_OUTPUT"; private final String NOTIFICATION_COMMAND_NAME = "COMMAND_NAME"; private final int NOTIFICATION_TYPE_HOST = 0; private final int NOTIFICATION_TYPE_SERVICE = 1; public DataUpdater() { super("IcingaDataUpdater"); mEventStack = new Stack<Map<String, Object>>(); } @Override protected void onHandleIntent(Intent intent) { currentSession = Session.getInstance(); updateSession(); Calendar now = Calendar.getInstance(); lastChecked = now.getTimeInMillis(); mNotiBuilder = new NotiBuilder(this); mNotiBuilder.setTitle(getResources().getString(R.string.app_name)); mNotiMng = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent i; while (true) { if (!isLoading && currentSession.isLogin()) { IcingaApi.cronks(DataUpdater.this, IcingaApi.TEMPLATE_ICINGA_NOTIFICATION); isLoading = true; } if (!mEventStack.isEmpty()) { while (!mEventStack.isEmpty()) { Map<String, Object> event = mEventStack.pop(); // Convert notification state to nature text String sState = ""; int nState = (Integer) event.get(NOTIFICATION_STATE); switch (nState) { case 0: // Host UP or service OK sState = ((Integer) event.get(NOTIFICATION_TYPE) == NOTIFICATION_TYPE_HOST) ? "UP" : "OK"; break; case 1: // Host DOWN or service WARNING sState = ((Integer) event.get(NOTIFICATION_TYPE) == NOTIFICATION_TYPE_HOST) ? "DOWN" : "WARNING"; break; case 2: // Host UNREACHABLE or service CRITICAL sState = ((Integer) event.get(NOTIFICATION_TYPE) == NOTIFICATION_TYPE_HOST) ? "UNREACHABLE" : "CRITICAL"; break; case 3: // Host PENDING or service UNKNOWN sState = ((Integer) event.get(NOTIFICATION_TYPE) == NOTIFICATION_TYPE_HOST) ? "PENDING" : "UNKNOWN"; break; } // Build string content text String content = "[" + sState + "]\n" + (((Integer) event.get(NOTIFICATION_TYPE) == NOTIFICATION_TYPE_HOST) ? event.get(NOTIFICATION_HOST_NAME) : event.get(NOTIFICATION_SERVICE_NAME)) + ": " + event.get(NOTIFICATION_OUTPUT); // Set content text and info mNotiBuilder.setContentTxt(content); mNotiBuilder.setContentInfo((String) event.get(NOTIFICATION_CONTACT)); // Set ticker to display summary on status bar mNotiBuilder.setTicker("[" + sState + "] " + event.get(NOTIFICATION_OUTPUT)); // Intent to open main activity i = new Intent("mhst.dreamteam.intent.action.NEW_NOTIFICATION"); i.setClassName("mhst.dreamteam", "mhst.dreamteam.MainActivity"); // Request code to decide which fragment will be opened when main activity displayed i.putExtra("RequestCode", (Integer) event.get(NOTIFICATION_TYPE)); i.putExtra("RequestInfo", (Integer) event.get(NOTIFICATION_STATE)); // What to do when click notification (in this case, open main activity) PendingIntent pending = PendingIntent.getActivity(DataUpdater.this, (Integer) event.get(NOTIFICATION_TYPE), i, PendingIntent.FLAG_UPDATE_CURRENT); mNotiBuilder.setContentIntent(pending); // Notify the notification service mNotiMng.notify((Integer) event.get(NOTIFICATION_ID), mNotiBuilder.build().build()); } } } } private void updateSession() { ContentResolver resolver = getContentResolver(); Cursor c = resolver.query(Uri.parse(GlobalConst.CONTENT_SESSION_URI), null, null, null, null); if (c != null) { c.moveToFirst(); while (c.moveToNext()) { if (!isNullOrEmpty(c.getString(c.getColumnIndex("Cookie")))) { currentSession.isLogin(true); currentSession.setServer(c.getString(c.getColumnIndex("Server"))); currentSession.setCookie(c.getString(c.getColumnIndex("Cookie"))); } } } } private boolean isNullOrEmpty(String s) { return ((s == null) || (s.isEmpty())); } @Override @SuppressWarnings("unchecked") public void onComplete(Object obj, String sender) { if (obj != null) { ArrayList<Map<String, Object>> result = (ArrayList<Map<String, Object>>) obj; long startTime; for (Map<String, Object> o : result) { String[] split = ((String) o.get(NOTIFICATION_STARTTIME)).split(" "); startTime = Time.valueOf(split[1]).getTime(); if (startTime > lastChecked) { mEventStack.push(o); } } } isLoading = false; } }