X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/b2a260286e1dcc5301264087cb699b9741957b80..405e801bf6a3271bc475b485b367a2904e44f3ad:/src/com/owncloud/android/files/services/FileObserverService.java diff --git a/src/com/owncloud/android/files/services/FileObserverService.java b/src/com/owncloud/android/files/services/FileObserverService.java index 4d6dd027..88bf0b91 100644 --- a/src/com/owncloud/android/files/services/FileObserverService.java +++ b/src/com/owncloud/android/files/services/FileObserverService.java @@ -1,3 +1,21 @@ +/* ownCloud Android client application + * Copyright (C) 2012 Bartek Przybylski + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + package com.owncloud.android.files.services; import java.util.ArrayList; @@ -7,6 +25,8 @@ import com.owncloud.android.AccountUtils; import com.owncloud.android.datamodel.FileDataStorageManager; import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; import com.owncloud.android.files.OwnCloudFileObserver; +import com.owncloud.android.files.OwnCloudFileObserver.FileObserverStatusListener; +import com.owncloud.android.ui.activity.ConflictsResolveActivity; import android.accounts.Account; import android.accounts.AccountManager; @@ -20,7 +40,7 @@ import android.os.Binder; import android.os.IBinder; import android.util.Log; -public class FileObserverService extends Service { +public class FileObserverService extends Service implements FileObserverStatusListener { public final static String KEY_FILE_CMD = "KEY_FILE_CMD"; public final static String KEY_CMD_ARG = "KEY_CMD_ARG"; @@ -28,6 +48,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 +91,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,10 +131,11 @@ 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)))); + observer.addObserverStatusListener(this); observer.startWatching(); mObservers.add(observer); Log.d(TAG, "Started watching file " + path); @@ -143,6 +168,7 @@ public class FileObserverService extends Service { new FileDataStorageManager(account, getContentResolver()); observer.setStorageManager(storage); observer.setOCFile(storage.getFileByLocalPath(path)); + observer.addObserverStatusListener(this); DownloadCompletedReceiver receiver = new DownloadCompletedReceiver(path, observer); registerReceiver(receiver, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE)); @@ -167,6 +193,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) { @@ -179,7 +223,28 @@ public class FileObserverService extends Service { mDownloadReceivers.remove(r); } } - + + @Override + public void OnObservedFileStatusUpdate(String localPath, String remotePath, Account account, Status status) { + switch (status) { + case CONFLICT: + { + Intent i = new Intent(getApplicationContext(), ConflictsResolveActivity.class); + i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); + i.putExtra("remotepath", remotePath); + i.putExtra("localpath", localPath); + i.putExtra("account", account); + startActivity(i); + break; + } + case SENDING_TO_UPLOADER: + case INCORRECT_MASK: + break; + default: + Log.wtf(TAG, "Unhandled status " + status); + } + } + private class DownloadCompletedReceiver extends BroadcastReceiver { String mPath; OwnCloudFileObserver mObserver;