Fixed typos from 'shareWhatever' to 'sharedWhatever', and updated reference to library
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFileObserver.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 ownCloud Inc.
4 *
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.
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.lib.common.operations.RemoteOperationResult;
26 import com.owncloud.android.operations.SynchronizeFileOperation;
27 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
28 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
29 import com.owncloud.android.utils.Log_OC;
30
31 import android.accounts.Account;
32 import android.content.Context;
33 import android.content.Intent;
34 import android.os.FileObserver;
35
36 public class OwnCloudFileObserver extends FileObserver {
37
38 private static int MASK = (FileObserver.MODIFY | FileObserver.CLOSE_WRITE);
39
40 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
41
42 private String mPath;
43 private int mMask;
44 private Account mOCAccount;
45 private Context mContext;
46 private boolean mModified;
47
48
49 public OwnCloudFileObserver(String path, Account account, Context context) {
50 super(path, MASK);
51 if (path == null)
52 throw new IllegalArgumentException("NULL path argument received");
53 if (account == null)
54 throw new IllegalArgumentException("NULL account argument received");
55 if (context == null)
56 throw new IllegalArgumentException("NULL context argument received");
57 mPath = path;
58 mOCAccount = account;
59 mContext = context;
60 mModified = false;
61 }
62
63
64 @Override
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 " +
70 mPath);
71 } else {
72 if ((event & FileObserver.MODIFY) != 0) {
73 // file changed
74 mModified = true;
75 }
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) {
78 // file closed
79 if (mModified) {
80 mModified = false;
81 startSyncOperation();
82 }
83 }
84 }
85 }
86
87
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,
93 null,
94 mOCAccount,
95 true,
96 mContext);
97 RemoteOperationResult result = sfo.execute(storageManager, mContext);
98 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
99 // ISSUE 5: if the user is not running the app (this is a service!), this can be very intrusive; a notification should be preferred
100 Intent i = new Intent(mContext, ConflictsResolveActivity.class);
101 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
102 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
103 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mOCAccount);
104 mContext.startActivity(i);
105 }
106 // TODO save other errors in some point where the user can inspect them later;
107 // or maybe just toast them;
108 // or nothing, very strange fails
109 }
110
111 }