Fixed bug: when a file is checked as 'keep in sync' and the immediate synchronization...
[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.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;
34
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;
46
47 public class FileObserverService extends Service implements FileObserverStatusListener {
48
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";
52
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;
57
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();
63
64 public class LocalBinder extends Binder {
65 FileObserverService getService() {
66 return FileObserverService.this;
67 }
68 }
69
70 @Override
71 public IBinder onBind(Intent intent) {
72 return mBinder;
73 }
74
75 @Override
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
79 if (intent == null) {
80 initializeObservedList();
81 return Service.START_STICKY;
82 }
83
84 if (!intent.hasExtra(KEY_FILE_CMD)) {
85 Log.e(TAG, "No KEY_FILE_CMD argument given");
86 return Service.START_STICKY;
87 }
88
89 switch (intent.getIntExtra(KEY_FILE_CMD, -1)) {
90 case CMD_INIT_OBSERVED_LIST:
91 initializeObservedList();
92 break;
93 case CMD_ADD_OBSERVED_FILE:
94 addObservedFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
95 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
96 break;
97 case CMD_DEL_OBSERVED_FILE:
98 removeObservedFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
99 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
100 break;
101 case CMD_ADD_DOWNLOADING_FILE:
102 addDownloadingFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
103 (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
104 break;
105 default:
106 Log.wtf(TAG, "Incorrect key given");
107 }
108
109 return Service.START_STICKY;
110 }
111
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,
118 null,
119 ProviderTableMeta.FILE_KEEP_IN_SYNC + " = ?",
120 new String[] {String.valueOf(1)},
121 null);
122 if (!c.moveToFirst()) return;
123 AccountManager acm = AccountManager.get(this);
124 Account[] accounts = acm.getAccounts();
125 do {
126 Account account = null;
127 for (Account a : accounts)
128 if (a.name.equals(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ACCOUNT_OWNER)))) {
129 account = a;
130 break;
131 }
132
133 if (account == null) continue;
134 FileDataStorageManager storage =
135 new FileDataStorageManager(account, getContentResolver());
136 if (!storage.fileExists(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))))
137 continue;
138
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);
150
151 } while (c.moveToNext());
152 c.close();
153 }
154
155 /**
156 * Registers the local copy of a remote file to be observed for local changes,
157 * an automatically updated in the ownCloud server.
158 *
159 * @param file Object representing a remote file which local copy must be observed.
160 * @param account OwnCloud account containing file.
161 */
162 private void addObservedFile(OCFile file, Account account) {
163 if (file == null) {
164 Log.e(TAG, "Trying to observe a NULL file");
165 return;
166 }
167 if (mObservers == null) {
168 // this is very rare case when service was killed by system
169 // and observers list was deleted in that procedure
170 initializeObservedList();
171 }
172 String localPath = file.getStoragePath();
173 if (!file.isDown()) {
174 // this is a file downloading / to be download for the first time
175 localPath = FileStorageUtils.getDefaultSavePathFor(account.name, file);
176 }
177 OwnCloudFileObserver tmpObserver = null, observer = null;
178 for (int i = 0; i < mObservers.size(); ++i) {
179 tmpObserver = mObservers.get(i);
180 if (tmpObserver.getPath().equals(localPath)) {
181 observer = tmpObserver;
182 }
183 tmpObserver.setContext(getApplicationContext()); // 'refreshing' context to all the observers? why?
184 }
185 if (observer == null) {
186 /// the local file was never registered to observe before
187 observer = new OwnCloudFileObserver(localPath, OwnCloudFileObserver.CHANGES_ONLY);
188 //Account account = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
189 observer.setAccount(account);
190 FileDataStorageManager storage =
191 new FileDataStorageManager(account, getContentResolver()); // I don't trust in this resolver's life span...
192 observer.setStorageManager(storage);
193 //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
194 observer.setOCFile(file);
195 observer.addObserverStatusListener(this);
196 observer.setContext(getApplicationContext());
197
198 } else {
199 /* LET'S IGNORE THAT, CURRENTLY, A LOCAL FILE CAN BE LINKED TO DIFFERENT FILES IN OWNCLOUD;
200 * we should change that
201 *
202 /// the local file is already observed for some other OCFile(s)
203 observer.addOCFile(account, file); // OCFiles should have a reference to the account containing them to not be confused
204 */
205 }
206
207 mObservers.add(observer);
208 Log.d(TAG, "Observer added for path " + localPath);
209
210 if (!file.isDown()) {
211 // if the file is not down, it can't be observed for changes
212 DownloadCompletedReceiver receiver = new DownloadCompletedReceiver(localPath, observer);
213 registerReceiver(receiver, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
214
215 } else {
216 observer.startWatching();
217 Log.d(TAG, "Started watching " + localPath);
218
219 }
220
221 }
222
223
224 /**
225 * Unregisters the local copy of a remote file to be observed for local changes.
226 *
227 * @param file Object representing a remote file which local copy must be not observed longer.
228 * @param account OwnCloud account containing file.
229 */
230 private void removeObservedFile(OCFile file, Account account) {
231 if (file == null) {
232 Log.e(TAG, "Trying to unobserve a NULL file");
233 return;
234 }
235 if (mObservers == null) {
236 initializeObservedList();
237 }
238 String localPath = file.getStoragePath();
239 if (!file.isDown()) {
240 // this happens when a file not in the device is set to be kept synchronized, and quickly unset again,
241 // while the download is not finished
242 localPath = FileStorageUtils.getDefaultSavePathFor(account.name, file);
243 }
244
245 for (int i = 0; i < mObservers.size(); ++i) {
246 OwnCloudFileObserver observer = mObservers.get(i);
247 if (observer.getPath().equals(localPath)) {
248 observer.stopWatching();
249 mObservers.remove(i); // assuming, again, that a local file can be only linked to only ONE remote file; currently false
250 if (!file.isDown()) {
251 // TODO unregister download receiver ;forget this until list of receivers is replaced for a single receiver
252 }
253 Log.d(TAG, "Stopped watching " + localPath);
254 break;
255 }
256 }
257
258 }
259
260
261 /**
262 * Temporarily disables the observance of a file that is going to be download.
263 *
264 * @param file Object representing the remote file which local copy must not be observed temporarily.
265 * @param account OwnCloud account containing file.
266 */
267 private void addDownloadingFile(OCFile file, Account account) {
268 OwnCloudFileObserver observer = null;
269 for (OwnCloudFileObserver o : mObservers) {
270 if (o.getRemotePath().equals(file.getRemotePath()) && o.getAccount().equals(account)) {
271 observer = o;
272 break;
273 }
274 }
275 if (observer == null) {
276 Log.e(TAG, "Couldn't find observer for remote file " + file.getRemotePath());
277 return;
278 }
279 observer.stopWatching();
280 DownloadCompletedReceiver dcr = new DownloadCompletedReceiver(observer.getPath(), observer);
281 registerReceiver(dcr, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
282 }
283
284
285 private static void addReceiverToList(DownloadCompletedReceiver r) {
286 synchronized(mReceiverListLock) {
287 mDownloadReceivers.add(r);
288 }
289 }
290
291 private static void removeReceiverFromList(DownloadCompletedReceiver r) {
292 synchronized(mReceiverListLock) {
293 mDownloadReceivers.remove(r);
294 }
295 }
296
297 @Override
298 public void onObservedFileStatusUpdate(String localPath, String remotePath, Account account, RemoteOperationResult result) {
299 if (!result.isSuccess()) {
300 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
301 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
302 Intent i = new Intent(getApplicationContext(), ConflictsResolveActivity.class);
303 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
304 i.putExtra("remotepath", remotePath);
305 i.putExtra("localpath", localPath);
306 i.putExtra("account", account);
307 startActivity(i);
308
309 } else {
310 // TODO send notification to the notification bar?
311 }
312 } // else, nothing else to do; now it's duty of FileUploader service
313 }
314
315 private class DownloadCompletedReceiver extends BroadcastReceiver {
316 String mPath;
317 OwnCloudFileObserver mObserver;
318
319 public DownloadCompletedReceiver(String path, OwnCloudFileObserver observer) {
320 mPath = path;
321 mObserver = observer;
322 addReceiverToList(this);
323 }
324
325 @Override
326 public void onReceive(Context context, Intent intent) {
327 if (mPath.equals(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH))) {
328 if ((new File(mPath)).exists()) {
329 // the download could be successful, or not; in both cases, the file could be down, due to a former download or upload
330 context.unregisterReceiver(this);
331 removeReceiverFromList(this);
332 mObserver.startWatching();
333 Log.d(TAG, "Started watching " + mPath);
334 return;
335 } // else - keep waiting for a future retry of the download ;
336 // mObserver.startWatching() won't ever work if the file is not in the device when it's called
337 }
338 }
339
340 @Override
341 public boolean equals(Object o) {
342 if (o instanceof DownloadCompletedReceiver)
343 return mPath.equals(((DownloadCompletedReceiver)o).mPath);
344 return super.equals(o);
345 }
346 }
347 }