8a03fee660e7651c5fb5643e43d42b9a391dc861
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFileObserver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
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.
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 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
31 import eu.alefzero.webdav.WebdavClient;
32
33 import android.accounts.Account;
34 import android.content.Context;
35 import android.content.Intent;
36 import android.os.FileObserver;
37 import android.util.Log;
38
39 public class OwnCloudFileObserver extends FileObserver {
40
41 public static int CHANGES_ONLY = CLOSE_WRITE;
42
43 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
44
45 private String mPath;
46 private int mMask;
47 private Account mOCAccount;
48 //private OCFile mFile;
49 private Context mContext;
50
51
52 public OwnCloudFileObserver(String path, Account account, Context context, int mask) {
53 super(path, mask);
54 if (path == null)
55 throw new IllegalArgumentException("NULL path argument received");
56 /*if (file == null)
57 throw new IllegalArgumentException("NULL file 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 /*if (!path.equals(file.getStoragePath()) && !path.equals(FileStorageUtils.getDefaultSavePathFor(account.name, file)))
63 throw new IllegalArgumentException("File argument is not linked to the local file set in path argument"); */
64 mPath = path;
65 //mFile = file;
66 mOCAccount = account;
67 mContext = context;
68 mMask = mask;
69 }
70
71 @Override
72 public void onEvent(int event, String path) {
73 Log.d(TAG, "Got file modified with event " + event + " and path " + mPath + ((path != null) ? File.separator + path : ""));
74 if ((event & mMask) == 0) {
75 Log.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath + ((path != null) ? File.separator + path : "") +
76 " with registered for " + mMask + " and original path " +
77 mPath);
78 return;
79 }
80 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mOCAccount, mContext);
81 FileDataStorageManager storageManager = new FileDataStorageManager(mOCAccount, mContext.getContentResolver());
82 OCFile file = storageManager.getFileByLocalPath(mPath); // a fresh object is needed; many things could have occurred to the file since it was registered to observe
83 // again, assuming that local files are linked to a remote file AT MOST, SOMETHING TO BE DONE;
84 SynchronizeFileOperation sfo = new SynchronizeFileOperation(file,
85 null,
86 storageManager,
87 mOCAccount,
88 true,
89 true,
90 mContext);
91 RemoteOperationResult result = sfo.execute(wc);
92 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
93 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
94 Intent i = new Intent(mContext, ConflictsResolveActivity.class);
95 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
96 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
97 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mOCAccount);
98 mContext.startActivity(i);
99 }
100 // TODO save other errors in some point where the user can inspect them later;
101 // or maybe just toast them;
102 // or nothing, very strange fails
103 }
104
105 }