1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.services
;
20 import java
.io
.IOException
;
21 import java
.util
.Iterator
;
22 import java
.util
.concurrent
.ConcurrentHashMap
;
23 import java
.util
.concurrent
.ConcurrentLinkedQueue
;
24 import java
.util
.concurrent
.ConcurrentMap
;
26 import com
.owncloud
.android
.MainApp
;
27 import com
.owncloud
.android
.R
;
28 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
29 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
30 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
31 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
32 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
33 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentialsFactory
;
34 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
;
35 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
;
36 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
;
37 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
38 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
39 import com
.owncloud
.android
.lib
.resources
.files
.ExistenceCheckRemoteOperation
;
40 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
41 import com
.owncloud
.android
.lib
.resources
.users
.GetRemoteUserNameOperation
;
42 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
43 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
44 import com
.owncloud
.android
.operations
.CreateShareOperation
;
45 import com
.owncloud
.android
.operations
.GetServerInfoOperation
;
46 import com
.owncloud
.android
.operations
.MoveFileOperation
;
47 import com
.owncloud
.android
.operations
.OAuth2GetAccessToken
;
48 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
49 import com
.owncloud
.android
.operations
.RenameFileOperation
;
50 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
51 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
52 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
54 import android
.accounts
.Account
;
55 import android
.accounts
.AccountsException
;
56 import android
.accounts
.AuthenticatorException
;
57 import android
.accounts
.OperationCanceledException
;
58 import android
.app
.Service
;
59 import android
.content
.Intent
;
60 import android
.net
.Uri
;
61 import android
.os
.Binder
;
62 import android
.os
.Handler
;
63 import android
.os
.HandlerThread
;
64 import android
.os
.IBinder
;
65 import android
.os
.Looper
;
66 import android
.os
.Message
;
67 import android
.os
.Process
;
68 import android
.util
.Pair
;
70 public class OperationsService
extends Service
{
72 private static final String TAG
= OperationsService
.class.getSimpleName();
74 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
75 public static final String EXTRA_SERVER_URL
= "SERVER_URL";
76 public static final String EXTRA_OAUTH2_QUERY_PARAMETERS
= "OAUTH2_QUERY_PARAMETERS";
77 public static final String EXTRA_REMOTE_PATH
= "REMOTE_PATH";
78 public static final String EXTRA_SEND_INTENT
= "SEND_INTENT";
79 public static final String EXTRA_NEWNAME
= "NEWNAME";
80 public static final String EXTRA_REMOVE_ONLY_LOCAL
= "REMOVE_LOCAL_COPY";
81 public static final String EXTRA_CREATE_FULL_PATH
= "CREATE_FULL_PATH";
82 public static final String EXTRA_SYNC_FILE_CONTENTS
= "SYNC_FILE_CONTENTS";
83 public static final String EXTRA_RESULT
= "RESULT";
84 public static final String EXTRA_NEW_PARENT_PATH
= "NEW_PARENT_PATH";
86 // TODO review if ALL OF THEM are necessary
87 public static final String EXTRA_SUCCESS_IF_ABSENT
= "SUCCESS_IF_ABSENT";
88 public static final String EXTRA_USERNAME
= "USERNAME";
89 public static final String EXTRA_PASSWORD
= "PASSWORD";
90 public static final String EXTRA_AUTH_TOKEN
= "AUTH_TOKEN";
91 public static final String EXTRA_COOKIE
= "COOKIE";
93 public static final String ACTION_CREATE_SHARE
= "CREATE_SHARE";
94 public static final String ACTION_UNSHARE
= "UNSHARE";
95 public static final String ACTION_GET_SERVER_INFO
= "GET_SERVER_INFO";
96 public static final String ACTION_OAUTH2_GET_ACCESS_TOKEN
= "OAUTH2_GET_ACCESS_TOKEN";
97 public static final String ACTION_EXISTENCE_CHECK
= "EXISTENCE_CHECK";
98 public static final String ACTION_GET_USER_NAME
= "GET_USER_NAME";
99 public static final String ACTION_RENAME
= "RENAME";
100 public static final String ACTION_REMOVE
= "REMOVE";
101 public static final String ACTION_CREATE_FOLDER
= "CREATE_FOLDER";
102 public static final String ACTION_SYNC_FILE
= "SYNC_FILE";
103 public static final String ACTION_SYNC_FOLDER
= "SYNC_FOLDER"; // for the moment, just to download
104 public static final String ACTION_CANCEL_SYNC_FOLDER
= "CANCEL_SYNC_FOLDER"; // for the moment, just to download
105 public static final String ACTION_MOVE_FILE
= "MOVE_FILE";
107 public static final String ACTION_OPERATION_ADDED
= OperationsService
.class.getName() + ".OPERATION_ADDED";
108 public static final String ACTION_OPERATION_FINISHED
= OperationsService
.class.getName() + ".OPERATION_FINISHED";
111 private ConcurrentMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>>
112 mUndispatchedFinishedOperations
=
113 new ConcurrentHashMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>>();
115 private static class Target
{
116 public Uri mServerUrl
= null
;
117 public Account mAccount
= null
;
118 public String mUsername
= null
;
119 public String mPassword
= null
;
120 public String mAuthToken
= null
;
121 public String mCookie
= null
;
123 public Target(Account account
, Uri serverUrl
, String username
, String password
, String authToken
,
126 mServerUrl
= serverUrl
;
127 mUsername
= username
;
128 mPassword
= password
;
129 mAuthToken
= authToken
;
134 private ServiceHandler mOperationsHandler
;
135 private OperationsServiceBinder mOperationsBinder
;
137 private ServiceHandler mSyncFolderHandler
;
140 * Service initialization
143 public void onCreate() {
145 /// First worker thread for most of operations
146 HandlerThread thread
= new HandlerThread("Operations thread", Process
.THREAD_PRIORITY_BACKGROUND
);
148 mOperationsHandler
= new ServiceHandler(thread
.getLooper(), this);
149 mOperationsBinder
= new OperationsServiceBinder(mOperationsHandler
);
151 /// Separated worker thread for download of folders (WIP)
152 thread
= new HandlerThread("Syncfolder thread", Process
.THREAD_PRIORITY_BACKGROUND
);
154 mSyncFolderHandler
= new ServiceHandler(thread
.getLooper(), this);
159 * Entry point to add a new operation to the queue of operations.
161 * New operations are added calling to startService(), resulting in a call to this method.
162 * This ensures the service will keep on working although the caller activity goes away.
165 public int onStartCommand(Intent intent
, int flags
, int startId
) {
166 if (ACTION_SYNC_FOLDER
.equals(intent
.getAction())) {
167 // WIP: for the moment, only SYNC_FOLDER is expected here; the rest of the operations are requested through
169 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(intent
);
170 if (itemToQueue
!= null
) {
171 mSyncFolderHandler
.mPendingOperations
.add(itemToQueue
);
172 Message msg
= mSyncFolderHandler
.obtainMessage();
174 mSyncFolderHandler
.sendMessage(msg
);
177 } else if (ACTION_CANCEL_SYNC_FOLDER
.equals(intent
.getAction())) {
180 Message msg
= mOperationsHandler
.obtainMessage();
182 mOperationsHandler
.sendMessage(msg
);
185 return START_NOT_STICKY
;
189 public void onDestroy() {
190 //Log_OC.wtf(TAG, "onDestroy init" );
193 OwnCloudClientManagerFactory
.getDefaultSingleton().
194 saveAllClients(this, MainApp
.getAccountType());
196 // TODO - get rid of these exceptions
197 } catch (AccountNotFoundException e
) {
199 } catch (AuthenticatorException e
) {
201 } catch (OperationCanceledException e
) {
203 } catch (IOException e
) {
207 //Log_OC.wtf(TAG, "Clear mUndispatchedFinisiedOperations" );
208 mUndispatchedFinishedOperations
.clear();
210 //Log_OC.wtf(TAG, "onDestroy end" );
216 * Provides a binder object that clients can use to perform actions on the queue of operations,
217 * except the addition of new operations.
220 public IBinder
onBind(Intent intent
) {
221 //Log_OC.wtf(TAG, "onBind" );
222 return mOperationsBinder
;
227 * Called when ALL the bound clients were unbound.
230 public boolean onUnbind(Intent intent
) {
231 ((OperationsServiceBinder
)mOperationsBinder
).clearListeners();
232 return false
; // not accepting rebinding (default behaviour)
237 * Binder to let client components to perform actions on the queue of operations.
239 * It provides by itself the available operations.
241 public class OperationsServiceBinder
extends Binder
/* implements OnRemoteOperationListener */ {
244 * Map of listeners that will be reported about the end of operations from a {@link OperationsServiceBinder} instance
246 private ConcurrentMap
<OnRemoteOperationListener
, Handler
> mBoundListeners
=
247 new ConcurrentHashMap
<OnRemoteOperationListener
, Handler
>();
249 private ServiceHandler mServiceHandler
= null
;
252 public OperationsServiceBinder(ServiceHandler serviceHandler
) {
253 mServiceHandler
= serviceHandler
;
258 * Cancels an operation
262 public void cancel() {
267 public void clearListeners() {
269 mBoundListeners
.clear();
274 * Adds a listener interested in being reported about the end of operations.
276 * @param listener Object to notify about the end of operations.
277 * @param callbackHandler {@link Handler} to access the listener without breaking Android threading protection.
279 public void addOperationListener (OnRemoteOperationListener listener
, Handler callbackHandler
) {
280 synchronized (mBoundListeners
) {
281 mBoundListeners
.put(listener
, callbackHandler
);
287 * Removes a listener from the list of objects interested in the being reported about the end of operations.
289 * @param listener Object to notify about progress of transfer.
291 public void removeOperationListener (OnRemoteOperationListener listener
) {
292 synchronized (mBoundListeners
) {
293 mBoundListeners
.remove(listener
);
299 * TODO - IMPORTANT: update implementation when more operations are moved into the service
301 * @return 'True' when an operation that enforces the user to wait for completion is in process.
303 public boolean isPerformingBlockingOperation() {
304 return (!mServiceHandler
.mPendingOperations
.isEmpty());
309 * Creates and adds to the queue a new operation, as described by operationIntent.
311 * Calls startService to make the operation is processed by the ServiceHandler.
313 * @param operationIntent Intent describing a new operation to queue and execute.
314 * @return Identifier of the operation created, or null if failed.
316 public long queueNewOperation(Intent operationIntent
) {
317 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(operationIntent
);
318 if (itemToQueue
!= null
) {
319 mServiceHandler
.mPendingOperations
.add(itemToQueue
);
320 startService(new Intent(OperationsService
.this, OperationsService
.class));
321 return itemToQueue
.second
.hashCode();
324 return Long
.MAX_VALUE
;
329 public boolean dispatchResultIfFinished(int operationId
, OnRemoteOperationListener listener
) {
330 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
331 mUndispatchedFinishedOperations
.remove(operationId
);
332 if (undispatched
!= null
) {
333 listener
.onRemoteOperationFinish(undispatched
.first
, undispatched
.second
);
335 //Log_OC.wtf(TAG, "Sending callback later");
337 if (!mServiceHandler
.mPendingOperations
.isEmpty()) {
342 //Log_OC.wtf(TAG, "Not finished yet");
350 * Operations worker. Performs the pending operations in the order they were requested.
352 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
354 private static class ServiceHandler
extends Handler
{
355 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
358 OperationsService mService
;
361 private ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>> mPendingOperations
=
362 new ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>>();
363 private RemoteOperation mCurrentOperation
= null
;
364 private Target mLastTarget
= null
;
365 private OwnCloudClient mOwnCloudClient
= null
;
366 private FileDataStorageManager mStorageManager
;
369 public ServiceHandler(Looper looper
, OperationsService service
) {
371 if (service
== null
) {
372 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
378 public void handleMessage(Message msg
) {
380 mService
.stopSelf(msg
.arg1
);
385 * Performs the next operation in the queue
387 private void nextOperation() {
389 //Log_OC.wtf(TAG, "nextOperation init" );
391 Pair
<Target
, RemoteOperation
> next
= null
;
392 synchronized(mPendingOperations
) {
393 next
= mPendingOperations
.peek();
398 mCurrentOperation
= next
.second
;
399 RemoteOperationResult result
= null
;
401 /// prepare client object to send the request to the ownCloud server
402 if (mLastTarget
== null
|| !mLastTarget
.equals(next
.first
)) {
403 mLastTarget
= next
.first
;
404 if (mLastTarget
.mAccount
!= null
) {
405 OwnCloudAccount ocAccount
= new OwnCloudAccount(mLastTarget
.mAccount
, mService
);
406 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
407 getClientFor(ocAccount
, mService
);
408 mStorageManager
= new FileDataStorageManager(
409 mLastTarget
.mAccount
,
410 mService
.getContentResolver()
413 OwnCloudCredentials credentials
= null
;
414 if (mLastTarget
.mUsername
!= null
&&
415 mLastTarget
.mUsername
.length() > 0) {
416 credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(
417 mLastTarget
.mUsername
,
418 mLastTarget
.mPassword
); // basic
420 } else if (mLastTarget
.mAuthToken
!= null
&&
421 mLastTarget
.mAuthToken
.length() > 0) {
422 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
423 mLastTarget
.mAuthToken
); // bearer token
425 } else if (mLastTarget
.mCookie
!= null
&&
426 mLastTarget
.mCookie
.length() > 0) {
427 credentials
= OwnCloudCredentialsFactory
.newSamlSsoCredentials(
428 mLastTarget
.mCookie
); // SAML SSO
430 OwnCloudAccount ocAccount
= new OwnCloudAccount(
431 mLastTarget
.mServerUrl
, credentials
);
432 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
433 getClientFor(ocAccount
, mService
);
434 mStorageManager
= null
;
438 /// perform the operation
439 if (mCurrentOperation
instanceof SyncOperation
) {
440 result
= ((SyncOperation
)mCurrentOperation
).execute(mOwnCloudClient
, mStorageManager
);
442 result
= mCurrentOperation
.execute(mOwnCloudClient
);
445 } catch (AccountsException e
) {
446 if (mLastTarget
.mAccount
== null
) {
447 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
449 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
451 result
= new RemoteOperationResult(e
);
453 } catch (IOException e
) {
454 if (mLastTarget
.mAccount
== null
) {
455 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
457 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
459 result
= new RemoteOperationResult(e
);
460 } catch (Exception e
) {
461 if (mLastTarget
.mAccount
== null
) {
462 Log_OC
.e(TAG
, "Unexpected error for a NULL account", e
);
464 Log_OC
.e(TAG
, "Unexpected error for " + mLastTarget
.mAccount
.name
, e
);
466 result
= new RemoteOperationResult(e
);
469 synchronized(mPendingOperations
) {
470 mPendingOperations
.poll();
474 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
475 mService
.dispatchResultToOperationListeners(mLastTarget
, mCurrentOperation
, result
);
485 * Creates a new operation, as described by operationIntent.
487 * TODO - move to ServiceHandler (probably)
489 * @param operationIntent Intent describing a new operation to queue and execute.
490 * @return Pair with the new operation object and the information about its target server.
492 private Pair
<Target
, RemoteOperation
> newOperation(Intent operationIntent
) {
493 RemoteOperation operation
= null
;
494 Target target
= null
;
496 if (!operationIntent
.hasExtra(EXTRA_ACCOUNT
) &&
497 !operationIntent
.hasExtra(EXTRA_SERVER_URL
)) {
498 Log_OC
.e(TAG
, "Not enough information provided in intent");
501 Account account
= operationIntent
.getParcelableExtra(EXTRA_ACCOUNT
);
502 String serverUrl
= operationIntent
.getStringExtra(EXTRA_SERVER_URL
);
503 String username
= operationIntent
.getStringExtra(EXTRA_USERNAME
);
504 String password
= operationIntent
.getStringExtra(EXTRA_PASSWORD
);
505 String authToken
= operationIntent
.getStringExtra(EXTRA_AUTH_TOKEN
);
506 String cookie
= operationIntent
.getStringExtra(EXTRA_COOKIE
);
509 (serverUrl
== null
) ? null
: Uri
.parse(serverUrl
),
516 String action
= operationIntent
.getAction();
517 if (action
.equals(ACTION_CREATE_SHARE
)) { // Create Share
518 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
519 Intent sendIntent
= operationIntent
.getParcelableExtra(EXTRA_SEND_INTENT
);
520 if (remotePath
.length() > 0) {
521 operation
= new CreateShareOperation(remotePath
, ShareType
.PUBLIC_LINK
,
522 "", false
, "", 1, sendIntent
);
525 } else if (action
.equals(ACTION_UNSHARE
)) { // Unshare file
526 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
527 if (remotePath
.length() > 0) {
528 operation
= new UnshareLinkOperation(
530 OperationsService
.this);
533 } else if (action
.equals(ACTION_GET_SERVER_INFO
)) {
534 // check OC server and get basic information from it
535 operation
= new GetServerInfoOperation(serverUrl
, OperationsService
.this);
537 } else if (action
.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN
)) {
538 /// GET ACCESS TOKEN to the OAuth server
539 String oauth2QueryParameters
=
540 operationIntent
.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS
);
541 operation
= new OAuth2GetAccessToken(
542 getString(R
.string
.oauth2_client_id
),
543 getString(R
.string
.oauth2_redirect_uri
),
544 getString(R
.string
.oauth2_grant_type
),
545 oauth2QueryParameters
);
547 } else if (action
.equals(ACTION_EXISTENCE_CHECK
)) {
549 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
550 boolean successIfAbsent
= operationIntent
.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT
, false
);
551 operation
= new ExistenceCheckRemoteOperation(remotePath
, OperationsService
.this, successIfAbsent
);
553 } else if (action
.equals(ACTION_GET_USER_NAME
)) {
555 operation
= new GetRemoteUserNameOperation();
557 } else if (action
.equals(ACTION_RENAME
)) {
558 // Rename file or folder
559 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
560 String newName
= operationIntent
.getStringExtra(EXTRA_NEWNAME
);
561 operation
= new RenameFileOperation(remotePath
, newName
);
563 } else if (action
.equals(ACTION_REMOVE
)) {
564 // Remove file or folder
565 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
566 boolean onlyLocalCopy
= operationIntent
.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL
, false
);
567 operation
= new RemoveFileOperation(remotePath
, onlyLocalCopy
);
569 } else if (action
.equals(ACTION_CREATE_FOLDER
)) {
571 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
572 boolean createFullPath
= operationIntent
.getBooleanExtra(EXTRA_CREATE_FULL_PATH
, true
);
573 operation
= new CreateFolderOperation(remotePath
, createFullPath
);
575 } else if (action
.equals(ACTION_SYNC_FILE
)) {
577 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
578 boolean syncFileContents
= operationIntent
.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS
, true
);
579 operation
= new SynchronizeFileOperation(
580 remotePath
, account
, syncFileContents
, getApplicationContext()
583 } else if (action
.equals(ACTION_SYNC_FOLDER
)) {
585 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
586 operation
= new SynchronizeFolderOperation(
587 this, // TODO remove this dependency from construction time
590 System
.currentTimeMillis() // TODO remove this dependency from construction time
593 } else if (action
.equals(ACTION_MOVE_FILE
)) {
595 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
596 String newParentPath
= operationIntent
.getStringExtra(EXTRA_NEW_PARENT_PATH
);
597 operation
= new MoveFileOperation(remotePath
,newParentPath
,account
);
602 } catch (IllegalArgumentException e
) {
603 Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage());
607 if (operation
!= null
) {
608 return new Pair
<Target
, RemoteOperation
>(target
, operation
);
616 * Sends a broadcast when a new operation is added to the queue.
618 * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\
620 * @param target Account or URL pointing to an OC server.
621 * @param operation Added operation.
623 private void sendBroadcastNewOperation(Target target
, RemoteOperation operation
) {
624 Intent intent
= new Intent(ACTION_OPERATION_ADDED
);
625 if (target
.mAccount
!= null
) {
626 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
628 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
630 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
631 //lbm.sendBroadcast(intent);
632 sendStickyBroadcast(intent
);
636 // TODO - maybe add a notification for real start of operations
639 * Sends a LOCAL broadcast when an operations finishes in order to the interested activities can update their view
641 * Local broadcasts are only delivered to activities in the same process.
643 * @param target Account or URL pointing to an OC server.
644 * @param operation Finished operation.
645 * @param result Result of the operation.
647 private void sendBroadcastOperationFinished(Target target
, RemoteOperation operation
, RemoteOperationResult result
) {
648 Intent intent
= new Intent(ACTION_OPERATION_FINISHED
);
649 intent
.putExtra(EXTRA_RESULT
, result
);
650 if (target
.mAccount
!= null
) {
651 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
653 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
655 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
656 //lbm.sendBroadcast(intent);
657 sendStickyBroadcast(intent
);
662 * Notifies the currently subscribed listeners about the end of an operation.
664 * @param target Account or URL pointing to an OC server.
665 * @param operation Finished operation.
666 * @param result Result of the operation.
668 private void dispatchResultToOperationListeners(
669 Target target
, final RemoteOperation operation
, final RemoteOperationResult result
) {
671 Iterator
<OnRemoteOperationListener
> listeners
= mOperationsBinder
.mBoundListeners
.keySet().iterator();
672 while (listeners
.hasNext()) {
673 final OnRemoteOperationListener listener
= listeners
.next();
674 final Handler handler
= mOperationsBinder
.mBoundListeners
.get(listener
);
675 if (handler
!= null
) {
676 handler
.post(new Runnable() {
679 listener
.onRemoteOperationFinish(operation
, result
);
686 //mOperationResults.put(operation.hashCode(), result);
687 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
688 new Pair
<RemoteOperation
, RemoteOperationResult
>(operation
, result
);
689 mUndispatchedFinishedOperations
.put(operation
.hashCode(), undispatched
);
691 Log_OC
.d(TAG
, "Called " + count
+ " listeners");