+ public class OperationsServiceBinder extends Binder /* implements OnRemoteOperationListener */ {
+
+ /**
+ * Map of listeners that will be reported about the end of operations from a
+ * {@link OperationsServiceBinder} instance
+ */
+ private ConcurrentMap<OnRemoteOperationListener, Handler> mBoundListeners =
+ new ConcurrentHashMap<OnRemoteOperationListener, Handler>();
+
+ private ServiceHandler mServiceHandler = null;
+
+ public OperationsServiceBinder(ServiceHandler serviceHandler) {
+ mServiceHandler = serviceHandler;
+ }
+
+
+ /**
+ * Cancels a pending or current synchronization.
+ *
+ * @param account ownCloud account where the remote folder is stored.
+ * @param file A folder in the queue of pending synchronizations
+ */
+ public void cancel(Account account, OCFile file) {
+ mSyncFolderHandler.cancel(account, file);
+ }
+
+
+ public void clearListeners() {
+
+ mBoundListeners.clear();
+ }
+
+
+ /**
+ * Adds a listener interested in being reported about the end of operations.
+ *
+ * @param listener Object to notify about the end of operations.
+ * @param callbackHandler {@link Handler} to access the listener without
+ * breaking Android threading protection.
+ */
+ public void addOperationListener (OnRemoteOperationListener listener,
+ Handler callbackHandler) {
+ synchronized (mBoundListeners) {
+ mBoundListeners.put(listener, callbackHandler);
+ }
+ }
+
+
+ /**
+ * Removes a listener from the list of objects interested in the being reported about
+ * the end of operations.
+ *
+ * @param listener Object to notify about progress of transfer.
+ */
+ public void removeOperationListener (OnRemoteOperationListener listener) {
+ synchronized (mBoundListeners) {
+ mBoundListeners.remove(listener);
+ }
+ }
+
+
+ /**
+ * TODO - IMPORTANT: update implementation when more operations are moved into the service
+ *
+ * @return 'True' when an operation that enforces the user to wait for completion is
+ * in process.
+ */
+ public boolean isPerformingBlockingOperation() {
+ return (!mServiceHandler.mPendingOperations.isEmpty());
+ }
+
+
+ /**
+ * Creates and adds to the queue a new operation, as described by operationIntent.
+ *
+ * Calls startService to make the operation is processed by the ServiceHandler.
+ *
+ * @param operationIntent Intent describing a new operation to queue and execute.
+ * @return Identifier of the operation created, or null if failed.
+ */
+ public long queueNewOperation(Intent operationIntent) {
+ Pair<Target, RemoteOperation> itemToQueue = newOperation(operationIntent);
+ if (itemToQueue != null) {
+ mServiceHandler.mPendingOperations.add(itemToQueue);
+ startService(new Intent(OperationsService.this, OperationsService.class));
+ return itemToQueue.second.hashCode();
+
+ } else {
+ return Long.MAX_VALUE;
+ }
+ }
+
+
+ public boolean dispatchResultIfFinished(int operationId,
+ OnRemoteOperationListener listener) {
+ Pair<RemoteOperation, RemoteOperationResult> undispatched =
+ mUndispatchedFinishedOperations.remove(operationId);
+ if (undispatched != null) {
+ listener.onRemoteOperationFinish(undispatched.first, undispatched.second);
+ return true;
+ //Log_OC.wtf(TAG, "Sending callback later");
+ } else {
+ return (!mServiceHandler.mPendingOperations.isEmpty());
+ }
+ }
+
+
+ /**
+ * Returns True when the file described by 'file' in the ownCloud account 'account' is
+ * downloading or waiting to download.
+ *
+ * If 'file' is a directory, returns 'true' if some of its descendant files is downloading
+ * or waiting to download.
+ *
+ * @param account ownCloud account where the remote file is stored.
+ * @param remotePath Path of the folder to check if something is synchronizing
+ * / downloading / uploading inside.
+ */
+ public boolean isSynchronizing(Account account, String remotePath) {
+ return mSyncFolderHandler.isSynchronizing(account, remotePath);
+ }
+