Download Task : Download « Network « Android






Download Task

  

/**
 *
 */
//package org.alldroid.forum.net;

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;


import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import android.os.Bundle;

/**
 * @author trr4rac
 *
 */
 abstract class OnDownloadTaskCompletedListener<T> {
  private DownloadTask<T> task;
  private Bundle state;
  private Object tag;

  public OnDownloadTaskCompletedListener ( DownloadTask<T> task ) {
    this(task,null);
  }

  public OnDownloadTaskCompletedListener ( DownloadTask<T> task, Bundle state ) {
    this(task,state,null);
  }

  public OnDownloadTaskCompletedListener ( DownloadTask<T> task, Bundle state, Object tag ) {
    this.setTask ( task );
    this.setState(state);
    this.setTag ( tag );
  }

  public abstract void onCompletion ( T t ) throws Exception;

  /**
   * @param task the task to set
   */
  protected void setTask ( DownloadTask<T> task ) {
    this.task = task;
  }
  /**
   * @return the task
   */
  public DownloadTask<T> getTask ( ) {
    return task;
  }
  /**
   * @param state the state to set
   */
  public void setState ( Bundle state ) {
    this.state = state;
  }
  /**
   * @return the state
   */
  public Bundle getState ( ) {
    return state;
  }

  /**
   * @param tag the tag to set
   */
  public void setTag ( Object tag ) {
    this.tag = tag;
  }

  /**
   * @return the tag
   */
  public Object getTag ( ) {
    return tag;
  }
}

/**
 * @author trr4rac
 */
 abstract class DownloadTask<T> extends AsyncTask<HttpUriRequest, Integer, T> {

  private static final String TAG = DownloadTask.class.getSimpleName ( );
  private OnDownloadTaskCompletedListener<T> listener;
  private DefaultHttpClient httpClient;
  private UsernamePasswordCredentials credentials;
  private CookieStore cookieStore;
  private Exception executionException;

  public DownloadTask ( ) {

  }

  @Override
  protected void onPostExecute ( T result ) {
    if ( getOnDownloadTaskCompletedListener ( ) != null ) {
      try {
        Log.d(TAG,"notifying completed listener");
        getOnDownloadTaskCompletedListener ( ).onCompletion ( result );
      } catch ( Exception ex ) {
        this.setExecutionException ( ex );
        Log.e ( TAG, ex.getMessage ( ), ex );
      }
    } else {
      Log.w ( TAG, "listener was not set" );
    }
  }

  /*
   * (non-Javadoc)
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  @Override
  protected abstract T doInBackground ( HttpUriRequest... requests );

  public void setOnDownloadTaskCompletedListener ( OnDownloadTaskCompletedListener<T> l ) {
    this.listener = l;
  }

  public OnDownloadTaskCompletedListener<T> getOnDownloadTaskCompletedListener ( ) {
    return listener;
  }

  /**
   * @return the httpClient
   */
  protected DefaultHttpClient getHttpClient ( HttpUriRequest request ) {
    if ( request == null ) { throw new NullPointerException ( "request parameter cannot be null" ); }

    DefaultHttpClient httpClient = new DefaultHttpClient ( );
    if ( this.getCredentials ( ) != null ) {
      httpClient.getCredentialsProvider ( ).setCredentials ( new AuthScope ( request.getURI ( ).getHost ( ), request.getURI ( ).getPort ( ) ), this.getCredentials ( ) );
    }

    if ( this.getCookieStore ( ) != null ) {
      httpClient.setCookieStore ( this.getCookieStore ( ) );
    }

    return httpClient;
  }

  /**
   * @param credentials
   *          the credentials to set
   */
  public void setCredentials ( UsernamePasswordCredentials credentials ) {
    this.credentials = credentials;
  }

  /**
   * @return the credentials
   */
  public UsernamePasswordCredentials getCredentials ( ) {
    return credentials;
  }

  /**
   * @param cookieStore
   *          the cookieStore to set
   */
  public void setCookieStore ( CookieStore cookieStore ) {
    this.cookieStore = cookieStore;
  }

  /**
   * @return the cookieStore
   */
  public CookieStore getCookieStore ( ) {
    return cookieStore;
  }

  /**
   * @param httpClient the httpClient to set
   */
  public void setHttpClient ( DefaultHttpClient httpClient ) {
    this.httpClient = httpClient;
  }

  /**
   * @return the httpClient
   */
  public DefaultHttpClient getHttpClient ( ) {
    return httpClient;
  }

  /**
   * @param executionException the executionException to set
   */
  protected void setExecutionException ( Exception executionException ) {
    this.executionException = executionException;
  }

  /**
   * @return the executionException
   */
  public Exception getExecutionException ( ) {
    return executionException;
  }

}

   
    
  








Related examples in the same category

1.Android download file tools
2.Download Manager Demo
3.Using AsyncTask to download a big file
4.Using DownloadManager
5.File Download Service
6.Cacheable Downloader
7.Download Manager
8.Book Downloader
9.download File