Merge branch 'develop' into ADFS_auth_support
[pub/Android/ownCloud.git] / src / com / owncloud / android / services / OperationsService.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.services;
19
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;
25
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.resources.files.ExistenceCheckRemoteOperation;
39 import com.owncloud.android.lib.resources.shares.ShareType;
40 import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;
41 import com.owncloud.android.operations.common.SyncOperation;
42 import com.owncloud.android.operations.CreateFolderOperation;
43 import com.owncloud.android.operations.CreateShareOperation;
44 import com.owncloud.android.operations.GetServerInfoOperation;
45 import com.owncloud.android.operations.MoveFileOperation;
46 import com.owncloud.android.operations.OAuth2GetAccessToken;
47 import com.owncloud.android.operations.RemoveFileOperation;
48 import com.owncloud.android.operations.RenameFileOperation;
49 import com.owncloud.android.operations.SynchronizeFileOperation;
50 import com.owncloud.android.operations.UnshareLinkOperation;
51 import com.owncloud.android.utils.Log_OC;
52
53 import android.accounts.Account;
54 import android.accounts.AccountsException;
55 import android.accounts.AuthenticatorException;
56 import android.accounts.OperationCanceledException;
57 import android.app.Service;
58 import android.content.Intent;
59 import android.net.Uri;
60 import android.os.Binder;
61 import android.os.Handler;
62 import android.os.HandlerThread;
63 import android.os.IBinder;
64 import android.os.Looper;
65 import android.os.Message;
66 import android.os.Process;
67 import android.util.Pair;
68
69 public class OperationsService extends Service {
70
71 private static final String TAG = OperationsService.class.getSimpleName();
72
73 public static final String EXTRA_ACCOUNT = "ACCOUNT";
74 public static final String EXTRA_SERVER_URL = "SERVER_URL";
75 public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS";
76 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
77 public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
78 public static final String EXTRA_NEWNAME = "NEWNAME";
79 public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
80 public static final String EXTRA_CREATE_FULL_PATH = "CREATE_FULL_PATH";
81 public static final String EXTRA_SYNC_FILE_CONTENTS = "SYNC_FILE_CONTENTS";
82 public static final String EXTRA_RESULT = "RESULT";
83 public static final String EXTRA_NEW_PARENT_PATH = "NEW_PARENT_PATH";
84
85 // TODO review if ALL OF THEM are necessary
86 public static final String EXTRA_SUCCESS_IF_ABSENT = "SUCCESS_IF_ABSENT";
87 public static final String EXTRA_USERNAME = "USERNAME";
88 public static final String EXTRA_PASSWORD = "PASSWORD";
89 public static final String EXTRA_AUTH_TOKEN = "AUTH_TOKEN";
90 public static final String EXTRA_COOKIE = "COOKIE";
91
92 public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
93 public static final String ACTION_UNSHARE = "UNSHARE";
94 public static final String ACTION_GET_SERVER_INFO = "GET_SERVER_INFO";
95 public static final String ACTION_OAUTH2_GET_ACCESS_TOKEN = "OAUTH2_GET_ACCESS_TOKEN";
96 public static final String ACTION_EXISTENCE_CHECK = "EXISTENCE_CHECK";
97 public static final String ACTION_GET_USER_NAME = "GET_USER_NAME";
98 public static final String ACTION_RENAME = "RENAME";
99 public static final String ACTION_REMOVE = "REMOVE";
100 public static final String ACTION_CREATE_FOLDER = "CREATE_FOLDER";
101 public static final String ACTION_SYNC_FILE = "SYNC_FILE";
102 public static final String ACTION_MOVE_FILE = "MOVE_FILE";
103
104 public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
105 public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
106
107 private ConcurrentLinkedQueue<Pair<Target, RemoteOperation>> mPendingOperations =
108 new ConcurrentLinkedQueue<Pair<Target, RemoteOperation>>();
109
110 private ConcurrentMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>
111 mUndispatchedFinishedOperations =
112 new ConcurrentHashMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>();
113
114 private static class Target {
115 public Uri mServerUrl = null;
116 public Account mAccount = null;
117 public String mUsername = null;
118 public String mPassword = null;
119 public String mAuthToken = null;
120 public String mCookie = null;
121
122 public Target(Account account, Uri serverUrl, String username, String password, String authToken,
123 String cookie) {
124 mAccount = account;
125 mServerUrl = serverUrl;
126 mUsername = username;
127 mPassword = password;
128 mAuthToken = authToken;
129 mCookie = cookie;
130 }
131 }
132
133 private Looper mServiceLooper;
134 private ServiceHandler mServiceHandler;
135 private OperationsServiceBinder mBinder;
136 private OwnCloudClient mOwnCloudClient = null;
137 private Target mLastTarget = null;
138 private FileDataStorageManager mStorageManager;
139 private RemoteOperation mCurrentOperation = null;
140
141
142 /**
143 * Service initialization
144 */
145 @Override
146 public void onCreate() {
147 super.onCreate();
148 HandlerThread thread = new HandlerThread("Operations service thread", Process.THREAD_PRIORITY_BACKGROUND);
149 thread.start();
150 mServiceLooper = thread.getLooper();
151 mServiceHandler = new ServiceHandler(mServiceLooper, this);
152 mBinder = new OperationsServiceBinder();
153 }
154
155
156 /**
157 * Entry point to add a new operation to the queue of operations.
158 *
159 * New operations are added calling to startService(), resulting in a call to this method.
160 * This ensures the service will keep on working although the caller activity goes away.
161 *
162 * IMPORTANT: the only operations performed here right now is {@link GetSharedFilesOperation}. The class
163 * is taking advantage of it due to time constraints.
164 */
165 @Override
166 public int onStartCommand(Intent intent, int flags, int startId) {
167 //Log_OC.wtf(TAG, "onStartCommand init" );
168 Message msg = mServiceHandler.obtainMessage();
169 msg.arg1 = startId;
170 mServiceHandler.sendMessage(msg);
171 //Log_OC.wtf(TAG, "onStartCommand end" );
172 return START_NOT_STICKY;
173 }
174
175 @Override
176 public void onDestroy() {
177 //Log_OC.wtf(TAG, "onDestroy init" );
178 // Saving cookies
179 try {
180 OwnCloudClientManagerFactory.getDefaultSingleton().
181 saveAllClients(this, MainApp.getAccountType());
182
183 // TODO - get rid of these exceptions
184 } catch (AccountNotFoundException e) {
185 e.printStackTrace();
186 } catch (AuthenticatorException e) {
187 e.printStackTrace();
188 } catch (OperationCanceledException e) {
189 e.printStackTrace();
190 } catch (IOException e) {
191 e.printStackTrace();
192 }
193
194 //Log_OC.wtf(TAG, "Clear mUndispatchedFinisiedOperations" );
195 mUndispatchedFinishedOperations.clear();
196
197 //Log_OC.wtf(TAG, "onDestroy end" );
198 super.onDestroy();
199 }
200
201
202 /**
203 * Provides a binder object that clients can use to perform actions on the queue of operations,
204 * except the addition of new operations.
205 */
206 @Override
207 public IBinder onBind(Intent intent) {
208 //Log_OC.wtf(TAG, "onBind" );
209 return mBinder;
210 }
211
212
213 /**
214 * Called when ALL the bound clients were unbound.
215 */
216 @Override
217 public boolean onUnbind(Intent intent) {
218 ((OperationsServiceBinder)mBinder).clearListeners();
219 return false; // not accepting rebinding (default behaviour)
220 }
221
222
223 /**
224 * Binder to let client components to perform actions on the queue of operations.
225 *
226 * It provides by itself the available operations.
227 */
228 public class OperationsServiceBinder extends Binder /* implements OnRemoteOperationListener */ {
229
230 /**
231 * Map of listeners that will be reported about the end of operations from a {@link OperationsServiceBinder} instance
232 */
233 private ConcurrentMap<OnRemoteOperationListener, Handler> mBoundListeners =
234 new ConcurrentHashMap<OnRemoteOperationListener, Handler>();
235
236 /**
237 * Cancels an operation
238 *
239 * TODO
240 */
241 public void cancel() {
242 // TODO
243 }
244
245
246 public void clearListeners() {
247
248 mBoundListeners.clear();
249 }
250
251
252 /**
253 * Adds a listener interested in being reported about the end of operations.
254 *
255 * @param listener Object to notify about the end of operations.
256 * @param callbackHandler {@link Handler} to access the listener without breaking Android threading protection.
257 */
258 public void addOperationListener (OnRemoteOperationListener listener, Handler callbackHandler) {
259 synchronized (mBoundListeners) {
260 mBoundListeners.put(listener, callbackHandler);
261 }
262 }
263
264
265 /**
266 * Removes a listener from the list of objects interested in the being reported about the end of operations.
267 *
268 * @param listener Object to notify about progress of transfer.
269 */
270 public void removeOperationListener (OnRemoteOperationListener listener) {
271 synchronized (mBoundListeners) {
272 mBoundListeners.remove(listener);
273 }
274 }
275
276
277 /**
278 * TODO - IMPORTANT: update implementation when more operations are moved into the service
279 *
280 * @return 'True' when an operation that enforces the user to wait for completion is in process.
281 */
282 public boolean isPerformingBlockingOperation() {
283 return (!mPendingOperations.isEmpty());
284 }
285
286
287 /**
288 * Creates and adds to the queue a new operation, as described by operationIntent
289 *
290 * @param operationIntent Intent describing a new operation to queue and execute.
291 * @return Identifier of the operation created, or null if failed.
292 */
293 public long newOperation(Intent operationIntent) {
294 RemoteOperation operation = null;
295 Target target = null;
296 try {
297 if (!operationIntent.hasExtra(EXTRA_ACCOUNT) &&
298 !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
299 Log_OC.e(TAG, "Not enough information provided in intent");
300
301 } else {
302 Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
303 String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
304 String username = operationIntent.getStringExtra(EXTRA_USERNAME);
305 String password = operationIntent.getStringExtra(EXTRA_PASSWORD);
306 String authToken = operationIntent.getStringExtra(EXTRA_AUTH_TOKEN);
307 String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
308 target = new Target(
309 account,
310 (serverUrl == null) ? null : Uri.parse(serverUrl),
311 username,
312 password,
313 authToken,
314 cookie
315 );
316
317 String action = operationIntent.getAction();
318 if (action.equals(ACTION_CREATE_SHARE)) { // Create Share
319 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
320 Intent sendIntent = operationIntent.getParcelableExtra(EXTRA_SEND_INTENT);
321 if (remotePath.length() > 0) {
322 operation = new CreateShareOperation(remotePath, ShareType.PUBLIC_LINK,
323 "", false, "", 1, sendIntent);
324 }
325
326 } else if (action.equals(ACTION_UNSHARE)) { // Unshare file
327 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
328 if (remotePath.length() > 0) {
329 operation = new UnshareLinkOperation(
330 remotePath,
331 OperationsService.this);
332 }
333
334 } else if (action.equals(ACTION_GET_SERVER_INFO)) {
335 // check OC server and get basic information from it
336 operation = new GetServerInfoOperation(serverUrl, OperationsService.this);
337
338 } else if (action.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN)) {
339 /// GET ACCESS TOKEN to the OAuth server
340 String oauth2QueryParameters =
341 operationIntent.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS);
342 operation = new OAuth2GetAccessToken(
343 getString(R.string.oauth2_client_id),
344 getString(R.string.oauth2_redirect_uri),
345 getString(R.string.oauth2_grant_type),
346 oauth2QueryParameters);
347
348 } else if (action.equals(ACTION_EXISTENCE_CHECK)) {
349 // Existence Check
350 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
351 boolean successIfAbsent = operationIntent.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT, false);
352 operation = new ExistenceCheckRemoteOperation(remotePath, OperationsService.this, successIfAbsent);
353
354 } else if (action.equals(ACTION_GET_USER_NAME)) {
355 // Get User Name
356 operation = new GetRemoteUserNameOperation();
357
358 } else if (action.equals(ACTION_RENAME)) {
359 // Rename file or folder
360 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
361 String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
362 operation = new RenameFileOperation(remotePath, account, newName);
363
364 } else if (action.equals(ACTION_REMOVE)) {
365 // Remove file or folder
366 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
367 boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
368 operation = new RemoveFileOperation(remotePath, onlyLocalCopy);
369
370 } else if (action.equals(ACTION_CREATE_FOLDER)) {
371 // Create Folder
372 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
373 boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
374 operation = new CreateFolderOperation(remotePath, createFullPath);
375
376 } else if (action.equals(ACTION_SYNC_FILE)) {
377 // Sync file
378 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
379 boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
380 operation = new SynchronizeFileOperation(remotePath, account, syncFileContents, getApplicationContext());
381 } else if (action.equals(ACTION_MOVE_FILE)) {
382 // Move file/folder
383 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
384 String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
385 operation = new MoveFileOperation(remotePath,newParentPath,account);
386 }
387
388 }
389
390 } catch (IllegalArgumentException e) {
391 Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
392 operation = null;
393 }
394
395 if (operation != null) {
396 mPendingOperations.add(new Pair<Target , RemoteOperation>(target, operation));
397 startService(new Intent(OperationsService.this, OperationsService.class));
398 //Log_OC.wtf(TAG, "New operation added, opId: " + operation.hashCode());
399 // better id than hash? ; should be good enough by the time being
400 return operation.hashCode();
401
402 } else {
403 //Log_OC.wtf(TAG, "New operation failed, returned Long.MAX_VALUE");
404 return Long.MAX_VALUE;
405 }
406 }
407
408 public boolean dispatchResultIfFinished(int operationId, OnRemoteOperationListener listener) {
409 Pair<RemoteOperation, RemoteOperationResult> undispatched =
410 mUndispatchedFinishedOperations.remove(operationId);
411 if (undispatched != null) {
412 listener.onRemoteOperationFinish(undispatched.first, undispatched.second);
413 return true;
414 //Log_OC.wtf(TAG, "Sending callback later");
415 } else {
416 if (!mPendingOperations.isEmpty()) {
417 return true;
418 } else {
419 return false;
420 }
421 //Log_OC.wtf(TAG, "Not finished yet");
422 }
423 }
424
425 }
426
427
428 /**
429 * Operations worker. Performs the pending operations in the order they were requested.
430 *
431 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
432 */
433 private static class ServiceHandler extends Handler {
434 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
435 OperationsService mService;
436 public ServiceHandler(Looper looper, OperationsService service) {
437 super(looper);
438 if (service == null) {
439 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
440 }
441 mService = service;
442 }
443
444 @Override
445 public void handleMessage(Message msg) {
446 mService.nextOperation();
447 mService.stopSelf(msg.arg1);
448 }
449 }
450
451
452 /**
453 * Performs the next operation in the queue
454 */
455 private void nextOperation() {
456
457 //Log_OC.wtf(TAG, "nextOperation init" );
458
459 Pair<Target, RemoteOperation> next = null;
460 synchronized(mPendingOperations) {
461 next = mPendingOperations.peek();
462 }
463
464 if (next != null) {
465
466 mCurrentOperation = next.second;
467 RemoteOperationResult result = null;
468 try {
469 /// prepare client object to send the request to the ownCloud server
470 if (mLastTarget == null || !mLastTarget.equals(next.first)) {
471 mLastTarget = next.first;
472 if (mLastTarget.mAccount != null) {
473 OwnCloudAccount ocAccount = new OwnCloudAccount(mLastTarget.mAccount, this);
474 mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
475 getClientFor(ocAccount, this);
476 mStorageManager =
477 new FileDataStorageManager(
478 mLastTarget.mAccount,
479 getContentResolver());
480 } else {
481 OwnCloudCredentials credentials = null;
482 if (mLastTarget.mUsername != null &&
483 mLastTarget.mUsername.length() > 0) {
484 credentials = OwnCloudCredentialsFactory.newBasicCredentials(
485 mLastTarget.mUsername,
486 mLastTarget.mPassword); // basic
487
488 } else if (mLastTarget.mAuthToken != null &&
489 mLastTarget.mAuthToken.length() > 0) {
490 credentials = OwnCloudCredentialsFactory.newBearerCredentials(
491 mLastTarget.mAuthToken); // bearer token
492
493 } else if (mLastTarget.mCookie != null &&
494 mLastTarget.mCookie.length() > 0) {
495 credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(
496 mLastTarget.mCookie); // SAML SSO
497 }
498 OwnCloudAccount ocAccount = new OwnCloudAccount(
499 mLastTarget.mServerUrl, credentials);
500 mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().
501 getClientFor(ocAccount, this);
502 mStorageManager = null;
503 }
504 }
505
506 /// perform the operation
507 if (mCurrentOperation instanceof SyncOperation) {
508 result = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient, mStorageManager);
509 } else {
510 result = mCurrentOperation.execute(mOwnCloudClient);
511 }
512
513 } catch (AccountsException e) {
514 if (mLastTarget.mAccount == null) {
515 Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
516 } else {
517 Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
518 }
519 result = new RemoteOperationResult(e);
520
521 } catch (IOException e) {
522 if (mLastTarget.mAccount == null) {
523 Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
524 } else {
525 Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
526 }
527 result = new RemoteOperationResult(e);
528 } catch (Exception e) {
529 if (mLastTarget.mAccount == null) {
530 Log_OC.e(TAG, "Unexpected error for a NULL account", e);
531 } else {
532 Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e);
533 }
534 result = new RemoteOperationResult(e);
535
536 } finally {
537 synchronized(mPendingOperations) {
538 mPendingOperations.poll();
539 }
540 }
541
542 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
543 dispatchResultToOperationListeners(mLastTarget, mCurrentOperation, result);
544 }
545 }
546
547
548 /**
549 * Sends a broadcast when a new operation is added to the queue.
550 *
551 * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\
552 *
553 * @param target Account or URL pointing to an OC server.
554 * @param operation Added operation.
555 */
556 private void sendBroadcastNewOperation(Target target, RemoteOperation operation) {
557 Intent intent = new Intent(ACTION_OPERATION_ADDED);
558 if (target.mAccount != null) {
559 intent.putExtra(EXTRA_ACCOUNT, target.mAccount);
560 } else {
561 intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl);
562 }
563 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
564 //lbm.sendBroadcast(intent);
565 sendStickyBroadcast(intent);
566 }
567
568
569 // TODO - maybe add a notification for real start of operations
570
571 /**
572 * Sends a LOCAL broadcast when an operations finishes in order to the interested activities can update their view
573 *
574 * Local broadcasts are only delivered to activities in the same process.
575 *
576 * @param target Account or URL pointing to an OC server.
577 * @param operation Finished operation.
578 * @param result Result of the operation.
579 */
580 private void sendBroadcastOperationFinished(Target target, RemoteOperation operation, RemoteOperationResult result) {
581 Intent intent = new Intent(ACTION_OPERATION_FINISHED);
582 intent.putExtra(EXTRA_RESULT, result);
583 if (target.mAccount != null) {
584 intent.putExtra(EXTRA_ACCOUNT, target.mAccount);
585 } else {
586 intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl);
587 }
588 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
589 //lbm.sendBroadcast(intent);
590 sendStickyBroadcast(intent);
591 }
592
593
594 /**
595 * Notifies the currently subscribed listeners about the end of an operation.
596 *
597 * @param target Account or URL pointing to an OC server.
598 * @param operation Finished operation.
599 * @param result Result of the operation.
600 */
601 private void dispatchResultToOperationListeners(
602 Target target, final RemoteOperation operation, final RemoteOperationResult result) {
603 int count = 0;
604 Iterator<OnRemoteOperationListener> listeners = mBinder.mBoundListeners.keySet().iterator();
605 while (listeners.hasNext()) {
606 final OnRemoteOperationListener listener = listeners.next();
607 final Handler handler = mBinder.mBoundListeners.get(listener);
608 if (handler != null) {
609 handler.post(new Runnable() {
610 @Override
611 public void run() {
612 listener.onRemoteOperationFinish(operation, result);
613 }
614 });
615 count += 1;
616 }
617 }
618 if (count == 0) {
619 //mOperationResults.put(operation.hashCode(), result);
620 Pair<RemoteOperation, RemoteOperationResult> undispatched =
621 new Pair<RemoteOperation, RemoteOperationResult>(operation, result);
622 mUndispatchedFinishedOperations.put(operation.hashCode(), undispatched);
623 }
624 Log_OC.d(TAG, "Called " + count + " listeners");
625 }
626
627
628 }