7463d20f372ee97a97284439e61a0d0d00653dcd
[pub/Android/ownCloud.git] / src / com / owncloud / android / providers / FileContentProvider.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.providers;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23
24 import com.owncloud.android.R;
25 import com.owncloud.android.datamodel.FileDataStorageManager;
26 import com.owncloud.android.db.ProviderMeta;
27 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
28 import com.owncloud.android.utils.Log_OC;
29
30
31
32 import android.content.ContentProvider;
33 import android.content.ContentProviderOperation;
34 import android.content.ContentProviderResult;
35 import android.content.ContentUris;
36 import android.content.ContentValues;
37 import android.content.Context;
38 import android.content.OperationApplicationException;
39 import android.content.UriMatcher;
40 import android.database.Cursor;
41 import android.database.SQLException;
42 import android.database.sqlite.SQLiteDatabase;
43 import android.database.sqlite.SQLiteOpenHelper;
44 import android.database.sqlite.SQLiteQueryBuilder;
45 import android.net.Uri;
46 import android.text.TextUtils;
47
48 /**
49 * The ContentProvider for the ownCloud App.
50 *
51 * @author Bartek Przybylski
52 * @author David A. Velasco
53 *
54 */
55 public class FileContentProvider extends ContentProvider {
56
57 private DataBaseHelper mDbHelper;
58
59 // Projection for filelist table
60 private static HashMap<String, String> mFileProjectionMap;
61 static {
62 mFileProjectionMap = new HashMap<String, String>();
63 mFileProjectionMap.put(ProviderTableMeta._ID, ProviderTableMeta._ID);
64 mFileProjectionMap.put(ProviderTableMeta.FILE_PARENT,
65 ProviderTableMeta.FILE_PARENT);
66 mFileProjectionMap.put(ProviderTableMeta.FILE_PATH,
67 ProviderTableMeta.FILE_PATH);
68 mFileProjectionMap.put(ProviderTableMeta.FILE_NAME,
69 ProviderTableMeta.FILE_NAME);
70 mFileProjectionMap.put(ProviderTableMeta.FILE_CREATION,
71 ProviderTableMeta.FILE_CREATION);
72 mFileProjectionMap.put(ProviderTableMeta.FILE_MODIFIED,
73 ProviderTableMeta.FILE_MODIFIED);
74 mFileProjectionMap.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA,
75 ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA);
76 mFileProjectionMap.put(ProviderTableMeta.FILE_CONTENT_LENGTH,
77 ProviderTableMeta.FILE_CONTENT_LENGTH);
78 mFileProjectionMap.put(ProviderTableMeta.FILE_CONTENT_TYPE,
79 ProviderTableMeta.FILE_CONTENT_TYPE);
80 mFileProjectionMap.put(ProviderTableMeta.FILE_STORAGE_PATH,
81 ProviderTableMeta.FILE_STORAGE_PATH);
82 mFileProjectionMap.put(ProviderTableMeta.FILE_LAST_SYNC_DATE,
83 ProviderTableMeta.FILE_LAST_SYNC_DATE);
84 mFileProjectionMap.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA,
85 ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA);
86 mFileProjectionMap.put(ProviderTableMeta.FILE_KEEP_IN_SYNC,
87 ProviderTableMeta.FILE_KEEP_IN_SYNC);
88 mFileProjectionMap.put(ProviderTableMeta.FILE_ACCOUNT_OWNER,
89 ProviderTableMeta.FILE_ACCOUNT_OWNER);
90 mFileProjectionMap.put(ProviderTableMeta.FILE_ETAG,
91 ProviderTableMeta.FILE_ETAG);
92 mFileProjectionMap.put(ProviderTableMeta.FILE_SHARE_BY_LINK,
93 ProviderTableMeta.FILE_SHARE_BY_LINK);
94 mFileProjectionMap.put(ProviderTableMeta.FILE_PUBLIC_LINK,
95 ProviderTableMeta.FILE_PUBLIC_LINK);
96 }
97
98 private static final int SINGLE_FILE = 1;
99 private static final int DIRECTORY = 2;
100 private static final int ROOT_DIRECTORY = 3;
101 private static final int SHARES = 4;
102
103 // Projection for ocshares table
104 private static HashMap<String, String> mOCSharesProjectionMap;
105 static {
106 mOCSharesProjectionMap = new HashMap<String, String>();
107 mOCSharesProjectionMap.put(ProviderTableMeta._ID, ProviderTableMeta._ID);
108 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_FILE_SOURCE,
109 ProviderTableMeta.OCSHARES_FILE_SOURCE);
110 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE,
111 ProviderTableMeta.OCSHARES_ITEM_SOURCE);
112 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_SHARE_TYPE,
113 ProviderTableMeta.OCSHARES_SHARE_TYPE);
114 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_SHARE_WITH,
115 ProviderTableMeta.OCSHARES_SHARE_WITH);
116 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_PATH,
117 ProviderTableMeta.OCSHARES_PATH);
118 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_PERMISSIONS,
119 ProviderTableMeta.OCSHARES_PERMISSIONS);
120 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_SHARED_DATE,
121 ProviderTableMeta.OCSHARES_SHARED_DATE);
122 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE,
123 ProviderTableMeta.OCSHARES_EXPIRATION_DATE);
124 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_TOKEN,
125 ProviderTableMeta.OCSHARES_TOKEN);
126 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME,
127 ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME);
128 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY,
129 ProviderTableMeta.OCSHARES_IS_DIRECTORY);
130 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_USER_ID,
131 ProviderTableMeta.OCSHARES_USER_ID);
132 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED,
133 ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED);
134 mOCSharesProjectionMap.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER,
135 ProviderTableMeta.OCSHARES_ACCOUNT_OWNER);
136 }
137
138 private UriMatcher mUriMatcher;
139
140 @Override
141 public int delete(Uri uri, String where, String[] whereArgs) {
142 //Log_OC.d(TAG, "Deleting " + uri + " at provider " + this);
143 int count = 0;
144 SQLiteDatabase db = mDbHelper.getWritableDatabase();
145 db.beginTransaction();
146 try {
147 count = delete(db, uri, where, whereArgs);
148 db.setTransactionSuccessful();
149 } finally {
150 db.endTransaction();
151 }
152 getContext().getContentResolver().notifyChange(uri, null);
153 return count;
154 }
155
156 private int delete(SQLiteDatabase db, Uri uri, String where, String[] whereArgs) {
157 int count = 0;
158 switch (mUriMatcher.match(uri)) {
159 case SINGLE_FILE:
160 /*Cursor c = query(db, uri, null, where, whereArgs, null);
161 String remotePath = "(unexisting)";
162 if (c != null && c.moveToFirst()) {
163 remotePath = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH));
164 }
165 Log_OC.d(TAG, "Removing FILE " + remotePath);
166 */
167 count = db.delete(ProviderTableMeta.FILE_TABLE_NAME,
168 ProviderTableMeta._ID
169 + "="
170 + uri.getPathSegments().get(1)
171 + (!TextUtils.isEmpty(where) ? " AND (" + where
172 + ")" : ""), whereArgs);
173 /* just for log
174 if (c!=null) {
175 c.close();
176 }
177 */
178 break;
179 case DIRECTORY:
180 // deletion of folder is recursive
181 /*
182 Uri folderUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, Long.parseLong(uri.getPathSegments().get(1)));
183 Cursor folder = query(db, folderUri, null, null, null, null);
184 String folderName = "(unknown)";
185 if (folder != null && folder.moveToFirst()) {
186 folderName = folder.getString(folder.getColumnIndex(ProviderTableMeta.FILE_PATH));
187 }
188 */
189 Cursor children = query(uri, null, null, null, null);
190 if (children != null && children.moveToFirst()) {
191 long childId;
192 boolean isDir;
193 //String remotePath;
194 while (!children.isAfterLast()) {
195 childId = children.getLong(children.getColumnIndex(ProviderTableMeta._ID));
196 isDir = "DIR".equals(children.getString(children.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
197 //remotePath = children.getString(children.getColumnIndex(ProviderTableMeta.FILE_PATH));
198 if (isDir) {
199 count += delete(db, ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, childId), null, null);
200 } else {
201 count += delete(db, ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, childId), null, null);
202 }
203 children.moveToNext();
204 }
205 children.close();
206 } /*else {
207 Log_OC.d(TAG, "No child to remove in DIRECTORY " + folderName);
208 }
209 Log_OC.d(TAG, "Removing DIRECTORY " + folderName + " (or maybe not) ");
210 */
211 count += db.delete(ProviderTableMeta.FILE_TABLE_NAME,
212 ProviderTableMeta._ID
213 + "="
214 + uri.getPathSegments().get(1)
215 + (!TextUtils.isEmpty(where) ? " AND (" + where
216 + ")" : ""), whereArgs);
217 /* Just for log
218 if (folder != null) {
219 folder.close();
220 }*/
221 break;
222 case ROOT_DIRECTORY:
223 //Log_OC.d(TAG, "Removing ROOT!");
224 count = db.delete(ProviderTableMeta.FILE_TABLE_NAME, where, whereArgs);
225 break;
226 case SHARES:
227 count = db.delete(ProviderTableMeta.OCSHARES_TABLE_NAME,
228 ProviderTableMeta._ID
229 + "="
230 + uri.getPathSegments().get(1)
231 + (!TextUtils.isEmpty(where) ? " AND (" + where
232 + ")" : ""), whereArgs);
233 break;
234 default:
235 //Log_OC.e(TAG, "Unknown uri " + uri);
236 throw new IllegalArgumentException("Unknown uri: " + uri.toString());
237 }
238 return count;
239 }
240
241
242 // TODO: switch(uri)
243 @Override
244 public String getType(Uri uri) {
245 switch (mUriMatcher.match(uri)) {
246 case ROOT_DIRECTORY:
247 return ProviderTableMeta.CONTENT_TYPE;
248 case SINGLE_FILE:
249 return ProviderTableMeta.CONTENT_TYPE_ITEM;
250 default:
251 throw new IllegalArgumentException("Unknown Uri id."
252 + uri.toString());
253 }
254 }
255
256 @Override
257 public Uri insert(Uri uri, ContentValues values) {
258 //Log_OC.d(TAG, "Inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
259 Uri newUri = null;
260 SQLiteDatabase db = mDbHelper.getWritableDatabase();
261 db.beginTransaction();
262 try {
263 newUri = insert(db, uri, values);
264 db.setTransactionSuccessful();
265 } finally {
266 db.endTransaction();
267 }
268 getContext().getContentResolver().notifyChange(newUri, null);
269 return newUri;
270 }
271
272 private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
273 switch (mUriMatcher.match(uri)){
274 case ROOT_DIRECTORY:
275 case SINGLE_FILE:
276 String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
277 String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
278 String[] projection = new String[] {ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH, ProviderTableMeta.FILE_ACCOUNT_OWNER };
279 String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
280 String[] whereArgs = new String[] {remotePath, accountName};
281 Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
282 if (doubleCheck == null || !doubleCheck.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
283 long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
284 if (rowId > 0) {
285 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
286 //Log_OC.d(TAG, "Inserted " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
287 return insertedFileUri;
288 } else {
289 //Log_OC.d(TAG, "Error while inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
290 throw new SQLException("ERROR " + uri);
291 }
292 } else {
293 // file is already inserted; race condition, let's avoid a duplicated entry
294 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID)));
295 doubleCheck.close();
296
297 return insertedFileUri;
298 }
299
300 case SHARES:
301 String path = values.getAsString(ProviderTableMeta.OCSHARES_PATH);
302 String accountNameShare= values.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER);
303 String[] projectionShare = new String[] {ProviderTableMeta._ID, ProviderTableMeta.OCSHARES_PATH, ProviderTableMeta.OCSHARES_ACCOUNT_OWNER };
304 String whereShare = ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
305 String[] whereArgsShare = new String[] {path, accountNameShare};
306 Cursor doubleCheckShare = query(db, uri, projectionShare, whereShare, whereArgsShare, null);
307 if (doubleCheckShare == null || !doubleCheckShare.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
308 long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
309 if (rowId >0) {
310 Uri insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
311 return insertedShareUri;
312 } else {
313 throw new SQLException("ERROR " + uri);
314
315 }
316 } else {
317 // file is already inserted; race condition, let's avoid a duplicated entry
318 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID)));
319 doubleCheckShare.close();
320
321 return insertedFileUri;
322 }
323
324
325 default:
326 throw new IllegalArgumentException("Unknown uri id: " + uri);
327 }
328
329 }
330
331
332 @Override
333 public boolean onCreate() {
334 mDbHelper = new DataBaseHelper(getContext());
335
336 String authority = getContext().getResources().getString(R.string.authority);
337 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
338 mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
339 mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
340 mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
341 mUriMatcher.addURI(authority, "dir/", DIRECTORY);
342 mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
343 mUriMatcher.addURI(authority, "shares/", SHARES);
344 mUriMatcher.addURI(authority, "shares/#", SHARES);
345
346 return true;
347 }
348
349
350 @Override
351 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
352 Cursor result = null;
353 SQLiteDatabase db = mDbHelper.getReadableDatabase();
354 db.beginTransaction();
355 try {
356 result = query(db, uri, projection, selection, selectionArgs, sortOrder);
357 db.setTransactionSuccessful();
358 } finally {
359 db.endTransaction();
360 }
361 return result;
362 }
363
364 private Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
365 SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
366
367 sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
368 sqlQuery.setProjectionMap(mFileProjectionMap);
369
370 switch (mUriMatcher.match(uri)) {
371 case ROOT_DIRECTORY:
372 break;
373 case DIRECTORY:
374 String folderId = uri.getPathSegments().get(1);
375 sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
376 + folderId);
377 break;
378 case SINGLE_FILE:
379 if (uri.getPathSegments().size() > 1) {
380 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
381 + uri.getPathSegments().get(1));
382 }
383 break;
384 case SHARES:
385 sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
386 sqlQuery.setProjectionMap(mOCSharesProjectionMap);
387 if (uri.getPathSegments().size() > 1) {
388 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
389 + uri.getPathSegments().get(1));
390 }
391 break;
392 default:
393 throw new IllegalArgumentException("Unknown uri id: " + uri);
394 }
395
396 String order;
397 if (TextUtils.isEmpty(sortOrder)) {
398 if (mUriMatcher.match(uri) == SHARES) {
399 order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
400 } else {
401
402 order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
403 }
404 } else {
405 order = sortOrder;
406 }
407
408 // DB case_sensitive
409 db.execSQL("PRAGMA case_sensitive_like = true");
410 Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
411 c.setNotificationUri(getContext().getContentResolver(), uri);
412 return c;
413 }
414
415 @Override
416 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
417
418 //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
419 int count = 0;
420 SQLiteDatabase db = mDbHelper.getWritableDatabase();
421 db.beginTransaction();
422 try {
423 count = update(db, uri, values, selection, selectionArgs);
424 db.setTransactionSuccessful();
425 } finally {
426 db.endTransaction();
427 }
428 getContext().getContentResolver().notifyChange(uri, null);
429 return count;
430 }
431
432
433
434 private int update(SQLiteDatabase db, Uri uri, ContentValues values, String selection, String[] selectionArgs) {
435 switch (mUriMatcher.match(uri)) {
436 case DIRECTORY:
437 return updateFolderSize(db, selectionArgs[0]);
438 case SHARES:
439 return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
440 default:
441 return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
442 }
443 }
444
445
446 private int updateFolderSize(SQLiteDatabase db, String folderId) {
447 int count = 0;
448 String [] whereArgs = new String[] { folderId };
449
450 // read current size saved for the folder
451 long folderSize = 0;
452 long folderParentId = -1;
453 Uri selectFolderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, folderId);
454 String[] folderProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
455 String folderWhere = ProviderTableMeta._ID + "=?";
456 Cursor folderCursor = query(db, selectFolderUri, folderProjection, folderWhere, whereArgs, null);
457 if (folderCursor != null && folderCursor.moveToFirst()) {
458 folderSize = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));;
459 folderParentId = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));;
460 }
461 folderCursor.close();
462
463 // read and sum sizes of children
464 long childrenSize = 0;
465 Uri selectChildrenUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, folderId);
466 String[] childrenProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
467 String childrenWhere = ProviderTableMeta.FILE_PARENT + "=?";
468 Cursor childrenCursor = query(db, selectChildrenUri, childrenProjection, childrenWhere, whereArgs, null);
469 if (childrenCursor != null && childrenCursor.moveToFirst()) {
470 while (!childrenCursor.isAfterLast()) {
471 childrenSize += childrenCursor.getLong(childrenCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
472 childrenCursor.moveToNext();
473 }
474 }
475 childrenCursor.close();
476
477 // update if needed
478 if (folderSize != childrenSize) {
479 Log_OC.d("FileContentProvider", "Updating " + folderSize + " to " + childrenSize);
480 ContentValues cv = new ContentValues();
481 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, childrenSize);
482 count = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, folderWhere, whereArgs);
483
484 // propagate update until root
485 if (folderParentId > FileDataStorageManager.ROOT_PARENT_ID) {
486 Log_OC.d("FileContentProvider", "Propagating update to " + folderParentId);
487 updateFolderSize(db, String.valueOf(folderParentId));
488 } else {
489 Log_OC.d("FileContentProvider", "NOT propagating to " + folderParentId);
490 }
491 } else {
492 Log_OC.d("FileContentProvider", "NOT updating, sizes are " + folderSize + " and " + childrenSize);
493 }
494 return count;
495 }
496
497
498 @Override
499 public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
500 Log_OC.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" );
501 ContentProviderResult[] results = new ContentProviderResult[operations.size()];
502 int i=0;
503
504 SQLiteDatabase db = mDbHelper.getWritableDatabase();
505 db.beginTransaction(); // it's supposed that transactions can be nested
506 try {
507 for (ContentProviderOperation operation : operations) {
508 results[i] = operation.apply(this, results, i);
509 i++;
510 }
511 db.setTransactionSuccessful();
512 } finally {
513 db.endTransaction();
514 }
515 Log_OC.d("FileContentProvider", "applied batch in provider " + this);
516 return results;
517 }
518
519
520 class DataBaseHelper extends SQLiteOpenHelper {
521
522 public DataBaseHelper(Context context) {
523 super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
524
525 }
526
527 @Override
528 public void onCreate(SQLiteDatabase db) {
529 // files table
530 Log_OC.i("SQL", "Entering in onCreate");
531 db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
532 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
533 + ProviderTableMeta.FILE_NAME + " TEXT, "
534 + ProviderTableMeta.FILE_PATH + " TEXT, "
535 + ProviderTableMeta.FILE_PARENT + " INTEGER, "
536 + ProviderTableMeta.FILE_CREATION + " INTEGER, "
537 + ProviderTableMeta.FILE_MODIFIED + " INTEGER, "
538 + ProviderTableMeta.FILE_CONTENT_TYPE + " TEXT, "
539 + ProviderTableMeta.FILE_CONTENT_LENGTH + " INTEGER, "
540 + ProviderTableMeta.FILE_STORAGE_PATH + " TEXT, "
541 + ProviderTableMeta.FILE_ACCOUNT_OWNER + " TEXT, "
542 + ProviderTableMeta.FILE_LAST_SYNC_DATE + " INTEGER, "
543 + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER, "
544 + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " INTEGER, "
545 + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER, "
546 + ProviderTableMeta.FILE_ETAG + " TEXT, "
547 + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER, "
548 + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT );"
549 );
550
551 // Create table ocshares
552 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
553 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
554 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
555 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
556 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
557 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
558 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
559 + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, "
560 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
561 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
562 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
563 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
564 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
565 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
566 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
567 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
568 }
569
570 @Override
571 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
572 Log_OC.i("SQL", "Entering in onUpgrade");
573 boolean upgraded = false;
574 if (oldVersion == 1 && newVersion >= 2) {
575 Log_OC.i("SQL", "Entering in the #1 ADD in onUpgrade");
576 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
577 " ADD COLUMN " + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
578 " DEFAULT 0");
579 upgraded = true;
580 }
581 if (oldVersion < 3 && newVersion >= 3) {
582 Log_OC.i("SQL", "Entering in the #2 ADD in onUpgrade");
583 db.beginTransaction();
584 try {
585 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
586 " ADD COLUMN " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " INTEGER " +
587 " DEFAULT 0");
588
589 // assume there are not local changes pending to upload
590 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
591 " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = " + System.currentTimeMillis() +
592 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
593
594 upgraded = true;
595 db.setTransactionSuccessful();
596 } finally {
597 db.endTransaction();
598 }
599 }
600 if (oldVersion < 4 && newVersion >= 4) {
601 Log_OC.i("SQL", "Entering in the #3 ADD in onUpgrade");
602 db.beginTransaction();
603 try {
604 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
605 " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER " +
606 " DEFAULT 0");
607
608 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
609 " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " + ProviderTableMeta.FILE_MODIFIED +
610 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
611
612 upgraded = true;
613 db.setTransactionSuccessful();
614 } finally {
615 db.endTransaction();
616 }
617 }
618 if (!upgraded)
619 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
620
621 if (oldVersion < 5 && newVersion >= 5) {
622 Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade");
623 db.beginTransaction();
624 try {
625 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
626 " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " +
627 " DEFAULT NULL");
628
629 upgraded = true;
630 db.setTransactionSuccessful();
631 } finally {
632 db.endTransaction();
633 }
634 }
635 if (!upgraded)
636 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
637
638 if (oldVersion < 6 && newVersion >= 6) {
639 Log_OC.i("SQL", "Entering in the #5 ADD in onUpgrade");
640 db.beginTransaction();
641 try {
642 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
643 " ADD COLUMN " + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER " +
644 " DEFAULT 0");
645
646 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
647 " ADD COLUMN " + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
648 " DEFAULT NULL");
649
650 // Create table ocshares
651 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
652 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
653 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
654 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
655 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
656 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
657 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
658 + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, "
659 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
660 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
661 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
662 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
663 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
664 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
665 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
666 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
667
668 upgraded = true;
669 db.setTransactionSuccessful();
670 } finally {
671 db.endTransaction();
672 }
673 }
674 if (!upgraded)
675 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
676 }
677 }
678
679 }