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