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 | 873daa3 | 2013-08-18 17:38:20 -0700 | [diff] [blame^] | 82 | // TODO: subscribe to the notify uri from query |
| 83 | |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 84 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 85 | final Cursor cursor = resolver.query(uri, null, null, null, getQuerySortOrder(), signal); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 86 | try { |
| 87 | while (cursor != null && cursor.moveToNext()) { |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 88 | Document doc = null; |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 89 | switch (mType) { |
| 90 | case TYPE_NORMAL: |
| 91 | case TYPE_SEARCH: |
| 92 | doc = Document.fromDirectoryCursor(uri, cursor); |
| 93 | break; |
| 94 | case TYPE_RECENT_OPEN: |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 95 | try { |
| 96 | doc = Document.fromRecentOpenCursor(resolver, cursor); |
| 97 | } catch (FileNotFoundException e) { |
| 98 | Log.w(TAG, "Failed to find recent: " + e); |
| 99 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 100 | break; |
| 101 | default: |
| 102 | throw new IllegalArgumentException("Unknown type"); |
| 103 | } |
| 104 | |
Jeff Sharkey | 744dbbd | 2013-08-07 18:33:33 -0700 | [diff] [blame] | 105 | if (doc != null && (mFilter == null || mFilter.apply(doc))) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 106 | result.add(doc); |
| 107 | } |
| 108 | } |
| 109 | } finally { |
| 110 | IoUtils.closeQuietly(cursor); |
| 111 | } |
| 112 | |
| 113 | if (mSortOrder != null) { |
| 114 | Collections.sort(result, mSortOrder); |
| 115 | } |
| 116 | |
| 117 | return result; |
| 118 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 119 | |
| 120 | private String getQuerySortOrder() { |
| 121 | if (mSortOrder instanceof Document.DateComparator) { |
| 122 | return DocumentColumns.LAST_MODIFIED + " DESC"; |
| 123 | } else if (mSortOrder instanceof Document.NameComparator) { |
| 124 | return DocumentColumns.DISPLAY_NAME + " ASC"; |
| 125 | } else if (mSortOrder instanceof Document.SizeComparator) { |
| 126 | return DocumentColumns.SIZE + " DESC"; |
| 127 | } else { |
| 128 | return null; |
| 129 | } |
| 130 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 131 | } |