Android Open Source - Munin-for-Android Grid






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.obj;
/*ww w .j  a  va2s .  com*/
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.chteuchteu.munin.MuninFoo;
import com.chteuchteu.munin.R;
import com.chteuchteu.munin.hlpr.GridDownloadHelper;
import com.chteuchteu.munin.hlpr.Util;

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

public class Grid {
  public long id;
  public String name;
  public int nbColumns;
  public int nbLines;
  public List<GridItem> items;
  public MuninFoo f;
  public GridDownloadHelper dHelper;
  
  private LinearLayout container;
  
  public MuninPlugin currentlyOpenedPlugin = null;
  
  public Grid(String name, MuninFoo f) {
    this.name = name;
    this.items = new ArrayList<>();
    this.nbColumns = 2;
    this.nbLines = 2;
    this.f = f;
  }
  
  private void preAdd(GridItem item) {
    // Check if exists
    boolean exists = false;
    for (GridItem i : items) {
      if (i.grid.name.equals(this.name) && i.plugin != null && i.plugin.equals(item.plugin))
        exists = true;
    }
    if (exists)
      return;
    
    // Add columns / lines if necessary
    while (item.X+1 > nbColumns)
      nbColumns++;
    while (item.Y+1 > nbLines)
      nbLines++;
    
    if (get(item.X, item.Y) == null)
      this.items.add(item);
    else
      MuninFoo.logE("This item cannot be placed (" + item.X + "," + item.Y + ")");
  }
  
  public void updateLayoutSizes(Context c) {
    for (int y=0; y<nbLines; y++) {
      for (int x=0; x<nbColumns; x++) {
        if (getViewAt(x, y) != null)
          updateGridSize(getViewAt(x, y), c);
      }
    }
  }
  
  public void add(GridItem item, Context c, MuninFoo f, boolean editView) {
    // Check if exists
    boolean exists = false;
    for (GridItem i : items) {
      if (i.grid.name.equals(name) && i.plugin.equals(item.plugin))
        exists = true;
    }
    if (exists)
      return;
    
    // Add columns / lines if necessary
    while (item.X+1 >= nbColumns)
      addColumn(c, editView);
    while (item.Y+1 >= nbLines)
      addLine(c, editView);
    
    if (get(item.X, item.Y) == null)
      this.items.add(item);
    else
      MuninFoo.logE("", "This item cannot be placed (" + item.X + "," + item.Y + ")");
    
    // Update this items size on this line
    for (int x=0; x<nbColumns; x++) {
      LinearLayout ll = getViewAt(x, item.Y);
      if (ll != null)
        updateGridSize(ll, c);
    }
    
    f.sqlite.saveGridItemRelations(this);
  }
  
  private GridItem get(int posX, int posY) {
    for (GridItem i : items) {
      if (i.X == posX && i.Y == posY)
        return i;
    }
    return null;
  }
  
  public LinearLayout buildLayout(Context c) {
    container = new LinearLayout(c);
    container.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    container.setOrientation(LinearLayout.VERTICAL);
    // Line per line
    LinearLayout line;
    for (int y=0; y<nbLines; y++) {
      line = new LinearLayout(c);
      line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      line.setOrientation(LinearLayout.HORIZONTAL);
      
      // Column per column
      for (int x=0; x<nbColumns; x++) {
        if (get(x, y) == null)
          line.addView(GridItem.getEmptyView(this, c, f, x, y));
        else
          line.addView(get(x, y).getView(c));
      }
      
      container.addView(line);
    }
    return container;
  }
  
  public void edit(Activity a) {
    a.findViewById(R.id.add_line_bottom).setVisibility(View.VISIBLE);
    a.findViewById(R.id.add_column_right).setVisibility(View.VISIBLE);
    reEnablePlusButtons();
  }
  
  public void cancelEdit(Context c) {
    ((Activity) c).findViewById(R.id.add_line_bottom).setVisibility(View.GONE);
    ((Activity) c).findViewById(R.id.add_column_right).setVisibility(View.GONE);
    for (GridItem i : items) {
      if (i.editing)
        i.cancelEdit();
    }
    disablePlusButtons();
    if (items.size() > 0) {
      removeEmptyColumns(c);
      removeEmptyLines();
    }
  }
  
  public void cancelAlpha() {
    for (GridItem i : items) {
      if (i.iv != null && i.iv.getAlpha() != 1.0f)
        i.iv.setAlpha(1f);
    }
  }

  public void toggleFootersVisibility(boolean visible) {
    for (GridItem i : items)
      i.footer.setVisibility(visible ? View.VISIBLE : View.GONE);
  }
  
  private void removeEmptyColumns(Context c) {
    for (int x=nbColumns-1; x>=0; x--) {
      if (isColumnEmpty(x))
        removeEmptyColumn(c, x);
      else // Stop removing columns when the target column isn't empty
        return;
    }
  }
  
  private void removeEmptyLines() {
    for (int y=nbLines-1; y>=0; y--) {
      if (isLineEmpty(y))
        removeEmptyLine(y);
      else // Stop removing lines when the target line isn't empty
        return;
    }
  }
  
  public int getGridItemHeight(Context c, int nbCol) {
    float ratio = (float) (800.0 / 600.0);
    int deviceWidth = Util.getDeviceSize(c)[0];
    int gridItemWidth = deviceWidth / nbCol;
    int gridItemHeight = Math.round(gridItemWidth / ratio);
    return gridItemHeight;
  }
  
  
  public void setupLayout() {
    List<GridItem> l = items;
    items = new ArrayList<>();
    for (GridItem i : l)
      this.preAdd(i);
  }
  
  public void addLine(Context c, boolean editView) {
    this.nbLines++;
    
    if (editView) {
      LinearLayout line = new LinearLayout(c);
      line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      line.setOrientation(LinearLayout.HORIZONTAL);
      for (int i=0; i<nbColumns; i++)
        line.addView(GridItem.getEmptyView(this, c, f, i, nbLines-1));
      
      container.addView(line);
      
      for (GridItem i : items) {
        if (i.editing)
          i.updateActionButtons();
      }
    }
  }
  
  public void addColumn(Context c, boolean editView) {
    this.nbColumns++;
    
    if (editView) {
      for (int i=0; i<nbLines; i++) {
        // Get the layout to add the view
        LinearLayout row = (LinearLayout) container.getChildAt(i);
        row.addView(GridItem.getEmptyView(this, c, f, nbColumns-1, i));
      }
      
      // Update each view
      updateAllGridSizes(c);
      
      for (GridItem i : items) {
        if (i.editing)
          i.updateActionButtonsAfterAddingColumn();
      }
    }
  }
  
  private boolean removeEmptyColumn(Context c, int x) {
    if (x >= nbColumns)
      return false;
    
    boolean canRemove = true;
    
    for (int y=0; y<nbLines; y++) {
      if (get(x, y) != null)
        canRemove = false;
    }
    
    if (canRemove) {
      for (int y=0; y<nbLines; y++) {
        LinearLayout toRemove = getViewAt(x, y);
        LinearLayout line = (LinearLayout) toRemove.getParent();
        line.removeView(toRemove);
      }
      nbColumns--;
      updateAllGridSizes(c);
      return true;
    }
    return false;
  }
  
  private boolean removeEmptyLine(int y) {
    if (y >= nbLines)
      return false;
    
    boolean canRemove = true;
    
    for (int x=0; x<nbColumns; x++) {
      if (get(x, y) != null)
        canRemove = false;
    }
    
    if (canRemove) {
      View v = getViewAt(0, y);
      if (v != null) {
        LinearLayout line = (LinearLayout) getViewAt(0, y).getParent();
        if (line != null) {
          container.removeView(line);
          nbLines--;
        }
      }
      return true;
    }
    return false;
  }
  
  public void remove(int x, int y) {
    items.remove(get(x, y));
  }
  
  public void move(int x, int y, int newX, int newY) {
    LinearLayout curView = getViewAt(x, y);
    LinearLayout destView = getViewAt(newX, newY);
    GridItem curItem = get(x, y);
    if (curItem != null) {
      GridItem destItem = get(newX, newY);
      if (destItem != null) {
        destItem.X = x;
        destItem.Y = y;
      }
      curItem.X = newX;
      curItem.Y = newY;
      curItem.updateActionButtons();
      
      swapViews(curView, destView);
    }
  }
  
  public void swapViews(LinearLayout view1, LinearLayout view2) {
    RelativeLayout content1 = (RelativeLayout) view1.getChildAt(0);
    RelativeLayout content2 = (RelativeLayout) view2.getChildAt(0);
    
    view1.removeView(content1);
    view2.removeView(content2);
    
    view1.addView(content2);
    view2.addView(content1);
  }
  
  private void disablePlusButtons() {
    for (int x=0; x<nbColumns; x++) {
      for (int y=0; y<nbLines; y++) {
        if (getViewAt(x, y) != null && get(x, y) == null) { // Empty view
          LinearLayout outerContainer = getViewAt(x, y);
          outerContainer.setVisibility(View.INVISIBLE);
        }
      }
    }
  }
  
  private void reEnablePlusButtons() {
    for (int x=0; x<nbColumns; x++) {
      for (int y=0; y<nbLines; y++) {
        if (getViewAt(x, y) != null && get(x, y) == null) { // Empty view
          LinearLayout outerContainer = getViewAt(x, y);
          outerContainer.setVisibility(View.VISIBLE);
        }
      }
    }
  }
  
  private void updateAllGridSizes(Context c) {
    for (int y=0; y<nbLines; y++) {
      for (int x=0; x<nbColumns; x++) {
        if (getViewAt(x, y) != null)
          updateGridSize(getViewAt(x, y), c);
      }
    }
  }
  
  private void updateGridSize(LinearLayout v, Context c) {
    if (v != null)
      v.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, getGridItemHeight(c, nbColumns), 1.0f));
  }
  
  public LinearLayout getViewAt(int x, int y) {
    LinearLayout row = (LinearLayout) container.getChildAt(y);
    if (row != null)
      return (LinearLayout) row.getChildAt(x);
    return null;
  }
  
  private int getGridWidth() {
    int curWidth = 0;
    for (GridItem i : items) {
      if (i.X > curWidth)
        curWidth = i.X;
    }
    return curWidth+1;
  }
  
  // Returns the width using getWidth,
  // less the empty columns at the end
  public int getFullWidth() {
    int w = getGridWidth()-1;
    int lastFullCol = w;
    for (int i=w;i>=0;i--) {
      if (isColumnEmpty(lastFullCol))
        lastFullCol--;
    }
    return lastFullCol + 1;
  }
  
  private int getGridHeight() {
    int curHeight = 0;
    for (GridItem i : items) {
      if (i.Y > curHeight)
        curHeight = i.Y;
    }
    return curHeight+1;
  }
  
  public int getFullHeight() {
    int h = getGridHeight()-1;
    int lastFullRow = h;
    for (int i=h; i>=0;i--) {
      if (isLineEmpty(lastFullRow))
        lastFullRow--;
    }
    return lastFullRow + 1;
  }
  
  private boolean isColumnEmpty(int x) {
    for (int y=0; y<nbLines; y++) {
      if (get(x, y) != null)
        return false;
    }
    return true;
  }
  
  private boolean isLineEmpty(int y) {
    for (int x=0; x<nbColumns; x++) {
      if (get(x, y) != null)
        return false;
    }
    return true;
  }
  
  public int[] getNextAvailable(int beginX, int beginY, int maxWidth, Context c) {
    int[] r = new int[2]; r[0] = beginX; r[1] = beginY;
    boolean available = get(beginX, beginY) == null;
    
    if(!available) {
      int curX = beginX;
      int curY = beginY;
      
      while (!available) {
        curX++;
        if (curX > maxWidth-1) {
          curX = 0;
          curY++;
          if (nbLines <= curY)
            addLine(c, true);
        }
        available = get(curX, curY) == null;
        if (available) {
          r[0] = curX; r[1] = curY;
          return r;
        }
      }
    }
    
    return r;
  }
}




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