/
FormsProvider.java
583 lines (502 loc) 路 25.5 KB
/
FormsProvider.java
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
* Copyright (C) 2007 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 org.odk.collect.android.provider;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import org.odk.collect.android.R;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.database.ItemsetDbAdapter;
import org.odk.collect.android.database.helpers.FormsDatabaseHelper;
import org.odk.collect.android.provider.FormsProviderAPI.FormsColumns;
import org.odk.collect.android.utilities.FileUtils;
import org.odk.collect.android.utilities.MediaUtils;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import timber.log.Timber;
import static org.odk.collect.android.database.helpers.FormsDatabaseHelper.FORMS_TABLE_NAME;
import static org.odk.collect.android.utilities.PermissionUtils.areStoragePermissionsGranted;
public class FormsProvider extends ContentProvider {
private static HashMap<String, String> sFormsProjectionMap;
private static final int FORMS = 1;
private static final int FORM_ID = 2;
// Forms unique by ID, keeping only the latest one downloaded
private static final int NEWEST_FORMS_BY_FORM_ID = 3;
private static final UriMatcher URI_MATCHER;
private static FormsDatabaseHelper dbHelper;
private synchronized FormsDatabaseHelper getDbHelper() {
// wrapper to test and reset/set the dbHelper based upon the attachment state of the device.
try {
Collect.createODKDirs();
} catch (RuntimeException e) {
return null;
}
if (dbHelper == null) {
dbHelper = new FormsDatabaseHelper();
}
return dbHelper;
}
@Override
public boolean onCreate() {
if (!areStoragePermissionsGranted(getContext())) {
Timber.i("Read and write permissions are required for this content provider to function.");
return false;
}
// must be at the beginning of any activity that can be called from an external intent
FormsDatabaseHelper h = getDbHelper();
return h != null;
}
@Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
if (!areStoragePermissionsGranted(getContext())) {
return null;
}
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(FORMS_TABLE_NAME);
qb.setProjectionMap(sFormsProjectionMap);
qb.setStrict(true);
Cursor c = null;
String groupBy = null;
FormsDatabaseHelper formsDatabaseHelper = getDbHelper();
if (formsDatabaseHelper != null) {
switch (URI_MATCHER.match(uri)) {
case FORMS:
break;
case FORM_ID:
qb.appendWhere(FormsColumns._ID + "="
+ uri.getPathSegments().get(1));
break;
// Only include the latest form that was downloaded with each form_id
case NEWEST_FORMS_BY_FORM_ID:
Map<String, String> filteredProjectionMap = new HashMap<>(sFormsProjectionMap);
filteredProjectionMap.put(FormsColumns.DATE, "MAX(" + FormsColumns.DATE + ")");
qb.setProjectionMap(filteredProjectionMap);
groupBy = FormsColumns.JR_FORM_ID;
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
c = qb.query(formsDatabaseHelper.getReadableDatabase(), projection, selection, selectionArgs, groupBy, null, sortOrder);
// Tell the cursor what uri to watch, so it knows when its source data changes
c.setNotificationUri(getContext().getContentResolver(), uri);
}
return c;
}
@Override
public String getType(@NonNull Uri uri) {
switch (URI_MATCHER.match(uri)) {
case FORMS:
case NEWEST_FORMS_BY_FORM_ID:
return FormsColumns.CONTENT_TYPE;
case FORM_ID:
return FormsColumns.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public synchronized Uri insert(@NonNull Uri uri, ContentValues initialValues) {
// Validate the requested uri
if (URI_MATCHER.match(uri) != FORMS) {
throw new IllegalArgumentException("Unknown URI " + uri);
}
if (!areStoragePermissionsGranted(getContext())) {
return null;
}
FormsDatabaseHelper formsDatabaseHelper = getDbHelper();
if (formsDatabaseHelper != null) {
ContentValues values;
if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}
if (!values.containsKey(FormsColumns.FORM_FILE_PATH)) {
throw new IllegalArgumentException(FormsColumns.FORM_FILE_PATH
+ " must be specified.");
}
// Normalize the file path.
// (don't trust the requester).
String filePath = values.getAsString(FormsColumns.FORM_FILE_PATH);
File form = new File(filePath);
filePath = form.getAbsolutePath(); // normalized
values.put(FormsColumns.FORM_FILE_PATH, filePath);
Long now = System.currentTimeMillis();
// Make sure that the necessary fields are all set
if (!values.containsKey(FormsColumns.DATE)) {
values.put(FormsColumns.DATE, now);
}
if (!values.containsKey(FormsColumns.DISPLAY_SUBTEXT)) {
Date today = new Date();
String ts = new SimpleDateFormat(getContext().getString(
R.string.added_on_date_at_time), Locale.getDefault())
.format(today);
values.put(FormsColumns.DISPLAY_SUBTEXT, ts);
}
if (!values.containsKey(FormsColumns.DISPLAY_NAME)) {
values.put(FormsColumns.DISPLAY_NAME, form.getName());
}
// don't let users put in a manual md5 hash
if (values.containsKey(FormsColumns.MD5_HASH)) {
values.remove(FormsColumns.MD5_HASH);
}
String md5 = FileUtils.getMd5Hash(form);
values.put(FormsColumns.MD5_HASH, md5);
if (!values.containsKey(FormsColumns.JRCACHE_FILE_PATH)) {
String cachePath = Collect.CACHE_PATH + File.separator + md5
+ ".formdef";
values.put(FormsColumns.JRCACHE_FILE_PATH, cachePath);
}
if (!values.containsKey(FormsColumns.FORM_MEDIA_PATH)) {
values.put(FormsColumns.FORM_MEDIA_PATH, FileUtils.constructMediaPath(filePath));
}
SQLiteDatabase db = formsDatabaseHelper.getWritableDatabase();
// first try to see if a record with this filename already exists...
String[] projection = {FormsColumns._ID, FormsColumns.FORM_FILE_PATH};
String[] selectionArgs = {filePath};
String selection = FormsColumns.FORM_FILE_PATH + "=?";
Cursor c = null;
try {
c = db.query(FORMS_TABLE_NAME, projection, selection,
selectionArgs, null, null, null);
if (c.getCount() > 0) {
// already exists
throw new SQLException("FAILED Insert into " + uri
+ " -- row already exists for form definition file: "
+ filePath);
}
} finally {
if (c != null) {
c.close();
}
}
long rowId = db.insert(FORMS_TABLE_NAME, null, values);
if (rowId > 0) {
Uri formUri = ContentUris.withAppendedId(FormsColumns.CONTENT_URI,
rowId);
getContext().getContentResolver().notifyChange(formUri, null);
getContext().getContentResolver().notifyChange(FormsProviderAPI.FormsColumns.CONTENT_NEWEST_FORMS_BY_FORMID_URI, null);
return formUri;
}
}
throw new SQLException("Failed to insert row into " + uri);
}
private void deleteFileOrDir(String fileName) {
File file = new File(fileName);
if (file.exists()) {
if (file.isDirectory()) {
// delete any media entries for files in this directory...
int images = MediaUtils
.deleteImagesInFolderFromMediaProvider(file);
int audio = MediaUtils
.deleteAudioInFolderFromMediaProvider(file);
int video = MediaUtils
.deleteVideoInFolderFromMediaProvider(file);
Timber.i("removed from content providers: %d image files, %d audio files, and %d"
+ " video files.", images, audio, video);
// delete all the containing files
File[] files = file.listFiles();
if (files != null) {
for (File f : files) {
// should make this recursive if we get worried about
// the media directory containing directories
Timber.i("attempting to delete file: %s", f.getAbsolutePath());
f.delete();
}
}
}
file.delete();
Timber.i("attempting to delete file: %s", file.getAbsolutePath());
}
}
/**
* This method removes the entry from the content provider, and also removes
* any associated files. files: form.xml, [formmd5].formdef, formname-media
* {directory}
*/
@Override
public int delete(@NonNull Uri uri, String where, String[] whereArgs) {
if (!areStoragePermissionsGranted(getContext())) {
return 0;
}
int count = 0;
FormsDatabaseHelper formsDatabaseHelper = getDbHelper();
if (formsDatabaseHelper != null) {
SQLiteDatabase db = formsDatabaseHelper.getWritableDatabase();
switch (URI_MATCHER.match(uri)) {
case FORMS:
Cursor del = null;
try {
del = this.query(uri, null, where, whereArgs, null);
if (del != null && del.getCount() > 0) {
del.moveToFirst();
do {
deleteFileOrDir(del
.getString(del
.getColumnIndex(FormsColumns.JRCACHE_FILE_PATH)));
String formFilePath = del.getString(del
.getColumnIndex(FormsColumns.FORM_FILE_PATH));
deleteFileOrDir(formFilePath);
deleteFileOrDir(del.getString(del
.getColumnIndex(FormsColumns.FORM_MEDIA_PATH)));
} while (del.moveToNext());
}
} finally {
if (del != null) {
del.close();
}
}
count = db.delete(FORMS_TABLE_NAME, where, whereArgs);
break;
case FORM_ID:
String formId = uri.getPathSegments().get(1);
Cursor c = null;
try {
c = this.query(uri, null, where, whereArgs, null);
// This should only ever return 1 record.
if (c != null && c.getCount() > 0) {
c.moveToFirst();
do {
deleteFileOrDir(c.getString(c
.getColumnIndex(FormsColumns.JRCACHE_FILE_PATH)));
String formFilePath = c.getString(c
.getColumnIndex(FormsColumns.FORM_FILE_PATH));
deleteFileOrDir(formFilePath);
deleteFileOrDir(c.getString(c
.getColumnIndex(FormsColumns.FORM_MEDIA_PATH)));
try {
// get rid of the old tables
ItemsetDbAdapter ida = new ItemsetDbAdapter();
ida.open();
ida.delete(c.getString(c
.getColumnIndex(FormsColumns.FORM_MEDIA_PATH))
+ "/itemsets.csv");
ida.close();
} catch (Exception e) {
// if something else is accessing the provider this may not exist
// so catch it and move on.
}
} while (c.moveToNext());
}
} finally {
if (c != null) {
c.close();
}
}
count = db.delete(
FORMS_TABLE_NAME,
FormsColumns._ID
+ "=?"
+ (!TextUtils.isEmpty(where) ? " AND (" + where
+ ')' : ""), prepareWhereArgs(whereArgs, formId));
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(FormsColumns.CONTENT_NEWEST_FORMS_BY_FORMID_URI, null);
}
return count;
}
@Override
public int update(Uri uri, ContentValues values, String where,
String[] whereArgs) {
if (!areStoragePermissionsGranted(getContext())) {
return 0;
}
int count = 0;
FormsDatabaseHelper formsDatabaseHelper = getDbHelper();
if (formsDatabaseHelper != null) {
SQLiteDatabase db = formsDatabaseHelper.getWritableDatabase();
switch (URI_MATCHER.match(uri)) {
case FORMS:
// don't let users manually update md5
if (values.containsKey(FormsColumns.MD5_HASH)) {
values.remove(FormsColumns.MD5_HASH);
}
// if values contains path, then all filepaths and md5s will get
// updated
// this probably isn't a great thing to do.
if (values.containsKey(FormsColumns.FORM_FILE_PATH)) {
String formFile = values
.getAsString(FormsColumns.FORM_FILE_PATH);
values.put(FormsColumns.MD5_HASH,
FileUtils.getMd5Hash(new File(formFile)));
}
Cursor c = null;
try {
c = this.query(uri, null, where, whereArgs, null);
if (c != null && c.getCount() > 0) {
c.moveToPosition(-1);
while (c.moveToNext()) {
// before updating the paths, delete all the files
if (values.containsKey(FormsColumns.FORM_FILE_PATH)) {
String newFile = values
.getAsString(FormsColumns.FORM_FILE_PATH);
String delFile = c
.getString(c
.getColumnIndex(FormsColumns.FORM_FILE_PATH));
if (!newFile.equalsIgnoreCase(delFile)) {
deleteFileOrDir(delFile);
}
// either way, delete the old cache because we'll
// calculate a new one.
deleteFileOrDir(c
.getString(c
.getColumnIndex(FormsColumns.JRCACHE_FILE_PATH)));
}
}
}
} finally {
if (c != null) {
c.close();
}
}
// Make sure that the necessary fields are all set
if (values.containsKey(FormsColumns.DATE)) {
Date today = new Date();
String ts = new SimpleDateFormat(getContext().getString(
R.string.added_on_date_at_time), Locale.getDefault())
.format(today);
values.put(FormsColumns.DISPLAY_SUBTEXT, ts);
}
count = db.update(FORMS_TABLE_NAME, values, where, whereArgs);
break;
case FORM_ID:
String formId = uri.getPathSegments().get(1);
// Whenever file paths are updated, delete the old files.
Cursor update = null;
try {
update = this.query(uri, null, where, whereArgs, null);
// This should only ever return 1 record.
if (update != null && update.getCount() > 0) {
update.moveToFirst();
// don't let users manually update md5
if (values.containsKey(FormsColumns.MD5_HASH)) {
values.remove(FormsColumns.MD5_HASH);
}
// the order here is important (jrcache needs to be before
// form file)
// because we update the jrcache file if there's a new form
// file
if (values.containsKey(FormsColumns.JRCACHE_FILE_PATH)) {
deleteFileOrDir(update
.getString(update
.getColumnIndex(FormsColumns.JRCACHE_FILE_PATH)));
}
if (values.containsKey(FormsColumns.FORM_FILE_PATH)) {
String formFile = values
.getAsString(FormsColumns.FORM_FILE_PATH);
String oldFile = update.getString(update
.getColumnIndex(FormsColumns.FORM_FILE_PATH));
if (formFile == null || !formFile.equalsIgnoreCase(oldFile)) {
deleteFileOrDir(oldFile);
}
// we're updating our file, so update the md5
// and get rid of the cache (doesn't harm anything)
deleteFileOrDir(update
.getString(update
.getColumnIndex(FormsColumns.JRCACHE_FILE_PATH)));
String newMd5 = FileUtils
.getMd5Hash(new File(formFile));
values.put(FormsColumns.MD5_HASH, newMd5);
values.put(FormsColumns.JRCACHE_FILE_PATH,
Collect.CACHE_PATH + File.separator + newMd5
+ ".formdef");
}
// Make sure that the necessary fields are all set
if (values.containsKey(FormsColumns.DATE)) {
Date today = new Date();
String ts = new SimpleDateFormat(getContext()
.getString(R.string.added_on_date_at_time),
Locale.getDefault()).format(today);
values.put(FormsColumns.DISPLAY_SUBTEXT, ts);
}
count = db.update(
FORMS_TABLE_NAME,
values,
FormsColumns._ID
+ "=?"
+ (!TextUtils.isEmpty(where) ? " AND ("
+ where + ')' : ""), prepareWhereArgs(whereArgs, formId));
} else {
Timber.e("Attempting to update row that does not exist");
}
} finally {
if (update != null) {
update.close();
}
}
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
getContext().getContentResolver().notifyChange(FormsProviderAPI.FormsColumns.CONTENT_NEWEST_FORMS_BY_FORMID_URI, null);
}
return count;
}
@NonNull
private String[] prepareWhereArgs(String[] whereArgs, String formId) {
String[] newWhereArgs;
if (whereArgs == null || whereArgs.length == 0) {
newWhereArgs = new String[] {formId};
} else {
newWhereArgs = new String[whereArgs.length + 1];
newWhereArgs[0] = formId;
System.arraycopy(whereArgs, 0, newWhereArgs, 1, whereArgs.length);
}
return newWhereArgs;
}
// Leading slashes are removed from paths to support minSdkVersion < 18:
// https://developer.android.com/reference/android/content/UriMatcher
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(FormsProviderAPI.AUTHORITY, FormsColumns.CONTENT_URI.getPath().replaceAll("^/+", ""), FORMS);
URI_MATCHER.addURI(FormsProviderAPI.AUTHORITY, FormsColumns.CONTENT_URI.getPath().replaceAll("^/+", "") + "/#", FORM_ID);
// Only available for query and type
URI_MATCHER.addURI(FormsProviderAPI.AUTHORITY, FormsColumns.CONTENT_NEWEST_FORMS_BY_FORMID_URI.getPath().replaceAll("^/+", ""), NEWEST_FORMS_BY_FORM_ID);
sFormsProjectionMap = new HashMap<>();
sFormsProjectionMap.put(FormsColumns._ID, FormsColumns._ID);
sFormsProjectionMap.put(FormsColumns.DISPLAY_NAME, FormsColumns.DISPLAY_NAME);
sFormsProjectionMap.put(FormsColumns.DISPLAY_SUBTEXT, FormsColumns.DISPLAY_SUBTEXT);
sFormsProjectionMap.put(FormsColumns.DESCRIPTION, FormsColumns.DESCRIPTION);
sFormsProjectionMap.put(FormsColumns.JR_FORM_ID, FormsColumns.JR_FORM_ID);
sFormsProjectionMap.put(FormsColumns.JR_VERSION, FormsColumns.JR_VERSION);
sFormsProjectionMap.put(FormsColumns.SUBMISSION_URI, FormsColumns.SUBMISSION_URI);
sFormsProjectionMap.put(FormsColumns.BASE64_RSA_PUBLIC_KEY, FormsColumns.BASE64_RSA_PUBLIC_KEY);
sFormsProjectionMap.put(FormsColumns.MD5_HASH, FormsColumns.MD5_HASH);
sFormsProjectionMap.put(FormsColumns.DATE, FormsColumns.DATE);
sFormsProjectionMap.put(FormsColumns.FORM_MEDIA_PATH, FormsColumns.FORM_MEDIA_PATH);
sFormsProjectionMap.put(FormsColumns.FORM_FILE_PATH, FormsColumns.FORM_FILE_PATH);
sFormsProjectionMap.put(FormsColumns.JRCACHE_FILE_PATH, FormsColumns.JRCACHE_FILE_PATH);
sFormsProjectionMap.put(FormsColumns.LANGUAGE, FormsColumns.LANGUAGE);
sFormsProjectionMap.put(FormsColumns.AUTO_DELETE, FormsColumns.AUTO_DELETE);
sFormsProjectionMap.put(FormsColumns.AUTO_SEND, FormsColumns.AUTO_SEND);
sFormsProjectionMap.put(FormsColumns.LAST_DETECTED_FORM_VERSION_HASH, FormsColumns.LAST_DETECTED_FORM_VERSION_HASH);
}
}