Android Open Source - Resonos-Android-Framework Tool Bar






From Project

Back to project page Resonos-Android-Framework.

License

The source code is released under:

Apache License

If you think the Android project Resonos-Android-Framework 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.resonos.apps.library.widget;
//ww  w  .j  av a2s .c om
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.resonos.app.library.R;
import com.resonos.apps.library.Action;
import com.resonos.apps.library.FragmentBaseActivity;
import com.resonos.apps.library.util.ParameterList;

/**
 * This class creates a toolbar of buttons similar to the style of the ActionBar.
 * @author Chris
 */
public abstract class ToolBar extends LinearLayout implements ToolBarButton.OnPressButtonListener {
  
  // context
  private Activity cx;
  
  // object pools
  private ArrayList<LinearLayout> mRows;
  private ArrayList<ToolBarButton> mButtons;
  private ArrayList<SpacerView> mSpacers;

  // objects
  private ArrayList<Action> mMenuItems = new ArrayList<Action>();
    private Animation mFadeInAnimation;
    private Animation mFadeOutAnimation;
    private ModalToolbarDialog mModal;
  private Handler mHandler = new Handler();

    // vars
  private int mSpacersUsed, mRowsUsed;
    private boolean bVrt; // convenience var
    private int mResTBB;
  private ParameterList<Param> mParams;
  private boolean bSetVisibilityOnInit = false, bPendingVisibility;
  
  /**
   * Parameters for the ToolBar class.
   */
  public enum Param {
    /** Make this a vertical toolbar. Incompatible with many of the more special features, common sense applies */
    VERTICAL,
    /** Use Action Mode style to draw this toolbar */
    STYLE_ACTION_MODE,
    /** Start out with the toolbar hidden */
    START_HIDDEN,
    /** Use the Split style */
    STYLE_SPLIT,
    /** Put spacers on the outside of the toolbar buttons instead of just between them. */
    OUTER_SPACERS
  };
  
  /**
   * The ModalToolbarDialog interface can be implemented by any popup window
   * that the Toolbar opens.
   */
  public interface ModalToolbarDialog {
    /** Show the dialog */
    public void show();
    /** Close the dialog */
    public void close();
  }
  
  /**
   * This class puts even spacing between ToolBarButtons.
   */
  public class SpacerView extends View {
    LinearLayout.LayoutParams lllp;

    /**
     * Create a new SpacerView
     * @param context
     */
    public SpacerView(Context context) {
      super(context);
      lllp = new LinearLayout.LayoutParams(
          bVrt ? LayoutParams.MATCH_PARENT : 0,
              bVrt ? 0 : LayoutParams.MATCH_PARENT, 1);
    }
  }
  
  public ToolBar(Context context) {
    super(context);
  }

  public ToolBar(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public ToolBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }
  
  /**
   * Call this method to initialize your toolbar and pass your desired parameters to it.
   * Make sure the toolbar is added to the view hierarchy before you call this function.
   * (see {@link Param}).
   * @param cx : the context
   * @param params : the parameters
   */
  public void init(Activity cx, Param... params) {
    this.cx = cx;
    mParams = new ParameterList<Param>(params);
    bVrt = mParams.has(Param.VERTICAL);
    mResTBB = bVrt ? R.layout.toolbar_button_vrt : R.layout.toolbar_button;
        mFadeInAnimation = AnimationUtils.loadAnimation(cx, android.R.anim.fade_in);
        mFadeOutAnimation = AnimationUtils.loadAnimation(cx, android.R.anim.fade_out);

        mButtons = new ArrayList<ToolBarButton>();
        mSpacers = new ArrayList<SpacerView>();
        mRows = new ArrayList<LinearLayout>();
        
    setOrientation(bVrt ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
    
    updateLayout();
    
    if ((bSetVisibilityOnInit && !bPendingVisibility)
        || (mParams.has(Param.START_HIDDEN) && !bSetVisibilityOnInit))
      setVisibility(View.GONE);
    
    // styling
    boolean bMode = mParams.has(Param.STYLE_ACTION_MODE);
        boolean bSplit = mParams.has(Param.STYLE_SPLIT);
        int r = 0;
        if (bMode) {
          if (bVrt)
            r = bSplit ? R.drawable.tb_mode_right : R.drawable.tb_mode_left;
          else
            r = bSplit ? R.drawable.tb_mode_bottom : R.drawable.tb_mode_top;
        } else {
          if (bVrt)
            r = bSplit ? R.drawable.tb_right : R.drawable.tb_left;
          else
            r = bSplit ? R.drawable.tb_bottom : R.drawable.tb_top;
        }
        setBackgroundResource(r);

        // create the buttons!
        invalidateToolbar();
  }
  
  /**
   * Return whether the toolbar has been initialized for display
   */
  public boolean isInitialized() {
    return cx != null;
  }
  
  /**
   * Updates the layout after an orientation change.
   */
  private void updateLayout() {
    this.getLayoutParams().width = bVrt ? (FragmentBaseActivity.AB_HEIGHT * mRowsUsed)
        : ViewGroup.LayoutParams.MATCH_PARENT;
    this.getLayoutParams().height = bVrt ? ViewGroup.LayoutParams.MATCH_PARENT
        : (FragmentBaseActivity.AB_HEIGHT * mRowsUsed);
    this.requestLayout();
  }

  /**
   * Returns a button based on the tag associated with it
   * @param e : the enum tag.
   * @return The {@link ToolBarButton}, or null if not found
   */
  public View getChildFromTag(Enum<?> e) {
    for (int i = 0; i < mMenuItems.size(); i++) {
      if (mMenuItems.get(i).tag.equals(e))
        return mButtons.get(i);
    }
    return null;
  }

  /**
   * Add a new row.
   * @param index : the index of the row
   * @return The LinearLayout of the new row.
   */
  private LinearLayout addRow() {
    LinearLayout sv;
    while (mRows.size() <= mRowsUsed) {
      sv = new LinearLayout(cx);
      mRows.add(sv);
      addView(sv);
    }
    sv = mRows.get(mRowsUsed);
    mRowsUsed++;

    sv.setOrientation(bVrt ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);
    return sv;
  }

  /**
   * Invalidate the Toolbar, causing {@link onCreateMenu} to be called again.
   */
  public void invalidateToolbar() {
    if (this.getVisibility() != View.VISIBLE)
          return;
    Action a;
    mMenuItems.clear();
    onCreateMenu(mMenuItems);
    mSpacersUsed = 0;
    mRowsUsed = 0;
    LinearLayout row = addRow();
    boolean outerSpacers = mParams.has(Param.OUTER_SPACERS);
    for (int i = 0; i < mMenuItems.size(); i++) {
      a = mMenuItems.get(i);
      if (outerSpacers)
        addSpacer(row);
      if (a.newRow) {
        row = addRow();
        if (outerSpacers)
          addSpacer(row);
      }
      addToolBarItem(row, i, a, mMenuItems.size());
      if (outerSpacers)
        addSpacer(row);
      if (i < (mMenuItems.size() - 1))
        if (!mMenuItems.get(i + 1).newRow)
          addSpacer(row);
    }
    int count = mMenuItems.size();

    for (int i = 0; i < mButtons.size(); i++) {
      if (i >= count)
        mButtons.get(i).setVisibility(View.GONE);
    }

    for (int i = 0; i < mSpacers.size(); i++) {
      if (i >= mSpacersUsed)
        mSpacers.get(i).setVisibility(View.GONE);
    }

    for (int i = 0; i < mRows.size(); i++) {
      if (i >= mRowsUsed)
        mRows.get(i).setVisibility(View.GONE);
    }
    this.updateLayout();
    }

  /**
   * Add a new {@link SpacerView} to the ToolBar
   * @param row : the row to add the view to.
   */
  private void addSpacer(LinearLayout row) {
    SpacerView sv;
    while (mSpacers.size() <= mSpacersUsed) {
      sv = new SpacerView(cx);
      mSpacers.add(sv);
      row.addView(sv, sv.lllp);
    }
    sv = mSpacers.get(mSpacersUsed);
    mSpacersUsed++;

    // now set it up
    sv.setVisibility(View.VISIBLE);
  }

  /**
   * Add a new item to the toolbar.
   * @param row : the ViewGroup to add to.
   * @param index : the index of the button
   * @param action : the {@link Action} describing the button
   * @param itemCount : the total number of buttons there will be
   */
  private void addToolBarItem(LinearLayout row, int index, Action action, int itemCount) {
    // get the right tbb object, create if necessary
        LayoutInflater li = (LayoutInflater)cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ToolBarButton tbb;
    while (mButtons.size() <= index) {
      tbb = (ToolBarButton)li.inflate(mResTBB, null);
      tbb.setPressListener(this);
      mButtons.add(tbb);
      row.addView(tbb);
    }
    tbb = mButtons.get(index);
    tbb.setLayoutParams(new LinearLayout.LayoutParams(
        (itemCount == 1) ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT,
            LayoutParams.MATCH_PARENT));
    
    tbb.setup(action, bVrt);
  }

  /**
   * This function is called every time the ToolBar is invalidated.
   * Add the actions you would like in the ToolBar to items
   * @param items : the arraylist to add new {@link Action}s to
   */
  protected abstract void onCreateMenu(ArrayList<Action> items);

  /**
   * Close the toolbar without animation, and any modal dialog open.
   */
  public void close() {
    closeModal();
      mHandler.removeCallbacks(setViewGone);
    setVisibility(View.GONE);
    cx = null;
  }

  /**
   * Show the toolbar, using a fade animation.
   */
  public void show() {
    if (!isInitialized()) {
      bSetVisibilityOnInit = true;
      bPendingVisibility = true;
      return;
    }
        if (!isShowing()) {
          mHandler.removeCallbacks(setViewGone);
          startAnimation(mFadeInAnimation);
          setVisibility(View.VISIBLE);
          invalidateToolbar();
        }
  }

  /**
   * Hide the toolbar, using a fade animation.
   */
  public void hide() {
    if (!isInitialized()) {
      bSetVisibilityOnInit = true;
      bPendingVisibility = false;
      return;
    }
    closeModal();
        if (isShowing()) {
          startAnimation(mFadeOutAnimation);
          setVisibility(View.INVISIBLE);
          mHandler.postDelayed(setViewGone, mFadeOutAnimation.getDuration());
        }
  }
  
  @Override
  public void onDetachedFromWindow() {
      mHandler.removeCallbacks(setViewGone);
  }
  
  /** Runnable to actually hide the toolbar after the fade out */
  private Runnable setViewGone = new Runnable() {
    public void run() {
      setVisibility(GONE);
    }
  };

  /**
   * Get the visibility of the ToolBar.
   * @return true if the ToolBar is showing.
   */
  public boolean isShowing() {
    return (getVisibility() == View.VISIBLE);
  }
  
  /**
   * Close a modal dialog, if open.
   */
  public void closeModal() {
      if (mModal != null) {
        mModal.close();
        mModal = null;
      }
  }
  
  /**
   * Show a modal dialog and set it as the ToolBar's current modal dialog.
   * @param modal : the popup window implementing {@link ModalToolbarDialog}
   */
  public void showModal(ModalToolbarDialog modal) {
    closeModal();
    mModal = modal;
    if (modal != null)
      modal.show();
  }

  @Override
  public void onButtonPressed(ToolBarButton tbb) {
    int id = tbb.getId();
    for (int i = 0; i < mMenuItems.size(); i++) {
      if (id == mMenuItems.get(i).id) {
        onItemSelected(mMenuItems.get(i).tag);
        return;
      }
    }
  }

  /**
   * This method is called when an item from the toolbar is selected.
   * @param action : This is the enum tag associated with the action.
   * It is easiest to cast action to your enum type and then make a switch case block using it.
   */
  protected abstract void onItemSelected(Enum<?> action);
}




Java Source Code List

com.resonos.apps.library.Action.java
com.resonos.apps.library.AlertFragment.java
com.resonos.apps.library.App.java
com.resonos.apps.library.BaseFragment.java
com.resonos.apps.library.FragmentBaseActivity.java
com.resonos.apps.library.file.AltAndroidFileHandle.java
com.resonos.apps.library.file.AltAndroidFiles.java
com.resonos.apps.library.file.AltFileHandle.java
com.resonos.apps.library.file.FileCache.java
com.resonos.apps.library.media.AudioVisualizer.java
com.resonos.apps.library.media.BitmapMemoryCache.java
com.resonos.apps.library.media.HueColorFilter.java
com.resonos.apps.library.media.ImageLoader.java
com.resonos.apps.library.media.MediaScannerNotifier.java
com.resonos.apps.library.model.Coord.java
com.resonos.apps.library.model.ImmutableCoord.java
com.resonos.apps.library.tabviewpager.CustomViewPager.java
com.resonos.apps.library.tabviewpager.PageIndicator.java
com.resonos.apps.library.tabviewpager.TabPageIndicator.java
com.resonos.apps.library.tabviewpager.TabViewPagerAdapter.java
com.resonos.apps.library.tabviewpager.TabViewPagerFragment.java
com.resonos.apps.library.tabviewpager.TitleProvider.java
com.resonos.apps.library.util.AppUtils.java
com.resonos.apps.library.util.ErrorReporter.java
com.resonos.apps.library.util.LifecycleTaskQueue.java
com.resonos.apps.library.util.M.java
com.resonos.apps.library.util.NetworkClient.java
com.resonos.apps.library.util.NetworkRequest.java
com.resonos.apps.library.util.ParameterList.java
com.resonos.apps.library.util.SensorReader.java
com.resonos.apps.library.util.TouchViewWorker.java
com.resonos.apps.library.util.ViewServer.java
com.resonos.apps.library.widget.DashboardLayout.java
com.resonos.apps.library.widget.FormBuilder.java
com.resonos.apps.library.widget.FormElement.java
com.resonos.apps.library.widget.ListFormBuilder.java
com.resonos.apps.library.widget.PopupWindows3D.java
com.resonos.apps.library.widget.QuickAction3D.java
com.resonos.apps.library.widget.RangeSeekBar.java
com.resonos.apps.library.widget.SeekBar.java
com.resonos.apps.library.widget.ToolBarButton.java
com.resonos.apps.library.widget.ToolBar.java