Advertisement
Guest User

Untitled

a guest
Jun 9th, 2017
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. ------------ MyCustomLoader.java ------------
  2.  
  3. public class MyCustomLoader extends AsyncTaskLoader<PojoWrapper> {
  4.   final ForceLoadContentObserver mObserver;
  5.  
  6.   Uri mUri;
  7.   String[] mProjection;
  8.   String mSelection;
  9.   String[] mSelectionArgs;
  10.   String mSortOrder;
  11.  
  12.   PojoWrapper wrapper;
  13.   CancellationSignal mCancellationSignal;
  14.  
  15.   /* Runs on a worker thread */
  16.   @Override
  17.   public PojoWrapper loadInBackground() {
  18.     synchronized (this) {
  19.       if (isLoadInBackgroundCanceled()) {
  20.         throw new OperationCanceledException();
  21.       }
  22.       mCancellationSignal = new CancellationSignal();
  23.     }
  24.     try {
  25.       Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
  26.           mSelectionArgs, mSortOrder, mCancellationSignal);
  27.       if (cursor != null) {
  28.         try {
  29.           // Ensure the cursor window is filled.
  30.           cursor.getCount();
  31.           cursor.registerContentObserver(mObserver);
  32.         } catch (RuntimeException ex) {
  33.           cursor.close();
  34.           throw ex;
  35.         }
  36.       }
  37.       // `CursorLoader` performs following:
  38.       // return cursor;
  39.  
  40.  
  41.       // We perform some operation here with `cursor`
  42.       // and return PojoWrapper, that consists of `cursor` and `List<Pojo>`
  43.       return new PojoWrapper(cursor, new ArrayList<Pojo>());
  44.     } finally {
  45.       synchronized (this) {
  46.         mCancellationSignal = null;
  47.       }
  48.     }
  49.   }
  50.  
  51.   @Override
  52.   public void cancelLoadInBackground() {
  53.     super.cancelLoadInBackground();
  54.  
  55.     synchronized (this) {
  56.       if (mCancellationSignal != null) {
  57.         mCancellationSignal.cancel();
  58.       }
  59.     }
  60.   }
  61.  
  62.   /* Runs on the UI thread */
  63.   @Override
  64.   public void deliverResult(PojoWrapper wrapper) {
  65.     Cursor cursor = wrapper.cursor;
  66.     if (isReset()) {
  67.       // An async query came in while the loader is stopped
  68.       if (cursor != null) {
  69.         cursor.close();
  70.       }
  71.       return;
  72.     }
  73.     Cursor oldCursor = this.wrapper.cursor;
  74.     this.wrapper.cursor = cursor;
  75.  
  76.     if (isStarted()) {
  77.       super.deliverResult(wrapper);
  78.     }
  79.  
  80.     if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
  81.       oldCursor.close();
  82.     }
  83.   }
  84.  
  85.   /**
  86.    * Creates an empty unspecified CursorLoader.  You must follow this with
  87.    * calls to {@link #setUri(Uri)}, {@link #setSelection(String)}, etc
  88.    * to specify the query to perform.
  89.    */
  90.   public MyCustomLoader(Context context) {
  91.     super(context);
  92.     mObserver = new ForceLoadContentObserver();
  93.   }
  94.  
  95.   /**
  96.    * Creates a fully-specified CursorLoader.  See
  97.    * {@link ContentResolver#query(Uri, String[], String, String[], String)
  98.    * ContentResolver.query()} for documentation on the meaning of the
  99.    * parameters.  These will be passed as-is to that call.
  100.    */
  101.   public MyCustomLoader(Context context, Uri uri, String[] projection, String selection,
  102.       String[] selectionArgs, String sortOrder) {
  103.     super(context);
  104.     mObserver = new ForceLoadContentObserver();
  105.     mUri = uri;
  106.     mProjection = projection;
  107.     mSelection = selection;
  108.     mSelectionArgs = selectionArgs;
  109.     mSortOrder = sortOrder;
  110.   }
  111.  
  112.   /**
  113.    * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
  114.    * will be called on the UI thread. If a previous load has been completed and is still valid
  115.    * the result may be passed to the callbacks immediately.
  116.    *
  117.    * Must be called from the UI thread
  118.    */
  119.   @Override
  120.   protected void onStartLoading() {
  121.     if (wrapper != null) {
  122.       deliverResult(wrapper);
  123.     }
  124.     if (takeContentChanged() || wrapper == null) {
  125.       forceLoad();
  126.     }
  127.   }
  128.  
  129.   /**
  130.    * Must be called from the UI thread
  131.    */
  132.   @Override
  133.   protected void onStopLoading() {
  134.     // Attempt to cancel the current load task if possible.
  135.     cancelLoad();
  136.   }
  137.  
  138.   @Override
  139.   public void onCanceled(PojoWrapper wrapper) {
  140.     Cursor cursor = wrapper.cursor;
  141.     if (cursor != null && !cursor.isClosed()) {
  142.       cursor.close();
  143.     }
  144.   }
  145.  
  146.   @Override
  147.   protected void onReset() {
  148.     super.onReset();
  149.  
  150.     // Ensure the loader is stopped
  151.     onStopLoading();
  152.  
  153.     Cursor cursor = wrapper.cursor;
  154.     if (cursor != null && !cursor.isClosed()) {
  155.       cursor.close();
  156.     }
  157.     wrapper.cursor = null;
  158.   }
  159.  
  160.   public Uri getUri() {
  161.     return mUri;
  162.   }
  163.  
  164.   public void setUri(Uri uri) {
  165.     mUri = uri;
  166.   }
  167.  
  168.   public String[] getProjection() {
  169.     return mProjection;
  170.   }
  171.  
  172.   public void setProjection(String[] projection) {
  173.     mProjection = projection;
  174.   }
  175.  
  176.   public String getSelection() {
  177.     return mSelection;
  178.   }
  179.  
  180.   public void setSelection(String selection) {
  181.     mSelection = selection;
  182.   }
  183.  
  184.   public String[] getSelectionArgs() {
  185.     return mSelectionArgs;
  186.   }
  187.  
  188.   public void setSelectionArgs(String[] selectionArgs) {
  189.     mSelectionArgs = selectionArgs;
  190.   }
  191.  
  192.   public String getSortOrder() {
  193.     return mSortOrder;
  194.   }
  195.  
  196.   public void setSortOrder(String sortOrder) {
  197.     mSortOrder = sortOrder;
  198.   }
  199. }
  200.  
  201. ------------ PojoWrapper.java ------------
  202.  
  203. public class PojoWrapper {
  204.   Cursor cursor;
  205.   List<Pojo> list;
  206.  
  207.   public PojoWrapper(Cursor cursor, List<Pojo> list) {
  208.     this.cursor = cursor;
  209.     this.list = list;
  210.   }
  211. }
  212.  
  213. ------------ Pojo.java ------------
  214.  
  215. public class Pojo {
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement