X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/fc5608de767a9c45073b870e2e2860a012800f99..a1f52fe73b3bdc9e365533a8ebc0b205fa5d7c5c:/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 8a63a6ee..a90e551e 100644 --- a/src/com/owncloud/android/files/services/FileObserverService.java +++ b/src/com/owncloud/android/files/services/FileObserverService.java @@ -22,14 +22,6 @@ import java.io.File; import java.util.HashMap; import java.util.Map; -import com.owncloud.android.Log_OC; -import com.owncloud.android.datamodel.FileDataStorageManager; -import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; -import com.owncloud.android.files.OwnCloudFileObserver; -import com.owncloud.android.operations.SynchronizeFileOperation; -import com.owncloud.android.utils.FileStorageUtils; - import android.accounts.Account; import android.accounts.AccountManager; import android.app.Service; @@ -39,8 +31,18 @@ import android.content.Intent; import android.content.IntentFilter; import android.database.Cursor; import android.os.Binder; +import android.os.Handler; import android.os.IBinder; +import com.owncloud.android.MainApp; +import com.owncloud.android.datamodel.FileDataStorageManager; +import com.owncloud.android.datamodel.OCFile; +import com.owncloud.android.db.ProviderMeta.ProviderTableMeta; +import com.owncloud.android.files.OwnCloudFileObserver; +import com.owncloud.android.operations.SynchronizeFileOperation; +import com.owncloud.android.utils.FileStorageUtils; +import com.owncloud.android.utils.Log_OC; + public class FileObserverService extends Service { public final static int CMD_INIT_OBSERVED_LIST = 1; @@ -56,10 +58,8 @@ public class FileObserverService extends Service { private static Map mObserversMap; private static DownloadCompletedReceiverBis mDownloadReceiver; private IBinder mBinder = new LocalBinder(); + private Handler mHandler = new Handler(); - private String mDownloadAddedMessage; - private String mDownloadFinishMessage; - public class LocalBinder extends Binder { FileObserverService getService() { return FileObserverService.this; @@ -71,13 +71,9 @@ public class FileObserverService extends Service { super.onCreate(); mDownloadReceiver = new DownloadCompletedReceiverBis(); - FileDownloader downloader = new FileDownloader(); - mDownloadAddedMessage = downloader.getDownloadAddedMessage(); - mDownloadFinishMessage= downloader.getDownloadFinishMessage(); - IntentFilter filter = new IntentFilter(); - filter.addAction(mDownloadAddedMessage); - filter.addAction(mDownloadFinishMessage); + filter.addAction(FileDownloader.getDownloadAddedMessage()); + filter.addAction(FileDownloader.getDownloadFinishMessage()); registerReceiver(mDownloadReceiver, filter); mObserversMap = new HashMap(); @@ -87,10 +83,10 @@ public class FileObserverService extends Service { @Override public void onDestroy() { - super.onDestroy(); unregisterReceiver(mDownloadReceiver); mObserversMap = null; // TODO study carefully the life cycle of Services to grant the best possible observance Log_OC.d(TAG, "Bye, bye"); + super.onDestroy(); } @@ -147,7 +143,7 @@ public class FileObserverService extends Service { null); if (c == null || !c.moveToFirst()) return; AccountManager acm = AccountManager.get(this); - Account[] accounts = acm.getAccounts(); + Account[] accounts = acm.getAccountsByType(MainApp.getAccountType()); do { Account account = null; for (Account a : accounts) @@ -165,11 +161,7 @@ public class FileObserverService extends Service { String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)); if (path == null || path.length() <= 0) continue; - OwnCloudFileObserver observer = - new OwnCloudFileObserver( path, - account, - getApplicationContext(), - OwnCloudFileObserver.CHANGES_ONLY); + OwnCloudFileObserver observer = new OwnCloudFileObserver(path, account, getApplicationContext(), mHandler); mObserversMap.put(path, observer); if (new File(path).exists()) { observer.startWatching(); @@ -208,8 +200,8 @@ public class FileObserverService extends Service { /// the local file was never registered to observe before observer = new OwnCloudFileObserver( localPath, account, - getApplicationContext(), - OwnCloudFileObserver.CHANGES_ONLY); + getApplicationContext(), + mHandler); mObserversMap.put(localPath, observer); Log_OC.d(TAG, "Observer added for path " + localPath); @@ -267,12 +259,12 @@ public class FileObserverService extends Service { String downloadPath = intent.getStringExtra(FileDownloader.EXTRA_FILE_PATH); OwnCloudFileObserver observer = mObserversMap.get(downloadPath); if (observer != null) { - if (intent.getAction().equals(mDownloadFinishMessage) && + if (intent.getAction().equals(FileDownloader.getDownloadFinishMessage()) && new File(downloadPath).exists()) { // the download could be successful. not; in both cases, the file could be down, due to a former download or upload observer.startWatching(); Log_OC.d(TAG, "Watching again " + downloadPath); - } else if (intent.getAction().equals(mDownloadAddedMessage)) { + } else if (intent.getAction().equals(FileDownloader.getDownloadAddedMessage())) { observer.stopWatching(); Log_OC.d(TAG, "Disabling observance of " + downloadPath); }