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