Skip to content

Commit

Permalink
Merge branch 'central-dev' into central-master
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbWatershed committed Dec 8, 2019
2 parents 71636fe + 450d30a commit b9a3f63
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
//noinspection ExpiringTargetSdkVersion
targetSdkVersion 28
versionCode 130 // is updated automatically by BitRise; only used when building locally
versionName '1.10.0'
versionName '1.10.1'

def fkToken = '\"' + (System.getenv("FK_TOKEN")?: "") + '\"'
def includeObjectBoxBrowser = System.getenv("INCLUDE_OBJECTBOX_BROWSER") ?: "false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import me.devsaki.hentoid.json.JsonContent;
import timber.log.Timber;

import static me.devsaki.hentoid.util.FileHelper.deleteQuietly;
import static me.devsaki.hentoid.util.FileHelper.getDefaultDir;
import static me.devsaki.hentoid.util.FileHelper.getExtSdCardFolder;
import static me.devsaki.hentoid.util.FileHelper.isSAF;
import static me.devsaki.hentoid.util.FileUtil.deleteQuietly;

/**
* Utility class for Content-related operations
Expand Down
58 changes: 1 addition & 57 deletions app/src/main/java/me/devsaki/hentoid/util/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,36 +337,13 @@ public static boolean removeFile(File target) {
*/
public static boolean cleanDirectory(@NonNull File target) {
try {
return tryCleanDirectory(target);
return FileUtil.tryCleanDirectory(target);
} catch (Exception e) {
Timber.e(e, "Failed to clean directory");
return false;
}
}

/**
* Cleans a directory without deleting it.
* <p>
* Custom substitute for commons.io.FileUtils.cleanDirectory that supports devices without File.toPath
*
* @param directory directory to clean
* @return true if directory has been successfully cleaned
* @throws IOException in case cleaning is unsuccessful
*/
private static boolean tryCleanDirectory(@NonNull File directory) throws IOException {
File[] files = directory.listFiles();
if (files == null) throw new IOException("Failed to list content of " + directory);

boolean isSuccess = true;

for (File file : files) {
if (file.isDirectory() && !tryCleanDirectory(file)) isSuccess = false;
if (!file.delete() && file.exists()) isSuccess = false;
}

return isSuccess;
}

public static boolean checkAndSetRootFolder(String folder) {
return checkAndSetRootFolder(folder, false);
}
Expand Down Expand Up @@ -529,39 +506,6 @@ public static void saveBinaryInFile(File file, byte[] binaryContent) throws IOEx
}
}

/**
* Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.
* <p>
* The difference between File.delete() and this method are:
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
* <li>No exceptions are thrown when a file or directory cannot be deleted.</li>
* </ul>
* <p>
* Custom substitute for commons.io.FileUtils.deleteQuietly that works with devices that doesn't support File.toPath
*
* @param file file or directory to delete, can be {@code null}
* @return {@code true} if the file or directory was deleted, otherwise
* {@code false}
*/
static boolean deleteQuietly(final File file) {
if (file == null) {
return false;
}
try {
if (file.isDirectory()) {
tryCleanDirectory(file);
}
} catch (final Exception ignored) {
}

try {
return file.delete();
} catch (final Exception ignored) {
return false;
}
}

public static boolean renameDirectory(File srcDir, File destDir) {
try {
FileUtils.moveDirectory(srcDir, destDir);
Expand Down
60 changes: 58 additions & 2 deletions app/src/main/java/me/devsaki/hentoid/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ static InputStream getInputStream(@NonNull final File target) throws IOException
/**
* Create a file.
*
* @param file The file to be created.
* @param file The file to be created.
* @param forceWrite Force writing operation even if an existing file is found (useful for I/O tests)
* @return true if creation was successful.
*/
Expand Down Expand Up @@ -320,7 +320,63 @@ static boolean makeDir(@NonNull final File file) {
* @return true if successfully deleted or if the file does not exist.
*/
static boolean deleteFile(@NonNull final File file) {
return !file.exists() || FileUtils.deleteQuietly(file) || deleteWithSAF(file);
return !file.exists() || deleteQuietly(file) || deleteWithSAF(file);
}

/**
* Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.
* <p>
* The difference between File.delete() and this method are:
* <ul>
* <li>A directory to be deleted does not have to be empty.</li>
* <li>No exceptions are thrown when a file or directory cannot be deleted.</li>
* </ul>
* <p>
* Custom substitute for commons.io.FileUtils.deleteQuietly that works with devices that doesn't support File.toPath
*
* @param file file or directory to delete, can be {@code null}
* @return {@code true} if the file or directory was deleted, otherwise
* {@code false}
*/
static boolean deleteQuietly(final File file) {
if (file == null) {
return false;
}
try {
if (file.isDirectory()) {
tryCleanDirectory(file);
}
} catch (final Exception ignored) {
}

try {
return file.delete();
} catch (final Exception ignored) {
return false;
}
}

/**
* Cleans a directory without deleting it.
* <p>
* Custom substitute for commons.io.FileUtils.cleanDirectory that supports devices without File.toPath
*
* @param directory directory to clean
* @return true if directory has been successfully cleaned
* @throws IOException in case cleaning is unsuccessful
*/
static boolean tryCleanDirectory(@NonNull File directory) throws IOException {
File[] files = directory.listFiles();
if (files == null) throw new IOException("Failed to list content of " + directory);

boolean isSuccess = true;

for (File file : files) {
if (file.isDirectory() && !tryCleanDirectory(file)) isSuccess = false;
if (!file.delete() && file.exists()) isSuccess = false;
}

return isSuccess;
}

static boolean deleteWithSAF(File file) {
Expand Down

0 comments on commit b9a3f63

Please sign in to comment.