1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package com
.owncloud
.android
.files
;
22 import java
.util
.LinkedList
;
23 import java
.util
.List
;
25 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
28 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
29 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
31 import eu
.alefzero
.webdav
.WebdavClient
;
33 import android
.accounts
.Account
;
34 import android
.content
.Context
;
35 import android
.os
.FileObserver
;
36 import android
.util
.Log
;
38 public class OwnCloudFileObserver
extends FileObserver
{
40 public static int CHANGES_ONLY
= CLOSE_WRITE
;
42 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
45 private DataStorageManager mStorage
;
46 private Account mOCAccount
;
48 private Context mContext
;
49 private List
<FileObserverStatusListener
> mListeners
;
51 public OwnCloudFileObserver(String path
) {
52 this(path
, ALL_EVENTS
);
55 public OwnCloudFileObserver(String path
, int mask
) {
59 mListeners
= new LinkedList
<FileObserverStatusListener
>();
62 public void setAccount(Account account
) {
66 public void setStorageManager(DataStorageManager manager
) {
70 public void setOCFile(OCFile file
) {
74 public void setContext(Context context
) {
78 public String
getPath() {
82 public String
getRemotePath() {
83 return mFile
.getRemotePath();
86 public void addObserverStatusListener(FileObserverStatusListener listener
) {
87 mListeners
.add(listener
);
91 public void onEvent(int event
, String path
) {
92 Log
.d(TAG
, "Got file modified with event " + event
+ " and path " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: ""));
93 if ((event
& mMask
) == 0) {
94 Log
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: "") +
95 " with registered for " + mMask
+ " and original path " +
97 /* Unexpected event that will be ignored; no reason to propagate it
98 for (FileObserverStatusListener l : mListeners)
99 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.INCORRECT_MASK);
103 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mOCAccount
, mContext
);
104 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(getRemotePath(), mStorage
, mOCAccount
, true
, false
, mContext
);
105 RemoteOperationResult result
= sfo
.execute(wc
);
106 for (FileObserverStatusListener l
: mListeners
) {
107 l
.onObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, result
);
112 public interface FileObserverStatusListener
{
113 public void onObservedFileStatusUpdate(String localPath
,
116 RemoteOperationResult result
);
119 public OCFile
getOCFile() {
123 public Account
getAccount() {