|
@@ -19,8 +19,8 @@
|
|
|
package com.owncloud.android.files.services;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
|
@@ -29,6 +29,7 @@ import com.owncloud.android.files.OwnCloudFileObserver;
|
|
|
import com.owncloud.android.files.OwnCloudFileObserver.FileObserverStatusListener;
|
|
|
import com.owncloud.android.operations.RemoteOperationResult;
|
|
|
import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
|
|
|
+import com.owncloud.android.operations.SynchronizeFileOperation;
|
|
|
import com.owncloud.android.ui.activity.ConflictsResolveActivity;
|
|
|
import com.owncloud.android.utils.FileStorageUtils;
|
|
|
|
|
@@ -46,19 +47,18 @@ import android.util.Log;
|
|
|
|
|
|
public class FileObserverService extends Service implements FileObserverStatusListener {
|
|
|
|
|
|
- public final static String KEY_FILE_CMD = "KEY_FILE_CMD";
|
|
|
- public final static String KEY_CMD_ARG_FILE = "KEY_CMD_ARG_FILE";
|
|
|
- public final static String KEY_CMD_ARG_ACCOUNT = "KEY_CMD_ARG_ACCOUNT";
|
|
|
-
|
|
|
public final static int CMD_INIT_OBSERVED_LIST = 1;
|
|
|
public final static int CMD_ADD_OBSERVED_FILE = 2;
|
|
|
public final static int CMD_DEL_OBSERVED_FILE = 3;
|
|
|
- public final static int CMD_ADD_DOWNLOADING_FILE = 4;
|
|
|
+
|
|
|
+ public final static String KEY_FILE_CMD = "KEY_FILE_CMD";
|
|
|
+ public final static String KEY_CMD_ARG_FILE = "KEY_CMD_ARG_FILE";
|
|
|
+ public final static String KEY_CMD_ARG_ACCOUNT = "KEY_CMD_ARG_ACCOUNT";
|
|
|
|
|
|
private static String TAG = FileObserverService.class.getSimpleName();
|
|
|
- private static List<OwnCloudFileObserver> mObservers;
|
|
|
- private static List<DownloadCompletedReceiver> mDownloadReceivers;
|
|
|
- private static Object mReceiverListLock = new Object();
|
|
|
+
|
|
|
+ private static Map<String, OwnCloudFileObserver> mObserversMap;
|
|
|
+ private static DownloadCompletedReceiverBis mDownloadReceiver;
|
|
|
private IBinder mBinder = new LocalBinder();
|
|
|
|
|
|
public class LocalBinder extends Binder {
|
|
@@ -67,6 +67,28 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void onCreate() {
|
|
|
+ super.onCreate();
|
|
|
+ mDownloadReceiver = new DownloadCompletedReceiverBis();
|
|
|
+ IntentFilter filter = new IntentFilter();
|
|
|
+ filter.addAction(FileDownloader.DOWNLOAD_ADDED_MESSAGE);
|
|
|
+ filter.addAction(FileDownloader.DOWNLOAD_FINISH_MESSAGE);
|
|
|
+ registerReceiver(mDownloadReceiver, filter);
|
|
|
+
|
|
|
+ mObserversMap = new HashMap<String, OwnCloudFileObserver>();
|
|
|
+ initializeObservedList();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ unregisterReceiver(mDownloadReceiver);
|
|
|
+ mObserversMap = null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
return mBinder;
|
|
@@ -98,10 +120,6 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
removeObservedFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
|
|
|
(Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
|
|
|
break;
|
|
|
- case CMD_ADD_DOWNLOADING_FILE:
|
|
|
- addDownloadingFile( (OCFile)intent.getParcelableExtra(KEY_CMD_ARG_FILE),
|
|
|
- (Account)intent.getParcelableExtra(KEY_CMD_ARG_ACCOUNT));
|
|
|
- break;
|
|
|
default:
|
|
|
Log.wtf(TAG, "Incorrect key given");
|
|
|
}
|
|
@@ -109,10 +127,13 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
return Service.START_STICKY;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ * Read from the local database the list of files that must to be kept synchronized and
|
|
|
+ * starts file observers to monitor local changes on them
|
|
|
+ */
|
|
|
private void initializeObservedList() {
|
|
|
- if (mObservers != null) return;
|
|
|
- mObservers = new ArrayList<OwnCloudFileObserver>();
|
|
|
- mDownloadReceivers = new ArrayList<DownloadCompletedReceiver>();
|
|
|
+ mObserversMap.clear();
|
|
|
Cursor c = getContentResolver().query(
|
|
|
ProviderTableMeta.CONTENT_URI,
|
|
|
null,
|
|
@@ -144,9 +165,11 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
observer.setStorageManager(storage);
|
|
|
observer.setOCFile(storage.getFileByPath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))));
|
|
|
observer.addObserverStatusListener(this);
|
|
|
- observer.startWatching();
|
|
|
- mObservers.add(observer);
|
|
|
- Log.d(TAG, "Started watching file " + path);
|
|
|
+ mObserversMap.put(path, observer);
|
|
|
+ if (new File(path).exists()) {
|
|
|
+ observer.startWatching();
|
|
|
+ Log.d(TAG, "Started watching file " + path);
|
|
|
+ }
|
|
|
|
|
|
} while (c.moveToNext());
|
|
|
c.close();
|
|
@@ -155,33 +178,26 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
|
|
|
* Registers the local copy of a remote file to be observed for local changes,
|
|
|
* an automatically updated in the ownCloud server.
|
|
|
+ *
|
|
|
+ * This method does NOT perform a {@link SynchronizeFileOperation} over the file.
|
|
|
*
|
|
|
+ * TODO We are ignoring that, currently, a local file can be linked to different files
|
|
|
+ * in ownCloud if it's uploaded several times. That's something pending to update: we
|
|
|
+ * will avoid that the same local file is linked to different remote files.
|
|
|
+ *
|
|
|
* @param file Object representing a remote file which local copy must be observed.
|
|
|
* @param account OwnCloud account containing file.
|
|
|
*/
|
|
|
private void addObservedFile(OCFile file, Account account) {
|
|
|
if (file == null) {
|
|
|
- Log.e(TAG, "Trying to observe a NULL file");
|
|
|
+ Log.e(TAG, "Trying to add a NULL file to observer");
|
|
|
return;
|
|
|
}
|
|
|
- if (mObservers == null) {
|
|
|
-
|
|
|
-
|
|
|
- initializeObservedList();
|
|
|
- }
|
|
|
String localPath = file.getStoragePath();
|
|
|
- if (!file.isDown()) {
|
|
|
-
|
|
|
+ if (localPath == null || localPath.length() <= 0) {
|
|
|
localPath = FileStorageUtils.getDefaultSavePathFor(account.name, file);
|
|
|
}
|
|
|
- OwnCloudFileObserver tmpObserver = null, observer = null;
|
|
|
- for (int i = 0; i < mObservers.size(); ++i) {
|
|
|
- tmpObserver = mObservers.get(i);
|
|
|
- if (tmpObserver.getPath().equals(localPath)) {
|
|
|
- observer = tmpObserver;
|
|
|
- }
|
|
|
- tmpObserver.setContext(getApplicationContext());
|
|
|
- }
|
|
|
+ OwnCloudFileObserver observer = mObserversMap.get(localPath);
|
|
|
if (observer == null) {
|
|
|
|
|
|
observer = new OwnCloudFileObserver(localPath, OwnCloudFileObserver.CHANGES_ONLY);
|
|
@@ -194,28 +210,14 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
observer.setOCFile(file);
|
|
|
observer.addObserverStatusListener(this);
|
|
|
observer.setContext(getApplicationContext());
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- * we should change that
|
|
|
- *
|
|
|
-
|
|
|
- observer.addOCFile(account, file);
|
|
|
- */
|
|
|
- }
|
|
|
|
|
|
- mObservers.add(observer);
|
|
|
- Log.d(TAG, "Observer added for path " + localPath);
|
|
|
+ mObserversMap.put(localPath, observer);
|
|
|
+ Log.d(TAG, "Observer added for path " + localPath);
|
|
|
|
|
|
- if (!file.isDown()) {
|
|
|
-
|
|
|
- DownloadCompletedReceiver receiver = new DownloadCompletedReceiver(localPath, observer);
|
|
|
- registerReceiver(receiver, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
|
|
|
-
|
|
|
- } else {
|
|
|
- observer.startWatching();
|
|
|
- Log.d(TAG, "Started watching " + localPath);
|
|
|
-
|
|
|
+ if (file.isDown()) {
|
|
|
+ observer.startWatching();
|
|
|
+ Log.d(TAG, "Started watching " + localPath);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -224,76 +226,35 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
|
|
|
* Unregisters the local copy of a remote file to be observed for local changes.
|
|
|
*
|
|
|
+ * Starts to watch it, if the file has a local copy to watch.
|
|
|
+ *
|
|
|
+ * TODO We are ignoring that, currently, a local file can be linked to different files
|
|
|
+ * in ownCloud if it's uploaded several times. That's something pending to update: we
|
|
|
+ * will avoid that the same local file is linked to different remote files.
|
|
|
+ *
|
|
|
* @param file Object representing a remote file which local copy must be not observed longer.
|
|
|
* @param account OwnCloud account containing file.
|
|
|
*/
|
|
|
private void removeObservedFile(OCFile file, Account account) {
|
|
|
if (file == null) {
|
|
|
- Log.e(TAG, "Trying to unobserve a NULL file");
|
|
|
+ Log.e(TAG, "Trying to remove a NULL file");
|
|
|
return;
|
|
|
}
|
|
|
- if (mObservers == null) {
|
|
|
- initializeObservedList();
|
|
|
- }
|
|
|
String localPath = file.getStoragePath();
|
|
|
- if (!file.isDown()) {
|
|
|
-
|
|
|
-
|
|
|
+ if (localPath == null || localPath.length() <= 0) {
|
|
|
localPath = FileStorageUtils.getDefaultSavePathFor(account.name, file);
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < mObservers.size(); ++i) {
|
|
|
- OwnCloudFileObserver observer = mObservers.get(i);
|
|
|
- if (observer.getPath().equals(localPath)) {
|
|
|
- observer.stopWatching();
|
|
|
- mObservers.remove(i);
|
|
|
- if (!file.isDown()) {
|
|
|
-
|
|
|
- }
|
|
|
- Log.d(TAG, "Stopped watching " + localPath);
|
|
|
- break;
|
|
|
- }
|
|
|
+ OwnCloudFileObserver observer = mObserversMap.get(localPath);
|
|
|
+ if (observer != null) {
|
|
|
+ observer.stopWatching();
|
|
|
+ mObserversMap.remove(observer);
|
|
|
+ Log.d(TAG, "Stopped watching " + localPath);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- * Temporarily disables the observance of a file that is going to be download.
|
|
|
- *
|
|
|
- * @param file Object representing the remote file which local copy must not be observed temporarily.
|
|
|
- * @param account OwnCloud account containing file.
|
|
|
- */
|
|
|
- private void addDownloadingFile(OCFile file, Account account) {
|
|
|
- OwnCloudFileObserver observer = null;
|
|
|
- for (OwnCloudFileObserver o : mObservers) {
|
|
|
- if (o.getRemotePath().equals(file.getRemotePath()) && o.getAccount().equals(account)) {
|
|
|
- observer = o;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (observer == null) {
|
|
|
- Log.e(TAG, "Couldn't find observer for remote file " + file.getRemotePath());
|
|
|
- return;
|
|
|
- }
|
|
|
- observer.stopWatching();
|
|
|
- DownloadCompletedReceiver dcr = new DownloadCompletedReceiver(observer.getPath(), observer);
|
|
|
- registerReceiver(dcr, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private static void addReceiverToList(DownloadCompletedReceiver r) {
|
|
|
- synchronized(mReceiverListLock) {
|
|
|
- mDownloadReceivers.add(r);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private static void removeReceiverFromList(DownloadCompletedReceiver r) {
|
|
|
- synchronized(mReceiverListLock) {
|
|
|
- mDownloadReceivers.remove(r);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
@Override
|
|
|
public void onObservedFileStatusUpdate(String localPath, String remotePath, Account account, RemoteOperationResult result) {
|
|
|
if (!result.isSuccess()) {
|
|
@@ -311,37 +272,34 @@ public class FileObserverService extends Service implements FileObserverStatusLi
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
|
|
|
- private class DownloadCompletedReceiver extends BroadcastReceiver {
|
|
|
- String mPath;
|
|
|
- OwnCloudFileObserver mObserver;
|
|
|
-
|
|
|
- public DownloadCompletedReceiver(String path, OwnCloudFileObserver observer) {
|
|
|
- mPath = path;
|
|
|
- mObserver = observer;
|
|
|
- addReceiverToList(this);
|
|
|
- }
|
|
|
+
|
|
|
+ * Private receiver listening to events broadcast by the FileDownloader service.
|
|
|
+ *
|
|
|
+ * Starts and stops the observance on registered files when they are being download,
|
|
|
+ * in order to avoid to start unnecessary synchronizations.
|
|
|
+ */
|
|
|
+ private class DownloadCompletedReceiverBis extends BroadcastReceiver {
|
|
|
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
- if (mPath.equals(intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH))) {
|
|
|
- if ((new File(mPath)).exists()) {
|
|
|
-
|
|
|
- context.unregisterReceiver(this);
|
|
|
- removeReceiverFromList(this);
|
|
|
- mObserver.startWatching();
|
|
|
- Log.d(TAG, "Started watching " + mPath);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
+ String downloadPath = intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH);
|
|
|
+ OwnCloudFileObserver observer = mObserversMap.get(downloadPath);
|
|
|
+ if (observer != null) {
|
|
|
+ if (intent.getAction().equals(FileDownloader.DOWNLOAD_FINISH_MESSAGE) &&
|
|
|
+ new File(downloadPath).exists()) {
|
|
|
+ observer.startWatching();
|
|
|
+ Log.d(TAG, "Watching again " + downloadPath);
|
|
|
+
|
|
|
+ } else if (intent.getAction().equals(FileDownloader.DOWNLOAD_ADDED_MESSAGE)) {
|
|
|
+ observer.stopWatching();
|
|
|
+ Log.d(TAG, "Disabling observance of " + downloadPath);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public boolean equals(Object o) {
|
|
|
- if (o instanceof DownloadCompletedReceiver)
|
|
|
- return mPath.equals(((DownloadCompletedReceiver)o).mPath);
|
|
|
- return super.equals(o);
|
|
|
- }
|
|
|
}
|
|
|
+
|
|
|
}
|