1 package com
.owncloud
.android
.files
;
5 import android
.accounts
.Account
;
6 import android
.content
.Context
;
7 import android
.content
.Intent
;
8 import android
.os
.FileObserver
;
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
;
18 public class OwnCloudFolderObserver
extends FileObserver
{
20 private static int MASK
= (FileObserver
.CREATE
| FileObserver
.MOVED_TO
);
22 private static String TAG
= OwnCloudFolderObserver
.class.getSimpleName();
26 private Account mOCAccount
;
27 private Context mContext
;
29 public OwnCloudFolderObserver(String path
, Account account
, Context context
) {
30 super(path
, FileObserver
.ALL_EVENTS
);
32 throw new IllegalArgumentException("NULL path argument received");
34 throw new IllegalArgumentException("NULL account argument received");
36 throw new IllegalArgumentException("NULL context argument received");
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
);
52 if ((event
& FileObserver
.CREATE
) != 0) {
55 if ((event
& FileObserver
.MOVED_TO
) != 0) {
61 private void startSyncOperation() {
62 // TODO Move to common file because it is being used in OCFileObserver
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
);