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
.content
.Intent
;
25 import android
.os
.Handler
;
26 import android
.os
.Looper
;
27 import android
.os
.Message
;
28 import android
.util
.Pair
;
30 import com
.owncloud
.android
.MainApp
;
31 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
32 import com
.owncloud
.android
.datamodel
.OCFile
;
33 import com
.owncloud
.android
.files
.services
.FileDownloader
;
34 import com
.owncloud
.android
.files
.services
.IndexedForest
;
35 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
36 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
37 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
38 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
39 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
40 import com
.owncloud
.android
.operations
.SynchronizeFolderOperation
;
41 import com
.owncloud
.android
.utils
.FileStorageUtils
;
43 import java
.io
.IOException
;
46 * SyncFolder worker. Performs the pending operations in the order they were requested.
48 * Created with the Looper of a new thread, started in
49 * {@link com.owncloud.android.services.OperationsService#onCreate()}.
51 class SyncFolderHandler
extends Handler
{
53 private static final String TAG
= SyncFolderHandler
.class.getSimpleName();
56 OperationsService mService
;
58 private IndexedForest
<SynchronizeFolderOperation
> mPendingOperations
=
59 new IndexedForest
<SynchronizeFolderOperation
>();
61 private OwnCloudClient mOwnCloudClient
= null
;
62 private Account mCurrentAccount
= null
;
63 private FileDataStorageManager mStorageManager
;
64 private SynchronizeFolderOperation mCurrentSyncOperation
;
67 public SyncFolderHandler(Looper looper
, OperationsService service
) {
69 if (service
== null
) {
70 throw new IllegalArgumentException("Received invalid NULL in parameter 'service'");
77 * Returns True when the folder located in 'remotePath' in the ownCloud account 'account', or any of its
78 * descendants, is being synchronized (or waiting for it).
80 * @param account ownCloud account where the remote folder is stored.
81 * @param remotePath The path to a folder that could be in the queue of synchronizations.
83 public boolean isSynchronizing(Account account
, String remotePath
) {
84 if (account
== null
|| remotePath
== null
) return false
;
85 return (mPendingOperations
.contains(account
, remotePath
));
90 public void handleMessage(Message msg
) {
91 Pair
<Account
, String
> itemSyncKey
= (Pair
<Account
, String
>) msg
.obj
;
92 doOperation(itemSyncKey
.first
, itemSyncKey
.second
);
93 Log_OC
.d(TAG
, "Stopping after command with id " + msg
.arg1
);
94 mService
.stopSelf(msg
.arg1
);
99 * Performs the next operation in the queue
101 private void doOperation(Account account
, String remotePath
) {
103 mCurrentSyncOperation
= mPendingOperations
.get(account
, remotePath
);
105 if (mCurrentSyncOperation
!= null
) {
106 RemoteOperationResult result
= null
;
110 if (mCurrentAccount
== null
|| !mCurrentAccount
.equals(account
)) {
111 mCurrentAccount
= account
;
112 mStorageManager
= new FileDataStorageManager(
114 mService
.getContentResolver()
116 } // else, reuse storage manager from previous operation
118 // always get client from client manager, to get fresh credentials in case of update
119 OwnCloudAccount ocAccount
= new OwnCloudAccount(account
, mService
);
120 mOwnCloudClient
= OwnCloudClientManagerFactory
.getDefaultSingleton().
121 getClientFor(ocAccount
, mService
);
123 result
= mCurrentSyncOperation
.execute(mOwnCloudClient
, mStorageManager
);
125 } catch (AccountsException e
) {
126 Log_OC
.e(TAG
, "Error while trying to get authorization", e
);
127 } catch (IOException e
) {
128 Log_OC
.e(TAG
, "Error while trying to get authorization", e
);
130 mPendingOperations
.removePayload(account
, remotePath
);
132 mService
.dispatchResultToOperationListeners(mCurrentSyncOperation
, result
);
134 sendBroadcastFinishedSyncFolder(account
, remotePath
, result
.isSuccess());
139 public void add(Account account
, String remotePath
,
140 SynchronizeFolderOperation syncFolderOperation
){
141 Pair
<String
, String
> putResult
=
142 mPendingOperations
.putIfAbsent(account
, remotePath
, syncFolderOperation
);
143 if (putResult
!= null
) {
144 sendBroadcastNewSyncFolder(account
, remotePath
); // TODO upgrade!
150 * Cancels a pending or current sync' operation.
152 * @param account ownCloud account where the remote file is stored.
153 * @param file A file in the queue of pending synchronizations
155 public void cancel(Account account
, OCFile file
){
156 if (account
== null
|| file
== null
) {
157 Log_OC
.e(TAG
, "Cannot cancel with NULL parameters");
160 Pair
<SynchronizeFolderOperation
, String
> removeResult
=
161 mPendingOperations
.remove(account
, file
.getRemotePath());
162 SynchronizeFolderOperation synchronization
= removeResult
.first
;
163 if (synchronization
!= null
) {
164 synchronization
.cancel();
167 if (mCurrentSyncOperation
!= null
&& mCurrentAccount
!= null
&&
168 mCurrentSyncOperation
.getRemotePath().startsWith(file
.getRemotePath()) &&
169 account
.name
.equals(mCurrentAccount
.name
)) {
170 mCurrentSyncOperation
.cancel();
174 //sendBroadcastFinishedSyncFolder(account, file.getRemotePath());
178 * TODO review this method when "folder synchronization" replaces "folder download";
179 * this is a fast and ugly patch.
181 private void sendBroadcastNewSyncFolder(Account account
, String remotePath
) {
182 Intent added
= new Intent(FileDownloader
.getDownloadAddedMessage());
183 added
.putExtra(FileDownloader
.ACCOUNT_NAME
, account
.name
);
184 added
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, remotePath
);
185 added
.putExtra(FileDownloader
.EXTRA_FILE_PATH
, FileStorageUtils
.getSavePath(account
.name
)
187 mService
.sendStickyBroadcast(added
);
191 * TODO review this method when "folder synchronization" replaces "folder download";
192 * this is a fast and ugly patch.
194 private void sendBroadcastFinishedSyncFolder(Account account
, String remotePath
,
196 Intent finished
= new Intent(FileDownloader
.getDownloadFinishMessage());
197 finished
.putExtra(FileDownloader
.ACCOUNT_NAME
, account
.name
);
198 finished
.putExtra(FileDownloader
.EXTRA_REMOTE_PATH
, remotePath
);
199 finished
.putExtra(FileDownloader
.EXTRA_FILE_PATH
,
200 FileStorageUtils
.getSavePath(account
.name
) + remotePath
);
201 finished
.putExtra(FileDownloader
.EXTRA_DOWNLOAD_RESULT
, success
);
202 mService
.sendStickyBroadcast(finished
);