Two way synchronization for files
authorBartek Przybylski <bart.p.pl@gmail.com>
Mon, 22 Oct 2012 19:15:26 +0000 (21:15 +0200)
committerBartek Przybylski <bart.p.pl@gmail.com>
Mon, 22 Oct 2012 19:15:26 +0000 (21:15 +0200)
AndroidManifest.xml
src/com/owncloud/android/files/OwnCloudFileObserver.java
src/com/owncloud/android/files/services/FileObserverService.java
src/com/owncloud/android/syncadapter/FileSyncAdapter.java
src/com/owncloud/android/ui/activity/FileDisplayActivity.java
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

index ae53634..91b215d 100644 (file)
                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
             </intent-filter>\r
         </receiver>\r
                 <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
             </intent-filter>\r
         </receiver>\r
-        <!--  receiver android:name=".files.BootupBroadcastReceiver">\r
+        <receiver android:name=".files.BootupBroadcastReceiver">\r
             <intent-filter>\r
                 <action android:name="android.intent.action.BOOT_COMPLETED"/>\r
             </intent-filter>\r
             <intent-filter>\r
                 <action android:name="android.intent.action.BOOT_COMPLETED"/>\r
             </intent-filter>\r
-        </receiver -->\r
-        <!--  service android:name=".files.services.FileObserverService"/ -->\r
+        </receiver>\r
+        <service android:name=".files.services.FileObserverService"/>
     </application>\r
 \r
 </manifest>
     </application>\r
 \r
 </manifest>
index b01b8fa..42e4763 100644 (file)
@@ -52,9 +52,14 @@ public class OwnCloudFileObserver extends FileObserver {
         return mPath;
     }
     
         return mPath;
     }
     
+    public String getRemotePath() {
+        return mFile.getRemotePath();
+    }
+    
     @Override
     public void onEvent(int event, String path) {
     @Override
     public void onEvent(int event, String path) {
-        if ((event | mMask) == 0) {
+        Log.d(TAG, "Got file modified with event " + event + " and path " + path);
+        if ((event & mMask) == 0) {
             Log.wtf(TAG, "Incorrect event " + event + " sent for file " + path +
                          " with registered for " + mMask + " and original path " +
                          mPath);
             Log.wtf(TAG, "Incorrect event " + event + " sent for file " + path +
                          " with registered for " + mMask + " and original path " +
                          mPath);
index 4d6dd02..25b5f1c 100644 (file)
@@ -28,6 +28,7 @@ public class FileObserverService extends Service {
     public final static int CMD_INIT_OBSERVED_LIST = 1;
     public final static int CMD_ADD_OBSERVED_FILE = 2;
     public final static int CMD_DEL_OBSERVED_FILE = 3;
     public final static int CMD_INIT_OBSERVED_LIST = 1;
     public final static int CMD_ADD_OBSERVED_FILE = 2;
     public final static int CMD_DEL_OBSERVED_FILE = 3;
+    public final static int CMD_ADD_DOWNLOADING_FILE = 4;
 
     private static String TAG = "FileObserverService";
     private static List<OwnCloudFileObserver> mObservers;
 
     private static String TAG = "FileObserverService";
     private static List<OwnCloudFileObserver> mObservers;
@@ -70,13 +71,16 @@ public class FileObserverService extends Service {
             case CMD_DEL_OBSERVED_FILE:
                 removeObservedFile(intent.getStringExtra(KEY_CMD_ARG));
                 break;
             case CMD_DEL_OBSERVED_FILE:
                 removeObservedFile(intent.getStringExtra(KEY_CMD_ARG));
                 break;
+            case CMD_ADD_DOWNLOADING_FILE:
+                addDownloadingFile(intent.getStringExtra(KEY_CMD_ARG));
+                break;
             default:
                 Log.wtf(TAG, "Incorrect key given");
         }
 
         return Service.START_STICKY;
     }
             default:
                 Log.wtf(TAG, "Incorrect key given");
         }
 
         return Service.START_STICKY;
     }
-    
+
     private void initializeObservedList() {
         if (mObservers != null) return; // nothing to do here
         mObservers = new ArrayList<OwnCloudFileObserver>();
     private void initializeObservedList() {
         if (mObservers != null) return; // nothing to do here
         mObservers = new ArrayList<OwnCloudFileObserver>();
@@ -107,7 +111,7 @@ public class FileObserverService extends Service {
             String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
             OwnCloudFileObserver observer =
                     new OwnCloudFileObserver(path, OwnCloudFileObserver.CHANGES_ONLY);
             String path = c.getString(c.getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH));
             OwnCloudFileObserver observer =
                     new OwnCloudFileObserver(path, OwnCloudFileObserver.CHANGES_ONLY);
-            observer.setContext(getBaseContext());
+            observer.setContext(getApplicationContext());
             observer.setAccount(account);
             observer.setStorageManager(storage);
             observer.setOCFile(storage.getFileByPath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))));
             observer.setAccount(account);
             observer.setStorageManager(storage);
             observer.setOCFile(storage.getFileByPath(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PATH))));
@@ -167,6 +171,24 @@ public class FileObserverService extends Service {
         }
         Log.d(TAG, "Stopped watching " + path);
     }
         }
         Log.d(TAG, "Stopped watching " + path);
     }
+        
+    private void addDownloadingFile(String remotePath) {
+        OwnCloudFileObserver observer = null;
+        for (OwnCloudFileObserver o : mObservers) {
+            if (o.getRemotePath().equals(remotePath)) {
+                observer = o;
+                break;
+            }
+        }
+        if (observer == null) {
+            Log.e(TAG, "Couldn't find observer for remote file " + remotePath);
+            return;
+        }
+        observer.stopWatching();
+        DownloadCompletedReceiver dcr = new DownloadCompletedReceiver(observer.getPath(), observer);
+        registerReceiver(dcr, new IntentFilter(FileDownloader.DOWNLOAD_FINISH_MESSAGE));
+    }
+
     
     private static void addReceiverToList(DownloadCompletedReceiver r) {
         synchronized(mReceiverListLock) {
     
     private static void addReceiverToList(DownloadCompletedReceiver r) {
         synchronized(mReceiverListLock) {
index 5b398c0..8c9a153 100644 (file)
@@ -34,6 +34,7 @@ import com.owncloud.android.authenticator.AccountAuthenticator;
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
+import com.owncloud.android.files.services.FileObserverService;\r
 import com.owncloud.android.utils.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
 import com.owncloud.android.utils.OwnCloudVersion;\r
 \r
 import android.accounts.Account;\r
@@ -211,7 +212,13 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                             getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() &&\r
                             file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath())\r
                                                                          .getModificationTimestamp()) {\r
                             getStorageManager().getFileByPath(file.getRemotePath()).keepInSync() &&\r
                             file.getModificationTimestamp() > getStorageManager().getFileByPath(file.getRemotePath())\r
                                                                          .getModificationTimestamp()) {\r
-                        Intent intent = new Intent(this.getContext(), FileDownloader.class);\r
+                        // first disable observer so we won't get file upload right after download\r
+                        Log.d(TAG, "Disabling observation of remote file" + file.getRemotePath());\r
+                        Intent intent = new Intent(getContext(), FileObserverService.class);\r
+                        intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_ADD_DOWNLOADING_FILE);\r
+                        intent.putExtra(FileObserverService.KEY_CMD_ARG, file.getRemotePath());\r
+                        getContext().startService(intent);\r
+                        intent = new Intent(this.getContext(), FileDownloader.class);\r
                         intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());\r
                         intent.putExtra(FileDownloader.EXTRA_FILE, file);\r
                         file.setKeepInSync(true);\r
                         intent.putExtra(FileDownloader.EXTRA_ACCOUNT, getAccount());\r
                         intent.putExtra(FileDownloader.EXTRA_FILE, file);\r
                         file.setKeepInSync(true);\r
index 22ef63b..328d396 100644 (file)
@@ -68,6 +68,7 @@ import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
+import com.owncloud.android.files.services.FileObserverService;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.network.OwnCloudClientUtils;\r
@@ -154,10 +155,10 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
         }\r
 \r
         // file observer\r
         }\r
 \r
         // file observer\r
-        /*Intent observer_intent = new Intent(this, FileObserverService.class);\r
+        Intent observer_intent = new Intent(this, FileObserverService.class);\r
         observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);\r
         startService(observer_intent);\r
         observer_intent.putExtra(FileObserverService.KEY_FILE_CMD, FileObserverService.CMD_INIT_OBSERVED_LIST);\r
         startService(observer_intent);\r
-        */\r
+        \r
             \r
         /// USER INTERFACE\r
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
             \r
         /// USER INTERFACE\r
         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);\r
index 007c294..3f0be07 100644 (file)
@@ -78,6 +78,7 @@ import com.owncloud.android.authenticator.AccountAuthenticator;
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
 import com.owncloud.android.datamodel.FileDataStorageManager;\r
 import com.owncloud.android.datamodel.OCFile;\r
 import com.owncloud.android.files.services.FileDownloader;\r
+import com.owncloud.android.files.services.FileObserverService;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
 import com.owncloud.android.files.services.FileUploader;\r
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;\r
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;\r
@@ -302,7 +303,7 @@ public class FileDetailFragment extends SherlockFragment implements
                 } else {\r
                     mContainerActivity.onFileStateChanged();    // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
                 }\r
                 } else {\r
                     mContainerActivity.onFileStateChanged();    // put inside 'else' to not call it twice (here, and in the virtual click on fdDownloadBtn)\r
                 }\r
-                /*\r
+                \r
                 Intent intent = new Intent(getActivity().getApplicationContext(),\r
                                            FileObserverService.class);\r
                 intent.putExtra(FileObserverService.KEY_FILE_CMD,\r
                 Intent intent = new Intent(getActivity().getApplicationContext(),\r
                                            FileObserverService.class);\r
                 intent.putExtra(FileObserverService.KEY_FILE_CMD,\r
@@ -310,8 +311,9 @@ public class FileDetailFragment extends SherlockFragment implements
                                    FileObserverService.CMD_ADD_OBSERVED_FILE:\r
                                    FileObserverService.CMD_DEL_OBSERVED_FILE));\r
                 intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath());\r
                                    FileObserverService.CMD_ADD_OBSERVED_FILE:\r
                                    FileObserverService.CMD_DEL_OBSERVED_FILE));\r
                 intent.putExtra(FileObserverService.KEY_CMD_ARG, mFile.getStoragePath());\r
+                Log.e(TAG, "starting observer service");\r
                 getActivity().startService(intent);\r
                 getActivity().startService(intent);\r
-                */\r
+                \r
                 break;\r
             }\r
             case R.id.fdRenameBtn: {\r
                 break;\r
             }\r
             case R.id.fdRenameBtn: {\r