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