X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/e9ce426cdcf01df10a3541d8dc5db0dcc539d585..10f99ceb9ca7a65b2d59f30a7ddfbfa7ffc60a7a:/src/com/owncloud/android/services/OperationsService.java diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index ea9fcd88..80caea71 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@ -18,18 +18,21 @@ package com.owncloud.android.services; import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import java.util.concurrent.ConcurrentLinkedQueue; import com.owncloud.android.datamodel.FileDataStorageManager; - -import com.owncloud.android.lib.network.OwnCloudClientFactory; -import com.owncloud.android.lib.network.OwnCloudClient; -import com.owncloud.android.operations.CreateShareOperation; -import com.owncloud.android.operations.GetSharesOperation; +import com.owncloud.android.lib.common.OwnCloudClientFactory; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; +import com.owncloud.android.lib.common.operations.RemoteOperation; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.resources.shares.ShareType; import com.owncloud.android.operations.common.SyncOperation; -import com.owncloud.android.lib.operations.common.RemoteOperation; -import com.owncloud.android.lib.operations.common.RemoteOperationResult; -import com.owncloud.android.lib.operations.common.ShareType; +import com.owncloud.android.operations.CreateShareOperation; +import com.owncloud.android.operations.UnshareLinkOperation; import com.owncloud.android.utils.Log_OC; import android.accounts.Account; @@ -76,7 +79,7 @@ public class OperationsService extends Service { private Looper mServiceLooper; private ServiceHandler mServiceHandler; - private IBinder mBinder; + private OperationsServiceBinder mBinder; private OwnCloudClient mOwnCloudClient = null; private Target mLastTarget = null; private FileDataStorageManager mStorageManager; @@ -119,21 +122,25 @@ public class OperationsService extends Service { RemoteOperation operation = null; String action = intent.getAction(); - if (action == ACTION_CREATE_SHARE) { + if (action.equals(ACTION_CREATE_SHARE)) { // Create Share String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); Intent sendIntent = intent.getParcelableExtra(EXTRA_SEND_INTENT); if (remotePath.length() > 0) { operation = new CreateShareOperation(remotePath, ShareType.PUBLIC_LINK, "", false, "", 1, sendIntent); } - } else if (action == ACTION_UNSHARE) { - + } else if (action.equals(ACTION_UNSHARE)) { // Unshare file + String remotePath = intent.getStringExtra(EXTRA_REMOTE_PATH); + if (remotePath.length() > 0) { + operation = new UnshareLinkOperation(remotePath, this.getApplicationContext()); + } } else { - operation = new GetSharesOperation(); + // nothing we are going to handle + return START_NOT_STICKY; } mPendingOperations.add(new Pair(target, operation)); - sendBroadcastNewOperation(target, operation); + //sendBroadcastNewOperation(target, operation); Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; @@ -173,8 +180,59 @@ public class OperationsService extends Service { * * It provides by itself the available operations. */ - public class OperationsServiceBinder extends Binder { - // TODO + 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 Map mBoundListeners = new HashMap(); + + /** + * Cancels an operation + * + * TODO + */ + public void cancel() { + // TODO + } + + + 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) { + 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) { + 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 (!mPendingOperations.isEmpty()); + } + } @@ -265,15 +323,16 @@ public class OperationsService extends Service { } } - sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result); + //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result); + callbackOperationListeners(mLastTarget, mCurrentOperation, result); } } /** - * Sends a LOCAL broadcast when a new operation is added to the queue. + * Sends a broadcast when a new operation is added to the queue. * - * Local broadcasts are only delivered to activities in the same process. + * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\ * * @param target Account or URL pointing to an OC server. * @param operation Added operation. @@ -314,6 +373,31 @@ public class OperationsService extends Service { //lbm.sendBroadcast(intent); sendStickyBroadcast(intent); } + + /** + * Notifies the currently subscribed listeners about the end of an operation. + * + * @param target Account or URL pointing to an OC server. + * @param operation Finished operation. + * @param result Result of the operation. + */ + private void callbackOperationListeners(Target target, final RemoteOperation operation, final RemoteOperationResult result) { + Iterator listeners = mBinder.mBoundListeners.keySet().iterator(); + while (listeners.hasNext()) { + final OnRemoteOperationListener listener = listeners.next(); + final Handler handler = mBinder.mBoundListeners.get(listener); + if (handler != null) { + handler.post(new Runnable() { + @Override + public void run() { + listener.onRemoteOperationFinish(operation, result); + } + }); + } + } + + } + }