From 2ff6d73470e5862b94bf0aa887472ae8b594b78b Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Tue, 24 Dec 2019 11:19:37 +0100 Subject: [PATCH 01/19] Refactor fileExists Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 69 ++++++++----------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 60bbc4652923..d455d286f667 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -896,57 +896,46 @@ private OCFile createRootDir() { } private boolean fileExists(String cmp_key, String value) { - Cursor c; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI, - null, - cmp_key + AND - + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", - new String[]{value, account.name}, null); + Cursor cursor = getFileCursorForValue(cmp_key, value); + boolean isExists = false; + + if (cursor == null) { + Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance"); } else { - try { - c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI, - null, - cmp_key + AND - + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, null); - } catch (RemoteException e) { - Log_OC.e(TAG, "Couldn't determine file existance, assuming non existance: " + e.getMessage(), e); - return false; - } + isExists = cursor.moveToFirst(); + cursor.close(); } - boolean retval = c.moveToFirst(); - c.close(); - return retval; + + return isExists; } private Cursor getFileCursorForValue(String key, String value) { - Cursor c; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI, - null, - key + AND - + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", - new String[]{value, account.name}, null); + Cursor cursor; + ContentResolver contentResolver = getContentResolver(); + String selection = key + + AND + + ProviderTableMeta.FILE_ACCOUNT_OWNER + + "=?"; + + if (contentResolver != null) { + cursor = contentResolver.query(ProviderTableMeta.CONTENT_URI, + null, + selection, + new String[]{value, account.name}, + null); } else { try { - c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI, - null, - key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?", new String[]{value, account.name}, - null); + cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI, + null, + selection, + new String[]{value, account.name}, + null); } catch (RemoteException e) { Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e); - c = null; + cursor = null; } } - return c; + return cursor; } @Nullable From aa11e9b1ee29ec95f147461711a3b0bfa0c08fed Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Tue, 24 Dec 2019 16:44:04 +0100 Subject: [PATCH 02/19] Optimization isFileExists Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 87 +++++++++++++++++-- .../operations/RefreshFolderOperation.java | 2 +- .../SynchronizeFolderOperation.java | 3 +- 3 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index d455d286f667..c9723e6520c3 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -320,6 +320,17 @@ public void saveNewFile(OCFile newFile) { } } + private boolean isFileExists(ArrayList filesExists, OCFile file) { + for (Iterator iterator = filesExists.iterator(); iterator.hasNext(); ) { + OCFile ocFile = iterator.next(); + if (file.getFileId() == ocFile.getFileId() + || file.getRemotePath().equals(ocFile.getRemotePath())) { + iterator.remove(); + return true; + } + } + return false; + } /** * Inserts or updates the list of files contained in a given folder. @@ -331,28 +342,30 @@ public void saveNewFile(OCFile newFile) { * @param updatedFiles * @param filesToRemove */ - public void saveFolder(OCFile folder, Collection updatedFiles, Collection filesToRemove) { + public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection filesToRemove) { Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove"); ArrayList operations = new ArrayList<>(updatedFiles.size()); + ArrayList fileExistList = getFilesExistsID(updatedFiles); + // prepare operations to insert or update files to save in the given folder for (OCFile file : updatedFiles) { ContentValues cv = createContentValueForFile(file, folder); - if (fileExists(file.getFileId()) || fileExists(file.getRemotePath())) { + if (isFileExists(fileExistList, file)) { long fileId; - if (file.getFileId() != -1) { + if (file.fileExists()) { fileId = file.getFileId(); } else { fileId = getFileByPath(file.getRemotePath()).getFileId(); } // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) - .withValues(cv) + .withValues(cv) .withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)}) - .build()); + .build()); } else { // adding a new file operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build()); @@ -909,32 +922,88 @@ private boolean fileExists(String cmp_key, String value) { return isExists; } + private ArrayList getFilesExistsID(ArrayList updatedFiles) { + StringBuilder listIDString = new StringBuilder(updatedFiles.size() * 2); + StringBuilder listPathString = new StringBuilder(updatedFiles.size() * 2); + + if (updatedFiles.size() > 0) { + OCFile file = updatedFiles.get(0); + listIDString + .append(file.getFileId()); + listPathString + .append("'") + .append(file.getRemotePath()) + .append("'"); + } + for (int i = 1; i < updatedFiles.size(); i++) { + OCFile file = updatedFiles.get(i); + listIDString + .append(",") + .append(file.getFileId()); + listPathString.append(","); + listPathString + .append("'") + .append(file.getRemotePath()) + .append("'"); + } + + String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + + " = ? AND (" + + ProviderTableMeta._ID + + " IN (" + listIDString + ") OR " + + ProviderTableMeta.FILE_PATH + + " IN (" + listPathString + ") ) "; + + Cursor cursor = getCursorQueryResolver(selection, new String[]{account.name}); + ArrayList existsFiles = new ArrayList<>(); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + OCFile file = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); + file.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); + existsFiles.add(file); + } while (cursor.moveToNext()); + } + cursor.close(); + } + + return existsFiles; + } + private Cursor getFileCursorForValue(String key, String value) { - Cursor cursor; - ContentResolver contentResolver = getContentResolver(); String selection = key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; + String[] selectionArgs = {value, account.name}; + + return getCursorQueryResolver(selection, selectionArgs); + } + + private Cursor getCursorQueryResolver(String selection, String[] selectionArgs) { + Cursor cursor; + ContentResolver contentResolver = getContentResolver(); if (contentResolver != null) { cursor = contentResolver.query(ProviderTableMeta.CONTENT_URI, null, selection, - new String[]{value, account.name}, + selectionArgs, null); } else { try { cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI, null, selection, - new String[]{value, account.name}, + selectionArgs, null); } catch (RemoteException e) { Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e); cursor = null; } } + return cursor; } diff --git a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index 9026bcf65d88..da17b3c0fffe 100644 --- a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -412,7 +412,7 @@ private void synchronizeData(List folderAndFiles) { Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data "); - List updatedFiles = new ArrayList<>(folderAndFiles.size() - 1); + ArrayList updatedFiles = new ArrayList<>(folderAndFiles.size() - 1); mFilesToSyncContents.clear(); // if local folder is encrypted, download fresh metadata diff --git a/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java index e9751fbc6f49..2f89f09d9abb 100644 --- a/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -43,6 +43,7 @@ import com.owncloud.android.utils.MimeTypeUtil; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -262,7 +263,7 @@ private void synchronizeData(List folderAndFiles) throws OperationCancel } FileDataStorageManager storageManager = getStorageManager(); - List updatedFiles = new Vector<>(folderAndFiles.size() - 1); + ArrayList updatedFiles = new ArrayList<>(folderAndFiles.size() - 1); // get current data about local contents of the folder to synchronize List localFiles = storageManager.getFolderContent(mLocalFolder, false); From 068ff229a71d6e24c89857783310ce6a03279961 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Thu, 26 Dec 2019 10:29:49 +0100 Subject: [PATCH 03/19] Fix sql injection Signed-off-by: Joris Bodin --- .../com/owncloud/android/datamodel/FileDataStorageManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index c9723e6520c3..6449b784e58f 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -943,7 +943,7 @@ private ArrayList getFilesExistsID(ArrayList updatedFiles) { listPathString.append(","); listPathString .append("'") - .append(file.getRemotePath()) + .append(file.getRemotePath().replaceAll("'","''")) .append("'"); } From 339770fa923b0312284e75ed411cbfedfbaa38e6 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Thu, 26 Dec 2019 17:48:29 +0100 Subject: [PATCH 04/19] bulkInsert is more efficient avg 15s to 0.2s for 900 new files ! Remove unnecessary double check and very old method not used (since 2015) Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 121 ++---------------- .../operations/RefreshFolderOperation.java | 4 - .../providers/FileContentProvider.java | 74 ++++++----- 3 files changed, 50 insertions(+), 149 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 6449b784e58f..6b8beaa27a8f 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -349,10 +349,11 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection ArrayList operations = new ArrayList<>(updatedFiles.size()); ArrayList fileExistList = getFilesExistsID(updatedFiles); + ArrayList newFileInsertList = new ArrayList<>(); // prepare operations to insert or update files to save in the given folder for (OCFile file : updatedFiles) { - ContentValues cv = createContentValueForFile(file, folder); + ContentValues contentValues = createContentValueForFile(file, folder); if (isFileExists(fileExistList, file)) { long fileId; @@ -363,12 +364,12 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) - .withValues(cv) + .withValues(contentValues) .withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)}) .build()); } else { // adding a new file - operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build()); + newFileInsertList.add(contentValues); } } @@ -416,8 +417,13 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); try { - if (getContentResolver() != null) { - results = getContentResolver().applyBatch(MainApp.getAuthority(), operations); + ContentResolver contentResolver = getContentResolver(); + if (contentResolver != null) { + results = contentResolver.applyBatch(MainApp.getAuthority(), operations); + + ContentValues[] newFileContentValues = new ContentValues[newFileInsertList.size()]; + newFileContentValues = newFileInsertList.toArray(newFileContentValues); + int insertNumber = contentResolver.bulkInsert(ProviderTableMeta.CONTENT_URI, newFileContentValues); } else { results = getContentProviderClient().applyBatch(operations); @@ -1461,87 +1467,6 @@ public void saveShares(Collection shares) { } - public void updateSharedFiles(Collection sharedFiles) { - resetShareFlagsInAllFiles(); - - if (sharedFiles != null) { - ArrayList operations = new ArrayList<>(sharedFiles.size()); - - // prepare operations to insert or update files to save in the given folder - for (OCFile file : sharedFiles) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); - cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() - ); - cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType()); - cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); - cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); - cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - if (!file.isFolder()) { - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); - cv.put( - ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, - file.getLastSyncDateForData() - ); - cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); - cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); - cv.put(ProviderTableMeta.FILE_FAVORITE, file.isFavorite()); - cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); - - boolean existsByPath = fileExists(file.getRemotePath()); - if (existsByPath || fileExists(file.getFileId())) { - // updating an existing file - operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())}) - .build()); - - } else { - // adding a new file - operations.add( - ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI). - withValues(cv). - build() - ); - } - } - - // apply operations in batch - if (operations.size() > 0) { - @SuppressWarnings("unused") - ContentProviderResult[] results = null; - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - results = getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - results = getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } - } - } - - } - public void removeShare(OCShare share) { Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE; String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + @@ -1589,30 +1514,6 @@ public void saveSharesDB(List shares) { Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); } } - -// // TODO: review if it is needed -// // Update shared files -// ArrayList sharedFiles = new ArrayList(); -// -// for (OCShare share : shares) { -// // Get the path -// String path = share.getPath(); -// if (share.isFolder()) { -// path = path + FileUtils.PATH_SEPARATOR; -// } -// -// // Update OCFile with data from share: ShareByLink, publicLink and -// OCFile file = getFileByPath(path); -// if (file != null) { -// if (share.getShareType().equals(ShareType.PUBLIC_LINK)) { -// file.setShareViaLink(true); -// sharedFiles.add(file); -// } -// } -// } -// -// // TODO: Review -// updateSharedFiles(sharedFiles); } public void removeSharesForFile(String remotePath) { diff --git a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index da17b3c0fffe..6201e281a3d8 100644 --- a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -449,10 +449,6 @@ private void synchronizeData(List folderAndFiles) { // retrieve local data for the read file localFile = localFilesMap.remove(remoteFile.getRemotePath()); - if (localFile == null) { - localFile = mStorageManager.getFileByPath(updatedFile.getRemotePath()); - } - // add to updatedFile data about LOCAL STATE (not existing in server) updatedFile.setLastSyncDateForProperties(mCurrentSyncTime); diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index e9292b62dff8..6be4d1acfc18 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -242,6 +242,33 @@ public String getType(@NonNull Uri uri) { } } + @Override + public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { + if (isCallerNotAllowed(uri)) { + return 0; + } + + switch (mUriMatcher.match(uri)) { + case ROOT_DIRECTORY: + case SINGLE_FILE: + SQLiteDatabase database = mDbHelper.getWritableDatabase(); + database.beginTransaction(); + int contentInsert; + try { + for (ContentValues contentValues : values) { + insert(database, uri, contentValues); + } + database.setTransactionSuccessful(); + contentInsert = values.length; + } finally { + database.endTransaction(); + } + return contentInsert; + default: + return super.bulkInsert(uri, values); + } + } + @Override public Uri insert(@NonNull Uri uri, ContentValues values) { if (isCallerNotAllowed(uri)) { @@ -265,43 +292,20 @@ private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) { switch (mUriMatcher.match(uri)) { case ROOT_DIRECTORY: case SINGLE_FILE: - String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH); - String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER); - String[] projection = new String[]{ - ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH, - ProviderTableMeta.FILE_ACCOUNT_OWNER - }; - String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{remotePath, accountName}; - Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null); - // ugly patch; serious refactorization is needed to reduce work in - // FileDataStorageManager and bring it to FileContentProvider - if (doubleCheck == null || !doubleCheck.moveToFirst()) { - if (doubleCheck != null) { - doubleCheck.close(); - } - long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values); - if (rowId > 0) { - return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId); - } else { - throw new SQLException(ERROR + uri); - } + Uri insertedFileUri; + long idFile = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values); + if (idFile > 0) { + insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, idFile); } else { - // file is already inserted; race condition, let's avoid a duplicated entry - Uri insertedFileUri = ContentUris.withAppendedId( - ProviderTableMeta.CONTENT_URI_FILE, - doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID)) - ); - doubleCheck.close(); - - return insertedFileUri; + throw new SQLException(ERROR + uri); } + return insertedFileUri; case SHARES: Uri insertedShareUri; - long rowId = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values); - if (rowId > 0) { - insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, rowId); + long idShares = db.insert(ProviderTableMeta.OCSHARES_TABLE_NAME, null, values); + if (idShares > 0) { + insertedShareUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_SHARE, idShares); } else { throw new SQLException(ERROR + uri); @@ -312,9 +316,9 @@ private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) { case CAPABILITIES: Uri insertedCapUri; - long id = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values); - if (id > 0) { - insertedCapUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, id); + long idCapabilities = db.insert(ProviderTableMeta.CAPABILITIES_TABLE_NAME, null, values); + if (idCapabilities > 0) { + insertedCapUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_CAPABILITIES, idCapabilities); } else { throw new SQLException(ERROR + uri); } From 87a8ab5f07e659aca7960d46b00c9a35b60d02ab Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Fri, 27 Dec 2019 10:52:33 +0100 Subject: [PATCH 05/19] Split query for SQL variables limit and remove unused method Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 104 +++++++++--------- 1 file changed, 49 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 6b8beaa27a8f..4f92783398c1 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -307,19 +307,6 @@ public OCFile saveFileWithParent(OCFile file, Context context) { return file; } - public void saveNewFile(OCFile newFile) { - String remoteParentPath = new File(newFile.getRemotePath()).getParent(); - remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ? - remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR; - OCFile parent = getFileByPath(remoteParentPath); - if (parent != null) { - newFile.setParentId(parent.getFileId()); - saveFile(newFile); - } else { - throw new IllegalArgumentException("Saving a new file in an unexisting folder"); - } - } - private boolean isFileExists(ArrayList filesExists, OCFile file) { for (Iterator iterator = filesExists.iterator(); iterator.hasNext(); ) { OCFile ocFile = iterator.next(); @@ -929,50 +916,57 @@ private boolean fileExists(String cmp_key, String value) { } private ArrayList getFilesExistsID(ArrayList updatedFiles) { - StringBuilder listIDString = new StringBuilder(updatedFiles.size() * 2); - StringBuilder listPathString = new StringBuilder(updatedFiles.size() * 2); - - if (updatedFiles.size() > 0) { - OCFile file = updatedFiles.get(0); - listIDString - .append(file.getFileId()); - listPathString - .append("'") - .append(file.getRemotePath()) - .append("'"); - } - for (int i = 1; i < updatedFiles.size(); i++) { - OCFile file = updatedFiles.get(i); - listIDString - .append(",") - .append(file.getFileId()); - listPathString.append(","); - listPathString - .append("'") - .append(file.getRemotePath().replaceAll("'","''")) - .append("'"); - } - - String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER - + " = ? AND (" - + ProviderTableMeta._ID - + " IN (" + listIDString + ") OR " - + ProviderTableMeta.FILE_PATH - + " IN (" + listPathString + ") ) "; - - Cursor cursor = getCursorQueryResolver(selection, new String[]{account.name}); - ArrayList existsFiles = new ArrayList<>(); - if (cursor != null) { - if (cursor.moveToFirst()) { - do { - OCFile file = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); - file.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); - existsFiles.add(file); - } while (cursor.moveToNext()); + ArrayList existsFiles = new ArrayList<>(); + ArrayList listIDString = new ArrayList<>(); + ArrayList listPathString = new ArrayList<>(); + + int totalSize, processSize, loopSize; + totalSize = updatedFiles.size(); + processSize = 0; + + do { + loopSize = Math.min((totalSize - processSize), 499); + + listIDString.clear(); + listPathString.clear(); + StringBuilder inList = new StringBuilder(loopSize * 2); + for (int i = 0; i < loopSize; i++, processSize++) { + OCFile file = updatedFiles.get(processSize); + if (i > 0) { + inList.append(","); + } + inList.append("?"); + listIDString.add(String.valueOf(file.getFileId())); + listPathString.add(file.getRemotePath()); + } + + String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + + " = ? AND (" + + ProviderTableMeta._ID + + " IN (" + inList + ") OR " + + ProviderTableMeta.FILE_PATH + + " IN (" + inList + "))"; + + ArrayList selectionArgsList = new ArrayList<>(); + selectionArgsList.add(account.name); + selectionArgsList.addAll(listIDString); + selectionArgsList.addAll(listPathString); + String[] selectionArgs = selectionArgsList.toArray(new String[0]); + + Cursor cursor = getCursorQueryResolver(selection, selectionArgs); + + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + OCFile file = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); + file.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); + existsFiles.add(file); + } while (cursor.moveToNext()); + } + cursor.close(); } - cursor.close(); - } + } while ((totalSize - processSize) > 0); return existsFiles; } From 1c198467110a75214a150a12498f1c5c1b34c84f Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Fri, 27 Dec 2019 12:07:05 +0100 Subject: [PATCH 06/19] Remove bulkInsert and optimize applyBatch (only >= Android M) Before avg 6s (for avg new 1000 files) now 0.2s with applyBatch Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 8 +--- .../providers/FileContentProvider.java | 44 +++++-------------- 2 files changed, 13 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 4f92783398c1..7644066f6146 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -336,7 +336,6 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection ArrayList operations = new ArrayList<>(updatedFiles.size()); ArrayList fileExistList = getFilesExistsID(updatedFiles); - ArrayList newFileInsertList = new ArrayList<>(); // prepare operations to insert or update files to save in the given folder for (OCFile file : updatedFiles) { @@ -356,7 +355,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection .build()); } else { // adding a new file - newFileInsertList.add(contentValues); + operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(contentValues).build()); } } @@ -407,11 +406,6 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection ContentResolver contentResolver = getContentResolver(); if (contentResolver != null) { results = contentResolver.applyBatch(MainApp.getAuthority(), operations); - - ContentValues[] newFileContentValues = new ContentValues[newFileInsertList.size()]; - newFileContentValues = newFileInsertList.toArray(newFileContentValues); - int insertNumber = contentResolver.bulkInsert(ProviderTableMeta.CONTENT_URI, newFileContentValues); - } else { results = getContentProviderClient().applyBatch(operations); } diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index 6be4d1acfc18..aeba0fb9292a 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -40,6 +40,7 @@ import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.os.Binder; +import android.os.Build; import android.text.TextUtils; import com.nextcloud.client.core.Clock; @@ -242,33 +243,6 @@ public String getType(@NonNull Uri uri) { } } - @Override - public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { - if (isCallerNotAllowed(uri)) { - return 0; - } - - switch (mUriMatcher.match(uri)) { - case ROOT_DIRECTORY: - case SINGLE_FILE: - SQLiteDatabase database = mDbHelper.getWritableDatabase(); - database.beginTransaction(); - int contentInsert; - try { - for (ContentValues contentValues : values) { - insert(database, uri, contentValues); - } - database.setTransactionSuccessful(); - contentInsert = values.length; - } finally { - database.endTransaction(); - } - return contentInsert; - default: - return super.bulkInsert(uri, values); - } - } - @Override public Uri insert(@NonNull Uri uri, ContentValues values) { if (isCallerNotAllowed(uri)) { @@ -666,16 +640,22 @@ public ContentProviderResult[] applyBatch(@NonNull ArrayList= Build.VERSION_CODES.M && operation.isInsert()) { + ContentValues contentValues = operation.resolveValueBackReferences(results, i); + Uri newUri = insert(database, operation.getUri(), contentValues); + results[i] = new ContentProviderResult(newUri); + } else { + results[i] = operation.apply(this, results, i); + } i++; } - db.setTransactionSuccessful(); + database.setTransactionSuccessful(); } finally { - db.endTransaction(); + database.endTransaction(); } Log_OC.d("FileContentProvider", "applied batch in provider " + this); return results; From 235ca28bc66ae54c57e924f2ef8dead71d99be26 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Thu, 2 Jan 2020 10:34:15 +0100 Subject: [PATCH 07/19] Optimization RemoveSharesInFolder Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 159 +++++++++--------- 1 file changed, 83 insertions(+), 76 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 7644066f6146..fe025a531ec8 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -153,9 +153,9 @@ public boolean fileExists(String path) { } - public List getFolderContent(OCFile f, boolean onlyOnDevice) { - if (f != null && f.isFolder() && f.getFileId() != -1) { - return getFolderContent(f.getFileId(), onlyOnDevice); + public List getFolderContent(OCFile ocFile, boolean onlyOnDevice) { + if (ocFile != null && ocFile.isFolder() && ocFile.fileExists()) { + return getFolderContent(ocFile.getFileId(), onlyOnDevice); } else { return new ArrayList<>(); } @@ -841,49 +841,28 @@ public void migrateStoredFiles(String srcPath, String dstPath) } private List getFolderContent(long parentId, boolean onlyOnDevice) { + List folderContent = new ArrayList<>(); - List ret = new ArrayList<>(); + Uri requestURI = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId)); - Uri req_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId)); - Cursor c; + String selection = ProviderTableMeta.FILE_PARENT + "=?"; + String[] selectionArgs = {String.valueOf(parentId)}; - if (getContentProviderClient() != null) { - try { - c = getContentProviderClient().query( - req_uri, - null, - ProviderTableMeta.FILE_PARENT + "=?", - new String[]{String.valueOf(parentId)}, - null - ); - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage(), e); - return ret; - } - } else { - c = getContentResolver().query( - req_uri, - null, - ProviderTableMeta.FILE_PARENT + "=?", - new String[]{String.valueOf(parentId)}, - null - ); - } + Cursor cursor = getCursorQueryResolver(requestURI, selection, selectionArgs); - if (c != null) { - if (c.moveToFirst()) { + if (cursor != null) { + if (cursor.moveToFirst()) { do { - OCFile child = createFileInstance(c); + OCFile child = createFileInstance(cursor); if (!onlyOnDevice || child.existsOnDevice()) { - ret.add(child); + folderContent.add(child); } - } while (c.moveToNext()); + } while (cursor.moveToNext()); } - - c.close(); + cursor.close(); } - return ret; + return folderContent; } @@ -948,7 +927,7 @@ private ArrayList getFilesExistsID(ArrayList updatedFiles) { selectionArgsList.addAll(listPathString); String[] selectionArgs = selectionArgsList.toArray(new String[0]); - Cursor cursor = getCursorQueryResolver(selection, selectionArgs); + Cursor cursor = getCursorQueryResolver(ProviderTableMeta.CONTENT_URI, selection, selectionArgs); if (cursor != null) { if (cursor.moveToFirst()) { @@ -972,28 +951,28 @@ private Cursor getFileCursorForValue(String key, String value) { + "=?"; String[] selectionArgs = {value, account.name}; - return getCursorQueryResolver(selection, selectionArgs); + return getCursorQueryResolver(ProviderTableMeta.CONTENT_URI, selection, selectionArgs); } - private Cursor getCursorQueryResolver(String selection, String[] selectionArgs) { + private Cursor getCursorQueryResolver(Uri requestURI, String selection, String[] selectionArgs) { Cursor cursor; ContentResolver contentResolver = getContentResolver(); if (contentResolver != null) { - cursor = contentResolver.query(ProviderTableMeta.CONTENT_URI, + cursor = contentResolver.query(requestURI, null, selection, selectionArgs, null); } else { try { - cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI, + cursor = getContentProviderClient().query(requestURI, null, selection, selectionArgs, null); } catch (RemoteException e) { - Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e); + Log_OC.e(TAG, e.getMessage(), e); cursor = null; } } @@ -1562,59 +1541,87 @@ public void saveSharesInFolder(ArrayList shares, OCFile folder) { * @return */ private ArrayList prepareInsertShares( - List shares, ArrayList operations) { + List shares, ArrayList operations) { if (shares != null) { - ContentValues cv; + ContentValues contentValues; // prepare operations to insert or update files to save in the given folder for (OCShare share : shares) { - cv = new ContentValues(); - cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); - cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); - cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); - cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); - cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); - cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); - cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); - cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); - cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); - cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); - cv.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); + contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); + contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); + contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); + contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); + contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); + contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); + contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); + contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); + contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); + contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); // adding a new share resource - operations.add(ContentProviderOperation.newInsert( - ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build()); + operations.add(ContentProviderOperation + .newInsert(ProviderTableMeta.CONTENT_URI_SHARE) + .withValues(contentValues) + .build()); } } return operations; } private ArrayList prepareRemoveSharesInFolder( - OCFile folder, ArrayList preparedOperations) { + OCFile folder, ArrayList preparedOperations) { + if (folder != null) { - String where = ProviderTableMeta.OCSHARES_PATH + AND - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{"", account.name}; + List folderContent = getFolderContent(folder, false); - List files = getFolderContent(folder, false); + ArrayList listPathString = new ArrayList<>(); + + int totalSize, processSize, loopSize; + totalSize = folderContent.size(); + processSize = 0; + + do { + loopSize = Math.min((totalSize - processSize), 998); + + listPathString.clear(); + StringBuilder inList = new StringBuilder(folderContent.size() * 2); + for (int i = 0; i < loopSize; i++, processSize++) { + OCFile file = folderContent.get(processSize); + if (i > 0) { + inList.append(","); + } + inList.append("?"); + + listPathString.add(file.getRemotePath()); + } + + String selection = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + + ProviderTableMeta.OCSHARES_PATH + + " IN (" + inList + ")"; + + ArrayList selectionArgsList = new ArrayList<>(); + selectionArgsList.add(account.name); + selectionArgsList.addAll(listPathString); + String[] selectionArgs = selectionArgsList.toArray(new String[0]); - for (OCFile file : files) { - whereArgs[0] = file.getRemotePath(); preparedOperations.add( - ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE). - withSelection(where, whereArgs). - build() + ContentProviderOperation + .newDelete(ProviderTableMeta.CONTENT_URI_SHARE) + .withSelection(selection, selectionArgs) + .build() ); - } + } while ((totalSize - processSize) > 0); } - return preparedOperations; + return preparedOperations; } private ArrayList prepareRemoveSharesInFile( From 724147d27281101137a91e218da444bc8bb9793e Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Thu, 2 Jan 2020 18:52:55 +0100 Subject: [PATCH 08/19] Begin refactor FileDataStorageManager And readd localFile = mStorageManager.getFileByPath(updatedFile.getRemotePath()); And optimize insert virtualFile Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 1394 ++++++++--------- .../operations/RefreshFolderOperation.java | 4 + .../providers/FileContentProvider.java | 24 + .../android/ui/adapter/OCFileListAdapter.java | 3 +- 4 files changed, 672 insertions(+), 753 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index fe025a531ec8..910d44adefff 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -75,8 +75,8 @@ public class FileDataStorageManager { private static final String TAG = FileDataStorageManager.class.getSimpleName(); - private static final String AND = "=? AND "; - private static final String FAILED_TO_INSERT_MSG = "Fail to insert insert file to database "; + private static final String AND = " = ? AND "; + private static final String FAILED_TO_INSERT_MSG = "Fail to insert file to database "; private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider"; private static final String EXCEPTION_MSG = "Exception in batch of operations "; @@ -87,25 +87,56 @@ public class FileDataStorageManager { private ContentProviderClient contentProviderClient; @Setter private Account account; - public FileDataStorageManager(Account account, ContentResolver cr) { - contentProviderClient = null; - contentResolver = cr; + public FileDataStorageManager(Account account, ContentResolver contentResolver) { + this.contentProviderClient = null; + this.contentResolver = contentResolver; this.account = account; } - public FileDataStorageManager(Account account, ContentProviderClient cp) { - contentProviderClient = cp; - contentResolver = null; + public FileDataStorageManager(Account account, ContentProviderClient contentProviderClient) { + this.contentProviderClient = contentProviderClient; + this.contentResolver = null; this.account = account; } + private Cursor executeQuery(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder, String errorMessage) { + Cursor cursor; + ContentResolver contentResolver = getContentResolver(); + + if (contentResolver != null) { + cursor = contentResolver.query(uri, + projection, + selection, + selectionArgs, + sortOrder); + } else { + try { + cursor = getContentProviderClient().query(uri, + projection, + selection, + selectionArgs, + sortOrder); + } catch (RemoteException e) { + Log_OC.e(TAG, errorMessage + e.getMessage(), e); + cursor = null; + } + } + + return cursor; + } + + private Cursor executeQuery(Uri uri, String selection, String[] selectionArgs, String errorMessage) { + return executeQuery(uri, null, selection, selectionArgs, null, errorMessage); + } + public OCFile getFileByPath(String path) { - Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path); + Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path); OCFile file = null; - if (c.moveToFirst()) { - file = createFileInstance(c); + if (cursor.moveToFirst()) { + file = createFileInstance(cursor); } - c.close(); + cursor.close(); if (file == null && OCFile.ROOT_PATH.equals(path)) { return createRootDir(); // root should always exist } @@ -114,33 +145,33 @@ public OCFile getFileByPath(String path) { public @Nullable OCFile getFileById(long id) { - Cursor c = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); + Cursor cursor = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); OCFile file = null; - if (c.moveToFirst()) { - file = createFileInstance(c); + if (cursor.moveToFirst()) { + file = createFileInstance(cursor); } - c.close(); + cursor.close(); return file; } public OCFile getFileByLocalPath(String path) { - Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path); + Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path); OCFile file = null; - if (c.moveToFirst()) { - file = createFileInstance(c); + if (cursor.moveToFirst()) { + file = createFileInstance(cursor); } - c.close(); + cursor.close(); return file; } public @Nullable OCFile getFileByRemoteId(String remoteId) { - Cursor c = getFileCursorForValue(ProviderTableMeta.FILE_REMOTE_ID, remoteId); + Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_REMOTE_ID, remoteId); OCFile file = null; - if (c.moveToFirst()) { - file = createFileInstance(c); + if (cursor.moveToFirst()) { + file = createFileInstance(cursor); } - c.close(); + cursor.close(); return file; } @@ -183,8 +214,8 @@ public boolean saveFile(OCFile file) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + file.getModificationTimestampAtLastSyncForData() ); cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); @@ -219,7 +250,7 @@ public boolean saveFile(OCFile file) { boolean sameRemotePath = fileExists(file.getRemotePath()); if (sameRemotePath || - fileExists(file.getFileId())) { // for renamed files; no more delete and create + fileExists(file.getFileId())) { // for renamed files; no more delete and create if (sameRemotePath) { @@ -230,13 +261,13 @@ public boolean saveFile(OCFile file) { overridden = true; if (getContentResolver() != null) { getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, - ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())}); + ProviderTableMeta._ID + " = ?", + new String[]{String.valueOf(file.getFileId())}); } else { try { getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, - cv, ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())}); + cv, ProviderTableMeta._ID + " = ?", + new String[]{String.valueOf(file.getFileId())}); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } @@ -262,10 +293,10 @@ public boolean saveFile(OCFile file) { } /** - * traverses a files parent tree to be able to store a file with its parents. - * Throws a RemoteOperationFailedException in case the parent can't be retrieved. + * traverses a files parent tree to be able to store a file with its parents. Throws a + * RemoteOperationFailedException in case the parent can't be retrieved. * - * @param file the file + * @param file the file * @param context the app context * @return the parent file */ @@ -288,7 +319,7 @@ public OCFile saveFileWithParent(OCFile file, Context context) { } else { Exception exception = result.getException(); String message = "Error during saving file with parents: " + file.getRemotePath() + " / " - + result.getLogMessage(); + + result.getLogMessage(); if (exception != null) { throw new RemoteOperationFailedException(message, exception); @@ -321,9 +352,9 @@ private boolean isFileExists(ArrayList filesExists, OCFile file) { /** * Inserts or updates the list of files contained in a given folder. - * - * CALLER IS RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD. - * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED + *

+ * CALLER IS RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD. HERE ONLY DATA CONSISTENCY + * SHOULD BE GRANTED * * @param folder * @param updatedFiles @@ -331,7 +362,7 @@ private boolean isFileExists(ArrayList filesExists, OCFile file) { */ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection filesToRemove) { Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() - + " children and " + filesToRemove.size() + " files to remove"); + + " children and " + filesToRemove.size() + " files to remove"); ArrayList operations = new ArrayList<>(updatedFiles.size()); @@ -351,7 +382,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) .withValues(contentValues) - .withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(fileId)}) + .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(fileId)}) .build()); } else { // adding a new file @@ -360,7 +391,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } // prepare operations to remove files in the given folder - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[2]; for (OCFile file : filesToRemove) { if (file.getParentId() == folder.getFileId()) { @@ -368,8 +399,8 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection whereArgs[1] = file.getRemotePath(); if (file.isFolder()) { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())) - .withSelection(where, whereArgs).build()); + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())) + .withSelection(where, whereArgs).build()); File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(account.name, file)); if (localFolder.exists()) { @@ -377,8 +408,8 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } } else { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())) - .withSelection(where, whereArgs).build()); + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())) + .withSelection(where, whereArgs).build()); if (file.isDown()) { String path = file.getStoragePath(); @@ -394,9 +425,9 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection ContentValues cv = createContentValueForFile(folder); operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) - .withValues(cv) - .withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(folder.getFileId())}) - .build()); + .withValues(cv) + .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(folder.getFileId())}) + .build()); // apply operations in batch ContentProviderResult[] results = null; @@ -427,7 +458,6 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } if (result.uri != null) { newId = Long.parseLong(result.uri.getPathSegments().get(1)); - //updatedFiles.get(i).setFileId(newId); if (file != null) { file.setFileId(newId); } @@ -440,8 +470,8 @@ private ContentValues createContentValueForFile(OCFile folder) { ContentValues cv = new ContentValues(); cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - folder.getModificationTimestampAtLastSyncForData() + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + folder.getModificationTimestampAtLastSyncForData() ); cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); @@ -523,7 +553,7 @@ public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocal //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, // ""+file.getFileId()); Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[]{account.name, file.getRemotePath()}; int deleted = 0; if (getContentProviderClient() != null) { @@ -578,7 +608,7 @@ public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeL private boolean removeFolderInDb(OCFile folder) { Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(folder.getFileId())); // URI // for recursive deletion - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[]{account.name, folder.getRemotePath()}; int deleted = 0; if (getContentProviderClient() != null) { @@ -600,20 +630,18 @@ private boolean removeLocalFolder(OCFile folder) { if (localFolder.exists()) { // stage 1: remove the local files already registered in the files database List files = getFolderContent(folder.getFileId(), false); - if (files != null) { - for (OCFile file : files) { - if (file.isFolder()) { - success &= removeLocalFolder(file); - } else { - if (file.isDown()) { - File localFile = new File(file.getStoragePath()); - success &= localFile.delete(); - if (success) { - // notify MediaScanner about removed file - deleteFileInMediaScan(file.getStoragePath()); - file.setStoragePath(null); - saveFile(file); - } + for (OCFile file : files) { + if (file.isFolder()) { + success &= removeLocalFolder(file); + } else { + if (file.isDown()) { + File localFile = new File(file.getStoragePath()); + success &= localFile.delete(); + if (success) { + // notify MediaScanner about removed file + deleteFileInMediaScan(file.getStoragePath()); + file.setStoragePath(null); + saveFile(file); } } } @@ -644,9 +672,8 @@ private boolean removeLocalFolder(File localFolder) { /** * Updates database and file system for a file or folder that was moved to a different location. - * - * TODO explore better (faster) implementations - * TODO throw exceptions up ! + *

+ * TODO explore better (faster) implementations TODO throw exceptions up ! */ public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) { @@ -662,11 +689,11 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat if (getContentProviderClient() != null) { try { c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, file.getRemotePath() + "%"}, - ProviderTableMeta.FILE_PATH + " ASC " + ProviderTableMeta.CONTENT_URI, + null, + ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", + new String[]{account.name, file.getRemotePath() + "%"}, + ProviderTableMeta.FILE_PATH + " ASC " ); } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage(), e); @@ -674,11 +701,11 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat } else { c = getContentResolver().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, file.getRemotePath() + "%"}, - ProviderTableMeta.FILE_PATH + " ASC " + ProviderTableMeta.CONTENT_URI, + null, + ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", + new String[]{account.name, file.getRemotePath() + "%"}, + ProviderTableMeta.FILE_PATH + " ASC " ); } @@ -695,13 +722,13 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat ContentValues cv = new ContentValues(); // keep construction in the loop OCFile child = createFileInstance(c); cv.put( - ProviderTableMeta.FILE_PATH, - targetPath + child.getRemotePath().substring(lengthOfOldPath) + ProviderTableMeta.FILE_PATH, + targetPath + child.getRemotePath().substring(lengthOfOldPath) ); if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) { // update link to downloaded content - but local move is not done here! String targetLocalPath = defaultSavePath + targetPath + - child.getStoragePath().substring(lengthOfOldStoragePath); + child.getStoragePath().substring(lengthOfOldStoragePath); cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath); @@ -716,10 +743,10 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat } fileId[0] = String.valueOf(child.getFileId()); operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + "=?", fileId) - .build()); + ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). + withValues(cv). + withSelection(ProviderTableMeta._ID + " = ?", fileId) + .build()); } while (c.moveToNext()); } @@ -786,22 +813,22 @@ public void copyLocalFile(OCFile file, String targetPath) { } public void migrateStoredFiles(String srcPath, String dstPath) - throws RemoteException, OperationApplicationException { + throws RemoteException, OperationApplicationException { Cursor cursor; try { if (getContentResolver() != null) { cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE, - null, - ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", - null, - null); + null, + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", + null, + null); } else { cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE, - new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH}, - ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", - null, - null); + new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH}, + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", + null, + null); } } catch (RemoteException e) { Log_OC.e(TAG, e.getMessage(), e); @@ -815,17 +842,17 @@ public void migrateStoredFiles(String srcPath, String dstPath) ContentValues cv = new ContentValues(); fileId[0] = String.valueOf(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); String oldFileStoragePath = - cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); + cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); if (oldFileStoragePath.startsWith(srcPath)) { cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(srcPath, dstPath)); operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + "=?", fileId) - .build()); + ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). + withValues(cv). + withSelection(ProviderTableMeta._ID + " = ?", fileId) + .build()); } } while (cursor.moveToNext()); @@ -845,10 +872,10 @@ private List getFolderContent(long parentId, boolean onlyOnDevice) { Uri requestURI = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId)); - String selection = ProviderTableMeta.FILE_PARENT + "=?"; + String selection = ProviderTableMeta.FILE_PARENT + " = ?"; String[] selectionArgs = {String.valueOf(parentId)}; - Cursor cursor = getCursorQueryResolver(requestURI, selection, selectionArgs); + Cursor cursor = executeQuery(requestURI, selection, selectionArgs, ""); if (cursor != null) { if (cursor.moveToFirst()) { @@ -927,7 +954,7 @@ private ArrayList getFilesExistsID(ArrayList updatedFiles) { selectionArgsList.addAll(listPathString); String[] selectionArgs = selectionArgsList.toArray(new String[0]); - Cursor cursor = getCursorQueryResolver(ProviderTableMeta.CONTENT_URI, selection, selectionArgs); + Cursor cursor = executeQuery(ProviderTableMeta.CONTENT_URI, selection, selectionArgs, "getFilesExistsID "); if (cursor != null) { if (cursor.moveToFirst()) { @@ -948,47 +975,17 @@ private Cursor getFileCursorForValue(String key, String value) { String selection = key + AND + ProviderTableMeta.FILE_ACCOUNT_OWNER - + "=?"; + + " = ?"; String[] selectionArgs = {value, account.name}; - return getCursorQueryResolver(ProviderTableMeta.CONTENT_URI, selection, selectionArgs); - } - - private Cursor getCursorQueryResolver(Uri requestURI, String selection, String[] selectionArgs) { - Cursor cursor; - ContentResolver contentResolver = getContentResolver(); - - if (contentResolver != null) { - cursor = contentResolver.query(requestURI, - null, - selection, - selectionArgs, - null); - } else { - try { - cursor = getContentProviderClient().query(requestURI, - null, - selection, - selectionArgs, - null); - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage(), e); - cursor = null; - } - } - - return cursor; + return executeQuery(ProviderTableMeta.CONTENT_URI, selection, selectionArgs, "Could not get file details: "); } @Nullable - private OCFile createFileInstanceFromVirtual(Cursor c) { - OCFile file = null; - if (c != null) { - long fileId = c.getLong(c.getColumnIndex(ProviderTableMeta.VIRTUAL_OCFILE_ID)); - file = getFileById(fileId); - } + private OCFile createFileInstanceFromVirtual(Cursor cursor) { + long fileId = cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.VIRTUAL_OCFILE_ID)); - return file; + return getFileById(fileId); } private OCFile createFileInstance(Cursor c) { @@ -1014,7 +1011,7 @@ private OCFile createFileInstance(Cursor c) { file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION))); file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED))); file.setModificationTimestampAtLastSyncForData(c.getLong( - c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); + c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); file.setLastSyncDateForProperties(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE))); file.setLastSyncDateForData(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA))); file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG))); @@ -1033,7 +1030,7 @@ private OCFile createFileInstance(Cursor c) { file.setFileName(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME))); } file.setMountType(WebdavEntry.MountType.values()[c.getInt( - c.getColumnIndex(ProviderTableMeta.FILE_MOUNT_TYPE))]); + c.getColumnIndex(ProviderTableMeta.FILE_MOUNT_TYPE))]); file.setPreviewAvailable(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_HAS_PREVIEW)) == 1); file.setUnreadCommentsCount(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT))); file.setOwnerId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_OWNER_ID))); @@ -1060,88 +1057,89 @@ private OCFile createFileInstance(Cursor c) { return file; } - // Methods for Shares - public boolean saveShare(OCShare share) { - boolean overridden = false; - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); - cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); - cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); - cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); - cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); - cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); - cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); - cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); - cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); - cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); - cv.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); + public void saveShare(OCShare share) { + Uri contentUriShare = ProviderTableMeta.CONTENT_URI_SHARE; + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); + contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); + contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); + contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); + contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); + contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); + contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); + contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); + contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); + contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); if (shareExistsForRemoteId(share.getRemoteId())) {// for renamed files; no more delete and create - overridden = true; + String where = ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " = ?"; + String[] selectionArgs = {String.valueOf(share.getRemoteId())}; + if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv, - ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[]{String.valueOf(share.getRemoteId())}); + getContentResolver().update(contentUriShare, + contentValues, + where, + selectionArgs); } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE, - cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[]{String.valueOf(share.getRemoteId())}); + getContentProviderClient().update(contentUriShare, + contentValues, + where, + selectionArgs); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } } } else { - Uri result_uri = null; + Uri resultUri = null; if (getContentResolver() != null) { - result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv); + resultUri = getContentResolver().insert(contentUriShare, contentValues); } else { try { - result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_SHARE, cv); + resultUri = getContentProviderClient().insert(contentUriShare, contentValues); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } } - if (result_uri != null) { - long new_id = Long.parseLong(result_uri.getPathSegments().get(1)); + if (resultUri != null) { + long new_id = Long.parseLong(resultUri.getPathSegments().get(1)); share.setId(new_id); } } - - return overridden; } /** * Retrieves an stored {@link OCShare} given its id. * - * @param id Identifier. + * @param id Identifier. * @return Stored {@link OCShare} given its id. */ public OCShare getShareById(long id) { OCShare share = null; - Cursor c = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); - if (c != null) { - if (c.moveToFirst()) { - share = createShareInstance(c); + Cursor cursor = getShareCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); + if (cursor != null) { + if (cursor.moveToFirst()) { + share = createShareInstance(cursor); } - c.close(); + cursor.close(); } return share; } /** - * Checks the existance of an stored {@link OCShare} matching the given remote id (not to be confused with - * the local id) in the current account. + * Checks the existance of an stored {@link OCShare} matching the given remote id (not to be confused with the local + * id) in the current account. * - * @param remoteId Remote of the share in the server. - * @return 'True' if a matching {@link OCShare} is stored in the current account. + * @param remoteId Remote of the share in the server. + * @return 'True' if a matching {@link OCShare} is stored in the current account. */ private boolean shareExistsForRemoteId(long remoteId) { return shareExistsForValue(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId)); @@ -1149,64 +1147,51 @@ private boolean shareExistsForRemoteId(long remoteId) { /** - * Checks the existance of an stored {@link OCShare} in the current account - * matching a given column and a value for that column + * Checks the existance of an stored {@link OCShare} in the current account matching a given column and a value for + * that column * - * @param key Name of the column to match. - * @param value Value of the column to match. - * @return 'True' if a matching {@link OCShare} is stored in the current account. + * @param key Name of the column to match. + * @param value Value of the column to match. + * @return 'True' if a matching {@link OCShare} is stored in the current account. */ private boolean shareExistsForValue(String key, String value) { - Cursor c = getShareCursorForValue(key, value); - boolean retval = c.moveToFirst(); - c.close(); + Cursor cursor = getShareCursorForValue(key, value); + boolean retval = cursor.moveToFirst(); + cursor.close(); + return retval; } /** - * Gets a {@link Cursor} for an stored {@link OCShare} in the current account - * matching a given column and a value for that column + * Gets a {@link Cursor} for an stored {@link OCShare} in the current account matching a given column and a value + * for that column * - * @param key Name of the column to match. - * @param value Value of the column to match. - * @return 'True' if a matching {@link OCShare} is stored in the current account. + * @param key Name of the column to match. + * @param value Value of the column to match. + * @return 'True' if a matching {@link OCShare} is stored in the current account. */ private Cursor getShareCursorForValue(String key, String value) { - Cursor c; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI_SHARE, + String selection = key + AND + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; + String[] selectionArgs = {value, account.name}; + + return executeQuery(ProviderTableMeta.CONTENT_URI_SHARE, null, - key + AND - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, - null - ); - } else { - try { - c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI_SHARE, - null, - key + AND + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?", - new String[]{value, account.name}, - null - ); - } catch (RemoteException e) { - Log_OC.w(TAG, "Could not get details, assuming share does not exist: " + e.getMessage()); - c = null; - } - } - return c; + selection, + selectionArgs, + null, + "Could not get details, assuming share does not exist: " + ); } /** * Get first share bound to a file with a known path and given {@link ShareType}. * - * @param path Path of the file. - * @param type Type of the share to get - * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK} + * @param path Path of the file. + * @param type Type of the share to get + * @param shareWith Target of the share. Ignored in type is {@link ShareType#PUBLIC_LINK} * @return First {@link OCShare} instance found in DB bound to the file in 'path' */ public OCShare getFirstShareByPathAndType(String path, ShareType type, String shareWith) { @@ -1216,47 +1201,36 @@ public OCShare getFirstShareByPathAndType(String path, ShareType type, String sh } String selection = ProviderTableMeta.OCSHARES_PATH + AND - + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; + + ProviderTableMeta.OCSHARES_SHARE_TYPE + AND + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; if (!ShareType.PUBLIC_LINK.equals(type)) { - selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + "=?"; + selection += " AND " + ProviderTableMeta.OCSHARES_SHARE_WITH + " = ?"; } String[] selectionArgs; if (ShareType.PUBLIC_LINK.equals(type)) { selectionArgs = new String[]{ - path, - Integer.toString(type.getValue()), - account.name + path, + Integer.toString(type.getValue()), + account.name }; } else { selectionArgs = new String[]{ - path, - Integer.toString(type.getValue()), - account.name, - shareWith + path, + Integer.toString(type.getValue()), + account.name, + shareWith }; } - if (getContentResolver() != null) { - cursor = getContentResolver().query( - ProviderTableMeta.CONTENT_URI_SHARE, - null, - selection, selectionArgs, - null); - } else { - try { - cursor = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI_SHARE, - null, - selection, selectionArgs, - null); + cursor = executeQuery( + ProviderTableMeta.CONTENT_URI_SHARE, + null, + selection, + selectionArgs, + null, + "Could not get file details: "); - } catch (RemoteException e) { - Log_OC.e(TAG, "Could not get file details: " + e.getMessage(), e); - cursor = null; - } - } OCShare share = null; if (cursor != null) { @@ -1268,64 +1242,40 @@ public OCShare getFirstShareByPathAndType(String path, ShareType type, String sh return share; } - private OCShare createShareInstance(Cursor c) { - OCShare share = null; - if (c != null) { - share = new OCShare(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_PATH))); - share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID))); - share.setFileSource(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE))); - share.setShareType(ShareType.fromValue(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE)))); - share.setShareWith(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH))); - share.setPermissions(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS))); - share.setSharedDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE))); - share.setExpirationDate(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE))); - share.setToken(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN))); - share.setSharedWithDisplayName( - c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME))); - share.setFolder(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1); - share.setUserId(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID))); - share.setRemoteId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))); - share.setPasswordProtected(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED)) == 1); - share.setNote(c.getString(c.getColumnIndex(ProviderTableMeta.OCSHARES_NOTE))); - share.setHideFileDownload(c.getInt(c.getColumnIndex(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD)) == 1); - } - return share; - } - - private void resetShareFlagsInAllFiles() { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; - String[] whereArgs = new String[]{account.name}; + private OCShare createShareInstance(Cursor cursor) { + OCShare share = new OCShare(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_PATH))); + share.setId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); + share.setFileSource(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE))); + share.setShareType(ShareType.fromValue(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE)))); + share.setShareWith(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH))); + share.setPermissions(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS))); + share.setSharedDate(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE))); + share.setExpirationDate(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE))); + share.setToken(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN))); + share.setSharedWithDisplayName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME))); + share.setFolder(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1); + share.setUserId(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID))); + share.setRemoteId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED))); + share.setPasswordProtected(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED)) == 1); + share.setNote(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_NOTE))); + share.setHideFileDownload(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD)) == 1); - if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); - - } else { - try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in resetShareFlagsInAllFiles" + e.getMessage(), e); - } - } + return share; } private void resetShareFlagsInFolder(OCFile folder) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + "=?"; + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + " = ?"; String[] whereArgs = new String[]{account.name, String.valueOf(folder.getFileId())}; if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); - + getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); + getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e); } @@ -1333,19 +1283,18 @@ private void resetShareFlagsInFolder(OCFile folder) { } private void resetShareFlagInAFile(String filePath) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, Boolean.FALSE); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, Boolean.FALSE); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ""); + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[]{account.name, filePath}; if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); - + getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs); + getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e); } @@ -1353,15 +1302,15 @@ private void resetShareFlagInAFile(String filePath) { } private void cleanShares() { - String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; + Uri contentUriShare = ProviderTableMeta.CONTENT_URI_SHARE; + String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; String[] whereArgs = new String[]{account.name}; if (getContentResolver() != null) { - getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs); - + getContentResolver().delete(contentUriShare, where, whereArgs); } else { try { - getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs); + getContentProviderClient().delete(contentUriShare, where, whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e); } @@ -1370,83 +1319,75 @@ private void cleanShares() { public void saveShares(Collection shares) { cleanShares(); - if (shares != null) { - ArrayList operations = - new ArrayList(shares.size()); + ArrayList operations = new ArrayList<>(shares.size()); - // prepare operations to insert or update files to save in the given folder - for (OCShare share : shares) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); - cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); - cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); - cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); - cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); - cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); - cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); - cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); - cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); - cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); - cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); - cv.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); - cv.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); - - if (shareExistsForRemoteId(share.getRemoteId())) { - // updating an existing file - operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE). - withValues(cv). - withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?", - new String[]{String.valueOf(share.getRemoteId())}) - .build()); - } else { - // adding a new file - operations.add( - ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE). - withValues(cv). - build() - ); - } + // prepare operations to insert or update files to save in the given folder + for (OCShare share : shares) { + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); + contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); + contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); + contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); + contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); + contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); + contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); + contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); + contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); + contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); + + if (shareExistsForRemoteId(share.getRemoteId())) { + // updating an existing file + operations.add( + ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE) + .withValues(contentValues) + .withSelection(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " = ?", + new String[]{String.valueOf(share.getRemoteId())}) + .build()); + } else { + // adding a new file + operations.add( + ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE) + .withValues(contentValues) + .build() + ); } + } - // apply operations in batch - if (operations.size() > 0) { - @SuppressWarnings("unused") - ContentProviderResult[] results = null; - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - results = getContentResolver().applyBatch(MainApp.getAuthority(), - operations); - } else { - results = getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); + // apply operations in batch + if (operations.size() > 0) { + Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); + try { + if (getContentResolver() != null) { + getContentResolver().applyBatch(MainApp.getAuthority(), operations); + } else { + getContentProviderClient().applyBatch(operations); } + } catch (OperationApplicationException | RemoteException e) { + Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); } } - } public void removeShare(OCShare share) { - Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE; + Uri contentUriShare = ProviderTableMeta.CONTENT_URI_SHARE; String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + - ProviderTableMeta._ID + "=?"; - String[] whereArgs = new String[]{account.name, Long.toString(share.getId())}; + ProviderTableMeta._ID + " = ?"; + String[] whereArgs = {account.name, Long.toString(share.getId())}; if (getContentProviderClient() != null) { try { - getContentProviderClient().delete(share_uri, where, whereArgs); + getContentProviderClient().delete(contentUriShare, where, whereArgs); } catch (RemoteException e) { Log_OC.d(TAG, e.getMessage(), e); } } else { - getContentResolver().delete(share_uri, where, whereArgs); + getContentResolver().delete(contentUriShare, where, whereArgs); } } @@ -1472,7 +1413,6 @@ public void saveSharesDB(List shares) { try { if (getContentResolver() != null) { getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { getContentProviderClient().applyBatch(operations); } @@ -1485,10 +1425,9 @@ public void saveSharesDB(List shares) { public void removeSharesForFile(String remotePath) { resetShareFlagInAFile(remotePath); - ArrayList operations = new ArrayList<>(); - operations = prepareRemoveSharesInFile(remotePath, operations); + ArrayList operations = prepareRemoveSharesInFile(remotePath, new ArrayList<>()); // apply operations in batch - if (operations.size() > 0) { + if (!operations.isEmpty()) { Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); try { if (getContentResolver() != null) { @@ -1510,10 +1449,8 @@ public void saveSharesInFolder(ArrayList shares, OCFile folder) { ArrayList operations = new ArrayList<>(); operations = prepareRemoveSharesInFolder(folder, operations); - if (shares != null) { - // prepare operations to insert or update files to save in the given folder - operations = prepareInsertShares(shares, operations); - } + // prepare operations to insert or update files to save in the given folder + operations = prepareInsertShares(shares, operations); // apply operations in batch if (operations.size() > 0) { @@ -1521,9 +1458,7 @@ public void saveSharesInFolder(ArrayList shares, OCFile folder) { try { if (getContentResolver() != null) { getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); } @@ -1536,43 +1471,43 @@ public void saveSharesInFolder(ArrayList shares, OCFile folder) { /** * Prepare operations to insert or update files to save in the given folder - * @param shares List of shares to insert - * @param operations List of operations + * + * @param shares List of shares to insert + * @param operations List of operations * @return */ private ArrayList prepareInsertShares( List shares, ArrayList operations) { - if (shares != null) { - ContentValues contentValues; - // prepare operations to insert or update files to save in the given folder - for (OCShare share : shares) { - contentValues = new ContentValues(); - contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); - contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); - contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); - contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); - contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); - contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); - contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); - contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); - contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); - contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); - contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); - contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); - contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); - contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); - contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); - contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); - contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); - - // adding a new share resource - operations.add(ContentProviderOperation - .newInsert(ProviderTableMeta.CONTENT_URI_SHARE) - .withValues(contentValues) - .build()); - } + ContentValues contentValues; + // prepare operations to insert or update files to save in the given folder + for (OCShare share : shares) { + contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource()); + contentValues.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith()); + contentValues.put(ProviderTableMeta.OCSHARES_PATH, share.getPath()); + contentValues.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate()); + contentValues.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate()); + contentValues.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken()); + contentValues.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName()); + contentValues.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId()); + contentValues.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getRemoteId()); + contentValues.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.OCSHARES_IS_PASSWORD_PROTECTED, share.isPasswordProtected() ? 1 : 0); + contentValues.put(ProviderTableMeta.OCSHARES_NOTE, share.getNote()); + contentValues.put(ProviderTableMeta.OCSHARES_HIDE_DOWNLOAD, share.isHideFileDownload()); + + // adding a new share resource + operations.add(ContentProviderOperation + .newInsert(ProviderTableMeta.CONTENT_URI_SHARE) + .withValues(contentValues) + .build()); } + return operations; } @@ -1625,16 +1560,17 @@ private ArrayList prepareRemoveSharesInFolder( } private ArrayList prepareRemoveSharesInFile( - String filePath, ArrayList preparedOperations) { + String filePath, ArrayList preparedOperations) { String where = ProviderTableMeta.OCSHARES_PATH + AND - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?"; + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; String[] whereArgs = new String[]{filePath, account.name}; preparedOperations.add( - ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE). - withSelection(where, whereArgs). - build() + ContentProviderOperation + .newDelete(ProviderTableMeta.CONTENT_URI_SHARE) + .withSelection(where, whereArgs) + .build() ); return preparedOperations; @@ -1642,34 +1578,27 @@ private ArrayList prepareRemoveSharesInFile( } public List getSharesWithForAFile(String filePath, String accountName) { - // Condition - String where = ProviderTableMeta.OCSHARES_PATH + AND - + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND - + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR " - + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR " - + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR " - + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? OR " - + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? ) "; - String[] whereArgs = new String[]{filePath, accountName, - Integer.toString(ShareType.USER.getValue()), - Integer.toString(ShareType.GROUP.getValue()), - Integer.toString(ShareType.EMAIL.getValue()), - Integer.toString(ShareType.FEDERATED.getValue()), - Integer.toString(ShareType.ROOM.getValue())}; - - Cursor cursor = null; - if (getContentResolver() != null) { - cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_SHARE, null, where, whereArgs, null); - } else { - try { - cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_SHARE, null, where, - whereArgs, null); + String selection = ProviderTableMeta.OCSHARES_PATH + AND + + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + + " (" + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR " + + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR " + + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR " + + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? OR " + + ProviderTableMeta.OCSHARES_SHARE_TYPE + " = ? ) "; + String[] selectionArgs = new String[]{filePath, accountName, + Integer.toString(ShareType.USER.getValue()), + Integer.toString(ShareType.GROUP.getValue()), + Integer.toString(ShareType.EMAIL.getValue()), + Integer.toString(ShareType.FEDERATED.getValue()), + Integer.toString(ShareType.ROOM.getValue())}; + + Cursor cursor = executeQuery(ProviderTableMeta.CONTENT_URI_SHARE, + null, + selection, + selectionArgs, + null, + "Could not get list of shares with: "); - } catch (RemoteException e) { - Log_OC.e(TAG, "Could not get list of shares with: " + e.getMessage(), e); - cursor = null; - } - } ArrayList shares = new ArrayList<>(); OCShare share; if (cursor != null) { @@ -1695,23 +1624,23 @@ public static void triggerMediaScan(String path) { } public void deleteFileInMediaScan(String path) { - String mimetypeString = FileStorageUtils.getMimeTypeFromName(path); ContentResolver contentResolver = getContentResolver(); + String[] selectionArgs = {path}; if (contentResolver != null) { if (MimeTypeUtil.isImage(mimetypeString)) { // Images contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - MediaStore.Images.Media.DATA + "=?", new String[]{path}); + MediaStore.Images.Media.DATA + " = ?", selectionArgs); } else if (MimeTypeUtil.isAudio(mimetypeString)) { // Audio contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - MediaStore.Audio.Media.DATA + "=?", new String[]{path}); + MediaStore.Audio.Media.DATA + " = ?", selectionArgs); } else if (MimeTypeUtil.isVideo(mimetypeString)) { // Video contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, - MediaStore.Video.Media.DATA + "=?", new String[]{path}); + MediaStore.Video.Media.DATA + " = ?", selectionArgs); } } else { ContentProviderClient contentProviderClient = getContentProviderClient(); @@ -1719,15 +1648,15 @@ public void deleteFileInMediaScan(String path) { if (MimeTypeUtil.isImage(mimetypeString)) { // Images contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - MediaStore.Images.Media.DATA + "=?", new String[]{path}); + MediaStore.Images.Media.DATA + " = ?", selectionArgs); } else if (MimeTypeUtil.isAudio(mimetypeString)) { // Audio contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - MediaStore.Audio.Media.DATA + "=?", new String[]{path}); + MediaStore.Audio.Media.DATA + " = ?", selectionArgs); } else if (MimeTypeUtil.isVideo(mimetypeString)) { // Video contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, - MediaStore.Video.Media.DATA + "=?", new String[]{path}); + MediaStore.Video.Media.DATA + " = ?", selectionArgs); } } catch (RemoteException e) { Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e); @@ -1739,24 +1668,18 @@ public void saveConflict(OCFile file, String etagInConflict) { if (!file.isDown()) { etagInConflict = null; } - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict); + + Uri contentUriFile = ProviderTableMeta.CONTENT_URI_FILE; + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict); + String where = ProviderTableMeta._ID + " = ?"; + String[] selectionArgs = {String.valueOf(file.getFileId())}; int updated = 0; if (getContentResolver() != null) { - updated = getContentResolver().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())} - ); + updated = getContentResolver().update(contentUriFile, contentValues, where, selectionArgs); } else { try { - updated = getContentProviderClient().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())} - ); + updated = getContentProviderClient().update(contentUriFile, contentValues, where, selectionArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); } @@ -1776,27 +1699,28 @@ public void saveConflict(OCFile file, String etagInConflict) { } if (ancestorIds.size() > 0) { - final StringBuffer whereBuffer = new StringBuffer(); - whereBuffer.append(ProviderTableMeta._ID).append(" IN ("); + //TODO bug if ancestorIds.size() > 1000 + final StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(ProviderTableMeta._ID).append(" IN ("); for (int i = 0; i < ancestorIds.size() - 1; i++) { - whereBuffer.append("?,"); + stringBuilder.append("?, "); } - whereBuffer.append("?)"); + stringBuilder.append("?)"); if (getContentResolver() != null) { - updated = getContentResolver().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - whereBuffer.toString(), - ancestorIds.toArray(new String[]{}) + getContentResolver().update( + contentUriFile, + contentValues, + stringBuilder.toString(), + ancestorIds.toArray(new String[]{}) ); } else { try { - updated = getContentProviderClient().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - whereBuffer.toString(), - ancestorIds.toArray(new String[]{}) + getContentProviderClient().update( + contentUriFile, + contentValues, + stringBuilder.toString(), + ancestorIds.toArray(new String[]{}) ); } catch (RemoteException e) { Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); @@ -1814,54 +1738,44 @@ public void saveConflict(OCFile file, String etagInConflict) { parentPath = parentPath.substring(0, parentPath.lastIndexOf(OCFile.PATH_SEPARATOR) + 1); Log_OC.d(TAG, "checking parents to remove conflict; STARTING with " + parentPath); - String[] projection = new String[]{ProviderTableMeta._ID}; while (parentPath.length() > 0) { + String[] projection = {ProviderTableMeta._ID}; String whereForDescencentsInConflict = - ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " + - ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " + - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + - ProviderTableMeta.FILE_PATH + " LIKE ?"; - Cursor descendentsInConflict = null; - if (getContentResolver() != null) { - descendentsInConflict = getContentResolver().query( - ProviderTableMeta.CONTENT_URI_FILE, - projection, - whereForDescencentsInConflict, - new String[]{account.name, parentPath + "%"}, - null - ); - } else { - try { - descendentsInConflict = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI_FILE, - projection, - whereForDescencentsInConflict, - new String[]{account.name, parentPath + "%"}, - null - ); - } catch (RemoteException e) { - Log_OC.e(TAG, "Failed querying for descendents in conflict " + e.getMessage(), e); - } - } + ProviderTableMeta.FILE_ETAG_IN_CONFLICT + " IS NOT NULL AND " + + ProviderTableMeta.FILE_CONTENT_TYPE + " != 'DIR' AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + + ProviderTableMeta.FILE_PATH + " LIKE ?"; + selectionArgs = new String[]{account.name, parentPath + "%"}; + + Cursor descendentsInConflict = executeQuery( + contentUriFile, + projection, + whereForDescencentsInConflict, + selectionArgs, + null, + "Failed querying for descendents in conflict " + ); + if (descendentsInConflict == null || descendentsInConflict.getCount() == 0) { Log_OC.d(TAG, "NO MORE conflicts in " + parentPath); + where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + + ProviderTableMeta.FILE_PATH + " = ?"; + selectionArgs = new String[]{account.name, parentPath}; if (getContentResolver() != null) { - updated = getContentResolver().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + - ProviderTableMeta.FILE_PATH + "=?", - new String[]{account.name, parentPath} + getContentResolver().update( + contentUriFile, + contentValues, + where, + selectionArgs ); } else { try { - updated = getContentProviderClient().update( - ProviderTableMeta.CONTENT_URI_FILE, - cv, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + - ProviderTableMeta.FILE_PATH + "=?" - , new String[]{account.name, parentPath} + getContentProviderClient().update( + contentUriFile, + contentValues, + where, + selectionArgs ); } catch (RemoteException e) { Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); @@ -1885,332 +1799,307 @@ public void saveConflict(OCFile file, String etagInConflict) { } - public OCCapability saveCapabilities(OCCapability capability) { - - // Prepare capabilities data - ContentValues cv = createContentValues(account.name, capability); + public void saveCapabilities(OCCapability capability) { + Uri contentUriCapabilities = ProviderTableMeta.CONTENT_URI_CAPABILITIES; + ContentValues contentValues = createContentValues(account.name, capability); if (capabilityExists(account.name)) { + String where = ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + " = ?"; + String[] selectionArgs = {account.name}; if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv, - ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?", - new String[]{account.name}); + getContentResolver().update(contentUriCapabilities, contentValues, where, selectionArgs); } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_CAPABILITIES, - cv, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=?", - new String[]{account.name}); + getContentProviderClient().update(contentUriCapabilities, contentValues, where, selectionArgs); } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + Log_OC.e(TAG, "Failed saveCapabilities update" + e.getMessage(), e); } } } else { - Uri result_uri = null; + Uri resultUri = null; if (getContentResolver() != null) { - result_uri = getContentResolver().insert( - ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv); + resultUri = getContentResolver().insert(contentUriCapabilities, contentValues); } else { try { - result_uri = getContentProviderClient().insert( - ProviderTableMeta.CONTENT_URI_CAPABILITIES, cv); + resultUri = getContentProviderClient().insert(contentUriCapabilities, contentValues); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } } - if (result_uri != null) { - long new_id = Long.parseLong(result_uri.getPathSegments() - .get(1)); - capability.setId(new_id); + + if (resultUri != null) { + long newId = Long.parseLong(resultUri.getPathSegments().get(1)); + capability.setId(newId); capability.setAccountName(account.name); } } - - return capability; } @NonNull private ContentValues createContentValues(String accountName, OCCapability capability) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, accountName); - cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, capability.getVersionMayor()); - cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, capability.getVersionMinor()); - cv.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, capability.getVersionMicro()); - cv.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, capability.getVersionString()); - cv.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, capability.getVersionEdition()); - cv.put(ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT, capability.getExtendedSupport().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, capability.getCorePollInterval()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, capability.getFilesSharingApiEnabled().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED, - capability.getFilesSharingPublicEnabled().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED, - capability.getFilesSharingPublicPasswordEnforced().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD, - capability.getFilesSharingPublicAskForOptionalPassword().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED, - capability.getFilesSharingPublicExpireDateEnabled().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS, - capability.getFilesSharingPublicExpireDateDays()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED, - capability.getFilesSharingPublicExpireDateEnforced().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL, - capability.getFilesSharingPublicSendMail().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD, - capability.getFilesSharingPublicUpload().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL, - capability.getFilesSharingUserSendMail().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, capability.getFilesSharingResharing().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING, - capability.getFilesSharingFederationOutgoing().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING, - capability.getFilesSharingFederationIncoming().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, capability.getFilesBigFileChunking().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, capability.getFilesUndelete().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, capability.getFilesVersioning().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, capability.getFilesFileDrop().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS, capability.getExternalLinks().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_NAME, capability.getServerName()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_COLOR, capability.getServerColor()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR, capability.getServerTextColor()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR, capability.getServerElementColor()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL, capability.getServerBackground()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN, capability.getServerSlogan()); - cv.put(ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION, capability.getEndToEndEncryption().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT, capability.getServerBackgroundDefault() - .getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN, capability.getServerBackgroundPlain() - .getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_ACTIVITY, capability.getActivity().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT, capability.getRichDocuments().getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST, - TextUtils.join(",", capability.getRichDocumentsMimeTypeList())); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST, - TextUtils.join(",", capability.getRichDocumentsOptionalMimeTypeList())); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING, capability.getRichDocumentsDirectEditing() - .getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES, capability.getRichDocumentsTemplatesAvailable() - .getValue()); - cv.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME, capability.getRichDocumentsProductName()); - cv.put(ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG, capability.getDirectEditingEtag()); - - return cv; + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME, + accountName); + contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MAYOR, + capability.getVersionMayor()); + contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MINOR, + capability.getVersionMinor()); + contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_MICRO, + capability.getVersionMicro()); + contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_STRING, + capability.getVersionString()); + contentValues.put(ProviderTableMeta.CAPABILITIES_VERSION_EDITION, + capability.getVersionEdition()); + contentValues.put(ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT, + capability.getExtendedSupport().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL, + capability.getCorePollInterval()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED, + capability.getFilesSharingApiEnabled().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED, + capability.getFilesSharingPublicEnabled().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED, + capability.getFilesSharingPublicPasswordEnforced().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD, + capability.getFilesSharingPublicAskForOptionalPassword().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED, + capability.getFilesSharingPublicExpireDateEnabled().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS, + capability.getFilesSharingPublicExpireDateDays()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED, + capability.getFilesSharingPublicExpireDateEnforced().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL, + capability.getFilesSharingPublicSendMail().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD, + capability.getFilesSharingPublicUpload().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL, + capability.getFilesSharingUserSendMail().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_RESHARING, + capability.getFilesSharingResharing().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING, + capability.getFilesSharingFederationOutgoing().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING, + capability.getFilesSharingFederationIncoming().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING, + capability.getFilesBigFileChunking().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_UNDELETE, + capability.getFilesUndelete().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_VERSIONING, + capability.getFilesVersioning().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_FILES_DROP, + capability.getFilesFileDrop().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS, + capability.getExternalLinks().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_NAME, + capability.getServerName()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_COLOR, + capability.getServerColor()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR, + capability.getServerTextColor()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR, + capability.getServerElementColor()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL, + capability.getServerBackground()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN, + capability.getServerSlogan()); + contentValues.put(ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION, + capability.getEndToEndEncryption().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT, + capability.getServerBackgroundDefault().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN, + capability.getServerBackgroundPlain().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_ACTIVITY, + capability.getActivity().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT, + capability.getRichDocuments().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST, + TextUtils.join(",", capability.getRichDocumentsMimeTypeList())); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST, + TextUtils.join(",", capability.getRichDocumentsOptionalMimeTypeList())); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING, + capability.getRichDocumentsDirectEditing().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES, + capability.getRichDocumentsTemplatesAvailable().getValue()); + contentValues.put(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME, + capability.getRichDocumentsProductName()); + contentValues.put(ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG, + capability.getDirectEditingEtag()); + + return contentValues; } private boolean capabilityExists(String accountName) { - Cursor c = getCapabilityCursorForAccount(accountName); + Cursor cursor = getCapabilityCursorForAccount(accountName); boolean exists = false; - if (c != null) { - exists = c.moveToFirst(); - c.close(); + + if (cursor != null) { + exists = cursor.moveToFirst(); + cursor.close(); } + return exists; } private Cursor getCapabilityCursorForAccount(String accountName) { - Cursor c = null; - if (getContentResolver() != null) { - c = getContentResolver() - .query(ProviderTableMeta.CONTENT_URI_CAPABILITIES, - null, - ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ", - new String[]{accountName}, null); - } else { - try { - c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI_CAPABILITIES, - null, - ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + "=? ", - new String[]{accountName}, null); - } catch (RemoteException e) { - Log_OC.e(TAG, "Couldn't determine capability existence, assuming non existance: " + e.getMessage(), e); - } - } + String selection = ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + " = ?"; + String[] selectionArgs = {accountName}; - return c; + return executeQuery(ProviderTableMeta.CONTENT_URI_CAPABILITIES, + selection, + selectionArgs, + "Couldn't determine capability existence, assuming non existance: "); } @NonNull public OCCapability getCapability(String accountName) { OCCapability capability; - Cursor c = getCapabilityCursorForAccount(accountName); + Cursor cursor = getCapabilityCursorForAccount(accountName); - if (c.moveToFirst()) { - capability = createCapabilityInstance(c); + if (cursor.moveToFirst()) { + capability = createCapabilityInstance(cursor); } else { capability = new OCCapability(); // return default with all UNKNOWN } - c.close(); + cursor.close(); + return capability; } - private OCCapability createCapabilityInstance(Cursor c) { + private OCCapability createCapabilityInstance(Cursor cursor) { OCCapability capability = null; - if (c != null) { + if (cursor != null) { capability = new OCCapability(); - capability.setId(getLong(c, ProviderTableMeta._ID)); - capability.setAccountName(getString(c, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)); - capability.setVersionMayor(getInt(c, ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)); - capability.setVersionMinor(getInt(c, ProviderTableMeta.CAPABILITIES_VERSION_MINOR)); - capability.setVersionMicro(getInt(c, ProviderTableMeta.CAPABILITIES_VERSION_MICRO)); - capability.setVersionString(getString(c, ProviderTableMeta.CAPABILITIES_VERSION_STRING)); - capability.setVersionEdition(getString(c, ProviderTableMeta.CAPABILITIES_VERSION_EDITION)); - capability.setExtendedSupport(getBoolean(c, ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT)); - capability.setCorePollInterval(getInt(c, ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)); - capability.setFilesSharingApiEnabled(getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED)); + capability.setId(getLong(cursor, ProviderTableMeta._ID)); + capability.setAccountName(getString(cursor, ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME)); + capability.setVersionMayor(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MAYOR)); + capability.setVersionMinor(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MINOR)); + capability.setVersionMicro(getInt(cursor, ProviderTableMeta.CAPABILITIES_VERSION_MICRO)); + capability.setVersionString(getString(cursor, ProviderTableMeta.CAPABILITIES_VERSION_STRING)); + capability.setVersionEdition(getString(cursor, ProviderTableMeta.CAPABILITIES_VERSION_EDITION)); + capability.setExtendedSupport(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_EXTENDED_SUPPORT)); + capability.setCorePollInterval(getInt(cursor, ProviderTableMeta.CAPABILITIES_CORE_POLLINTERVAL)); + capability.setFilesSharingApiEnabled(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_API_ENABLED)); capability.setFilesSharingPublicEnabled( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ENABLED)); capability.setFilesSharingPublicPasswordEnforced( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_PASSWORD_ENFORCED)); capability.setFilesSharingPublicAskForOptionalPassword( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_ASK_FOR_OPTIONAL_PASSWORD)); capability.setFilesSharingPublicExpireDateEnabled( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENABLED)); capability.setFilesSharingPublicExpireDateDays( - getInt(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)); + getInt(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_DAYS)); capability.setFilesSharingPublicExpireDateEnforced( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_EXPIRE_DATE_ENFORCED)); capability.setFilesSharingPublicSendMail( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL)); - capability.setFilesSharingPublicUpload(getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD)); - capability.setFilesSharingUserSendMail(getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL)); - capability.setFilesSharingResharing(getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_RESHARING)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_SEND_MAIL)); + capability.setFilesSharingPublicUpload(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_PUBLIC_UPLOAD)); + capability.setFilesSharingUserSendMail(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_USER_SEND_MAIL)); + capability.setFilesSharingResharing(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_RESHARING)); capability.setFilesSharingFederationOutgoing( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_OUTGOING)); capability.setFilesSharingFederationIncoming( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING)); - capability.setFilesBigFileChunking(getBoolean(c, ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING)); - capability.setFilesUndelete(getBoolean(c, ProviderTableMeta.CAPABILITIES_FILES_UNDELETE)); - capability.setFilesVersioning(getBoolean(c, ProviderTableMeta.CAPABILITIES_FILES_VERSIONING)); - capability.setFilesFileDrop(getBoolean(c, ProviderTableMeta.CAPABILITIES_FILES_DROP)); - capability.setExternalLinks(getBoolean(c, ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS)); - capability.setServerName(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_NAME)); - capability.setServerColor(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_COLOR)); - capability.setServerTextColor(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR)); - capability.setServerElementColor(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR)); - capability.setServerBackground(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL)); - capability.setServerSlogan(getString(c, ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN)); - capability.setEndToEndEncryption(getBoolean(c, ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SHARING_FEDERATION_INCOMING)); + capability.setFilesBigFileChunking(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_BIGFILECHUNKING)); + capability.setFilesUndelete(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_UNDELETE)); + capability.setFilesVersioning(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_VERSIONING)); + capability.setFilesFileDrop(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_FILES_DROP)); + capability.setExternalLinks(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_EXTERNAL_LINKS)); + capability.setServerName(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_NAME)); + capability.setServerColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_COLOR)); + capability.setServerTextColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_TEXT_COLOR)); + capability.setServerElementColor(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_ELEMENT_COLOR)); + capability.setServerBackground(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_URL)); + capability.setServerSlogan(getString(cursor, ProviderTableMeta.CAPABILITIES_SERVER_SLOGAN)); + capability.setEndToEndEncryption(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_END_TO_END_ENCRYPTION)); capability.setServerBackgroundDefault( - getBoolean(c, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT)); - capability.setServerBackgroundPlain(getBoolean(c, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN)); - capability.setActivity(getBoolean(c, ProviderTableMeta.CAPABILITIES_ACTIVITY)); - capability.setRichDocuments(getBoolean(c, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_DEFAULT)); + capability.setServerBackgroundPlain(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_SERVER_BACKGROUND_PLAIN)); + capability.setActivity(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_ACTIVITY)); + capability.setRichDocuments(getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT)); capability.setRichDocumentsDirectEditing( - getBoolean(c, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_DIRECT_EDITING)); capability.setRichDocumentsTemplatesAvailable( - getBoolean(c, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES)); - String mimetypes = c.getString(c.getColumnIndex(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST)); + getBoolean(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_TEMPLATES)); + String mimetypes = cursor.getString(cursor.getColumnIndex(ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_MIMETYPE_LIST)); if (mimetypes == null) { mimetypes = ""; } capability.setRichDocumentsMimeTypeList(Arrays.asList(mimetypes.split(","))); - String optionalMimetypes = getString(c, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST); + String optionalMimetypes = getString(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_OPTIONAL_MIMETYPE_LIST); if (optionalMimetypes == null) { optionalMimetypes = ""; } capability.setRichDocumentsOptionalMimeTypeList(Arrays.asList(optionalMimetypes.split(","))); - capability.setRichDocumentsProductName(getString(c, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME)); - capability.setDirectEditingEtag(getString(c, ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG)); + capability.setRichDocumentsProductName(getString(cursor, ProviderTableMeta.CAPABILITIES_RICHDOCUMENT_PRODUCT_NAME)); + capability.setDirectEditingEtag(getString(cursor, ProviderTableMeta.CAPABILITIES_DIRECT_EDITING_ETAG)); } return capability; } public void deleteVirtuals(VirtualFolderType type) { - if (getContentResolver() != null) { - getContentResolver().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL, - ProviderTableMeta.VIRTUAL_TYPE + "=?", new String[]{String.valueOf(type)}); - } else { - try { - getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_VIRTUAL, - ProviderTableMeta.VIRTUAL_TYPE + "=?", new String[]{String.valueOf(type)}); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } - } - - public void saveVirtuals(VirtualFolderType type, List values) { + Uri contentUriVirtual = ProviderTableMeta.CONTENT_URI_VIRTUAL; + String where = ProviderTableMeta.VIRTUAL_TYPE + " = ?"; + String[] selectionArgs = {String.valueOf(type)}; if (getContentResolver() != null) { - getContentResolver().bulkInsert(ProviderTableMeta.CONTENT_URI_VIRTUAL, values.toArray(new ContentValues[0])); + getContentResolver().delete(contentUriVirtual, where, selectionArgs); } else { try { - getContentProviderClient().bulkInsert(ProviderTableMeta.CONTENT_URI_VIRTUAL, values.toArray(new ContentValues[0])); + getContentProviderClient().delete(contentUriVirtual, where, selectionArgs); } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + Log_OC.e(TAG, "deleteVirtuals" + e.getMessage(), e); } } } - public void saveVirtual(VirtualFolderType type, OCFile file) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.VIRTUAL_TYPE, type.toString()); - cv.put(ProviderTableMeta.VIRTUAL_OCFILE_ID, file.getFileId()); + public void saveVirtuals(List values) { + Uri contentUriVirtual = ProviderTableMeta.CONTENT_URI_VIRTUAL; + ContentValues[] arrayValues = values.toArray(new ContentValues[0]); if (getContentResolver() != null) { - getContentResolver().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv); + getContentResolver().bulkInsert(contentUriVirtual, arrayValues); } else { try { - getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_VIRTUAL, cv); + getContentProviderClient().bulkInsert(contentUriVirtual, arrayValues); } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + Log_OC.e(TAG, "saveVirtuals" + e.getMessage(), e); } } } public List getVirtualFolderContent(VirtualFolderType type, boolean onlyImages) { List ocFiles = new ArrayList<>(); - Uri req_uri = ProviderTableMeta.CONTENT_URI_VIRTUAL; - Cursor c; - if (getContentProviderClient() != null) { - try { - c = getContentProviderClient().query( - req_uri, - null, - ProviderTableMeta.VIRTUAL_TYPE + "=?", - new String[]{String.valueOf(type)}, - null - ); - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage(), e); - return ocFiles; - } - } else { - c = getContentResolver().query( - req_uri, - null, - ProviderTableMeta.VIRTUAL_TYPE + "=?", - new String[]{String.valueOf(type)}, - null - ); - } + String selection = ProviderTableMeta.VIRTUAL_TYPE + " = ?"; + String[] selectionArgs = {String.valueOf(type)}; - if (c != null) { - if (c.moveToFirst()) { + Cursor cursor = executeQuery(ProviderTableMeta.CONTENT_URI_VIRTUAL, + selection, + selectionArgs, + "getVirtualFolderContent "); + + if (cursor != null) { + if (cursor.moveToFirst()) { do { - OCFile child = createFileInstanceFromVirtual(c); + OCFile ocFile = createFileInstanceFromVirtual(cursor); - if (child != null) { - ocFiles.add(child); + if (ocFile != null) { + if (onlyImages) { + if (MimeTypeUtil.isImage(ocFile)) { + ocFiles.add(ocFile); + } + } else { + ocFiles.add(ocFile); + } } - } while (c.moveToNext()); - } - c.close(); - } - - if (onlyImages) { - List temp = new ArrayList<>(); - - for (OCFile file : ocFiles) { - if (MimeTypeUtil.isImage(file)) { - temp.add(file); - } + } while (cursor.moveToNext()); } - ocFiles = temp; + cursor.close(); } - if (ocFiles.size() > 0) { + if (!ocFiles.isEmpty()) { Collections.sort(ocFiles); } @@ -2218,15 +2107,16 @@ public List getVirtualFolderContent(VirtualFolderType type, boolean only } public void deleteAllFiles() { - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "= ? AND " + - ProviderTableMeta.FILE_PATH + "= ?"; + Uri contentUriDir = ProviderTableMeta.CONTENT_URI_DIR; + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + + ProviderTableMeta.FILE_PATH + "= ?"; String[] whereArgs = new String[]{account.name, OCFile.ROOT_PATH}; if (getContentResolver() != null) { - getContentResolver().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs); + getContentResolver().delete(contentUriDir, where, whereArgs); } else { try { - getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_DIR, where, whereArgs); + getContentProviderClient().delete(contentUriDir, where, whereArgs); } catch (RemoteException e) { Log_OC.e(TAG, "Exception in deleteAllFiles for account " + account.name + ": " + e.getMessage(), e); } diff --git a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index 6201e281a3d8..da17b3c0fffe 100644 --- a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -449,6 +449,10 @@ private void synchronizeData(List folderAndFiles) { // retrieve local data for the read file localFile = localFilesMap.remove(remoteFile.getRemotePath()); + if (localFile == null) { + localFile = mStorageManager.getFileByPath(updatedFile.getRemotePath()); + } + // add to updatedFile data about LOCAL STATE (not existing in server) updatedFile.setLastSyncDateForProperties(mCurrentSyncTime); diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index aeba0fb9292a..1d2d2c02d394 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -631,6 +631,30 @@ private int update(SQLiteDatabase db, Uri uri, ContentValues values, String sele } } + @Override + public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) { + if (isCallerNotAllowed(uri)) { + return 0; + } + + if (mUriMatcher.match(uri) == VIRTUAL) { + SQLiteDatabase database = mDbHelper.getWritableDatabase(); + database.beginTransaction(); + int contentInsert; + try { + for (ContentValues contentValues : values) { + insert(database, uri, contentValues); + } + database.setTransactionSuccessful(); + contentInsert = values.length; + } finally { + database.endTransaction(); + } + return contentInsert; + } + return super.bulkInsert(uri, values); + } + @NonNull @Override public ContentProviderResult[] applyBatch(@NonNull ArrayList operations) diff --git a/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java b/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java index 293bbf17a570..6fc89a0c5891 100644 --- a/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java +++ b/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java @@ -881,6 +881,7 @@ private void parseShares(List objects) { } } } + mStorageManager.saveShares(shares); } @@ -953,7 +954,7 @@ private void parseVirtuals(List objects, ExtendedListFragment.SearchType } preferences.setPhotoSearchTimestamp(System.currentTimeMillis()); - mStorageManager.saveVirtuals(type, contentValues); + mStorageManager.saveVirtuals(contentValues); } public void showVirtuals(VirtualFolderType type, boolean onlyImages, FileDataStorageManager storageManager) { From f22b386cc5b2193918d3bbae20ac2fbc50fdbf93 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Fri, 3 Jan 2020 12:57:56 +0100 Subject: [PATCH 09/19] Finish refactor FileDataStorageManager Rename file to ocFile for OCFile object to avoid ambiguity with the objects of type File Remove unUsed variable != null (i check method caller) Add != null is necessary Rename to camelCase Consistency in the code Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 803 +++++++++--------- .../operations/RefreshFolderOperation.java | 1 + 2 files changed, 399 insertions(+), 405 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 910d44adefff..f35d8fb80e5d 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -132,47 +132,56 @@ private Cursor executeQuery(Uri uri, String selection, String[] selectionArgs, S public OCFile getFileByPath(String path) { Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path); - OCFile file = null; + OCFile ocFile = null; + if (cursor.moveToFirst()) { - file = createFileInstance(cursor); + ocFile = createFileInstance(cursor); } cursor.close(); - if (file == null && OCFile.ROOT_PATH.equals(path)) { + + if (ocFile == null && OCFile.ROOT_PATH.equals(path)) { return createRootDir(); // root should always exist } - return file; + + return ocFile; } public @Nullable OCFile getFileById(long id) { Cursor cursor = getFileCursorForValue(ProviderTableMeta._ID, String.valueOf(id)); - OCFile file = null; + OCFile ocFile = null; + if (cursor.moveToFirst()) { - file = createFileInstance(cursor); + ocFile = createFileInstance(cursor); } cursor.close(); - return file; + + return ocFile; } public OCFile getFileByLocalPath(String path) { Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path); - OCFile file = null; + OCFile ocFile = null; + if (cursor.moveToFirst()) { - file = createFileInstance(cursor); + ocFile = createFileInstance(cursor); } cursor.close(); - return file; + + return ocFile; } public @Nullable OCFile getFileByRemoteId(String remoteId) { Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_REMOTE_ID, remoteId); - OCFile file = null; + OCFile ocFile = null; + if (cursor.moveToFirst()) { - file = createFileInstance(cursor); + ocFile = createFileInstance(cursor); } cursor.close(); - return file; + + return ocFile; } public boolean fileExists(long id) { @@ -194,98 +203,100 @@ public List getFolderContent(OCFile ocFile, boolean onlyOnDevice) { public List getFolderImages(OCFile folder, boolean onlyOnDevice) { - List ret = new ArrayList<>(); + List imageList = new ArrayList<>(); if (folder != null) { // TODO better implementation, filtering in the access to database instead of here - List tmp = getFolderContent(folder, onlyOnDevice); + List folderContent = getFolderContent(folder, onlyOnDevice); - for (OCFile file : tmp) { - if (MimeTypeUtil.isImage(file)) { - ret.add(file); + for (OCFile ocFile : folderContent) { + if (MimeTypeUtil.isImage(ocFile)) { + imageList.add(ocFile); } } } - return ret; + + return imageList; } - public boolean saveFile(OCFile file) { + public boolean saveFile(OCFile ocFile) { boolean overridden = false; - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); - cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() - ); - cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType()); - cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); - cv.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, file.getEncryptedFileName()); - cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); - cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - if (!file.isFolder()) { - cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, file.isEncrypted()); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); - cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); - cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); - cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded()); - cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); - cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); - cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, file.getUnreadCommentsCount()); - cv.put(ProviderTableMeta.FILE_OWNER_ID, file.getOwnerId()); - cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, file.getOwnerDisplayName()); - cv.put(ProviderTableMeta.FILE_NOTE, file.getNote()); - cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(file.getSharees())); - cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, file.getRichWorkspace()); - - boolean sameRemotePath = fileExists(file.getRemotePath()); - if (sameRemotePath || - fileExists(file.getFileId())) { // for renamed files; no more delete and create + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + ocFile.getModificationTimestampAtLastSyncForData()); + contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); + contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); + contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); + contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); + if (!ocFile.isFolder()) { + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); + } + contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); + contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); + contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); + contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); + contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); + contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); + contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); + contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); + contentValues.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote()); + contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); + contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); + + boolean sameRemotePath = fileExists(ocFile.getRemotePath()); + if (sameRemotePath || fileExists(ocFile.getFileId())) { // for renamed files; no more delete and create if (sameRemotePath) { - OCFile oldFile = getFileByPath(file.getRemotePath()); - file.setFileId(oldFile.getFileId()); + OCFile oldFile = getFileByPath(ocFile.getRemotePath()); + ocFile.setFileId(oldFile.getFileId()); } overridden = true; + Uri contentUri = ProviderTableMeta.CONTENT_URI; + String where = ProviderTableMeta._ID + " = ?"; + String[] selectionArgs = {String.valueOf(ocFile.getFileId())}; + if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, - ProviderTableMeta._ID + " = ?", - new String[]{String.valueOf(file.getFileId())}); + getContentResolver().update(contentUri, contentValues, + where, + selectionArgs); } else { try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, - cv, ProviderTableMeta._ID + " = ?", - new String[]{String.valueOf(file.getFileId())}); + getContentProviderClient().update(contentUri, + contentValues, where, + selectionArgs); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } } } else { - Uri result_uri = null; + Uri resultUri = null; if (getContentResolver() != null) { - result_uri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); + resultUri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, contentValues); } else { try { - result_uri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); + resultUri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, contentValues); } catch (RemoteException e) { Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); } } - if (result_uri != null) { - long new_id = Long.parseLong(result_uri.getPathSegments().get(1)); - file.setFileId(new_id); + if (resultUri != null) { + long newId = Long.parseLong(resultUri.getPathSegments().get(1)); + ocFile.setFileId(newId); } } @@ -296,21 +307,22 @@ public boolean saveFile(OCFile file) { * traverses a files parent tree to be able to store a file with its parents. Throws a * RemoteOperationFailedException in case the parent can't be retrieved. * - * @param file the file + * @param ocFile the file * @param context the app context * @return the parent file */ - public OCFile saveFileWithParent(OCFile file, Context context) { - if (file.getParentId() == 0 && !OCFile.ROOT_PATH.equals(file.getRemotePath())) { - String remotePath = file.getRemotePath(); - String parentPath = remotePath.substring(0, remotePath.lastIndexOf(file.getFileName())); + public OCFile saveFileWithParent(OCFile ocFile, Context context) { + if (ocFile.getParentId() == 0 && !OCFile.ROOT_PATH.equals(ocFile.getRemotePath())) { + String remotePath = ocFile.getRemotePath(); + String parentPath = remotePath.substring(0, remotePath.lastIndexOf(ocFile.getFileName())); OCFile parentFile = getFileByPath(parentPath); - OCFile returnFile; + if (parentFile == null) { // remote request ReadFileRemoteOperation operation = new ReadFileRemoteOperation(parentPath); + // TODO Deprecated RemoteOperationResult result = operation.execute(getAccount(), context); if (result.isSuccess()) { OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); @@ -318,7 +330,7 @@ public OCFile saveFileWithParent(OCFile file, Context context) { returnFile = saveFileWithParent(remoteFolder, context); } else { Exception exception = result.getException(); - String message = "Error during saving file with parents: " + file.getRemotePath() + " / " + String message = "Error during saving file with parents: " + ocFile.getRemotePath() + " / " + result.getLogMessage(); if (exception != null) { @@ -331,22 +343,23 @@ public OCFile saveFileWithParent(OCFile file, Context context) { returnFile = saveFileWithParent(parentFile, context); } - file.setParentId(returnFile.getFileId()); - saveFile(file); + ocFile.setParentId(returnFile.getFileId()); + saveFile(ocFile); } - return file; + return ocFile; } - private boolean isFileExists(ArrayList filesExists, OCFile file) { + private boolean isFileExists(ArrayList filesExists, OCFile ocFile) { for (Iterator iterator = filesExists.iterator(); iterator.hasNext(); ) { - OCFile ocFile = iterator.next(); - if (file.getFileId() == ocFile.getFileId() - || file.getRemotePath().equals(ocFile.getRemotePath())) { + OCFile fileExists = iterator.next(); + if (ocFile.getFileId() == fileExists.getFileId() + || ocFile.getRemotePath().equals(fileExists.getRemotePath())) { iterator.remove(); return true; } } + return false; } @@ -369,15 +382,15 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection ArrayList fileExistList = getFilesExistsID(updatedFiles); // prepare operations to insert or update files to save in the given folder - for (OCFile file : updatedFiles) { - ContentValues contentValues = createContentValueForFile(file, folder); + for (OCFile ocFile : updatedFiles) { + ContentValues contentValues = createContentValueForFile(ocFile, folder); - if (isFileExists(fileExistList, file)) { + if (isFileExists(fileExistList, ocFile)) { long fileId; - if (file.fileExists()) { - fileId = file.getFileId(); + if (ocFile.fileExists()) { + fileId = ocFile.getFileId(); } else { - fileId = getFileByPath(file.getRemotePath()).getFileId(); + fileId = getFileByPath(ocFile.getRemotePath()).getFileId(); } // updating an existing file operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) @@ -393,27 +406,27 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection // prepare operations to remove files in the given folder String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[2]; - for (OCFile file : filesToRemove) { - if (file.getParentId() == folder.getFileId()) { - whereArgs[0] = account.name; - whereArgs[1] = file.getRemotePath(); - if (file.isFolder()) { + whereArgs[0] = account.name; + for (OCFile ocFile : filesToRemove) { + if (ocFile.getParentId() == folder.getFileId()) { + whereArgs[1] = ocFile.getRemotePath(); + if (ocFile.isFolder()) { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())) + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, ocFile.getFileId())) .withSelection(where, whereArgs).build()); - File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(account.name, file)); + File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile)); if (localFolder.exists()) { removeLocalFolder(localFolder); } } else { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())) + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId())) .withSelection(where, whereArgs).build()); - if (file.isDown()) { - String path = file.getStoragePath(); - if (new File(path).delete() && MimeTypeUtil.isMedia(file.getMimeType())) { + if (ocFile.isDown()) { + String path = ocFile.getStoragePath(); + if (new File(path).delete() && MimeTypeUtil.isMedia(ocFile.getMimeType())) { triggerMediaScan(path); // notify MediaScanner about removed file } } @@ -422,10 +435,10 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } // update metadata of folder - ContentValues cv = createContentValueForFile(folder); + ContentValues contentValues = createContentValueForFile(folder); operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) - .withValues(cv) + .withValues(contentValues) .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(folder.getFileId())}) .build()); @@ -448,18 +461,18 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection // update new id in file objects for insertions if (results != null) { long newId; - Iterator filesIt = updatedFiles.iterator(); - OCFile file; + Iterator fileIterator = updatedFiles.iterator(); + OCFile ocFile; for (ContentProviderResult result : results) { - if (filesIt.hasNext()) { - file = filesIt.next(); + if (fileIterator.hasNext()) { + ocFile = fileIterator.next(); } else { - file = null; + ocFile = null; } if (result.uri != null) { newId = Long.parseLong(result.uri.getPathSegments().get(1)); - if (file != null) { - file.setFileId(newId); + if (ocFile != null) { + ocFile.setFileId(newId); } } } @@ -467,117 +480,119 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } private ContentValues createContentValueForFile(OCFile folder) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); - cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - folder.getModificationTimestampAtLastSyncForData() - ); - cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimeType()); - cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName()); - cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); - cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); - cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); - cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, folder.getEtagOnServer()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); - cv.put(ProviderTableMeta.FILE_FAVORITE, folder.isFavorite()); - cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, folder.isEncrypted()); - cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, folder.getUnreadCommentsCount()); - cv.put(ProviderTableMeta.FILE_OWNER_ID, folder.getOwnerId()); - cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, folder.getOwnerDisplayName()); - cv.put(ProviderTableMeta.FILE_NOTE, folder.getNote()); - cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(folder.getSharees())); - cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, folder.getRichWorkspace()); - - return cv; - } - - private ContentValues createContentValueForFile(OCFile file, OCFile folder) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); - cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData()); - cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimeType()); - cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); - cv.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, file.getEncryptedFileName()); - cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); - cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - if (!file.isFolder()) { - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + folder.getModificationTimestampAtLastSyncForData()); + contentValues.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); + contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimeType()); + contentValues.put(ProviderTableMeta.FILE_NAME, folder.getFileName()); + contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); + contentValues.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); + contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); + contentValues.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); + contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, folder.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); + contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); + contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); + contentValues.put(ProviderTableMeta.FILE_FAVORITE, folder.isFavorite()); + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, folder.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, folder.getUnreadCommentsCount()); + contentValues.put(ProviderTableMeta.FILE_OWNER_ID, folder.getOwnerId()); + contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, folder.getOwnerDisplayName()); + contentValues.put(ProviderTableMeta.FILE_NOTE, folder.getNote()); + contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(folder.getSharees())); + contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, folder.getRichWorkspace()); + + return contentValues; + } + + private ContentValues createContentValueForFile(OCFile ocFile, OCFile folder) { + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + ocFile.getModificationTimestampAtLastSyncForData()); + contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); + contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); + contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); + contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); + if (!ocFile.isFolder()) { + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); - cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); - cv.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, file.getEtagOnServer()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); - cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.isUpdateThumbnailNeeded()); - cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); - cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); - cv.put(ProviderTableMeta.FILE_FAVORITE, file.isFavorite()); - cv.put(ProviderTableMeta.FILE_IS_ENCRYPTED, file.isEncrypted()); - cv.put(ProviderTableMeta.FILE_MOUNT_TYPE, file.getMountType().ordinal()); - cv.put(ProviderTableMeta.FILE_HAS_PREVIEW, file.isPreviewAvailable() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, file.getUnreadCommentsCount()); - cv.put(ProviderTableMeta.FILE_OWNER_ID, file.getOwnerId()); - cv.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, file.getOwnerDisplayName()); - cv.put(ProviderTableMeta.FILE_NOTE, file.getNote()); - cv.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(file.getSharees())); - cv.put(ProviderTableMeta.FILE_RICH_WORKSPACE, file.getRichWorkspace()); - - return cv; - } - - - public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) { + contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); + contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); + contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); + contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); + contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); + contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); + contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); + contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); + contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); + contentValues.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote()); + contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); + contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); + + return contentValues; + } + + + public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLocalCopy) { boolean success = true; - if (file != null) { - if (file.isFolder()) { - success = removeFolder(file, removeDBData, removeLocalCopy); + + if (ocFile != null) { + if (ocFile.isFolder()) { + success = removeFolder(ocFile, removeDBData, removeLocalCopy); } else { + if (removeDBData) { - //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, - // ""+file.getFileId()); - Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId()); + Uri fileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId()); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; - String[] whereArgs = new String[]{account.name, file.getRemotePath()}; + String[] whereArgs = new String[]{account.name, ocFile.getRemotePath()}; int deleted = 0; - if (getContentProviderClient() != null) { + + if (getContentResolver() != null) { + deleted = getContentResolver().delete(fileUri, where, whereArgs); + } else { try { - deleted = getContentProviderClient().delete(file_uri, where, whereArgs); + deleted = getContentProviderClient().delete(fileUri, where, whereArgs); } catch (RemoteException e) { Log_OC.d(TAG, e.getMessage(), e); } - } else { - deleted = getContentResolver().delete(file_uri, where, whereArgs); } success = deleted > 0; } - String localPath = file.getStoragePath(); - if (removeLocalCopy && file.isDown() && localPath != null && success) { + + String localPath = ocFile.getStoragePath(); + if (removeLocalCopy && ocFile.isDown() && localPath != null && success) { success = new File(localPath).delete(); if (success) { deleteFileInMediaScan(localPath); } - if (!removeDBData && success) { + + if (success && !removeDBData) { // maybe unnecessary, but should be checked TODO remove if unnecessary - file.setStoragePath(null); - saveFile(file); - saveConflict(file, null); + ocFile.setStoragePath(null); + saveFile(ocFile); + saveConflict(ocFile, null); } } } @@ -606,20 +621,21 @@ public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeL } private boolean removeFolderInDb(OCFile folder) { - Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(folder.getFileId())); // URI + Uri folderUri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(folder.getFileId())); // for recursive deletion String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; - String[] whereArgs = new String[]{account.name, folder.getRemotePath()}; + String[] whereArgs = {account.name, folder.getRemotePath()}; int deleted = 0; - if (getContentProviderClient() != null) { + if (getContentResolver() != null) { + deleted = getContentResolver().delete(folderUri, where, whereArgs); + } else { try { - deleted = getContentProviderClient().delete(folder_uri, where, whereArgs); + deleted = getContentProviderClient().delete(folderUri, where, whereArgs); } catch (RemoteException e) { Log_OC.d(TAG, e.getMessage(), e); } - } else { - deleted = getContentResolver().delete(folder_uri, where, whereArgs); } + return deleted > 0; } @@ -627,22 +643,22 @@ private boolean removeLocalFolder(OCFile folder) { boolean success = true; String localFolderPath = FileStorageUtils.getDefaultSavePathFor(account.name, folder); File localFolder = new File(localFolderPath); + if (localFolder.exists()) { // stage 1: remove the local files already registered in the files database List files = getFolderContent(folder.getFileId(), false); - for (OCFile file : files) { - if (file.isFolder()) { - success &= removeLocalFolder(file); - } else { - if (file.isDown()) { - File localFile = new File(file.getStoragePath()); - success &= localFile.delete(); - if (success) { - // notify MediaScanner about removed file - deleteFileInMediaScan(file.getStoragePath()); - file.setStoragePath(null); - saveFile(file); - } + for (OCFile ocFile : files) { + if (ocFile.isFolder()) { + success &= removeLocalFolder(ocFile); + } else if (ocFile.isDown()) { + File localFile = new File(ocFile.getStoragePath()); + success &= localFile.delete(); + + if (success) { + // notify MediaScanner about removed file + deleteFileInMediaScan(ocFile.getStoragePath()); + ocFile.setStoragePath(null); + saveFile(ocFile); } } } @@ -651,12 +667,14 @@ private boolean removeLocalFolder(OCFile folder) { // for instance, after clearing the app cache or reinstalling success &= removeLocalFolder(localFolder); } + return success; } private boolean removeLocalFolder(File localFolder) { boolean success = true; File[] localFiles = localFolder.listFiles(); + if (localFiles != null) { for (File localFile : localFiles) { if (localFile.isDirectory()) { @@ -667,6 +685,7 @@ private boolean removeLocalFolder(File localFolder) { } } success &= localFolder.delete(); + return success; } @@ -675,9 +694,8 @@ private boolean removeLocalFolder(File localFolder) { *

* TODO explore better (faster) implementations TODO throw exceptions up ! */ - public void moveLocalFile(OCFile file, String targetPath, String targetParentPath) { - - if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) { + public void moveLocalFile(OCFile ocFile, String targetPath, String targetParentPath) { + if (ocFile.fileExists() && !OCFile.ROOT_PATH.equals(ocFile.getFileName())) { OCFile targetParent = getFileByPath(targetParentPath); if (targetParent == null) { @@ -685,72 +703,61 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat } /// 1. get all the descendants of the moved element in a single QUERY - Cursor c = null; - if (getContentProviderClient() != null) { - try { - c = getContentProviderClient().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, file.getRemotePath() + "%"}, - ProviderTableMeta.FILE_PATH + " ASC " - ); - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage(), e); - } + Uri contentUri = ProviderTableMeta.CONTENT_URI; + String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? "; + String[] selectionArgs = {account.name, ocFile.getRemotePath() + "%"}; + String sortOrder = ProviderTableMeta.FILE_PATH + " ASC "; - } else { - c = getContentResolver().query( - ProviderTableMeta.CONTENT_URI, - null, - ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " LIKE ? ", - new String[]{account.name, file.getRemotePath() + "%"}, - ProviderTableMeta.FILE_PATH + " ASC " - ); + Cursor cursor = executeQuery(contentUri, null, selection, selectionArgs, sortOrder, ""); + + if (cursor == null) { + return; } /// 2. prepare a batch of update operations to change all the descendants - ArrayList operations = new ArrayList<>(c.getCount()); + ArrayList operations = new ArrayList<>(cursor.getCount()); String defaultSavePath = FileStorageUtils.getSavePath(account.name); List originalPathsToTriggerMediaScan = new ArrayList<>(); List newPathsToTriggerMediaScan = new ArrayList<>(); - if (c.moveToFirst()) { - int lengthOfOldPath = file.getRemotePath().length(); + + if (cursor.moveToFirst()) { + int lengthOfOldPath = ocFile.getRemotePath().length(); int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath; - String[] fileId = new String[1]; do { - ContentValues cv = new ContentValues(); // keep construction in the loop - OCFile child = createFileInstance(c); - cv.put( + ContentValues contentValues = new ContentValues(); // keep construction in the loop + OCFile childFile = createFileInstance(cursor); + contentValues.put( ProviderTableMeta.FILE_PATH, - targetPath + child.getRemotePath().substring(lengthOfOldPath) + targetPath + childFile.getRemotePath().substring(lengthOfOldPath) ); - if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) { + + if (childFile.getStoragePath() != null && childFile.getStoragePath().startsWith(defaultSavePath)) { // update link to downloaded content - but local move is not done here! String targetLocalPath = defaultSavePath + targetPath + - child.getStoragePath().substring(lengthOfOldStoragePath); + childFile.getStoragePath().substring(lengthOfOldStoragePath); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath); + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, targetLocalPath); - if (MimeTypeUtil.isMedia(child.getMimeType())) { - originalPathsToTriggerMediaScan.add(child.getStoragePath()); + if (MimeTypeUtil.isMedia(childFile.getMimeType())) { + originalPathsToTriggerMediaScan.add(childFile.getStoragePath()); newPathsToTriggerMediaScan.add(targetLocalPath); } } - if (child.getRemotePath().equals(file.getRemotePath())) { - cv.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId()); + + if (childFile.getRemotePath().equals(ocFile.getRemotePath())) { + contentValues.put(ProviderTableMeta.FILE_PARENT, targetParent.getFileId()); } - fileId[0] = String.valueOf(child.getFileId()); + operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + " = ?", fileId) + ContentProviderOperation.newUpdate(contentUri) + .withValues(contentValues) + .withSelection(ProviderTableMeta._ID + " = ?", new String[]{String.valueOf(childFile.getFileId())}) .build()); - } while (c.moveToNext()); + } while (cursor.moveToNext()); } - c.close(); + cursor.close(); /// 3. apply updates in batch try { @@ -759,51 +766,51 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat } else { getContentProviderClient().applyBatch(operations); } - } catch (Exception e) { - Log_OC.e(TAG, "Fail to update " + file.getFileId() + " and descendants in database", e); + Log_OC.e(TAG, "Fail to update " + ocFile.getFileId() + " and descendants in database", e); } /// 4. move in local file system - String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(account.name, file); + String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile); String targetLocalPath = defaultSavePath + targetPath; File localFile = new File(originalLocalPath); boolean renamed = false; + if (localFile.exists()) { File targetFile = new File(targetLocalPath); File targetFolder = targetFile.getParentFile(); - if (!targetFolder.exists() && !targetFolder.mkdirs()) { + if (targetFolder != null && !targetFolder.exists() && !targetFolder.mkdirs()) { Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath()); } renamed = localFile.renameTo(targetFile); } if (renamed) { - Iterator it = originalPathsToTriggerMediaScan.iterator(); - while (it.hasNext()) { + Iterator pathIterator = originalPathsToTriggerMediaScan.iterator(); + while (pathIterator.hasNext()) { // Notify MediaScanner about removed file - deleteFileInMediaScan(it.next()); + deleteFileInMediaScan(pathIterator.next()); } - it = newPathsToTriggerMediaScan.iterator(); - while (it.hasNext()) { + + pathIterator = newPathsToTriggerMediaScan.iterator(); + while (pathIterator.hasNext()) { // Notify MediaScanner about new file/folder - triggerMediaScan(it.next()); + triggerMediaScan(pathIterator.next()); } } } } - public void copyLocalFile(OCFile file, String targetPath) { - - if (file != null && file.fileExists() && !OCFile.ROOT_PATH.equals(file.getFileName())) { - String localPath = FileStorageUtils.getDefaultSavePathFor(account.name, file); + public void copyLocalFile(OCFile ocFile, String targetPath) { + if (ocFile.fileExists() && !OCFile.ROOT_PATH.equals(ocFile.getFileName())) { + String localPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile); File localFile = new File(localPath); boolean copied = false; String defaultSavePath = FileStorageUtils.getSavePath(account.name); if (localFile.exists()) { File targetFile = new File(defaultSavePath + targetPath); File targetFolder = targetFile.getParentFile(); - if (!targetFolder.exists() && !targetFolder.mkdirs()) { + if (targetFolder != null && !targetFolder.exists() && !targetFolder.mkdirs()) { Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath()); } copied = FileStorageUtils.copyFile(localFile, targetFile); @@ -812,58 +819,43 @@ public void copyLocalFile(OCFile file, String targetPath) { } } - public void migrateStoredFiles(String srcPath, String dstPath) + public void migrateStoredFiles(String sourcePath, String destinationPath) throws RemoteException, OperationApplicationException { - Cursor cursor; - try { - if (getContentResolver() != null) { - cursor = getContentResolver().query(ProviderTableMeta.CONTENT_URI_FILE, - null, - ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", - null, - null); - - } else { - cursor = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI_FILE, - new String[]{ProviderTableMeta._ID, ProviderTableMeta.FILE_STORAGE_PATH}, - ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", - null, - null); - } - } catch (RemoteException e) { - Log_OC.e(TAG, e.getMessage(), e); - throw e; - } - - ArrayList operations = new ArrayList<>(cursor.getCount()); - if (cursor.moveToFirst()) { - String[] fileId = new String[1]; - do { - ContentValues cv = new ContentValues(); - fileId[0] = String.valueOf(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); - String oldFileStoragePath = - cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); - - if (oldFileStoragePath.startsWith(srcPath)) { + Cursor cursor = executeQuery(ProviderTableMeta.CONTENT_URI_FILE, + ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", + null, + ""); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(srcPath, dstPath)); + if (cursor != null) { + ArrayList operations = new ArrayList<>(cursor.getCount()); - operations.add( - ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + " = ?", fileId) - .build()); - } + if (cursor.moveToFirst()) { + do { + String oldFileStoragePath = + cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); + + if (oldFileStoragePath.startsWith(sourcePath)) { + ContentValues contentValues = new ContentValues(); + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFileStoragePath.replaceFirst(sourcePath, destinationPath)); + String[] withSelection = {String.valueOf(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID)))}; + + operations.add( + ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) + .withValues(contentValues) + .withSelection(ProviderTableMeta._ID + " = ?", withSelection) + .build()); + } - } while (cursor.moveToNext()); - } - cursor.close(); + } while (cursor.moveToNext()); + } + cursor.close(); - /// 3. apply updates in batch - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); + /// 3. apply updates in batch + if (getContentResolver() != null) { + getContentResolver().applyBatch(MainApp.getAuthority(), operations); + } else { + getContentProviderClient().applyBatch(operations); + } } } @@ -894,15 +886,16 @@ private List getFolderContent(long parentId, boolean onlyOnDevice) { private OCFile createRootDir() { - OCFile file = new OCFile(OCFile.ROOT_PATH); - file.setMimeType(MimeType.DIRECTORY); - file.setParentId(FileDataStorageManager.ROOT_PARENT_ID); - saveFile(file); - return file; + OCFile ocFile = new OCFile(OCFile.ROOT_PATH); + ocFile.setMimeType(MimeType.DIRECTORY); + ocFile.setParentId(FileDataStorageManager.ROOT_PARENT_ID); + saveFile(ocFile); + + return ocFile; } - private boolean fileExists(String cmp_key, String value) { - Cursor cursor = getFileCursorForValue(cmp_key, value); + private boolean fileExists(String key, String value) { + Cursor cursor = getFileCursorForValue(key, value); boolean isExists = false; if (cursor == null) { @@ -932,13 +925,13 @@ private ArrayList getFilesExistsID(ArrayList updatedFiles) { listPathString.clear(); StringBuilder inList = new StringBuilder(loopSize * 2); for (int i = 0; i < loopSize; i++, processSize++) { - OCFile file = updatedFiles.get(processSize); + OCFile ocFile = updatedFiles.get(processSize); if (i > 0) { inList.append(","); } inList.append("?"); - listIDString.add(String.valueOf(file.getFileId())); - listPathString.add(file.getRemotePath()); + listIDString.add(String.valueOf(ocFile.getFileId())); + listPathString.add(ocFile.getRemotePath()); } String selection = ProviderTableMeta.FILE_ACCOUNT_OWNER @@ -959,9 +952,9 @@ private ArrayList getFilesExistsID(ArrayList updatedFiles) { if (cursor != null) { if (cursor.moveToFirst()) { do { - OCFile file = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); - file.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); - existsFiles.add(file); + OCFile ocFile = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); + ocFile.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); + existsFiles.add(ocFile); } while (cursor.moveToNext()); } cursor.close(); @@ -988,73 +981,73 @@ private OCFile createFileInstanceFromVirtual(Cursor cursor) { return getFileById(fileId); } - private OCFile createFileInstance(Cursor c) { - OCFile file = null; - if (c != null) { - file = new OCFile(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))); - file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID))); - file.setParentId(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_PARENT))); - file.setEncryptedFileName(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ENCRYPTED_NAME))); - file.setMimeType(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))); - file.setStoragePath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))); - if (file.getStoragePath() == null) { + private OCFile createFileInstance(Cursor cursor) { + OCFile ocFile = null; + if (cursor != null) { + ocFile = new OCFile(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PATH))); + ocFile.setFileId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta._ID))); + ocFile.setParentId(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_PARENT))); + ocFile.setEncryptedFileName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ENCRYPTED_NAME))); + ocFile.setMimeType(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE))); + ocFile.setStoragePath(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH))); + if (ocFile.getStoragePath() == null) { // try to find existing file and bind it with current account; // with the current update of SynchronizeFolderOperation, this won't be // necessary anymore after a full synchronization of the account - File f = new File(FileStorageUtils.getDefaultSavePathFor(account.name, file)); - if (f.exists()) { - file.setStoragePath(f.getAbsolutePath()); - file.setLastSyncDateForData(f.lastModified()); + File file = new File(FileStorageUtils.getDefaultSavePathFor(account.name, ocFile)); + if (file.exists()) { + ocFile.setStoragePath(file.getAbsolutePath()); + ocFile.setLastSyncDateForData(file.lastModified()); } } - file.setFileLength(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))); - file.setCreationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_CREATION))); - file.setModificationTimestamp(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED))); - file.setModificationTimestampAtLastSyncForData(c.getLong( - c.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); - file.setLastSyncDateForProperties(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE))); - file.setLastSyncDateForData(c.getLong(c.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA))); - file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG))); - file.setEtagOnServer(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG_ON_SERVER))); - file.setSharedViaLink(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1); - file.setSharedWithSharee(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1); - file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK))); - file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS))); - file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID))); - file.setUpdateThumbnailNeeded(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1); - file.setDownloading(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1); - file.setEtagInConflict(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT))); - file.setFavorite(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_FAVORITE)) == 1); - file.setEncrypted(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_IS_ENCRYPTED)) == 1); - if (file.isEncrypted()) { - file.setFileName(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NAME))); + ocFile.setFileLength(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH))); + ocFile.setCreationTimestamp(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_CREATION))); + ocFile.setModificationTimestamp(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED))); + ocFile.setModificationTimestampAtLastSyncForData(cursor.getLong( + cursor.getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA))); + ocFile.setLastSyncDateForProperties(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE))); + ocFile.setLastSyncDateForData(cursor.getLong(cursor.getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA))); + ocFile.setEtag(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG))); + ocFile.setEtagOnServer(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG_ON_SERVER))); + ocFile.setSharedViaLink(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_SHARED_VIA_LINK)) == 1); + ocFile.setSharedWithSharee(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_SHARED_WITH_SHAREE)) == 1); + ocFile.setPublicLink(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK))); + ocFile.setPermissions(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS))); + ocFile.setRemoteId(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID))); + ocFile.setUpdateThumbnailNeeded(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_UPDATE_THUMBNAIL)) == 1); + ocFile.setDownloading(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_IS_DOWNLOADING)) == 1); + ocFile.setEtagInConflict(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_ETAG_IN_CONFLICT))); + ocFile.setFavorite(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_FAVORITE)) == 1); + ocFile.setEncrypted(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_IS_ENCRYPTED)) == 1); + if (ocFile.isEncrypted()) { + ocFile.setFileName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_NAME))); } - file.setMountType(WebdavEntry.MountType.values()[c.getInt( - c.getColumnIndex(ProviderTableMeta.FILE_MOUNT_TYPE))]); - file.setPreviewAvailable(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_HAS_PREVIEW)) == 1); - file.setUnreadCommentsCount(c.getInt(c.getColumnIndex(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT))); - file.setOwnerId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_OWNER_ID))); - file.setOwnerDisplayName(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME))); - file.setNote(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_NOTE))); - file.setRichWorkspace(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_RICH_WORKSPACE))); + ocFile.setMountType(WebdavEntry.MountType.values()[cursor.getInt( + cursor.getColumnIndex(ProviderTableMeta.FILE_MOUNT_TYPE))]); + ocFile.setPreviewAvailable(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_HAS_PREVIEW)) == 1); + ocFile.setUnreadCommentsCount(cursor.getInt(cursor.getColumnIndex(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT))); + ocFile.setOwnerId(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_OWNER_ID))); + ocFile.setOwnerDisplayName(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME))); + ocFile.setNote(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_NOTE))); + ocFile.setRichWorkspace(cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_RICH_WORKSPACE))); - String sharees = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_SHAREES)); + String sharees = cursor.getString(cursor.getColumnIndex(ProviderTableMeta.FILE_SHAREES)); if (sharees == null || NULL_STRING.equals(sharees) || sharees.isEmpty()) { - file.setSharees(new ArrayList<>()); + ocFile.setSharees(new ArrayList<>()); } else { try { ShareeUser[] shareesArray = new Gson().fromJson(sharees, ShareeUser[].class); - file.setSharees(new ArrayList<>(Arrays.asList(shareesArray))); + ocFile.setSharees(new ArrayList<>(Arrays.asList(shareesArray))); } catch (JsonSyntaxException e) { // ignore saved value due to api change - file.setSharees(new ArrayList<>()); + ocFile.setSharees(new ArrayList<>()); } } } - return file; + return ocFile; } public void saveShare(OCShare share) { @@ -1380,14 +1373,14 @@ public void removeShare(OCShare share) { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + ProviderTableMeta._ID + " = ?"; String[] whereArgs = {account.name, Long.toString(share.getId())}; - if (getContentProviderClient() != null) { + if (getContentResolver() != null) { + getContentResolver().delete(contentUriShare, where, whereArgs); + } else { try { getContentProviderClient().delete(contentUriShare, where, whereArgs); } catch (RemoteException e) { Log_OC.d(TAG, e.getMessage(), e); } - } else { - getContentResolver().delete(contentUriShare, where, whereArgs); } } @@ -1529,13 +1522,13 @@ private ArrayList prepareRemoveSharesInFolder( listPathString.clear(); StringBuilder inList = new StringBuilder(folderContent.size() * 2); for (int i = 0; i < loopSize; i++, processSize++) { - OCFile file = folderContent.get(processSize); + OCFile ocFile = folderContent.get(processSize); if (i > 0) { inList.append(","); } inList.append("?"); - listPathString.add(file.getRemotePath()); + listPathString.add(ocFile.getRemotePath()); } String selection = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND @@ -1664,8 +1657,8 @@ public void deleteFileInMediaScan(String path) { } } - public void saveConflict(OCFile file, String etagInConflict) { - if (!file.isDown()) { + public void saveConflict(OCFile ocFile, String etagInConflict) { + if (!ocFile.isDown()) { etagInConflict = null; } @@ -1673,7 +1666,7 @@ public void saveConflict(OCFile file, String etagInConflict) { ContentValues contentValues = new ContentValues(); contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict); String where = ProviderTableMeta._ID + " = ?"; - String[] selectionArgs = {String.valueOf(file.getFileId())}; + String[] selectionArgs = {String.valueOf(ocFile.getFileId())}; int updated = 0; if (getContentResolver() != null) { updated = getContentResolver().update(contentUriFile, contentValues, where, selectionArgs); @@ -1691,7 +1684,7 @@ public void saveConflict(OCFile file, String etagInConflict) { if (etagInConflict != null) { /// set conflict in all ancestor folders - long parentId = file.getParentId(); + long parentId = ocFile.getParentId(); Set ancestorIds = new HashSet<>(); while (parentId != FileDataStorageManager.ROOT_PARENT_ID) { ancestorIds.add(Long.toString(parentId)); @@ -1726,12 +1719,12 @@ public void saveConflict(OCFile file, String etagInConflict) { Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); } } - } // else file is ROOT folder, no parent to set in conflict + } // else ocFile is ROOT folder, no parent to set in conflict } else { /// update conflict in ancestor folders // (not directly unset; maybe there are more conflicts below them) - String parentPath = file.getRemotePath(); + String parentPath = ocFile.getRemotePath(); if (parentPath.endsWith(OCFile.PATH_SEPARATOR)) { parentPath = parentPath.substring(0, parentPath.length() - 1); } diff --git a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index da17b3c0fffe..fa01cd58b827 100644 --- a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -449,6 +449,7 @@ private void synchronizeData(List folderAndFiles) { // retrieve local data for the read file localFile = localFilesMap.remove(remoteFile.getRemotePath()); + // TODO better implementation is need if (localFile == null) { localFile = mStorageManager.getFileByPath(updatedFile.getRemotePath()); } From 2b6989269415e936a79f15f6a6e9dfd41b807ef3 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 14:13:57 +0100 Subject: [PATCH 10/19] Extract method for applyBatch and updateFiles Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 224 +++++------------- 1 file changed, 57 insertions(+), 167 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index f35d8fb80e5d..7865f836e88c 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -77,6 +77,7 @@ public class FileDataStorageManager { private static final String AND = " = ? AND "; private static final String FAILED_TO_INSERT_MSG = "Fail to insert file to database "; + private static final String FAILED_TO_UPDATE_MSG = "Fail to update file to database "; private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider"; private static final String EXCEPTION_MSG = "Exception in batch of operations "; @@ -130,6 +131,46 @@ private Cursor executeQuery(Uri uri, String selection, String[] selectionArgs, S return executeQuery(uri, null, selection, selectionArgs, null, errorMessage); } + private void applyBatch(ArrayList operations, String errorMessage) { + Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); + if (!operations.isEmpty()) { + try { + if (getContentResolver() != null) { + getContentResolver().applyBatch(MainApp.getAuthority(), operations); + } else { + getContentProviderClient().applyBatch(operations); + } + } catch (OperationApplicationException | RemoteException e) { + Log_OC.e(TAG, errorMessage + e.getMessage(), e); + } + } + } + + private void applyBatch(ArrayList operations) { + applyBatch(operations, EXCEPTION_MSG); + } + + private int updateFiles(Uri contentUri, ContentValues contentValues, String where, String[] selectionArgs, + String errorMessage) { + int updated = 0; + if (getContentResolver() != null) { + updated = getContentResolver().update(contentUri, + contentValues, + where, + selectionArgs); + } else { + try { + updated = getContentProviderClient().update(contentUri, + contentValues, + where, + selectionArgs); + } catch (RemoteException e) { + Log_OC.e(TAG, errorMessage + e.getMessage(), e); + } + } + return updated; + } + public OCFile getFileByPath(String path) { Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path); OCFile ocFile = null; @@ -270,19 +311,7 @@ public boolean saveFile(OCFile ocFile) { String where = ProviderTableMeta._ID + " = ?"; String[] selectionArgs = {String.valueOf(ocFile.getFileId())}; - if (getContentResolver() != null) { - getContentResolver().update(contentUri, contentValues, - where, - selectionArgs); - } else { - try { - getContentProviderClient().update(contentUri, - contentValues, where, - selectionArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } + updateFiles(contentUri, contentValues, where, selectionArgs, FAILED_TO_UPDATE_MSG); } else { Uri resultUri = null; if (getContentResolver() != null) { @@ -760,15 +789,7 @@ public void moveLocalFile(OCFile ocFile, String targetPath, String targetParentP cursor.close(); /// 3. apply updates in batch - try { - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); - } - } catch (Exception e) { - Log_OC.e(TAG, "Fail to update " + ocFile.getFileId() + " and descendants in database", e); - } + applyBatch(operations, "Fail to update " + ocFile.getFileId() + " and descendants in database"); /// 4. move in local file system String originalLocalPath = FileStorageUtils.getDefaultSavePathFor(account.name, ocFile); @@ -851,11 +872,7 @@ public void migrateStoredFiles(String sourcePath, String destinationPath) cursor.close(); /// 3. apply updates in batch - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); - } + applyBatch(operations); } } @@ -1075,21 +1092,7 @@ public void saveShare(OCShare share) { String where = ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + " = ?"; String[] selectionArgs = {String.valueOf(share.getRemoteId())}; - if (getContentResolver() != null) { - getContentResolver().update(contentUriShare, - contentValues, - where, - selectionArgs); - } else { - try { - getContentProviderClient().update(contentUriShare, - contentValues, - where, - selectionArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } + updateFiles(contentUriShare, contentValues, where, selectionArgs, FAILED_TO_UPDATE_MSG); } else { Uri resultUri = null; if (getContentResolver() != null) { @@ -1264,15 +1267,7 @@ private void resetShareFlagsInFolder(OCFile folder) { String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PARENT + " = ?"; String[] whereArgs = new String[]{account.name, String.valueOf(folder.getFileId())}; - if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); - } else { - try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e); - } - } + updateFiles(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs, "Exception in resetShareFlagsInFolder "); } private void resetShareFlagInAFile(String filePath) { @@ -1283,15 +1278,7 @@ private void resetShareFlagInAFile(String filePath) { String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[]{account.name, filePath}; - if (getContentResolver() != null) { - getContentResolver().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); - } else { - try { - getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in resetShareFlagsInFolder " + e.getMessage(), e); - } - } + updateFiles(ProviderTableMeta.CONTENT_URI, contentValues, where, whereArgs, "Exception in resetShareFlagInAFile "); } private void cleanShares() { @@ -1354,18 +1341,7 @@ public void saveShares(Collection shares) { } // apply operations in batch - if (operations.size() > 0) { - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); - } - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } - } + applyBatch(operations); } public void removeShare(OCShare share) { @@ -1401,39 +1377,14 @@ public void saveSharesDB(List shares) { operations = prepareInsertShares(shares, operations); // apply operations in batch - if (operations.size() > 0) { - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } - } + applyBatch(operations); } public void removeSharesForFile(String remotePath) { resetShareFlagInAFile(remotePath); ArrayList operations = prepareRemoveSharesInFile(remotePath, new ArrayList<>()); // apply operations in batch - if (!operations.isEmpty()) { - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - - } else { - getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } - } + applyBatch(operations); } @@ -1446,19 +1397,7 @@ public void saveSharesInFolder(ArrayList shares, OCFile folder) { operations = prepareInsertShares(shares, operations); // apply operations in batch - if (operations.size() > 0) { - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - try { - if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); - } else { - getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } - } + applyBatch(operations); } @@ -1667,16 +1606,7 @@ public void saveConflict(OCFile ocFile, String etagInConflict) { contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, etagInConflict); String where = ProviderTableMeta._ID + " = ?"; String[] selectionArgs = {String.valueOf(ocFile.getFileId())}; - int updated = 0; - if (getContentResolver() != null) { - updated = getContentResolver().update(contentUriFile, contentValues, where, selectionArgs); - } else { - try { - updated = getContentProviderClient().update(contentUriFile, contentValues, where, selectionArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); - } - } + int updated = updateFiles(contentUriFile, contentValues, where, selectionArgs, "Failed saving conflict in database "); Log_OC.d(TAG, "Number of files updated with CONFLICT: " + updated); @@ -1700,25 +1630,7 @@ public void saveConflict(OCFile ocFile, String etagInConflict) { } stringBuilder.append("?)"); - if (getContentResolver() != null) { - getContentResolver().update( - contentUriFile, - contentValues, - stringBuilder.toString(), - ancestorIds.toArray(new String[]{}) - ); - } else { - try { - getContentProviderClient().update( - contentUriFile, - contentValues, - stringBuilder.toString(), - ancestorIds.toArray(new String[]{}) - ); - } catch (RemoteException e) { - Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); - } - } + updateFiles(contentUriFile, contentValues, stringBuilder.toString(), ancestorIds.toArray(new String[]{}), "Failed saving conflict in database "); } // else ocFile is ROOT folder, no parent to set in conflict } else { @@ -1755,25 +1667,11 @@ public void saveConflict(OCFile ocFile, String etagInConflict) { where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; selectionArgs = new String[]{account.name, parentPath}; - if (getContentResolver() != null) { - getContentResolver().update( - contentUriFile, - contentValues, - where, - selectionArgs - ); - } else { - try { - getContentProviderClient().update( - contentUriFile, + updateFiles(contentUriFile, contentValues, where, - selectionArgs - ); - } catch (RemoteException e) { - Log_OC.e(TAG, "Failed saving conflict in database " + e.getMessage(), e); - } - } + selectionArgs, + "Failed saving conflict in database "); } else { Log_OC.d(TAG, "STILL " + descendentsInConflict.getCount() + " in " + parentPath); @@ -1799,15 +1697,7 @@ public void saveCapabilities(OCCapability capability) { if (capabilityExists(account.name)) { String where = ProviderTableMeta.CAPABILITIES_ACCOUNT_NAME + " = ?"; String[] selectionArgs = {account.name}; - if (getContentResolver() != null) { - getContentResolver().update(contentUriCapabilities, contentValues, where, selectionArgs); - } else { - try { - getContentProviderClient().update(contentUriCapabilities, contentValues, where, selectionArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Failed saveCapabilities update" + e.getMessage(), e); - } - } + updateFiles(contentUriCapabilities, contentValues, where, selectionArgs, "Failed saveCapabilities update"); } else { Uri resultUri = null; if (getContentResolver() != null) { From f7c8bf2f4c7f081ecf66b8849151021285de002d Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 14:33:29 +0100 Subject: [PATCH 11/19] Extract insertFile and DeleteFiles Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 130 ++++++------------ 1 file changed, 44 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 7865f836e88c..4871b77719b6 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -171,6 +171,40 @@ private int updateFiles(Uri contentUri, ContentValues contentValues, String wher return updated; } + private Uri insertFile(Uri uri, ContentValues values) { + Uri resultUri = null; + if (getContentResolver() != null) { + resultUri = getContentResolver().insert(uri, values); + } else { + try { + resultUri = getContentProviderClient().insert(uri, values); + } catch (RemoteException e) { + Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + } + } + + return resultUri; + } + + private int deleteFiles(Uri uri, String where, String[] whereArgs, String errorMessage) { + int deleted = 0; + if (getContentResolver() != null) { + deleted = getContentResolver().delete(uri, where, whereArgs); + } else { + try { + deleted = getContentProviderClient().delete(uri, where, whereArgs); + } catch (RemoteException e) { + Log_OC.e(TAG, errorMessage + e.getMessage(), e); + } + } + + return deleted; + } + + private int deleteFiles(Uri uri, String where, String[] whereArgs) { + return deleteFiles(uri, where, whereArgs, ""); + } + public OCFile getFileByPath(String path) { Cursor cursor = getFileCursorForValue(ProviderTableMeta.FILE_PATH, path); OCFile ocFile = null; @@ -313,16 +347,7 @@ public boolean saveFile(OCFile ocFile) { updateFiles(contentUri, contentValues, where, selectionArgs, FAILED_TO_UPDATE_MSG); } else { - Uri resultUri = null; - if (getContentResolver() != null) { - resultUri = getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, contentValues); - } else { - try { - resultUri = getContentProviderClient().insert(ProviderTableMeta.CONTENT_URI_FILE, contentValues); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } + Uri resultUri = insertFile(ProviderTableMeta.CONTENT_URI_FILE, contentValues); if (resultUri != null) { long newId = Long.parseLong(resultUri.getPathSegments().get(1)); ocFile.setFileId(newId); @@ -596,17 +621,8 @@ public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLoc Uri fileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, ocFile.getFileId()); String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = new String[]{account.name, ocFile.getRemotePath()}; - int deleted = 0; + int deleted = deleteFiles(fileUri, where, whereArgs); - if (getContentResolver() != null) { - deleted = getContentResolver().delete(fileUri, where, whereArgs); - } else { - try { - deleted = getContentProviderClient().delete(fileUri, where, whereArgs); - } catch (RemoteException e) { - Log_OC.d(TAG, e.getMessage(), e); - } - } success = deleted > 0; } @@ -654,16 +670,7 @@ private boolean removeFolderInDb(OCFile folder) { // for recursive deletion String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + " = ?"; String[] whereArgs = {account.name, folder.getRemotePath()}; - int deleted = 0; - if (getContentResolver() != null) { - deleted = getContentResolver().delete(folderUri, where, whereArgs); - } else { - try { - deleted = getContentProviderClient().delete(folderUri, where, whereArgs); - } catch (RemoteException e) { - Log_OC.d(TAG, e.getMessage(), e); - } - } + int deleted = deleteFiles(folderUri, where, whereArgs); return deleted > 0; } @@ -1094,16 +1101,7 @@ public void saveShare(OCShare share) { updateFiles(contentUriShare, contentValues, where, selectionArgs, FAILED_TO_UPDATE_MSG); } else { - Uri resultUri = null; - if (getContentResolver() != null) { - resultUri = getContentResolver().insert(contentUriShare, contentValues); - } else { - try { - resultUri = getContentProviderClient().insert(contentUriShare, contentValues); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } + Uri resultUri = insertFile(contentUriShare, contentValues); if (resultUri != null) { long new_id = Long.parseLong(resultUri.getPathSegments().get(1)); share.setId(new_id); @@ -1286,15 +1284,7 @@ private void cleanShares() { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + " = ?"; String[] whereArgs = new String[]{account.name}; - if (getContentResolver() != null) { - getContentResolver().delete(contentUriShare, where, whereArgs); - } else { - try { - getContentProviderClient().delete(contentUriShare, where, whereArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage(), e); - } - } + deleteFiles(contentUriShare, where, whereArgs, "Exception in cleanShares "); } public void saveShares(Collection shares) { @@ -1349,15 +1339,8 @@ public void removeShare(OCShare share) { String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + AND + ProviderTableMeta._ID + " = ?"; String[] whereArgs = {account.name, Long.toString(share.getId())}; - if (getContentResolver() != null) { - getContentResolver().delete(contentUriShare, where, whereArgs); - } else { - try { - getContentProviderClient().delete(contentUriShare, where, whereArgs); - } catch (RemoteException e) { - Log_OC.d(TAG, e.getMessage(), e); - } - } + + deleteFiles(contentUriShare, where, whereArgs); } public void saveSharesDB(List shares) { @@ -1699,16 +1682,7 @@ public void saveCapabilities(OCCapability capability) { String[] selectionArgs = {account.name}; updateFiles(contentUriCapabilities, contentValues, where, selectionArgs, "Failed saveCapabilities update"); } else { - Uri resultUri = null; - if (getContentResolver() != null) { - resultUri = getContentResolver().insert(contentUriCapabilities, contentValues); - } else { - try { - resultUri = getContentProviderClient().insert(contentUriCapabilities, contentValues); - } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); - } - } + Uri resultUri = insertFile(contentUriCapabilities, contentValues); if (resultUri != null) { long newId = Long.parseLong(resultUri.getPathSegments().get(1)); @@ -1926,15 +1900,7 @@ public void deleteVirtuals(VirtualFolderType type) { String where = ProviderTableMeta.VIRTUAL_TYPE + " = ?"; String[] selectionArgs = {String.valueOf(type)}; - if (getContentResolver() != null) { - getContentResolver().delete(contentUriVirtual, where, selectionArgs); - } else { - try { - getContentProviderClient().delete(contentUriVirtual, where, selectionArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "deleteVirtuals" + e.getMessage(), e); - } - } + deleteFiles(contentUriVirtual, where, selectionArgs, "deleteVirtuals "); } public void saveVirtuals(List values) { @@ -1995,15 +1961,7 @@ public void deleteAllFiles() { + ProviderTableMeta.FILE_PATH + "= ?"; String[] whereArgs = new String[]{account.name, OCFile.ROOT_PATH}; - if (getContentResolver() != null) { - getContentResolver().delete(contentUriDir, where, whereArgs); - } else { - try { - getContentProviderClient().delete(contentUriDir, where, whereArgs); - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception in deleteAllFiles for account " + account.name + ": " + e.getMessage(), e); - } - } + deleteFiles(contentUriDir, where, whereArgs, "Exception in deleteAllFiles for account " + account.name + ": "); } private String getString(Cursor cursor, String columnName) { From 65acb834ef37a4c1f62c50b4fa0cd4bd2daf7be5 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 15:45:32 +0100 Subject: [PATCH 12/19] Finish extract deleteFiles and applyBatch Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 82 ++++++------------- 1 file changed, 26 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 4871b77719b6..3176096817dc 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -76,7 +76,6 @@ public class FileDataStorageManager { private static final String TAG = FileDataStorageManager.class.getSimpleName(); private static final String AND = " = ? AND "; - private static final String FAILED_TO_INSERT_MSG = "Fail to insert file to database "; private static final String FAILED_TO_UPDATE_MSG = "Fail to update file to database "; private static final String SENDING_TO_FILECONTENTPROVIDER_MSG = "Sending %d operations to FileContentProvider"; private static final String EXCEPTION_MSG = "Exception in batch of operations "; @@ -131,23 +130,26 @@ private Cursor executeQuery(Uri uri, String selection, String[] selectionArgs, S return executeQuery(uri, null, selection, selectionArgs, null, errorMessage); } - private void applyBatch(ArrayList operations, String errorMessage) { + private ContentProviderResult[] applyBatch(ArrayList operations, String errorMessage) { Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); + ContentProviderResult[] contentProviderResults = null; if (!operations.isEmpty()) { try { if (getContentResolver() != null) { - getContentResolver().applyBatch(MainApp.getAuthority(), operations); + contentProviderResults = getContentResolver().applyBatch(MainApp.getAuthority(), operations); } else { - getContentProviderClient().applyBatch(operations); + contentProviderResults = getContentProviderClient().applyBatch(operations); } } catch (OperationApplicationException | RemoteException e) { Log_OC.e(TAG, errorMessage + e.getMessage(), e); } } + + return contentProviderResults; } - private void applyBatch(ArrayList operations) { - applyBatch(operations, EXCEPTION_MSG); + private ContentProviderResult[] applyBatch(ArrayList operations) { + return applyBatch(operations, EXCEPTION_MSG); } private int updateFiles(Uri contentUri, ContentValues contentValues, String where, String[] selectionArgs, @@ -179,7 +181,7 @@ private Uri insertFile(Uri uri, ContentValues values) { try { resultUri = getContentProviderClient().insert(uri, values); } catch (RemoteException e) { - Log_OC.e(TAG, FAILED_TO_INSERT_MSG + e.getMessage(), e); + Log_OC.e(TAG, "Fail to insert file to database " + e.getMessage(), e); } } @@ -497,20 +499,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection .build()); // apply operations in batch - ContentProviderResult[] results = null; - Log_OC.d(TAG, String.format(Locale.ENGLISH, SENDING_TO_FILECONTENTPROVIDER_MSG, operations.size())); - - try { - ContentResolver contentResolver = getContentResolver(); - if (contentResolver != null) { - results = contentResolver.applyBatch(MainApp.getAuthority(), operations); - } else { - results = getContentProviderClient().applyBatch(operations); - } - - } catch (OperationApplicationException | RemoteException e) { - Log_OC.e(TAG, EXCEPTION_MSG + e.getMessage(), e); - } + ContentProviderResult[] results = applyBatch(operations); // update new id in file objects for insertions if (results != null) { @@ -1540,42 +1529,23 @@ public static void triggerMediaScan(String path) { public void deleteFileInMediaScan(String path) { String mimetypeString = FileStorageUtils.getMimeTypeFromName(path); - ContentResolver contentResolver = getContentResolver(); - String[] selectionArgs = {path}; - if (contentResolver != null) { - if (MimeTypeUtil.isImage(mimetypeString)) { - // Images - contentResolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - MediaStore.Images.Media.DATA + " = ?", selectionArgs); - } else if (MimeTypeUtil.isAudio(mimetypeString)) { - // Audio - contentResolver.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - MediaStore.Audio.Media.DATA + " = ?", selectionArgs); - } else if (MimeTypeUtil.isVideo(mimetypeString)) { - // Video - contentResolver.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, - MediaStore.Video.Media.DATA + " = ?", selectionArgs); - } - } else { - ContentProviderClient contentProviderClient = getContentProviderClient(); - try { - if (MimeTypeUtil.isImage(mimetypeString)) { - // Images - contentProviderClient.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, - MediaStore.Images.Media.DATA + " = ?", selectionArgs); - } else if (MimeTypeUtil.isAudio(mimetypeString)) { - // Audio - contentProviderClient.delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, - MediaStore.Audio.Media.DATA + " = ?", selectionArgs); - } else if (MimeTypeUtil.isVideo(mimetypeString)) { - // Video - contentProviderClient.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, - MediaStore.Video.Media.DATA + " = ?", selectionArgs); - } - } catch (RemoteException e) { - Log_OC.e(TAG, "Exception deleting media file in MediaStore " + e.getMessage(), e); - } + + if (MimeTypeUtil.isImage(mimetypeString)) { + deleteFiles(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, + MediaStore.Images.Media.DATA + " = ?", + selectionArgs, + "Exception deleting media file in MediaStore "); + } else if (MimeTypeUtil.isAudio(mimetypeString)) { + deleteFiles(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, + MediaStore.Audio.Media.DATA + " = ?", + selectionArgs, + "Exception deleting media file in MediaStore "); + } else if (MimeTypeUtil.isVideo(mimetypeString)) { + deleteFiles(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, + MediaStore.Video.Media.DATA + " = ?", + selectionArgs, + "Exception deleting media file in MediaStore "); } } From f303cd0dab0e8e4370374347656a4a48c82286be Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 17:16:05 +0100 Subject: [PATCH 13/19] Prepare remove duplication Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 80 ++++++++++--------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 3176096817dc..aa855c663925 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -522,54 +522,49 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } } - private ContentValues createContentValueForFile(OCFile folder) { + private ContentValues createContentValueForFile(OCFile ocFile) { ContentValues contentValues = new ContentValues(); - contentValues.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - folder.getModificationTimestampAtLastSyncForData()); - contentValues.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); - contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimeType()); - contentValues.put(ProviderTableMeta.FILE_NAME, folder.getFileName()); - contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); - contentValues.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); + ocFile.getModificationTimestampAtLastSyncForData()); + contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); + contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); + contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); - contentValues.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); - contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, folder.getEtagOnServer()); - contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); - contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); - contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); - contentValues.put(ProviderTableMeta.FILE_FAVORITE, folder.isFavorite()); - contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, folder.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, folder.getUnreadCommentsCount()); - contentValues.put(ProviderTableMeta.FILE_OWNER_ID, folder.getOwnerId()); - contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, folder.getOwnerDisplayName()); - contentValues.put(ProviderTableMeta.FILE_NOTE, folder.getNote()); - contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(folder.getSharees())); - contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, folder.getRichWorkspace()); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); + contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); + contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); + contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); + contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); + contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); + contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); + contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); + contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); + contentValues.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote()); + contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); + contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); + + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); + contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); return contentValues; } - private ContentValues createContentValueForFile(OCFile ocFile, OCFile folder) { + private ContentValues createContentValueForFile(OCFile ocFile, OCFile parentFolder) { ContentValues contentValues = new ContentValues(); contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, ocFile.getModificationTimestampAtLastSyncForData()); contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); - contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); - contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); - if (!ocFile.isFolder()) { - contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); - } contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); @@ -580,13 +575,8 @@ private ContentValues createContentValueForFile(OCFile ocFile, OCFile folder) { contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); - contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); - contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); - contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); - contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); @@ -594,6 +584,20 @@ private ContentValues createContentValueForFile(OCFile ocFile, OCFile folder) { contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); + contentValues.put(ProviderTableMeta.FILE_PARENT, parentFolder.getFileId()); + + + if (!ocFile.isFolder()) { + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); + } + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); + contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); + contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); + return contentValues; } From f47fa9404644be6531695c26fd2be935171771ca Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 18:05:52 +0100 Subject: [PATCH 14/19] Add old comment Signed-off-by: Joris Bodin --- .../com/owncloud/android/datamodel/FileDataStorageManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index aa855c663925..a5839fcb5cbb 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -550,7 +550,7 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); // FileContentProvider calculates the right size contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); return contentValues; From d7122e70e4d2b93d265ae6755aab57d1f2c49d8d Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 18:44:46 +0100 Subject: [PATCH 15/19] Remove createContentValueForFile doublon Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 55 ++++--------------- 1 file changed, 10 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index a5839fcb5cbb..da23c772f935 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -439,7 +439,8 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection // prepare operations to insert or update files to save in the given folder for (OCFile ocFile : updatedFiles) { - ContentValues contentValues = createContentValueForFile(ocFile, folder); + ContentValues contentValues = createContentValueForFile(ocFile); + contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); if (isFileExists(fileExistList, ocFile)) { long fileId; @@ -492,6 +493,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection // update metadata of folder ContentValues contentValues = createContentValueForFile(folder); + contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) .withValues(contentValues) @@ -528,6 +530,7 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, ocFile.getModificationTimestampAtLastSyncForData()); contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); + contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); @@ -536,6 +539,7 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); @@ -550,53 +554,14 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); // FileContentProvider calculates the right size - contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); - - return contentValues; - } - - private ContentValues createContentValueForFile(OCFile ocFile, OCFile parentFolder) { - ContentValues contentValues = new ContentValues(); - contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - ocFile.getModificationTimestampAtLastSyncForData()); - contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); - contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); - contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); - contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); - contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); - contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); - contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); - contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); - contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); - contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); - contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); - contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); - contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); - contentValues.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote()); - contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); - contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); - - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); - contentValues.put(ProviderTableMeta.FILE_PARENT, parentFolder.getFileId()); - - if (!ocFile.isFolder()) { contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); + contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); + contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); } - contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); - contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); - contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); - contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); - contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); - contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); return contentValues; } From 0d6114283d2e1451909c98aa217ca472a1638514 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Mon, 6 Jan 2020 19:04:14 +0100 Subject: [PATCH 16/19] Export createContentValue for saveFile() Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 77 ++++++------------- 1 file changed, 22 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index da23c772f935..5cf53da1166f 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -296,9 +296,7 @@ public List getFolderImages(OCFile folder, boolean onlyOnDevice) { return imageList; } - public boolean saveFile(OCFile ocFile) { - boolean overridden = false; - + private ContentValues createContentValueForFile(OCFile ocFile) { ContentValues contentValues = new ContentValues(); contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, @@ -307,26 +305,19 @@ public boolean saveFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); - contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); - contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); - if (!ocFile.isFolder()) { - contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); - } contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); + contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); - contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); - contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); - contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); + contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); @@ -334,6 +325,25 @@ public boolean saveFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); + if (!ocFile.isFolder()) { + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); + contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + } + + return contentValues; + } + + public boolean saveFile(OCFile ocFile) { + boolean overridden = false; + + ContentValues contentValues = createContentValueForFile(ocFile); + contentValues.put(ProviderTableMeta.FILE_PARENT, ocFile.getParentId()); + boolean sameRemotePath = fileExists(ocFile.getRemotePath()); if (sameRemotePath || fileExists(ocFile.getFileId())) { // for renamed files; no more delete and create @@ -524,49 +534,6 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection } } - private ContentValues createContentValueForFile(OCFile ocFile) { - ContentValues contentValues = new ContentValues(); - contentValues.put(ProviderTableMeta.FILE_MODIFIED, ocFile.getModificationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - ocFile.getModificationTimestampAtLastSyncForData()); - contentValues.put(ProviderTableMeta.FILE_CREATION, ocFile.getCreationTimestamp()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); - contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); - contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); - contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); - contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); - contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); - contentValues.put(ProviderTableMeta.FILE_ETAG, ocFile.getEtag()); - contentValues.put(ProviderTableMeta.FILE_ETAG_ON_SERVER, ocFile.getEtagOnServer()); - contentValues.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, ocFile.getEtagInConflict()); - contentValues.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, ocFile.isSharedViaLink() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, ocFile.isSharedWithSharee() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_PUBLIC_LINK, ocFile.getPublicLink()); - contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); - contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); - contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); - contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); - contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); - contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); - contentValues.put(ProviderTableMeta.FILE_NOTE, ocFile.getNote()); - contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); - contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); - - if (!ocFile.isFolder()) { - contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); - contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); - contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); - contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); - contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); - contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); - } - - return contentValues; - } - - public boolean removeFile(OCFile ocFile, boolean removeDBData, boolean removeLocalCopy) { boolean success = true; From 0960d083211ef2578450062524093e37e1e49005 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Tue, 7 Jan 2020 18:35:40 +0100 Subject: [PATCH 17/19] Fix when search file Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 5cf53da1166f..4cad238b5925 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -305,7 +305,12 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_CONTENT_LENGTH, ocFile.getFileLength()); contentValues.put(ProviderTableMeta.FILE_CONTENT_TYPE, ocFile.getMimeType()); contentValues.put(ProviderTableMeta.FILE_NAME, ocFile.getFileName()); + contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); contentValues.put(ProviderTableMeta.FILE_PATH, ocFile.getRemotePath()); + if (!ocFile.isFolder()) { + contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); + contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); + } contentValues.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, account.name); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, ocFile.getLastSyncDateForProperties()); contentValues.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, ocFile.getLastSyncDateForData()); @@ -318,6 +323,8 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_PERMISSIONS, ocFile.getPermissions()); contentValues.put(ProviderTableMeta.FILE_REMOTE_ID, ocFile.getRemoteId()); contentValues.put(ProviderTableMeta.FILE_FAVORITE, ocFile.isFavorite()); + contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); + contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); contentValues.put(ProviderTableMeta.FILE_UNREAD_COMMENTS_COUNT, ocFile.getUnreadCommentsCount()); contentValues.put(ProviderTableMeta.FILE_OWNER_ID, ocFile.getOwnerId()); contentValues.put(ProviderTableMeta.FILE_OWNER_DISPLAY_NAME, ocFile.getOwnerDisplayName()); @@ -325,16 +332,6 @@ private ContentValues createContentValueForFile(OCFile ocFile) { contentValues.put(ProviderTableMeta.FILE_SHAREES, new Gson().toJson(ocFile.getSharees())); contentValues.put(ProviderTableMeta.FILE_RICH_WORKSPACE, ocFile.getRichWorkspace()); - if (!ocFile.isFolder()) { - contentValues.put(ProviderTableMeta.FILE_ENCRYPTED_NAME, ocFile.getEncryptedFileName()); - contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); - contentValues.put(ProviderTableMeta.FILE_IS_DOWNLOADING, ocFile.isDownloading()); - contentValues.put(ProviderTableMeta.FILE_IS_ENCRYPTED, ocFile.isEncrypted()); - contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); - contentValues.put(ProviderTableMeta.FILE_STORAGE_PATH, ocFile.getStoragePath()); - contentValues.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, ocFile.isUpdateThumbnailNeeded()); - } - return contentValues; } @@ -451,6 +448,8 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection for (OCFile ocFile : updatedFiles) { ContentValues contentValues = createContentValueForFile(ocFile); contentValues.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); + contentValues.put(ProviderTableMeta.FILE_MOUNT_TYPE, ocFile.getMountType().ordinal()); + contentValues.put(ProviderTableMeta.FILE_HAS_PREVIEW, ocFile.isPreviewAvailable() ? 1 : 0); if (isFileExists(fileExistList, ocFile)) { long fileId; From 4ce14abb2c3ac931668d04d466438f80ae83a1f8 Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Fri, 10 Jan 2020 11:39:47 +0100 Subject: [PATCH 18/19] Fix bug on first connect Signed-off-by: Joris Bodin --- .../datamodel/FileDataStorageManager.java | 4 +- .../operations/RefreshFolderOperation.java | 10 ++--- .../providers/FileContentProvider.java | 37 ++++++++++++++++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index 4cad238b5925..e3c561741949 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -441,6 +441,7 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection + " children and " + filesToRemove.size() + " files to remove"); ArrayList operations = new ArrayList<>(updatedFiles.size()); + ArrayList operations = new ArrayList<>(updatedFiles.size() + filesToRemove.size()); ArrayList fileExistList = getFilesExistsID(updatedFiles); @@ -771,8 +772,7 @@ public void copyLocalFile(OCFile ocFile, String targetPath) { } } - public void migrateStoredFiles(String sourcePath, String destinationPath) - throws RemoteException, OperationApplicationException { + public void migrateStoredFiles(String sourcePath, String destinationPath) { Cursor cursor = executeQuery(ProviderTableMeta.CONTENT_URI_FILE, ProviderTableMeta.FILE_STORAGE_PATH + " IS NOT NULL", null, diff --git a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java index fa01cd58b827..21e9cd43e2f2 100644 --- a/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java +++ b/src/main/java/com/owncloud/android/operations/RefreshFolderOperation.java @@ -435,21 +435,21 @@ private void synchronizeData(List folderAndFiles) { OCFile remoteFile; OCFile localFile; OCFile updatedFile; - RemoteFile r; + RemoteFile remote; for (int i = 1; i < folderAndFiles.size(); i++) { /// new OCFile instance with the data from the server - r = (RemoteFile) folderAndFiles.get(i); - remoteFile = FileStorageUtils.fillOCFile(r); + remote = (RemoteFile) folderAndFiles.get(i); + remoteFile = FileStorageUtils.fillOCFile(remote); // new OCFile instance to merge fresh data from server with local state - updatedFile = FileStorageUtils.fillOCFile(r); + updatedFile = FileStorageUtils.fillOCFile(remote); updatedFile.setParentId(mLocalFolder.getFileId()); // retrieve local data for the read file localFile = localFilesMap.remove(remoteFile.getRemotePath()); - // TODO better implementation is need + // TODO better implementation is needed if (localFile == null) { localFile = mStorageManager.getFileByPath(updatedFile.getRemotePath()); } diff --git a/src/main/java/com/owncloud/android/providers/FileContentProvider.java b/src/main/java/com/owncloud/android/providers/FileContentProvider.java index b4b22550828a..21b9721fc1a6 100644 --- a/src/main/java/com/owncloud/android/providers/FileContentProvider.java +++ b/src/main/java/com/owncloud/android/providers/FileContentProvider.java @@ -266,14 +266,39 @@ private Uri insert(SQLiteDatabase db, Uri uri, ContentValues values) { switch (mUriMatcher.match(uri)) { case ROOT_DIRECTORY: case SINGLE_FILE: - Uri insertedFileUri; - long idFile = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values); - if (idFile > 0) { - insertedFileUri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, idFile); + String[] projection = new String[]{ + ProviderTableMeta._ID, ProviderTableMeta.FILE_PATH, + ProviderTableMeta.FILE_ACCOUNT_OWNER + }; + String where = ProviderTableMeta.FILE_PATH + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?"; + + String remotePath = values.getAsString(ProviderTableMeta.FILE_PATH); + String accountName = values.getAsString(ProviderTableMeta.FILE_ACCOUNT_OWNER); + String[] whereArgs = {remotePath, accountName}; + + Cursor doubleCheck = query(db, uri, projection, where, whereArgs, null); + // TODO better implementation is needed + // ugly patch; serious refactorization is needed to reduce work in + // FileDataStorageManager and bring it to FileContentProvider + if (doubleCheck.moveToFirst()) { + // file is already inserted; race condition, let's avoid a duplicated entry + Uri insertedFileUri = ContentUris.withAppendedId( + ProviderTableMeta.CONTENT_URI_FILE, + doubleCheck.getLong(doubleCheck.getColumnIndex(ProviderTableMeta._ID)) + ); + doubleCheck.close(); + + return insertedFileUri; } else { - throw new SQLException(ERROR + uri); + doubleCheck.close(); + + long rowId = db.insert(ProviderTableMeta.FILE_TABLE_NAME, null, values); + if (rowId > 0) { + return ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, rowId); + } else { + throw new SQLException(ERROR + uri); + } } - return insertedFileUri; case SHARES: Uri insertedShareUri; From 87855c1335cbdf4e18c77b0e3821eafe17ee2c1b Mon Sep 17 00:00:00 2001 From: Joris Bodin Date: Fri, 10 Jan 2020 15:02:45 +0100 Subject: [PATCH 19/19] Oups Signed-off-by: Joris Bodin --- .../com/owncloud/android/datamodel/FileDataStorageManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index e3c561741949..eb5bb6431b06 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -440,7 +440,6 @@ public void saveFolder(OCFile folder, ArrayList updatedFiles, Collection Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove"); - ArrayList operations = new ArrayList<>(updatedFiles.size()); ArrayList operations = new ArrayList<>(updatedFiles.size() + filesToRemove.size()); ArrayList fileExistList = getFilesExistsID(updatedFiles);