[Title] Add progress monitor to check file size when push/pull files to device.
authorhyunsik.noh <hyunsik.noh@samsung.com>
Fri, 18 Jan 2013 07:59:29 +0000 (16:59 +0900)
committerhyunsik.noh <hyunsik.noh@samsung.com>
Fri, 18 Jan 2013 07:59:29 +0000 (16:59 +0900)
[Type]
[Module]common
[Priority]
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]

org.tizen.common.sdblib/src/org/tizen/sdblib/SyncService.java

index 9169aea..26ab121 100755 (executable)
@@ -48,6 +48,8 @@ public final class SyncService {
     private final static NullSyncProgressMonitor sNullSyncProgressMonitor =
             new NullSyncProgressMonitor();
 
+    public static final String MESSAGE_SIZECHECKING = "File size checking: it can take some time.";
+
     private final static int SYNC_DATA_MAX = 64*1024;
     private final static int REMOTE_PATH_MAX_LENGTH = 1024;
 
@@ -396,8 +398,20 @@ public final class SyncService {
         // get a FileListingService object
         FileListingService fls = new FileListingService(mDevice);
 
+        monitor.start(-1);
+
+        monitor.startSubTask(MESSAGE_SIZECHECKING);
+
         // compute the number of file to move
-        long total = getTotalRemoteFileSizeLong(entries, fls);
+        long total;
+        try {
+            total = getTotalRemoteFileSizeLong(entries, fls, monitor);
+        } catch (InterruptedException e) {
+            return new SyncResult(RESULT_CANCELED);
+        } finally {
+            monitor.stop();
+        }
+
         TransferInfo transferInfo = new TransferInfo();
 
         // start the monitor
@@ -559,10 +573,23 @@ public final class SyncService {
         for (String path : local) {
             files.add(new File(path));
         }
+        
+        monitor.start(-1);
+        
+        monitor.startSubTask(MESSAGE_SIZECHECKING);
 
         // get the total count of the bytes to transfer
         File[] fileArray = files.toArray(new File[files.size()]);
-        long total = getTotalLocalFileSizeLong(fileArray);
+
+        long total;
+        try {
+            total = getTotalLocalFileSizeLong(fileArray, monitor);
+        } catch (InterruptedException e) {
+            return new SyncResult(RESULT_CANCELED);
+        } finally {
+            monitor.stop();
+        }
+
         TransferInfo sizeInfo = new TransferInfo();
         
         monitor.start(sizeInfo.downSizing(total));
@@ -647,6 +674,35 @@ public final class SyncService {
 
         return count;
     }
+    
+    /**
+     * compute the recursive file size of all the files in the list. Folder
+     * have a weight of 1.
+     * @param entries
+     * @param fls
+     * @param monitor 
+     * @return long typed file size
+     * @throws InterruptedException 
+     */
+    private long getTotalRemoteFileSizeLong(FileEntry[] entries, FileListingService fls, ISyncProgressMonitor monitor) throws InterruptedException {
+        long count = 0;
+        for (FileEntry e : entries) {
+            if (monitor.isCanceled()) {
+                throw new InterruptedException("The computing file size was cancelled");
+            }
+            int type = e.getType();
+            if (type == FileListingService.TYPE_DIRECTORY || type == FileListingService.TYPE_ROOT_DEVICE
+                    || type == FileListingService.TYPE_ROOT_EMULATOR) {
+                // get the children
+                FileEntry[] children = fls.getChildren(e, false, null);
+                count += getTotalRemoteFileSizeLong(children, fls, monitor) + 1;
+            } else if (type == FileListingService.TYPE_FILE) {
+                count += e.getSizeValue();
+            }
+        }
+
+        return count;
+    }
 
     /**
      * compute the recursive file size of all the files in the list. Folder
@@ -677,6 +733,33 @@ public final class SyncService {
      * This does not check for circular links.
      * @param files
      * @return long typed file size
+     * @throws InterruptedException 
+     */
+    private long getTotalLocalFileSizeLong(File[] files, ISyncProgressMonitor monitor) throws InterruptedException {
+        long count = 0;
+
+        for (File f : files) {
+            if (monitor.isCanceled()) {
+                throw new InterruptedException("The computing file size was cancelled");
+            }
+            if (f.exists()) {
+                if (f.isDirectory()) {
+                    return getTotalLocalFileSizeLong(f.listFiles(), monitor) + 1;
+                } else if (f.isFile()) {
+                    count += f.length();
+                }
+            }
+        }
+
+        return count;
+    }
+    
+    /**
+     * compute the recursive file size of all the files in the list. Folder
+     * have a weight of 1.
+     * This does not check for circular links.
+     * @param files
+     * @return long typed file size
      */
     private long getTotalLocalFileSizeLong(File[] files) {
         long count = 0;