use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.
the class DocumentsStorageProvider method queryChildDocuments.
@SuppressLint("LongLogTag")
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException {
Log.d(TAG, "queryChildDocuments(), id=" + parentDocumentId);
Context context = getNonNullContext();
Document parentFolder = toDocument(parentDocumentId);
FileDataStorageManager storageManager = parentFolder.getStorageManager();
final FileCursor resultCursor = new FileCursor(projection);
for (OCFile file : storageManager.getFolderContent(parentFolder.getFile(), false)) {
resultCursor.addFile(new Document(storageManager, file));
}
boolean isLoading = false;
if (parentFolder.isExpired()) {
final ReloadFolderDocumentTask task = new ReloadFolderDocumentTask(parentFolder, result -> context.getContentResolver().notifyChange(toNotifyUri(parentFolder), null, false));
task.executeOnExecutor(executor);
resultCursor.setLoadingTask(task);
isLoading = true;
}
final Bundle extra = new Bundle();
extra.putBoolean(DocumentsContract.EXTRA_LOADING, isLoading);
resultCursor.setExtras(extra);
resultCursor.setNotificationUri(context.getContentResolver(), toNotifyUri(parentFolder));
return resultCursor;
}
use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.
the class FileDownloader method downloadFile.
/**
* Core download method: requests a file to download and stores it.
*
* @param downloadKey Key to access the download to perform, contained in mPendingDownloads
*/
private void downloadFile(String downloadKey) {
mStartedDownload = true;
mCurrentDownload = mPendingDownloads.get(downloadKey);
if (mCurrentDownload != null) {
// Detect if the account exists
if (accountManager.exists(mCurrentDownload.getAccount())) {
Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().name + " exists");
notifyDownloadStart(mCurrentDownload);
RemoteOperationResult downloadResult = null;
try {
// / prepare client object to send the request to the ownCloud server
Account currentDownloadAccount = mCurrentDownload.getAccount();
Optional<User> currentDownloadUser = accountManager.getUser(currentDownloadAccount.name);
if (!currentUser.equals(currentDownloadUser)) {
currentUser = currentDownloadUser;
mStorageManager = new FileDataStorageManager(currentUser.get(), getContentResolver());
}
// else, reuse storage manager from previous operation
// always get client from client manager, to get fresh credentials in case
// of update
OwnCloudAccount ocAccount = currentDownloadUser.get().toOwnCloudAccount();
mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
// / perform the download
downloadResult = mCurrentDownload.execute(mDownloadClient);
if (downloadResult.isSuccess()) {
saveDownloadedFile();
}
} catch (Exception e) {
Log_OC.e(TAG, "Error downloading", e);
downloadResult = new RemoteOperationResult(e);
} finally {
Pair<DownloadFileOperation, String> removeResult = mPendingDownloads.removePayload(mCurrentDownload.getUser().getAccountName(), mCurrentDownload.getRemotePath());
if (downloadResult == null) {
downloadResult = new RemoteOperationResult(new RuntimeException("Error downloading…"));
}
// / notify result
notifyDownloadResult(mCurrentDownload, downloadResult);
sendBroadcastDownloadFinished(mCurrentDownload, downloadResult, removeResult.second);
}
} else {
cancelPendingDownloads(mCurrentDownload.getUser().getAccountName());
}
}
}
use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.
the class FileUploader method retryUploads.
/**
* Retries a list of uploads.
*/
private void retryUploads(Intent intent, User user, List<String> requestedUploads) {
boolean onWifiOnly;
boolean whileChargingOnly;
OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
onWifiOnly = upload.isUseWifiOnly();
whileChargingOnly = upload.isWhileChargingOnly();
UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, null, upload, upload.getNameCollisionPolicy(), upload.getLocalAction(), this, onWifiOnly, whileChargingOnly, true, new FileDataStorageManager(user, getContentResolver()));
newUpload.addDataTransferProgressListener(this);
newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), upload.getRemotePath(), newUpload);
if (putResult != null) {
String uploadKey = putResult.first;
requestedUploads.add(uploadKey);
// Update upload in database
upload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
mUploadsStorageManager.updateUpload(upload);
}
}
use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.
the class FileUploader method startNewUpload.
/**
* Start a new {@link UploadFileOperation}.
*/
@SuppressLint("SdCardPath")
private void startNewUpload(User user, List<String> requestedUploads, boolean onWifiOnly, boolean whileChargingOnly, NameCollisionPolicy nameCollisionPolicy, int localAction, boolean isCreateRemoteFolder, int createdBy, OCFile file, boolean disableRetries) {
if (file.getStoragePath().startsWith("/data/data/")) {
Log_OC.d(TAG, "Upload from sensitive path is not allowed");
return;
}
OCUpload ocUpload = new OCUpload(file, user);
ocUpload.setFileSize(file.getFileLength());
ocUpload.setNameCollisionPolicy(nameCollisionPolicy);
ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
ocUpload.setCreatedBy(createdBy);
ocUpload.setLocalAction(localAction);
ocUpload.setUseWifiOnly(onWifiOnly);
ocUpload.setWhileChargingOnly(whileChargingOnly);
ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
UploadFileOperation newUpload = new UploadFileOperation(mUploadsStorageManager, connectivityService, powerManagementService, user, file, ocUpload, nameCollisionPolicy, localAction, this, onWifiOnly, whileChargingOnly, disableRetries, new FileDataStorageManager(user, getContentResolver()));
newUpload.setCreatedBy(createdBy);
if (isCreateRemoteFolder) {
newUpload.setRemoteFolderToBeCreated();
}
newUpload.addDataTransferProgressListener(this);
newUpload.addDataTransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(user.getAccountName(), file.getRemotePath(), newUpload);
if (putResult != null) {
requestedUploads.add(putResult.first);
// Save upload in database
long id = mUploadsStorageManager.storeUpload(ocUpload);
newUpload.setOCUploadId(id);
}
}
use of com.owncloud.android.datamodel.FileDataStorageManager in project android by nextcloud.
the class FileUploader method uploadFile.
/**
* Core upload method: sends the file(s) to upload
*
* @param uploadKey Key to access the upload to perform, contained in mPendingUploads
*/
public void uploadFile(String uploadKey) {
mCurrentUpload = mPendingUploads.get(uploadKey);
if (mCurrentUpload != null) {
// / Check account existence
if (!accountManager.exists(mCurrentUpload.getAccount())) {
Log_OC.w(TAG, "Account " + mCurrentUpload.getAccount().name + " does not exist anymore -> cancelling all its uploads");
cancelPendingUploads(mCurrentUpload.getUser().getAccountName());
return;
}
// / OK, let's upload
mUploadsStorageManager.updateDatabaseUploadStart(mCurrentUpload);
notifyUploadStart(mCurrentUpload);
sendBroadcastUploadStarted(mCurrentUpload);
RemoteOperationResult uploadResult = null;
try {
// / prepare client object to send the request to the ownCloud server
if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
mCurrentAccount = mCurrentUpload.getAccount();
mStorageManager = new FileDataStorageManager(getCurrentUser().get(), getContentResolver());
}
// else, reuse storage manager from previous operation
// always get client from client manager, to get fresh credentials in case of update
OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
// // If parent folder is encrypted, upload file encrypted
// OCFile parent = mStorageManager.getFileByPath(mCurrentUpload.getFile().getParentRemotePath());
// if (parent.isEncrypted()) {
// UploadEncryptedFileOperation uploadEncryptedFileOperation =
// new UploadEncryptedFileOperation(parent, mCurrentUpload);
//
// uploadResult = uploadEncryptedFileOperation.execute(mUploadClient, mStorageManager);
// } else {
// / perform the regular upload
uploadResult = mCurrentUpload.execute(mUploadClient);
// }
} catch (Exception e) {
Log_OC.e(TAG, "Error uploading", e);
uploadResult = new RemoteOperationResult(e);
} finally {
Pair<UploadFileOperation, String> removeResult;
if (mCurrentUpload.wasRenamed()) {
removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getOldFile().getRemotePath());
// TODO: grant that name is also updated for mCurrentUpload.getOCUploadId
} else {
removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getDecryptedRemotePath());
}
mUploadsStorageManager.updateDatabaseUploadResult(uploadResult, mCurrentUpload);
// / notify result
notifyUploadResult(mCurrentUpload, uploadResult);
sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
}
// generate new Thumbnail
Optional<User> user = getCurrentUser();
if (user.isPresent()) {
final ThumbnailsCacheManager.ThumbnailGenerationTask task = new ThumbnailsCacheManager.ThumbnailGenerationTask(mStorageManager, user.get());
File file = new File(mCurrentUpload.getOriginalStoragePath());
String remoteId = mCurrentUpload.getFile().getRemoteId();
task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(file, remoteId));
}
}
}
Aggregations