Some improvements in synchronization and refresh of directory list
authorDavid A. Velasco <dvelasco@solidgear.es>
Fri, 6 Jul 2012 13:09:13 +0000 (15:09 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Fri, 6 Jul 2012 13:09:13 +0000 (15:09 +0200)
AndroidManifest.xml
src/eu/alefzero/owncloud/syncadapter/FileSyncAdapter.java
src/eu/alefzero/owncloud/syncadapter/FileSyncService.java
src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java

index 281844b..aed7a2f 100644 (file)
@@ -18,7 +18,7 @@
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
  -->\r
 <manifest package="eu.alefzero.owncloud"\r
     android:versionCode="1"\r
-    android:versionName="0.1.148B" xmlns:android="http://schemas.android.com/apk/res/android">\r
+    android:versionName="0.1.149B" xmlns:android="http://schemas.android.com/apk/res/android">\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
 \r
     <uses-permission android:name="android.permission.GET_ACCOUNTS" />\r
     <uses-permission android:name="android.permission.USE_CREDENTIALS" />\r
index 14237e4..d3b6fc2 100644 (file)
 package eu.alefzero.owncloud.syncadapter;\r
 \r
 import java.io.IOException;\r
 package eu.alefzero.owncloud.syncadapter;\r
 \r
 import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.Iterator;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.Queue;\r
 import java.util.Vector;\r
 \r
 import org.apache.jackrabbit.webdav.DavException;\r
 import java.util.Vector;\r
 \r
 import org.apache.jackrabbit.webdav.DavException;\r
@@ -39,7 +34,6 @@ import android.content.Intent;
 import android.content.SyncResult;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
 import android.content.SyncResult;\r
 import android.os.Bundle;\r
 import android.util.Log;\r
-import android.webkit.MimeTypeMap;\r
 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.webdav.WebdavEntry;\r
 import eu.alefzero.owncloud.datamodel.FileDataStorageManager;\r
 import eu.alefzero.owncloud.datamodel.OCFile;\r
 import eu.alefzero.webdav.WebdavEntry;\r
@@ -72,7 +66,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         \r
         Log.d(TAG, "syncing owncloud account " + account.name);\r
 \r
         \r
         Log.d(TAG, "syncing owncloud account " + account.name);\r
 \r
-        sendStickyBroadcast(true, -1);  // message to signal the start to the UI\r
+        sendStickyBroadcast(true, null);  // message to signal the start to the UI\r
 \r
         PropFindMethod query;\r
         try {\r
 \r
         PropFindMethod query;\r
         try {\r
@@ -87,7 +81,7 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
                 OCFile file = fillOCFile(we);\r
                 file.setParentId(0);\r
                 getStorageManager().saveFile(file);\r
                 OCFile file = fillOCFile(we);\r
                 file.setParentId(0);\r
                 getStorageManager().saveFile(file);\r
-                fetchData(getUri().toString(), syncResult, file.getFileId());\r
+                fetchData(getUri().toString(), syncResult, file.getFileId(), account);\r
             }\r
         } catch (OperationCanceledException e) {\r
             e.printStackTrace();\r
             }\r
         } catch (OperationCanceledException e) {\r
             e.printStackTrace();\r
@@ -100,34 +94,27 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         } catch (DavException e) {\r
             syncResult.stats.numIoExceptions++;\r
             e.printStackTrace();\r
         } catch (DavException e) {\r
             syncResult.stats.numIoExceptions++;\r
             e.printStackTrace();\r
-        } catch (RuntimeException r) {\r
-            //TODO count ; any type of exception should be treated, and the progress indicator finished;\r
-            //             reporting the user about bad synchronizations should be discussed\r
-            r.printStackTrace();\r
-            throw r;\r
+        } catch (Throwable t) {\r
+            // TODO update syncResult\r
+            Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);\r
+            t.printStackTrace();\r
         }\r
         }\r
-        sendStickyBroadcast(false, -1);        \r
+        sendStickyBroadcast(false, null);        \r
     }\r
 \r
     }\r
 \r
-    private void fetchData(String uri, SyncResult syncResult, long parentId) {\r
+    private void fetchData(String uri, SyncResult syncResult, long parentId, Account account) {\r
         try {\r
             Log.v(TAG, "syncing: fetching " + uri);\r
             \r
         try {\r
             Log.v(TAG, "syncing: fetching " + uri);\r
             \r
-            boolean logmore = (uri.contains("many-files"));\r
-\r
             // remote request \r
             // remote request \r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, TO REQUEST");\r
             PropFindMethod query = new PropFindMethod(uri);\r
             getClient().executeMethod(query);\r
             MultiStatus resp = null;\r
             \r
             PropFindMethod query = new PropFindMethod(uri);\r
             getClient().executeMethod(query);\r
             MultiStatus resp = null;\r
             \r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, TO PREPARE THE RESPONSE");\r
             resp = query.getResponseBodyAsMultiStatus();\r
             \r
             // insertion of updated files\r
             resp = query.getResponseBodyAsMultiStatus();\r
             \r
             // insertion of updated files\r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, TO PARSE REPONSES");\r
             for (int i = 1; i < resp.getResponses().length; ++i) {\r
             for (int i = 1; i < resp.getResponses().length; ++i) {\r
-                if (logmore) Log.v(TAG, "syncing: fetching many-files, PARSING REPONSE " + i + "-esima");\r
                 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());\r
                 OCFile file = fillOCFile(we);\r
                 file.setParentId(parentId);\r
                 WebdavEntry we = new WebdavEntry(resp.getResponses()[i], getUri().getPath());\r
                 OCFile file = fillOCFile(we);\r
                 file.setParentId(parentId);\r
@@ -137,26 +124,26 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
             }\r
             \r
             // removal of old files\r
             }\r
             \r
             // removal of old files\r
-            // TODO - getDirectoryContent is crashing the app by lack of memory when a lot of files are in the same directory!!!!\r
-            //        tested with the path /\r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, RETRIEVING VECTOR OF FILES");\r
             Vector<OCFile> files = getStorageManager().getDirectoryContent(\r
                     getStorageManager().getFileById(parentId));\r
             Vector<OCFile> files = getStorageManager().getDirectoryContent(\r
                     getStorageManager().getFileById(parentId));\r
-            for (OCFile file : files) {\r
-                if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0)\r
+            OCFile file;\r
+            for (int i=0; i < files.size(); ) {\r
+                file = files.get(i);\r
+                if (file.getLastSyncDate() != mCurrentSyncTime && file.getLastSyncDate() != 0) {\r
                     getStorageManager().removeFile(file);\r
                     getStorageManager().removeFile(file);\r
+                    files.remove(i);\r
+                } else {\r
+                    i++;\r
+                }\r
             }\r
             \r
             }\r
             \r
-            // synched folder -> notice to IU\r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, NOTIFYING THE UI");\r
-            sendStickyBroadcast(true, parentId);\r
+            // synchronized folder -> notice to UI\r
+            sendStickyBroadcast(true, getStorageManager().getFileById(parentId).getRemotePath());\r
 \r
             // recursive fetch\r
 \r
             // recursive fetch\r
-            if (logmore) Log.v(TAG, "syncing: fetching many-files, TRYING TO RECURSE");\r
-            files = getStorageManager().getDirectoryContent(getStorageManager().getFileById(parentId));\r
-            for (OCFile file : files) {\r
-                if (file.getMimetype().equals("DIR")) {\r
-                    fetchData(getUri().toString() + file.getRemotePath(), syncResult, file.getFileId());\r
+            for (OCFile newFile : files) {\r
+                if (newFile.getMimetype().equals("DIR")) {\r
+                    fetchData(getUri().toString() + newFile.getRemotePath(), syncResult, newFile.getFileId(), account);\r
                 }\r
             }\r
 \r
                 }\r
             }\r
 \r
@@ -172,6 +159,10 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
         } catch (DavException e) {\r
             syncResult.stats.numIoExceptions++;\r
             e.printStackTrace();\r
         } catch (DavException e) {\r
             syncResult.stats.numIoExceptions++;\r
             e.printStackTrace();\r
+        } catch (Throwable t) {\r
+            // TODO update syncResult\r
+            Log.e(TAG, "problem while synchronizing owncloud account " + account.name, t);\r
+            t.printStackTrace();\r
         }\r
     }\r
 \r
         }\r
     }\r
 \r
@@ -186,12 +177,12 @@ public class FileSyncAdapter extends AbstractOwnCloudSyncAdapter {
     }\r
     \r
     \r
     }\r
     \r
     \r
-    private void sendStickyBroadcast(boolean inProgress, long OCDirId) {\r
+    private void sendStickyBroadcast(boolean inProgress, String dirRemotePath) {\r
         Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
         i.putExtra(FileSyncService.IN_PROGRESS, inProgress);\r
         i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);\r
         Intent i = new Intent(FileSyncService.SYNC_MESSAGE);\r
         i.putExtra(FileSyncService.IN_PROGRESS, inProgress);\r
         i.putExtra(FileSyncService.ACCOUNT_NAME, getAccount().name);\r
-        if (OCDirId > 0) {\r
-            i.putExtra(FileSyncService.SYNC_FOLDER, OCDirId);\r
+        if (dirRemotePath != null) {\r
+            i.putExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH, dirRemotePath);\r
         }\r
         getContext().sendStickyBroadcast(i);\r
     }\r
         }\r
         getContext().sendStickyBroadcast(i);\r
     }\r
index 6c1ed0f..42eaf6c 100644 (file)
@@ -29,9 +29,9 @@ import android.os.IBinder;
  */\r
 public class FileSyncService extends Service {\r
     public static final String SYNC_MESSAGE = "eu.alefzero.owncloud.files.ACCOUNT_SYNC";\r
  */\r
 public class FileSyncService extends Service {\r
     public static final String SYNC_MESSAGE = "eu.alefzero.owncloud.files.ACCOUNT_SYNC";\r
+    public static final String SYNC_FOLDER_REMOTE_PATH = "eu.alefzero.owncloud.files.SYNC_FOLDER_REMOTE_PATH";\r
     public static final String IN_PROGRESS = "sync_in_progress";\r
     public static final String ACCOUNT_NAME = "account_name";\r
     public static final String IN_PROGRESS = "sync_in_progress";\r
     public static final String ACCOUNT_NAME = "account_name";\r
-    public static final String SYNC_FOLDER = "eu.alefzero.owncloud.files.SYNC_FOLDER";\r
 \r
     /*\r
      * {@inheritDoc}\r
 \r
     /*\r
      * {@inheritDoc}\r
index 188d1f0..e0eccf9 100644 (file)
@@ -160,6 +160,8 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
                 break;\r
             }\r
             case R.id.startSync: {\r
                 break;\r
             }\r
             case R.id.startSync: {\r
+                // This could be interesting\r
+                //ContentResolver.cancelSync(null, "org.owncloud");   // cancel the current synchronizations of any other ownCloud account\r
                 Bundle bundle = new Bundle();\r
                 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
                 ContentResolver.requestSync(\r
                 Bundle bundle = new Bundle();\r
                 bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);\r
                 ContentResolver.requestSync(\r
@@ -614,29 +616,25 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements
             Log.d("FileDisplay", "sync of account " + account_name\r
                     + " is in_progress: " + inProgress);\r
 \r
             Log.d("FileDisplay", "sync of account " + account_name\r
                     + " is in_progress: " + inProgress);\r
 \r
-            //if (account_name.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {  // TODO - probably this check should be added, but won't push it until really tests are done; no time now\r
+            if (account_name.equals(AccountUtils.getCurrentOwnCloudAccount(context).name)) {  \r
             \r
             \r
-                /// try to refresh the view with every message received from the FileSyncAdapter; brute, but more user friendly when there are a lot of files in the server\r
-                OCFile currentDir;\r
+                String synchFolderRemotePath = intent.getStringExtra(FileSyncService.SYNC_FOLDER_REMOTE_PATH); \r
+                        \r
                 if (mCurrentDir == null)\r
                 if (mCurrentDir == null)\r
-                    currentDir = mStorageManager.getFileByPath("/");\r
-                else\r
-                    currentDir = mStorageManager.getFileByPath(mCurrentDir.getRemotePath());\r
-                \r
-                if (currentDir != null) {\r
-                    mCurrentDir = currentDir;\r
+                    mCurrentDir = mStorageManager.getFileByPath("/");\r
+\r
+                if (synchFolderRemotePath != null && mCurrentDir != null && mCurrentDir.getRemotePath().equals(synchFolderRemotePath) ) {\r
                     FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager()\r
                             .findFragmentById(R.id.fileList);\r
                     FileListFragment fileListFragment = (FileListFragment) getSupportFragmentManager()\r
                             .findFragmentById(R.id.fileList);\r
+                    mCurrentDir = getStorageManager().getFileByPath(synchFolderRemotePath);\r
                     if (fileListFragment != null) {\r
                     if (fileListFragment != null) {\r
-                        if (!mCurrentDir.equals(fileListFragment.getCurrentFile())) {\r
-                            fileListFragment.listDirectory(mCurrentDir);    // only set the directory in the fragment first time\r
-                        } else\r
-                            fileListFragment.listDirectory();   // enough to show new files in the current directory if they are added after \r
+                        fileListFragment.listDirectory(mCurrentDir);  \r
                     }\r
                 }\r
                 \r
                 setSupportProgressBarIndeterminateVisibility(inProgress);\r
                     }\r
                 }\r
                 \r
                 setSupportProgressBarIndeterminateVisibility(inProgress);\r
-            //}\r
+                \r
+            }\r
         }\r
     }\r
     \r
         }\r
     }\r
     \r