- Cursor childrenLengths = query(db, selectUri, selectProjection, selectWhere, whereArgs, null);
- if (childrenLengths != null && childrenLengths.moveToFirst()) {
- long size = 0;
- while (!childrenLengths.isAfterLast()) {
- size += childrenLengths.getLong(childrenLengths.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH));
- childrenLengths.moveToNext();
+
+ // 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();