Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 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.documentsui; |
| 18 | |
| 19 | import android.content.AsyncTaskLoader; |
| 20 | import android.content.Context; |
| 21 | |
| 22 | import com.android.documentsui.DocumentsActivity.State; |
| 23 | import com.android.documentsui.model.RootInfo; |
| 24 | |
| 25 | import java.util.Collection; |
| 26 | |
| 27 | public class RootsLoader extends AsyncTaskLoader<Collection<RootInfo>> { |
| 28 | private final RootsCache mRoots; |
| 29 | private final State mState; |
| 30 | |
| 31 | private Collection<RootInfo> mResult; |
| 32 | |
| 33 | public RootsLoader(Context context, RootsCache roots, State state) { |
| 34 | super(context); |
| 35 | mRoots = roots; |
| 36 | mState = state; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public final Collection<RootInfo> loadInBackground() { |
| 41 | return mRoots.getMatchingRootsBlocking(mState); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void deliverResult(Collection<RootInfo> result) { |
| 46 | if (isReset()) { |
| 47 | return; |
| 48 | } |
| 49 | Collection<RootInfo> oldResult = mResult; |
| 50 | mResult = result; |
| 51 | |
| 52 | if (isStarted()) { |
| 53 | super.deliverResult(result); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void onStartLoading() { |
| 59 | if (mResult != null) { |
| 60 | deliverResult(mResult); |
| 61 | } |
| 62 | if (takeContentChanged() || mResult == null) { |
| 63 | forceLoad(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void onStopLoading() { |
| 69 | cancelLoad(); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected void onReset() { |
| 74 | super.onReset(); |
| 75 | |
| 76 | // Ensure the loader is stopped |
| 77 | onStopLoading(); |
| 78 | |
| 79 | mResult = null; |
| 80 | } |
| 81 | } |