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