Download button in file details view upgraded to sync file content in both directions
[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 * 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
161 * is finished.
162 *
163 * @param file Object representing a remote file which local copy must be observed.
164 * @param account OwnCloud account containing file.
165 */
166 private void addObservedFile(OCFile file, Account account) {
167 if (file == null) {
168 Log.e(TAG, "Trying to observe a NULL file");
169 return;
170 }
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();
175 }
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);
180 }
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;
186 }
187 tmpObserver.setContext(getApplicationContext()); // 'refreshing' context to all the observers? why?
188 }
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());
201
202 } else {
203 /* LET'S IGNORE THAT, CURRENTLY, A LOCAL FILE CAN BE LINKED TO DIFFERENT FILES IN OWNCLOUD;
204 * we should change that
205 *
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
208 */
209 }
210
211 mObservers.add(observer);
212 Log.d(TAG, "Observer added for path " + localPath);
213
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));
218
219 Intent i = new Intent(this, FileDownloader.class);
220 i.putExtra(FileDownloader.EXTRA_ACCOUNT, account);
221 i.putExtra(FileDownloader.EXTRA_FILE, file);
222 startService(i);
223
224 } else {
225 observer.startWatching();
226 Log.d(TAG, "Started watching " + localPath);
227
228 }
229
230 }
231
232
233 /**
234 * Unregisters the local copy of a remote file to be observed for local changes.
235 *
236 * @param file Object representing a remote file which local copy must be not observed longer.
237 * @param account OwnCloud account containing file.
238 */
239 private void removeObservedFile(OCFile file, Account account) {
240 if (file == null) {
241 Log.e(TAG, "Trying to unobserve a NULL file");
242 return;
243 }
244 if (mObservers == null) {
245 initializeObservedList();
246 }
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);
252 }
253
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
261 }
262 Log.d(TAG, "Stopped watching " + localPath);
263 break;
264 }
265 }
266
267 }
268
269
270 /**
271 * Temporarily disables the observance of a file that is going to be download.
272 *
273 * @param file Object representing the remote file which local copy must not be observed temporarily.
274 * @param account OwnCloud account containing file.
275 */
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)) {
280 observer = o;
281 break;
282 }
283 }
284 if (observer == null) {
285 Log.e(TAG, "Couldn't find observer for remote file " + file.getRemotePath());
286 return;
287 }
288 observer.stopWatching();
289 DownloadCompletedReceiver dcr = new DownloadCompletedReceiver(observer.getPath(), observer);
290 registerReceiver(dcr, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
291 }
292
293
294 private static void addReceiverToList(DownloadCompletedReceiver r) {
295 synchronized(mReceiverListLock) {
296 mDownloadReceivers.add(r);
297 }
298 }
299
300 private static void removeReceiverFromList(DownloadCompletedReceiver r) {
301 synchronized(mReceiverListLock) {
302 mDownloadReceivers.remove(r);
303 }
304 }
305
306 @Override
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);
316 startActivity(i);
317
318 } else {
319 // TODO send notification to the notification bar?
320 }
321 } // else, nothing else to do; now it's duty of FileUploader service
322 }
323
324 private class DownloadCompletedReceiver extends BroadcastReceiver {
325 String mPath;
326 OwnCloudFileObserver mObserver;
327
328 public DownloadCompletedReceiver(String path, OwnCloudFileObserver observer) {
329 mPath = path;
330 mObserver = observer;
331 addReceiverToList(this);
332 }
333
334 @Override
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);
343 return;
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
346 }
347 }
348
349 @Override
350 public boolean equals(Object o) {
351 if (o instanceof DownloadCompletedReceiver)
352 return mPath.equals(((DownloadCompletedReceiver)o).mPath);
353 return super.equals(o);
354 }
355 }
356 }