+
+
+ /**
+ * SyncFolder worker. Performs the pending operations in the order they were requested.
+ *
+ * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
+ */
+ private static class SyncFolderHandler 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 ConcurrentMap<String,SynchronizeFolderOperation> mPendingOperations =
+ new ConcurrentHashMap<String,SynchronizeFolderOperation>();
+ private RemoteOperation mCurrentOperation = null;
+ private OwnCloudClient mOwnCloudClient = null;
+ private FileDataStorageManager mStorageManager;
+ private SynchronizeFolderOperation mCurrentSyncOperation;
+
+
+ public SyncFolderHandler(Looper looper, OperationsService service) {
+ super(looper);
+ if (service == null) {
+ throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
+ }
+ mService = service;
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ Pair<Account, OCFile> itemSyncKey = (Pair<Account, OCFile>) msg.obj;
+ nextOperation(itemSyncKey.first,itemSyncKey.second);
+ mService.stopSelf(msg.arg1);
+ }
+
+
+ /**
+ * Performs the next operation in the queue
+ */
+ private void nextOperation(Account account, OCFile file) {
+
+ String syncKey = buildRemoteName(account,file);
+
+ synchronized(mPendingOperations) {
+ mCurrentSyncOperation = mPendingOperations.get(syncKey);
+ }
+
+ if (mCurrentSyncOperation != null) {
+
+ RemoteOperationResult operationResult = null;
+ try {
+
+ OwnCloudAccount ocAccount = new OwnCloudAccount(account, mService);
+ mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
+ getClientFor(ocAccount, mService);
+ mStorageManager = new FileDataStorageManager(
+ account,
+ mService.getContentResolver()
+ );
+
+
+ /// perform the operation
+ if (mCurrentOperation instanceof SyncOperation) {
+ operationResult = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient, mStorageManager);
+ } else {
+ operationResult = mCurrentOperation.execute(mOwnCloudClient);
+ }
+
+ } catch (AccountsException e) {
+ Log_OC.e(TAG, "Error while trying to get autorization", e);
+ operationResult = new RemoteOperationResult(e);
+ } catch (IOException e) {
+ Log_OC.e(TAG, "Error while trying to get autorization", e);
+ operationResult = new RemoteOperationResult(e);
+
+ } finally {
+ synchronized(mPendingOperations) {
+ mPendingOperations.remove(syncKey);
+ }
+ }
+
+ /// TODO notify operation result if needed
+
+ }
+ }
+
+ public void add(Account account, OCFile file, SynchronizeFolderOperation syncFolderOperation){
+ String syncKey = buildRemoteName(account,file);
+ mPendingOperations.putIfAbsent(syncKey,syncFolderOperation);
+ }
+
+ /**
+ * Cancels a pending or current sync operation.
+ *
+ * @param account Owncloud account where the remote file is stored.
+ * @param file A file in the queue of pending downloads
+ */
+ public void cancel(Account account, OCFile file) {
+ SynchronizeFolderOperation syncOperation = null;
+ synchronized (mPendingOperations) {
+ syncOperation = mPendingOperations.remove(buildRemoteName(account, file));
+ }
+ if (syncOperation != null) {
+ syncOperation.cancel();
+ }
+ }
+
+ /**
+ * Builds a key from the account and file to download
+ *
+ * @param account Account where the file to download is stored
+ * @param file File to download
+ */
+ private String buildRemoteName(Account account, OCFile file) {
+ return account.name + file.getRemotePath();
+ }
+ }
+
+