Fixed lack of observance for files that are set as favourites when never were downloa...
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / services / FileObserverService.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.files.services;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
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;
32
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;
44
45 public class FileObserverService extends Service implements FileObserverStatusListener {
46
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";
50
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;
55
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();
61
62 public class LocalBinder extends Binder {
63 FileObserverService getService() {
64 return FileObserverService.this;
65 }
66 }
67
68 @Override
69 public IBinder onBind(Intent intent) {
70 return mBinder;
71 }
72
73 @Override
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
77 if (intent == null) {
78 initializeObservedList();
79 return Service.START_STICKY;
80 }
81
82 if (!intent.hasExtra(KEY_FILE_CMD)) {
83 Log.e(TAG, "No KEY_FILE_CMD argument given");
84 return Service.START_STICKY;
85 }
86
87 switch (intent.getIntExtra(KEY_FILE_CMD, -1)) {
88 case CMD_INIT_OBSERVED_LIST:
89 initializeObservedList();
90 break;
91 case CMD_ADD_OBSERVED_FILE:
92 addObservedFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
93 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
94 break;
95 case CMD_DEL_OBSERVED_FILE:
96 removeObservedFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
97 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
98 break;
99 case CMD_ADD_DOWNLOADING_FILE:
100 addDownloadingFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
101 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
102 break;
103 default:
104 Log.wtf(TAG, "Incorrect key given");
105 }
106
107 return Service.START_STICKY;
108 }
109
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,
116 null,
117 ProviderTableMeta.FILE_KEEP_IN_SYNC + " = ?",
118 new String[] {String.valueOf(1)},
119 null);
120 if (!c.moveToFirst()) return;
121 AccountManager acm = AccountManager.get(this);
122 Account[] accounts = acm.getAccounts();
123 do {
124 Account account = null;
125 for (Account a : accounts)
126 if (a.name.equals(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ACCOUNT_OWNER)))) {
127 account = a;
128 break;
129 }
130
131 if (account == null) continue;
132 FileDataStorageManager storage =
133 new FileDataStorageManager(account, getContentResolver());
134 if (!storage.fileExists(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))))
135 continue;
136
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);
148
149 } while (c.moveToNext());
150 c.close();
151 }
152
153 /**
154 * Registers the local copy of a remote file to be observed for local changes,
155 * an automatically updated in the ownCloud server.
156 *
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
159 * is finished.
160 *
161 * @param file Object representing a remote file which local copy must be observed.
162 * @param account OwnCloud account containing file.
163 */
164 private void addObservedFile(OCFile file, Account account) {
165 if (file == null) {
166 Log.e(TAG, "Trying to observe a NULL file");
167 return;
168 }
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();
173 }
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);
178 }
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;
184 }
185 tmpObserver.setContext(getApplicationContext()); // 'refreshing' context to all the observers? why?
186 }
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());
199
200 } else {
201 /* LET'S IGNORE THAT, CURRENTLY, A LOCAL FILE CAN BE LINKED TO DIFFERENT FILES IN OWNCLOUD;
202 * we should change that
203 *
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
206 */
207 }
208
209 mObservers.add(observer);
210 Log.d(TAG, "Observer added for path " + localPath);
211
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));
216
217 Intent i = new Intent(this, FileDownloader.class);
218 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
219 i.putExtra(FileDownloader.EXTRA_FILE, file);
220 startService(i);
221
222 } else {
223 observer.startWatching();
224 Log.d(TAG, "Started watching " + localPath);
225
226 }
227
228 }
229
230
231 /**
232 * Unregisters the local copy of a remote file to be observed for local changes.
233 *
234 * @param file Object representing a remote file which local copy must be not observed longer.
235 * @param account OwnCloud account containing file.
236 */
237 private void removeObservedFile(OCFile file, Account account) {
238 if (file == null) {
239 Log.e(TAG, "Trying to unobserve a NULL file");
240 return;
241 }
242 if (mObservers == null) {
243 initializeObservedList();
244 }
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);
250 }
251
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
259 }
260 Log.d(TAG, "Stopped watching " + localPath);
261 break;
262 }
263 }
264
265 }
266
267
268 /**
269 * Temporarily disables the observance of a file that is going to be download.
270 *
271 * @param file Object representing the remote file which local copy must not be observed temporarily.
272 * @param account OwnCloud account containing file.
273 */
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)) {
278 observer = o;
279 break;
280 }
281 }
282 if (observer == null) {
283 Log.e(TAG, "Couldn't find observer for remote file " + file.getRemotePath());
284 return;
285 }
286 observer.stopWatching();
287 DownloadCompletedReceiver dcr = new DownloadCompletedReceiver(observer.getPath(), observer);
288 registerReceiver(dcr, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
289 }
290
291
292 private static void addReceiverToList(DownloadCompletedReceiver r) {
293 synchronized(mReceiverListLock) {
294 mDownloadReceivers.add(r);
295 }
296 }
297
298 private static void removeReceiverFromList(DownloadCompletedReceiver r) {
299 synchronized(mReceiverListLock) {
300 mDownloadReceivers.remove(r);
301 }
302 }
303
304 @Override
305 public void OnObservedFileStatusUpdate(String localPath, String remotePath, Account account, Status status) {
306 switch (status) {
307 case CONFLICT:
308 {
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);
315 startActivity(i);
316 break;
317 }
318 case SENDING_TO_UPLOADER:
319 case INCORRECT_MASK:
320 break;
321 default:
322 Log.wtf(TAG, "Unhandled status " + status);
323 }
324 }
325
326 private class DownloadCompletedReceiver extends BroadcastReceiver {
327 String mPath;
328 OwnCloudFileObserver mObserver;
329
330 public DownloadCompletedReceiver(String path, OwnCloudFileObserver observer) {
331 mPath = path;
332 mObserver = observer;
333 addReceiverToList(this);
334 }
335
336 @Override
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);
345 return;
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
348 }
349 }
350
351 @Override
352 public boolean equals(Object o) {
353 if (o instanceof DownloadCompletedReceiver)
354 return mPath.equals(((DownloadCompletedReceiver)o).mPath);
355 return super.equals(o);
356 }
357 }
358 }