Adapted code to use single OwnCloudClientManager#getClientFor(...) method
[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.OwnCloudCredentials;
32 import com.owncloud.android.lib.common.OwnCloudCredentialsFactory;
33 import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
34 import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
35 import com.owncloud.android.lib.common.operations.RemoteOperation;
36 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
37 import com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation;
38 import com.owncloud.android.lib.resources.shares.ShareType;
39 import com.owncloud.android.lib.resources.users.GetRemoteUserNameOperation;
40 import com.owncloud.android.operations.common.SyncOperation;
41 import com.owncloud.android.operations.CreateFolderOperation;
42 import com.owncloud.android.operations.CreateShareOperation;
43 import com.owncloud.android.operations.GetServerInfoOperation;
44 import com.owncloud.android.operations.OAuth2GetAccessToken;
45 import com.owncloud.android.operations.RemoveFileOperation;
46 import com.owncloud.android.operations.RenameFileOperation;
47 import com.owncloud.android.operations.SynchronizeFileOperation;
48 import com.owncloud.android.operations.UnshareLinkOperation;
49 import com.owncloud.android.utils.Log_OC;
50
51 import android.accounts.Account;
52 import android.accounts.AccountsException;
53 import android.accounts.AuthenticatorException;
54 import android.accounts.OperationCanceledException;
55 import android.app.Service;
56 import android.content.Intent;
57 import android.net.Uri;
58 import android.os.Binder;
59 import android.os.Handler;
60 import android.os.HandlerThread;
61 import android.os.IBinder;
62 import android.os.Looper;
63 import android.os.Message;
64 import android.os.Process;
65 import android.util.Pair;
66
67 public class OperationsService extends Service {
68
69 private static final String TAG = OperationsService.class.getSimpleName();
70
71 public static final String EXTRA_ACCOUNT = "ACCOUNT";
72 public static final String EXTRA_SERVER_URL = "SERVER_URL";
73 public static final String EXTRA_AUTH_TOKEN_TYPE = "AUTH_TOKEN_TYPE";
74 public static final String EXTRA_OAUTH2_QUERY_PARAMETERS = "OAUTH2_QUERY_PARAMETERS";
75 public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
76 public static final String EXTRA_SEND_INTENT = "SEND_INTENT";
77 public static final String EXTRA_NEWNAME = "NEWNAME";
78 public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
79 public static final String EXTRA_CREATE_FULL_PATH = "CREATE_FULL_PATH";
80 public static final String EXTRA_SYNC_FILE_CONTENTS = "SYNC_FILE_CONTENTS";
81 public static final String EXTRA_RESULT = "RESULT";
82
83 // TODO review if ALL OF THEM are necessary
84 public static final String EXTRA_SUCCESS_IF_ABSENT = "SUCCESS_IF_ABSENT";
85 public static final String EXTRA_USERNAME = "USERNAME";
86 public static final String EXTRA_PASSWORD = "PASSWORD";
87 public static final String EXTRA_AUTH_TOKEN = "AUTH_TOKEN";
88 public static final String EXTRA_FOLLOW_REDIRECTS = "FOLLOW_REDIRECTS";
89 public static final String EXTRA_COOKIE = "COOKIE";
90
91 public static final String ACTION_CREATE_SHARE = "CREATE_SHARE";
92 public static final String ACTION_UNSHARE = "UNSHARE";
93 public static final String ACTION_GET_SERVER_INFO = "GET_SERVER_INFO";
94 public static final String ACTION_OAUTH2_GET_ACCESS_TOKEN = "OAUTH2_GET_ACCESS_TOKEN";
95 public static final String ACTION_EXISTENCE_CHECK = "EXISTENCE_CHECK";
96 public static final String ACTION_GET_USER_NAME = "GET_USER_NAME";
97 public static final String ACTION_RENAME = "RENAME";
98 public static final String ACTION_REMOVE = "REMOVE";
99 public static final String ACTION_CREATE_FOLDER = "CREATE_FOLDER";
100 public static final String ACTION_SYNC_FILE = "SYNC_FILE";
101
102 public static final String ACTION_OPERATION_ADDED = OperationsService.class.getName() + ".OPERATION_ADDED";
103 public static final String ACTION_OPERATION_FINISHED = OperationsService.class.getName() + ".OPERATION_FINISHED";
104
105 private ConcurrentLinkedQueue<Pair<Target, RemoteOperation>> mPendingOperations =
106 new ConcurrentLinkedQueue<Pair<Target, RemoteOperation>>();
107
108 private ConcurrentMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>
109 mUndispatchedFinishedOperations =
110 new ConcurrentHashMap<Integer, Pair<RemoteOperation, RemoteOperationResult>>();
111
112 private static class Target {
113 public Uri mServerUrl = null;
114 public Account mAccount = null;
115 public String mUsername = null;
116 public String mPassword = null;
117 public String mAuthToken = null;
118 public boolean mFollowRedirects = true;
119 public String mCookie = null;
120
121 public Target(Account account, Uri serverUrl, String username, String password, String authToken,
122 boolean followRedirects, String cookie) {
123 mAccount = account;
124 mServerUrl = serverUrl;
125 mUsername = username;
126 mPassword = password;
127 mAuthToken = authToken;
128 mFollowRedirects = followRedirects;
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 ((MainApp)getApplicationContext()).getOwnCloudClientManager().
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 boolean followRedirects = operationIntent.getBooleanExtra(EXTRA_FOLLOW_REDIRECTS, true);
308 String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
309 target = new Target(
310 account,
311 (serverUrl == null) ? null : Uri.parse(serverUrl),
312 username,
313 password,
314 authToken,
315 followRedirects,
316 cookie
317 );
318
319 String action = operationIntent.getAction();
320 if (action.equals(ACTION_CREATE_SHARE)) { // Create Share
321 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
322 Intent sendIntent = operationIntent.getParcelableExtra(EXTRA_SEND_INTENT);
323 if (remotePath.length() > 0) {
324 operation = new CreateShareOperation(remotePath, ShareType.PUBLIC_LINK,
325 "", false, "", 1, sendIntent);
326 }
327
328 } else if (action.equals(ACTION_UNSHARE)) { // Unshare file
329 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
330 if (remotePath.length() > 0) {
331 operation = new UnshareLinkOperation(
332 remotePath,
333 OperationsService.this);
334 }
335
336 } else if (action.equals(ACTION_GET_SERVER_INFO)) {
337 // check OC server and get basic information from it
338 String authTokenType =
339 operationIntent.getStringExtra(EXTRA_AUTH_TOKEN_TYPE);
340 operation = new GetServerInfoOperation(
341 serverUrl, authTokenType, OperationsService.this);
342
343 } else if (action.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN)) {
344 /// GET ACCESS TOKEN to the OAuth server
345 String oauth2QueryParameters =
346 operationIntent.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS);
347 operation = new OAuth2GetAccessToken(
348 getString(R.string.oauth2_client_id),
349 getString(R.string.oauth2_redirect_uri),
350 getString(R.string.oauth2_grant_type),
351 oauth2QueryParameters);
352
353 } else if (action.equals(ACTION_EXISTENCE_CHECK)) {
354 // Existence Check
355 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
356 boolean successIfAbsent = operationIntent.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT, true);
357 operation = new ExistenceCheckRemoteOperation(remotePath, OperationsService.this, successIfAbsent);
358
359 } else if (action.equals(ACTION_GET_USER_NAME)) {
360 // Get User Name
361 operation = new GetRemoteUserNameOperation();
362
363 } else if (action.equals(ACTION_RENAME)) {
364 // Rename file or folder
365 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
366 String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
367 operation = new RenameFileOperation(remotePath, account, newName);
368
369 } else if (action.equals(ACTION_REMOVE)) {
370 // Remove file or folder
371 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
372 boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
373 operation = new RemoveFileOperation(remotePath, onlyLocalCopy);
374
375 } else if (action.equals(ACTION_CREATE_FOLDER)) {
376 // Create Folder
377 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
378 boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
379 operation = new CreateFolderOperation(remotePath, createFullPath);
380
381 } else if (action.equals(ACTION_SYNC_FILE)) {
382 // Sync file
383 String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
384 boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
385 operation = new SynchronizeFileOperation(remotePath, account, syncFileContents, getApplicationContext());
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 = ((MainApp)getApplicationContext()).
475 getOwnCloudClientManager().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 credentials = OwnCloudCredentialsFactory.newBasicCredentials(
484 mLastTarget.mUsername,
485 mLastTarget.mPassword); // basic
486
487 } else if (mLastTarget.mAuthToken != null) {
488 credentials = OwnCloudCredentialsFactory.newBearerCredentials(
489 mLastTarget.mAuthToken); // bearer token
490
491 } else if (mLastTarget.mCookie != null) {
492 credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(
493 mLastTarget.mCookie); // SAML SSO
494 }
495 OwnCloudAccount ocAccount = new OwnCloudAccount(
496 mLastTarget.mServerUrl, credentials);
497 mOwnCloudClient = ((MainApp)getApplicationContext()).
498 getOwnCloudClientManager().getClientFor(ocAccount, this);
499 mOwnCloudClient.setFollowRedirects(mLastTarget.mFollowRedirects);
500 mStorageManager = null;
501 }
502 }
503
504 /// perform the operation
505 if (mCurrentOperation instanceof SyncOperation) {
506 result = ((SyncOperation)mCurrentOperation).execute(mOwnCloudClient, mStorageManager);
507 } else {
508 result = mCurrentOperation.execute(mOwnCloudClient);
509 }
510
511 } catch (AccountsException e) {
512 if (mLastTarget.mAccount == null) {
513 Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
514 } else {
515 Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
516 }
517 result = new RemoteOperationResult(e);
518
519 } catch (IOException e) {
520 if (mLastTarget.mAccount == null) {
521 Log_OC.e(TAG, "Error while trying to get authorization for a NULL account", e);
522 } else {
523 Log_OC.e(TAG, "Error while trying to get authorization for " + mLastTarget.mAccount.name, e);
524 }
525 result = new RemoteOperationResult(e);
526 } catch (Exception e) {
527 if (mLastTarget.mAccount == null) {
528 Log_OC.e(TAG, "Unexpected error for a NULL account", e);
529 } else {
530 Log_OC.e(TAG, "Unexpected error for " + mLastTarget.mAccount.name, e);
531 }
532 result = new RemoteOperationResult(e);
533
534 } finally {
535 synchronized(mPendingOperations) {
536 mPendingOperations.poll();
537 }
538 }
539
540 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
541 dispatchResultToOperationListeners(mLastTarget, mCurrentOperation, result);
542 }
543 }
544
545
546 /**
547 * Sends a broadcast when a new operation is added to the queue.
548 *
549 * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\
550 *
551 * @param target Account or URL pointing to an OC server.
552 * @param operation Added operation.
553 */
554 private void sendBroadcastNewOperation(Target target, RemoteOperation operation) {
555 Intent intent = new Intent(ACTION_OPERATION_ADDED);
556 if (target.mAccount != null) {
557 intent.putExtra(EXTRA_ACCOUNT, target.mAccount);
558 } else {
559 intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl);
560 }
561 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
562 //lbm.sendBroadcast(intent);
563 sendStickyBroadcast(intent);
564 }
565
566
567 // TODO - maybe add a notification for real start of operations
568
569 /**
570 * Sends a LOCAL broadcast when an operations finishes in order to the interested activities can update their view
571 *
572 * Local broadcasts are only delivered to activities in the same process.
573 *
574 * @param target Account or URL pointing to an OC server.
575 * @param operation Finished operation.
576 * @param result Result of the operation.
577 */
578 private void sendBroadcastOperationFinished(Target target, RemoteOperation operation, RemoteOperationResult result) {
579 Intent intent = new Intent(ACTION_OPERATION_FINISHED);
580 intent.putExtra(EXTRA_RESULT, result);
581 if (target.mAccount != null) {
582 intent.putExtra(EXTRA_ACCOUNT, target.mAccount);
583 } else {
584 intent.putExtra(EXTRA_SERVER_URL, target.mServerUrl);
585 }
586 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
587 //lbm.sendBroadcast(intent);
588 sendStickyBroadcast(intent);
589 }
590
591
592 /**
593 * Notifies the currently subscribed listeners about the end of an operation.
594 *
595 * @param target Account or URL pointing to an OC server.
596 * @param operation Finished operation.
597 * @param result Result of the operation.
598 */
599 private void dispatchResultToOperationListeners(
600 Target target, final RemoteOperation operation, final RemoteOperationResult result) {
601 int count = 0;
602 Iterator<OnRemoteOperationListener> listeners = mBinder.mBoundListeners.keySet().iterator();
603 while (listeners.hasNext()) {
604 final OnRemoteOperationListener listener = listeners.next();
605 final Handler handler = mBinder.mBoundListeners.get(listener);
606 if (handler != null) {
607 handler.post(new Runnable() {
608 @Override
609 public void run() {
610 listener.onRemoteOperationFinish(operation, result);
611 }
612 });
613 count += 1;
614 }
615 }
616 if (count == 0) {
617 //mOperationResults.put(operation.hashCode(), result);
618 Pair<RemoteOperation, RemoteOperationResult> undispatched =
619 new Pair<RemoteOperation, RemoteOperationResult>(operation, result);
620 mUndispatchedFinishedOperations.put(operation.hashCode(), undispatched);
621 }
622 Log_OC.d(TAG, "Called " + count + " listeners");
623 }
624
625
626 }