1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.documentsui.model;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.DocumentColumns;
import android.provider.DocumentsContract.Documents;
import com.android.documentsui.RecentsProvider;
import libcore.io.IoUtils;
import java.io.FileNotFoundException;
import java.util.Comparator;
/**
* Representation of a single document.
*/
public class Document {
public final Uri uri;
public final String mimeType;
public final String displayName;
public final long lastModified;
public final int flags;
public final String summary;
public final long size;
private Document(Uri uri, String mimeType, String displayName, long lastModified, int flags,
String summary, long size) {
this.uri = uri;
this.mimeType = mimeType;
this.displayName = displayName;
this.lastModified = lastModified;
this.flags = flags;
this.summary = summary;
this.size = size;
}
public static Document fromRoot(ContentResolver resolver, Root root)
throws FileNotFoundException {
return fromUri(resolver, root.uri);
}
public static Document fromDirectoryCursor(Uri parent, Cursor cursor) {
final String authority = parent.getAuthority();
final String rootId = DocumentsContract.getRootId(parent);
final String docId = getCursorString(cursor, DocumentColumns.DOC_ID);
final Uri uri = DocumentsContract.buildDocumentUri(authority, rootId, docId);
final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE);
final String displayName = getCursorString(cursor, DocumentColumns.DISPLAY_NAME);
final long lastModified = getCursorLong(cursor, DocumentColumns.LAST_MODIFIED);
final int flags = getCursorInt(cursor, DocumentColumns.FLAGS);
final String summary = getCursorString(cursor, DocumentColumns.SUMMARY);
final long size = getCursorLong(cursor, DocumentColumns.SIZE);
return new Document(uri, mimeType, displayName, lastModified, flags, summary, size);
}
public static Document fromRecentOpenCursor(ContentResolver resolver, Cursor recentCursor)
throws FileNotFoundException {
final Uri uri = Uri.parse(getCursorString(recentCursor, RecentsProvider.COL_URI));
final long lastModified = getCursorLong(recentCursor, RecentsProvider.COL_TIMESTAMP);
Cursor cursor = null;
try {
cursor = resolver.query(uri, null, null, null, null);
if (!cursor.moveToFirst()) {
throw new FileNotFoundException("Missing details for " + uri);
}
final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE);
final String displayName = getCursorString(cursor, DocumentColumns.DISPLAY_NAME);
final int flags = getCursorInt(cursor, DocumentColumns.FLAGS)
& Documents.FLAG_SUPPORTS_THUMBNAIL;
final String summary = getCursorString(cursor, DocumentColumns.SUMMARY);
final long size = getCursorLong(cursor, DocumentColumns.SIZE);
return new Document(uri, mimeType, displayName, lastModified, flags, summary, size);
} catch (Throwable t) {
throw asFileNotFoundException(t);
} finally {
IoUtils.closeQuietly(cursor);
}
}
public static Document fromUri(ContentResolver resolver, Uri uri) throws FileNotFoundException {
Cursor cursor = null;
try {
cursor = resolver.query(uri, null, null, null, null);
if (!cursor.moveToFirst()) {
throw new FileNotFoundException("Missing details for " + uri);
}
final String mimeType = getCursorString(cursor, DocumentColumns.MIME_TYPE);
final String displayName = getCursorString(cursor, DocumentColumns.DISPLAY_NAME);
final long lastModified = getCursorLong(cursor, DocumentColumns.LAST_MODIFIED);
final int flags = getCursorInt(cursor, DocumentColumns.FLAGS);
final String summary = getCursorString(cursor, DocumentColumns.SUMMARY);
final long size = getCursorLong(cursor, DocumentColumns.SIZE);
return new Document(uri, mimeType, displayName, lastModified, flags, summary, size);
} catch (Throwable t) {
throw asFileNotFoundException(t);
} finally {
IoUtils.closeQuietly(cursor);
}
}
@Override
public String toString() {
return "Document{name=" + displayName + ", uri=" + uri + "}";
}
public boolean isCreateSupported() {
return (flags & Documents.FLAG_SUPPORTS_CREATE) != 0;
}
public boolean isSearchSupported() {
return (flags & Documents.FLAG_SUPPORTS_SEARCH) != 0;
}
public boolean isThumbnailSupported() {
return (flags & Documents.FLAG_SUPPORTS_THUMBNAIL) != 0;
}
public boolean isDirectory() {
return Documents.MIME_TYPE_DIR.equals(mimeType);
}
private static String getCursorString(Cursor cursor, String columnName) {
final int index = cursor.getColumnIndex(columnName);
return (index != -1) ? cursor.getString(index) : null;
}
/**
* Missing or null values are returned as -1.
*/
private static long getCursorLong(Cursor cursor, String columnName) {
final int index = cursor.getColumnIndex(columnName);
if (index == -1) return -1;
final String value = cursor.getString(index);
if (value == null) return -1;
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return -1;
}
}
private static int getCursorInt(Cursor cursor, String columnName) {
final int index = cursor.getColumnIndex(columnName);
return (index != -1) ? cursor.getInt(index) : 0;
}
public static class NameComparator implements Comparator<Document> {
@Override
public int compare(Document lhs, Document rhs) {
final boolean leftDir = lhs.isDirectory();
final boolean rightDir = rhs.isDirectory();
if (leftDir != rightDir) {
return leftDir ? -1 : 1;
} else {
return Root.compareToIgnoreCaseNullable(lhs.displayName, rhs.displayName);
}
}
}
public static class DateComparator implements Comparator<Document> {
@Override
public int compare(Document lhs, Document rhs) {
return Long.compare(rhs.lastModified, lhs.lastModified);
}
}
public static class SizeComparator implements Comparator<Document> {
@Override
public int compare(Document lhs, Document rhs) {
return Long.compare(rhs.size, lhs.size);
}
}
public static FileNotFoundException asFileNotFoundException(Throwable t)
throws FileNotFoundException {
if (t instanceof FileNotFoundException) {
throw (FileNotFoundException) t;
}
final FileNotFoundException fnfe = new FileNotFoundException(t.getMessage());
fnfe.initCause(t);
throw fnfe;
}
}
|