Android Open Source - Munin-for-Android Service_ Notifications






From Project

Back to project page Munin-for-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project Munin-for-Android 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 com.chteuchteu.munin;
/*w w  w  .  ja v a2 s . c o  m*/
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.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.Vibrator;

import com.chteuchteu.munin.hlpr.DatabaseHelper;
import com.chteuchteu.munin.hlpr.Util;
import com.chteuchteu.munin.obj.MuninMaster;
import com.chteuchteu.munin.obj.MuninPlugin;
import com.chteuchteu.munin.obj.MuninPlugin.AlertState;
import com.chteuchteu.munin.obj.MuninServer;
import com.chteuchteu.munin.ui.Activity_Alerts;

import java.util.ArrayList;
import java.util.List;

/**
 * Notifications Service
 * Launched when enabling notifications from the app or
 * on device boot using BootReceiver class
 */
public class Service_Notifications extends Service {
  private WakeLock mWakeLock;
  
  /**
   * Simply return null, since our Service will not be communicating with
   * any other components. It just does its work silently.
   */
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  
  @SuppressWarnings("deprecation")
  private void handleIntent(Intent intent) {
    // obtain the wake lock
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.chteuchteu.munin");
    mWakeLock.acquire();
    
    // check the global background data setting
    ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    if (!cm.getBackgroundDataSetting()) {
      stopSelf();
      return;
    }
    
    NetworkInfo mWifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean wifiOnly = Util.getPref(Service_Notifications.this, Util.PrefKeys.Notifs_WifiOnly).equals("true");
    
    if (!wifiOnly || mWifi.isConnected())
      new PollTask().execute();
    else {
      if (mWakeLock.isHeld())
        mWakeLock.release();
      stopSelf();
    }
  }
  
  private class PollTask extends AsyncTask<Void, Void, Void> {
    private int nbCriticals;
    private int nbWarnings;
    private int nbServers;
    private String criticalPlugins;
    private String warningPlugins;
    
    @Override
    protected Void doInBackground(Void... params) {
      List<MuninServer> servers = new ArrayList<>();
      String serversList = Util.getPref(Service_Notifications.this, Util.PrefKeys.Notifs_ServersList);
      String[] serversToWatch = serversList.split(";");

      DatabaseHelper dbHelper = new DatabaseHelper(Service_Notifications.this);
      List<MuninMaster> masters = new ArrayList<>();
      List<MuninServer> dbServers = dbHelper.getServers(masters);
      
      nbCriticals = 0;
      nbWarnings = 0;
      nbServers = 0;
      criticalPlugins = "";
      warningPlugins = "";
      
      for (MuninServer s: dbServers) {
        for (String url : serversToWatch) {
          if (s.equalsApprox(url))
            servers.add(s);
        }
      }
      
      for (MuninServer s: servers)
        s.fetchPluginsStates(MuninFoo.getUserAgent(Service_Notifications.this));
      
      
      for (MuninServer s: servers) {
        boolean throatingServer = false;
        for (MuninPlugin p: s.getPlugins()) {
          if (p != null) {
            if (p.getState() == AlertState.CRITICAL || p.getState() == AlertState.WARNING)
              throatingServer = true;
            if (p.getState() == AlertState.CRITICAL) {
              criticalPlugins = criticalPlugins + p.getFancyName() + ", ";
              nbCriticals++;
            }
            else if (p.getState() == AlertState.WARNING) {
              warningPlugins = warningPlugins + p.getFancyName() + ", ";
              nbWarnings++;
            }
          }
        }
        if (throatingServer)
          nbServers++;
      }
      
      return null;
    }
    
    @SuppressWarnings("deprecation")
    @Override
    protected void onPostExecute(Void result) {
      //<string name="text58"> critical / criticals /&amp;amp; / warning / warnings /on / server/ servers</string>
      String[] strings = getString(R.string.text58).split("/");
      
      String notifTitle = "";
      if (nbCriticals > 0 && nbWarnings > 0) {
        notifTitle = nbCriticals + "";
        if (nbCriticals == 1)
          notifTitle += " " + strings[0];
        else
          notifTitle += strings[1];
        notifTitle += strings[2];
        notifTitle += nbWarnings;
        if (nbWarnings == 1)
          notifTitle += strings[3];
        else
          notifTitle += strings[4];
        notifTitle += strings[5];
        notifTitle += nbServers;
        if (nbServers == 1)
          notifTitle += strings[6];
        else
          notifTitle += strings[7];
        //String titreNotification = nbCriticals + " criticals & " + nbWarnings + " warnings on " + nbServers + " servers";
      } else if (nbCriticals == 0 && nbWarnings > 0) {
        notifTitle = nbWarnings + "";
        if (nbWarnings == 1)
          notifTitle += strings[3];
        else
          notifTitle += strings[4];
        notifTitle += strings[5];
        notifTitle += nbServers;
        if (nbServers == 1)
          notifTitle += strings[6];
        else
          notifTitle += strings[7];
      } else if (nbCriticals > 0 && nbWarnings == 0) {
        notifTitle = nbCriticals + "";
        if (nbCriticals == 1)
          notifTitle += " " + strings[0];
        else
          notifTitle += strings[1];
        notifTitle += strings[5];
        notifTitle += nbServers;
        if (nbServers == 1)
          notifTitle += strings[6];
        else
          notifTitle += strings[7];
      }
      
      String notifText;
      
      if (criticalPlugins.length() > 2 && criticalPlugins.substring(criticalPlugins.length()-2).equals(", "))
        criticalPlugins = criticalPlugins.substring(0, criticalPlugins.length()-2);
      if (warningPlugins.length() > 2 && warningPlugins.substring(warningPlugins.length()-2).equals(", "))
        warningPlugins = warningPlugins.substring(0, warningPlugins.length()-2);
      
      if (nbCriticals > 0 && nbWarnings > 0)
        notifText = criticalPlugins + ", " + warningPlugins;
      else if (nbCriticals > 0)
        notifText = criticalPlugins;
      else
        notifText = warningPlugins;
      
      if (nbCriticals > 0 || nbWarnings > 0) {
        if (!Util.getPref(Service_Notifications.this, Util.PrefKeys.Notifs_LastNotificationText).equals(notifText)) {
          NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
          Notification notification = new Notification(R.drawable.launcher_icon_mono, getString(R.string.app_name), System.currentTimeMillis());
          
          PendingIntent pendingIntent = PendingIntent.getActivity(Service_Notifications.this, 0,
              new Intent(Service_Notifications.this, Activity_Alerts.class), 0);
          notification.setLatestEventInfo(Service_Notifications.this, notifTitle, notifText, pendingIntent);

          // Dismiss notification on click
          notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
          
          Util.setPref(Service_Notifications.this, Util.PrefKeys.Notifs_LastNotificationText, notifText);

          if (Util.getPref(Service_Notifications.this, Util.PrefKeys.Notifs_Vibrate).equals("true"))
            vibrate();
          
          notificationManager.notify(1234, notification);
        }
      } else {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(1234);
      }
      
      // Important : release wake lock in the end
      stopSelf();
      if (mWakeLock.isHeld())
        mWakeLock.release();
    }
  }

  private void vibrate() {
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if (v.hasVibrator())
      v.vibrate(500);
  }
  
  /**
   * Returning START_NOT_STICKY tells the system to not restart the
   * service if it is killed because of poor resource (memory/cpu) conditions.
   */
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    handleIntent(intent);
    return START_NOT_STICKY;
  }
  
  /**
   * In onDestroy() we release our wake lock. This ensures that whenever the
   * Service stops (killed for resources, stopSelf() called, etc.), the wake
   * lock will be released.
   */
  public void onDestroy() {
    super.onDestroy();
    if (mWakeLock.isHeld())
      mWakeLock.release();
  }
}




Java Source Code List

com.chteuchteu.munin.BootReceiver.java
com.chteuchteu.munin.CustomSSLFactory.java
com.chteuchteu.munin.MuninFoo.java
com.chteuchteu.munin.Service_Notifications.java
com.chteuchteu.munin.adptr.Adapter_ExpandableListView.java
com.chteuchteu.munin.adptr.Adapter_GraphView.java
com.chteuchteu.munin.adptr.Adapter_IconList.java
com.chteuchteu.munin.adptr.Adapter_SeparatedList.java
com.chteuchteu.munin.exc.ImportExportWebserviceException.java
com.chteuchteu.munin.exc.NullMuninFooException.java
com.chteuchteu.munin.exc.TrialExpirationDateReached.java
com.chteuchteu.munin.hlpr.BillingService.java
com.chteuchteu.munin.hlpr.DatabaseHelper.java
com.chteuchteu.munin.hlpr.DigestUtils.java
com.chteuchteu.munin.hlpr.DocumentationHelper.java
com.chteuchteu.munin.hlpr.DrawerHelper.java
com.chteuchteu.munin.hlpr.DynazoomHelper.java
com.chteuchteu.munin.hlpr.EncryptionHelper.java
com.chteuchteu.munin.hlpr.GridDownloadHelper.java
com.chteuchteu.munin.hlpr.I18nHelper.java
com.chteuchteu.munin.hlpr.ImportExportHelper.java
com.chteuchteu.munin.hlpr.JSONHelper.java
com.chteuchteu.munin.hlpr.MediaScannerUtil.java
com.chteuchteu.munin.hlpr.NetHelper.java
com.chteuchteu.munin.hlpr.SQLite.java
com.chteuchteu.munin.hlpr.Util.java
com.chteuchteu.munin.obj.AlertsWidget.java
com.chteuchteu.munin.obj.GraphWidget.java
com.chteuchteu.munin.obj.GridItem.java
com.chteuchteu.munin.obj.Grid.java
com.chteuchteu.munin.obj.HTTPResponse_Bitmap.java
com.chteuchteu.munin.obj.HTTPResponse.java
com.chteuchteu.munin.obj.Label.java
com.chteuchteu.munin.obj.MuninMaster.java
com.chteuchteu.munin.obj.MuninPlugin.java
com.chteuchteu.munin.obj.MuninServer.java
com.chteuchteu.munin.obj.SearchResult.java
com.chteuchteu.munin.ui.Activity_About.java
com.chteuchteu.munin.ui.Activity_AlertsPluginSelection.java
com.chteuchteu.munin.ui.Activity_Alerts.java
com.chteuchteu.munin.ui.Activity_GoPremium.java
com.chteuchteu.munin.ui.Activity_GraphView.java
com.chteuchteu.munin.ui.Activity_Grid.java
com.chteuchteu.munin.ui.Activity_Grids.java
com.chteuchteu.munin.ui.Activity_Label.java
com.chteuchteu.munin.ui.Activity_Labels.java
com.chteuchteu.munin.ui.Activity_Main.java
com.chteuchteu.munin.ui.Activity_Notifications.java
com.chteuchteu.munin.ui.Activity_Plugins.java
com.chteuchteu.munin.ui.Activity_Server.java
com.chteuchteu.munin.ui.Activity_ServersEdit.java
com.chteuchteu.munin.ui.Activity_Servers.java
com.chteuchteu.munin.ui.Activity_Settings.java
com.chteuchteu.munin.ui.HackyDrawerLayout.java
com.chteuchteu.munin.ui.MuninActivity.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_ViewsFactory.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetProvider.java
com.chteuchteu.munin.wdget.Widget_AlertsWidget_WidgetService.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_Configure.java
com.chteuchteu.munin.wdget.Widget_GraphWidget_WidgetProvider.java
com.mobeta.android.dslv.DragSortController.java
com.mobeta.android.dslv.DragSortCursorAdapter.java
com.mobeta.android.dslv.DragSortItemViewCheckable.java
com.mobeta.android.dslv.DragSortItemView.java
com.mobeta.android.dslv.DragSortListView.java
com.mobeta.android.dslv.ResourceDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleDragSortCursorAdapter.java
com.mobeta.android.dslv.SimpleFloatViewManager.java
org.taptwo.android.widget.CircleFlowIndicator.java
org.taptwo.android.widget.FlowIndicator.java
org.taptwo.android.widget.TitleFlowIndicator.java
org.taptwo.android.widget.TitleProvider.java
org.taptwo.android.widget.ViewFlow.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.IPhotoView.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java