Merge pull request #364 from owncloud/share_link__code_improvements
[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, where, whereArgs);
228 break;
229 default:
230 //Log_OC.e(TAG, "Unknown uri " + uri);
231 throw new IllegalArgumentException("Unknown uri: " + uri.toString());
232 }
233 return count;
234 }
235
236
237 // TODO: switch(uri)
238 @Override
239 public String getType(Uri uri) {
240 switch (mUriMatcher.match(uri)) {
241 case ROOT_DIRECTORY:
242 return ProviderTableMeta.CONTENT_TYPE;
243 case SINGLE_FILE:
244 return ProviderTableMeta.CONTENT_TYPE_ITEM;
245 default:
246 throw new IllegalArgumentException("Unknown Uri id."
247 + uri.toString());
248 }
249 }
250
251 @Override
252 public Uri insert(Uri uri, ContentValues values) {
253 //Log_OC.d(TAG, "Inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
254 Uri newUri = null;
255 SQLiteDatabase db = mDbHelper.getWritableDatabase();
256 db.beginTransaction();
257 try {
258 newUri = insert(db, uri, values);
259 db.setTransactionSuccessful();
260 } finally {
261 db.endTransaction();
262 }
263 getContext().getContentResolver().notifyChange(newUri, null);
264 return newUri;
265 }
266
267 private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) {
268 switch (mUriMatcher.match(uri)){
269 case ROOT_DIRECTORY:
270 case SINGLE_FILE:
271 String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH);
272 String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER);
273 String[] projection = new String[] {ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH, ProviderTableMeta.FILE_ACCOUNT_OWNER };
274 String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
275 String[] whereArgs = new String[] {remotePath, accountName};
276 Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null);
277 if (doubleCheck == null || !doubleCheck.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
278 long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values);
279 if (rowId > 0) {
280 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId);
281 //Log_OC.d(TAG, "Inserted " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
282 return insertedFileUri;
283 } else {
284 //Log_OC.d(TAG, "Error while inserting " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
285 throw new SQLException("ERROR " + uri);
286 }
287 } else {
288 // file is already inserted; race condition, let's avoid a duplicated entry
289 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID)));
290 doubleCheck.close();
291
292 return insertedFileUri;
293 }
294
295 case SHARES:
296 String path = values.getAsString(ProviderTableMeta.OCSHARES_PATH);
297 String accountNameShare= values.getAsString(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER);
298 String[] projectionShare = new String[] {ProviderTableMeta._ID, ProviderTableMeta.OCSHARES_PATH, ProviderTableMeta.OCSHARES_ACCOUNT_OWNER };
299 String whereShare = ProviderTableMeta.OCSHARES_PATH + "=? AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
300 String[] whereArgsShare = new String[] {path, accountNameShare};
301 Cursor doubleCheckShare = query(db, uri, projectionShare, whereShare, whereArgsShare, null);
302 if (doubleCheckShare == null || !doubleCheckShare.moveToFirst()) { // ugly patch; serious refactorization is needed to reduce work in FileDataStorageManager and bring it to FileContentProvider
303 long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values);
304 if (rowId >0) {
305 Uri insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId);
306 return insertedShareUri;
307 } else {
308 throw new SQLException("ERROR " + uri);
309
310 }
311 } else {
312 // file is already inserted; race condition, let's avoid a duplicated entry
313 Uri insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, doubleCheckShare.getLong(doubleCheckShare.getColumnIndex(ProviderTableMeta._ID)));
314 doubleCheckShare.close();
315
316 return insertedFileUri;
317 }
318
319
320 default:
321 throw new IllegalArgumentException("Unknown uri id: " + uri);
322 }
323
324 }
325
326
327 @Override
328 public boolean onCreate() {
329 mDbHelper = new DataBaseHelper(getContext());
330
331 String authority = getContext().getResources().getString(R.string.authority);
332 mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
333 mUriMatcher.addURI(authority, null, ROOT_DIRECTORY);
334 mUriMatcher.addURI(authority, "file/", SINGLE_FILE);
335 mUriMatcher.addURI(authority, "file/#", SINGLE_FILE);
336 mUriMatcher.addURI(authority, "dir/", DIRECTORY);
337 mUriMatcher.addURI(authority, "dir/#", DIRECTORY);
338 mUriMatcher.addURI(authority, "shares/", SHARES);
339 mUriMatcher.addURI(authority, "shares/#", SHARES);
340
341 return true;
342 }
343
344
345 @Override
346 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
347 Cursor result = null;
348 SQLiteDatabase db = mDbHelper.getReadableDatabase();
349 db.beginTransaction();
350 try {
351 result = query(db, uri, projection, selection, selectionArgs, sortOrder);
352 db.setTransactionSuccessful();
353 } finally {
354 db.endTransaction();
355 }
356 return result;
357 }
358
359 private Cursor query(SQLiteDatabase db, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
360 SQLiteQueryBuilder sqlQuery = new SQLiteQueryBuilder();
361
362 sqlQuery.setTables(ProviderTableMeta.FILE_TABLE_NAME);
363 sqlQuery.setProjectionMap(mFileProjectionMap);
364
365 switch (mUriMatcher.match(uri)) {
366 case ROOT_DIRECTORY:
367 break;
368 case DIRECTORY:
369 String folderId = uri.getPathSegments().get(1);
370 sqlQuery.appendWhere(ProviderTableMeta.FILE_PARENT + "="
371 + folderId);
372 break;
373 case SINGLE_FILE:
374 if (uri.getPathSegments().size() > 1) {
375 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
376 + uri.getPathSegments().get(1));
377 }
378 break;
379 case SHARES:
380 sqlQuery.setTables(ProviderTableMeta.OCSHARES_TABLE_NAME);
381 sqlQuery.setProjectionMap(mOCSharesProjectionMap);
382 if (uri.getPathSegments().size() > 1) {
383 sqlQuery.appendWhere(ProviderTableMeta._ID + "="
384 + uri.getPathSegments().get(1));
385 }
386 break;
387 default:
388 throw new IllegalArgumentException("Unknown uri id: " + uri);
389 }
390
391 String order;
392 if (TextUtils.isEmpty(sortOrder)) {
393 if (mUriMatcher.match(uri) == SHARES) {
394 order = ProviderTableMeta.OCSHARES_DEFAULT_SORT_ORDER;
395 } else {
396
397 order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
398 }
399 } else {
400 order = sortOrder;
401 }
402
403 // DB case_sensitive
404 db.execSQL("PRAGMA case_sensitive_like = true");
405 Cursor c = sqlQuery.query(db, projection, selection, selectionArgs, null, null, order);
406 c.setNotificationUri(getContext().getContentResolver(), uri);
407 return c;
408 }
409
410 @Override
411 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
412
413 //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
414 int count = 0;
415 SQLiteDatabase db = mDbHelper.getWritableDatabase();
416 db.beginTransaction();
417 try {
418 count = update(db, uri, values, selection, selectionArgs);
419 db.setTransactionSuccessful();
420 } finally {
421 db.endTransaction();
422 }
423 getContext().getContentResolver().notifyChange(uri, null);
424 return count;
425 }
426
427
428
429 private int update(SQLiteDatabase db, Uri uri, ContentValues values, String selection, String[] selectionArgs) {
430 switch (mUriMatcher.match(uri)) {
431 case DIRECTORY:
432 return updateFolderSize(db, selectionArgs[0]);
433 case SHARES:
434 return db.update(ProviderTableMeta.OCSHARES_TABLE_NAME, values, selection, selectionArgs);
435 default:
436 return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
437 }
438 }
439
440
441 private int updateFolderSize(SQLiteDatabase db, String folderId) {
442 int count = 0;
443 String [] whereArgs = new String[] { folderId };
444
445 // read current size saved for the folder
446 long folderSize = 0;
447 long folderParentId = -1;
448 Uri selectFolderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, folderId);
449 String[] folderProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
450 String folderWhere = ProviderTableMeta._ID + "=?";
451 Cursor folderCursor = query(db, selectFolderUri, folderProjection, folderWhere, whereArgs, null);
452 if (folderCursor != null && folderCursor.moveToFirst()) {
453 folderSize = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));;
454 folderParentId = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));;
455 }
456 folderCursor.close();
457
458 // read and sum sizes of children
459 long childrenSize = 0;
460 Uri selectChildrenUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, folderId);
461 String[] childrenProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
462 String childrenWhere = ProviderTableMeta.FILE_PARENT + "=?";
463 Cursor childrenCursor = query(db, selectChildrenUri, childrenProjection, childrenWhere, whereArgs, null);
464 if (childrenCursor != null && childrenCursor.moveToFirst()) {
465 while (!childrenCursor.isAfterLast()) {
466 childrenSize += childrenCursor.getLong(childrenCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
467 childrenCursor.moveToNext();
468 }
469 }
470 childrenCursor.close();
471
472 // update if needed
473 if (folderSize != childrenSize) {
474 Log_OC.d("FileContentProvider", "Updating " + folderSize + " to " + childrenSize);
475 ContentValues cv = new ContentValues();
476 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, childrenSize);
477 count = db.update(ProviderTableMeta.FILE_TABLE_NAME, cv, folderWhere, whereArgs);
478
479 // propagate update until root
480 if (folderParentId > FileDataStorageManager.ROOT_PARENT_ID) {
481 Log_OC.d("FileContentProvider", "Propagating update to " + folderParentId);
482 updateFolderSize(db, String.valueOf(folderParentId));
483 } else {
484 Log_OC.d("FileContentProvider", "NOT propagating to " + folderParentId);
485 }
486 } else {
487 Log_OC.d("FileContentProvider", "NOT updating, sizes are " + folderSize + " and " + childrenSize);
488 }
489 return count;
490 }
491
492
493 @Override
494 public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
495 Log_OC.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" );
496 ContentProviderResult[] results = new ContentProviderResult[operations.size()];
497 int i=0;
498
499 SQLiteDatabase db = mDbHelper.getWritableDatabase();
500 db.beginTransaction(); // it's supposed that transactions can be nested
501 try {
502 for (ContentProviderOperation operation : operations) {
503 results[i] = operation.apply(this, results, i);
504 i++;
505 }
506 db.setTransactionSuccessful();
507 } finally {
508 db.endTransaction();
509 }
510 Log_OC.d("FileContentProvider", "applied batch in provider " + this);
511 return results;
512 }
513
514
515 class DataBaseHelper extends SQLiteOpenHelper {
516
517 public DataBaseHelper(Context context) {
518 super(context, ProviderMeta.DB_NAME, null, ProviderMeta.DB_VERSION);
519
520 }
521
522 @Override
523 public void onCreate(SQLiteDatabase db) {
524 // files table
525 Log_OC.i("SQL", "Entering in onCreate");
526 db.execSQL("CREATE TABLE " + ProviderTableMeta.FILE_TABLE_NAME + "("
527 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
528 + ProviderTableMeta.FILE_NAME + " TEXT, "
529 + ProviderTableMeta.FILE_PATH + " TEXT, "
530 + ProviderTableMeta.FILE_PARENT + " INTEGER, "
531 + ProviderTableMeta.FILE_CREATION + " INTEGER, "
532 + ProviderTableMeta.FILE_MODIFIED + " INTEGER, "
533 + ProviderTableMeta.FILE_CONTENT_TYPE + " TEXT, "
534 + ProviderTableMeta.FILE_CONTENT_LENGTH + " INTEGER, "
535 + ProviderTableMeta.FILE_STORAGE_PATH + " TEXT, "
536 + ProviderTableMeta.FILE_ACCOUNT_OWNER + " TEXT, "
537 + ProviderTableMeta.FILE_LAST_SYNC_DATE + " INTEGER, "
538 + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER, "
539 + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " INTEGER, "
540 + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER, "
541 + ProviderTableMeta.FILE_ETAG + " TEXT, "
542 + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER, "
543 + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT );"
544 );
545
546 // Create table ocshares
547 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
548 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
549 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
550 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
551 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
552 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
553 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
554 + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, "
555 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
556 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
557 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
558 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
559 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
560 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
561 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
562 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
563 }
564
565 @Override
566 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
567 Log_OC.i("SQL", "Entering in onUpgrade");
568 boolean upgraded = false;
569 if (oldVersion == 1 && newVersion >= 2) {
570 Log_OC.i("SQL", "Entering in the #1 ADD in onUpgrade");
571 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
572 " ADD COLUMN " + ProviderTableMeta.FILE_KEEP_IN_SYNC + " INTEGER " +
573 " DEFAULT 0");
574 upgraded = true;
575 }
576 if (oldVersion < 3 && newVersion >= 3) {
577 Log_OC.i("SQL", "Entering in the #2 ADD in onUpgrade");
578 db.beginTransaction();
579 try {
580 db.execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
581 " ADD COLUMN " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " INTEGER " +
582 " DEFAULT 0");
583
584 // assume there are not local changes pending to upload
585 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
586 " SET " + ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA + " = " + System.currentTimeMillis() +
587 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
588
589 upgraded = true;
590 db.setTransactionSuccessful();
591 } finally {
592 db.endTransaction();
593 }
594 }
595 if (oldVersion < 4 && newVersion >= 4) {
596 Log_OC.i("SQL", "Entering in the #3 ADD in onUpgrade");
597 db.beginTransaction();
598 try {
599 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
600 " ADD COLUMN " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " INTEGER " +
601 " DEFAULT 0");
602
603 db.execSQL("UPDATE " + ProviderTableMeta.FILE_TABLE_NAME +
604 " SET " + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA + " = " + ProviderTableMeta.FILE_MODIFIED +
605 " WHERE " + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL");
606
607 upgraded = true;
608 db.setTransactionSuccessful();
609 } finally {
610 db.endTransaction();
611 }
612 }
613 if (!upgraded)
614 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
615
616 if (oldVersion < 5 && newVersion >= 5) {
617 Log_OC.i("SQL", "Entering in the #4 ADD in onUpgrade");
618 db.beginTransaction();
619 try {
620 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
621 " ADD COLUMN " + ProviderTableMeta.FILE_ETAG + " TEXT " +
622 " DEFAULT NULL");
623
624 upgraded = true;
625 db.setTransactionSuccessful();
626 } finally {
627 db.endTransaction();
628 }
629 }
630 if (!upgraded)
631 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
632
633 if (oldVersion < 6 && newVersion >= 6) {
634 Log_OC.i("SQL", "Entering in the #5 ADD in onUpgrade");
635 db.beginTransaction();
636 try {
637 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
638 " ADD COLUMN " + ProviderTableMeta.FILE_SHARE_BY_LINK + " INTEGER " +
639 " DEFAULT 0");
640
641 db .execSQL("ALTER TABLE " + ProviderTableMeta.FILE_TABLE_NAME +
642 " ADD COLUMN " + ProviderTableMeta.FILE_PUBLIC_LINK + " TEXT " +
643 " DEFAULT NULL");
644
645 // Create table ocshares
646 db.execSQL("CREATE TABLE " + ProviderTableMeta.OCSHARES_TABLE_NAME + "("
647 + ProviderTableMeta._ID + " INTEGER PRIMARY KEY, "
648 + ProviderTableMeta.OCSHARES_FILE_SOURCE + " INTEGER, "
649 + ProviderTableMeta.OCSHARES_ITEM_SOURCE + " INTEGER, "
650 + ProviderTableMeta.OCSHARES_SHARE_TYPE + " INTEGER, "
651 + ProviderTableMeta.OCSHARES_SHARE_WITH + " TEXT, "
652 + ProviderTableMeta.OCSHARES_PATH + " TEXT, "
653 + ProviderTableMeta.OCSHARES_PERMISSIONS+ " INTEGER, "
654 + ProviderTableMeta.OCSHARES_SHARED_DATE + " INTEGER, "
655 + ProviderTableMeta.OCSHARES_EXPIRATION_DATE + " INTEGER, "
656 + ProviderTableMeta.OCSHARES_TOKEN + " TEXT, "
657 + ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME + " TEXT, "
658 + ProviderTableMeta.OCSHARES_IS_DIRECTORY + " INTEGER, " // boolean
659 + ProviderTableMeta.OCSHARES_USER_ID + " INTEGER, "
660 + ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " INTEGER,"
661 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " TEXT );" );
662
663 upgraded = true;
664 db.setTransactionSuccessful();
665 } finally {
666 db.endTransaction();
667 }
668 }
669 if (!upgraded)
670 Log_OC.i("SQL", "OUT of the ADD in onUpgrade; oldVersion == " + oldVersion + ", newVersion == " + newVersion);
671 }
672 }
673
674 }