Android Open Source - Icinga-Mobile Data Updater






From Project

Back to project page Icinga-Mobile.

License

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.

Java Source Code

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;
    }
}




Java Source Code List

mhst.dreamteam.ApplicationContext.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.ApplicationTest.java
mhst.dreamteam.MainActivity.java
mhst.dreamteam.IcingaClient.GlobalConfig.java
mhst.dreamteam.IcingaClient.GlobalConst.java
mhst.dreamteam.IcingaClient.Controller.NetControllerTest.java
mhst.dreamteam.IcingaClient.Controller.NetController.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApiConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaApi.java
mhst.dreamteam.IcingaClient.Icinga.IcingaConst.java
mhst.dreamteam.IcingaClient.Icinga.IcingaExecutor.java
mhst.dreamteam.IcingaClient.Icinga.IcingaParam.java
mhst.dreamteam.IcingaClient.Icinga.IcingaUdt.java
mhst.dreamteam.IcingaClient.Icinga.package-info.java
mhst.dreamteam.IcingaClient.Interface.OnCompleteListener.java
mhst.dreamteam.IcingaClient.Interface.OnPieChartClickListener.java
mhst.dreamteam.IcingaClient.Json.JsonHelperTest.java
mhst.dreamteam.IcingaClient.Json.JsonHelper.java
mhst.dreamteam.IcingaClient.Misc.CookieMng.java
mhst.dreamteam.IcingaClient.Misc.CookieTest.java
mhst.dreamteam.IcingaClient.SessionMng.LogInTest.java
mhst.dreamteam.IcingaClient.SessionMng.Login.java
mhst.dreamteam.IcingaClient.SessionMng.Logout.java
mhst.dreamteam.IcingaClient.SessionMng.Session.java
mhst.dreamteam.IcingaService.ApplicationContext.java
mhst.dreamteam.IcingaService.DataUpdater.java
mhst.dreamteam.IcingaService.MessageReveicer.java
mhst.dreamteam.IcingaService.NotiBuilder.java
mhst.dreamteam.IcingaService.SQLHelper.java
mhst.dreamteam.IcingaService.SessionProvider.java
mhst.dreamteam.UI.Color.java
mhst.dreamteam.UI.GradientLine.java
mhst.dreamteam.UI.HostDetailsFragment.java
mhst.dreamteam.UI.HostlistAdapter.java
mhst.dreamteam.UI.HostlistFragment.java
mhst.dreamteam.UI.LoginActivity.java
mhst.dreamteam.UI.OverviewFragment.java
mhst.dreamteam.UI.PieGraph.java
mhst.dreamteam.UI.ProgressDialog.java
mhst.dreamteam.UI.ServiceDetailsFragment.java
mhst.dreamteam.UI.ServicelistAdapter.java
mhst.dreamteam.UI.ServicelistFragment.java
org.json.CDL.java
org.json.CookieList.java
org.json.Cookie.java
org.json.HTTPTokener.java
org.json.HTTP.java
org.json.JSONArray.java
org.json.JSONException.java
org.json.JSONML.java
org.json.JSONObject.java
org.json.JSONString.java
org.json.JSONStringer.java
org.json.JSONTokener.java
org.json.JSONWriter.java
org.json.Kim.java
org.json.Property.java
org.json.XMLTokener.java
org.json.XML.java
org.json.zip.BitInputStream.java
org.json.zip.BitOutputStream.java
org.json.zip.BitReader.java
org.json.zip.BitWriter.java
org.json.zip.Huff.java
org.json.zip.JSONzip.java
org.json.zip.Keep.java
org.json.zip.None.java
org.json.zip.PostMortem.java
org.json.zip.Unzipper.java
org.json.zip.Zipper.java