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
;
21 import java
.util
.LinkedList
;
22 import java
.util
.List
;
24 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.files
.OwnCloudFileObserver
.FileObserverStatusListener
.Status
;
27 import com
.owncloud
.android
.files
.services
.FileUploader
;
28 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
29 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
30 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
32 import eu
.alefzero
.webdav
.WebdavClient
;
34 import android
.accounts
.Account
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
37 import android
.os
.FileObserver
;
38 import android
.util
.Log
;
40 public class OwnCloudFileObserver
extends FileObserver
{
42 public static int CHANGES_ONLY
= CLOSE_WRITE
;
44 private static String TAG
= "OwnCloudFileObserver";
47 DataStorageManager mStorage
;
50 static Context mContext
; // ISSUE 4: why is this static?
51 List
<FileObserverStatusListener
> mListeners
;
53 public OwnCloudFileObserver(String path
) {
54 this(path
, ALL_EVENTS
);
57 public OwnCloudFileObserver(String path
, int mask
) {
61 mListeners
= new LinkedList
<FileObserverStatusListener
>();
64 public void setAccount(Account account
) {
68 public void setStorageManager(DataStorageManager manager
) {
72 public void setOCFile(OCFile file
) {
76 public void setContext(Context context
) {
80 public String
getPath() {
84 public String
getRemotePath() {
85 return mFile
.getRemotePath();
88 public void addObserverStatusListener(FileObserverStatusListener listener
) {
89 mListeners
.add(listener
);
93 public void onEvent(int event
, String path
) {
94 Log
.d(TAG
, "Got file modified with event " + event
+ " and path " + path
);
95 if ((event
& mMask
) == 0) {
96 Log
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + path
+
97 " with registered for " + mMask
+ " and original path " +
99 for (FileObserverStatusListener l
: mListeners
)
100 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.INCORRECT_MASK
);
103 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mOCAccount
, mContext
);
104 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(mFile
.getRemotePath(), mStorage
, mOCAccount
, mContext
);
105 RemoteOperationResult result
= sfo
.execute(wc
);
107 if (result
.getExtraData() == Boolean
.TRUE
) {
108 // inform user about conflict and let him decide what to do
109 for (FileObserverStatusListener l
: mListeners
)
110 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.CONFLICT
);
114 for (FileObserverStatusListener l
: mListeners
)
115 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.SENDING_TO_UPLOADER
);
117 Intent i
= new Intent(mContext
, FileUploader
.class);
118 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mOCAccount
);
119 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, mFile
.getRemotePath());
120 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, mPath
);
121 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
122 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
123 mContext
.startService(i
);
126 public interface FileObserverStatusListener
{
133 public void OnObservedFileStatusUpdate(String localPath
,
136 FileObserverStatusListener
.Status status
);