I want to check if a folder exists in the root of the linked Google Drive account. If it exists, delete a file in that folder and create a new one in there. If it doesn't, create the folder and then create a file in it.
This is what I'm doing to query:
private void checkIfBackupFolderIsInDrive(final File databaseFile) {
final Query backupFolderQuery = new Query.Builder()
.addFilter(Filters.and(
Filters.eq(SearchableField.TITLE, DATABASE_BACKUP_FOLDER_NAME),
Filters.eq(SearchableField.MIME_TYPE, DriveFolder.MIME_TYPE),
Filters.eq(SearchableField.TRASHED, false)))
.build();
mDriveResourceClient.getRootFolder()
.continueWithTask(task -> {
final DriveFolder rootFolder = task.getResult();
return mDriveResourceClient.queryChildren(rootFolder, backupFolderQuery);
})
.addOnSuccessListener(mActivity, metadataBuffer -> {
for (final Metadata metadata : metadataBuffer) {
final DriveFolder driveFolder = metadata.getDriveId().asDriveFolder();
final String title = metadata.getTitle();
final String mimeType = metadata.getMimeType();
if (title.equals(DATABASE_BACKUP_FOLDER_NAME) && mimeType.equals(DriveFolder.MIME_TYPE)) {
Utils.log("SettingsFragment", "Backup folder already exists");
deleteExistingDatabaseFileInFolder(driveFolder, databaseFile);
metadataBuffer.release();
return;
}
}
metadataBuffer.release();
createFolderToRootDriveFolder(databaseFile);
})
.addOnFailureListener(mActivity, e -> Utils.log("SettingsFragment", "Error retrieving files list: " + e.getMessage()));
}
As you can see, I'm already calling the release() method on the metadataBuffer but when I delete the recently created folder, even emptied from the Trash and then execute the query again, it keeps saying that the folder already exists.
If I do not empty the trash, the new file is created inside the trashed folder.
I am using this version of the drive API alongside with this version of the auth API:
implementation 'com.google.android.gms:play-services-auth:16.0.0'
implementation 'com.google.android.gms:play-services-drive:15.0.1'
This is driving me nuts. How can I perform a clean query to avoid that behaviour to happen?
Thank you very much in advance.
metadataBuffernot being cleared even when calling thereleasemethod. - anonymousOnSuccessListener:Utils.log("SettingsFragment", "Backup folder already exists");. So according to Google, everything went well. And it did, but the information is not up to date. But I already sorted it out. Take a look at my answer below. Thanks :) - anonymous