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