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
.ui
.activity
.ConflictsResolveActivity
;
31 import com
.owncloud
.android
.utils
.FileStorageUtils
;
33 import android
.accounts
.Account
;
34 import android
.accounts
.AccountManager
;
35 import android
.app
.Service
;
36 import android
.content
.BroadcastReceiver
;
37 import android
.content
.Context
;
38 import android
.content
.Intent
;
39 import android
.content
.IntentFilter
;
40 import android
.database
.Cursor
;
41 import android
.os
.Binder
;
42 import android
.os
.IBinder
;
43 import android
.util
.Log
;
45 public class FileObserverService
extends Service
implements FileObserverStatusListener
{
47 public final static String KEY_FILE_CMD
= "KEY_FILE_CMD";
48 public final static String KEY_CMD_ARG_FILE
= "KEY_CMD_ARG_FILE";
49 public final static String KEY_CMD_ARG_ACCOUNT
= "KEY_CMD_ARG_ACCOUNT";
51 public final static int CMD_INIT_OBSERVED_LIST
= 1;
52 public final static int CMD_ADD_OBSERVED_FILE
= 2;
53 public final static int CMD_DEL_OBSERVED_FILE
= 3;
54 public final static int CMD_ADD_DOWNLOADING_FILE
= 4;
56 private static String TAG
= FileObserverService
.class.getSimpleName();
57 private static List
<OwnCloudFileObserver
> mObservers
;
58 private static List
<DownloadCompletedReceiver
> mDownloadReceivers
;
59 private static Object mReceiverListLock
= new Object();
60 private IBinder mBinder
= new LocalBinder();
62 public class LocalBinder
extends Binder
{
63 FileObserverService
getService() {
64 return FileObserverService
.this;
69 public IBinder
onBind(Intent intent
) {
74 public int onStartCommand(Intent intent
, int flags
, int startId
) {
75 // this occurs when system tries to restart
76 // service, so we need to reinitialize observers
78 initializeObservedList();
79 return Service
.START_STICKY
;
82 if (!intent
.hasExtra(KEY_FILE_CMD
)) {
83 Log
.e(TAG
, "No KEY_FILE_CMD argument given");
84 return Service
.START_STICKY
;
87 switch (intent
.getIntExtra(KEY_FILE_CMD
, -1)) {
88 case CMD_INIT_OBSERVED_LIST
:
89 initializeObservedList();
91 case CMD_ADD_OBSERVED_FILE
:
92 addObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
93 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
95 case CMD_DEL_OBSERVED_FILE
:
96 removeObservedFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
97 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
99 case CMD_ADD_DOWNLOADING_FILE
:
100 addDownloadingFile( (OCFile
)intent
.getParcelableExtra(KEY_CMD_ARG_FILE
),
101 (Account
)intent
.getParcelableExtra(KEY_CMD_ARG_ACCOUNT
));
104 Log
.wtf(TAG
, "Incorrect key given");
107 return Service
.START_STICKY
;
110 private void initializeObservedList() {
111 if (mObservers
!= null
) return; // nothing to do here
112 mObservers
= new ArrayList
<OwnCloudFileObserver
>();
113 mDownloadReceivers
= new ArrayList
<DownloadCompletedReceiver
>();
114 Cursor c
= getContentResolver().query(
115 ProviderTableMeta
.CONTENT_URI
,
117 ProviderTableMeta
.FILE_KEEP_IN_SYNC
+ " = ?",
118 new String
[] {String
.valueOf(1)},
120 if (!c
.moveToFirst()) return;
121 AccountManager acm
= AccountManager
.get(this);
122 Account
[] accounts
= acm
.getAccounts();
124 Account account
= null
;
125 for (Account a
: accounts
)
126 if (a
.name
.equals(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ACCOUNT_OWNER
)))) {
131 if (account
== null
) continue;
132 FileDataStorageManager storage
=
133 new FileDataStorageManager(account
, getContentResolver());
134 if (!storage
.fileExists(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))))
137 String path
= c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
));
138 OwnCloudFileObserver observer
=
139 new OwnCloudFileObserver(path
, OwnCloudFileObserver
.CHANGES_ONLY
);
140 observer
.setContext(getApplicationContext());
141 observer
.setAccount(account
);
142 observer
.setStorageManager(storage
);
143 observer
.setOCFile(storage
.getFileByPath(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PATH
))));
144 observer
.addObserverStatusListener(this);
145 observer
.startWatching();
146 mObservers
.add(observer
);
147 Log
.d(TAG
, "Started watching file " + path
);
149 } while (c
.moveToNext());
154 * Registers the local copy of a remote file to be observed for local changes,
155 * an automatically updated in the ownCloud server.
157 * If there is no local copy of the remote file, a request to download it is send
158 * to the FileDownloader service. The observation is delayed until the download
161 * @param file Object representing a remote file which local copy must be observed.
162 * @param account OwnCloud account containing file.
164 private void addObservedFile(OCFile file
, Account account
) {
166 Log
.e(TAG
, "Trying to observe a NULL file");
169 if (mObservers
== null
) {
170 // this is very rare case when service was killed by system
171 // and observers list was deleted in that procedure
172 initializeObservedList();
174 String localPath
= file
.getStoragePath();
175 if (!file
.isDown()) {
176 // this is a file downloading / to be download for the first time
177 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
179 OwnCloudFileObserver tmpObserver
= null
, observer
= null
;
180 for (int i
= 0; i
< mObservers
.size(); ++i
) {
181 tmpObserver
= mObservers
.get(i
);
182 if (tmpObserver
.getPath().equals(localPath
)) {
183 observer
= tmpObserver
;
185 tmpObserver
.setContext(getApplicationContext()); // 'refreshing' context to all the observers? why?
187 if (observer
== null
) {
188 /// the local file was never registered to observe before
189 observer
= new OwnCloudFileObserver(localPath
, OwnCloudFileObserver
.CHANGES_ONLY
);
190 //Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
191 observer
.setAccount(account
);
192 FileDataStorageManager storage
=
193 new FileDataStorageManager(account
, getContentResolver()); // I don't trust in this resolver's life span...
194 observer
.setStorageManager(storage
);
195 //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
196 observer
.setOCFile(file
);
197 observer
.addObserverStatusListener(this);
198 observer
.setContext(getApplicationContext());
201 /* LET'S IGNORE THAT, CURRENTLY, A LOCAL FILE CAN BE LINKED TO DIFFERENT FILES IN OWNCLOUD;
202 * we should change that
204 /// the local file is already observed for some other OCFile(s)
205 observer.addOCFile(account, file); // OCFiles should have a reference to the account containing them to not be confused
209 mObservers
.add(observer
);
210 Log
.d(TAG
, "Observer added for path " + localPath
);
212 if (!file
.isDown()) {
213 // if the file is not down, it can't be observed for changes
214 DownloadCompletedReceiver receiver
= new DownloadCompletedReceiver(localPath
, observer
);
215 registerReceiver(receiver
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
217 Intent i
= new Intent(this, FileDownloader
.class);
218 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, account
);
219 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
223 observer
.startWatching();
224 Log
.d(TAG
, "Started watching " + localPath
);
232 * Unregisters the local copy of a remote file to be observed for local changes.
234 * @param file Object representing a remote file which local copy must be not observed longer.
235 * @param account OwnCloud account containing file.
237 private void removeObservedFile(OCFile file
, Account account
) {
239 Log
.e(TAG
, "Trying to unobserve a NULL file");
242 if (mObservers
== null
) {
243 initializeObservedList();
245 String localPath
= file
.getStoragePath();
246 if (!file
.isDown()) {
247 // this happens when a file not in the device is set to be kept synchronized, and quickly unset again,
248 // while the download is not finished
249 localPath
= FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
);
252 for (int i
= 0; i
< mObservers
.size(); ++i
) {
253 OwnCloudFileObserver observer
= mObservers
.get(i
);
254 if (observer
.getPath().equals(localPath
)) {
255 observer
.stopWatching();
256 mObservers
.remove(i
); // assuming, again, that a local file can be only linked to only ONE remote file; currently false
257 if (!file
.isDown()) {
258 // TODO unregister download receiver ;forget this until list of receivers is replaced for a single receiver
260 Log
.d(TAG
, "Stopped watching " + localPath
);
269 * Temporarily disables the observance of a file that is going to be download.
271 * @param file Object representing the remote file which local copy must not be observed temporarily.
272 * @param account OwnCloud account containing file.
274 private void addDownloadingFile(OCFile file
, Account account
) {
275 OwnCloudFileObserver observer
= null
;
276 for (OwnCloudFileObserver o
: mObservers
) {
277 if (o
.getRemotePath().equals(file
.getRemotePath()) && o
.getAccount().equals(account
)) {
282 if (observer
== null
) {
283 Log
.e(TAG
, "Couldn't find observer for remote file " + file
.getRemotePath());
286 observer
.stopWatching();
287 DownloadCompletedReceiver dcr
= new DownloadCompletedReceiver(observer
.getPath(), observer
);
288 registerReceiver(dcr
, new IntentFilter(FileDownloader
.DOWNLOAD_FINISH_MESSAGE
));
292 private static void addReceiverToList(DownloadCompletedReceiver r
) {
293 synchronized(mReceiverListLock
) {
294 mDownloadReceivers
.add(r
);
298 private static void removeReceiverFromList(DownloadCompletedReceiver r
) {
299 synchronized(mReceiverListLock
) {
300 mDownloadReceivers
.remove(r
);
305 public void OnObservedFileStatusUpdate(String localPath
, String remotePath
, Account account
, Status status
) {
309 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
310 Intent i
= new Intent(getApplicationContext(), ConflictsResolveActivity
.class);
311 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
312 i
.putExtra("remotepath", remotePath
);
313 i
.putExtra("localpath", localPath
);
314 i
.putExtra("account", account
);
318 case SENDING_TO_UPLOADER
:
322 Log
.wtf(TAG
, "Unhandled status " + status
);
326 private class DownloadCompletedReceiver
extends BroadcastReceiver
{
328 OwnCloudFileObserver mObserver
;
330 public DownloadCompletedReceiver(String path
, OwnCloudFileObserver observer
) {
332 mObserver
= observer
;
333 addReceiverToList(this);
337 public void onReceive(Context context
, Intent intent
) {
338 if (mPath
.equals(intent
.getStringExtra(FileDownloader
.EXTRA_FILE_PATH
))) {
339 if ((new File(mPath
)).exists()) {
340 // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload
341 context
.unregisterReceiver(this);
342 removeReceiverFromList(this);
343 mObserver
.startWatching();
344 Log
.d(TAG
, "Started watching " + mPath
);
346 } // else - keep waiting for a future retry of the download ;
347 // mObserver.startWatching() won't ever work if the file is not in the device when it's called
352 public boolean equals(Object o
) {
353 if (o
instanceof DownloadCompletedReceiver
)
354 return mPath
.equals(((DownloadCompletedReceiver
)o
).mPath
);
355 return super.equals(o
);