1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com
.owncloud
.android
.files
;
21 import java
.util
.HashMap
;
24 import android
.accounts
.Account
;
25 import android
.content
.Context
;
26 import android
.content
.Intent
;
27 import android
.os
.FileObserver
;
29 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
32 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
33 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
34 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
35 import com
.owncloud
.android
.utils
.Log_OC
;
38 * Observer watching a folder to request the synchronization of kept-in-sync files
41 * Takes into account two possible update cases:
42 * - an editor directly updates the file;
43 * - an editor works on a temporal file, and later replaces the kept-in-sync file with the
46 * The second case requires to monitor the folder parent of the files, since a direct
47 * {@link FileObserver} on it will not receive more events after the file is deleted to
50 * @author David A. Velasco
52 public class OwnCloudFolderObserver
extends FileObserver
{
54 private static int UPDATE_MASK
= (
55 FileObserver
.ATTRIB
| FileObserver
.MODIFY
|
56 FileObserver
.MOVED_TO
| FileObserver
.CLOSE_WRITE
59 private static int ALL_EVENTS_EVEN_THOSE_NOT_DOCUMENTED = 0x7fffffff; // NEVER use 0xffffffff
62 private static String TAG
= OwnCloudFolderObserver
.class.getSimpleName();
65 private Account mAccount
;
66 private Context mContext
;
67 private Map
<String
, Boolean
> mObservedChildren
;
69 public OwnCloudFolderObserver(String path
, Account account
, Context context
) {
70 super(path
, UPDATE_MASK
);
73 throw new IllegalArgumentException("NULL path argument received");
75 throw new IllegalArgumentException("NULL account argument received");
77 throw new IllegalArgumentException("NULL context argument received");
82 mObservedChildren
= new HashMap
<String
, Boolean
>();
87 public void onEvent(int event
, String path
) {
88 Log_OC
.d(TAG
, "Got event " + event
+ " on FOLDER " + mPath
+ " about "
89 + ((path
!= null
) ? path
: ""));
91 boolean shouldSynchronize
= false
;
92 synchronized(mObservedChildren
) {
93 if (path
!= null
&& path
.length() > 0 && mObservedChildren
.containsKey(path
)) {
95 if ((event
& FileObserver
.MODIFY
) != 0) {
96 if (!mObservedChildren
.get(path
)) {
97 mObservedChildren
.put(path
, Boolean
.valueOf(true
));
102 if ((event & FileObserver.ATTRIB) != 0) {
103 if (mObservedChildren.get(path) != true) {
104 mObservedChildren.put(path, Boolean.valueOf(true));
110 if ((event & FileObserver.MOVED_TO) != 0) {
111 if (mObservedChildren.get(path) != true) {
112 mObservedChildren.put(path, Boolean.valueOf(true));
117 if ((event
& FileObserver
.CLOSE_WRITE
) != 0) {
118 mObservedChildren
.put(path
, Boolean
.valueOf(false
));
119 shouldSynchronize
= true
;
123 if (shouldSynchronize
) {
124 startSyncOperation(path
);
127 if ((event
& OwnCloudFileObserver
.IN_IGNORE
) != 0 &&
128 (path
== null
|| path
.length() == 0)) {
129 Log_OC
.d(TAG
, "Stopping the observance on " + mPath
);
135 public void startWatching(String localPath
) {
136 synchronized (mObservedChildren
) {
137 if (!mObservedChildren
.containsKey(localPath
)) {
138 mObservedChildren
.put(localPath
, Boolean
.valueOf(false
));
142 if (new File(mPath
).exists()) {
144 Log_OC
.d(TAG
, "Started watching parent folder " + mPath
+ "/");
146 // else - the observance can't be started on a file not existing;
149 public void stopWatching(String localPath
) {
150 synchronized (mObservedChildren
) {
151 mObservedChildren
.remove(localPath
);
152 if (mObservedChildren
.isEmpty()) {
154 Log_OC
.d(TAG
, "Stopped watching parent folder " + mPath
+ "/");
159 public boolean isEmpty() {
160 synchronized (mObservedChildren
) {
161 return mObservedChildren
.isEmpty();
166 private void startSyncOperation(String childName
) {
167 FileDataStorageManager storageManager
=
168 new FileDataStorageManager(mAccount
, mContext
.getContentResolver());
169 // a fresh object is needed; many things could have occurred to the file
170 // since it was registered to observe again, assuming that local files
171 // are linked to a remote file AT MOST, SOMETHING TO BE DONE;
172 OCFile file
= storageManager
.getFileByLocalPath(mPath
+ File
.separator
+ childName
);
173 SynchronizeFileOperation sfo
=
174 new SynchronizeFileOperation(file
, null
, mAccount
, true
, mContext
);
175 RemoteOperationResult result
= sfo
.execute(storageManager
, mContext
);
176 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
177 // ISSUE 5: if the user is not running the app (this is a service!),
178 // this can be very intrusive; a notification should be preferred
179 Intent i
= new Intent(mContext
, ConflictsResolveActivity
.class);
180 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
181 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
182 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mAccount
);
183 mContext
.startActivity(i
);
185 // TODO save other errors in some point where the user can inspect them later;
186 // or maybe just toast them;
187 // or nothing, very strange fails