Browse Source

Formated the code and removed Pair

Kilian Périsset 5 years ago
parent
commit
52edda73f6

+ 11 - 7
src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

@@ -34,7 +34,6 @@ import android.os.Bundle;
 import android.os.Handler;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Looper;
 import android.text.TextUtils;
 import android.text.TextUtils;
-import android.util.Pair;
 import android.view.ActionMode;
 import android.view.ActionMode;
 import android.view.LayoutInflater;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.Menu;
@@ -1727,19 +1726,24 @@ public class OCFileListFragment extends ExtendedListFragment implements
 
 
     private void syncAndCheckFiles(Collection<OCFile> files) {
     private void syncAndCheckFiles(Collection<OCFile> files) {
         for (OCFile file : files) {
         for (OCFile file : files) {
-            Pair<Boolean, Long> fileSyncResult= FileOperationsHelper.isSpaceEnough(file);
-            if (fileSyncResult.first) {
+            // Get the remaining space on device (after file download)
+            long remainingSpaceOnDevice = FileOperationsHelper.getRemainingSpaceOnDevice(file);
+            // Get the total available space on device (before file download)
+            long availableSpaceOnDevice = file.getFileLength() + remainingSpaceOnDevice;
+            // Determine if space is enough to download the file
+            boolean isSpaceEnough = remainingSpaceOnDevice > 0;
+
+            if (isSpaceEnough) {
                 mContainerActivity.getFileOperationsHelper().syncFile(file);
                 mContainerActivity.getFileOperationsHelper().syncFile(file);
             } else {
             } else {
-                showSpaceErrorDialog(file, fileSyncResult.second);
+                showSpaceErrorDialog(file, availableSpaceOnDevice);
             }
             }
-
         }
         }
     }
     }
 
 
-    private void showSpaceErrorDialog(OCFile file, long availableDeviceSpace) {
+    private void showSpaceErrorDialog(OCFile file, long spaceAvailableOnDevice) {
         SyncFileNotEnoughSpaceDialogFragment dialog =
         SyncFileNotEnoughSpaceDialogFragment dialog =
-            SyncFileNotEnoughSpaceDialogFragment.newInstance(file, availableDeviceSpace);
+            SyncFileNotEnoughSpaceDialogFragment.newInstance(file, spaceAvailableOnDevice);
         dialog.setTargetFragment(this, NOT_ENOUGH_SPACE_FRAG_REQUEST_CODE);
         dialog.setTargetFragment(this, NOT_ENOUGH_SPACE_FRAG_REQUEST_CODE);
 
 
         if (getFragmentManager() != null) {
         if (getFragmentManager() != null) {

+ 4 - 7
src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -1039,21 +1039,18 @@ public class FileOperationsHelper {
         return new SimpleDateFormat("yyyy-MM-dd_HHmmss", Locale.US).format(new Date()) + ".jpg";
         return new SimpleDateFormat("yyyy-MM-dd_HHmmss", Locale.US).format(new Date()) + ".jpg";
     }
     }
 
 
-    public static Pair<Boolean, Long> isSpaceEnough(OCFile file) {
+    public static Long getRemainingSpaceOnDevice(OCFile file) {
         StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
         StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
         long availableBytesOnDevice;
         long availableBytesOnDevice;
+
         if (android.os.Build.VERSION.SDK_INT >=
         if (android.os.Build.VERSION.SDK_INT >=
             android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
             android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
             availableBytesOnDevice = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
             availableBytesOnDevice = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
-        }
-        else {
+        } else {
             availableBytesOnDevice = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();
             availableBytesOnDevice = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();
         }
         }
 
 
-        boolean isSpaceEnough = file.getFileLength() < availableBytesOnDevice;
-
-        return new Pair<>(isSpaceEnough, availableBytesOnDevice);
-
+        return availableBytesOnDevice - file.getFileLength();
     }
     }