Android Open Source - Munin-for-Android Munin Foo






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. j  a  va2  s  .  c  o m*/
import android.content.Context;
import android.util.Log;

import com.chteuchteu.munin.exc.NullMuninFooException;
import com.chteuchteu.munin.exc.TrialExpirationDateReached;
import com.chteuchteu.munin.hlpr.SQLite;
import com.chteuchteu.munin.hlpr.Util;
import com.chteuchteu.munin.obj.Label;
import com.chteuchteu.munin.obj.MuninMaster;
import com.chteuchteu.munin.obj.MuninPlugin;
import com.chteuchteu.munin.obj.MuninServer;
import com.crashlytics.android.Crashlytics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Main class of the app. This singleton is created on app launch
 * Its instance is retrieved using MuninFoo.getInstance().
 *  (we supply a Context reference on first getInstance call)
 */
public class MuninFoo {
  private static MuninFoo instance;
  public boolean languageLoaded = false;
  
  private List<MuninServer> servers;
  public List<Label> labels;
  public List<MuninMaster> masters;
  
  public SQLite sqlite;
  private MuninServer currentServer;

  private String userAgent;
  
  // === VERSION === //
  // HISTORY    current:   _______________________________________________________________________________________________________________________________
  // android:versionName:    | 1.1    1.2    1.3    1.4    1.4.1  1.4.2  1.4.5  1.4.6  2.0    2.0.1  2.1    2.2    2.3    2.4    2.5    2.6 |
  // android:versionCode:   |  1     2     3     4     5     6     7      8       10    11    12    13    14    15    16    17  |
  // MfA version:        | 1.1    1.2    1.3    1.4    1.5    1.6    1.7    1.8     1.9    2.0    2.1   2.2    2.3    2.4    2.5    2.6  |
  //              |-------------------------------------------------------------------------------------------------------------------------------|
  //              | 2.6.1    2.6.2  2.6.3  2.6.4  2.6.5  2.7    2.7.1  2.7.5  2.7.6  2.7.7  2.8    2.8.1  2.8.2  2.8.3  2.8.4  3.0 |
  //              |  18     19    20    21    22    23    24    25    26    27    28    29    30    31    32    33  |
  //              |  2.7    2.8    2.9    3.0    3.1    3.2    3.3    3.4    3.5    3.6    3.7    3.8    3.9    4.0    4.1    4.2 |
  //              |                              beta  beta  beta      fix    fix    fix    fix      |
  //              |-------------------------------------------------------------------------------------------------------------------------------|
  //              | 3.1    3.2    3.2.1   3.3    3.4    3.4.5  3.4.6  3.4.7  3.4.8                            |
  //              | 34    35    36    37    38    39    40    41    42                              |
  //              | 4.3    4.4    4.5    4.6    4.7    4.8    4.9    5.0    5.1                                |
  //              | fix        fix                fix    fix    fix                              |
  //              |-------------------------------------------------------------------------------------------------------------------------------|
  
  public static final double VERSION = 5.1;
  // =============== //
  private static final boolean FORCE_NOT_PREMIUM = false;

    // Allows an user to test the app until the TRIAL_EXPIRATION date is reached
    public static final boolean TRIAL = false;
    public static final Calendar TRIAL_EXPIRATION = new GregorianCalendar(2015, 1, 10);

    public boolean premium;
  
  // Import/Export webservice
  public static final String IMPORT_EXPORT_URI = "http://www.munin-for-android.com/ws/importExport.php";
  public static final int IMPORT_EXPORT_VERSION = 1;
  
  public Calendar alerts_lastUpdated;
  
  private MuninFoo(Context context) {
    premium = false;
    servers = new ArrayList<>();
    labels = new ArrayList<>();
    masters = new ArrayList<>();
    sqlite = new SQLite(context, this);
    instance = null;

    // User agent
    String userAgentPref = Util.getPref(context, Util.PrefKeys.UserAgent);
    this.userAgent = userAgentPref.equals("") ? generateUserAgent(context) : userAgentPref;

    loadInstance(context);

        if (TRIAL && isTrialExpired())
            throw new RuntimeException(new TrialExpirationDateReached("Trial has expired"));
  }
  
  public static boolean isLoaded() { return instance != null; }
  
  private void loadInstance(Context context) {
    this.masters = sqlite.dbHlpr.getMasters();
    this.servers = sqlite.dbHlpr.getServers(this.masters);
    this.labels = sqlite.dbHlpr.getLabels(this.masters);

    attachOrphanServers();

    if (BuildConfig.DEBUG)
      this.sqlite.logMasters();

    if (context != null) {
      // Set default server
      this.currentServer = getCurrentServer(context);
      
      this.premium = isPremium(context);
    }
  }

  public MuninServer getCurrentServer() { return getCurrentServer(null); }
  public MuninServer getCurrentServer(Context context) {
    updateCurrentServer(context);
    return this.currentServer;
  }
  public void updateCurrentServer(Context context) {
    if (this.currentServer != null)
      return;

    if (this.servers.isEmpty())
      return;

    if (context != null) {
      String defaultServerUrl = Util.getPref(context, Util.PrefKeys.DefaultServer);

      if (!defaultServerUrl.equals("")) {
        MuninServer defaultServer = getServer(defaultServerUrl);
        if (defaultServer != null) {
          this.currentServer = defaultServer;
          return;
        }
      }
    }

    // Failed to find the defaultServer
    if (this.servers.isEmpty()) {
      this.currentServer = null;
      return;
    }

    this.currentServer = this.servers.get(0);
  }
  public void setCurrentServer(MuninServer server) { this.currentServer = server; }
  
  public void resetInstance(Context context) {
    servers = new ArrayList<>();
    labels = new ArrayList<>();
    sqlite = new SQLite(context, this);
    loadInstance(context);
  }
  
  public static synchronized MuninFoo getInstance() {
    if (instance == null)
      Crashlytics.logException(
          new NullMuninFooException("getInstante() called without Context for the first time"));
    return instance;
  }
  
  public static synchronized MuninFoo getInstance(Context context) {
    if (instance == null)
      instance = new MuninFoo(context);
    return instance;
  }
  
  /**
   * Set a common parent to the servers which does not
   * have one after getting them
   */
  private void attachOrphanServers() {
    int n = 0;
    MuninMaster defMaster = new MuninMaster();
    defMaster.setName("Default");
    defMaster.defaultMaster = true;
    
    for (MuninServer s : this.servers) {
      if (s.getParent() == null) {
        s.setParent(defMaster);
        n++;
      }
    }
    
    if (n > 0)
      this.masters.add(defMaster);
  }
  
  public void addServer(MuninServer server) {
    boolean contains = false;
    int pos = -1;
    for (int i=0; i<servers.size(); i++) {
      if (servers.get(i) != null && servers.get(i).equalsApprox(server)) {
        contains = true; pos = i; break;
      }
    }
    if (contains) // Replacement
      servers.set(pos, server);
    else
      servers.add(server);
  }
  public void deleteServer(MuninServer s, boolean rebuildChildren) {
    if (rebuildChildren)
      s.getParent().rebuildChildren(this);
    
    // Delete from servers list
    this.servers.remove(s);
    
    if (rebuildChildren)
      s.getParent().rebuildChildren(this);
    
    // Update current server
    if (this.currentServer.equals(s) && this.servers.size() > 0)
      this.currentServer = this.servers.get(0);
  }
  public void deleteMuninMaster(MuninMaster master) { deleteMuninMaster(master, null); }
  public void deleteMuninMaster(MuninMaster master, Util.ProgressNotifier progressNotifier) {
    if (this.masters.remove(master)) {
      sqlite.dbHlpr.deleteMaster(master, true, progressNotifier);
      
      // Remove labels relations for the current session
      for (MuninServer server : master.getChildren()) {
        for (MuninPlugin plugin : server.getPlugins())
          removeLabelRelation(plugin);
      }
      
      this.servers.removeAll(master.getChildren());
    }
  }
  public boolean addLabel(Label l) {
    boolean contains = false;
    for (Label ml : labels) {
      if (ml.getName().equals(l.getName())) {
        contains = true; break;
      }
    }
    if (!contains) {
      labels.add(l);
      sqlite.saveLabels();
    }
    return !contains;
  }
  public boolean removeLabel(Label label) {
    List<Label> list = new ArrayList<>();
    boolean somthingDeleted = false;
    for (Label l : labels) {
      if (!l.equals(label))
        list.add(l);
      else
        somthingDeleted = true;
    }
    labels = list;
    if (somthingDeleted)
      sqlite.saveLabels();
    return somthingDeleted;
  }
  /**
   * When removing a plugin, delete its label relations.
   * Warning : this does not deletes it from the local db.
   * @param plugin MuninPlugin
   */
  public void removeLabelRelation(MuninPlugin plugin) {
    for (Label label : this.labels) {
      for (MuninPlugin labelPlugin : label.plugins) {
        if (labelPlugin.equals(plugin)) {
          label.plugins.remove(labelPlugin);
          return;
        }
      }
    }
  }

  public List<MuninServer> getServers() {
    return this.servers;
  }
  public MuninServer getServer(int pos) {
    if (pos >= 0 && pos < servers.size())
      return servers.get(pos);
    else
      return null;
  }
  @Deprecated
  public MuninServer getServerFromFlatPosition(int position) {
    // si pos -> 0 1 4 8 9 11
    // gSFFP(2) -> 4 (!= null)
    if (position >= 0 && position < getOrderedServers().size())
      return getOrderedServers().get(position);
    return null;
  }
  private MuninServer getServersInstanceFromMuninMasterInstance(MuninServer s) {
    for (MuninServer server : this.servers) {
      if (server.getId() == s.getId())
        return server;
    }
    // Couldn't get server from its id, trying with equals method
    for (MuninServer server : this.servers) {
      if (server.equalsApprox(s))
        return server;
    }
    return null;
  }
  public List<MuninServer> getOrderedServers() {
    List<MuninServer> l = new ArrayList<>();
    for (MuninMaster m : this.masters) {
      for (MuninServer s : m.getOrderedChildren())
        l.add(getServersInstanceFromMuninMasterInstance(s));
    }
    return l;
  }
  public List<MuninServer> getServersFromPlugin(MuninPlugin pl) {
    List<MuninServer> l = new ArrayList<>();
    for (MuninServer s : getOrderedServers()) {
      for (MuninPlugin p : s.getPlugins()) {
        if (p.equalsApprox(pl)) {
          l.add(s); break;
        }
      }
    }
    return l;
  }
  public MuninServer getServer(String url) {
    for (MuninServer s : servers) {
      if (s.getServerUrl().equals(url))
        return s;
    }
    return null;
  }
  public MuninPlugin getPlugin(int id) {
    for (MuninServer server : servers) {
      for (MuninPlugin plugin : server.getPlugins()) {
        if (plugin.getId() == id)
          return plugin;
      }
    }
    return null;
  }
  
  public ArrayList<MuninMaster> getMasters() { return (ArrayList<MuninMaster>) this.masters; }

  public MuninMaster getMasterById(int id) {
    for (MuninMaster m : this.masters) {
      if (m.getId() == id)
        return m;
    }
    return null;
  }
  
  public int getMasterPosition(MuninMaster m) {
    int i = 0;
    for (MuninMaster mas : this.masters) {
      if (mas.getId() == m.getId())
        return i;
      i++;
    }
    return 0;
  }
  
  public Label getLabel(String lname) {
    for (Label l : labels) {
      if (l.getName().equals(lname))
        return l;
    }
    return null;
  }

  public Label getLabel(long labelId) {
    for (Label label : labels) {
      if (label.getId() == labelId)
        return label;
    }
    return null;
  }
  
  public boolean contains(MuninMaster master) {
    for (MuninMaster m : masters) {
      if (m.equalsApprox(master))
        return true;
    }
    return false;
  }
  
  public List<List<MuninServer>> getGroupedServersList() {
    List<List<MuninServer>> l = new ArrayList<>();
    for (MuninMaster master : masters) {
      List<MuninServer> serversList = new ArrayList<>();
      serversList.addAll(master.getChildren());
      l.add(serversList);
    }
    return l;
  }
  
  /**
   * Returns true if we should retrieve servers information
   *  We consider alerts information as outdated after 10 minutes.
   *  Note : hitting refresh on alerts screen forces data refresh.
   * @return bool
   */
  public boolean shouldUpdateAlerts() {
    if (alerts_lastUpdated == null) {
      alerts_lastUpdated = Calendar.getInstance();
      return true;
    }
    
    Calendar updateTreshold = Calendar.getInstance();
    updateTreshold.add(Calendar.MINUTE, -10);
    
    // If the last time the information was retrieved is before
    // now -10 minutes, we should update it again.
    
    return alerts_lastUpdated.before(updateTreshold);
  }

  public static void log(String msg) { log("Munin for Android", msg); }
  public static void log(String tag, String msg) { if (BuildConfig.DEBUG) Log.i(tag, msg); }
  public static void logV(String msg) { logV("Munin for Android", msg); }
  public static void logV(String tag, String msg) { if (BuildConfig.DEBUG) Log.v(tag, msg); }
  public static void logE(String msg) { logE("Munin for Android", msg); }
  public static void logE(String tag, String msg) { if (BuildConfig.DEBUG) Log.e(tag, msg); }
  public static void logW(String msg) { logW("Munin for Android", msg); }
  public static void logW(String tag, String msg) { if (BuildConfig.DEBUG) Log.w(tag, msg); }

  public static boolean isPremium(Context c) {
        if (TRIAL)
            return true;

    if (Util.isPackageInstalled("com.chteuchteu.muninforandroidfeaturespack", c)) {
      if (BuildConfig.DEBUG && FORCE_NOT_PREMIUM)
        return false;
      if (BuildConfig.DEBUG)
        return true;
      //PackageManager manager = c.getPackageManager();
      //return (manager.checkSignatures("com.chteuchteu.munin", "com.chteuchteu.muninforandroidfeaturespack")
      //    == PackageManager.SIGNATURE_MATCH);
      return true;
    }
    return false;
  }

    public static boolean isTrialExpired() {
        Calendar today = Calendar.getInstance();
        return today.after(TRIAL_EXPIRATION);
    }

  /**
   * Generates "MuninForAndroid/3.0 (Android 4.4.4 KITKAT)" from context
   * @param context Application/activity context
   */
  public static String generateUserAgent(Context context) {
    if (context == null)
      return generateUserAgent();

    String appVersion = Util.getAppVersion(context);
    String androidVersion = Util.getAndroidVersion();
    String userAgent = "MuninForAndroid/" + appVersion + " (" + androidVersion + ")";
    log("User agent : " + userAgent);
    return userAgent;
  }

  /**
   * Context-less version of generateUserAgent(Context context)
   * Generates "MuninForAndroid (Android 4.4.4 KITKAT)"
   */
  private static String generateUserAgent() {
    String androidVersion = Util.getAndroidVersion();
    String userAgent = "MuninForAndroid (" + androidVersion + ")";
    log("User agent : " + userAgent);
    return userAgent;
  }

  public String getUserAgent() { return this.userAgent; }
  /**
   * Get user agent in a context where MuninFoo isn't probably loaded
   * @param context Valid context (not nullable)
   * @return User Agent
   */
  public static String getUserAgent(Context context) {
    String userAgentPref = Util.getPref(context, Util.PrefKeys.UserAgent);
    return userAgentPref.equals("") ? generateUserAgent(context) : userAgentPref;
  }
  public void setUserAgent(String val) { this.userAgent = val; }
}




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