Progress Monitor : Progress « UI « Android






Progress Monitor

   
//package org.anddev.andengine.util.progress;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 18:07:35 - 09.07.2009
 */
public class ProgressMonitor implements IProgressListener {
  //          
  // Constants
  //          

  //          
  // Fields
  //          

  private final ProgressMonitor mParentProgressMonitor;
  private final IProgressListener mListener;

  private int mSubMonitorRangeFrom = 0;
  private int mSubMonitorRangeTo = 100;

  private int mProgress = 0;

  //          
  // Constructors
  //          

  public ProgressMonitor(final IProgressListener pListener) {
    this.mListener = pListener;
    this.mParentProgressMonitor = null;
  }

  public ProgressMonitor(final ProgressMonitor pParent){
    this.mListener = null;
    this.mParentProgressMonitor = pParent;
  }

  //          
  // Getter & Setter
  //          

  public ProgressMonitor getParentProgressMonitor() {
    return this.mParentProgressMonitor;
  }

  public int getProgress() {
    return this.mProgress;
  }

  public void setSubMonitorRange(final int pSubMonitorRangeFrom, final int pSubMonitorRangeTo) {
    this.mSubMonitorRangeFrom = pSubMonitorRangeFrom;
    this.mSubMonitorRangeTo = pSubMonitorRangeTo;
  }

  //          
  // Methods for/from SuperClass/Interfaces
  //          

  /**
   * @param pProgress between 0 and 100.
   */
  @Override
  public void onProgressChanged(final int pProgress){
    this.mProgress = pProgress;
    if(this.mParentProgressMonitor != null) {
      this.mParentProgressMonitor.onSubProgressChanged(pProgress);
    } else {
      this.mListener.onProgressChanged(pProgress);
    }
  }

  private void onSubProgressChanged(final int pSubProgress){
    final int subRange = this.mSubMonitorRangeTo- this.mSubMonitorRangeFrom;
    final int subProgressInRange = this.mSubMonitorRangeFrom + (int)(subRange * pSubProgress / 100f);

    if(this.mParentProgressMonitor != null) {
      this.mParentProgressMonitor.onSubProgressChanged(subProgressInRange);
    }else{
      this.mListener.onProgressChanged(subProgressInRange);
    }
  }
}


/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 20:52:44 - 03.01.2010
 */
 interface ProgressCallable<T> {
  //          
  // Final Fields
  //          

  //          
  // Methods
  //          

  /**
   * Computes a result, or throws an exception if unable to do so.
   * @param pProgressListener
   * @return computed result
   * @throws Exception if unable to compute a result
   */
  public T call(final IProgressListener pProgressListener) throws Exception;
}
/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 18:07:35 - 09.07.2009
 */
 interface IProgressListener {
  //          
  // Constants
  //          

  //          
  // Methods
  //          

  /**
   * @param pProgress between 0 and 100.
   */
  public void onProgressChanged(final int pProgress);
}

   
    
    
  








Related examples in the same category

1.Using progress bar
2.Using Thread and Progress bar
3.Round ProgressBar vs Flat ProgressBar
4.Use progress bars as widgets and in the title bar. The progress bar in the title will be shown until the progress is complete, at which point it fades away.
5.Using indeterminate progress bars as widgets and in the window's title bar.
6.Use an indeterminate progress indicator in the window's title bar.