Browse Source

spotbugs: Exceptional return value ignored

use lombok

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
Andy Scherzinger 6 years ago
parent
commit
d41a8a9054

+ 9 - 5
src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -369,8 +369,9 @@ public class FileDataStorageManager {
 
                     if (file.isDown()) {
                         String path = file.getStoragePath();
-                        new File(path).delete();
-                        triggerMediaScan(path); // notify MediaScanner about removed file
+                        if (new File(path).delete()) {
+                            triggerMediaScan(path); // notify MediaScanner about removed file
+                        }
                     }
                 }
             }
@@ -727,7 +728,9 @@ public class FileDataStorageManager {
                 File targetFile = new File(targetLocalPath);
                 File targetFolder = targetFile.getParentFile();
                 if (!targetFolder.exists()) {
-                    targetFolder.mkdirs();
+                    if (!targetFolder.mkdirs()) {
+                        Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath());
+                    }
                 }
                 renamed = localFile.renameTo(targetFile);
             }
@@ -745,7 +748,6 @@ public class FileDataStorageManager {
                 }
             }
         }
-
     }
 
     public void copyLocalFile(OCFile file, String targetPath) {
@@ -759,7 +761,9 @@ public class FileDataStorageManager {
                 File targetFile = new File(defaultSavePath + targetPath);
                 File targetFolder = targetFile.getParentFile();
                 if (!targetFolder.exists()) {
-                    targetFolder.mkdirs();
+                    if (!targetFolder.mkdirs()) {
+                        Log_OC.e(TAG, "Unable to create parent folder " + targetFolder.getAbsolutePath());
+                    }
                 }
                 copied = FileStorageUtils.copyFile(localFile, targetFile);
             }

+ 66 - 83
src/main/java/com/owncloud/android/operations/DownloadFileOperation.java

@@ -45,26 +45,32 @@ import java.util.Iterator;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import lombok.Getter;
+
 /**
- * Remote mDownloadOperation performing the download of a file to an ownCloud server
+ * Remote DownloadOperation performing the download of a file to an ownCloud server
  */
 public class DownloadFileOperation extends RemoteOperation {
 
     private static final String TAG = DownloadFileOperation.class.getSimpleName();
-    private Account mAccount;
-
-    private OCFile mFile;
-    private String mBehaviour;
-    private Context mContext;
-    private Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<>();
-    private long mModificationTimestamp;
-    private String mEtag = "";
-    private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
-
-    private DownloadFileRemoteOperation mDownloadOperation;
-    private String mActivityName;
-    private String mPackageName;
-
+    @Getter
+    private Account account;
+    @Getter
+    private OCFile file;
+    @Getter
+    private String behaviour;
+    private Context context;
+    private Set<OnDatatransferProgressListener> dataTransferListeners = new HashSet<>();
+    private long modificationTimestamp;
+    @Getter
+    private String etag = "";
+    private final AtomicBoolean cancellationRequested = new AtomicBoolean(false);
+
+    private DownloadFileRemoteOperation downloadOperation;
+    @Getter
+    private String activityName;
+    @Getter
+    private String packageName;
 
     public DownloadFileOperation(Account account, OCFile file, String behaviour, String activityName,
                                  String packageName, Context context) {
@@ -77,60 +83,47 @@ public class DownloadFileOperation extends RemoteOperation {
                     "creation");
         }
 
-        mAccount = account;
-        mFile = file;
-        mBehaviour = behaviour;
-        mActivityName = activityName;
-        mPackageName = packageName;
-        mContext = context;
-    }
-
-
-    public Account getAccount() {
-        return mAccount;
-    }
-
-    public OCFile getFile() {
-        return mFile;
-    }
-
-    public String getBehaviour() {
-        return mBehaviour;
+        this.account = account;
+        this.file = file;
+        this.behaviour = behaviour;
+        this.activityName = activityName;
+        this.packageName = packageName;
+        this.context = context;
     }
 
     public String getSavePath() {
-        if (mFile.getStoragePath() != null) {
-            File path = new File(mFile.getStoragePath());  // re-downloads should be done over the original file
+        if (file.getStoragePath() != null) {
+            File path = new File(file.getStoragePath());  // re-downloads should be done over the original file
             if (path.canWrite()) {
                 return path.getAbsolutePath();
             }
         }
-        return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
+        return FileStorageUtils.getDefaultSavePathFor(account.name, file);
     }
 
     public String getTmpPath() {
-        return FileStorageUtils.getTemporalPath(mAccount.name) + mFile.getRemotePath();
+        return FileStorageUtils.getTemporalPath(account.name) + file.getRemotePath();
     }
 
     public String getTmpFolder() {
-        return FileStorageUtils.getTemporalPath(mAccount.name);
+        return FileStorageUtils.getTemporalPath(account.name);
     }
 
     public String getRemotePath() {
-        return mFile.getRemotePath();
+        return file.getRemotePath();
     }
 
     public String getMimeType() {
-        String mimeType = mFile.getMimeType();
+        String mimeType = file.getMimeType();
         if (mimeType == null || mimeType.length() <= 0) {
             try {
                 mimeType = MimeTypeMap.getSingleton()
                     .getMimeTypeFromExtension(
-                            mFile.getRemotePath().substring(
-                                    mFile.getRemotePath().lastIndexOf('.') + 1));
+                        file.getRemotePath().substring(
+                            file.getRemotePath().lastIndexOf('.') + 1));
             } catch (IndexOutOfBoundsException e) {
                 Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " +
-                        mFile.getRemotePath());
+                        file.getRemotePath());
             }
         }
         if (mimeType == null) {
@@ -140,22 +133,18 @@ public class DownloadFileOperation extends RemoteOperation {
     }
 
     public long getSize() {
-        return mFile.getFileLength();
+        return file.getFileLength();
     }
 
     public long getModificationTimestamp() {
-        return mModificationTimestamp > 0 ? mModificationTimestamp : mFile.getModificationTimestamp();
-    }
-
-    public String getEtag() {
-        return mEtag;
+        return modificationTimestamp > 0 ? modificationTimestamp : file.getModificationTimestamp();
     }
 
     @Override
     protected RemoteOperationResult run(OwnCloudClient client) {
         /// perform the download
-        synchronized(mCancellationRequested) {
-            if (mCancellationRequested.get()) {
+        synchronized(cancellationRequested) {
+            if (cancellationRequested.get()) {
                 return new RemoteOperationResult(new OperationCancelledException());
             }
         }
@@ -169,36 +158,38 @@ public class DownloadFileOperation extends RemoteOperation {
 
         String tmpFolder =  getTmpFolder();
 
-        mDownloadOperation = new DownloadFileRemoteOperation(mFile.getRemotePath(), tmpFolder);
-        Iterator<OnDatatransferProgressListener> listener = mDataTransferListeners.iterator();
+        downloadOperation = new DownloadFileRemoteOperation(file.getRemotePath(), tmpFolder);
+        Iterator<OnDatatransferProgressListener> listener = dataTransferListeners.iterator();
         while (listener.hasNext()) {
-            mDownloadOperation.addDatatransferProgressListener(listener.next());
+            downloadOperation.addDatatransferProgressListener(listener.next());
         }
-        result = mDownloadOperation.execute(client, client.isUseNextcloudUserAgent());
+        result = downloadOperation.execute(client, client.isUseNextcloudUserAgent());
 
         if (result.isSuccess()) {
-            mModificationTimestamp = mDownloadOperation.getModificationTimestamp();
-            mEtag = mDownloadOperation.getEtag();
+            modificationTimestamp = downloadOperation.getModificationTimestamp();
+            etag = downloadOperation.getEtag();
             newFile = new File(getSavePath());
-            newFile.getParentFile().mkdirs();
+            if (!newFile.getParentFile().mkdirs()) {
+                Log_OC.e(TAG, "Unable to create parent folder " + newFile.getParentFile().getAbsolutePath());
+            }
 
             // decrypt file
-            if (mFile.isEncrypted() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
-                FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(mAccount, mContext.getContentResolver());
+            if (file.isEncrypted() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
+                FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(account, context.getContentResolver());
 
-                OCFile parent = fileDataStorageManager.getFileByPath(mFile.getParentRemotePath());
+                OCFile parent = fileDataStorageManager.getFileByPath(file.getParentRemotePath());
 
-                DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, mContext, mAccount);
+                DecryptedFolderMetadata metadata = EncryptionUtils.downloadFolderMetadata(parent, client, context, account);
 
                 if (metadata == null) {
                     return new RemoteOperationResult(RemoteOperationResult.ResultCode.METADATA_NOT_FOUND);
                 }
                 byte[] key = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
-                        .get(mFile.getEncryptedFileName()).getEncrypted().getKey());
+                        .get(file.getEncryptedFileName()).getEncrypted().getKey());
                 byte[] iv = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
-                        .get(mFile.getEncryptedFileName()).getInitializationVector());
+                        .get(file.getEncryptedFileName()).getInitializationVector());
                 byte[] authenticationTag = EncryptionUtils.decodeStringToBase64Bytes(metadata.getFiles()
-                        .get(mFile.getEncryptedFileName()).getAuthenticationTag());
+                        .get(file.getEncryptedFileName()).getAuthenticationTag());
 
                 try {
                     byte[] decryptedBytes = EncryptionUtils.decryptFile(tmpFile, key, iv, authenticationTag);
@@ -211,42 +202,34 @@ public class DownloadFileOperation extends RemoteOperation {
                 }
             }
             moved = tmpFile.renameTo(newFile);
-            newFile.setLastModified(mFile.getModificationTimestamp());
+            newFile.setLastModified(file.getModificationTimestamp());
             if (!moved) {
                 result = new RemoteOperationResult(RemoteOperationResult.ResultCode.LOCAL_STORAGE_NOT_MOVED);
             }
         }
-        Log_OC.i(TAG, "Download of " + mFile.getRemotePath() + " to " + getSavePath() + ": " +
+        Log_OC.i(TAG, "Download of " + file.getRemotePath() + " to " + getSavePath() + ": " +
                 result.getLogMessage());
 
         return result;
     }
 
     public void cancel() {
-        mCancellationRequested.set(true);   // atomic set; there is no need of synchronizing it
-        if (mDownloadOperation != null) {
-            mDownloadOperation.cancel();
+        cancellationRequested.set(true);   // atomic set; there is no need of synchronizing it
+        if (downloadOperation != null) {
+            downloadOperation.cancel();
         }
     }
 
 
     public void addDatatransferProgressListener (OnDatatransferProgressListener listener) {
-        synchronized (mDataTransferListeners) {
-            mDataTransferListeners.add(listener);
+        synchronized (dataTransferListeners) {
+            dataTransferListeners.add(listener);
         }
     }
 
     public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
-        synchronized (mDataTransferListeners) {
-            mDataTransferListeners.remove(listener);
+        synchronized (dataTransferListeners) {
+            dataTransferListeners.remove(listener);
         }
     }
-
-    public String getActivityName() {
-        return mActivityName;
-    }
-
-    public String getPackageName() {
-        return mPackageName;
-    }
 }

+ 34 - 35
src/main/java/com/owncloud/android/operations/RenameFileOperation.java

@@ -34,6 +34,8 @@ import com.owncloud.android.utils.FileStorageUtils;
 import java.io.File;
 import java.io.IOException;
 
+import lombok.Getter;
+
 
 /**
  * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
@@ -42,10 +44,11 @@ public class RenameFileOperation extends SyncOperation {
 
     private static final String TAG = RenameFileOperation.class.getSimpleName();
 
-    private OCFile mFile;
-    private String mRemotePath;
-    private String mNewName;
-    private String mNewRemotePath;
+    @Getter
+    private OCFile file;
+    private String remotePath;
+    private String newName;
+    private String newRemotePath;
 
     /**
      * Constructor
@@ -55,16 +58,10 @@ public class RenameFileOperation extends SyncOperation {
      * @param newName               New name to set as the name of file.
      */
     public RenameFileOperation(String remotePath, String newName) {
-        mRemotePath = remotePath;
-        mNewName = newName;
-        mNewRemotePath = null;
-    }
-
-    public OCFile getFile() {
-        return mFile;
+        this.remotePath = remotePath;
+        this.newName = newName;
     }
 
-
     /**
      * Performs the rename operation.
      *
@@ -74,31 +71,31 @@ public class RenameFileOperation extends SyncOperation {
     protected RemoteOperationResult run(OwnCloudClient client) {
         RemoteOperationResult result = null;
 
-        mFile = getStorageManager().getFileByPath(mRemotePath);
+        file = getStorageManager().getFileByPath(remotePath);
 
         // check if the new name is valid in the local file system
         try {
             if (!isValidNewName()) {
                 return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
             }
-            String parent = new File(mFile.getRemotePath()).getParent();
+            String parent = new File(file.getRemotePath()).getParent();
             parent = parent.endsWith(OCFile.PATH_SEPARATOR) ? parent : parent + OCFile.PATH_SEPARATOR;
-            mNewRemotePath =  parent + mNewName;
-            if (mFile.isFolder()) {
-                mNewRemotePath += OCFile.PATH_SEPARATOR;
+            newRemotePath =  parent + newName;
+            if (file.isFolder()) {
+                newRemotePath += OCFile.PATH_SEPARATOR;
             }
 
             // check local overwrite
-            if (getStorageManager().getFileByPath(mNewRemotePath) != null) {
+            if (getStorageManager().getFileByPath(newRemotePath) != null) {
                 return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
             }
 
-            result = new RenameFileRemoteOperation(mFile.getFileName(), mFile.getRemotePath(), mNewName,
-                mFile.isFolder()).execute(client);
+            result = new RenameFileRemoteOperation(file.getFileName(), file.getRemotePath(), newName,
+                                                   file.isFolder()).execute(client);
 
             if (result.isSuccess()) {
-                if (mFile.isFolder()) {
-                    getStorageManager().moveLocalFile(mFile, mNewRemotePath, parent);
+                if (file.isFolder()) {
+                    getStorageManager().moveLocalFile(file, newRemotePath, parent);
                     //saveLocalDirectory();
 
                 } else {
@@ -107,8 +104,8 @@ public class RenameFileOperation extends SyncOperation {
             }
 
         } catch (IOException e) {
-            Log_OC.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((mNewRemotePath==null) ?
-                    mNewName : mNewRemotePath) + ": " +
+            Log_OC.e(TAG, "Rename " + file.getRemotePath() + " to " + ((newRemotePath ==null) ?
+                newName : newRemotePath) + ": " +
                     (result!= null ? result.getLogMessage() : ""), e);
         }
 
@@ -116,19 +113,19 @@ public class RenameFileOperation extends SyncOperation {
     }
 
     private void saveLocalFile() {
-        mFile.setFileName(mNewName);
+        file.setFileName(newName);
 
         // try to rename the local copy of the file
-        if (mFile.isDown()) {
-            String oldPath = mFile.getStoragePath();
+        if (file.isDown()) {
+            String oldPath = file.getStoragePath();
             File f = new File(oldPath);
             String parentStoragePath = f.getParent();
             if (!parentStoragePath.endsWith(File.separator)) {
                 parentStoragePath += File.separator;
             }
-            if (f.renameTo(new File(parentStoragePath + mNewName))) {
-                String newPath = parentStoragePath + mNewName;
-                mFile.setStoragePath(newPath);
+            if (f.renameTo(new File(parentStoragePath + newName))) {
+                String newPath = parentStoragePath + newName;
+                file.setStoragePath(newPath);
 
                 // notify MediaScanner about removed file
                 getStorageManager().deleteFileInMediaScan(oldPath);
@@ -140,7 +137,7 @@ public class RenameFileOperation extends SyncOperation {
             // TODO - study conditions when this could be a problem
         }
 
-        getStorageManager().saveFile(mFile);
+        getStorageManager().saveFile(file);
     }
 
     /**
@@ -161,14 +158,16 @@ public class RenameFileOperation extends SyncOperation {
      */
     private boolean isValidNewName() throws IOException {
         // check tricky names
-        if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator)) {
+        if (newName == null || newName.length() <= 0 || newName.contains(File.separator)) {
             return false;
         }
         // create a test file
         String tmpFolderName = FileStorageUtils.getTemporalPath("");
-        File testFile = new File(tmpFolderName + mNewName);
+        File testFile = new File(tmpFolderName + newName);
         File tmpFolder = testFile.getParentFile();
-        tmpFolder.mkdirs();
+        if (! tmpFolder.mkdirs()) {
+            Log_OC.e(TAG, "Unable to create parent folder " + tmpFolder.getAbsolutePath());
+        }
         if (!tmpFolder.isDirectory()) {
             throw new IOException("Unexpected error: temporal directory could not be created");
         }
@@ -176,7 +175,7 @@ public class RenameFileOperation extends SyncOperation {
             testFile.createNewFile();   // return value is ignored; it could be 'false' because
             // the file already existed, that doesn't invalidate the name
         } catch (IOException e) {
-            Log_OC.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
+            Log_OC.i(TAG, "Test for validity of name " + newName + " in the file system failed");
             return false;
         }
         boolean result = testFile.exists() && testFile.isFile();

+ 0 - 2
src/main/java/com/owncloud/android/ui/activity/ManageSpaceActivity.java

@@ -132,9 +132,7 @@ public class ManageSpaceActivity extends AppCompatActivity implements Injectable
                 ).show();
             } else {
                 finish();
-                System.exit(0);
             }
-
         }
 
         public boolean clearApplicationData() {