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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* 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;
import static com.android.documentsui.model.DocumentInfo.getCursorString;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.UriMatcher;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root;
import android.text.format.DateUtils;
import android.util.Log;
import com.android.documentsui.model.DocumentStack;
import com.android.documentsui.model.DurableUtils;
import com.android.internal.util.Predicate;
import com.google.android.collect.Sets;
import libcore.io.IoUtils;
import java.io.IOException;
import java.util.Set;
public class RecentsProvider extends ContentProvider {
private static final String TAG = "RecentsProvider";
private static final long MAX_HISTORY_IN_MILLIS = 45 * DateUtils.DAY_IN_MILLIS;
private static final String AUTHORITY = "com.android.documentsui.recents";
private static final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int URI_RECENT = 1;
private static final int URI_STATE = 2;
private static final int URI_RESUME = 3;
public static final String METHOD_PURGE = "purge";
public static final String METHOD_PURGE_PACKAGE = "purgePackage";
static {
sMatcher.addURI(AUTHORITY, "recent", URI_RECENT);
// state/authority/rootId/docId
sMatcher.addURI(AUTHORITY, "state/*/*/*", URI_STATE);
// resume/packageName
sMatcher.addURI(AUTHORITY, "resume/*", URI_RESUME);
}
public static final String TABLE_RECENT = "recent";
public static final String TABLE_STATE = "state";
public static final String TABLE_RESUME = "resume";
public static class RecentColumns {
public static final String KEY = "key";
public static final String STACK = "stack";
public static final String TIMESTAMP = "timestamp";
}
public static class StateColumns {
public static final String AUTHORITY = "authority";
public static final String ROOT_ID = Root.COLUMN_ROOT_ID;
public static final String DOCUMENT_ID = Document.COLUMN_DOCUMENT_ID;
public static final String MODE = "mode";
public static final String SORT_ORDER = "sortOrder";
}
public static class ResumeColumns {
public static final String PACKAGE_NAME = "package_name";
public static final String STACK = "stack";
public static final String TIMESTAMP = "timestamp";
public static final String EXTERNAL = "external";
}
public static Uri buildRecent() {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
.authority(AUTHORITY).appendPath("recent").build();
}
public static Uri buildState(String authority, String rootId, String documentId) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(AUTHORITY)
.appendPath("state").appendPath(authority).appendPath(rootId).appendPath(documentId)
.build();
}
public static Uri buildResume(String packageName) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT)
.authority(AUTHORITY).appendPath("resume").appendPath(packageName).build();
}
private DatabaseHelper mHelper;
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "recents.db";
private static final int VERSION_INIT = 1;
private static final int VERSION_AS_BLOB = 3;
private static final int VERSION_ADD_EXTERNAL = 4;
private static final int VERSION_ADD_RECENT_KEY = 5;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION_ADD_RECENT_KEY);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_RECENT + " (" +
RecentColumns.KEY + " TEXT PRIMARY KEY ON CONFLICT REPLACE," +
RecentColumns.STACK + " BLOB DEFAULT NULL," +
RecentColumns.TIMESTAMP + " INTEGER" +
")");
db.execSQL("CREATE TABLE " + TABLE_STATE + " (" +
StateColumns.AUTHORITY + " TEXT," +
StateColumns.ROOT_ID + " TEXT," +
StateColumns.DOCUMENT_ID + " TEXT," +
StateColumns.MODE + " INTEGER," +
StateColumns.SORT_ORDER + " INTEGER," +
"PRIMARY KEY (" + StateColumns.AUTHORITY + ", " + StateColumns.ROOT_ID + ", "
+ StateColumns.DOCUMENT_ID + ")" +
")");
db.execSQL("CREATE TABLE " + TABLE_RESUME + " (" +
ResumeColumns.PACKAGE_NAME + " TEXT NOT NULL PRIMARY KEY," +
ResumeColumns.STACK + " BLOB DEFAULT NULL," +
ResumeColumns.TIMESTAMP + " INTEGER," +
ResumeColumns.EXTERNAL + " INTEGER NOT NULL DEFAULT 0" +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database; wiping app data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECENT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RESUME);
onCreate(db);
}
}
@Override
public boolean onCreate() {
mHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
final SQLiteDatabase db = mHelper.getReadableDatabase();
switch (sMatcher.match(uri)) {
case URI_RECENT:
final long cutoff = System.currentTimeMillis() - MAX_HISTORY_IN_MILLIS;
return db.query(TABLE_RECENT, projection, RecentColumns.TIMESTAMP + ">" + cutoff,
null, null, null, sortOrder);
case URI_STATE:
final String authority = uri.getPathSegments().get(1);
final String rootId = uri.getPathSegments().get(2);
final String documentId = uri.getPathSegments().get(3);
return db.query(TABLE_STATE, projection, StateColumns.AUTHORITY + "=? AND "
+ StateColumns.ROOT_ID + "=? AND " + StateColumns.DOCUMENT_ID + "=?",
new String[] { authority, rootId, documentId }, null, null, sortOrder);
case URI_RESUME:
final String packageName = uri.getPathSegments().get(1);
return db.query(TABLE_RESUME, projection, ResumeColumns.PACKAGE_NAME + "=?",
new String[] { packageName }, null, null, sortOrder);
default:
throw new UnsupportedOperationException("Unsupported Uri " + uri);
}
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
final SQLiteDatabase db = mHelper.getWritableDatabase();
final ContentValues key = new ContentValues();
switch (sMatcher.match(uri)) {
case URI_RECENT:
values.put(RecentColumns.TIMESTAMP, System.currentTimeMillis());
db.insert(TABLE_RECENT, null, values);
final long cutoff = System.currentTimeMillis() - MAX_HISTORY_IN_MILLIS;
db.delete(TABLE_RECENT, RecentColumns.TIMESTAMP + "<" + cutoff, null);
return uri;
case URI_STATE:
final String authority = uri.getPathSegments().get(1);
final String rootId = uri.getPathSegments().get(2);
final String documentId = uri.getPathSegments().get(3);
key.put(StateColumns.AUTHORITY, authority);
key.put(StateColumns.ROOT_ID, rootId);
key.put(StateColumns.DOCUMENT_ID, documentId);
// Ensure that row exists, then update with changed values
db.insertWithOnConflict(TABLE_STATE, null, key, SQLiteDatabase.CONFLICT_IGNORE);
db.update(TABLE_STATE, values, StateColumns.AUTHORITY + "=? AND "
+ StateColumns.ROOT_ID + "=? AND " + StateColumns.DOCUMENT_ID + "=?",
new String[] { authority, rootId, documentId });
return uri;
case URI_RESUME:
values.put(ResumeColumns.TIMESTAMP, System.currentTimeMillis());
final String packageName = uri.getPathSegments().get(1);
key.put(ResumeColumns.PACKAGE_NAME, packageName);
// Ensure that row exists, then update with changed values
db.insertWithOnConflict(TABLE_RESUME, null, key, SQLiteDatabase.CONFLICT_IGNORE);
db.update(TABLE_RESUME, values, ResumeColumns.PACKAGE_NAME + "=?",
new String[] { packageName });
return uri;
default:
throw new UnsupportedOperationException("Unsupported Uri " + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Unsupported Uri " + uri);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
throw new UnsupportedOperationException("Unsupported Uri " + uri);
}
@Override
public Bundle call(String method, String arg, Bundle extras) {
if (METHOD_PURGE.equals(method)) {
// Purge references to unknown authorities
final Intent intent = new Intent(DocumentsContract.PROVIDER_INTERFACE);
final Set<String> knownAuth = Sets.newHashSet();
for (ResolveInfo info : getContext()
.getPackageManager().queryIntentContentProviders(intent, 0)) {
knownAuth.add(info.providerInfo.authority);
}
purgeByAuthority(new Predicate<String>() {
@Override
public boolean apply(String authority) {
// Purge unknown authorities
return !knownAuth.contains(authority);
}
});
return null;
} else if (METHOD_PURGE_PACKAGE.equals(method)) {
// Purge references to authorities in given package
final Intent intent = new Intent(DocumentsContract.PROVIDER_INTERFACE);
intent.setPackage(arg);
final Set<String> packageAuth = Sets.newHashSet();
for (ResolveInfo info : getContext()
.getPackageManager().queryIntentContentProviders(intent, 0)) {
packageAuth.add(info.providerInfo.authority);
}
if (!packageAuth.isEmpty()) {
purgeByAuthority(new Predicate<String>() {
@Override
public boolean apply(String authority) {
// Purge authority matches
return packageAuth.contains(authority);
}
});
}
return null;
} else {
return super.call(method, arg, extras);
}
}
/**
* Purge all internal data whose authority matches the given
* {@link Predicate}.
*/
private void purgeByAuthority(Predicate<String> predicate) {
final SQLiteDatabase db = mHelper.getWritableDatabase();
final DocumentStack stack = new DocumentStack();
Cursor cursor = db.query(TABLE_RECENT, null, null, null, null, null, null);
try {
while (cursor.moveToNext()) {
try {
final byte[] rawStack = cursor.getBlob(
cursor.getColumnIndex(RecentColumns.STACK));
DurableUtils.readFromArray(rawStack, stack);
if (stack.root != null && predicate.apply(stack.root.authority)) {
final String key = getCursorString(cursor, RecentColumns.KEY);
db.delete(TABLE_RECENT, RecentColumns.KEY + "=?", new String[] { key });
}
} catch (IOException ignored) {
}
}
} finally {
IoUtils.closeQuietly(cursor);
}
cursor = db.query(TABLE_STATE, new String[] {
StateColumns.AUTHORITY }, null, null, StateColumns.AUTHORITY, null, null);
try {
while (cursor.moveToNext()) {
final String authority = getCursorString(cursor, StateColumns.AUTHORITY);
if (predicate.apply(authority)) {
db.delete(TABLE_STATE, StateColumns.AUTHORITY + "=?", new String[] {
authority });
Log.d(TAG, "Purged state for " + authority);
}
}
} finally {
IoUtils.closeQuietly(cursor);
}
cursor = db.query(TABLE_RESUME, null, null, null, null, null, null);
try {
while (cursor.moveToNext()) {
try {
final byte[] rawStack = cursor.getBlob(
cursor.getColumnIndex(ResumeColumns.STACK));
DurableUtils.readFromArray(rawStack, stack);
if (stack.root != null && predicate.apply(stack.root.authority)) {
final String packageName = getCursorString(
cursor, ResumeColumns.PACKAGE_NAME);
db.delete(TABLE_RESUME, ResumeColumns.PACKAGE_NAME + "=?",
new String[] { packageName });
}
} catch (IOException ignored) {
}
}
} finally {
IoUtils.closeQuietly(cursor);
}
}
}
|