+ public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
+
+ //Log_OC.d(TAG, "Updating " + values.getAsString(ProviderTableMeta.FILE_PATH) + " at provider " + this);
+ int count = 0;
+ SQLiteDatabase db = mDbHelper.getWritableDatabase();
+ db.beginTransaction();
+ try {
+ count = update(db, uri, values, selection, selectionArgs);
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ getContext().getContentResolver().notifyChange(uri, null);
+ return count;
+ }
+
+
+ private int update(SQLiteDatabase db, Uri uri, ContentValues values, String selection, String[] selectionArgs) {
+ switch (mUriMatcher.match(uri)) {
+ case DIRECTORY:
+ return updateFolderSize(db, selectionArgs[0]);
+ default:
+ return db.update(ProviderTableMeta.DB_NAME, values, selection, selectionArgs);
+ }
+ }
+
+
+ private int updateFolderSize(SQLiteDatabase db, String folderId) {
+ int count = 0;
+ String [] whereArgs = new String[] { folderId };
+
+ // read current size saved for the folder
+ long folderSize = 0;
+ long folderParentId = -1;
+ Uri selectFolderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, folderId);
+ String[] folderProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
+ String folderWhere = ProviderTableMeta._ID + "=?";
+ Cursor folderCursor = query(db, selectFolderUri, folderProjection, folderWhere, whereArgs, null);
+ if (folderCursor != null && folderCursor.moveToFirst()) {
+ folderSize = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));;
+ folderParentId = folderCursor.getLong(folderCursor.getColumnIndex(ProviderTableMeta.FILE_PARENT));;
+ }
+ folderCursor.close();
+
+ // read and sum sizes of children
+ long childrenSize = 0;
+ Uri selectChildrenUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, folderId);
+ String[] childrenProjection = new String[] { ProviderTableMeta.FILE_CONTENT_LENGTH, ProviderTableMeta.FILE_PARENT};
+ String childrenWhere = ProviderTableMeta.FILE_PARENT + "=?";
+ Cursor childrenCursor = query(db, selectChildrenUri, childrenProjection, childrenWhere, whereArgs, null);
+ if (childrenCursor != null && childrenCursor.moveToFirst()) {
+ while (!childrenCursor.isAfterLast()) {
+ childrenSize += childrenCursor.getLong(childrenCursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
+ childrenCursor.moveToNext();
+ }
+ }
+ childrenCursor.close();
+
+ // update if needed
+ if (folderSize != childrenSize) {
+ Log_OC.d("FileContentProvider", "Updating " + folderSize + " to " + childrenSize);
+ ContentValues cv = new ContentValues();
+ cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, childrenSize);
+ count = db.update(ProviderTableMeta.DB_NAME, cv, folderWhere, whereArgs);
+
+ // propagate update until root
+ if (folderParentId > FileDataStorageManager.ROOT_PARENT_ID) {
+ Log_OC.d("FileContentProvider", "Propagating update to " + folderParentId);
+ updateFolderSize(db, String.valueOf(folderParentId));
+ } else {
+ Log_OC.d("FileContentProvider", "NOT propagating to " + folderParentId);
+ }
+ } else {
+ Log_OC.d("FileContentProvider", "NOT updating, sizes are " + folderSize + " and " + childrenSize);
+ }
+ return count;
+ }
+
+
+ @Override
+ public ContentProviderResult[] applyBatch (ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
+ Log_OC.d("FileContentProvider", "applying batch in provider " + this + " (temporary: " + isTemporary() + ")" );
+ ContentProviderResult[] results = new ContentProviderResult[operations.size()];
+ int i=0;
+
+ SQLiteDatabase db = mDbHelper.getWritableDatabase();
+ db.beginTransaction(); // it's supposed that transactions can be nested
+ try {
+ for (ContentProviderOperation operation : operations) {
+ results[i] = operation.apply(this, results, i);
+ i++;
+ }
+ db.setTransactionSuccessful();
+ } finally {
+ db.endTransaction();
+ }
+ Log_OC.d("FileContentProvider", "applied batch in provider " + this);
+ return results;