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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* 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.ContentProviderClient;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.DocumentsContract;
import android.provider.DocumentsProvider;
import android.provider.DocumentsContract.Document;
import com.android.documentsui.RootCursorWrapper;
import libcore.io.IoUtils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ProtocolException;
/**
* Representation of a {@link Document}.
*/
public class DocumentInfo implements Durable, Parcelable {
private static final int VERSION_INIT = 1;
private static final int VERSION_SPLIT_URI = 2;
public String authority;
public String documentId;
public String mimeType;
public String displayName;
public long lastModified;
public int flags;
public String summary;
public long size;
public int icon;
/** Derived fields that aren't persisted */
public Uri derivedUri;
public DocumentInfo() {
reset();
}
@Override
public void reset() {
authority = null;
documentId = null;
mimeType = null;
displayName = null;
lastModified = -1;
flags = 0;
summary = null;
size = -1;
icon = 0;
derivedUri = null;
}
@Override
public void read(DataInputStream in) throws IOException {
final int version = in.readInt();
switch (version) {
case VERSION_INIT:
throw new ProtocolException("Ignored upgrade");
case VERSION_SPLIT_URI:
authority = DurableUtils.readNullableString(in);
documentId = DurableUtils.readNullableString(in);
mimeType = DurableUtils.readNullableString(in);
displayName = DurableUtils.readNullableString(in);
lastModified = in.readLong();
flags = in.readInt();
summary = DurableUtils.readNullableString(in);
size = in.readLong();
icon = in.readInt();
deriveFields();
break;
default:
throw new ProtocolException("Unknown version " + version);
}
}
@Override
public void write(DataOutputStream out) throws IOException {
out.writeInt(VERSION_SPLIT_URI);
DurableUtils.writeNullableString(out, authority);
DurableUtils.writeNullableString(out, documentId);
DurableUtils.writeNullableString(out, mimeType);
DurableUtils.writeNullableString(out, displayName);
out.writeLong(lastModified);
out.writeInt(flags);
DurableUtils.writeNullableString(out, summary);
out.writeLong(size);
out.writeInt(icon);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
DurableUtils.writeToParcel(dest, this);
}
public static final Creator<DocumentInfo> CREATOR = new Creator<DocumentInfo>() {
@Override
public DocumentInfo createFromParcel(Parcel in) {
final DocumentInfo doc = new DocumentInfo();
DurableUtils.readFromParcel(in, doc);
return doc;
}
@Override
public DocumentInfo[] newArray(int size) {
return new DocumentInfo[size];
}
};
public static DocumentInfo fromDirectoryCursor(Cursor cursor) {
final String authority = getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY);
return fromCursor(cursor, authority);
}
public static DocumentInfo fromCursor(Cursor cursor, String authority) {
final DocumentInfo info = new DocumentInfo();
info.updateFromCursor(cursor, authority);
return info;
}
public void updateFromCursor(Cursor cursor, String authority) {
this.authority = authority;
this.documentId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
this.mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
this.documentId = getCursorString(cursor, Document.COLUMN_DOCUMENT_ID);
this.mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
this.displayName = getCursorString(cursor, Document.COLUMN_DISPLAY_NAME);
this.lastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
this.flags = getCursorInt(cursor, Document.COLUMN_FLAGS);
this.summary = getCursorString(cursor, Document.COLUMN_SUMMARY);
this.size = getCursorLong(cursor, Document.COLUMN_SIZE);
this.icon = getCursorInt(cursor, Document.COLUMN_ICON);
this.deriveFields();
}
public static DocumentInfo fromUri(ContentResolver resolver, Uri uri)
throws FileNotFoundException {
final DocumentInfo info = new DocumentInfo();
info.updateFromUri(resolver, uri);
return info;
}
/**
* Update a possibly stale restored document against a live
* {@link DocumentsProvider}.
*/
public void updateSelf(ContentResolver resolver) throws FileNotFoundException {
updateFromUri(resolver, derivedUri);
}
public void updateFromUri(ContentResolver resolver, Uri uri) throws FileNotFoundException {
final ContentProviderClient client = resolver.acquireUnstableContentProviderClient(
uri.getAuthority());
Cursor cursor = null;
try {
cursor = client.query(uri, null, null, null, null);
if (!cursor.moveToFirst()) {
throw new FileNotFoundException("Missing details for " + uri);
}
updateFromCursor(cursor, uri.getAuthority());
} catch (Throwable t) {
throw asFileNotFoundException(t);
} finally {
IoUtils.closeQuietly(cursor);
ContentProviderClient.closeQuietly(client);
}
}
private void deriveFields() {
derivedUri = DocumentsContract.buildDocumentUri(authority, documentId);
}
@Override
public String toString() {
return "Document{docId=" + documentId + ", name=" + displayName + "}";
}
public boolean isCreateSupported() {
return (flags & Document.FLAG_DIR_SUPPORTS_CREATE) != 0;
}
public boolean isThumbnailSupported() {
return (flags & Document.FLAG_SUPPORTS_THUMBNAIL) != 0;
}
public boolean isDirectory() {
return Document.MIME_TYPE_DIR.equals(mimeType);
}
public boolean isGridPreferred() {
return (flags & Document.FLAG_DIR_PREFERS_GRID) != 0;
}
public boolean isDeleteSupported() {
return (flags & Document.FLAG_SUPPORTS_DELETE) != 0;
}
public boolean isGridTitlesHidden() {
return (flags & Document.FLAG_DIR_HIDE_GRID_TITLES) != 0;
}
public 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.
*/
public 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;
}
}
/**
* Missing or null values are returned as 0.
*/
public static int getCursorInt(Cursor cursor, String columnName) {
final int index = cursor.getColumnIndex(columnName);
return (index != -1) ? cursor.getInt(index) : 0;
}
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;
}
public static int compareToIgnoreCaseNullable(String lhs, String rhs) {
if (lhs == null) return -1;
if (rhs == null) return 1;
return lhs.compareToIgnoreCase(rhs);
}
}
|