Home | History | Annotate | Download | only in loader
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.car.settingslib.loader;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.support.v4.content.AsyncTaskLoader;
     22 
     23 /**
     24  * This class fills in some boilerplate for AsyncTaskLoader to actually load things.
     25  * Classes the extend {@link AsyncLoader} need to properly implement required methods expressed in
     26  * {@link AsyncTaskLoader}
     27  *
     28  * <p>Taken from {@link com.android.settingslib.utils.AsyncLoader}. Only change to extend from
     29  * support library {@link AsyncTaskLoader}
     30  *
     31  * @param <T> the data type to be loaded.
     32  */
     33 public abstract class AsyncLoader<T> extends AsyncTaskLoader<T> {
     34     @Nullable
     35     private T mResult;
     36 
     37     public AsyncLoader(Context context) {
     38         super(context);
     39     }
     40 
     41     @Override
     42     protected void onStartLoading() {
     43         if (mResult != null) {
     44             deliverResult(mResult);
     45         }
     46 
     47         if (takeContentChanged() || mResult == null) {
     48             forceLoad();
     49         }
     50     }
     51 
     52     @Override
     53     protected void onStopLoading() {
     54         cancelLoad();
     55     }
     56 
     57     @Override
     58     public void deliverResult(T data) {
     59         if (isReset()) {
     60             return;
     61         }
     62         mResult = data;
     63         if (isStarted()) {
     64             super.deliverResult(data);
     65         }
     66     }
     67 
     68     @Override
     69     protected void onReset() {
     70         super.onReset();
     71         onStopLoading();
     72         mResult = null;
     73     }
     74 }
     75