blob: 98f9a4dbcd2d89e7230ef0ef43b2ff05a3972ccf [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.ArrayList;
40import java.util.Collections;
41import java.util.Comparator;
Jeff Sharkey1d890e02013-08-15 11:24:03 -070042import java.util.LinkedList;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070043import java.util.List;
44
45public class DirectoryLoader extends UriDerivativeLoader<List<Document>> {
46
47 private final int mType;
48 private Predicate<Document> mFilter;
49 private Comparator<Document> mSortOrder;
50
Jeff Sharkey1d890e02013-08-15 11:24:03 -070051 /**
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 Sharkeya5defe32013-08-05 17:56:48 -070062 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 Sharkey1d890e02013-08-15 11:24:03 -070072 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 Sharkeya5defe32013-08-05 17:56:48 -070080 final ArrayList<Document> result = Lists.newArrayList();
81
Jeff Sharkeya5defe32013-08-05 17:56:48 -070082 final ContentResolver resolver = getContext().getContentResolver();
Jeff Sharkey1d890e02013-08-15 11:24:03 -070083 final Cursor cursor = resolver.query(uri, null, null, null, getQuerySortOrder(), signal);
Jeff Sharkeya5defe32013-08-05 17:56:48 -070084 try {
85 while (cursor != null && cursor.moveToNext()) {
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070086 Document doc = null;
Jeff Sharkeya5defe32013-08-05 17:56:48 -070087 switch (mType) {
88 case TYPE_NORMAL:
89 case TYPE_SEARCH:
90 doc = Document.fromDirectoryCursor(uri, cursor);
91 break;
92 case TYPE_RECENT_OPEN:
Jeff Sharkey744dbbd2013-08-07 18:33:33 -070093 try {
94 doc = Document.fromRecentOpenCursor(resolver, cursor);
95 } catch (FileNotFoundException e) {
96 Log.w(TAG, "Failed to find recent: " + e);
97 }
Jeff Sharkeya5defe32013-08-05 17:56:48 -070098 break;
99 default:
100 throw new IllegalArgumentException("Unknown type");
101 }
102
Jeff Sharkey744dbbd2013-08-07 18:33:33 -0700103 if (doc != null && (mFilter == null || mFilter.apply(doc))) {
Jeff Sharkeya5defe32013-08-05 17:56:48 -0700104 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 Sharkey1d890e02013-08-15 11:24:03 -0700117
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 Sharkeya5defe32013-08-05 17:56:48 -0700129}