1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
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.
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
;
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
;
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
;
37 public class OwnCloudFileObserver
extends FileObserver
{
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;
43 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
47 private Account mOCAccount
;
48 private Context mContext
;
49 private boolean mModified
;
50 private long mFileLastModified
;
51 //private boolean mRestartWatching;
52 //private Handler mHandler;
54 public OwnCloudFileObserver(String path
, Account account
, Context context
, Handler handler
) {
55 super(path
, ALL_EVENTS
);
57 throw new IllegalArgumentException("NULL path argument received");
59 throw new IllegalArgumentException("NULL account argument received");
61 throw new IllegalArgumentException("NULL context argument received");
66 mFileLastModified
= new File(path
).lastModified();
68 Log_OC
.d(TAG
, "Create Observer - FileLastModified: " + mFileLastModified
);
72 public void onEvent(int event
, String path
) {
73 Log_OC
.v(TAG
, "Got event " + event
+ " on FILE " + mPath
+ " about "
74 + ((path
!= null
) ? path
: ""));
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);
82 // in case need start watching again
83 if ((event & IN_IGNORE) != 0 && mRestartWatching) {
84 mRestartWatching = false;
86 mHandler.postDelayed(new Runnable() {
95 if ((event
& FileObserver
.MODIFY
) != 0) {
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) {
105 //mRestartWatching = false;
106 startSyncOperation();
108 } else if (isFileUpdated()) {
109 // if file has been modified but Modify event type has not
111 //mRestartWatching = true;
112 mFileLastModified
= new File(mPath
).lastModified();
113 Log_OC
.d(TAG
, "CLOSE_WRITE - New FileLastModified: " + mFileLastModified
);
114 startSyncOperation();
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
);
137 // TODO save other errors in some point where the user can inspect them
139 // or maybe just toast them;
140 // or nothing, very strange fails
144 * Check if the timestamp of last file modification in local is more current
145 * that the timestamp when setting observer to the file
147 * @return boolean: True if file is updated, False if not
149 private boolean isFileUpdated() {
150 Log_OC
.d(TAG
, "FileLastModified: " + mFileLastModified
);
151 return (new File(mPath
).lastModified() > mFileLastModified
);