Watch for replacements of favorite file besides than direct notifications
[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 ((event & FileObserver.ATTRIB) != 0) ||
97 ((event & FileObserver.MOVED_TO) != 0) ) {
98
99 if (mObservedChildren.get(path) != true) {
100 mObservedChildren.put(path, Boolean.valueOf(true));
101 }
102 }
103
104 if ((event & FileObserver.CLOSE_WRITE) != 0) {
105 mObservedChildren.put(path, Boolean.valueOf(false));
106 shouldSynchronize = true;
107 }
108 }
109 }
110 if (shouldSynchronize) {
111 startSyncOperation(path);
112 }
113
114 if ((event & OwnCloudFileObserver.IN_IGNORE) != 0 &&
115 (path == null || path.length() == 0)) {
116 Log_OC.d(TAG, "Stopping the observance on " + mPath);
117 }
118
119 }
120
121
122 public void startWatching(String localPath) {
123 synchronized (mObservedChildren) {
124 if (!mObservedChildren.containsKey(localPath)) {
125 mObservedChildren.put(localPath, Boolean.valueOf(false));
126 }
127 }
128
129 if (new File(mPath).exists()) {
130 startWatching();
131 Log_OC.d(TAG, "Started watching parent folder " + mPath + "/");
132 }
133 // else - the observance can't be started on a file not existing;
134 }
135
136 public void stopWatching(String localPath) {
137 synchronized (mObservedChildren) {
138 mObservedChildren.remove(localPath);
139 if (mObservedChildren.isEmpty()) {
140 stopWatching();
141 Log_OC.d(TAG, "Stopped watching parent folder " + mPath + "/");
142 }
143 }
144 }
145
146 public boolean isEmpty() {
147 synchronized (mObservedChildren) {
148 return mObservedChildren.isEmpty();
149 }
150 }
151
152
153 private void startSyncOperation(String childName) {
154 FileDataStorageManager storageManager =
155 new FileDataStorageManager(mAccount, mContext.getContentResolver());
156 // a fresh object is needed; many things could have occurred to the file
157 // since it was registered to observe again, assuming that local files
158 // are linked to a remote file AT MOST, SOMETHING TO BE DONE;
159 OCFile file = storageManager.getFileByLocalPath(mPath + File.separator + childName);
160 SynchronizeFileOperation sfo =
161 new SynchronizeFileOperation(file, null, mAccount, true, mContext);
162 RemoteOperationResult result = sfo.execute(storageManager, mContext);
163 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
164 // ISSUE 5: if the user is not running the app (this is a service!),
165 // this can be very intrusive; a notification should be preferred
166 Intent i = new Intent(mContext, ConflictsResolveActivity.class);
167 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
168 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
169 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);
170 mContext.startActivity(i);
171 }
172 // TODO save other errors in some point where the user can inspect them later;
173 // or maybe just toast them;
174 // or nothing, very strange fails
175 }
176
177 }