OwnCloudFileObserver.java 4.8 KB

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