1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
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
.files
.services
;
23 import java
.util
.HashMap
;
26 import com
.owncloud
.android
.Log_OC
;
27 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
28 import com
.owncloud
.android
.datamodel
.OCFile
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.files
.OwnCloudFileObserver
;
31 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
32 import com
.owncloud
.android
.utils
.FileStorageUtils
;
34 import android
.accounts
.Account
;
35 import android
.accounts
.AccountManager
;
36 import android
.app
.Service
;
37 import android
.content
.BroadcastReceiver
;
38 import android
.content
.Context
;
39 import android
.content
.Intent
;
40 import android
.content
.IntentFilter
;
41 import android
.database
.Cursor
;
42 import android
.os
.Binder
;
43 import android
.os
.IBinder
;
44 import android
.util
.Log
;
46 public class FileObserverService
extends Service
{
48 public final static int CMD_INIT_OBSERVED_LIST
= 1;
49 public final static int CMD_ADD_OBSERVED_FILE
= 2;
50 public final static int CMD_DEL_OBSERVED_FILE
= 3;
52 public final static String KEY_FILE_CMD
= "KEY_FILE_CMD";
53 public final static String KEY_CMD_ARG_FILE
= "KEY_CMD_ARG_FILE";
54 public final static String KEY_CMD_ARG_ACCOUNT
= "KEY_CMD_ARG_ACCOUNT";
56 private static String TAG
= FileObserverService
.class.getSimpleName();
58 private static Map
<String
, OwnCloudFileObserver
> mObserversMap
;
59 private static DownloadCompletedReceiverBis mDownloadReceiver
;
60 private IBinder mBinder
= new LocalBinder();
62 public class LocalBinder
extends Binder
{
63 FileObserverService
getService() {
64 return FileObserverService
.this;
69 public void onCreate() {
71 mDownloadReceiver
= new DownloadCompletedReceiverBis();
72 IntentFilter filter
= new IntentFilter();
73 filter
.addAction(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
);
74 filter
.addAction(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
);
75 registerReceiver(mDownloadReceiver
, filter
);
77 mObserversMap
= new HashMap
<String
, OwnCloudFileObserver
>();
78 //initializeObservedList();
83 public void onDestroy() {
85 unregisterReceiver(mDownloadReceiver
);
86 mObserversMap
= null
; // TODO study carefully the life cycle of Services to grant the best possible observance
87 Log_OC
.d(TAG
, "Bye, bye");
92 public IBinder
onBind(Intent intent
) {
97 public int onStartCommand(Intent intent
, int flags
, int startId
) {
98 // this occurs when system tries to restart
99 // service, so we need to reinitialize observers
100 if (intent
== null
) {
101 initializeObservedList();
102 return Service
.START_STICKY
;
105 if (!intent
.hasExtra(KEY_FILE_CMD
)) {
106 Log_OC
.e(TAG
, "No KEY_FILE_CMD argument given");
107 return Service
.START_STICKY
;
110 switch (intent
.getIntExtra(KEY_FILE_CMD
, -1)) {
111 case CMD_INIT_OBSERVED_LIST
:
112 initializeObservedList();
114 case CMD_ADD_OBSERVED_FILE
:
115 addObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
116 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
118 case CMD_DEL_OBSERVED_FILE
:
119 removeObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
120 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
123 Log_OC
.wtf(TAG
, "Incorrect key given");
126 return Service
.START_STICKY
;
131 * Read from the local database the list of files that must to be kept synchronized and
132 * starts file observers to monitor local changes on them
134 private void initializeObservedList() {
135 mObserversMap
.clear();
136 Cursor c
= getContentResolver().query(
137 ProviderTableMeta
.CONTENT_URI
,
139 ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " = ?",
140 new String
[] {String
.valueOf(1)},
142 if (c
== null
|| !c
.moveToFirst()) return;
143 AccountManager acm
= AccountManager
.get(this);
144 Account
[] accounts
= acm
.getAccounts();
146 Account account
= null
;
147 for (Account a
: accounts
)
148 if (a
.name
.equals(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ACCOUNT_OWNER
)))) {
153 if (account
== null
) continue;
154 FileDataStorageManager storage
=
155 new FileDataStorageManager(account
, getContentResolver());
156 if (!storage
.fileExists(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))))
159 String path
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
160 if (path
== null
|| path
.length() <= 0)
162 OwnCloudFileObserver observer
=
163 new OwnCloudFileObserver( path
,
165 getApplicationContext(),
166 OwnCloudFileObserver
.CHANGES_ONLY
);
167 mObserversMap
.put(path
, observer
);
168 if (new File(path
).exists()) {
169 observer
.startWatching();
170 Log_OC
.d(TAG
, "Started watching file " + path
);
173 } while (c
.moveToNext());
179 * Registers the local copy of a remote file to be observed for local changes,
180 * an automatically updated in the ownCloud server.
182 * This method does NOT perform a {@link SynchronizeFileOperation} over the file.
184 * TODO We are ignoring that, currently, a local file can be linked to different files
185 * in ownCloud if it's uploaded several times. That's something pending to update: we
186 * will avoid that the same local file is linked to different remote files.
188 * @param file Object representing a remote file which local copy must be observed.
189 * @param account OwnCloud account containing file.
191 private void addObservedFile(OCFile file
, Account account
) {
193 Log_OC
.e(TAG
, "Trying to add a NULL file to observer");
196 String localPath
= file
.getStoragePath();
197 if (localPath
== null
|| localPath
.length() <= 0) { // file downloading / to be download for the first time
198 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
200 OwnCloudFileObserver observer
= mObserversMap
.get(localPath
);
201 if (observer
== null
) {
202 /// the local file was never registered to observe before
203 observer
= new OwnCloudFileObserver( localPath
,
205 getApplicationContext(),
206 OwnCloudFileObserver
.CHANGES_ONLY
);
207 mObserversMap
.put(localPath
, observer
);
208 Log_OC
.d(TAG
, "Observer added for path " + localPath
);
211 observer
.startWatching();
212 Log_OC
.d(TAG
, "Started watching " + localPath
);
213 } // else - the observance can't be started on a file not already down; mDownloadReceiver will get noticed when the download of the file finishes
220 * Unregisters the local copy of a remote file to be observed for local changes.
222 * Starts to watch it, if the file has a local copy to watch.
224 * TODO We are ignoring that, currently, a local file can be linked to different files
225 * in ownCloud if it's uploaded several times. That's something pending to update: we
226 * will avoid that the same local file is linked to different remote files.
228 * @param file Object representing a remote file which local copy must be not observed longer.
229 * @param account OwnCloud account containing file.
231 private void removeObservedFile(OCFile file
, Account account
) {
233 Log_OC
.e(TAG
, "Trying to remove a NULL file");
236 String localPath
= file
.getStoragePath();
237 if (localPath
== null
|| localPath
.length() <= 0) {
238 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
241 OwnCloudFileObserver observer
= mObserversMap
.get(localPath
);
242 if (observer
!= null
) {
243 observer
.stopWatching();
244 mObserversMap
.remove(observer
);
245 Log_OC
.d(TAG
, "Stopped watching " + localPath
);
252 * Private receiver listening to events broadcast by the FileDownloader service.
254 * Starts and stops the observance on registered files when they are being download,
255 * in order to avoid to start unnecessary synchronizations.
257 private class DownloadCompletedReceiverBis
extends BroadcastReceiver
{
260 public void onReceive(Context context
, Intent intent
) {
261 String downloadPath
= intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
);
262 OwnCloudFileObserver observer
= mObserversMap
.get(downloadPath
);
263 if (observer
!= null
) {
264 if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
) &&
265 new File(downloadPath
).exists()) { // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload
266 observer
.startWatching();
267 Log_OC
.d(TAG
, "Watching again " + downloadPath
);
269 } else if (intent
.getAction().equals(FileDownloader
.DOWNLOAD_ADDED_MESSAGE
)) {
270 observer
.stopWatching();
271 Log_OC
.d(TAG
, "Disabling observance of " + downloadPath
);