Jelajahi Sumber

Code cleanups

Bartosz Przybylski 9 tahun lalu
induk
melakukan
ebbab224b2

+ 1 - 34
src/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -32,7 +32,6 @@ import android.content.OperationApplicationException;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.RemoteException;
-import android.provider.BaseColumns;
 import android.provider.MediaStore;
 
 import com.owncloud.android.MainApp;
@@ -728,44 +727,12 @@ public class FileDataStorageManager {
                 if (!targetFolder.exists()) {
                     targetFolder.mkdirs();
                 }
-                copied = copyFile(localFile, targetFile);
+                copied = FileStorageUtils.copyFile(localFile, targetFile);
             }
             Log_OC.d(TAG, "Local file COPIED : " + copied);
         }
     }
 
-    private boolean copyFile(File src, File target) {
-        boolean ret = true;
-
-        InputStream in = null;
-        OutputStream out = null;
-
-        try {
-            in = new FileInputStream(src);
-            out = new FileOutputStream(target);
-            byte[] buf = new byte[1024];
-            int len;
-            while ((len = in.read(buf)) > 0) {
-                out.write(buf, 0, len);
-            }
-        } catch (IOException ex) {
-            ret = false;
-        } finally {
-            if (in != null) try {
-                in.close();
-            } catch (IOException e) {
-                e.printStackTrace(System.err);
-            }
-            if (out != null) try {
-                out.close();
-            } catch (IOException e) {
-                e.printStackTrace(System.err);
-            }
-        }
-
-        return ret;
-    }
-
     public void migrateStoredFiles(String srcPath, String dstPath) throws Exception {
         Cursor c = null;
         if (getContentResolver() != null) {

+ 5 - 7
src/com/owncloud/android/ui/activity/Preferences.java

@@ -218,7 +218,7 @@ public class Preferences extends PreferenceActivity {
         }
         
         boolean helpEnabled = getResources().getBoolean(R.bool.help_enabled);
-        Preference pHelp =  findPreference("help");
+        Preference pHelp = findPreference("help");
         if (pHelp != null ){
             if (helpEnabled) {
                 pHelp.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -264,7 +264,7 @@ public class Preferences extends PreferenceActivity {
                         intent.putExtra(Intent.EXTRA_TEXT, recommendText);
                         startActivity(intent);
 
-                        return(true);
+                        return true;
 
                     }
                 });
@@ -442,7 +442,7 @@ public class Preferences extends PreferenceActivity {
                 ((CheckBoxPreference)mPrefInstantUpload).isChecked());
 
         /* About App */
-       pAboutApp = (Preference) findPreference("about_app");
+       pAboutApp = findPreference("about_app");
        if (pAboutApp != null) { 
                pAboutApp.setTitle(String.format(getString(R.string.about_android),
                        getString(R.string.app_name)));
@@ -585,8 +585,7 @@ public class Preferences extends PreferenceActivity {
 
         if (requestCode == ACTION_SELECT_UPLOAD_PATH && resultCode == RESULT_OK){
 
-            OCFile folderToUpload =
-                    (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
+            OCFile folderToUpload =  data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
 
             mUploadPath = folderToUpload.getRemotePath();
 
@@ -599,8 +598,7 @@ public class Preferences extends PreferenceActivity {
 
         } else if (requestCode == ACTION_SELECT_UPLOAD_VIDEO_PATH && resultCode == RESULT_OK) {
 
-            OCFile folderToUploadVideo =
-                    (OCFile) data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
+            OCFile folderToUploadVideo = data.getParcelableExtra(UploadPathActivity.EXTRA_FOLDER);
 
             mUploadVideoPath = folderToUploadVideo.getRemotePath();
 

+ 4 - 53
src/com/owncloud/android/ui/activity/StorageMigrationActivity.java

@@ -37,13 +37,9 @@ import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.utils.FileStorageUtils;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 
 /**
  * Created by Bartosz Przybylski on 07.11.2015.
@@ -154,7 +150,7 @@ public class StorageMigrationActivity extends AppCompatActivity {
 				rollback();
 				return e.getResId();
 			} catch (MigrationCleanupException e) {
-				Log_OC.w(TAG, "Migration mleanup step failed");
+				Log_OC.w(TAG, "Migration cleanup step failed");
 				return 0;
 			} finally {
 				publishProgress(mProgress++, R.string.file_migration_restoring_accounts_configuration);
@@ -205,7 +201,7 @@ public class StorageMigrationActivity extends AppCompatActivity {
 			if (new File(dstFile, MainApp.getDataFolder()).exists())
 				throw new MigrationException(R.string.file_migration_failed_dir_already_exists);
 
-			if (dstFile.getFreeSpace() < calculateUsedSpace(new File(srcFile, MainApp.getDataFolder())))
+			if (dstFile.getFreeSpace() < FileStorageUtils.getFolderSize(new File(srcFile, MainApp.getDataFolder())))
 				throw new MigrationException(R.string.file_migration_failed_not_enough_space);
 		}
 
@@ -218,38 +214,6 @@ public class StorageMigrationActivity extends AppCompatActivity {
 			publishProgress(mProgress);
 		}
 
-		private boolean copyFile(File src, File target) {
-			boolean ret = true;
-
-			InputStream in = null;
-			OutputStream out = null;
-
-			try {
-				in = new FileInputStream(src);
-				out = new FileOutputStream(target);
-				byte[] buf = new byte[1024];
-				int len;
-				while ((len = in.read(buf)) > 0) {
-					out.write(buf, 0, len);
-				}
-			} catch (IOException ex) {
-				ret = false;
-			} finally {
-				if (in != null) try {
-					in.close();
-				} catch (IOException e) {
-					e.printStackTrace(System.err);
-				}
-				if (out != null) try {
-					out.close();
-				} catch (IOException e) {
-					e.printStackTrace(System.err);
-				}
-			}
-
-			return ret;
-		}
-
 		void copyDirs(File src, File dst) throws MigrationException {
 			if (!dst.mkdirs())
 				throw new MigrationException(R.string.file_migration_failed_while_coping);
@@ -261,7 +225,7 @@ public class StorageMigrationActivity extends AppCompatActivity {
 
 				if (f.isDirectory())
 					copyDirs(f, new File(dst, f.getName()));
-				else if (!copyFile(f, new File(dst, f.getName())))
+				else if (!FileStorageUtils.copyFile(f, new File(dst, f.getName())))
 					throw new MigrationException(R.string.file_migration_failed_while_coping);
 			}
 
@@ -290,19 +254,6 @@ public class StorageMigrationActivity extends AppCompatActivity {
 					Log_OC.w(TAG, "Rollback step failed");
 		}
 
-		long calculateUsedSpace(File dir) {
-			long result = 0;
-
-			for (File f : dir.listFiles()) {
-				if (f.isDirectory())
-					result += calculateUsedSpace(f);
-				else
-					result += f.length();
-			}
-
-			return result;
-		}
-
 		void saveAccountsSyncStatus(String authority, Account accounts[], boolean syncs[]) {
 			for (int i = 0; i < accounts.length; ++i)
 				syncs[i] = ContentResolver.getSyncAutomatically(accounts[i], authority);

+ 52 - 7
src/com/owncloud/android/utils/FileStorageUtils.java

@@ -40,6 +40,11 @@ import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
@@ -50,6 +55,16 @@ import java.util.Vector;
 import third_parties.daveKoeller.AlphanumComparator;
 
 
+import android.accounts.Account;
+import android.annotation.SuppressLint;
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+import android.net.Uri;
+import android.os.StatFs;
+import android.webkit.MimeTypeMap;
+
+
 /**
  * Static methods to help in access to local file system.
  */
@@ -439,13 +454,11 @@ public class FileStorageUtils {
     public static long getFolderSize(File dir) {
         if (dir.exists()) {
             long result = 0;
-            File[] fileList = dir.listFiles();
-            for(int i = 0; i < fileList.length; i++) {
-                if(fileList[i].isDirectory()) {
-                    result += getFolderSize(fileList[i]);
-                } else {
-                    result += fileList[i].length();
-                }
+            for (File f : dir.listFiles()) {
+                if (f.isDirectory())
+                    result += getFolderSize(f);
+                else
+                    result += f.length();
             }
             return result;
         }
@@ -495,4 +508,36 @@ public class FileStorageUtils {
         }
     }
 
+    public static boolean copyFile(File src, File target) {
+        boolean ret = true;
+
+        InputStream in = null;
+        OutputStream out = null;
+
+        try {
+            in = new FileInputStream(src);
+            out = new FileOutputStream(target);
+            byte[] buf = new byte[1024];
+            int len;
+            while ((len = in.read(buf)) > 0) {
+                out.write(buf, 0, len);
+            }
+        } catch (IOException ex) {
+            ret = false;
+        } finally {
+            if (in != null) try {
+                in.close();
+            } catch (IOException e) {
+                e.printStackTrace(System.err);
+            }
+            if (out != null) try {
+                out.close();
+            } catch (IOException e) {
+                e.printStackTrace(System.err);
+            }
+        }
+
+        return ret;
+    }
+
 }