1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.files
.services
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
28 import com
.owncloud
.android
.files
.OwnCloudFileObserver
;
29 import com
.owncloud
.android
.files
.OwnCloudFileObserver
.FileObserverStatusListener
;
30 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
32 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
33 import com
.owncloud
.android
.utils
.FileStorageUtils
;
35 import android
.accounts
.Account
;
36 import android
.accounts
.AccountManager
;
37 import android
.app
.Service
;
38 import android
.content
.BroadcastReceiver
;
39 import android
.content
.Context
;
40 import android
.content
.Intent
;
41 import android
.content
.IntentFilter
;
42 import android
.database
.Cursor
;
43 import android
.os
.Binder
;
44 import android
.os
.IBinder
;
45 import android
.util
.Log
;
47 public class FileObserverService
extends Service
implements FileObserverStatusListener
{
49 public final static String KEY_FILE_CMD
= "KEY_FILE_CMD";
50 public final static String KEY_CMD_ARG_FILE
= "KEY_CMD_ARG_FILE";
51 public final static String KEY_CMD_ARG_ACCOUNT
= "KEY_CMD_ARG_ACCOUNT";
53 public final static int CMD_INIT_OBSERVED_LIST
= 1;
54 public final static int CMD_ADD_OBSERVED_FILE
= 2;
55 public final static int CMD_DEL_OBSERVED_FILE
= 3;
56 public final static int CMD_ADD_DOWNLOADING_FILE
= 4;
58 private static String TAG
= FileObserverService
.class.getSimpleName();
59 private static List
<OwnCloudFileObserver
> mObservers
;
60 private static List
<DownloadCompletedReceiver
> mDownloadReceivers
;
61 private static Object mReceiverListLock
= new Object();
62 private IBinder mBinder
= new LocalBinder();
64 public class LocalBinder
extends Binder
{
65 FileObserverService
getService() {
66 return FileObserverService
.this;
71 public IBinder
onBind(Intent intent
) {
76 public int onStartCommand(Intent intent
, int flags
, int startId
) {
77 // this occurs when system tries to restart
78 // service, so we need to reinitialize observers
80 initializeObservedList();
81 return Service
.START_STICKY
;
84 if (!intent
.hasExtra(KEY_FILE_CMD
)) {
85 Log
.e(TAG
, "No KEY_FILE_CMD argument given");
86 return Service
.START_STICKY
;
89 switch (intent
.getIntExtra(KEY_FILE_CMD
, -1)) {
90 case CMD_INIT_OBSERVED_LIST
:
91 initializeObservedList();
93 case CMD_ADD_OBSERVED_FILE
:
94 addObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
95 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
97 case CMD_DEL_OBSERVED_FILE
:
98 removeObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
99 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
101 case CMD_ADD_DOWNLOADING_FILE
:
102 addDownloadingFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
103 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
106 Log
.wtf(TAG
, "Incorrect key given");
109 return Service
.START_STICKY
;
112 private void initializeObservedList() {
113 if (mObservers
!= null
) return; // nothing to do here
114 mObservers
= new ArrayList
<OwnCloudFileObserver
>();
115 mDownloadReceivers
= new ArrayList
<DownloadCompletedReceiver
>();
116 Cursor c
= getContentResolver().query(
117 ProviderTableMeta
.CONTENT_URI
,
119 ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " = ?",
120 new String
[] {String
.valueOf(1)},
122 if (!c
.moveToFirst()) return;
123 AccountManager acm
= AccountManager
.get(this);
124 Account
[] accounts
= acm
.getAccounts();
126 Account account
= null
;
127 for (Account a
: accounts
)
128 if (a
.name
.equals(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ACCOUNT_OWNER
)))) {
133 if (account
== null
) continue;
134 FileDataStorageManager storage
=
135 new FileDataStorageManager(account
, getContentResolver());
136 if (!storage
.fileExists(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))))
139 String path
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
140 OwnCloudFileObserver observer
=
141 new OwnCloudFileObserver(path
, OwnCloudFileObserver
.CHANGES_ONLY
);
142 observer
.setContext(getApplicationContext());
143 observer
.setAccount(account
);
144 observer
.setStorageManager(storage
);
145 observer
.setOCFile(storage
.getFileByPath(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))));
146 observer
.addObserverStatusListener(this);
147 observer
.startWatching();
148 mObservers
.add(observer
);
149 Log
.d(TAG
, "Started watching file " + path
);
151 } while (c
.moveToNext());
156 * Registers the local copy of a remote file to be observed for local changes,
157 * an automatically updated in the ownCloud server.
159 * If there is no local copy of the remote file, a request to download it is send
160 * to the FileDownloader service. The observation is delayed until the download
163 * @param file Object representing a remote file which local copy must be observed.
164 * @param account OwnCloud account containing file.
166 private void addObservedFile(OCFile file
, Account account
) {
168 Log
.e(TAG
, "Trying to observe a NULL file");
171 if (mObservers
== null
) {
172 // this is very rare case when service was killed by system
173 // and observers list was deleted in that procedure
174 initializeObservedList();
176 String localPath
= file
.getStoragePath();
177 if (!file
.isDown()) {
178 // this is a file downloading / to be download for the first time
179 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
181 OwnCloudFileObserver tmpObserver
= null
, observer
= null
;
182 for (int i
= 0; i
< mObservers
.size(); ++i
) {
183 tmpObserver
= mObservers
.get(i
);
184 if (tmpObserver
.getPath().equals(localPath
)) {
185 observer
= tmpObserver
;
187 tmpObserver
.setContext(getApplicationContext()); // 'refreshing' context to all the observers? why?
189 if (observer
== null
) {
190 /// the local file was never registered to observe before
191 observer
= new OwnCloudFileObserver(localPath
, OwnCloudFileObserver
.CHANGES_ONLY
);
192 //Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
193 observer
.setAccount(account
);
194 FileDataStorageManager storage
=
195 new FileDataStorageManager(account
, getContentResolver()); // I don't trust in this resolver's life span...
196 observer
.setStorageManager(storage
);
197 //observer.setOCFile(storage.getFileByLocalPath(path)); // ISSUE 10 - the fix in FileDetailsFragment to avoid path == null was not enough; it the file was never down before, this sets a NULL OCFile in the observer
198 observer
.setOCFile(file
);
199 observer
.addObserverStatusListener(this);
200 observer
.setContext(getApplicationContext());
203 /* LET'S IGNORE THAT, CURRENTLY, A LOCAL FILE CAN BE LINKED TO DIFFERENT FILES IN OWNCLOUD;
204 * we should change that
206 /// the local file is already observed for some other OCFile(s)
207 observer.addOCFile(account, file); // OCFiles should have a reference to the account containing them to not be confused
211 mObservers
.add(observer
);
212 Log
.d(TAG
, "Observer added for path " + localPath
);
214 if (!file
.isDown()) {
215 // if the file is not down, it can't be observed for changes
216 DownloadCompletedReceiver receiver
= new DownloadCompletedReceiver(localPath
, observer
);
217 registerReceiver(receiver
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
219 Intent i
= new Intent(this, FileDownloader
.class);
220 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
221 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
225 observer
.startWatching();
226 Log
.d(TAG
, "Started watching " + localPath
);
234 * Unregisters the local copy of a remote file to be observed for local changes.
236 * @param file Object representing a remote file which local copy must be not observed longer.
237 * @param account OwnCloud account containing file.
239 private void removeObservedFile(OCFile file
, Account account
) {
241 Log
.e(TAG
, "Trying to unobserve a NULL file");
244 if (mObservers
== null
) {
245 initializeObservedList();
247 String localPath
= file
.getStoragePath();
248 if (!file
.isDown()) {
249 // this happens when a file not in the device is set to be kept synchronized, and quickly unset again,
250 // while the download is not finished
251 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
254 for (int i
= 0; i
< mObservers
.size(); ++i
) {
255 OwnCloudFileObserver observer
= mObservers
.get(i
);
256 if (observer
.getPath().equals(localPath
)) {
257 observer
.stopWatching();
258 mObservers
.remove(i
); // assuming, again, that a local file can be only linked to only ONE remote file; currently false
259 if (!file
.isDown()) {
260 // TODO unregister download receiver ;forget this until list of receivers is replaced for a single receiver
262 Log
.d(TAG
, "Stopped watching " + localPath
);
271 * Temporarily disables the observance of a file that is going to be download.
273 * @param file Object representing the remote file which local copy must not be observed temporarily.
274 * @param account OwnCloud account containing file.
276 private void addDownloadingFile(OCFile file
, Account account
) {
277 OwnCloudFileObserver observer
= null
;
278 for (OwnCloudFileObserver o
: mObservers
) {
279 if (o
.getRemotePath().equals(file
.getRemotePath()) && o
.getAccount().equals(account
)) {
284 if (observer
== null
) {
285 Log
.e(TAG
, "Couldn't find observer for remote file " + file
.getRemotePath());
288 observer
.stopWatching();
289 DownloadCompletedReceiver dcr
= new DownloadCompletedReceiver(observer
.getPath(), observer
);
290 registerReceiver(dcr
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
294 private static void addReceiverToList(DownloadCompletedReceiver r
) {
295 synchronized(mReceiverListLock
) {
296 mDownloadReceivers
.add(r
);
300 private static void removeReceiverFromList(DownloadCompletedReceiver r
) {
301 synchronized(mReceiverListLock
) {
302 mDownloadReceivers
.remove(r
);
307 public void onObservedFileStatusUpdate(String localPath
, String remotePath
, Account account
, RemoteOperationResult result
) {
308 if (!result
.isSuccess()) {
309 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
310 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
311 Intent i
= new Intent(getApplicationContext(), ConflictsResolveActivity
.class);
312 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
313 i
.putExtra("remotepath", remotePath
);
314 i
.putExtra("localpath", localPath
);
315 i
.putExtra("account", account
);
319 // TODO send notification to the notification bar?
321 } // else, nothing else to do; now it's duty of FileUploader service
324 private class DownloadCompletedReceiver
extends BroadcastReceiver
{
326 OwnCloudFileObserver mObserver
;
328 public DownloadCompletedReceiver(String path
, OwnCloudFileObserver observer
) {
330 mObserver
= observer
;
331 addReceiverToList(this);
335 public void onReceive(Context context
, Intent intent
) {
336 if (mPath
.equals(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
))) {
337 if ((new File(mPath
)).exists()) {
338 // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload
339 context
.unregisterReceiver(this);
340 removeReceiverFromList(this);
341 mObserver
.startWatching();
342 Log
.d(TAG
, "Started watching " + mPath
);
344 } // else - keep waiting for a future retry of the download ;
345 // mObserver.startWatching() won't ever work if the file is not in the device when it's called
350 public boolean equals(Object o
) {
351 if (o
instanceof DownloadCompletedReceiver
)
352 return mPath
.equals(((DownloadCompletedReceiver
)o
).mPath
);
353 return super.equals(o
);