Grant that a favourite file in the device is watched if a redownload fails
[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.util.LinkedList;
22 import java.util.List;
23
24 import com.owncloud.android.datamodel.DataStorageManager;
25 import com.owncloud.android.datamodel.OCFile;
26 import com.owncloud.android.files.OwnCloudFileObserver.FileObserverStatusListener.Status;
27 import com.owncloud.android.files.services.FileUploader;
28 import com.owncloud.android.network.OwnCloudClientUtils;
29 import com.owncloud.android.operations.RemoteOperationResult;
30 import com.owncloud.android.operations.SynchronizeFileOperation;
31
32 import eu.alefzero.webdav.WebdavClient;
33
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;
39
40 public class OwnCloudFileObserver extends FileObserver {
41
42 public static int CHANGES_ONLY = CLOSE_WRITE;
43
44 private static String TAG = "OwnCloudFileObserver";
45 private String mPath;
46 private int mMask;
47 DataStorageManager mStorage;
48 Account mOCAccount;
49 OCFile mFile;
50 static Context mContext; // ISSUE 4: why is this static?
51 List<FileObserverStatusListener> mListeners;
52
53 public OwnCloudFileObserver(String path) {
54 this(path, ALL_EVENTS);
55 }
56
57 public OwnCloudFileObserver(String path, int mask) {
58 super(path, mask);
59 mPath = path;
60 mMask = mask;
61 mListeners = new LinkedList<FileObserverStatusListener>();
62 }
63
64 public void setAccount(Account account) {
65 mOCAccount = account;
66 }
67
68 public void setStorageManager(DataStorageManager manager) {
69 mStorage = manager;
70 }
71
72 public void setOCFile(OCFile file) {
73 mFile = file;
74 }
75
76 public void setContext(Context context) {
77 mContext = context;
78 }
79
80 public String getPath() {
81 return mPath;
82 }
83
84 public String getRemotePath() {
85 return mFile.getRemotePath();
86 }
87
88 public void addObserverStatusListener(FileObserverStatusListener listener) {
89 mListeners.add(listener);
90 }
91
92 @Override
93 public void onEvent(int event, String path) {
94 Log.d(TAG, "Got file modified with event " + event + " and path " + path);
95 if ((event & mMask) == 0) {
96 Log.wtf(TAG, "Incorrect event " + event + " sent for file " + path +
97 " with registered for " + mMask + " and original path " +
98 mPath);
99 for (FileObserverStatusListener l : mListeners)
100 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.INCORRECT_MASK);
101 return;
102 }
103 WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mOCAccount, mContext);
104 SynchronizeFileOperation sfo = new SynchronizeFileOperation(mFile.getRemotePath(), mStorage, mOCAccount, mContext);
105 RemoteOperationResult result = sfo.execute(wc);
106
107 if (result.getExtraData() == Boolean.TRUE) {
108 // inform user about conflict and let him decide what to do
109 for (FileObserverStatusListener l : mListeners)
110 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.CONFLICT);
111 return;
112 }
113
114 for (FileObserverStatusListener l : mListeners)
115 l.OnObservedFileStatusUpdate(mPath, getRemotePath(), mOCAccount, Status.SENDING_TO_UPLOADER);
116
117 Intent i = new Intent(mContext, FileUploader.class);
118 i.putExtra(FileUploader.KEY_ACCOUNT, mOCAccount);
119 i.putExtra(FileUploader.KEY_REMOTE_FILE, mFile.getRemotePath());
120 i.putExtra(FileUploader.KEY_LOCAL_FILE, mPath);
121 i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
122 i.putExtra(FileUploader.KEY_FORCE_OVERWRITE, true);
123 mContext.startService(i);
124 }
125
126 public interface FileObserverStatusListener {
127 public enum Status {
128 SENDING_TO_UPLOADER,
129 CONFLICT,
130 INCORRECT_MASK
131 }
132
133 public void OnObservedFileStatusUpdate(String localPath,
134 String remotePath,
135 Account account,
136 FileObserverStatusListener.Status status);
137 }
138
139 }