1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 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 com
.owncloud
.android
.Log_OC
;
24 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
25 import com
.owncloud
.android
.datamodel
.OCFile
;
26 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
27 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
28 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
29 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
31 import android
.accounts
.Account
;
32 import android
.content
.Context
;
33 import android
.content
.Intent
;
34 import android
.os
.FileObserver
;
36 public class OwnCloudFileObserver
extends FileObserver
{
38 private static int MASK
= (FileObserver
.MODIFY
| FileObserver
.CLOSE_WRITE
);
40 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
44 private Account mOCAccount
;
45 private Context mContext
;
46 private boolean mModified
;
49 public OwnCloudFileObserver(String path
, Account account
, Context context
) {
52 throw new IllegalArgumentException("NULL path argument received");
54 throw new IllegalArgumentException("NULL account argument received");
56 throw new IllegalArgumentException("NULL context argument received");
65 public void onEvent(int event
, String path
) {
66 Log_OC
.d(TAG
, "Got file modified with event " + event
+ " and path " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: ""));
67 if ((event
& MASK
) == 0) {
68 Log_OC
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: "") +
69 " with registered for " + mMask
+ " and original path " +
72 if ((event
& FileObserver
.MODIFY
) != 0) {
76 // not sure if it's possible, but let's assume that both kind of events can be received at the same time
77 if ((event
& FileObserver
.CLOSE_WRITE
) != 0) {
88 private void startSyncOperation() {
89 FileDataStorageManager storageManager
= new FileDataStorageManager(mOCAccount
, mContext
.getContentResolver());
90 OCFile file
= storageManager
.getFileByLocalPath(mPath
); // a fresh object is needed; many things could have occurred to the file since it was registered to observe
91 // again, assuming that local files are linked to a remote file AT MOST, SOMETHING TO BE DONE;
92 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(file
,
98 RemoteOperationResult result
= sfo
.execute(mOCAccount
, mContext
);
99 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
100 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
101 Intent i
= new Intent(mContext
, ConflictsResolveActivity
.class);
102 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
103 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
104 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mOCAccount
);
105 mContext
.startActivity(i
);
107 // TODO save other errors in some point where the user can inspect them later;
108 // or maybe just toast them;
109 // or nothing, very strange fails