Create observer class for parent folders
[pub/Android/ownCloud.git] / src / com / owncloud / android / files / OwnCloudFolderObserver.java
1 package com.owncloud.android.files;
2
3 import java.io.File;
4
5 import android.accounts.Account;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.os.FileObserver;
9
10 import com.owncloud.android.datamodel.FileDataStorageManager;
11 import com.owncloud.android.datamodel.OCFile;
12 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
13 import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
14 import com.owncloud.android.operations.SynchronizeFileOperation;
15 import com.owncloud.android.ui.activity.ConflictsResolveActivity;
16 import com.owncloud.android.utils.Log_OC;
17
18 public class OwnCloudFolderObserver extends FileObserver {
19
20 private static int MASK = (FileObserver.CREATE | FileObserver.MOVED_TO);
21
22 private static String TAG = OwnCloudFolderObserver.class.getSimpleName();
23
24 private String mPath;
25 private int mMask;
26 private Account mOCAccount;
27 private Context mContext;
28
29 public OwnCloudFolderObserver(String path, Account account, Context context) {
30 super(path, FileObserver.ALL_EVENTS);
31 if (path == null)
32 throw new IllegalArgumentException("NULL path argument received");
33 if (account == null)
34 throw new IllegalArgumentException("NULL account argument received");
35 if (context == null)
36 throw new IllegalArgumentException("NULL context argument received");
37 mPath = path;
38 mOCAccount = account;
39 mContext = context;
40 }
41
42 @Override
43 public void onEvent(int event, String path) {
44 Log_OC.d(TAG, "Got file modified with event " + event + " and path " + mPath
45 + ((path != null) ? File.separator + path : ""));
46 if ((event & MASK) == 0) {
47 Log_OC.wtf(TAG, "Incorrect event " + event + " sent for file " + mPath
48 + ((path != null) ? File.separator + path : "") + " with registered for " + mMask
49 + " and original path " + mPath);
50
51 } else {
52 if ((event & FileObserver.CREATE) != 0) {
53 // TODO Enable a flag
54 }
55 if ((event & FileObserver.MOVED_TO) != 0) {
56 // TODO Start sync
57 }
58 }
59 }
60
61 private void startSyncOperation() {
62 // TODO Move to common file because it is being used in OCFileObserver
63 // too
64
65 FileDataStorageManager storageManager = new FileDataStorageManager(mOCAccount, mContext.getContentResolver());
66 // a fresh object is needed; many things could have occurred to the file
67 // since it was registered to observe again, assuming that local files
68 // are linked to a remote file AT MOST, SOMETHING TO BE DONE;
69 OCFile file = storageManager.getFileByLocalPath(mPath);
70 SynchronizeFileOperation sfo = new SynchronizeFileOperation(file, null, mOCAccount, true, mContext);
71 RemoteOperationResult result = sfo.execute(storageManager, mContext);
72 if (result.getCode() == ResultCode.SYNC_CONFLICT) {
73 // ISSUE 5: if the user is not running the app (this is a service!),
74 // this can be very intrusive; a notification should be preferred
75 Intent i = new Intent(mContext, ConflictsResolveActivity.class);
76 i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
77 i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
78 i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mOCAccount);
79 mContext.startActivity(i);
80 }
81 }
82
83 }