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