Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -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 static com.android.documentsui.DirectoryFragment.TYPE_NORMAL; |
| 20 | import static com.android.documentsui.DirectoryFragment.TYPE_RECENT_OPEN; |
| 21 | import static com.android.documentsui.DirectoryFragment.TYPE_SEARCH; |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 22 | import static com.android.documentsui.DocumentsActivity.TAG; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 23 | |
| 24 | import android.content.ContentResolver; |
| 25 | import android.content.Context; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.CancellationSignal; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 29 | import android.provider.DocumentsContract.DocumentColumns; |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 31 | |
| 32 | import com.android.documentsui.model.Document; |
| 33 | import com.android.internal.util.Predicate; |
| 34 | import com.google.android.collect.Lists; |
| 35 | |
| 36 | import libcore.io.IoUtils; |
| 37 | |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 38 | import java.io.FileNotFoundException; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Comparator; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 42 | import java.util.LinkedList; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
| 45 | public class DirectoryLoader extends UriDerivativeLoader<List<Document>> { |
| 46 | |
| 47 | private final int mType; |
| 48 | private Predicate<Document> mFilter; |
| 49 | private Comparator<Document> mSortOrder; |
| 50 | |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 51 | /** |
| 52 | * Stub result that represents an internal error. |
| 53 | */ |
| 54 | public static class ExceptionResult extends LinkedList<Document> { |
| 55 | public final Exception e; |
| 56 | |
| 57 | public ExceptionResult(Exception e) { |
| 58 | this.e = e; |
| 59 | } |
| 60 | } |
| 61 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 62 | public DirectoryLoader(Context context, Uri uri, int type, Predicate<Document> filter, |
| 63 | Comparator<Document> sortOrder) { |
| 64 | super(context, uri); |
| 65 | mType = type; |
| 66 | mFilter = filter; |
| 67 | mSortOrder = sortOrder; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public List<Document> loadInBackground(Uri uri, CancellationSignal signal) { |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 72 | try { |
| 73 | return loadInBackgroundInternal(uri, signal); |
| 74 | } catch (Exception e) { |
| 75 | return new ExceptionResult(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private List<Document> loadInBackgroundInternal(Uri uri, CancellationSignal signal) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 80 | final ArrayList<Document> result = Lists.newArrayList(); |
| 81 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 82 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 83 | final Cursor cursor = resolver.query(uri, null, null, null, getQuerySortOrder(), signal); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 84 | try { |
| 85 | while (cursor != null && cursor.moveToNext()) { |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 86 | Document doc = null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 87 | switch (mType) { |
| 88 | case TYPE_NORMAL: |
| 89 | case TYPE_SEARCH: |
| 90 | doc = Document.fromDirectoryCursor(uri, cursor); |
| 91 | break; |
| 92 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 93 | try { |
| 94 | doc = Document.fromRecentOpenCursor(resolver, cursor); |
| 95 | } catch (FileNotFoundException e) { |
| 96 | Log.w(TAG, "Failed to find recent: " + e); |
| 97 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 98 | break; |
| 99 | default: |
| 100 | throw new IllegalArgumentException("Unknown type"); |
| 101 | } |
| 102 | |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 103 | if (doc != null && (mFilter == null || mFilter.apply(doc))) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 104 | result.add(doc); |
| 105 | } |
| 106 | } |
| 107 | } finally { |
| 108 | IoUtils.closeQuietly(cursor); |
| 109 | } |
| 110 | |
| 111 | if (mSortOrder != null) { |
| 112 | Collections.sort(result, mSortOrder); |
| 113 | } |
| 114 | |
| 115 | return result; |
| 116 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame^] | 117 | |
| 118 | private String getQuerySortOrder() { |
| 119 | if (mSortOrder instanceof Document.DateComparator) { |
| 120 | return DocumentColumns.LAST_MODIFIED + " DESC"; |
| 121 | } else if (mSortOrder instanceof Document.NameComparator) { |
| 122 | return DocumentColumns.DISPLAY_NAME + " ASC"; |
| 123 | } else if (mSortOrder instanceof Document.SizeComparator) { |
| 124 | return DocumentColumns.SIZE + " DESC"; |
| 125 | } else { |
| 126 | return null; |
| 127 | } |
| 128 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 129 | } |