1 /* 2 * Copyright (C) 2019 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.settings.common; 18 19 import android.annotation.Nullable; 20 import android.content.Context; 21 22 import androidx.loader.content.AsyncTaskLoader; 23 24 /** 25 * This class fills in some boilerplate for AsyncTaskLoader to actually load things. 26 * Classes that extend {@link AsyncLoader} need to properly implement required methods expressed in 27 * {@link AsyncTaskLoader} 28 * 29 * <p>Taken from {@link com.android.settingslib.utils.AsyncLoader}. Only change to extend from 30 * support library {@link AsyncTaskLoader} 31 * 32 * @param <T> the data type to be loaded. 33 */ 34 public abstract class AsyncLoader<T> extends AsyncTaskLoader<T> { 35 @Nullable 36 private T mResult; 37 AsyncLoader(Context context)38 public AsyncLoader(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(T data)59 public void deliverResult(T data) { 60 if (isReset()) { 61 return; 62 } 63 mResult = data; 64 if (isStarted()) { 65 super.deliverResult(data); 66 } 67 } 68 69 @Override onReset()70 protected void onReset() { 71 super.onReset(); 72 onStopLoading(); 73 mResult = null; 74 } 75 } 76