1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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
.datamodel
.FileDataStorageManager
;
24 import com
.owncloud
.android
.datamodel
.OCFile
;
25 import com
.owncloud
.android
.network
.OwnCloudClientUtils
;
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
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 import eu
.alefzero
.webdav
.WebdavClient
;
34 import android
.accounts
.Account
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
37 import android
.os
.FileObserver
;
38 import android
.util
.Log
;
40 public class OwnCloudFileObserver
extends FileObserver
{
42 public static int CHANGES_ONLY
= CLOSE_WRITE
;
44 private static String TAG
= OwnCloudFileObserver
.class.getSimpleName();
48 private Account mOCAccount
;
50 private Context mContext
;
53 public OwnCloudFileObserver(String path
, OCFile file
, Account account
, Context context
, int mask
) {
56 throw new IllegalArgumentException("NULL path argument received");
58 throw new IllegalArgumentException("NULL file argument received");
60 throw new IllegalArgumentException("NULL account argument received");
62 throw new IllegalArgumentException("NULL context argument received");
63 if (!path
.equals(file
.getStoragePath()) && !path
.equals(FileStorageUtils
.getDefaultSavePathFor(account
.name
, file
)))
64 throw new IllegalArgumentException("File argument is not linked to the local file set in path argument");
73 public void onEvent(int event
, String path
) {
74 Log
.d(TAG
, "Got file modified with event " + event
+ " and path " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: ""));
75 if ((event
& mMask
) == 0) {
76 Log
.wtf(TAG
, "Incorrect event " + event
+ " sent for file " + mPath
+ ((path
!= null
) ? File
.separator
+ path
: "") +
77 " with registered for " + mMask
+ " and original path " +
81 WebdavClient wc
= OwnCloudClientUtils
.createOwnCloudClient(mOCAccount
, mContext
);
82 SynchronizeFileOperation sfo
= new SynchronizeFileOperation(mFile
,
84 new FileDataStorageManager(mOCAccount
, mContext
.getContentResolver()),
89 RemoteOperationResult result
= sfo
.execute(wc
);
90 if (result
.getCode() == ResultCode
.SYNC_CONFLICT
) {
91 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
92 Intent i
= new Intent(mContext
, ConflictsResolveActivity
.class);
93 i
.setFlags(i
.getFlags() | Intent
.FLAG_ACTIVITY_NEW_TASK
);
94 i
.putExtra("remotepath", mFile
.getRemotePath());
95 i
.putExtra("localpath", mPath
);
96 i
.putExtra("account", mOCAccount
);
97 mContext
.startActivity(i
);
99 // TODO save other errors in some point where the user can inspect them later;
100 // or maybe just toast them;
101 // or nothing, very strange fails