From 2f6793f8786ecb2e0a6b6cfcbf000350300e1d22 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Mon, 15 May 2017 23:19:33 +0200 Subject: [PATCH 1/7] remove unnecessary return statement (codacy) --- src/main/java/com/owncloud/android/media/MediaService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/media/MediaService.java b/src/main/java/com/owncloud/android/media/MediaService.java index eb67c18c1c..8f88763831 100644 --- a/src/main/java/com/owncloud/android/media/MediaService.java +++ b/src/main/java/com/owncloud/android/media/MediaService.java @@ -508,7 +508,6 @@ public class MediaService extends Service implements OnCompletionListener, OnPre // nobody is bound processStopRequest(true); } - return; } From e29ca8091d5c17b5c54eb9fd560027119ad3dbe2 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Mon, 15 May 2017 23:22:13 +0200 Subject: [PATCH 2/7] Use one line for each declaration, it enhances code readability (codacy) --- .../com/owncloud/android/authentication/AccountUtils.java | 5 ++++- .../com/owncloud/android/files/services/IndexedForest.java | 3 ++- src/main/java/com/owncloud/android/ui/CustomPopup.java | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/owncloud/android/authentication/AccountUtils.java b/src/main/java/com/owncloud/android/authentication/AccountUtils.java index 998ec5d452..4e0b99ad09 100644 --- a/src/main/java/com/owncloud/android/authentication/AccountUtils.java +++ b/src/main/java/com/owncloud/android/authentication/AccountUtils.java @@ -208,7 +208,10 @@ public class AccountUtils { if (currentAccountVersion == null) { Log_OC.i(TAG, "Upgrading accounts to account version #" + ACCOUNT_VERSION); Account[] ocAccounts = accountMgr.getAccountsByType(MainApp.getAccountType()); - String serverUrl, username, newAccountName, password; + String serverUrl; + String username; + String newAccountName; + String password; Account newAccount; for (Account account : ocAccounts) { // build new account name diff --git a/src/main/java/com/owncloud/android/files/services/IndexedForest.java b/src/main/java/com/owncloud/android/files/services/IndexedForest.java index a3c051abe6..dbeb8a225b 100644 --- a/src/main/java/com/owncloud/android/files/services/IndexedForest.java +++ b/src/main/java/com/owncloud/android/files/services/IndexedForest.java @@ -115,7 +115,8 @@ public class IndexedForest { String currentPath = remotePath; String parentPath = null; String parentKey = null; - Node currentNode = valuedNode, parentNode = null; + Node currentNode = valuedNode; + Node parentNode = null; boolean linked = false; while (!OCFile.ROOT_PATH.equals(currentPath) && !linked) { parentPath = new File(currentPath).getParent(); diff --git a/src/main/java/com/owncloud/android/ui/CustomPopup.java b/src/main/java/com/owncloud/android/ui/CustomPopup.java index 88c7ee2c1c..24d302edc4 100644 --- a/src/main/java/com/owncloud/android/ui/CustomPopup.java +++ b/src/main/java/com/owncloud/android/ui/CustomPopup.java @@ -136,7 +136,8 @@ public class CustomPopup { LayoutParams.WRAP_CONTENT)); root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - int rootW = root.getWidth(), rootH = root.getHeight(); + int rootW = root.getWidth(); + int rootH = root.getHeight(); int screenW = mWManager.getDefaultDisplay().getWidth(); int xpos = ((screenW - rootW) / 2) + x; From b44320076b7cc709a169ac8cefa91d958aacb90d Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Mon, 15 May 2017 23:23:54 +0200 Subject: [PATCH 3/7] Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes (codacy) --- .../files/InstantUploadBroadcastReceiver.java | 14 +++++++------- .../ui/dialog/CreateFolderDialogFragment.java | 5 ++--- .../ui/dialog/RenameFileDialogFragment.java | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/owncloud/android/files/InstantUploadBroadcastReceiver.java b/src/main/java/com/owncloud/android/files/InstantUploadBroadcastReceiver.java index 34b07b64d4..977b090077 100644 --- a/src/main/java/com/owncloud/android/files/InstantUploadBroadcastReceiver.java +++ b/src/main/java/com/owncloud/android/files/InstantUploadBroadcastReceiver.java @@ -56,6 +56,13 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { // http://developer.android.com/reference/android/hardware/Camera.html#ACTION_NEW_VIDEO private static final String NEW_VIDEO_ACTION = "android.hardware.action.NEW_VIDEO"; + /** + * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that + * handleNewPictureAction is called twice for the same photo. Use this simple static variable to + * remember last uploaded photo to filter duplicates. Must not be null! + */ + static String lastUploadedPhotoPath = ""; + @Override public void onReceive(Context context, Intent intent) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { @@ -75,13 +82,6 @@ public class InstantUploadBroadcastReceiver extends BroadcastReceiver { } } - /** - * Because we support NEW_PHOTO_ACTION and NEW_PHOTO_ACTION_UNOFFICIAL it can happen that - * handleNewPictureAction is called twice for the same photo. Use this simple static variable to - * remember last uploaded photo to filter duplicates. Must not be null! - */ - static String lastUploadedPhotoPath = ""; - private void handleNewPictureAction(Context context, Intent intent) { Cursor c = null; String file_path = null; diff --git a/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.java b/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.java index e1c6b9b8b0..774889382f 100644 --- a/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.java +++ b/src/main/java/com/owncloud/android/ui/dialog/CreateFolderDialogFragment.java @@ -49,6 +49,8 @@ public class CreateFolderDialogFragment public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT"; + private OCFile mParentFolder; + /** * Public factory method to create new CreateFolderDialogFragment instances. * @@ -63,9 +65,6 @@ public class CreateFolderDialogFragment return frag; } - - private OCFile mParentFolder; - @Override public Dialog onCreateDialog(Bundle savedInstanceState) { diff --git a/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java b/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java index 2b0035a8de..817f1faf78 100644 --- a/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java +++ b/src/main/java/com/owncloud/android/ui/dialog/RenameFileDialogFragment.java @@ -54,6 +54,8 @@ public class RenameFileDialogFragment private static final String ARG_TARGET_FILE = "TARGET_FILE"; + private OCFile mTargetFile; + /** * Public factory method to create new RenameFileDialogFragment instances. * @@ -69,8 +71,6 @@ public class RenameFileDialogFragment } - private OCFile mTargetFile; - @Override public Dialog onCreateDialog(Bundle savedInstanceState) { mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE); From 03a81d902d4f2e6498bc0de1efd6d38299a105f5 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Mon, 15 May 2017 23:27:13 +0200 Subject: [PATCH 4/7] An instanceof check is being performed on the caught exception. Create a separate catch clause for this exception type. (codacy) --- .../owncloud/android/datamodel/ThumbnailsCacheManager.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java index ccf351f71a..94e09a1e1c 100644 --- a/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java +++ b/src/main/java/com/owncloud/android/datamodel/ThumbnailsCacheManager.java @@ -425,12 +425,13 @@ public class ThumbnailsCacheManager { thumbnail = doFileInBackground(mFile); } } + } // the app should never break due to a problem with thumbnails + catch (OutOfMemoryError t) { + Log_OC.e(TAG, "Generation of thumbnail for " + mFile.getAbsolutePath() + " failed", t); + System.gc(); } catch (Throwable t) { // the app should never break due to a problem with thumbnails Log_OC.e(TAG, "Generation of thumbnail for " + mFile.getAbsolutePath() + " failed", t); - if (t instanceof OutOfMemoryError) { - System.gc(); - } } return thumbnail; From a14401a2e4cab59157d568dfe98e270d67be59e3 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Tue, 16 May 2017 00:08:04 +0200 Subject: [PATCH 5/7] extract ContentValue object creation to private methods --- .../datamodel/FileDataStorageManager.java | 135 +++++++++--------- 1 file changed, 69 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java index bf027041de..fad4b094b8 100644 --- a/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java +++ b/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java @@ -279,49 +279,18 @@ public class FileDataStorageManager { Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove"); - ArrayList operations = - new ArrayList(updatedFiles.size()); + ArrayList operations = new ArrayList<>(updatedFiles.size()); // prepare operations to insert or update files to save in the given folder for (OCFile file : updatedFiles) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); - cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - file.getModificationTimestampAtLastSyncForData() - ); - cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); - cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); - //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); - cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); - cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); - if (!file.isFolder()) { - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); - } - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); - cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isAvailableOffline() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); - cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail()); - cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); - cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); - cv.put(ProviderTableMeta.FILE_FAVORITE, file.getIsFavorite()); + ContentValues cv = createContentValueForFile(file, folder); boolean existsByPath = fileExists(file.getRemotePath()); if (existsByPath || fileExists(file.getFileId())) { // updating an existing file - operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). - withValues(cv). - withSelection(ProviderTableMeta._ID + "=?", - new String[]{String.valueOf(file.getFileId())}) + operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI) + .withValues(cv) + .withSelection(ProviderTableMeta._ID + "=?", new String[]{String.valueOf(file.getFileId())}) .build()); } else { @@ -332,17 +301,14 @@ public class FileDataStorageManager { } // prepare operations to remove files in the given folder - String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + - ProviderTableMeta.FILE_PATH + "=?"; + String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + AND + ProviderTableMeta.FILE_PATH + "=?"; String [] whereArgs = null; for (OCFile file : filesToRemove) { if (file.getParentId() == folder.getFileId()) { whereArgs = new String[]{mAccount.name, file.getRemotePath()}; if (file.isFolder()) { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId( - ProviderTableMeta.CONTENT_URI_DIR, file.getFileId() - ) + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId()) ).withSelection(where, whereArgs).build()); File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file)); @@ -351,8 +317,7 @@ public class FileDataStorageManager { } } else { operations.add(ContentProviderOperation.newDelete( - ContentUris.withAppendedId( - ProviderTableMeta.CONTENT_URI_FILE, file.getFileId() + ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId() ) ).withSelection(where, whereArgs).build()); @@ -366,29 +331,7 @@ public class FileDataStorageManager { } // update metadata of folder - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); - cv.put( - ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, - folder.getModificationTimestampAtLastSyncForData() - ); - cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); - cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); - cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype()); - cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName()); - cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); - cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); - cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); - cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); - cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.isAvailableOffline() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); - cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0); - cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); - cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); - cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); - cv.put(ProviderTableMeta.FILE_FAVORITE, folder.getIsFavorite()); + ContentValues cv = createContentValueForFile(folder); operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI). withValues(cv). @@ -431,7 +374,67 @@ public class FileDataStorageManager { } } } + } + private ContentValues createContentValueForFile(OCFile folder) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp()); + cv.put( + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + folder.getModificationTimestampAtLastSyncForData() + ); + cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp()); + cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); + cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype()); + cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName()); + cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId()); + cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath()); + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); + cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties()); + cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData()); + cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.isAvailableOffline() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag()); + cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, folder.isSharedViaLink() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, folder.isSharedWithSharee() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink()); + cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions()); + cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId()); + cv.put(ProviderTableMeta.FILE_FAVORITE, folder.getIsFavorite()); + return cv; + } + + private ContentValues createContentValueForFile(OCFile file, OCFile folder) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp()); + cv.put( + ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, + file.getModificationTimestampAtLastSyncForData() + ); + cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp()); + cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength()); + cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype()); + cv.put(ProviderTableMeta.FILE_NAME, file.getFileName()); + //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId()); + cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId()); + cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath()); + if (!file.isFolder()) { + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath()); + } + cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name); + cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties()); + cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData()); + cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.isAvailableOffline() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag()); + cv.put(ProviderTableMeta.FILE_SHARED_VIA_LINK, file.isSharedViaLink() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_SHARED_WITH_SHAREE, file.isSharedWithSharee() ? 1 : 0); + cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink()); + cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions()); + cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId()); + cv.put(ProviderTableMeta.FILE_UPDATE_THUMBNAIL, file.needsUpdateThumbnail()); + cv.put(ProviderTableMeta.FILE_IS_DOWNLOADING, file.isDownloading()); + cv.put(ProviderTableMeta.FILE_ETAG_IN_CONFLICT, file.getEtagInConflict()); + cv.put(ProviderTableMeta.FILE_FAVORITE, file.getIsFavorite()); + return cv; } From 0fa8df201def128e4981c7460bc07060f44236ff Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Tue, 16 May 2017 00:18:24 +0200 Subject: [PATCH 6/7] A switch statement does not contain a break (codacy) --- .../java/com/owncloud/android/ui/activity/DrawerActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java index 810628d322..559c660313 100644 --- a/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java @@ -508,6 +508,7 @@ public abstract class DrawerActivity extends ToolbarActivity implements DisplayU case Menu.NONE: // account clicked accountClicked(menuItem.getTitle().toString()); + break; default: Log_OC.i(TAG, "Unknown drawer menu item clicked: " + menuItem.getTitle()); } From 766a691a1970ac190e85c6a1e9e9cc7f4ba74cd9 Mon Sep 17 00:00:00 2001 From: AndyScherzinger Date: Tue, 16 May 2017 00:20:35 +0200 Subject: [PATCH 7/7] A switch statement does not contain a break (codacy) --- .../com/owncloud/android/ui/activity/FileDisplayActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java index 8286dd1ceb..781f26f7ed 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -757,7 +757,7 @@ public class FileDisplayActivity extends HookActivity R.drawable.ic_view_list)); getListOfFilesFragment().setGridAsPreferred(); } - return true; + break; } default: retval = super.onOptionsItemSelected(item);