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.Collections; |
| 40 | import java.util.Comparator; |
| 41 | import java.util.List; |
| 42 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 43 | class DirectoryResult implements AutoCloseable { |
| 44 | Cursor cursor; |
| 45 | List<Document> contents = Lists.newArrayList(); |
| 46 | Exception e; |
| 47 | |
| 48 | @Override |
| 49 | public void close() throws Exception { |
| 50 | IoUtils.closeQuietly(cursor); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public class DirectoryLoader extends UriDerivativeLoader<Uri, DirectoryResult> { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 55 | |
| 56 | private final int mType; |
| 57 | private Predicate<Document> mFilter; |
| 58 | private Comparator<Document> mSortOrder; |
| 59 | |
| 60 | public DirectoryLoader(Context context, Uri uri, int type, Predicate<Document> filter, |
| 61 | Comparator<Document> sortOrder) { |
| 62 | super(context, uri); |
| 63 | mType = type; |
| 64 | mFilter = filter; |
| 65 | mSortOrder = sortOrder; |
| 66 | } |
| 67 | |
| 68 | @Override |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 69 | public DirectoryResult loadInBackground(Uri uri, CancellationSignal signal) { |
| 70 | final DirectoryResult result = new DirectoryResult(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 71 | try { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 72 | loadInBackgroundInternal(result, uri, signal); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 73 | } catch (Exception e) { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 74 | result.e = e; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 75 | } |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 76 | return result; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 79 | private void loadInBackgroundInternal( |
| 80 | DirectoryResult result, Uri uri, CancellationSignal signal) { |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 81 | final ContentResolver resolver = getContext().getContentResolver(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 82 | final Cursor cursor = resolver.query(uri, null, null, null, getQuerySortOrder(), signal); |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 83 | result.cursor = cursor; |
| 84 | result.cursor.registerContentObserver(mObserver); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 85 | |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 86 | while (cursor.moveToNext()) { |
| 87 | Document doc = null; |
| 88 | switch (mType) { |
| 89 | case TYPE_NORMAL: |
| 90 | case TYPE_SEARCH: |
| 91 | doc = Document.fromDirectoryCursor(uri, cursor); |
| 92 | break; |
| 93 | case TYPE_RECENT_OPEN: |
| 94 | try { |
| 95 | doc = Document.fromRecentOpenCursor(resolver, cursor); |
| 96 | } catch (FileNotFoundException e) { |
| 97 | Log.w(TAG, "Failed to find recent: " + e); |
| 98 | } |
| 99 | break; |
| 100 | default: |
| 101 | throw new IllegalArgumentException("Unknown type"); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 102 | } |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 103 | |
| 104 | if (doc != null && (mFilter == null || mFilter.apply(doc))) { |
| 105 | result.contents.add(doc); |
| 106 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (mSortOrder != null) { |
Jeff Sharkey | 46899c8 | 2013-08-18 22:26:48 -0700 | [diff] [blame^] | 110 | Collections.sort(result.contents, mSortOrder); |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 111 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 112 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 113 | |
| 114 | private String getQuerySortOrder() { |
| 115 | if (mSortOrder instanceof Document.DateComparator) { |
| 116 | return DocumentColumns.LAST_MODIFIED + " DESC"; |
| 117 | } else if (mSortOrder instanceof Document.NameComparator) { |
| 118 | return DocumentColumns.DISPLAY_NAME + " ASC"; |
| 119 | } else if (mSortOrder instanceof Document.SizeComparator) { |
| 120 | return DocumentColumns.SIZE + " DESC"; |
| 121 | } else { |
| 122 | return null; |
| 123 | } |
| 124 | } |
Jeff Sharkey | a5defe3 | 2013-08-05 17:56:48 -0700 | [diff] [blame] | 125 | } |