db84448c0dc79d807e3960671ed3ce4a209f9d01
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFolderObserver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2014 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package com.owncloud.android.files;
19
20 import java.io.File;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import android.accounts.Account;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.FileObserver;
28
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;
36
37 /**
38 * Observer watching a folder to request the synchronization of kept-in-sync files
39 * inside it.
40 *
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
44 * temporal.
45 *
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
48 * be replaced later.
49 *
50 * @author David A. Velasco
51 */
52 public class OwnCloudFolderObserver extends FileObserver {
53
54 private static int UPDATE_MASK = (
55 FileObserver.ATTRIB | FileObserver.MODIFY |
56 FileObserver.MOVED_TO | FileObserver.CLOSE_WRITE
57 );
58 /*
59 private static int ALL_EVENTS_EVEN_THOSE_NOT_DOCUMENTED = 0x7fffffff; // NEVER use 0xffffffff
60 */
61
62 private static String TAG = OwnCloudFolderObserver.class.getSimpleName();
63
64 private String mPath;
65 private Account mAccount;
66 private Context mContext;
67 private Map<String, Boolean> mObservedChildren;
68
69 public OwnCloudFolderObserver(String path, Account account, Context context) {
70 super(path, UPDATE_MASK);
71
72 if (path == null)
73 throw new IllegalArgumentException("NULL path argument received");
74 if (account == null)
75 throw new IllegalArgumentException("NULL account argument received");
76 if (context == null)
77 throw new IllegalArgumentException("NULL context argument received");
78
79 mPath = path;
80 mAccount = account;
81 mContext = context;
82 mObservedChildren = new HashMap<String, Boolean>();
83 }
84
85
86 @Override
87 public void onEvent(int event, String path) {
88 Log_OC.d(TAG, "Got event " + event + " on FOLDER " + mPath + " about "
89 + ((path != null) ? path : ""));
90
91 boolean shouldSynchronize = false;
92 synchronized(mObservedChildren) {
93 if (path != null && path.length() > 0 && mObservedChildren.containsKey(path)) {
94
95 if ((event & FileObserver.MODIFY) != 0) {
96 if (!mObservedChildren.get(path)) {
97 mObservedChildren.put(path, Boolean.valueOf(true));
98 }
99 }
100
101 /*
102 if ((event & FileObserver.ATTRIB) != 0) {
103 if (mObservedChildren.get(path) != true) {
104 mObservedChildren.put(path, Boolean.valueOf(true));
105 }
106 }
107 */
108
109 /*
110 if ((event & FileObserver.MOVED_TO) != 0) {
111 if (mObservedChildren.get(path) != true) {
112 mObservedChildren.put(path, Boolean.valueOf(true));
113 }
114 }
115 */
116
117 if ((event & FileObserver.CLOSE_WRITE) != 0) {
118 mObservedChildren.put(path, Boolean.valueOf(false));
119 shouldSynchronize = true;
120 }
121 }
122 }
123 if (shouldSynchronize) {
124 startSyncOperation(path);
125 }
126
127 if ((event & OwnCloudFileObserver.IN_IGNORE) != 0 &&
128 (path == null || path.length() == 0)) {
129 Log_OC.d(TAG, "Stopping the observance on " + mPath);
130 }
131
132 }
133
134
135 public void startWatching(String localPath) {
136 synchronized (mObservedChildren) {
137 if (!mObservedChildren.containsKey(localPath)) {
138 mObservedChildren.put(localPath, Boolean.valueOf(false));
139 }
140 }
141
142 if (new File(mPath).exists()) {
143 startWatching();
144 Log_OC.d(TAG, "Started watching parent folder " + mPath + "/");
145 }
146 // else - the observance can't be started on a file not existing;
147 }
148
149 public void stopWatching(String localPath) {
150 synchronized (mObservedChildren) {
151 mObservedChildren.remove(localPath);
152 if (mObservedChildren.isEmpty()) {
153 stopWatching();
154 Log_OC.d(TAG, "Stopped watching parent folder " + mPath + "/");
155 }
156 }
157 }
158
159 public boolean isEmpty() {
160 synchronized (mObservedChildren) {
161 return mObservedChildren.isEmpty();
162 }
163 }
164
165
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);
184 }
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
188 }
189
190 }