Android Open Source - Abstract-Model Async Model Factory






From Project

Back to project page Abstract-Model.

License

The source code is released under:

Apache License

If you think the Android project Abstract-Model 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.logician.abstractModel;
/*  www.  j a  v  a  2s.c om*/
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;

/**
 * A convenient class that wraps Model.Factory methods in AsyncTasks.
 * 
 * @see Model.Factory
 * @see AsyncTask
 * @author Logician
 *
 * @param <T> The Model class parameter of Model.Factory.
 */

public abstract class AsyncModelFactory<T extends Model>{
  
  
  private static Thread.UncaughtExceptionHandler mHandler = new Thread.UncaughtExceptionHandler() {
    
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
      Log.e("Exception in AsyncModelFactory", "Thread "+thread.getName(), ex);      
    }
  };
  
  /**
   * Set the UncaughtExceptionHandler to use in all AsyncTasks spawned by this class.
   * The default handler calls {@link Log#e(String, String, Throwable)} for convenience.
   * 
   * @see UncaughtExceptionHandler
   * @param handler The handler to use.
   */
  public static void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler){
    mHandler = handler;
  }
  
  /**
   * Returns the Model.Factory to wrap.
   * @return The Model.Factory to wrap.
   */
  public abstract Model.Factory<T> getFactory();
  
  /**
   * Callback for {@link Model.Factory#listFromCursor(Cursor)}
   * and {@link Model.Factory#listFromJSONArray(JSONArray)}.
   * 
   * @author Logician
   *
   * @param <T> The Model class parameter of Model.Factory.
   */
  public interface ListCallback<T>{
    public void onResult(List<T> models);
  }
  
  /**
   * Callback for {@link Model.Factory#listToJSONArray(List)}.
   * @author Logician
   *
   */
  public interface JSONArrayCallback{
    public void onResult(JSONArray json);
  }
  
  /**
   * Runs {@link Model.Factory#listFromCursor(Cursor)}
   * in an AsyncTask.
   *   
   * @param cursor The Cursor to pass to listFromCursor()
   * @param callback The callback that the instantiated list will be passed to.
   */
  public final void listFromCursor(Cursor cursor, ListCallback<T> callback){
    new CursorToListTask(getFactory(), callback, mHandler).execute(cursor);
  }
  
  /**
   * Runs {@link Model.Factory#listFromJSONArray(JSONArray)}
   * in an AsyncTask.
   * 
   * @param jA The JSONArray to pass to listFromJSONArray().
   * @param callback The callback that the instantiated list will be passed to.
   */
  public final void listFromJSONArray(JSONArray jA, ListCallback<T> callback){
    new JSONArrayToListTask(getFactory(), callback, mHandler).execute(jA);
  }
  
  /**
   * Runs {@link Model.Factory#listToJSONArray(List)}
   * in an AsyncTask.
   * 
   * @param models The list of Models to pass to listToJSONArray().
   * @param callback The callback that the created JSONArray will be passed to.
   */
  @SuppressWarnings("unchecked")
  public final void listToJSONArray(List<T> models, JSONArrayCallback callback){
    new ListToJSONArrayTask(getFactory(), callback, mHandler).execute(models);
  }
  
  
  private class CursorToListTask extends AsyncTask<Cursor, Void, List<T>>{
    private final ListCallback<T> callback;
    private final Model.Factory<T> factory;
    private final Thread.UncaughtExceptionHandler handler;
    public CursorToListTask(Model.Factory<T> factory, ListCallback<T> callback, Thread.UncaughtExceptionHandler handler){
      this.callback = callback;
      this.factory = factory;
      this.handler = handler;
    }

    @Override
    protected List<T> doInBackground(Cursor... arg0) {
      Thread.currentThread().setUncaughtExceptionHandler(handler);
      return factory.listFromCursor(arg0[0]);
    }
    
    @Override
    public void onPostExecute(List<T> list){
      callback.onResult(list);
    }
  }
  
  private class JSONArrayToListTask extends AsyncTask<JSONArray, Void, List<T>>{
    private final ListCallback<T> callback;
    private final Model.Factory<T> factory;
    private final Thread.UncaughtExceptionHandler handler;
    
    public JSONArrayToListTask(Model.Factory<T> factory, ListCallback<T> callback, Thread.UncaughtExceptionHandler handler){
      this.callback = callback;
      this.factory = factory;
      this.handler = handler;
    }

    @Override
    protected List<T> doInBackground(JSONArray... params) {
      Thread.currentThread().setUncaughtExceptionHandler(handler);
      return factory.listFromJSONArray(params[0]);
    }
    
    @Override
    public void onPostExecute(List<T> list){
      callback.onResult(list);
    }
  }
  
  private class ListToJSONArrayTask extends AsyncTask<List<T>, Void, JSONArray>{

    private final JSONArrayCallback callback;
    private final Model.Factory<T> factory;
    private final Thread.UncaughtExceptionHandler handler;
    
    public ListToJSONArrayTask(Model.Factory<T> factory, JSONArrayCallback callback, Thread.UncaughtExceptionHandler handler){
      this.callback = callback;
      this.factory = factory;
      this.handler = handler;
    }

    @Override
    protected JSONArray doInBackground(List<T>... models) {
      Thread.currentThread().setUncaughtExceptionHandler(handler);
      try {
        return factory.listToJSONArray(models[0]);
      } catch (JSONException e) {
        throw new RuntimeException(e);
      }
    }
    
    @Override
    public void onPostExecute(JSONArray jA){
      callback.onResult(jA);
    }
    
    
    
  }
  
  
}




Java Source Code List

com.logician.abstractModel.AsyncModelFactory.java
com.logician.abstractModel.BinarySerializable.java
com.logician.abstractModel.Column.java
com.logician.abstractModel.JSONSerializable.java
com.logician.abstractModel.ModelListAdapter.java
com.logician.abstractModel.Model.java
com.logician.abstractModel.Util.java
com.logician.abstractModel.examples.MyDatabaseOpenHelper.java
com.logician.abstractModel.examples.User.java
com.logician.abstractModel.examples.UsersActivity.java