From: David A. Velasco Date: Tue, 11 Feb 2014 10:37:52 +0000 (+0100) Subject: Fixed bug in callbacks from OperationsService, a Handler is needed to access the... X-Git-Tag: oc-android-1.5.5~48^2~12 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/77870373d9f4644ce067b2f1780e9da7fca0baff?ds=inline;hp=-c Fixed bug in callbacks from OperationsService, a Handler is needed to access the main thread from other --- 77870373d9f4644ce067b2f1780e9da7fca0baff diff --git a/src/com/owncloud/android/services/OperationsService.java b/src/com/owncloud/android/services/OperationsService.java index 6925cb0f..798a0d42 100644 --- a/src/com/owncloud/android/services/OperationsService.java +++ b/src/com/owncloud/android/services/OperationsService.java @@ -18,8 +18,10 @@ package com.owncloud.android.services; import java.io.IOException; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentLinkedQueue; @@ -187,7 +189,7 @@ public class OperationsService extends Service { /** * Map of listeners that will be reported about the end of operations from a {@link OperationsServiceBinder} instance */ - private Set mBoundListeners = new HashSet(); + private Map mBoundListeners = new HashMap(); /** * Cancels an operation @@ -207,10 +209,11 @@ public class OperationsService extends Service { /** * Adds a listener interested in being reported about the end of operations. * - * @param listener Object to notify 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) { - mBoundListeners.add(listener); + public void addOperationListener (OnRemoteOperationListener listener, Handler callbackHandler) { + mBoundListeners.put(listener, callbackHandler); } @@ -382,11 +385,21 @@ public class OperationsService extends Service { * @param operation Finished operation. * @param result Result of the operation. */ - private void callbackOperationListeners(Target target, RemoteOperation operation, RemoteOperationResult result) { - Iterator it = mBinder.mBoundListeners.iterator(); - while (it.hasNext()) { - it.next().onRemoteOperationFinish(operation, result); + 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); + } + }); + } } + } diff --git a/src/com/owncloud/android/ui/activity/FileActivity.java b/src/com/owncloud/android/ui/activity/FileActivity.java index 27753880..5b5de5b0 100644 --- a/src/com/owncloud/android/ui/activity/FileActivity.java +++ b/src/com/owncloud/android/ui/activity/FileActivity.java @@ -456,7 +456,7 @@ public class FileActivity extends SherlockFragmentActivity implements OnRemoteOp if (component.equals(new ComponentName(FileActivity.this, OperationsService.class))) { Log_OC.d(TAG, "Operations service connected"); mOperationsServiceBinder = (OperationsServiceBinder) service; - mOperationsServiceBinder.addOperationListener(FileActivity.this); + mOperationsServiceBinder.addOperationListener(FileActivity.this, mHandler); if (!mOperationsServiceBinder.isPerformingBlockingOperation()) { dismissLoadingDialog(); }