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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.files
;
24 import com
.owncloud
.android
.Log_OC
;
25 import com
.owncloud
.android
.datamodel
.FileDataStorageManager
;
26 import com
.owncloud
.android
.datamodel
.OCFile
;
27 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
28 import com
.owncloud
.android
.operations
.RemoteOperationResult
;
29 import com
.owncloud
.android
.operations
.SynchronizeFileOperation
;
30 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
31 import com
.owncloud
.android
.ui
.activity
.ConflictsResolveActivity
;
33 import eu
.alefzero
.webdav
.WebdavClient
;
35 import android
.accounts
.Account
;
36 import android
.content
.Context
;
37 import android
.content
.Intent
;
38 import android
.os
.FileObserver
;
39 import android
.util
.Log
;
41 public class OwnCloudFileObserver
extends FileObserver
{
43 public static int CHANGES_ONLY
= CLOSE_WRITE
;
45 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
49 private Account mOCAccount
;
50 //private OCFile mFile;
51 private Context mContext
;
54 public OwnCloudFileObserver(String path
, Account account
, Context context
, int mask
) {
57 throw new IllegalArgumentException("NULL path argument received");
59 throw new IllegalArgumentException("NULL file argument received");*/
61 throw new IllegalArgumentException("NULL account argument received");
63 throw new IllegalArgumentException("NULL context argument received");
64 /*if (!path.equals(file.getStoragePath()) && !path.equals(FileStorageUtils.getDefaultSavePathFor(account.name, file)))
65 throw new IllegalArgumentException("File argument is not linked to the local file set in path argument"); */
74 public void onEvent(int event
, String path
) {
75 Log_OC
.d(TAG
, "Got file modified with event " + event
+ " and path " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: ""));
76 if ((event
& mMask
) == 0) {
77 Log_OC
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: "") +
78 " with registered for " + mMask
+ " and original path " +
82 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mOCAccount
, mContext
);
83 FileDataStorageManager storageManager
= new FileDataStorageManager(mOCAccount
, mContext
.getContentResolver());
84 OCFile file
= storageManager
.getFileByLocalPath(mPath
); // a fresh object is needed; many things could have occurred to the file since it was registered to observe
85 // again, assuming that local files are linked to a remote file AT MOST, SOMETHING TO BE DONE;
86 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(file
,
93 RemoteOperationResult result
= sfo
.execute(wc
);
94 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
95 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
96 Intent i
= new Intent(mContext
, ConflictsResolveActivity
.class);
97 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
98 i
.putExtra(ConflictsResolveActivity
.EXTRA_FILE
, file
);
99 i
.putExtra(ConflictsResolveActivity
.EXTRA_ACCOUNT
, mOCAccount
);
100 mContext
.startActivity(i
);
102 // TODO save other errors in some point where the user can inspect them later;
103 // or maybe just toast them;
104 // or nothing, very strange fails