Android Open Source - android-weather-demo-application Base Async Task






From Project

Back to project page android-weather-demo-application.

License

The source code is released under:

GNU General Public License

If you think the Android project android-weather-demo-application 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 org.openweathermap.asynctask.base;
//from  w  w w  .j  a  va  2s.co  m
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.os.AsyncTask;

/**
 * Inherit this method to run and async tasks and return its result to 
 * the UI thread or service. An activity or service reference must be provided to
 * receive a response.
 * @author samkirton
 */
public abstract class BaseAsyncTask extends AsyncTask<IParam, Void, BaseResponse>  {
  private Context mContext;
  private AsyncCallback mAsyncCallback;
  private BaseResponse mResult;
  private BaseAsyncTask mInstanceRef;
  
  public interface AsyncCallback {
    public void onAsyncTaskFinished(BaseResponse response, BaseAsyncTask asyncTask);
  }
  
  /**
   * Run the result of the AsyncTask on the GUI thread
   */
  private Runnable doUpdateGUI = new Runnable() {
    public void run() { 
      mAsyncCallback.onAsyncTaskFinished(mResult, mInstanceRef);
    } 
  };
  
  public void setAsyncCallback(AsyncCallback newVal) {
    mAsyncCallback = newVal;
  }
  
  public Context getContext() {
    return mContext;
  }
  
  /**
   * Constructor
   * @param  applicationContext  The application context
   */
  public BaseAsyncTask(Context context) {
    if (!(context instanceof Activity) && !(context instanceof Service)) {
      throw new IllegalArgumentException("The AsyncTask context must be able to cast into Activity or Service");
    }
    
    mContext = context;
  }
  
  /**
   * An override of the AsyncTask execute method that takes a single
   * BaseContext param
   * @param  baseContext  The BaseContext to use as params
   */
  public void runTask(IParam param) {
    IParam[] params = new IParam[1];
    params[0] = param;
    this.execute(params);
  }
  
  @Override
  protected BaseResponse doInBackground(IParam... params) {
    IParam baseParam = null;
    if (params !=  null && params.length > 0) {
      baseParam = params[0];
    }
    
    return run(baseParam);
  }
  
  @Override
  protected void onPostExecute(BaseResponse result) {
    mResult = result;
    mInstanceRef = this;
    
    if (mContext instanceof Activity) {
      ((Activity)mContext).runOnUiThread(doUpdateGUI);
    } else if (mContext instanceof Service) {
      mAsyncCallback.onAsyncTaskFinished(mResult, mInstanceRef);
    }
  }
  
  @Override
  protected void onPreExecute() { }
  
  @Override
  protected void onProgressUpdate(Void... values) { }
  
  /**
   * Run the async logic based on the context provided
   * @param  baseParam  The param for the logic
   * @return  Return a response
   */
  protected abstract BaseResponse run(IParam param);
}




Java Source Code List

org.openweathermap.WeatherApplication.java
org.openweathermap.activity.MainActivity.java
org.openweathermap.activity.base.BaseActivity.java
org.openweathermap.adapter.CityAdapter.java
org.openweathermap.adapter.ForecastAdapter.java
org.openweathermap.asynctask.GetCitiesAsyncTask.java
org.openweathermap.asynctask.GetLastCityAsyncTask.java
org.openweathermap.asynctask.SaveForecastAsyncTask.java
org.openweathermap.asynctask.SearchCitiesAsyncTask.java
org.openweathermap.asynctask.base.BaseAsyncTask.java
org.openweathermap.asynctask.base.BaseResponse.java
org.openweathermap.asynctask.base.IParam.java
org.openweathermap.asynctask.param.SaveForecastParam.java
org.openweathermap.asynctask.param.SearchCitiesParam.java
org.openweathermap.asynctask.response.GetCitiesResponse.java
org.openweathermap.asynctask.response.GetLastCityResponse.java
org.openweathermap.asynctask.response.SaveForecastResponse.java
org.openweathermap.dto.CityDTO.java
org.openweathermap.dto.CoordDTO.java
org.openweathermap.dto.DayDTO.java
org.openweathermap.dto.ResultDTO.java
org.openweathermap.dto.TempDTO.java
org.openweathermap.dto.WeatherDTO.java
org.openweathermap.dto.base.IDTO.java
org.openweathermap.fragment.ForecastFragment.java
org.openweathermap.fragment.ModalDialogFragment.java
org.openweathermap.fragment.SelectCountryFragment.java
org.openweathermap.sql.SQLInitProvider.java
org.openweathermap.sql.model.CityModel.java
org.openweathermap.sql.model.IconModel.java
org.openweathermap.sql.model.WeatherModel.java
org.openweathermap.utils.DateHelper.java
org.openweathermap.utils.RESTProvider.java
org.openweathermap.utils.ReflectionHelper.java
org.openweathermap.utils.ResourceHelper.java
org.openweathermap.utils.StringHelper.java
org.openweathermap.view.CityListItemView.java
org.openweathermap.view.DateDisplayView.java
org.openweathermap.view.ViewPagerFade.java
org.openweathermap.view.ViewPagerIndicatorView.java
org.openweathermap.view.WeatherDataRowView.java
org.openweathermap.volley.callback.VolleyResponseCallback.java
org.openweathermap.volley.provider.VolleyImageLoader.java
org.openweathermap.volley.provider.VolleyRequest.java