Watch local changes of kept-in-sync files by observing their parent folder, instead...
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFileObserver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2,
7 * as published by the Free Software Foundation.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.files;
20
21 import java.io.File;
22
23 import android.accounts.Account;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.os.FileObserver;
27 import android.os.Handler;
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 public class OwnCloudFileObserver extends FileObserver {
38
39 //private static int MASK = (FileObserver.MODIFY | FileObserver.CLOSE_WRITE);
40 public static int IN_IGNORE = 32768;
41 public static int ALL_EVENTS_AND_MORE = 0xffffffff;
42
43 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
44
45 private String mPath;
46 //private int mMask;
47 private Account mOCAccount;
48 private Context mContext;
49 private boolean mModified;
50 private long mFileLastModified;
51 //private boolean mRestartWatching;
52 //private Handler mHandler;
53
54 public OwnCloudFileObserver(String path, Account account, Context context, Handler handler) {
55 super(path, ALL_EVENTS);
56 if (path == null)
57 throw new IllegalArgumentException("NULL path argument received");
58 if (account == null)
59 throw new IllegalArgumentException("NULL account argument received");
60 if (context == null)
61 throw new IllegalArgumentException("NULL context argument received");
62 mPath = path;
63 mOCAccount = account;
64 mContext = context;
65 mModified = false;
66 mFileLastModified = new File(path).lastModified();
67 //mHandler = handler;
68 Log_OC.d(TAG, "Create Observer - FileLastModified: " + mFileLastModified);
69 }
70
71 @Override
72 public void onEvent(int event, String path) {
73 Log_OC.v(TAG, "Got event " + event + " on FILE " + mPath + " about "
74 + ((path != null) ? path : ""));
75
76 /*
77 if ((event & MASK) == 0) {
78 Log_OC.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath
79 + ((path != null) ? File.separator + path : "") + " with registered for " + mMask
80 + " and original path " + mPath);
81
82 // in case need start watching again
83 if ((event & IN_IGNORE) != 0 && mRestartWatching) {
84 mRestartWatching = false;
85
86 mHandler.postDelayed(new Runnable() {
87 public void run() {
88 startWatching();
89 }
90 }, 5000);
91
92 }
93 } else {
94 */
95 if ((event & FileObserver.MODIFY) != 0) {
96 // file changed
97 mModified = true;
98 }
99 // not sure if it's possible, but let's assume that both kind of
100 // events can be received at the same time
101 if ((event & FileObserver.CLOSE_WRITE) != 0) {
102 // file closed
103 if (mModified) {
104 mModified = false;
105 //mRestartWatching = false;
106 startSyncOperation();
107
108 } else if (isFileUpdated()) {
109 // if file has been modified but Modify event type has not
110 // been launched
111 //mRestartWatching = true;
112 mFileLastModified = new File(mPath).lastModified();
113 Log_OC.d(TAG, "CLOSE_WRITE - New FileLastModified: " + mFileLastModified);
114 startSyncOperation();
115 }
116 }
117 //}
118 }
119
120 private void startSyncOperation() {
121 FileDataStorageManager storageManager = new FileDataStorageManager(mOCAccount, mContext.getContentResolver());
122 // a fresh object is needed; many things could have occurred to the file
123 // since it was registered to observe again, assuming that local files
124 // are linked to a remote file AT MOST, SOMETHING TO BE DONE;
125 OCFile file = storageManager.getFileByLocalPath(mPath);
126 SynchronizeFileOperation sfo = new SynchronizeFileOperation(file, null, mOCAccount, true, mContext);
127 RemoteOperationResult result = sfo.execute(storageManager, mContext);
128 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
129 // ISSUE 5: if the user is not running the app (this is a service!),
130 // this can be very intrusive; a notification should be preferred
131 Intent i = new Intent(mContext, ConflictsResolveActivity.class);
132 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
133 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
134 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mOCAccount);
135 mContext.startActivity(i);
136 }
137 // TODO save other errors in some point where the user can inspect them
138 // later;
139 // or maybe just toast them;
140 // or nothing, very strange fails
141 }
142
143 /**
144 * Check if the timestamp of last file modification in local is more current
145 * that the timestamp when setting observer to the file
146 *
147 * @return boolean: True if file is updated, False if not
148 */
149 private boolean isFileUpdated() {
150 Log_OC.d(TAG, "FileLastModified: " + mFileLastModified);
151 return (new File(mPath).lastModified() > mFileLastModified);
152 }
153 }