2  *   ownCloud Android client application 
   4  *   Copyright (C) 2015 ownCloud Inc. 
   6  *   This program is free software: you can redistribute it and/or modify 
   7  *   it under the terms of the GNU General Public License version 2, 
   8  *   as published by the Free Software Foundation. 
  10  *   This program is distributed in the hope that it will be useful, 
  11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  13  *   GNU General Public License for more details. 
  15  *   You should have received a copy of the GNU General Public License 
  16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  20 package com
.owncloud
.android
.services
; 
  22 import android
.accounts
.Account
; 
  23 import android
.accounts
.AccountsException
; 
  24 import android
.accounts
.AuthenticatorException
; 
  25 import android
.accounts
.OperationCanceledException
; 
  26 import android
.app
.Service
; 
  27 import android
.content
.Intent
; 
  28 import android
.net
.Uri
; 
  29 import android
.os
.Binder
; 
  30 import android
.os
.Handler
; 
  31 import android
.os
.HandlerThread
; 
  32 import android
.os
.IBinder
; 
  33 import android
.os
.Looper
; 
  34 import android
.os
.Message
; 
  35 import android
.os
.Process
; 
  36 import android
.util
.Pair
; 
  38 import com
.owncloud
.android
.MainApp
; 
  39 import com
.owncloud
.android
.R
; 
  40 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
; 
  41 import com
.owncloud
.android
.datamodel
.OCFile
; 
  42 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
; 
  43 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
; 
  44 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
; 
  45 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
; 
  46 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentialsFactory
; 
  47 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
.AccountNotFoundException
; 
  48 import com
.owncloud
.android
.lib
.common
.operations
.OnRemoteOperationListener
; 
  49 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperation
; 
  50 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
; 
  51 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
; 
  52 import com
.owncloud
.android
.lib
.resources
.shares
.ShareType
; 
  53 import com
.owncloud
.android
.lib
.resources
.status
.OwnCloudVersion
; 
  54 import com
.owncloud
.android
.lib
.resources
.users
.GetRemoteUserNameOperation
; 
  55 import com
.owncloud
.android
.operations
.CopyFileOperation
; 
  56 import com
.owncloud
.android
.operations
.CreateFolderOperation
; 
  57 import com
.owncloud
.android
.operations
.CreateShareOperation
; 
  58 import com
.owncloud
.android
.operations
.GetServerInfoOperation
; 
  59 import com
.owncloud
.android
.operations
.MoveFileOperation
; 
  60 import com
.owncloud
.android
.operations
.OAuth2GetAccessToken
; 
  61 import com
.owncloud
.android
.operations
.RemoveFileOperation
; 
  62 import com
.owncloud
.android
.operations
.RenameFileOperation
; 
  63 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
; 
  64 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
; 
  65 import com
.owncloud
.android
.operations
.UnshareLinkOperation
; 
  66 import com
.owncloud
.android
.operations
.common
.SyncOperation
; 
  68 import java
.io
.IOException
; 
  69 import java
.util
.Iterator
; 
  70 import java
.util
.concurrent
.ConcurrentHashMap
; 
  71 import java
.util
.concurrent
.ConcurrentLinkedQueue
; 
  72 import java
.util
.concurrent
.ConcurrentMap
; 
  74 public class OperationsService 
extends Service 
{ 
  76     private static final String TAG 
= OperationsService
.class.getSimpleName(); 
  78     public static final String EXTRA_ACCOUNT 
= "ACCOUNT"; 
  79     public static final String EXTRA_SERVER_URL 
= "SERVER_URL"; 
  80     public static final String EXTRA_OAUTH2_QUERY_PARAMETERS 
= "OAUTH2_QUERY_PARAMETERS"; 
  81     public static final String EXTRA_REMOTE_PATH 
= "REMOTE_PATH"; 
  82     public static final String EXTRA_SEND_INTENT 
= "SEND_INTENT"; 
  83     public static final String EXTRA_NEWNAME 
= "NEWNAME"; 
  84     public static final String EXTRA_REMOVE_ONLY_LOCAL 
= "REMOVE_LOCAL_COPY"; 
  85     public static final String EXTRA_CREATE_FULL_PATH 
= "CREATE_FULL_PATH"; 
  86     public static final String EXTRA_SYNC_FILE_CONTENTS 
= "SYNC_FILE_CONTENTS"; 
  87     public static final String EXTRA_RESULT 
= "RESULT"; 
  88     public static final String EXTRA_NEW_PARENT_PATH 
= "NEW_PARENT_PATH"; 
  89     public static final String EXTRA_FILE 
= "FILE"; 
  90     public static final String EXTRA_PASSWORD_SHARE 
= "PASSWORD_SHARE"; 
  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_GET_USER_NAME 
= "GET_USER_NAME"; 
  99     public static final String ACTION_RENAME 
= "RENAME"; 
 100     public static final String ACTION_REMOVE 
= "REMOVE"; 
 101     public static final String ACTION_CREATE_FOLDER 
= "CREATE_FOLDER"; 
 102     public static final String ACTION_SYNC_FILE 
= "SYNC_FILE"; 
 103     public static final String ACTION_SYNC_FOLDER 
= "SYNC_FOLDER";//for the moment, just to download 
 104     public static final String ACTION_MOVE_FILE 
= "MOVE_FILE"; 
 105     public static final String ACTION_COPY_FILE 
= "COPY_FILE"; 
 107     public static final String ACTION_OPERATION_ADDED 
= OperationsService
.class.getName() + 
 109     public static final String ACTION_OPERATION_FINISHED 
= OperationsService
.class.getName() + 
 110             ".OPERATION_FINISHED"; 
 114     private ConcurrentMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>> 
 115             mUndispatchedFinishedOperations 
= 
 116             new ConcurrentHashMap
<Integer
, Pair
<RemoteOperation
, RemoteOperationResult
>>(); 
 118     private static class Target 
{ 
 119         public Uri mServerUrl 
= null
; 
 120         public Account mAccount 
= null
; 
 121         public String mCookie 
= null
; 
 123         public Target(Account account
, Uri serverUrl
, String cookie
) { 
 125             mServerUrl 
= serverUrl
; 
 130     private ServiceHandler mOperationsHandler
; 
 131     private OperationsServiceBinder mOperationsBinder
; 
 133     private SyncFolderHandler mSyncFolderHandler
; 
 136      * Service initialization 
 139     public void onCreate() { 
 141         Log_OC
.d(TAG
, "Creating service"); 
 143         /// First worker thread for most of operations  
 144         HandlerThread thread 
= new HandlerThread("Operations thread", 
 145                 Process
.THREAD_PRIORITY_BACKGROUND
); 
 147         mOperationsHandler 
= new ServiceHandler(thread
.getLooper(), this); 
 148         mOperationsBinder 
= new OperationsServiceBinder(mOperationsHandler
); 
 150         /// Separated worker thread for download of folders (WIP) 
 151         thread 
= new HandlerThread("Syncfolder thread", Process
.THREAD_PRIORITY_BACKGROUND
); 
 153         mSyncFolderHandler 
= new SyncFolderHandler(thread
.getLooper(), this); 
 158      * Entry point to add a new operation to the queue of operations. 
 160      * New operations are added calling to startService(), resulting in a call to this method. 
 161      * This ensures the service will keep on working although the caller activity goes away. 
 164     public int onStartCommand(Intent intent
, int flags
, int startId
) { 
 165         Log_OC
.d(TAG
, "Starting command with id " + startId
); 
 167         // WIP: for the moment, only SYNC_FOLDER is expected here; 
 168         // the rest of the operations are requested through the Binder 
 169         if (ACTION_SYNC_FOLDER
.equals(intent
.getAction())) { 
 171             if (!intent
.hasExtra(EXTRA_ACCOUNT
) || !intent
.hasExtra(EXTRA_REMOTE_PATH
)) { 
 172                 Log_OC
.e(TAG
, "Not enough information provided in intent"); 
 173                 return START_NOT_STICKY
; 
 175             Account account 
= intent
.getParcelableExtra(EXTRA_ACCOUNT
); 
 176             String remotePath 
= intent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 178             Pair
<Account
, String
> itemSyncKey 
=  new Pair
<Account 
, String
>(account
, remotePath
); 
 180             Pair
<Target
, RemoteOperation
> itemToQueue 
= newOperation(intent
); 
 181             if (itemToQueue 
!= null
) { 
 182                 mSyncFolderHandler
.add(account
, remotePath
, 
 183                         (SynchronizeFolderOperation
)itemToQueue
.second
); 
 184                 Message msg 
= mSyncFolderHandler
.obtainMessage(); 
 186                 msg
.obj 
= itemSyncKey
; 
 187                 mSyncFolderHandler
.sendMessage(msg
); 
 191             Message msg 
= mOperationsHandler
.obtainMessage(); 
 193             mOperationsHandler
.sendMessage(msg
); 
 196         return START_NOT_STICKY
; 
 200     public void onDestroy() { 
 201         Log_OC
.v(TAG
, "Destroying service" ); 
 204             OwnCloudClientManagerFactory
.getDefaultSingleton(). 
 205                     saveAllClients(this, MainApp
.getAccountType()); 
 207             // TODO - get rid of these exceptions 
 208         } catch (AccountNotFoundException 
| AuthenticatorException 
| OperationCanceledException 
| IOException e
) { 
 212         mUndispatchedFinishedOperations
.clear(); 
 214         mOperationsBinder 
= null
; 
 216         mOperationsHandler
.getLooper().quit(); 
 217         mOperationsHandler 
= null
; 
 219         mSyncFolderHandler
.getLooper().quit(); 
 220         mSyncFolderHandler 
= null
; 
 226      * Provides a binder object that clients can use to perform actions on the queue of operations, 
 227      * except the addition of new operations. 
 230     public IBinder 
onBind(Intent intent
) { 
 231         //Log_OC.wtf(TAG, "onBind" ); 
 232         return mOperationsBinder
; 
 237      * Called when ALL the bound clients were unbound. 
 240     public boolean onUnbind(Intent intent
) { 
 241         mOperationsBinder
.clearListeners(); 
 242         return false
;   // not accepting rebinding (default behaviour) 
 247      * Binder to let client components to perform actions on the queue of operations. 
 249      * It provides by itself the available operations. 
 251     public class OperationsServiceBinder 
extends Binder 
/* implements OnRemoteOperationListener */ { 
 254          * Map of listeners that will be reported about the end of operations from a 
 255          * {@link OperationsServiceBinder} instance 
 257         private final ConcurrentMap
<OnRemoteOperationListener
, Handler
> mBoundListeners 
= 
 258                 new ConcurrentHashMap
<OnRemoteOperationListener
, Handler
>(); 
 260         private ServiceHandler mServiceHandler 
= null
;    
 262         public OperationsServiceBinder(ServiceHandler serviceHandler
) { 
 263             mServiceHandler 
= serviceHandler
; 
 268          * Cancels a pending or current synchronization. 
 270          * @param account       ownCloud account where the remote folder is stored. 
 271          * @param file          A folder in the queue of pending synchronizations 
 273         public void cancel(Account account
, OCFile file
) { 
 274             mSyncFolderHandler
.cancel(account
, file
); 
 278         public void clearListeners() { 
 280             mBoundListeners
.clear(); 
 285          * Adds a listener interested in being reported about the end of operations. 
 287          * @param listener          Object to notify about the end of operations.     
 288          * @param callbackHandler   {@link Handler} to access the listener without 
 289          *                                         breaking Android threading protection. 
 291         public void addOperationListener (OnRemoteOperationListener listener
, 
 292                                           Handler callbackHandler
) { 
 293             synchronized (mBoundListeners
) { 
 294                 mBoundListeners
.put(listener
, callbackHandler
); 
 300          * Removes a listener from the list of objects interested in the being reported about 
 301          * the end of operations. 
 303          * @param listener      Object to notify about progress of transfer.     
 305         public void removeOperationListener(OnRemoteOperationListener listener
) { 
 306             synchronized (mBoundListeners
) { 
 307                 mBoundListeners
.remove(listener
); 
 313          * TODO - IMPORTANT: update implementation when more operations are moved into the service 
 315          * @return  'True' when an operation that enforces the user to wait for completion is 
 318         public boolean isPerformingBlockingOperation() { 
 319             return (!mServiceHandler
.mPendingOperations
.isEmpty()); 
 324          * Creates and adds to the queue a new operation, as described by operationIntent. 
 326          * Calls startService to make the operation is processed by the ServiceHandler. 
 328          * @param operationIntent       Intent describing a new operation to queue and execute. 
 329          * @return                      Identifier of the operation created, or null if failed. 
 331         public long queueNewOperation(Intent operationIntent
) { 
 332             Pair
<Target
, RemoteOperation
> itemToQueue 
= newOperation(operationIntent
); 
 333             if (itemToQueue 
!= null
) { 
 334                 mServiceHandler
.mPendingOperations
.add(itemToQueue
); 
 335                 startService(new Intent(OperationsService
.this, OperationsService
.class)); 
 336                 return itemToQueue
.second
.hashCode(); 
 339                 return Long
.MAX_VALUE
; 
 344         public boolean dispatchResultIfFinished(int operationId
, 
 345                                                 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                 return (!mServiceHandler
.mPendingOperations
.isEmpty()); 
 359          * Returns True when the file described by 'file' in the ownCloud account 'account' is 
 360          * downloading or waiting to download. 
 362          * If 'file' is a directory, returns 'true' if some of its descendant files is downloading 
 363          * or waiting to download. 
 365          * @param account       ownCloud account where the remote file is stored. 
 366          * @param remotePath    Path of the folder to check if something is synchronizing 
 367          *                      / downloading / uploading inside. 
 369         public boolean isSynchronizing(Account account
, String remotePath
) { 
 370             return mSyncFolderHandler
.isSynchronizing(account
, remotePath
); 
 377      * Operations worker. Performs the pending operations in the order they were requested. 
 379      * Created with the Looper of a new thread, started in {@link OperationsService#onCreate()}.  
 381     private static class ServiceHandler 
extends Handler 
{ 
 382         // don't make it a final class, and don't remove the static ; lint will warn about a p 
 383         // ossible memory leak 
 386         OperationsService mService
; 
 389         private ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>> mPendingOperations 
= 
 390                 new ConcurrentLinkedQueue
<Pair
<Target
, RemoteOperation
>>(); 
 391         private RemoteOperation mCurrentOperation 
= null
; 
 392         private Target mLastTarget 
= null
; 
 393         private OwnCloudClient mOwnCloudClient 
= null
; 
 394         private FileDataStorageManager mStorageManager
; 
 397         public ServiceHandler(Looper looper
, OperationsService service
) { 
 399             if (service 
== null
) { 
 400                 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'"); 
 406         public void handleMessage(Message msg
) { 
 408             Log_OC
.d(TAG
, "Stopping after command with id " + msg
.arg1
); 
 409             mService
.stopSelf(msg
.arg1
); 
 414          * Performs the next operation in the queue 
 416         private void nextOperation() { 
 418             //Log_OC.wtf(TAG, "nextOperation init" ); 
 420             Pair
<Target
, RemoteOperation
> next 
= null
; 
 421             synchronized(mPendingOperations
) { 
 422                 next 
= mPendingOperations
.peek(); 
 427                 mCurrentOperation 
= next
.second
; 
 428                 RemoteOperationResult result 
= null
; 
 430                     /// prepare client object to send the request to the ownCloud server 
 431                     if (mLastTarget 
== null 
|| !mLastTarget
.equals(next
.first
)) { 
 432                         mLastTarget 
= next
.first
; 
 433                         if (mLastTarget
.mAccount 
!= null
) { 
 434                             OwnCloudAccount ocAccount 
= new OwnCloudAccount(mLastTarget
.mAccount
, 
 436                             mOwnCloudClient 
= OwnCloudClientManagerFactory
.getDefaultSingleton(). 
 437                                     getClientFor(ocAccount
, mService
); 
 439                             OwnCloudVersion version 
= com
.owncloud
.android
.authentication
.AccountUtils
.getServerVersion( 
 442                             mOwnCloudClient
.setOwnCloudVersion(version
); 
 444                             mStorageManager 
= new FileDataStorageManager( 
 445                                     mLastTarget
.mAccount
,  
 446                                     mService
.getContentResolver() 
 449                             OwnCloudCredentials credentials 
= null
; 
 450                             if (mLastTarget
.mCookie 
!= null 
&& 
 451                                     mLastTarget
.mCookie
.length() > 0) { 
 452                                 // just used for GetUserName 
 453                                 // TODO refactor to run GetUserName as AsyncTask in the context of 
 454                                 // AuthenticatorActivity 
 455                                 credentials 
= OwnCloudCredentialsFactory
.newSamlSsoCredentials( 
 457                                         mLastTarget
.mCookie
);   // SAML SSO 
 459                             OwnCloudAccount ocAccount 
= new OwnCloudAccount( 
 460                                     mLastTarget
.mServerUrl
, credentials
); 
 461                             mOwnCloudClient 
= OwnCloudClientManagerFactory
.getDefaultSingleton(). 
 462                                     getClientFor(ocAccount
, mService
); 
 463                             mStorageManager 
= null
; 
 467                     /// perform the operation 
 468                     if (mCurrentOperation 
instanceof SyncOperation
) { 
 469                         result 
= ((SyncOperation
)mCurrentOperation
).execute(mOwnCloudClient
, 
 472                         result 
= mCurrentOperation
.execute(mOwnCloudClient
); 
 475                 } catch (AccountsException e
) { 
 476                     if (mLastTarget
.mAccount 
== null
) { 
 477                         Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", 
 480                         Log_OC
.e(TAG
, "Error while trying to get authorization for " + 
 481                                 mLastTarget
.mAccount
.name
, e
); 
 483                     result 
= new RemoteOperationResult(e
); 
 485                 } catch (IOException e
) { 
 486                     if (mLastTarget
.mAccount 
== null
) { 
 487                         Log_OC
.e(TAG
, "Error while trying to get authorization for a NULL account", 
 490                         Log_OC
.e(TAG
, "Error while trying to get authorization for " + 
 491                                 mLastTarget
.mAccount
.name
, e
); 
 493                     result 
= new RemoteOperationResult(e
); 
 494                 } catch (Exception e
) { 
 495                     if (mLastTarget
.mAccount 
== null
) { 
 496                         Log_OC
.e(TAG
, "Unexpected error for a NULL account", e
); 
 498                         Log_OC
.e(TAG
, "Unexpected error for " + mLastTarget
.mAccount
.name
, e
); 
 500                     result 
= new RemoteOperationResult(e
); 
 503                     synchronized(mPendingOperations
) { 
 504                         mPendingOperations
.poll(); 
 508                 //sendBroadcastOperationFinished(mLastTarget, mCurrentOperation, result); 
 509                 mService
.dispatchResultToOperationListeners(mCurrentOperation
, result
); 
 519      * Creates a new operation, as described by operationIntent. 
 521      * TODO - move to ServiceHandler (probably) 
 523      * @param operationIntent       Intent describing a new operation to queue and execute. 
 524      * @return                      Pair with the new operation object and the information about its 
 527     private Pair
<Target 
, RemoteOperation
> newOperation(Intent operationIntent
) { 
 528         RemoteOperation operation 
= null
; 
 529         Target target 
= null
; 
 531             if (!operationIntent
.hasExtra(EXTRA_ACCOUNT
) &&  
 532                     !operationIntent
.hasExtra(EXTRA_SERVER_URL
)) { 
 533                 Log_OC
.e(TAG
, "Not enough information provided in intent"); 
 536                 Account account 
= operationIntent
.getParcelableExtra(EXTRA_ACCOUNT
); 
 537                 String serverUrl 
= operationIntent
.getStringExtra(EXTRA_SERVER_URL
); 
 538                 String cookie 
= operationIntent
.getStringExtra(EXTRA_COOKIE
); 
 541                         (serverUrl 
== null
) ? null 
: Uri
.parse(serverUrl
), 
 545                 String action 
= operationIntent
.getAction(); 
 546                 if (action
.equals(ACTION_CREATE_SHARE
)) {  // Create Share 
 547                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 548                     String password 
= operationIntent
.getStringExtra(EXTRA_PASSWORD_SHARE
); 
 549                     Intent sendIntent 
= operationIntent
.getParcelableExtra(EXTRA_SEND_INTENT
); 
 550                     if (remotePath
.length() > 0) { 
 551                         operation 
= new CreateShareOperation(OperationsService
.this, remotePath
, 
 552                                 ShareType
.PUBLIC_LINK
, 
 553                                 "", false
, password
, 1, sendIntent
); 
 556                 } else if (action
.equals(ACTION_UNSHARE
)) {  // Unshare file 
 557                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 558                     if (remotePath
.length() > 0) { 
 559                         operation 
= new UnshareLinkOperation( 
 561                                 OperationsService
.this); 
 564                 } else if (action
.equals(ACTION_GET_SERVER_INFO
)) {  
 565                     // check OC server and get basic information from it 
 566                     operation 
= new GetServerInfoOperation(serverUrl
, OperationsService
.this); 
 568                 } else if (action
.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN
)) { 
 569                     /// GET ACCESS TOKEN to the OAuth server 
 570                     String oauth2QueryParameters 
= 
 571                             operationIntent
.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS
); 
 572                     operation 
= new OAuth2GetAccessToken( 
 573                             getString(R
.string
.oauth2_client_id
),  
 574                             getString(R
.string
.oauth2_redirect_uri
),        
 575                             getString(R
.string
.oauth2_grant_type
), 
 576                             oauth2QueryParameters
); 
 578                 } else if (action
.equals(ACTION_GET_USER_NAME
)) { 
 580                     operation 
= new GetRemoteUserNameOperation(); 
 582                 } else if (action
.equals(ACTION_RENAME
)) { 
 583                     // Rename file or folder 
 584                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 585                     String newName 
= operationIntent
.getStringExtra(EXTRA_NEWNAME
); 
 586                     operation 
= new RenameFileOperation(remotePath
, newName
); 
 588                 } else if (action
.equals(ACTION_REMOVE
)) { 
 589                     // Remove file or folder 
 590                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 591                     boolean onlyLocalCopy 
= operationIntent
.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL
, 
 593                     operation 
= new RemoveFileOperation(remotePath
, onlyLocalCopy
); 
 595                 } else if (action
.equals(ACTION_CREATE_FOLDER
)) { 
 597                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 598                     boolean createFullPath 
= operationIntent
.getBooleanExtra(EXTRA_CREATE_FULL_PATH
, 
 600                     operation 
= new CreateFolderOperation(remotePath
, createFullPath
); 
 602                 } else if (action
.equals(ACTION_SYNC_FILE
)) { 
 604                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 605                     boolean syncFileContents 
= 
 606                             operationIntent
.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS
, true
); 
 607                     operation 
= new SynchronizeFileOperation( 
 608                             remotePath
, account
, syncFileContents
, getApplicationContext() 
 611                 } else if (action
.equals(ACTION_SYNC_FOLDER
)) { 
 613                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 614                     operation 
= new SynchronizeFolderOperation( 
 615                             this,                       // TODO remove this dependency from construction time 
 618                             System
.currentTimeMillis()  // TODO remove this dependency from construction time 
 621                 } else if (action
.equals(ACTION_MOVE_FILE
)) { 
 623                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 624                     String newParentPath 
= operationIntent
.getStringExtra(EXTRA_NEW_PARENT_PATH
); 
 625                     operation 
= new MoveFileOperation(remotePath
, newParentPath
, account
); 
 627                 } else if (action
.equals(ACTION_COPY_FILE
)) { 
 629                     String remotePath 
= operationIntent
.getStringExtra(EXTRA_REMOTE_PATH
); 
 630                     String newParentPath 
= operationIntent
.getStringExtra(EXTRA_NEW_PARENT_PATH
); 
 631                     operation 
= new CopyFileOperation(remotePath
, newParentPath
, account
); 
 636         } catch (IllegalArgumentException e
) { 
 637             Log_OC
.e(TAG
, "Bad information provided in intent: " + e
.getMessage()); 
 641         if (operation 
!= null
) { 
 642             return new Pair
<Target 
, RemoteOperation
>(target
, operation
);   
 650      * Sends a broadcast when a new operation is added to the queue. 
 652      * Local broadcasts are only delivered to activities in the same process, but can't be 
 655      * @param target            Account or URL pointing to an OC server. 
 656      * @param operation         Added operation. 
 658     private void sendBroadcastNewOperation(Target target
, RemoteOperation operation
) { 
 659         Intent intent 
= new Intent(ACTION_OPERATION_ADDED
); 
 660         if (target
.mAccount 
!= null
) { 
 661             intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
); 
 663             intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
); 
 665         //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); 
 666         //lbm.sendBroadcast(intent); 
 667         sendStickyBroadcast(intent
); 
 671     // TODO - maybe add a notification for real start of operations 
 674      * Sends a LOCAL broadcast when an operations finishes in order to the interested activities c 
 675      * an update their view 
 677      * Local broadcasts are only delivered to activities in the same process. 
 679      * @param target    Account or URL pointing to an OC server. 
 680      * @param operation Finished operation. 
 681      * @param result    Result of the operation. 
 683     private void sendBroadcastOperationFinished(Target target
, RemoteOperation operation
, 
 684                                                 RemoteOperationResult result
) { 
 685         Intent intent 
= new Intent(ACTION_OPERATION_FINISHED
); 
 686         intent
.putExtra(EXTRA_RESULT
, result
); 
 687         if (target
.mAccount 
!= null
) { 
 688             intent
.putExtra(EXTRA_ACCOUNT
, target
.mAccount
); 
 690             intent
.putExtra(EXTRA_SERVER_URL
, target
.mServerUrl
); 
 692         //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this); 
 693         //lbm.sendBroadcast(intent); 
 694         sendStickyBroadcast(intent
); 
 699      * Notifies the currently subscribed listeners about the end of an operation. 
 701      * @param operation         Finished operation. 
 702      * @param result            Result of the operation. 
 704     protected void dispatchResultToOperationListeners( 
 705             final RemoteOperation operation
, final RemoteOperationResult result
 
 708         Iterator
<OnRemoteOperationListener
> listeners 
= 
 709                 mOperationsBinder
.mBoundListeners
.keySet().iterator(); 
 710         while (listeners
.hasNext()) { 
 711             final OnRemoteOperationListener listener 
= listeners
.next(); 
 712             final Handler handler 
= mOperationsBinder
.mBoundListeners
.get(listener
); 
 713             if (handler 
!= null
) {  
 714                 handler
.post(new Runnable() { 
 717                         listener
.onRemoteOperationFinish(operation
, result
); 
 724             //mOperationResults.put(operation.hashCode(), result); 
 725             Pair
<RemoteOperation
, RemoteOperationResult
> undispatched 
= 
 726                     new Pair
<>(operation
, result
); 
 727             mUndispatchedFinishedOperations
.put(((Runnable
) operation
).hashCode(), undispatched
); 
 729         Log_OC
.d(TAG
, "Called " + count 
+ " listeners");