use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class FileUploader method onStartCommand.
/**
* Entry point to add one or several files to the queue of uploads.
*
* New uploads are added calling to startService(), resulting in a call to
* this method. This ensures the service will keep on working although the
* caller activity goes away.
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log_OC.d(TAG, "Starting command with id " + startId);
boolean retry = intent.getBooleanExtra(KEY_RETRY, false);
AbstractList<String> requestedUploads = new Vector<String>();
if (!intent.hasExtra(KEY_ACCOUNT)) {
Log_OC.e(TAG, "Not enough information provided in intent");
return Service.START_NOT_STICKY;
}
Account account = intent.getParcelableExtra(KEY_ACCOUNT);
if (!AccountUtils.exists(account, getApplicationContext())) {
return Service.START_NOT_STICKY;
}
OwnCloudVersion ocv = AccountUtils.getServerVersion(account);
boolean chunked = ocv.isChunkedUploadSupported();
if (!retry) {
if (!(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
Log_OC.e(TAG, "Not enough information provided in intent");
return Service.START_NOT_STICKY;
}
String[] localPaths = null, remotePaths = null, mimeTypes = null;
OCFile[] files = null;
if (intent.hasExtra(KEY_FILE)) {
Parcelable[] files_temp = intent.getParcelableArrayExtra(KEY_FILE);
files = new OCFile[files_temp.length];
System.arraycopy(files_temp, 0, files, 0, files_temp.length);
} else {
localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
}
boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_FORGET);
boolean isCreateRemoteFolder = intent.getBooleanExtra(KEY_CREATE_REMOTE_FOLDER, false);
int createdBy = intent.getIntExtra(KEY_CREATED_BY, UploadFileOperation.CREATED_BY_USER);
if (intent.hasExtra(KEY_FILE) && files == null) {
Log_OC.e(TAG, "Incorrect array for OCFiles provided in upload intent");
return Service.START_NOT_STICKY;
} else if (!intent.hasExtra(KEY_FILE)) {
if (localPaths == null) {
Log_OC.e(TAG, "Incorrect array for local paths provided in upload intent");
return Service.START_NOT_STICKY;
}
if (remotePaths == null) {
Log_OC.e(TAG, "Incorrect array for remote paths provided in upload intent");
return Service.START_NOT_STICKY;
}
if (localPaths.length != remotePaths.length) {
Log_OC.e(TAG, "Different number of remote paths and local paths!");
return Service.START_NOT_STICKY;
}
files = new OCFile[localPaths.length];
for (int i = 0; i < localPaths.length; i++) {
files[i] = UploadFileOperation.obtainNewOCFileToUpload(remotePaths[i], localPaths[i], ((mimeTypes != null) ? mimeTypes[i] : null));
if (files[i] == null) {
Log_OC.e(TAG, "obtainNewOCFileToUpload() returned null for remotePaths[i]:" + remotePaths[i] + " and localPaths[i]:" + localPaths[i]);
return Service.START_NOT_STICKY;
}
}
}
// at this point variable "OCFile[] files" is loaded correctly.
String uploadKey = null;
UploadFileOperation newUpload = null;
try {
for (int i = 0; i < files.length; i++) {
OCUpload ocUpload = new OCUpload(files[i], account);
ocUpload.setFileSize(files[i].getFileLength());
ocUpload.setForceOverwrite(forceOverwrite);
ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
ocUpload.setCreatedBy(createdBy);
ocUpload.setLocalAction(localAction);
/*ocUpload.setUseWifiOnly(isUseWifiOnly);
ocUpload.setWhileChargingOnly(isWhileChargingOnly);*/
ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
newUpload = new UploadFileOperation(account, files[i], ocUpload, chunked, forceOverwrite, localAction, this);
newUpload.setCreatedBy(createdBy);
if (isCreateRemoteFolder) {
newUpload.setRemoteFolderToBeCreated();
}
newUpload.addDatatransferProgressListener(this);
newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(account.name, files[i].getRemotePath(), newUpload);
if (putResult != null) {
uploadKey = putResult.first;
requestedUploads.add(uploadKey);
// Save upload in database
long id = mUploadsStorageManager.storeUpload(ocUpload);
newUpload.setOCUploadId(id);
}
}
} catch (IllegalArgumentException e) {
Log_OC.e(TAG, "Not enough information provided in intent: " + e.getMessage());
return START_NOT_STICKY;
} catch (IllegalStateException e) {
Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
return START_NOT_STICKY;
} catch (Exception e) {
Log_OC.e(TAG, "Unexpected exception while processing upload intent", e);
return START_NOT_STICKY;
}
// *** TODO REWRITE: block inserted to request A retry; too many code copied, no control exception ***/
} else {
if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_RETRY_UPLOAD)) {
Log_OC.e(TAG, "Not enough information provided in intent: no KEY_RETRY_UPLOAD_KEY");
return START_NOT_STICKY;
}
OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
UploadFileOperation newUpload = new UploadFileOperation(account, null, upload, chunked, // TODO should be read from DB?
upload.isForceOverwrite(), // TODO should be read from DB?
upload.getLocalAction(), this);
newUpload.addDatatransferProgressListener(this);
newUpload.addDatatransferProgressListener((FileUploaderBinder) mBinder);
newUpload.addRenameUploadListener(this);
Pair<String, String> putResult = mPendingUploads.putIfAbsent(account.name, 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);
}
}
if (requestedUploads.size() > 0) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = requestedUploads;
mServiceHandler.sendMessage(msg);
sendBroadcastUploadsAdded();
}
return Service.START_NOT_STICKY;
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class SynchronizeFolderOperation method searchForLocalFileInDefaultPath.
/**
/**
* Scans the default location for saving local copies of files searching for
* a 'lost' file with the same full name as the {@link OCFile} received as
* parameter.
*
* This method helps to keep linked local copies of the files when the app is uninstalled, and then
* reinstalled in the device. OR after the cache of the app was deleted in system settings.
*
* The method is assuming that all the local changes in the file where synchronized in the past. This is dangerous,
* but assuming the contrary could lead to massive unnecessary synchronizations of downloaded file after deleting
* the app cache.
*
* This should be changed in the near future to avoid any chance of data loss, but we need to add some options
* to limit hard automatic synchronizations to wifi, unless the user wants otherwise.
*
* @param file File to associate a possible 'lost' local file.
*/
private void searchForLocalFileInDefaultPath(OCFile file) {
if (file.getStoragePath() == null && !file.isFolder()) {
File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
if (f.exists()) {
file.setStoragePath(f.getAbsolutePath());
file.setLastSyncDateForData(f.lastModified());
}
}
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class SynchronizeFolderOperation method mergeRemoteFolder.
/**
* Synchronizes the data retrieved from the server about the contents of the target folder
* with the current data in the local database.
*
* Grants that mFoldersToVisit is updated with fresh data after execution.
*
* @param folderAndFiles Remote folder and children files in folder
*/
private void mergeRemoteFolder(ArrayList<Object> folderAndFiles) throws OperationCancelledException {
Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
FileDataStorageManager storageManager = getStorageManager();
// parse data from remote folder
OCFile updatedFolder = FileStorageUtils.createOCFileFrom((RemoteFile) folderAndFiles.get(0));
// NOTE: updates ETag with remote value; that's INTENDED
updatedFolder.copyLocalPropertiesFrom(mLocalFolder);
Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
List<OCFile> updatedFiles = new Vector<>(folderAndFiles.size() - 1);
mFoldersToVisit = new Vector<>(folderAndFiles.size() - 1);
mFilesToSyncContents.clear();
if (mCancellationRequested.get()) {
throw new OperationCancelledException();
}
// get current data about local contents of the folder to synchronize
List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
for (OCFile file : localFiles) {
localFilesMap.put(file.getRemotePath(), file);
}
// loop to synchronize every child
OCFile remoteFile, localFile, updatedLocalFile;
RemoteFile r;
int foldersToExpand = 0;
for (int i = 1; i < folderAndFiles.size(); i++) {
/// new OCFile instance with the data from the server
r = (RemoteFile) folderAndFiles.get(i);
remoteFile = FileStorageUtils.createOCFileFrom(r);
/// new OCFile instance to merge fresh data from server with local state
updatedLocalFile = FileStorageUtils.createOCFileFrom(r);
/// retrieve local data for the read file
localFile = localFilesMap.remove(remoteFile.getRemotePath());
/// add to updatedFile data about LOCAL STATE (not existing in server)
updatedLocalFile.setLastSyncDateForProperties(mCurrentSyncTime);
if (localFile != null) {
updatedLocalFile.copyLocalPropertiesFrom(localFile);
// remote eTag will not be set unless file CONTENTS are synchronized
updatedLocalFile.setEtag(localFile.getEtag());
if (!updatedLocalFile.isFolder() && remoteFile.isImage() && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
updatedLocalFile.setNeedsUpdateThumbnail(true);
}
} else {
updatedLocalFile.setParentId(mLocalFolder.getFileId());
// remote eTag will not be set unless file CONTENTS are synchronized
updatedLocalFile.setEtag("");
// new files need to check av-off status of parent folder!
if (updatedFolder.isAvailableOffline()) {
updatedLocalFile.setAvailableOfflineStatus(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT);
}
}
/// check and fix, if needed, local storage path
searchForLocalFileInDefaultPath(updatedLocalFile);
/// prepare content synchronizations
boolean serverUnchanged = addToSyncContents(updatedLocalFile, remoteFile);
if (updatedLocalFile.isFolder() && !serverUnchanged) {
foldersToExpand++;
}
updatedFiles.add(updatedLocalFile);
}
// save updated contents in local database
if (foldersToExpand == 0) {
updatedFolder.setTreeEtag(updatedFolder.getEtag());
// TODO - propagate up
}
storageManager.saveFolder(updatedFolder, updatedFiles, localFilesMap.values());
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class FileDownloader method saveDownloadedFile.
/**
* Updates the OC File after a successful download.
*
* TODO move to DownloadFileOperation
*/
private void saveDownloadedFile() {
OCFile file = mStorageManager.getFileById(mCurrentDownload.getFile().getFileId());
long syncDate = System.currentTimeMillis();
file.setLastSyncDateForProperties(syncDate);
file.setLastSyncDateForData(syncDate);
file.setNeedsUpdateThumbnail(true);
file.setModificationTimestamp(mCurrentDownload.getModificationTimestamp());
file.setModificationTimestampAtLastSyncForData(mCurrentDownload.getModificationTimestamp());
file.setEtag(mCurrentDownload.getEtag());
file.setMimetype(mCurrentDownload.getMimeType());
file.setStoragePath(mCurrentDownload.getSavePath());
file.setFileLength((new File(mCurrentDownload.getSavePath()).length()));
file.setRemoteId(mCurrentDownload.getFile().getRemoteId());
mStorageManager.saveFile(file);
mStorageManager.triggerMediaScan(file.getStoragePath());
mStorageManager.saveConflict(file, null);
}
use of com.owncloud.android.datamodel.OCFile in project android by owncloud.
the class CreateFolderOperation method run.
@Override
protected RemoteOperationResult run(OwnCloudClient client) {
CreateRemoteFolderOperation operation = new CreateRemoteFolderOperation(mRemotePath, mCreateFullPath);
RemoteOperationResult result = operation.execute(client);
if (result.isSuccess()) {
OCFile newDir = saveFolderInDB();
String localPath = FileStorageUtils.getDefaultSavePathFor(getStorageManager().getAccount().name, newDir);
File localFile = new File(localPath);
boolean created = localFile.mkdirs();
if (!created) {
Log_OC.w(TAG, "Local folder " + localPath + " was not fully created");
}
} else {
Log_OC.e(TAG, mRemotePath + "hasn't been created");
}
return result;
}
Aggregations