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 String remotePath
= intent
.getStringExtra(EXTRA_REMOTE_PATH
);
177 OCFile file
= new OCFile(remotePath
);
178 Pair
<Account
, OCFile
> itemSyncKey
= new Pair
<Account
, OCFile
>(account
, file
);
180 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(intent
);
181 if (itemToQueue
!= null
) {
182 mSyncFolderHandler
.add(account
, file
, (SynchronizeFolderOperation
)itemToQueue
.second
);
183 Message msg
= mSyncFolderHandler
.obtainMessage();
185 msg
.obj
= itemSyncKey
;
186 mSyncFolderHandler
.sendMessage(msg
);
188 } else if (ACTION_CANCEL_SYNC_FOLDER
.equals(intent
.getAction())) {
189 if (!intent
.hasExtra(EXTRA_ACCOUNT
) || !intent
.hasExtra(EXTRA_REMOTE_PATH
)) {
190 Log_OC
.e(TAG
, "Not enough information provided in intent");
191 return START_NOT_STICKY
;
193 Account account
= intent
.getParcelableExtra(EXTRA_ACCOUNT
);
194 String remotePath
= intent
.getStringExtra(EXTRA_REMOTE_PATH
);
196 OCFile file
= new OCFile(remotePath
);
199 mSyncFolderHandler
.cancel(account
,file
);
201 Message msg
= mOperationsHandler
.obtainMessage();
203 mOperationsHandler
.sendMessage(msg
);
206 return START_NOT_STICKY
;
210 public void onDestroy() {
211 //Log_OC.wtf(TAG, "onDestroy init" );
214 OwnCloudClientManagerFactory
.getDefaultSingleton().
215 saveAllClients(this, MainApp
.getAccountType());
217 // TODO - get rid of these exceptions
218 } catch (AccountNotFoundException e
) {
220 } catch (AuthenticatorException e
) {
222 } catch (OperationCanceledException e
) {
224 } catch (IOException e
) {
228 //Log_OC.wtf(TAG, "Clear mUndispatchedFinisiedOperations" );
229 mUndispatchedFinishedOperations
.clear();
231 //Log_OC.wtf(TAG, "onDestroy end" );
236 * Provides a binder object that clients can use to perform actions on the queue of operations,
237 * except the addition of new operations.
240 public IBinder
onBind(Intent intent
) {
241 //Log_OC.wtf(TAG, "onBind" );
242 return mOperationsBinder
;
247 * Called when ALL the bound clients were unbound.
250 public boolean onUnbind(Intent intent
) {
251 ((OperationsServiceBinder
)mOperationsBinder
).clearListeners();
252 return false
; // not accepting rebinding (default behaviour)
257 * Binder to let client components to perform actions on the queue of operations.
259 * It provides by itself the available operations.
261 public class OperationsServiceBinder
extends Binder
/* implements OnRemoteOperationListener */ {
264 * Map of listeners that will be reported about the end of operations from a {@link OperationsServiceBinder} instance
266 private ConcurrentMap
<OnRemoteOperationListener
, Handler
> mBoundListeners
=
267 new ConcurrentHashMap
<OnRemoteOperationListener
, Handler
>();
269 private ServiceHandler mServiceHandler
= null
;
272 public OperationsServiceBinder(ServiceHandler serviceHandler
) {
273 mServiceHandler
= serviceHandler
;
278 * Cancels an operation
282 public void cancel() {
287 public void clearListeners() {
289 mBoundListeners
.clear();
294 * Adds a listener interested in being reported about the end of operations.
296 * @param listener Object to notify about the end of operations.
297 * @param callbackHandler {@link Handler} to access the listener without breaking Android threading protection.
299 public void addOperationListener (OnRemoteOperationListener listener
, Handler callbackHandler
) {
300 synchronized (mBoundListeners
) {
301 mBoundListeners
.put(listener
, callbackHandler
);
307 * Removes a listener from the list of objects interested in the being reported about the end of operations.
309 * @param listener Object to notify about progress of transfer.
311 public void removeOperationListener (OnRemoteOperationListener listener
) {
312 synchronized (mBoundListeners
) {
313 mBoundListeners
.remove(listener
);
319 * TODO - IMPORTANT: update implementation when more operations are moved into the service
321 * @return 'True' when an operation that enforces the user to wait for completion is in process.
323 public boolean isPerformingBlockingOperation() {
324 return (!mServiceHandler
.mPendingOperations
.isEmpty());
329 * Creates and adds to the queue a new operation, as described by operationIntent.
331 * Calls startService to make the operation is processed by the ServiceHandler.
333 * @param operationIntent Intent describing a new operation to queue and execute.
334 * @return Identifier of the operation created, or null if failed.
336 public long queueNewOperation(Intent operationIntent
) {
337 Pair
<Target
, RemoteOperation
> itemToQueue
= newOperation(operationIntent
);
338 if (itemToQueue
!= null
) {
339 mServiceHandler
.mPendingOperations
.add(itemToQueue
);
340 startService(new Intent(OperationsService
.this, OperationsService
.class));
341 return itemToQueue
.second
.hashCode();
344 return Long
.MAX_VALUE
;
349 public boolean dispatchResultIfFinished(int operationId
, OnRemoteOperationListener listener
) {
350 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
351 mUndispatchedFinishedOperations
.remove(operationId
);
352 if (undispatched
!= null
) {
353 listener
.onRemoteOperationFinish(undispatched
.first
, undispatched
.second
);
355 //Log_OC.wtf(TAG, "Sending callback later");
357 if (!mServiceHandler
.mPendingOperations
.isEmpty()) {
362 //Log_OC.wtf(TAG, "Not finished yet");
370 * SyncFolder worker. Performs the pending operations in the order they were requested.
372 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
374 private static class SyncFolderHandler
extends Handler
{
376 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
378 OperationsService mService
;
380 private ConcurrentMap
<String
,SynchronizeFolderOperation
> mPendingOperations
=
381 new ConcurrentHashMap
<String
,SynchronizeFolderOperation
>();
382 private OwnCloudClient mOwnCloudClient
= null
;
383 private FileDataStorageManager mStorageManager
;
384 private SynchronizeFolderOperation mCurrentSyncOperation
;
387 public SyncFolderHandler(Looper looper
, OperationsService service
) {
389 if (service
== null
) {
390 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
396 public void handleMessage(Message msg
) {
397 Pair
<Account
, OCFile
> itemSyncKey
= (Pair
<Account
, OCFile
>) msg
.obj
;
398 nextOperation(itemSyncKey
.first
,itemSyncKey
.second
);
399 mService
.stopSelf(msg
.arg1
);
404 * Performs the next operation in the queue
406 private void nextOperation(Account account
, OCFile file
) {
408 String syncKey
= buildRemoteName(account
,file
);
410 synchronized(mPendingOperations
) {
411 mCurrentSyncOperation
= mPendingOperations
.get(syncKey
);
414 if (mCurrentSyncOperation
!= null
) {
416 RemoteOperationResult operationResult
= null
;
419 OwnCloudAccount ocAccount
= new OwnCloudAccount(account
, mService
);
420 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
421 getClientFor(ocAccount
, mService
);
422 mStorageManager
= new FileDataStorageManager(
424 mService
.getContentResolver()
428 /// perform the operation
429 if (mCurrentSyncOperation
instanceof SyncOperation
) {
430 operationResult
= ((SyncOperation
)mCurrentSyncOperation
).execute(mOwnCloudClient
, mStorageManager
);
432 operationResult
= mCurrentSyncOperation
.execute(mOwnCloudClient
);
435 } catch (AccountsException e
) {
436 Log_OC
.e(TAG
, "Error while trying to get autorization", e
);
437 operationResult
= new RemoteOperationResult(e
);
438 } catch (IOException e
) {
439 Log_OC
.e(TAG
, "Error while trying to get autorization", e
);
440 operationResult
= new RemoteOperationResult(e
);
443 synchronized(mPendingOperations
) {
444 mPendingOperations
.remove(syncKey
);
448 /// TODO notify operation result if needed
453 public void add(Account account
, OCFile file
, SynchronizeFolderOperation syncFolderOperation
){
454 String syncKey
= buildRemoteName(account
,file
);
455 mPendingOperations
.putIfAbsent(syncKey
,syncFolderOperation
);
459 * Cancels a pending or current sync operation.
461 * @param account Owncloud account where the remote file is stored.
462 * @param file A file in the queue of pending downloads
464 public void cancel(Account account
, OCFile file
) {
465 SynchronizeFolderOperation syncOperation
= null
;
466 synchronized (mPendingOperations
) {
467 syncOperation
= mPendingOperations
.remove(buildRemoteName(account
, file
));
469 if (syncOperation
!= null
) {
470 syncOperation
.cancel();
475 * Builds a key from the account and file to download
477 * @param account Account where the file to download is stored
478 * @param file File to download
480 private String
buildRemoteName(Account account
, OCFile file
) {
481 return account
.name
+ file
.getRemotePath();
487 * Operations worker. Performs the pending operations in the order they were requested.
489 * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.
491 private static class ServiceHandler
extends Handler
{
492 // don't make it a final class, and don't remove the static ; lint will warn about a possible memory leak
495 OperationsService mService
;
498 private ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>> mPendingOperations
=
499 new ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>>();
500 private RemoteOperation mCurrentOperation
= null
;
501 private Target mLastTarget
= null
;
502 private OwnCloudClient mOwnCloudClient
= null
;
503 private FileDataStorageManager mStorageManager
;
506 public ServiceHandler(Looper looper
, OperationsService service
) {
508 if (service
== null
) {
509 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
515 public void handleMessage(Message msg
) {
517 mService
.stopSelf(msg
.arg1
);
522 * Performs the next operation in the queue
524 private void nextOperation() {
526 //Log_OC.wtf(TAG, "nextOperation init" );
528 Pair
<Target
, RemoteOperation
> next
= null
;
529 synchronized(mPendingOperations
) {
530 next
= mPendingOperations
.peek();
535 mCurrentOperation
= next
.second
;
536 RemoteOperationResult result
= null
;
538 /// prepare client object to send the request to the ownCloud server
539 if (mLastTarget
== null
|| !mLastTarget
.equals(next
.first
)) {
540 mLastTarget
= next
.first
;
541 if (mLastTarget
.mAccount
!= null
) {
542 OwnCloudAccount ocAccount
= new OwnCloudAccount(mLastTarget
.mAccount
, mService
);
543 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
544 getClientFor(ocAccount
, mService
);
545 mStorageManager
= new FileDataStorageManager(
546 mLastTarget
.mAccount
,
547 mService
.getContentResolver()
550 OwnCloudCredentials credentials
= null
;
551 if (mLastTarget
.mUsername
!= null
&&
552 mLastTarget
.mUsername
.length() > 0) {
553 credentials
= OwnCloudCredentialsFactory
.newBasicCredentials(
554 mLastTarget
.mUsername
,
555 mLastTarget
.mPassword
); // basic
557 } else if (mLastTarget
.mAuthToken
!= null
&&
558 mLastTarget
.mAuthToken
.length() > 0) {
559 credentials
= OwnCloudCredentialsFactory
.newBearerCredentials(
560 mLastTarget
.mAuthToken
); // bearer token
562 } else if (mLastTarget
.mCookie
!= null
&&
563 mLastTarget
.mCookie
.length() > 0) {
564 credentials
= OwnCloudCredentialsFactory
.newSamlSsoCredentials(
565 mLastTarget
.mCookie
); // SAML SSO
567 OwnCloudAccount ocAccount
= new OwnCloudAccount(
568 mLastTarget
.mServerUrl
, credentials
);
569 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
570 getClientFor(ocAccount
, mService
);
571 mStorageManager
= null
;
575 /// perform the operation
576 if (mCurrentOperation
instanceof SyncOperation
) {
577 result
= ((SyncOperation
)mCurrentOperation
).execute(mOwnCloudClient
, mStorageManager
);
579 result
= mCurrentOperation
.execute(mOwnCloudClient
);
582 } catch (AccountsException e
) {
583 if (mLastTarget
.mAccount
== null
) {
584 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
586 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
588 result
= new RemoteOperationResult(e
);
590 } catch (IOException e
) {
591 if (mLastTarget
.mAccount
== null
) {
592 Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", e
);
594 Log_OC
.e(TAG
, "Error while trying to get authorization for " + mLastTarget
.mAccount
.name
, e
);
596 result
= new RemoteOperationResult(e
);
597 } catch (Exception e
) {
598 if (mLastTarget
.mAccount
== null
) {
599 Log_OC
.e(TAG
, "Unexpected error for a NULL account", e
);
601 Log_OC
.e(TAG
, "Unexpected error for " + mLastTarget
.mAccount
.name
, e
);
603 result
= new RemoteOperationResult(e
);
606 synchronized(mPendingOperations
) {
607 mPendingOperations
.poll();
611 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result);
612 mService
.dispatchResultToOperationListeners(mLastTarget
, mCurrentOperation
, result
);
622 * Creates a new operation, as described by operationIntent.
624 * TODO - move to ServiceHandler (probably)
626 * @param operationIntent Intent describing a new operation to queue and execute.
627 * @return Pair with the new operation object and the information about its target server.
629 private Pair
<Target
, RemoteOperation
> newOperation(Intent operationIntent
) {
630 RemoteOperation operation
= null
;
631 Target target
= null
;
633 if (!operationIntent
.hasExtra(EXTRA_ACCOUNT
) &&
634 !operationIntent
.hasExtra(EXTRA_SERVER_URL
)) {
635 Log_OC
.e(TAG
, "Not enough information provided in intent");
638 Account account
= operationIntent
.getParcelableExtra(EXTRA_ACCOUNT
);
639 String serverUrl
= operationIntent
.getStringExtra(EXTRA_SERVER_URL
);
640 String username
= operationIntent
.getStringExtra(EXTRA_USERNAME
);
641 String password
= operationIntent
.getStringExtra(EXTRA_PASSWORD
);
642 String authToken
= operationIntent
.getStringExtra(EXTRA_AUTH_TOKEN
);
643 String cookie
= operationIntent
.getStringExtra(EXTRA_COOKIE
);
646 (serverUrl
== null
) ? null
: Uri
.parse(serverUrl
),
653 String action
= operationIntent
.getAction();
654 if (action
.equals(ACTION_CREATE_SHARE
)) { // Create Share
655 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
656 Intent sendIntent
= operationIntent
.getParcelableExtra(EXTRA_SEND_INTENT
);
657 if (remotePath
.length() > 0) {
658 operation
= new CreateShareOperation(remotePath
, ShareType
.PUBLIC_LINK
,
659 "", false
, "", 1, sendIntent
);
662 } else if (action
.equals(ACTION_UNSHARE
)) { // Unshare file
663 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
664 if (remotePath
.length() > 0) {
665 operation
= new UnshareLinkOperation(
667 OperationsService
.this);
670 } else if (action
.equals(ACTION_GET_SERVER_INFO
)) {
671 // check OC server and get basic information from it
672 operation
= new GetServerInfoOperation(serverUrl
, OperationsService
.this);
674 } else if (action
.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN
)) {
675 /// GET ACCESS TOKEN to the OAuth server
676 String oauth2QueryParameters
=
677 operationIntent
.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS
);
678 operation
= new OAuth2GetAccessToken(
679 getString(R
.string
.oauth2_client_id
),
680 getString(R
.string
.oauth2_redirect_uri
),
681 getString(R
.string
.oauth2_grant_type
),
682 oauth2QueryParameters
);
684 } else if (action
.equals(ACTION_EXISTENCE_CHECK
)) {
686 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
687 boolean successIfAbsent
= operationIntent
.getBooleanExtra(EXTRA_SUCCESS_IF_ABSENT
, false
);
688 operation
= new ExistenceCheckRemoteOperation(remotePath
, OperationsService
.this, successIfAbsent
);
690 } else if (action
.equals(ACTION_GET_USER_NAME
)) {
692 operation
= new GetRemoteUserNameOperation();
694 } else if (action
.equals(ACTION_RENAME
)) {
695 // Rename file or folder
696 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
697 String newName
= operationIntent
.getStringExtra(EXTRA_NEWNAME
);
698 operation
= new RenameFileOperation(remotePath
, newName
);
700 } else if (action
.equals(ACTION_REMOVE
)) {
701 // Remove file or folder
702 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
703 boolean onlyLocalCopy
= operationIntent
.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL
, false
);
704 operation
= new RemoveFileOperation(remotePath
, onlyLocalCopy
);
706 } else if (action
.equals(ACTION_CREATE_FOLDER
)) {
708 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
709 boolean createFullPath
= operationIntent
.getBooleanExtra(EXTRA_CREATE_FULL_PATH
, true
);
710 operation
= new CreateFolderOperation(remotePath
, createFullPath
);
712 } else if (action
.equals(ACTION_SYNC_FILE
)) {
714 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
715 boolean syncFileContents
= operationIntent
.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS
, true
);
716 operation
= new SynchronizeFileOperation(
717 remotePath
, account
, syncFileContents
, getApplicationContext()
720 } else if (action
.equals(ACTION_SYNC_FOLDER
)) {
722 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
723 operation
= new SynchronizeFolderOperation(
724 this, // TODO remove this dependency from construction time
727 System
.currentTimeMillis() // TODO remove this dependency from construction time
730 } else if (action
.equals(ACTION_MOVE_FILE
)) {
732 String remotePath
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
);
733 String newParentPath
= operationIntent
.getStringExtra(EXTRA_NEW_PARENT_PATH
);
734 operation
= new MoveFileOperation(remotePath
,newParentPath
,account
);
739 } catch (IllegalArgumentException e
) {
740 Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage());
744 if (operation
!= null
) {
745 return new Pair
<Target
, RemoteOperation
>(target
, operation
);
753 * Sends a broadcast when a new operation is added to the queue.
755 * Local broadcasts are only delivered to activities in the same process, but can't be done sticky :\
757 * @param target Account or URL pointing to an OC server.
758 * @param operation Added operation.
760 private void sendBroadcastNewOperation(Target target
, RemoteOperation operation
) {
761 Intent intent
= new Intent(ACTION_OPERATION_ADDED
);
762 if (target
.mAccount
!= null
) {
763 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
765 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
767 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
768 //lbm.sendBroadcast(intent);
769 sendStickyBroadcast(intent
);
773 // TODO - maybe add a notification for real start of operations
776 * Sends a LOCAL broadcast when an operations finishes in order to the interested activities can update their view
778 * Local broadcasts are only delivered to activities in the same process.
780 * @param target Account or URL pointing to an OC server.
781 * @param operation Finished operation.
782 * @param result Result of the operation.
784 private void sendBroadcastOperationFinished(Target target
, RemoteOperation operation
, RemoteOperationResult result
) {
785 Intent intent
= new Intent(ACTION_OPERATION_FINISHED
);
786 intent
.putExtra(EXTRA_RESULT
, result
);
787 if (target
.mAccount
!= null
) {
788 intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
);
790 intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
);
792 //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
793 //lbm.sendBroadcast(intent);
794 sendStickyBroadcast(intent
);
799 * Notifies the currently subscribed listeners about the end of an operation.
801 * @param target Account or URL pointing to an OC server.
802 * @param operation Finished operation.
803 * @param result Result of the operation.
805 private void dispatchResultToOperationListeners(
806 Target target
, final RemoteOperation operation
, final RemoteOperationResult result
) {
808 Iterator
<OnRemoteOperationListener
> listeners
= mOperationsBinder
.mBoundListeners
.keySet().iterator();
809 while (listeners
.hasNext()) {
810 final OnRemoteOperationListener listener
= listeners
.next();
811 final Handler handler
= mOperationsBinder
.mBoundListeners
.get(listener
);
812 if (handler
!= null
) {
813 handler
.post(new Runnable() {
816 listener
.onRemoteOperationFinish(operation
, result
);
823 //mOperationResults.put(operation.hashCode(), result);
824 Pair
<RemoteOperation
, RemoteOperationResult
> undispatched
=
825 new Pair
<RemoteOperation
, RemoteOperationResult
>(operation
, result
);
826 mUndispatchedFinishedOperations
.put(operation
.hashCode(), undispatched
);
828 Log_OC
.d(TAG
, "Called " + count
+ " listeners");