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
.files
.OwnCloudFileObserver
.FileObserverStatusListener
.Status
;
28 import com
.owncloud
.android
.files
.services
.FileUploader
;
29 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
30 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
31 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
33 import eu
.alefzero
.webdav
.WebdavClient
;
35 import android
.accounts
.Account
;
36 import android
.content
.Context
;
37 import android
.content
.Intent
;
38 import android
.os
.FileObserver
;
39 import android
.util
.Log
;
41 public class OwnCloudFileObserver
extends FileObserver
{
43 public static int CHANGES_ONLY
= CLOSE_WRITE
;
45 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
48 private DataStorageManager mStorage
;
49 private Account mOCAccount
;
51 private Context mContext
;
52 private List
<FileObserverStatusListener
> mListeners
;
54 public OwnCloudFileObserver(String path
) {
55 this(path
, ALL_EVENTS
);
58 public OwnCloudFileObserver(String path
, int mask
) {
62 mListeners
= new LinkedList
<FileObserverStatusListener
>();
65 public void setAccount(Account account
) {
69 public void setStorageManager(DataStorageManager manager
) {
73 public void setOCFile(OCFile file
) {
77 public void setContext(Context context
) {
81 public String
getPath() {
85 public String
getRemotePath() {
86 return mFile
.getRemotePath();
89 public void addObserverStatusListener(FileObserverStatusListener listener
) {
90 mListeners
.add(listener
);
94 public void onEvent(int event
, String path
) {
95 Log
.d(TAG
, "Got file modified with event " + event
+ " and path " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: ""));
96 if ((event
& mMask
) == 0) {
97 Log
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: "") +
98 " with registered for " + mMask
+ " and original path " +
100 for (FileObserverStatusListener l
: mListeners
)
101 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.INCORRECT_MASK
);
104 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mOCAccount
, mContext
);
105 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(mFile
.getRemotePath(), mStorage
, mOCAccount
);
106 RemoteOperationResult result
= sfo
.execute(wc
);
108 if (result
.getExtraData() == Boolean
.TRUE
) {
109 // inform user about conflict and let him decide what to do
110 for (FileObserverStatusListener l
: mListeners
)
111 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.CONFLICT
);
115 for (FileObserverStatusListener l
: mListeners
)
116 l
.OnObservedFileStatusUpdate(mPath
, getRemotePath(), mOCAccount
, Status
.SENDING_TO_UPLOADER
);
118 Intent i
= new Intent(mContext
, FileUploader
.class);
119 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mOCAccount
);
120 i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, mFile
.getRemotePath());
121 i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, mPath
);
122 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
123 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
124 mContext
.startService(i
);
127 public interface FileObserverStatusListener
{
134 public void OnObservedFileStatusUpdate(String localPath
,
137 FileObserverStatusListener
.Status status
);