Merge tag 'oc-android-1-3-22' into oauth_login
[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 as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
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.
14 *
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/>.
17 *
18 */
19
20 package com.owncloud.android.files;
21
22 import java.io.File;
23
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 import android.util.Log;
37
38 public class OwnCloudFileObserver extends FileObserver {
39
40 public static int CHANGES_ONLY = CLOSE_WRITE;
41
42 private static String TAG = OwnCloudFileObserver.class.getSimpleName();
43
44 private String mPath;
45 private int mMask;
46 private Account mOCAccount;
47 //private OCFile mFile;
48 private Context mContext;
49
50
51 public OwnCloudFileObserver(String path, Account account, Context context, int mask) {
52 super(path, mask);
53 if (path == null)
54 throw new IllegalArgumentException("NULL path argument received");
55 /*if (file == null)
56 throw new IllegalArgumentException("NULL file argument received");*/
57 if (account == null)
58 throw new IllegalArgumentException("NULL account argument received");
59 if (context == null)
60 throw new IllegalArgumentException("NULL context argument received");
61 /*if (!path.equals(file.getStoragePath()) && !path.equals(FileStorageUtils.getDefaultSavePathFor(account.name, file)))
62 throw new IllegalArgumentException("File argument is not linked to the local file set in path argument"); */
63 mPath = path;
64 //mFile = file;
65 mOCAccount = account;
66 mContext = context;
67 mMask = mask;
68 }
69
70 @Override
71 public void onEvent(int event, String path) {
72 Log.d(TAG, "Got file modified with event " + event + " and path " + mPath + ((path != null) ? File.separator + path : ""));
73 if ((event & mMask) == 0) {
74 Log.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath + ((path != null) ? File.separator + path : "") +
75 " with registered for " + mMask + " and original path " +
76 mPath);
77 return;
78 }
79 FileDataStorageManager storageManager = new FileDataStorageManager(mOCAccount, mContext.getContentResolver());
80 OCFile file = storageManager.getFileByLocalPath(mPath); // a fresh object is needed; many things could have occurred to the file since it was registered to observe
81 // again, assuming that local files are linked to a remote file AT MOST, SOMETHING TO BE DONE;
82 SynchronizeFileOperation sfo = new SynchronizeFileOperation(file,
83 null,
84 storageManager,
85 mOCAccount,
86 true,
87 true,
88 mContext);
89 RemoteOperationResult result = sfo.execute(mOCAccount, mContext);
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(ConflictsResolveActivity.EXTRA_FILE, file);
95 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mOCAccount);
96 mContext.startActivity(i);
97 }
98 // TODO save other errors in some point where the user can inspect them later;
99 // or maybe just toast them;
100 // or nothing, very strange fails
101 }
102
103 }