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.settingslib.utils;
18 
19 import android.content.Context;
20 
21 import androidx.loader.content.AsyncTaskLoader;
22 
23 /**
24  * This class fills in some boilerplate for AsyncTaskLoader to actually load things.
25  *
26  * Subclasses need to implement {@link AsyncLoaderCompat#loadInBackground()} to perform the actual
27  * background task, and {@link AsyncLoaderCompat#onDiscardResult(T)} to clean up previously loaded
28  * results.
29  *
30  * This loader is based on the MailAsyncTaskLoader from the AOSP EmailUnified repo.
31  *
32  * @param <T> the data type to be loaded.
33  */
34 public abstract class AsyncLoaderCompat<T> extends AsyncTaskLoader<T> {
35     private T mResult;
36 
AsyncLoaderCompat(final Context context)37     public AsyncLoaderCompat(final Context context) {
38         super(context);
39     }
40 
41     @Override
onStartLoading()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
onStopLoading()53     protected void onStopLoading() {
54         cancelLoad();
55     }
56 
57     @Override
deliverResult(final T data)58     public void deliverResult(final T data) {
59         if (isReset()) {
60             if (data != null) {
61                 onDiscardResult(data);
62             }
63             return;
64         }
65 
66         final T oldResult = mResult;
67         mResult = data;
68 
69         if (isStarted()) {
70             super.deliverResult(data);
71         }
72 
73         if (oldResult != null && oldResult != mResult) {
74             onDiscardResult(oldResult);
75         }
76     }
77 
78     @Override
onReset()79     protected void onReset() {
80         super.onReset();
81 
82         onStopLoading();
83 
84         if (mResult != null) {
85             onDiscardResult(mResult);
86         }
87         mResult = null;
88     }
89 
90     @Override
onCanceled(final T data)91     public void onCanceled(final T data) {
92         super.onCanceled(data);
93 
94         if (data != null) {
95             onDiscardResult(data);
96         }
97     }
98 
99     /**
100      * Called when discarding the load results so subclasses can take care of clean-up or
101      * recycling tasks. This is not called if the same result (by way of pointer equality) is
102      * returned again by a subsequent call to loadInBackground, or if result is null.
103      *
104      * Note that this may be called concurrently with loadInBackground(), and in some circumstances
105      * may be called more than once for a given object.
106      *
107      * @param result The value returned from {@link AsyncLoaderCompat#loadInBackground()} which
108      *               is to be discarded.
109      */
onDiscardResult(T result)110     protected abstract void onDiscardResult(T result);
111 }
112