Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2013 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.content.AsyncTaskLoader;
     21 import android.content.Context;
     22 
     23 /**
     24  * This class fills in some boilerplate for AsyncTaskLoader to actually load things.
     25  *
     26  * Subclasses need to implement {@link MailAsyncTaskLoader#loadInBackground()} to perform the actual
     27  * background task, and {@link MailAsyncTaskLoader#onDiscardResult(T)} to clean up previously loaded
     28  * results.
     29  */
     30 
     31 public abstract class MailAsyncTaskLoader<T> extends AsyncTaskLoader<T> {
     32     private T mResult;
     33 
     34     public MailAsyncTaskLoader(final Context context) {
     35         super(context);
     36     }
     37 
     38     @Override
     39     protected void onStartLoading() {
     40         if (mResult != null) {
     41             deliverResult(mResult);
     42         }
     43 
     44         if (takeContentChanged() || mResult == null) {
     45             forceLoad();
     46         }
     47     }
     48 
     49     @Override
     50     protected void onStopLoading() {
     51         cancelLoad();
     52     }
     53 
     54     @Override
     55     public void deliverResult(final T data) {
     56         if (isReset()) {
     57             if (data != null) {
     58                 onDiscardResult(data);
     59             }
     60             return;
     61         }
     62 
     63         final T oldResult = mResult;
     64         mResult = data;
     65 
     66         if (isStarted()) {
     67             super.deliverResult(data);
     68         }
     69 
     70         if (oldResult != null && oldResult != mResult) {
     71             onDiscardResult(oldResult);
     72         }
     73     }
     74 
     75     @Override
     76     protected void onReset() {
     77         super.onReset();
     78 
     79         onStopLoading();
     80 
     81         if (mResult != null) {
     82             onDiscardResult(mResult);
     83         }
     84         mResult = null;
     85     }
     86 
     87     @Override
     88     public void onCanceled(final T data) {
     89         super.onCanceled(data);
     90 
     91         if (data != null) {
     92             onDiscardResult(data);
     93         }
     94     }
     95 
     96     /**
     97      * Called when discarding the load results so subclasses can take care of clean-up or
     98      * recycling tasks. This is not called if the same result (by way of pointer equality) is
     99      * returned again by a subsequent call to loadInBackground, or if result is null.
    100      *
    101      * Note that this may be called concurrently with loadInBackground(), and in some circumstances
    102      * may be called more than once for a given object.
    103      *
    104      * @param result The value returned from {@link MailAsyncTaskLoader#loadInBackground()} which
    105      *               is to be discarded.
    106      */
    107     protected abstract void onDiscardResult(final T result);
    108 }
    109