blob: 14d6fd5e63331f5e1e5b7ebcd07c6a0b739ee5ee [file] [log] [blame]
Jeff Sharkeya5defe32013-08-05 17:56:48 -07001/*
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
17package com.android.documentsui;
18
19import static com.android.documentsui.DirectoryFragment.TYPE_NORMAL;
20import static com.android.documentsui.DirectoryFragment.TYPE_RECENT_OPEN;
21import static com.android.documentsui.DirectoryFragment.TYPE_SEARCH;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070022import static com.android.documentsui.DocumentsActivity.TAG;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070023
24import android.content.ContentResolver;
25import android.content.Context;
26import android.database.Cursor;
27import android.net.Uri;
28import android.os.CancellationSignal;
Jeff Sharkey1d890e02013-08-15 11:24:03 -070029import android.provider.DocumentsContract.DocumentColumns;
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070030import android.util.Log;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070031
32import com.android.documentsui.model.Document;
33import com.android.internal.util.Predicate;
34import com.google.android.collect.Lists;
35
36import libcore.io.IoUtils;
37
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070038import java.io.FileNotFoundException;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070039import java.util.Collections;
40import java.util.Comparator;
41import java.util.List;
42
Jeff Sharkey46899c82013-08-18 22:26:48 -070043class 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
54public class DirectoryLoader extends UriDerivativeLoader<Uri, DirectoryResult> {
Jeff Sharkeya5defe32013-08-05 17:56:48 -070055
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 Sharkey46899c82013-08-18 22:26:48 -070069 public DirectoryResult loadInBackground(Uri uri, CancellationSignal signal) {
70 final DirectoryResult result = new DirectoryResult();
Jeff Sharkey1d890e02013-08-15 11:24:03 -070071 try {
Jeff Sharkey46899c82013-08-18 22:26:48 -070072 loadInBackgroundInternal(result, uri, signal);
Jeff Sharkey1d890e02013-08-15 11:24:03 -070073 } catch (Exception e) {
Jeff Sharkey46899c82013-08-18 22:26:48 -070074 result.e = e;
Jeff Sharkey1d890e02013-08-15 11:24:03 -070075 }
Jeff Sharkey46899c82013-08-18 22:26:48 -070076 return result;
Jeff Sharkey1d890e02013-08-15 11:24:03 -070077 }
78
Jeff Sharkey46899c82013-08-18 22:26:48 -070079 private void loadInBackgroundInternal(
80 DirectoryResult result, Uri uri, CancellationSignal signal) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -070081 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkey1d890e02013-08-15 11:24:03 -070082 final Cursor cursor = resolver.query(uri, null, null, null, getQuerySortOrder(), signal);
Jeff Sharkey46899c82013-08-18 22:26:48 -070083 result.cursor = cursor;
84 result.cursor.registerContentObserver(mObserver);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070085
Jeff Sharkey46899c82013-08-18 22:26:48 -070086 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 Sharkeya5defe32013-08-05 17:56:48 -0700102 }
Jeff Sharkey46899c82013-08-18 22:26:48 -0700103
104 if (doc != null && (mFilter == null || mFilter.apply(doc))) {
105 result.contents.add(doc);
106 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700107 }
108
109 if (mSortOrder != null) {
Jeff Sharkey46899c82013-08-18 22:26:48 -0700110 Collections.sort(result.contents, mSortOrder);
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700111 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700112 }
Jeff Sharkey1d890e02013-08-15 11:24:03 -0700113
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 Sharkeya5defe32013-08-05 17:56:48 -0700125}