From: jabarros Date: Thu, 11 Dec 2014 08:21:42 +0000 (+0100) Subject: Merge pull request #797 from owncloud/download_folder__in_a_separate_worker_thread X-Git-Tag: oc-android-1.7.0_signed~23^2~29 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/6596d650679170c4da2b7f92ff79318aa106beff?hp=f89e06ec73e72ee38c2d9ed3d39cb0f6ba1bb72d Merge pull request #797 from owncloud/download_folder__in_a_separate_worker_thread Download folder in a separate worker thread --- diff --git a/src/com/owncloud/android/files/FileOperationsHelper.java b/src/com/owncloud/android/files/FileOperationsHelper.java index dc8d6b7b..bbef1f83 100644 --- a/src/com/owncloud/android/files/FileOperationsHelper.java +++ b/src/com/owncloud/android/files/FileOperationsHelper.java @@ -235,7 +235,6 @@ public class FileOperationsHelper { intent.setAction(OperationsService.ACTION_SYNC_FOLDER); intent.putExtra(OperationsService.EXTRA_ACCOUNT, mFileActivity.getAccount()); intent.putExtra(OperationsService.EXTRA_REMOTE_PATH, file.getRemotePath()); - intent.putExtra(OperationsService.EXTRA_SYNC_FILE_CONTENTS, true); mFileActivity.startService(intent); // reevaluating: with or without Binder? //mFileActivity.getOperationsServiceBinder().queueNewOperation(intent); } diff --git a/src/com/owncloud/android/operations/SyncFolderOperation.java b/src/com/owncloud/android/operations/SyncFolderOperation.java deleted file mode 100644 index 850c0b30..00000000 --- a/src/com/owncloud/android/operations/SyncFolderOperation.java +++ /dev/null @@ -1,523 +0,0 @@ -/* ownCloud Android client application - * Copyright (C) 2012-2014 ownCloud Inc. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package com.owncloud.android.operations; - -import android.accounts.Account; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import com.owncloud.android.datamodel.FileDataStorageManager; -import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.services.FileDownloader; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperation; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation; -import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; -import com.owncloud.android.lib.resources.files.RemoteFile; -import com.owncloud.android.operations.common.SyncOperation; -import com.owncloud.android.utils.FileStorageUtils; - -import org.apache.http.HttpStatus; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Vector; - -//import android.support.v4.content.LocalBroadcastManager; - - -/** - * Remote operation performing the synchronization of the list of files contained - * in a folder identified with its remote path. - * - * Fetches the list and properties of the files contained in the given folder, including their - * properties, and updates the local database with them. - * - * Does NOT enter in the child folders to synchronize their contents also. - * - * @author David A. Velasco - */ -public class SyncFolderOperation extends SyncOperation { - - private static final String TAG = SyncFolderOperation.class.getSimpleName(); - - /** Time stamp for the synchronization process in progress */ - private long mCurrentSyncTime; - - /** Remote folder to synchronize */ - private OCFile mLocalFolder; - - /** Access to the local database */ - private FileDataStorageManager mStorageManager; - - /** Account where the file to synchronize belongs */ - private Account mAccount; - - /** Android context; necessary to send requests to the download service */ - private Context mContext; - - /** Files and folders contained in the synchronized folder after a successful operation */ - private List mChildren; - - /** Counter of conflicts found between local and remote files */ - private int mConflictsFound; - - /** Counter of failed operations in synchronization of kept-in-sync files */ - private int mFailsInFavouritesFound; - - /** - * Map of remote and local paths to files that where locally stored in a location - * out of the ownCloud folder and couldn't be copied automatically into it - **/ - private Map mForgottenLocalFiles; - - /** 'True' means that the remote folder changed and should be fetched */ - private boolean mRemoteFolderChanged; - - - /** - * Creates a new instance of {@link SyncFolderOperation}. - * - * @param context Application context. - * @param remotePath Path to synchronize. - * @param account ownCloud account where the folder is located. - * @param currentSyncTime Time stamp for the synchronization process in progress. - */ - public SyncFolderOperation(Context context, String remotePath, Account account, long currentSyncTime){ - mLocalFolder = new OCFile(remotePath); - mCurrentSyncTime = currentSyncTime; - mStorageManager = getStorageManager(); - mAccount = account; - mContext = context; - mForgottenLocalFiles = new HashMap(); - mRemoteFolderChanged = false; - } - - - public int getConflictsFound() { - return mConflictsFound; - } - - public int getFailsInFavouritesFound() { - return mFailsInFavouritesFound; - } - - public Map getForgottenLocalFiles() { - return mForgottenLocalFiles; - } - - /** - * Returns the list of files and folders contained in the synchronized folder, - * if called after synchronization is complete. - * - * @return List of files and folders contained in the synchronized folder. - */ - public List getChildren() { - return mChildren; - } - - /** - * Performs the synchronization. - * - * {@inheritDoc} - */ - @Override - protected RemoteOperationResult run(OwnCloudClient client) { - RemoteOperationResult result = null; - mFailsInFavouritesFound = 0; - mConflictsFound = 0; - mForgottenLocalFiles.clear(); - - result = checkForChanges(client); - - if (result.isSuccess()) { - if (mRemoteFolderChanged) { - result = fetchAndSyncRemoteFolder(client); - } else { - mChildren = mStorageManager.getFolderContent(mLocalFolder); - } - } - - return result; - - } - - private RemoteOperationResult checkForChanges(OwnCloudClient client) { - mRemoteFolderChanged = true; - RemoteOperationResult result = null; - String remotePath = null; - - remotePath = mLocalFolder.getRemotePath(); - Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath); - - // remote request - ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath); - result = operation.execute(client); - if (result.isSuccess()){ - OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); - - // check if remote and local folder are different - mRemoteFolderChanged = - !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag())); - - result = new RemoteOperationResult(ResultCode.OK); - - Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + - (mRemoteFolderChanged ? "changed" : "not changed")); - - } else { - // check failed - if (result.getCode() == ResultCode.FILE_NOT_FOUND) { - removeLocalFolder(); - } - if (result.isException()) { - Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + - result.getLogMessage(), result.getException()); - } else { - Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + - result.getLogMessage()); - } - } - - return result; - } - - - private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) { - String remotePath = mLocalFolder.getRemotePath(); - ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath); - RemoteOperationResult result = operation.execute(client); - Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath); - - if (result.isSuccess()) { - synchronizeData(result.getData(), client); - if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) { - result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); - // should be a different result code, but will do the job - } - } else { - if (result.getCode() == ResultCode.FILE_NOT_FOUND) - removeLocalFolder(); - } - - return result; - } - - - private void removeLocalFolder() { - if (mStorageManager.fileExists(mLocalFolder.getFileId())) { - String currentSavePath = FileStorageUtils.getSavePath(mAccount.name); - mStorageManager.removeFolder( - mLocalFolder, - true, - ( mLocalFolder.isDown() && - mLocalFolder.getStoragePath().startsWith(currentSavePath) - ) - ); - } - } - - - /** - * Synchronizes the data retrieved from the server about the contents of the target folder - * with the current data in the local database. - * - * Grants that mChildren is updated with fresh data after execution. - * - * @param folderAndFiles Remote folder and children files in Folder - * - * @param client Client instance to the remote server where the data were - * retrieved. - * @return 'True' when any change was made in the local data, 'false' otherwise - */ - private void synchronizeData(ArrayList folderAndFiles, OwnCloudClient client) { - // get 'fresh data' from the database - mLocalFolder = mStorageManager.getFileByPath(mLocalFolder.getRemotePath()); - - // parse data from remote folder - OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0)); - remoteFolder.setParentId(mLocalFolder.getParentId()); - remoteFolder.setFileId(mLocalFolder.getFileId()); - - Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() - + " changed - starting update of local data "); - - List updatedFiles = new Vector(folderAndFiles.size() - 1); - List filesToSyncContents = new Vector(); - - // get current data about local contents of the folder to synchronize - List localFiles = mStorageManager.getFolderContent(mLocalFolder); - Map localFilesMap = new HashMap(localFiles.size()); - for (OCFile file : localFiles) { - localFilesMap.put(file.getRemotePath(), file); - } - - // loop to update every child - OCFile remoteFile = null, localFile = null; - for (int i=1; i filesToSyncContents, OwnCloudClient client - ) { - RemoteOperationResult contentsResult = null; - for (SynchronizeFileOperation op: filesToSyncContents) { - contentsResult = op.execute(mStorageManager, mContext); // async - if (!contentsResult.isSuccess()) { - if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) { - mConflictsFound++; - } else { - mFailsInFavouritesFound++; - if (contentsResult.getException() != null) { - Log_OC.e(TAG, "Error while synchronizing favourites : " - + contentsResult.getLogMessage(), contentsResult.getException()); - } else { - Log_OC.e(TAG, "Error while synchronizing favourites : " - + contentsResult.getLogMessage()); - } - } - } // won't let these fails break the synchronization process - } - } - - - public boolean isMultiStatus(int status) { - return (status == HttpStatus.SC_MULTI_STATUS); - } - - /** - * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server. - * - * @param remote remote file read from the server (remote file or folder). - * @return New OCFile instance representing the remote resource described by we. - */ - private OCFile fillOCFile(RemoteFile remote) { - OCFile file = new OCFile(remote.getRemotePath()); - file.setCreationTimestamp(remote.getCreationTimestamp()); - file.setFileLength(remote.getLength()); - file.setMimetype(remote.getMimeType()); - file.setModificationTimestamp(remote.getModifiedTimestamp()); - file.setEtag(remote.getEtag()); - file.setPermissions(remote.getPermissions()); - file.setRemoteId(remote.getRemoteId()); - return file; - } - - - /** - * Checks the storage path of the OCFile received as parameter. - * If it's out of the local ownCloud folder, tries to copy the file inside it. - * - * If the copy fails, the link to the local file is nullified. The account of forgotten - * files is kept in {@link #mForgottenLocalFiles} - *) - * @param file File to check and fix. - */ - private void checkAndFixForeignStoragePath(OCFile file) { - String storagePath = file.getStoragePath(); - String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file); - if (storagePath != null && !storagePath.equals(expectedPath)) { - /// fix storagePaths out of the local ownCloud folder - File originalFile = new File(storagePath); - if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) { - mForgottenLocalFiles.put(file.getRemotePath(), storagePath); - file.setStoragePath(null); - - } else { - InputStream in = null; - OutputStream out = null; - try { - File expectedFile = new File(expectedPath); - File expectedParent = expectedFile.getParentFile(); - expectedParent.mkdirs(); - if (!expectedParent.isDirectory()) { - throw new IOException( - "Unexpected error: parent directory could not be created" - ); - } - expectedFile.createNewFile(); - if (!expectedFile.isFile()) { - throw new IOException("Unexpected error: target file could not be created"); - } - in = new FileInputStream(originalFile); - out = new FileOutputStream(expectedFile); - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0){ - out.write(buf, 0, len); - } - file.setStoragePath(expectedPath); - - } catch (Exception e) { - Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e); - mForgottenLocalFiles.put(file.getRemotePath(), storagePath); - file.setStoragePath(null); - - } finally { - try { - if (in != null) in.close(); - } catch (Exception e) { - Log_OC.d(TAG, "Weird exception while closing input stream for " - + storagePath + " (ignoring)", e); - } - try { - if (out != null) out.close(); - } catch (Exception e) { - Log_OC.d(TAG, "Weird exception while closing output stream for " - + expectedPath + " (ignoring)", e); - } - } - } - } - } - - - /** - * Scans the default location for saving local copies of files searching for - * a 'lost' file with the same full name as the {@link com.owncloud.android.datamodel.OCFile} received as - * parameter. - * - * @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()); - } - } - } - - /** - * Requests for a download to the FileDownloader service - * - * @param file OCFile object representing the file to download - */ - private void requestForDownloadFile(OCFile file) { - Intent i = new Intent(mContext, FileDownloader.class); - i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); - i.putExtra(FileDownloader.EXTRA_FILE, file); - mContext.startService(i); - } - - public boolean getRemoteFolderChanged() { - return mRemoteFolderChanged; - } - -} diff --git a/src/com/owncloud/android/operations/SynchronizeFolderOperation.java b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java new file mode 100644 index 00000000..8b5a6814 --- /dev/null +++ b/src/com/owncloud/android/operations/SynchronizeFolderOperation.java @@ -0,0 +1,521 @@ +/* ownCloud Android client application + * Copyright (C) 2012-2014 ownCloud Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package com.owncloud.android.operations; + +import android.accounts.Account; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.files.services.FileDownloader; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.files.ReadRemoteFileOperation; +import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation; +import com.owncloud.android.lib.resources.files.RemoteFile; +import com.owncloud.android.operations.common.SyncOperation; +import com.owncloud.android.utils.FileStorageUtils; + +import org.apache.http.HttpStatus; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Vector; + +//import android.support.v4.content.LocalBroadcastManager; + + +/** + * Remote operation performing the synchronization of the list of files contained + * in a folder identified with its remote path. + * + * Fetches the list and properties of the files contained in the given folder, including their + * properties, and updates the local database with them. + * + * Does NOT enter in the child folders to synchronize their contents also. + * + * @author David A. Velasco + */ +public class SynchronizeFolderOperation extends SyncOperation { + + private static final String TAG = SynchronizeFolderOperation.class.getSimpleName(); + + /** Time stamp for the synchronization process in progress */ + private long mCurrentSyncTime; + + /** Remote folder to synchronize */ + private OCFile mLocalFolder; + + /** Account where the file to synchronize belongs */ + private Account mAccount; + + /** Android context; necessary to send requests to the download service */ + private Context mContext; + + /** Files and folders contained in the synchronized folder after a successful operation */ + private List mChildren; + + /** Counter of conflicts found between local and remote files */ + private int mConflictsFound; + + /** Counter of failed operations in synchronization of kept-in-sync files */ + private int mFailsInFavouritesFound; + + /** + * Map of remote and local paths to files that where locally stored in a location + * out of the ownCloud folder and couldn't be copied automatically into it + **/ + private Map mForgottenLocalFiles; + + /** 'True' means that the remote folder changed and should be fetched */ + private boolean mRemoteFolderChanged; + + + /** + * Creates a new instance of {@link SynchronizeFolderOperation}. + * + * @param context Application context. + * @param remotePath Path to synchronize. + * @param account ownCloud account where the folder is located. + * @param currentSyncTime Time stamp for the synchronization process in progress. + */ + public SynchronizeFolderOperation(Context context, String remotePath, Account account, long currentSyncTime){ + mLocalFolder = new OCFile(remotePath); + mCurrentSyncTime = currentSyncTime; + mAccount = account; + mContext = context; + mForgottenLocalFiles = new HashMap(); + mRemoteFolderChanged = false; + } + + + public int getConflictsFound() { + return mConflictsFound; + } + + public int getFailsInFavouritesFound() { + return mFailsInFavouritesFound; + } + + public Map getForgottenLocalFiles() { + return mForgottenLocalFiles; + } + + /** + * Returns the list of files and folders contained in the synchronized folder, + * if called after synchronization is complete. + * + * @return List of files and folders contained in the synchronized folder. + */ + public List getChildren() { + return mChildren; + } + + /** + * Performs the synchronization. + * + * {@inheritDoc} + */ + @Override + protected RemoteOperationResult run(OwnCloudClient client) { + RemoteOperationResult result = null; + mFailsInFavouritesFound = 0; + mConflictsFound = 0; + mForgottenLocalFiles.clear(); + + result = checkForChanges(client); + + if (result.isSuccess()) { + if (mRemoteFolderChanged) { + result = fetchAndSyncRemoteFolder(client); + } else { + mChildren = getStorageManager().getFolderContent(mLocalFolder); + } + } + + return result; + + } + + private RemoteOperationResult checkForChanges(OwnCloudClient client) { + mRemoteFolderChanged = true; + RemoteOperationResult result = null; + String remotePath = null; + + remotePath = mLocalFolder.getRemotePath(); + Log_OC.d(TAG, "Checking changes in " + mAccount.name + remotePath); + + // remote request + ReadRemoteFileOperation operation = new ReadRemoteFileOperation(remotePath); + result = operation.execute(client); + if (result.isSuccess()){ + OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) result.getData().get(0)); + + // check if remote and local folder are different + mRemoteFolderChanged = + !(remoteFolder.getEtag().equalsIgnoreCase(mLocalFolder.getEtag())); + + result = new RemoteOperationResult(ResultCode.OK); + + Log_OC.i(TAG, "Checked " + mAccount.name + remotePath + " : " + + (mRemoteFolderChanged ? "changed" : "not changed")); + + } else { + // check failed + if (result.getCode() == ResultCode.FILE_NOT_FOUND) { + removeLocalFolder(); + } + if (result.isException()) { + Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + + result.getLogMessage(), result.getException()); + } else { + Log_OC.e(TAG, "Checked " + mAccount.name + remotePath + " : " + + result.getLogMessage()); + } + } + + return result; + } + + + private RemoteOperationResult fetchAndSyncRemoteFolder(OwnCloudClient client) { + String remotePath = mLocalFolder.getRemotePath(); + ReadRemoteFolderOperation operation = new ReadRemoteFolderOperation(remotePath); + RemoteOperationResult result = operation.execute(client); + Log_OC.d(TAG, "Synchronizing " + mAccount.name + remotePath); + + if (result.isSuccess()) { + synchronizeData(result.getData(), client); + if (mConflictsFound > 0 || mFailsInFavouritesFound > 0) { + result = new RemoteOperationResult(ResultCode.SYNC_CONFLICT); + // should be a different result code, but will do the job + } + } else { + if (result.getCode() == ResultCode.FILE_NOT_FOUND) + removeLocalFolder(); + } + + return result; + } + + + private void removeLocalFolder() { + FileDataStorageManager storageManager = getStorageManager(); + if (storageManager.fileExists(mLocalFolder.getFileId())) { + String currentSavePath = FileStorageUtils.getSavePath(mAccount.name); + storageManager.removeFolder( + mLocalFolder, + true, + ( mLocalFolder.isDown() && + mLocalFolder.getStoragePath().startsWith(currentSavePath) + ) + ); + } + } + + + /** + * Synchronizes the data retrieved from the server about the contents of the target folder + * with the current data in the local database. + * + * Grants that mChildren is updated with fresh data after execution. + * + * @param folderAndFiles Remote folder and children files in Folder + * + * @param client Client instance to the remote server where the data were + * retrieved. + * @return 'True' when any change was made in the local data, 'false' otherwise + */ + private void synchronizeData(ArrayList folderAndFiles, OwnCloudClient client) { + FileDataStorageManager storageManager = getStorageManager(); + + // get 'fresh data' from the database + mLocalFolder = storageManager.getFileByPath(mLocalFolder.getRemotePath()); + + // parse data from remote folder + OCFile remoteFolder = fillOCFile((RemoteFile)folderAndFiles.get(0)); + remoteFolder.setParentId(mLocalFolder.getParentId()); + remoteFolder.setFileId(mLocalFolder.getFileId()); + + Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + + " changed - starting update of local data "); + + List updatedFiles = new Vector(folderAndFiles.size() - 1); + List filesToSyncContents = new Vector(); + + // get current data about local contents of the folder to synchronize + List localFiles = storageManager.getFolderContent(mLocalFolder); + Map localFilesMap = new HashMap(localFiles.size()); + for (OCFile file : localFiles) { + localFilesMap.put(file.getRemotePath(), file); + } + + // loop to update every child + OCFile remoteFile = null, localFile = null; + for (int i=1; i filesToSyncContents, OwnCloudClient client + ) { + RemoteOperationResult contentsResult = null; + for (SynchronizeFileOperation op: filesToSyncContents) { + contentsResult = op.execute(getStorageManager(), mContext); // async + if (!contentsResult.isSuccess()) { + if (contentsResult.getCode() == ResultCode.SYNC_CONFLICT) { + mConflictsFound++; + } else { + mFailsInFavouritesFound++; + if (contentsResult.getException() != null) { + Log_OC.e(TAG, "Error while synchronizing favourites : " + + contentsResult.getLogMessage(), contentsResult.getException()); + } else { + Log_OC.e(TAG, "Error while synchronizing favourites : " + + contentsResult.getLogMessage()); + } + } + } // won't let these fails break the synchronization process + } + } + + + public boolean isMultiStatus(int status) { + return (status == HttpStatus.SC_MULTI_STATUS); + } + + /** + * Creates and populates a new {@link com.owncloud.android.datamodel.OCFile} object with the data read from the server. + * + * @param remote remote file read from the server (remote file or folder). + * @return New OCFile instance representing the remote resource described by we. + */ + private OCFile fillOCFile(RemoteFile remote) { + OCFile file = new OCFile(remote.getRemotePath()); + file.setCreationTimestamp(remote.getCreationTimestamp()); + file.setFileLength(remote.getLength()); + file.setMimetype(remote.getMimeType()); + file.setModificationTimestamp(remote.getModifiedTimestamp()); + file.setEtag(remote.getEtag()); + file.setPermissions(remote.getPermissions()); + file.setRemoteId(remote.getRemoteId()); + return file; + } + + + /** + * Checks the storage path of the OCFile received as parameter. + * If it's out of the local ownCloud folder, tries to copy the file inside it. + * + * If the copy fails, the link to the local file is nullified. The account of forgotten + * files is kept in {@link #mForgottenLocalFiles} + *) + * @param file File to check and fix. + */ + private void checkAndFixForeignStoragePath(OCFile file) { + String storagePath = file.getStoragePath(); + String expectedPath = FileStorageUtils.getDefaultSavePathFor(mAccount.name, file); + if (storagePath != null && !storagePath.equals(expectedPath)) { + /// fix storagePaths out of the local ownCloud folder + File originalFile = new File(storagePath); + if (FileStorageUtils.getUsableSpace(mAccount.name) < originalFile.length()) { + mForgottenLocalFiles.put(file.getRemotePath(), storagePath); + file.setStoragePath(null); + + } else { + InputStream in = null; + OutputStream out = null; + try { + File expectedFile = new File(expectedPath); + File expectedParent = expectedFile.getParentFile(); + expectedParent.mkdirs(); + if (!expectedParent.isDirectory()) { + throw new IOException( + "Unexpected error: parent directory could not be created" + ); + } + expectedFile.createNewFile(); + if (!expectedFile.isFile()) { + throw new IOException("Unexpected error: target file could not be created"); + } + in = new FileInputStream(originalFile); + out = new FileOutputStream(expectedFile); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0){ + out.write(buf, 0, len); + } + file.setStoragePath(expectedPath); + + } catch (Exception e) { + Log_OC.e(TAG, "Exception while copying foreign file " + expectedPath, e); + mForgottenLocalFiles.put(file.getRemotePath(), storagePath); + file.setStoragePath(null); + + } finally { + try { + if (in != null) in.close(); + } catch (Exception e) { + Log_OC.d(TAG, "Weird exception while closing input stream for " + + storagePath + " (ignoring)", e); + } + try { + if (out != null) out.close(); + } catch (Exception e) { + Log_OC.d(TAG, "Weird exception while closing output stream for " + + expectedPath + " (ignoring)", e); + } + } + } + } + } + + + /** + * Scans the default location for saving local copies of files searching for + * a 'lost' file with the same full name as the {@link com.owncloud.android.datamodel.OCFile} received as + * parameter. + * + * @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()); + } + } + } + + /** + * Requests for a download to the FileDownloader service + * + * @param file OCFile object representing the file to download + */ + private void requestForDownloadFile(OCFile file) { + Intent i = new Intent(mContext, FileDownloader.class); + i.putExtra(FileDownloader.EXTRA_ACCOUNT, mAccount); + i.putExtra(FileDownloader.EXTRA_FILE, file); + mContext.startService(i); + } + + public boolean getRemoteFolderChanged() { + return mRemoteFolderChanged; + } + +} diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index 80efec65..02462df4 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@ -48,6 +48,7 @@ import com.owncloud.android.operations.OAuth2GetAccessToken; import com.owncloud.android.operations.RemoveFileOperation; import com.owncloud.android.operations.RenameFileOperation; import com.owncloud.android.operations.SynchronizeFileOperation; +import com.owncloud.android.operations.SynchronizeFolderOperation; import com.owncloud.android.operations.UnshareLinkOperation; import android.accounts.Account; @@ -106,9 +107,6 @@ public class OperationsService extends Service { public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED"; - private ConcurrentLinkedQueue> mPendingOperations = - new ConcurrentLinkedQueue>(); - private ConcurrentMap> mUndispatchedFinishedOperations = new ConcurrentHashMap>(); @@ -132,14 +130,10 @@ public class OperationsService extends Service { } } - private Looper mServiceLooper; - private ServiceHandler mServiceHandler; - private OperationsServiceBinder mBinder; - private OwnCloudClient mOwnCloudClient = null; - private Target mLastTarget = null; - private FileDataStorageManager mStorageManager; - private RemoteOperation mCurrentOperation = null; + private ServiceHandler mOperationsHandler; + private OperationsServiceBinder mOperationsBinder; + private ServiceHandler mSyncFolderHandler; /** * Service initialization @@ -147,11 +141,16 @@ public class OperationsService extends Service { @Override public void onCreate() { super.onCreate(); - HandlerThread thread = new HandlerThread("Operations service thread", Process.THREAD_PRIORITY_BACKGROUND); + /// First worker thread for most of operations + HandlerThread thread = new HandlerThread("Operations thread", Process.THREAD_PRIORITY_BACKGROUND); + thread.start(); + mOperationsHandler = new ServiceHandler(thread.getLooper(), this); + mOperationsBinder = new OperationsServiceBinder(mOperationsHandler); + + /// Separated worker thread for download of folders (WIP) + thread = new HandlerThread("Syncfolder thread", Process.THREAD_PRIORITY_BACKGROUND); thread.start(); - mServiceLooper = thread.getLooper(); - mServiceHandler = new ServiceHandler(mServiceLooper, this); - mBinder = new OperationsServiceBinder(); + mSyncFolderHandler = new ServiceHandler(thread.getLooper(), this); } @@ -168,13 +167,18 @@ public class OperationsService extends Service { // the Binder Pair itemToQueue = newOperation(intent); if (itemToQueue != null) { - mPendingOperations.add(itemToQueue); + mSyncFolderHandler.mPendingOperations.add(itemToQueue); + Message msg = mSyncFolderHandler.obtainMessage(); + msg.arg1 = startId; + mSyncFolderHandler.sendMessage(msg); } + + } else { + Message msg = mOperationsHandler.obtainMessage(); + msg.arg1 = startId; + mOperationsHandler.sendMessage(msg); } - Message msg = mServiceHandler.obtainMessage(); - msg.arg1 = startId; - mServiceHandler.sendMessage(msg); return START_NOT_STICKY; } @@ -212,7 +216,7 @@ public class OperationsService extends Service { @Override public IBinder onBind(Intent intent) { //Log_OC.wtf(TAG, "onBind" ); - return mBinder; + return mOperationsBinder; } @@ -221,7 +225,7 @@ public class OperationsService extends Service { */ @Override public boolean onUnbind(Intent intent) { - ((OperationsServiceBinder)mBinder).clearListeners(); + ((OperationsServiceBinder)mOperationsBinder).clearListeners(); return false; // not accepting rebinding (default behaviour) } @@ -239,6 +243,14 @@ public class OperationsService extends Service { private ConcurrentMap mBoundListeners = new ConcurrentHashMap(); + private ServiceHandler mServiceHandler = null; + + + public OperationsServiceBinder(ServiceHandler serviceHandler) { + mServiceHandler = serviceHandler; + } + + /** * Cancels an operation * @@ -286,7 +298,7 @@ public class OperationsService extends Service { * @return 'True' when an operation that enforces the user to wait for completion is in process. */ public boolean isPerformingBlockingOperation() { - return (!mPendingOperations.isEmpty()); + return (!mServiceHandler.mPendingOperations.isEmpty()); } @@ -301,7 +313,7 @@ public class OperationsService extends Service { public long queueNewOperation(Intent operationIntent) { Pair itemToQueue = newOperation(operationIntent); if (itemToQueue != null) { - mPendingOperations.add(itemToQueue); + mServiceHandler.mPendingOperations.add(itemToQueue); startService(new Intent(OperationsService.this, OperationsService.class)); return itemToQueue.second.hashCode(); @@ -319,7 +331,7 @@ public class OperationsService extends Service { return true; //Log_OC.wtf(TAG, "Sending callback later"); } else { - if (!mPendingOperations.isEmpty()) { + if (!mServiceHandler.mPendingOperations.isEmpty()) { return true; } else { return false; @@ -338,7 +350,19 @@ public class OperationsService extends Service { */ private static class ServiceHandler extends Handler { // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak + + OperationsService mService; + + + private ConcurrentLinkedQueue> mPendingOperations = + new ConcurrentLinkedQueue>(); + private RemoteOperation mCurrentOperation = null; + private Target mLastTarget = null; + private OwnCloudClient mOwnCloudClient = null; + private FileDataStorageManager mStorageManager; + + public ServiceHandler(Looper looper, OperationsService service) { super(looper); if (service == null) { @@ -349,15 +373,116 @@ public class OperationsService extends Service { @Override public void handleMessage(Message msg) { - mService.nextOperation(); + nextOperation(); mService.stopSelf(msg.arg1); } + + + /** + * Performs the next operation in the queue + */ + private void nextOperation() { + + //Log_OC.wtf(TAG, "nextOperation init" ); + + Pair next = null; + synchronized(mPendingOperations) { + next = mPendingOperations.peek(); + } + + if (next != null) { + + mCurrentOperation = next.second; + RemoteOperationResult result = null; + try { + /// prepare client object to send the request to the ownCloud server + if (mLastTarget == null || !mLastTarget.equals(next.first)) { + mLastTarget = next.first; + if (mLastTarget.mAccount != null) { + OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, mService); + mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(ocAccount, mService); + mStorageManager = new FileDataStorageManager( + mLastTarget.mAccount, + mService.getContentResolver() + ); + } else { + OwnCloudCredentials credentials = null; + if (mLastTarget.mUsername != null && + mLastTarget.mUsername.length() > 0) { + credentials = OwnCloudCredentialsFactory.newBasicCredentials( + mLastTarget.mUsername, + mLastTarget.mPassword); // basic + + } else if (mLastTarget.mAuthToken != null && + mLastTarget.mAuthToken.length() > 0) { + credentials = OwnCloudCredentialsFactory.newBearerCredentials( + mLastTarget.mAuthToken); // bearer token + + } else if (mLastTarget.mCookie != null && + mLastTarget.mCookie.length() > 0) { + credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials( + mLastTarget.mCookie); // SAML SSO + } + OwnCloudAccount ocAccount = new OwnCloudAccount( + mLastTarget.mServerUrl, credentials); + mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(ocAccount, mService); + mStorageManager = null; + } + } + + /// perform the operation + if (mCurrentOperation instanceof SyncOperation) { + result = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient, mStorageManager); + } else { + result = mCurrentOperation.execute(mOwnCloudClient); + } + + } catch (AccountsException e) { + if (mLastTarget.mAccount == null) { + Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e); + } else { + Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e); + } + result = new RemoteOperationResult(e); + + } catch (IOException e) { + if (mLastTarget.mAccount == null) { + Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e); + } else { + Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e); + } + result = new RemoteOperationResult(e); + } catch (Exception e) { + if (mLastTarget.mAccount == null) { + Log_OC.e(TAG, "Unexpected error for a NULL account", e); + } else { + Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e); + } + result = new RemoteOperationResult(e); + + } finally { + synchronized(mPendingOperations) { + mPendingOperations.poll(); + } + } + + //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result); + mService.dispatchResultToOperationListeners(mLastTarget, mCurrentOperation, result); + } + } + + + } /** * Creates a new operation, as described by operationIntent. * + * TODO - move to ServiceHandler (probably) + * * @param operationIntent Intent describing a new operation to queue and execute. * @return Pair with the new operation object and the information about its target server. */ @@ -455,12 +580,12 @@ public class OperationsService extends Service { } else if (action.equals(ACTION_SYNC_FOLDER)) { // Sync file String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH); - boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true); - /* TODO - merge code for new SynchronizeFolderOperation operation = new SynchronizeFolderOperation( - + this, // TODO remove this dependency from construction time + remotePath, + account, + System.currentTimeMillis() // TODO remove this dependency from construction time ); - */ } else if (action.equals(ACTION_MOVE_FILE)) { // Move file/folder @@ -485,102 +610,6 @@ public class OperationsService extends Service { /** - * Performs the next operation in the queue - */ - private void nextOperation() { - - //Log_OC.wtf(TAG, "nextOperation init" ); - - Pair next = null; - synchronized(mPendingOperations) { - next = mPendingOperations.peek(); - } - - if (next != null) { - - mCurrentOperation = next.second; - RemoteOperationResult result = null; - try { - /// prepare client object to send the request to the ownCloud server - if (mLastTarget == null || !mLastTarget.equals(next.first)) { - mLastTarget = next.first; - if (mLastTarget.mAccount != null) { - OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, this); - mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, this); - mStorageManager = - new FileDataStorageManager( - mLastTarget.mAccount, - getContentResolver()); - } else { - OwnCloudCredentials credentials = null; - if (mLastTarget.mUsername != null && - mLastTarget.mUsername.length() > 0) { - credentials = OwnCloudCredentialsFactory.newBasicCredentials( - mLastTarget.mUsername, - mLastTarget.mPassword); // basic - - } else if (mLastTarget.mAuthToken != null && - mLastTarget.mAuthToken.length() > 0) { - credentials = OwnCloudCredentialsFactory.newBearerCredentials( - mLastTarget.mAuthToken); // bearer token - - } else if (mLastTarget.mCookie != null && - mLastTarget.mCookie.length() > 0) { - credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials( - mLastTarget.mCookie); // SAML SSO - } - OwnCloudAccount ocAccount = new OwnCloudAccount( - mLastTarget.mServerUrl, credentials); - mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton(). - getClientFor(ocAccount, this); - mStorageManager = null; - } - } - - /// perform the operation - if (mCurrentOperation instanceof SyncOperation) { - result = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient, mStorageManager); - } else { - result = mCurrentOperation.execute(mOwnCloudClient); - } - - } catch (AccountsException e) { - if (mLastTarget.mAccount == null) { - Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e); - } else { - Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e); - } - result = new RemoteOperationResult(e); - - } catch (IOException e) { - if (mLastTarget.mAccount == null) { - Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e); - } else { - Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e); - } - result = new RemoteOperationResult(e); - } catch (Exception e) { - if (mLastTarget.mAccount == null) { - Log_OC.e(TAG, "Unexpected error for a NULL account", e); - } else { - Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e); - } - result = new RemoteOperationResult(e); - - } finally { - synchronized(mPendingOperations) { - mPendingOperations.poll(); - } - } - - //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result); - dispatchResultToOperationListeners(mLastTarget, mCurrentOperation, result); - } - } - - - /** * Sends a broadcast when a new operation is added to the queue. * * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\ @@ -636,10 +665,10 @@ public class OperationsService extends Service { private void dispatchResultToOperationListeners( Target target, final RemoteOperation operation, final RemoteOperationResult result) { int count = 0; - Iterator listeners = mBinder.mBoundListeners.keySet().iterator(); + Iterator listeners = mOperationsBinder.mBoundListeners.keySet().iterator(); while (listeners.hasNext()) { final OnRemoteOperationListener listener = listeners.next(); - final Handler handler = mBinder.mBoundListeners.get(listener); + final Handler handler = mOperationsBinder.mBoundListeners.get(listener); if (handler != null) { handler.post(new Runnable() { @Override