From: Bartek Przybylski Date: Mon, 22 Oct 2012 19:15:26 +0000 (+0200) Subject: Two way synchronization for files X-Git-Tag: oc-android-1.4.3~132^2~3 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/de94751cde3af2e78212f043d3d0c2aed90f69a8?ds=inline Two way synchronization for files --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ae536340..91b215d9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -143,12 +143,12 @@ - - + + diff --git a/src/com/owncloud/android/files/OwnCloudFileObserver.java b/src/com/owncloud/android/files/OwnCloudFileObserver.java index b01b8fa3..42e4763f 100644 --- a/src/com/owncloud/android/files/OwnCloudFileObserver.java +++ b/src/com/owncloud/android/files/OwnCloudFileObserver.java @@ -52,9 +52,14 @@ public class OwnCloudFileObserver extends FileObserver { return mPath; } + public String getRemotePath() { + return mFile.getRemotePath(); + } + @Override public void onEvent(int event, String path) { - if ((event | mMask) == 0) { + Log.d(TAG, "Got file modified with event " + event + " and path " + path); + if ((event & mMask) == 0) { Log.wtf(TAG, "Incorrect event " + event + " sent for file " + path + " with registered for " + mMask + " and original path " + mPath); diff --git a/src/com/owncloud/android/files/services/FileObserverService.java b/src/com/owncloud/android/files/services/FileObserverService.java index 4d6dd027..25b5f1c6 100644 --- a/src/com/owncloud/android/files/services/FileObserverService.java +++ b/src/com/owncloud/android/files/services/FileObserverService.java @@ -28,6 +28,7 @@ public class FileObserverService extends Service { 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; private static String TAG = "FileObserverService"; private static List mObservers; @@ -70,13 +71,16 @@ public class FileObserverService extends Service { case CMD_DEL_OBSERVED_FILE: removeObservedFile(intent.getStringExtra(KEY_CMD_ARG)); break; + case CMD_ADD_DOWNLOADING_FILE: + addDownloadingFile(intent.getStringExtra(KEY_CMD_ARG)); + break; default: Log.wtf(TAG, "Incorrect key given"); } return Service.START_STICKY; } - + private void initializeObservedList() { if (mObservers != null) return; // nothing to do here mObservers = new ArrayList(); @@ -107,7 +111,7 @@ public class FileObserverService extends Service { String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); OwnCloudFileObserver observer = new OwnCloudFileObserver(path, OwnCloudFileObserver.CHANGES_ONLY); - observer.setContext(getBaseContext()); + observer.setContext(getApplicationContext()); observer.setAccount(account); observer.setStorageManager(storage); observer.setOCFile(storage.getFileByPath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH)))); @@ -167,6 +171,24 @@ public class FileObserverService extends Service { } Log.d(TAG, "Stopped watching " + path); } + + private void addDownloadingFile(String remotePath) { + OwnCloudFileObserver observer = null; + for (OwnCloudFileObserver o : mObservers) { + if (o.getRemotePath().equals(remotePath)) { + observer = o; + break; + } + } + if (observer == null) { + Log.e(TAG, "Couldn't find observer for remote file " + remotePath); + 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) { diff --git a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java index 5b398c01..8c9a1535 100644 --- a/src/com/owncloud/android/syncadapter/FileSyncAdapter.java +++ b/src/com/owncloud/android/syncadapter/FileSyncAdapter.java @@ -34,6 +34,7 @@ import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; +import com.owncloud.android.files.services.FileObserverService; import com.owncloud.android.utils.OwnCloudVersion; import android.accounts.Account; @@ -211,7 +212,13 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter { getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() && file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath()) .getModificationTimestamp()) { - Intent intent = new Intent(this.getContext(), FileDownloader.class); + // first disable observer so we won't get file upload right after download + Log.d(TAG, "Disabling observation of remote file" + file.getRemotePath()); + Intent intent = new Intent(getContext(), FileObserverService.class); + intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_ADD_DOWNLOADING_FILE); + intent.putExtra(FileObserverService.KEY_CMD_ARG, file.getRemotePath()); + getContext().startService(intent); + intent = new Intent(this.getContext(), FileDownloader.class); intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount()); intent.putExtra(FileDownloader.EXTRA_FILE, file); file.setKeepInSync(true); diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index 22ef63b4..328d3966 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -68,6 +68,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; +import com.owncloud.android.files.services.FileObserverService; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; import com.owncloud.android.network.OwnCloudClientUtils; @@ -154,10 +155,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements } // file observer - /*Intent observer_intent = new Intent(this, FileObserverService.class); + Intent observer_intent = new Intent(this, FileObserverService.class); observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST); startService(observer_intent); - */ + /// USER INTERFACE requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); diff --git a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java index 007c294f..3f0be07e 100644 --- a/src/com/owncloud/android/ui/fragment/FileDetailFragment.java +++ b/src/com/owncloud/android/ui/fragment/FileDetailFragment.java @@ -78,6 +78,7 @@ import com.owncloud.android.authenticator.AccountAuthenticator; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; +import com.owncloud.android.files.services.FileObserverService; import com.owncloud.android.files.services.FileUploader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.files.services.FileUploader.FileUploaderBinder; @@ -302,7 +303,7 @@ public class FileDetailFragment extends SherlockFragment implements } else { mContainerActivity.onFileStateChanged(); // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn) } - /* + Intent intent = new Intent(getActivity().getApplicationContext(), FileObserverService.class); intent.putExtra(FileObserverService.KEY_FILE_CMD, @@ -310,8 +311,9 @@ public class FileDetailFragment extends SherlockFragment implements FileObserverService.CMD_ADD_OBSERVED_FILE: FileObserverService.CMD_DEL_OBSERVED_FILE)); intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath()); + Log.e(TAG, "starting observer service"); getActivity().startService(intent); - */ + break; } case R.id.fdRenameBtn: {