OwnCloudFileObserver.java 4.9 KB

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