1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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.
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/>.
18 package com
.owncloud
.android
.services
;
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
;
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
.files
.ExistenceCheckRemoteOperation
;
41 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
;
42 import com
.owncloud
.android
.lib
.resources
.users
.GetRemoteUserNameOperation
;
43 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
44 import com
.owncloud
.android
.operations
.CreateFolderOperation
;
45 import com
.owncloud
.android
.operations
.CreateShareOperation
;
46 import com
.owncloud
.android
.operations
.GetServerInfoOperation
;
47 import com
.owncloud
.android
.operations
.MoveFileOperation
;
48 import com
.owncloud
.android
.operations
.OAuth2GetAccessToken
;
49 import com
.owncloud
.android
.operations
.RemoveFileOperation
;
50 import com
.owncloud
.android
.operations
.RenameFileOperation
;
51 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
52 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
53 import com
.owncloud
.android
.operations
.UnshareLinkOperation
;
55 import android
.accounts
.Account
;
56 import android
.accounts
.AccountsException
;
57 import android
.accounts
.AuthenticatorException
;
58 import android
.accounts
.OperationCanceledException
;
59 import android
.app
.Service
;
60 import android
.content
.Intent
;
61 import android
.net
.Uri
;
62 import android
.os
.Binder
;
63 import android
.os
.Handler
;
64 import android
.os
.HandlerThread
;
65 import android
.os
.IBinder
;
66 import android
.os
.Looper
;
67 import android
.os
.Message
;
68 import android
.os
.Process
;
69 import android
.util
.Pair
;
71 public class OperationsService
extends Service
{
73 private static final String TAG
= OperationsService
.class.getSimpleName();
75 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
76 public static final String EXTRA_SERVER_URL
= "SERVER_URL";
77 public static final String EXTRA_OAUTH2_QUERY_PARAMETERS
= "OAUTH2_QUERY_PARAMETERS";
78 public static final String EXTRA_REMOTE_PATH
= "REMOTE_PATH";
79 public static final String EXTRA_SEND_INTENT
= "SEND_INTENT";
80 public static final String EXTRA_NEWNAME
= "NEWNAME";
81 public static final String EXTRA_REMOVE_ONLY_LOCAL
= "REMOVE_LOCAL_COPY";
82 public static final String EXTRA_CREATE_FULL_PATH
= "CREATE_FULL_PATH";
83 public static final String EXTRA_SYNC_FILE_CONTENTS
= "SYNC_FILE_CONTENTS";
84 public static final String EXTRA_RESULT
= "RESULT";
85 public static final String EXTRA_NEW_PARENT_PATH
= "NEW_PARENT_PATH";
87 // TODO review if ALL OF THEM are necessary
88 public static final String EXTRA_SUCCESS_IF_ABSENT
= "SUCCESS_IF_ABSENT";
89 public static final String EXTRA_USERNAME
= "USERNAME";
90 public static final String EXTRA_PASSWORD
= "PASSWORD";
91 public static final String EXTRA_AUTH_TOKEN
= "AUTH_TOKEN";
92 public static final String EXTRA_COOKIE
= "COOKIE";
94 public static final String ACTION_CREATE_SHARE
= "CREATE_SHARE";
95 public static final String ACTION_UNSHARE
= "UNSHARE";
96 public static final String ACTION_GET_SERVER_INFO
= "GET_SERVER_INFO";
97 public static final String ACTION_OAUTH2_GET_ACCESS_TOKEN
= "OAUTH2_GET_ACCESS_TOKEN";
98 public static final String ACTION_EXISTENCE_CHECK
= "EXISTENCE_CHECK";
99 public static final String ACTION_GET_USER_NAME
= "GET_USER_NAME";
100 public static final String ACTION_RENAME
= "RENAME";
101 public static final String ACTION_REMOVE
= "REMOVE";
102 public static final String ACTION_CREATE_FOLDER
= "CREATE_FOLDER";
103 public static final String ACTION_SYNC_FILE
= "SYNC_FILE";
104 public static final String ACTION_SYNC_FOLDER
= "SYNC_FOLDER"; // for the moment, just to download
105 public static final String ACTION_CANCEL_SYNC_FOLDER
= "CANCEL_SYNC_FOLDER"; // for the moment, just to download
106 public static final String ACTION_MOVE_FILE
= "MOVE_FILE";
108 public static final String ACTION_OPERATION_ADDED
= OperationsService
.class.getName() + ".OPERATION_ADDED";
109 public static final String ACTION_OPERATION_FINISHED
= OperationsService
.class.getName() + ".OPERATION_FINISHED";
112 private ConcurrentMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>>
113 mUndispatchedFinishedOperations
=
114 new ConcurrentHashMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>>();
116 private static class Target
{
117 public Uri mServerUrl
= null
;
118 public Account mAccount
= null
;
119 public String mUsername
= null
;
120 public String mPassword
= null
;
121 public String mAuthToken
= null
;
122 public String mCookie
= null
;
124 public Target(Account account
, Uri serverUrl
, String username
, String password
, String authToken
,
127 mServerUrl
= serverUrl
;
128 mUsername
= username
;
129 mPassword
= password
;
130 mAuthToken
= authToken
;
135 private ServiceHandler mOperationsHandler
;
136 private OperationsServiceBinder mOperationsBinder
;
138 private SyncFolderHandler mSyncFolderHandler
;
141 * Service initialization
144 public void onCreate() {
146 /// First worker thread for most of operations
147 HandlerThread thread
= new HandlerThread("Operations thread", Process
.THREAD_PRIORITY_BACKGROUND
);
149 mOperationsHandler
= new ServiceHandler(thread
.getLooper(), this);
150 mOperationsBinder
= new OperationsServiceBinder(mOperationsHandler
);
152 /// Separated worker thread for download of folders (WIP)
153 thread
= new HandlerThread("Syncfolder thread", Process
.THREAD_PRIORITY_BACKGROUND
);
155 mSyncFolderHandler
= new SyncFolderHandler(thread
.getLooper(), this);
160 * Entry point to add a new operation to the queue of operations.
162 * New operations are added calling to startService(), resulting in a call to this method.
163 * This ensures the service will keep on working although the caller activity goes away.
166 public int onStartCommand(Intent intent
, int flags
, int startId
) {
167 // WIP: for the moment, only SYNC_FOLDER and CANCEL_SYNC_FOLDER is expected here;
168 // the rest of the operations are requested through the Binder
169 if (ACTION_SYNC_FOLDER
.equals(intent
.getAction())) {
170 if (!intent
.hasExtra(EXTRA_ACCOUNT
) || !intent
.hasExtra(EXTRA_REMOTE_PATH
)) {
171 Log_OC
.e(TAG
, "Not enough information provided in intent");
172 return START_NOT_STICKY
;
174 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
175 OCFile file
= intent
.getParcelableExtra(EXTRA_REMOTE_PATH
);
176 Pair
<Account
, OCFile
> itemSyncKey
= new Pair
<Account
, OCFile
>(account
, file
);
178 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(intent
);
179 if (itemToQueue
!= null
) {
180 mSyncFolderHandler
.add(account
, file
, (SynchronizeFolderOperation
)itemToQueue
.second
);
181 Message msg
= mSyncFolderHandler
.obtainMessage();
183 msg
.obj
= itemSyncKey
;
184 mSyncFolderHandler
.sendMessage(msg
);
186 } else if (ACTION_CANCEL_SYNC_FOLDER
.equals(intent
.getAction())) {
187 if (!intent
.hasExtra(EXTRA_ACCOUNT
) || !intent
.hasExtra(EXTRA_REMOTE_PATH
)) {
188 Log_OC
.e(TAG
, "Not enough information provided in intent");
189 return START_NOT_STICKY
;
191 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
192 OCFile file
= intent
.getParcelableExtra(EXTRA_REMOTE_PATH
);
195 mSyncFolderHandler
.cancel(account
,file
);
197 Message msg
= mOperationsHandler
.obtainMessage();
199 mOperationsHandler
.sendMessage(msg
);
202 return START_NOT_STICKY
;
206 public void onDestroy() {
207 //Log_OC.wtf(TAG, "onDestroy init" );
210 OwnCloudClientManagerFactory
.getDefaultSingleton().
211 saveAllClients(this, MainApp
.getAccountType());
213 // TODO - get rid of these exceptions
214 } catch (AccountNotFoundException e
) {
216 } catch (AuthenticatorException e
) {
218 } catch (OperationCanceledException e
) {
220 } catch (IOException e
) {
224 //Log_OC.wtf(TAG, "Clear mUndispatchedFinisiedOperations" );
225 mUndispatchedFinishedOperations
.clear();
227 //Log_OC.wtf(TAG, "onDestroy end" );
232 * Provides a binder object that clients can use to perform actions on the queue of operations,
233 * except the addition of new operations.
236 public IBinder
onBind(Intent intent
) {
237 //Log_OC.wtf(TAG, "onBind" );
238 return mOperationsBinder
;
243 * Called when ALL the bound clients were unbound.
246 public boolean onUnbind(Intent intent
) {
247 ((OperationsServiceBinder
)mOperationsBinder
).clearListeners();
248 return false
; // not accepting rebinding (default behaviour)
253 * Binder to let client components to perform actions on the queue of operations.
255 * It provides by itself the available operations.
257 public class OperationsServiceBinder
extends Binder
/* implements OnRemoteOperationListener */ {
260 * Map of listeners that will be reported about the end of operations from a {@link OperationsServiceBinder} instance
262 private ConcurrentMap
<OnRemoteOperationListener
, Handler
> mBoundListeners
=
263 new ConcurrentHashMap
<OnRemoteOperationListener
, Handler
>();
265 private ServiceHandler mServiceHandler
= null
;
268 public OperationsServiceBinder(ServiceHandler serviceHandler
) {
269 mServiceHandler
= serviceHandler
;
274 * Cancels an operation
278 public void cancel() {
283 public void clearListeners() {
285 mBoundListeners
.clear();
290 * Adds a listener interested in being reported about the end of operations.
292 * @param listener Object to notify about the end of operations.
293 * @param callbackHandler {@link Handler} to access the listener without breaking Android threading protection.
295 public void addOperationListener (OnRemoteOperationListener listener
, Handler callbackHandler
) {
296 synchronized (mBoundListeners
) {
297 mBoundListeners
.put(listener
, callbackHandler
);
303 * Removes a listener from the list of objects interested in the being reported about the end of operations.
305 * @param listener Object to notify about progress of transfer.
307 public void removeOperationListener (OnRemoteOperationListener listener
) {
308 synchronized (mBoundListeners
) {
309 mBoundListeners
.remove(listener
);
315 * TODO - IMPORTANT: update implementation when more operations are moved into the service
317 * @return 'True' when an operation that enforces the user to wait for completion is in process.
319 public boolean isPerformingBlockingOperation() {
320 return (!mServiceHandler
.mPendingOperations
.isEmpty());
325 * Creates and adds to the queue a new operation, as described by operationIntent.
327 * Calls startService to make the operation is processed by the ServiceHandler.
329 * @param operationIntent Intent describing a new operation to queue and execute.
330 * @return Identifier of the operation created, or null if failed.
332 public long queueNewOperation(Intent operationIntent
) {
333 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(operationIntent
);
334 if (itemToQueue
!= null
) {
335 mServiceHandler
.mPendingOperations
.add(itemToQueue
);
336 startService(new Intent(OperationsService
.this, OperationsService
.class));
337 return itemToQueue
.second
.hashCode();
340 return Long
.MAX_VALUE
;
345 public boolean dispatchResultIfFinished(int operationId
, OnRemoteOperationListener listener
) {
346 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
347 mUndispatchedFinishedOperations
.remove(operationId
);
348 if (undispatched
!= null
) {
349 listener
.onRemoteOperationFinish(undispatched
.first
, undispatched
.second
);
351 //Log_OC.wtf(TAG, "Sending callback later");
353 if (!mServiceHandler
.mPendingOperations
.isEmpty()) {
358 //Log_OC.wtf(TAG, "Not finished yet");
366 * SyncFolder worker. Performs the pending operations in the order they were requested.
368 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
370 private static class SyncFolderHandler
extends Handler
{
372 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
374 OperationsService mService
;
376 private ConcurrentMap
<String
,SynchronizeFolderOperation
> mPendingOperations
=
377 new ConcurrentHashMap
<String
,SynchronizeFolderOperation
>();
378 private RemoteOperation mCurrentOperation
= null
;
379 private OwnCloudClient mOwnCloudClient
= null
;
380 private FileDataStorageManager mStorageManager
;
381 private SynchronizeFolderOperation mCurrentSyncOperation
;
384 public SyncFolderHandler(Looper looper
, OperationsService service
) {
386 if (service
== null
) {
387 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
393 public void handleMessage(Message msg
) {
394 Pair
<Account
, OCFile
> itemSyncKey
= (Pair
<Account
, OCFile
>) msg
.obj
;
395 nextOperation(itemSyncKey
.first
,itemSyncKey
.second
);
396 mService
.stopSelf(msg
.arg1
);
401 * Performs the next operation in the queue
403 private void nextOperation(Account account
, OCFile file
) {
405 String syncKey
= buildRemoteName(account
,file
);
407 synchronized(mPendingOperations
) {
408 mCurrentSyncOperation
= mPendingOperations
.get(syncKey
);
411 if (mCurrentSyncOperation
!= null
) {
413 RemoteOperationResult operationResult
= null
;
416 OwnCloudAccount ocAccount
= new OwnCloudAccount(account
, mService
);
417 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
418 getClientFor(ocAccount
, mService
);
419 mStorageManager
= new FileDataStorageManager(
421 mService
.getContentResolver()
425 /// perform the operation
426 if (mCurrentOperation
instanceof SyncOperation
) {
427 operationResult
= ((SyncOperation
)mCurrentOperation
).execute(mOwnCloudClient
, mStorageManager
);
429 operationResult
= mCurrentOperation
.execute(mOwnCloudClient
);
432 } catch (AccountsException e
) {
433 Log_OC
.e(TAG
, "Error while trying to get autorization", e
);
434 operationResult
= new RemoteOperationResult(e
);
435 } catch (IOException e
) {
436 Log_OC
.e(TAG
, "Error while trying to get autorization", e
);
437 operationResult
= new RemoteOperationResult(e
);
440 synchronized(mPendingOperations
) {
441 mPendingOperations
.remove(syncKey
);
445 /// TODO notify operation result if needed
450 public void add(Account account
, OCFile file
, SynchronizeFolderOperation syncFolderOperation
){
451 String syncKey
= buildRemoteName(account
,file
);
452 mPendingOperations
.putIfAbsent(syncKey
,syncFolderOperation
);
456 * Cancels a pending or current sync operation.
458 * @param account Owncloud account where the remote file is stored.
459 * @param file A file in the queue of pending downloads
461 public void cancel(Account account
, OCFile file
) {
462 SynchronizeFolderOperation syncOperation
= null
;
463 synchronized (mPendingOperations
) {
464 syncOperation
= mPendingOperations
.remove(buildRemoteName(account
, file
));
466 if (syncOperation
!= null
) {
467 syncOperation
.cancel();
472 * Builds a key from the account and file to download
474 * @param account Account where the file to download is stored
475 * @param file File to download
477 private String
buildRemoteName(Account account
, OCFile file
) {
478 return account
.name
+ file
.getRemotePath();
484 * Operations worker. Performs the pending operations in the order they were requested.
486 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
488 private static class ServiceHandler
extends Handler
{
489 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
492 OperationsService mService
;
495 private ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>> mPendingOperations
=
496 new ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>>();
497 private RemoteOperation mCurrentOperation
= null
;
498 private Target mLastTarget
= null
;
499 private OwnCloudClient mOwnCloudClient
= null
;
500 private FileDataStorageManager mStorageManager
;
503 public ServiceHandler(Looper looper
, OperationsService service
) {
505 if (service
== null
) {
506 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
512 public void handleMessage(Message msg
) {
514 mService
.stopSelf(msg
.arg1
);
519 * Performs the next operation in the queue
521 private void nextOperation() {
523 //Log_OC.wtf(TAG, "nextOperation init" );
525 Pair
<Target
, RemoteOperation
> next
= null
;
526 synchronized(mPendingOperations
) {
527 next
= mPendingOperations
.peek();
532 mCurrentOperation
= next
.second
;
533 RemoteOperationResult result
= null
;
535 /// prepare client object to send the request to the ownCloud server
536 if (mLastTarget
== null
|| !mLastTarget
.equals(next
.first
)) {
537 mLastTarget
= next
.first
;
538 if (mLastTarget
.mAccount
!= null
) {
539 OwnCloudAccount ocAccount
= new OwnCloudAccount(mLastTarget
.mAccount
, mService
);
540 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
541 getClientFor(ocAccount
, mService
);
542 mStorageManager
= new FileDataStorageManager(
543 mLastTarget
.mAccount
,
544 mService
.getContentResolver()
547 OwnCloudCredentials credentials
= null
;
548 if (mLastTarget
.mUsername
!= null
&&
549 mLastTarget
.mUsername
.length() > 0) {
550 credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(
551 mLastTarget
.mUsername
,
552 mLastTarget
.mPassword
); // basic
554 } else if (mLastTarget
.mAuthToken
!= null
&&
555 mLastTarget
.mAuthToken
.length() > 0) {
556 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
557 mLastTarget
.mAuthToken
); // bearer token
559 } else if (mLastTarget
.mCookie
!= null
&&
560 mLastTarget
.mCookie
.length() > 0) {
561 credentials
= OwnCloudCredentialsFactory
.newSamlSsoCredentials(
562 mLastTarget
.mCookie
); // SAML SSO
564 OwnCloudAccount ocAccount
= new OwnCloudAccount(
565 mLastTarget
.mServerUrl
, credentials
);
566 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
567 getClientFor(ocAccount
, mService
);
568 mStorageManager
= null
;
572 /// perform the operation
573 if (mCurrentOperation
instanceof SyncOperation
) {
574 result
= ((SyncOperation
)mCurrentOperation
).execute(mOwnCloudClient
, mStorageManager
);
576 result
= mCurrentOperation
.execute(mOwnCloudClient
);
579 } catch (AccountsException e
) {
580 if (mLastTarget
.mAccount
== null
) {
581 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
583 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
585 result
= new RemoteOperationResult(e
);
587 } catch (IOException e
) {
588 if (mLastTarget
.mAccount
== null
) {
589 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
591 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
593 result
= new RemoteOperationResult(e
);
594 } catch (Exception e
) {
595 if (mLastTarget
.mAccount
== null
) {
596 Log_OC
.e(TAG
, "Unexpected error for a NULL account", e
);
598 Log_OC
.e(TAG
, "Unexpected error for " + mLastTarget
.mAccount
.name
, e
);
600 result
= new RemoteOperationResult(e
);
603 synchronized(mPendingOperations
) {
604 mPendingOperations
.poll();
608 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
609 mService
.dispatchResultToOperationListeners(mLastTarget
, mCurrentOperation
, result
);
619 * Creates a new operation, as described by operationIntent.
621 * TODO - move to ServiceHandler (probably)
623 * @param operationIntent Intent describing a new operation to queue and execute.
624 * @return Pair with the new operation object and the information about its target server.
626 private Pair
<Target
, RemoteOperation
> newOperation(Intent operationIntent
) {
627 RemoteOperation operation
= null
;
628 Target target
= null
;
630 if (!operationIntent
.hasExtra(EXTRA_ACCOUNT
) &&
631 !operationIntent
.hasExtra(EXTRA_SERVER_URL
)) {
632 Log_OC
.e(TAG
, "Not enough information provided in intent");
635 Account account
= operationIntent
.getParcelableExtra(EXTRA_ACCOUNT
);
636 String serverUrl
= operationIntent
.getStringExtra(EXTRA_SERVER_URL
);
637 String username
= operationIntent
.getStringExtra(EXTRA_USERNAME
);
638 String password
= operationIntent
.getStringExtra(EXTRA_PASSWORD
);
639 String authToken
= operationIntent
.getStringExtra(EXTRA_AUTH_TOKEN
);
640 String cookie
= operationIntent
.getStringExtra(EXTRA_COOKIE
);
643 (serverUrl
== null
) ? null
: Uri
.parse(serverUrl
),
650 String action
= operationIntent
.getAction();
651 if (action
.equals(ACTION_CREATE_SHARE
)) { // Create Share
652 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
653 Intent sendIntent
= operationIntent
.getParcelableExtra(EXTRA_SEND_INTENT
);
654 if (remotePath
.length() > 0) {
655 operation
= new CreateShareOperation(remotePath
, ShareType
.PUBLIC_LINK
,
656 "", false
, "", 1, sendIntent
);
659 } else if (action
.equals(ACTION_UNSHARE
)) { // Unshare file
660 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
661 if (remotePath
.length() > 0) {
662 operation
= new UnshareLinkOperation(
664 OperationsService
.this);
667 } else if (action
.equals(ACTION_GET_SERVER_INFO
)) {
668 // check OC server and get basic information from it
669 operation
= new GetServerInfoOperation(serverUrl
, OperationsService
.this);
671 } else if (action
.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN
)) {
672 /// GET ACCESS TOKEN to the OAuth server
673 String oauth2QueryParameters
=
674 operationIntent
.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS
);
675 operation
= new OAuth2GetAccessToken(
676 getString(R
.string
.oauth2_client_id
),
677 getString(R
.string
.oauth2_redirect_uri
),
678 getString(R
.string
.oauth2_grant_type
),
679 oauth2QueryParameters
);
681 } else if (action
.equals(ACTION_EXISTENCE_CHECK
)) {
683 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
684 boolean successIfAbsent
= operationIntent
.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT
, false
);
685 operation
= new ExistenceCheckRemoteOperation(remotePath
, OperationsService
.this, successIfAbsent
);
687 } else if (action
.equals(ACTION_GET_USER_NAME
)) {
689 operation
= new GetRemoteUserNameOperation();
691 } else if (action
.equals(ACTION_RENAME
)) {
692 // Rename file or folder
693 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
694 String newName
= operationIntent
.getStringExtra(EXTRA_NEWNAME
);
695 operation
= new RenameFileOperation(remotePath
, newName
);
697 } else if (action
.equals(ACTION_REMOVE
)) {
698 // Remove file or folder
699 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
700 boolean onlyLocalCopy
= operationIntent
.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL
, false
);
701 operation
= new RemoveFileOperation(remotePath
, onlyLocalCopy
);
703 } else if (action
.equals(ACTION_CREATE_FOLDER
)) {
705 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
706 boolean createFullPath
= operationIntent
.getBooleanExtra(EXTRA_CREATE_FULL_PATH
, true
);
707 operation
= new CreateFolderOperation(remotePath
, createFullPath
);
709 } else if (action
.equals(ACTION_SYNC_FILE
)) {
711 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
712 boolean syncFileContents
= operationIntent
.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS
, true
);
713 operation
= new SynchronizeFileOperation(
714 remotePath
, account
, syncFileContents
, getApplicationContext()
717 } else if (action
.equals(ACTION_SYNC_FOLDER
)) {
719 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
720 operation
= new SynchronizeFolderOperation(
721 this, // TODO remove this dependency from construction time
724 System
.currentTimeMillis() // TODO remove this dependency from construction time
727 } else if (action
.equals(ACTION_MOVE_FILE
)) {
729 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
730 String newParentPath
= operationIntent
.getStringExtra(EXTRA_NEW_PARENT_PATH
);
731 operation
= new MoveFileOperation(remotePath
,newParentPath
,account
);
736 } catch (IllegalArgumentException e
) {
737 Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage());
741 if (operation
!= null
) {
742 return new Pair
<Target
, RemoteOperation
>(target
, operation
);
750 * Sends a broadcast when a new operation is added to the queue.
752 * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\
754 * @param target Account or URL pointing to an OC server.
755 * @param operation Added operation.
757 private void sendBroadcastNewOperation(Target target
, RemoteOperation operation
) {
758 Intent intent
= new Intent(ACTION_OPERATION_ADDED
);
759 if (target
.mAccount
!= null
) {
760 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
762 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
764 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
765 //lbm.sendBroadcast(intent);
766 sendStickyBroadcast(intent
);
770 // TODO - maybe add a notification for real start of operations
773 * Sends a LOCAL broadcast when an operations finishes in order to the interested activities can update their view
775 * Local broadcasts are only delivered to activities in the same process.
777 * @param target Account or URL pointing to an OC server.
778 * @param operation Finished operation.
779 * @param result Result of the operation.
781 private void sendBroadcastOperationFinished(Target target
, RemoteOperation operation
, RemoteOperationResult result
) {
782 Intent intent
= new Intent(ACTION_OPERATION_FINISHED
);
783 intent
.putExtra(EXTRA_RESULT
, result
);
784 if (target
.mAccount
!= null
) {
785 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
787 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
789 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
790 //lbm.sendBroadcast(intent);
791 sendStickyBroadcast(intent
);
796 * Notifies the currently subscribed listeners about the end of an operation.
798 * @param target Account or URL pointing to an OC server.
799 * @param operation Finished operation.
800 * @param result Result of the operation.
802 private void dispatchResultToOperationListeners(
803 Target target
, final RemoteOperation operation
, final RemoteOperationResult result
) {
805 Iterator
<OnRemoteOperationListener
> listeners
= mOperationsBinder
.mBoundListeners
.keySet().iterator();
806 while (listeners
.hasNext()) {
807 final OnRemoteOperationListener listener
= listeners
.next();
808 final Handler handler
= mOperationsBinder
.mBoundListeners
.get(listener
);
809 if (handler
!= null
) {
810 handler
.post(new Runnable() {
813 listener
.onRemoteOperationFinish(operation
, result
);
820 //mOperationResults.put(operation.hashCode(), result);
821 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
822 new Pair
<RemoteOperation
, RemoteOperationResult
>(operation
, result
);
823 mUndispatchedFinishedOperations
.put(operation
.hashCode(), undispatched
);
825 Log_OC
.d(TAG
, "Called " + count
+ " listeners");