Two way synchronization for files

This commit is contained in:
Bartek Przybylski 2012-10-22 21:15:26 +02:00
parent 9faab34fbd
commit de94751cde
6 changed files with 48 additions and 11 deletions

View file

@ -143,12 +143,12 @@
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<!-- receiver android:name=".files.BootupBroadcastReceiver">
<receiver android:name=".files.BootupBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver -->
<!-- service android:name=".files.services.FileObserverService"/ -->
</receiver>
<service android:name=".files.services.FileObserverService"/>
</application>
</manifest>

View file

@ -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);

View file

@ -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<OwnCloudFileObserver> 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<OwnCloudFileObserver>();
@ -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) {

View file

@ -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);

View file

@ -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);

View file

@ -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: {