• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.documentsui.inspector;
17 
18 import android.content.AsyncTaskLoader;
19 import android.content.Context;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.provider.DocumentsContract;
23 import android.support.annotation.Nullable;
24 import android.util.Log;
25 
26 import java.io.FileNotFoundException;
27 
28 /**
29  * Loads metadata from {@link DocumentsContract#getDocumentMetadata(android.content.ContentProviderClient, Uri, String[])}
30  */
31 final class MetadataLoader extends AsyncTaskLoader<Bundle> {
32 
33     private static final String TAG = "MetadataLoader";
34 
35     private final Context mContext;
36     private final Uri mUri;
37 
38     private @Nullable Bundle mMetadata;
39 
MetadataLoader(Context context, Uri uri)40     MetadataLoader(Context context, Uri uri) {
41         super(context);
42         mContext = context;
43         mUri = uri;
44     }
45 
46     @Override
loadInBackground()47     public Bundle loadInBackground() {
48         try {
49             return DocumentsContract.getDocumentMetadata(mContext.getContentResolver(), mUri);
50         } catch (FileNotFoundException e) {
51             Log.e(TAG, "Failed to load metadata for doc: " + mUri, e);
52         }
53 
54         return null;
55     }
56 
57     @Override
onStartLoading()58     protected void onStartLoading() {
59         if (mMetadata != null) {
60             deliverResult(mMetadata);
61         }
62         if (takeContentChanged() || mMetadata == null) {
63             forceLoad();
64         }
65     }
66 
67     @Override
deliverResult(Bundle metadata)68     public void deliverResult(Bundle metadata) {
69         if (isReset()) {
70             return;
71         }
72         mMetadata = metadata;
73         if (isStarted()) {
74             super.deliverResult(metadata);
75         }
76     }
77 
78     /**
79      * Must be called from the UI thread
80      */
81     @Override
onStopLoading()82     protected void onStopLoading() {
83         // Attempt to cancel the current load task if possible.
84         cancelLoad();
85     }
86 
87     @Override
onReset()88     protected void onReset() {
89         super.onReset();
90 
91         // Ensure the loader is stopped
92         onStopLoading();
93 
94         mMetadata = null;
95     }
96 }
97