diff --git a/CHANGELOG.md b/CHANGELOG.md index b3211206fe..7bdbfdff57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. ### Removed ### Fixed +## [0.71.6] +### Fixed +* Fix another crash caused by org.apache.commons.io.FileUtils.moveFile + ## [0.71.5] ### Fixed * Fix crash caused by org.apache.commons.io.FileUtils.moveFile w/ API level below 26 (Android Oreo) diff --git a/core/src/main/java/io/snabble/sdk/ProductDatabase.java b/core/src/main/java/io/snabble/sdk/ProductDatabase.java index 8ef8b8bebb..8b32eba629 100644 --- a/core/src/main/java/io/snabble/sdk/ProductDatabase.java +++ b/core/src/main/java/io/snabble/sdk/ProductDatabase.java @@ -28,6 +28,7 @@ import io.snabble.sdk.codes.templates.CodeTemplate; import io.snabble.sdk.utils.Dispatch; import io.snabble.sdk.utils.Downloader; +import io.snabble.sdk.utils.FileUtilsSupport; import io.snabble.sdk.utils.Logger; import io.snabble.sdk.utils.StringNormalizer; @@ -696,7 +697,7 @@ private void swap(File otherDbFile) throws IOException { File dbFile = application.getDatabasePath(dbName); if (!dbFile.exists() || application.deleteDatabase(dbFile.getName())) { - FileUtils.moveFile(otherDbFile, dbFile); + FileUtilsSupport.moveFile(otherDbFile, dbFile); application.deleteDatabase(otherDbFile.getName()); } @@ -1468,4 +1469,4 @@ public interface UpdateCallback { void error(); } -} \ No newline at end of file +} diff --git a/utils/src/main/java/io/snabble/sdk/utils/FileUtilsSupport.kt b/utils/src/main/java/io/snabble/sdk/utils/FileUtilsSupport.kt new file mode 100644 index 0000000000..62ce3ccd33 --- /dev/null +++ b/utils/src/main/java/io/snabble/sdk/utils/FileUtilsSupport.kt @@ -0,0 +1,58 @@ +@file:JvmName("FileUtilsSupport") + +package io.snabble.sdk.utils + +import android.os.Build +import org.apache.commons.io.FileUtils +import java.io.File +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException + +object FileUtilsSupport { + + @JvmStatic + @Throws(IOException::class) + fun moveFile(srcFile: File, dstFile: File) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + FileUtils.moveFile(srcFile, dstFile) + } else { + moveFileSupport(srcFile, dstFile) + } + } + + private fun moveFileSupport(srcFile: File, dstFile: File) { + try { + val isRenamed = srcFile.renameTo(dstFile) + if (!isRenamed) { + copyFile(srcFile, dstFile) + } + } catch (_: SecurityException) { + Logger.d("No write access to either srcFile, dstFile or both.") + } catch (_: NullPointerException) { + Logger.d("dstFile must not be null.") + } + FileUtils.deleteQuietly(srcFile) + } + + private fun copyFile(srcFile: File, dstFile: File) { + try { + FileOutputStream(dstFile).channel.use { dstChannel -> + FileInputStream(srcFile).channel.use { srcChannel -> + srcChannel.transferTo( + 0, + srcChannel.size(), + dstChannel + ) + } + } + } catch (_: FileNotFoundException) { + Logger.d("Either srcFile, dstFile or both do not exist.") + } catch (_: SecurityException) { + Logger.d("No write access to either srcFile, destFile or both.") + } catch (exception: IOException) { + Logger.e("Could not write srcFile to dstFile: %s", exception.toString()) + } + } +} diff --git a/utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java b/utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java index c5a6e5fbcb..44d842c728 100644 --- a/utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java +++ b/utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java @@ -4,10 +4,6 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Resources; -import android.os.Build; - -import androidx.annotation.NonNull; -import androidx.annotation.RequiresApi; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; @@ -15,11 +11,9 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.nio.channels.FileChannel; import java.nio.charset.Charset; import okhttp3.OkHttpClient; @@ -132,7 +126,7 @@ public synchronized void updateStorage(String content) { fos.close(); FileUtils.deleteQuietly(storageFile); - moveFileSupport(tempFile, storageFile); + FileUtilsSupport.moveFile(tempFile, storageFile); Logger.d("Updated saved data:%s", storageFile.getAbsolutePath()); } catch (IOException e) { Logger.e("Could not update saved data %s", e.toString()); @@ -149,42 +143,4 @@ protected void onResponse(Response response) throws IOException { } protected abstract void onDownloadFinished(String string); - - private void moveFileSupport(final File srcFile, final File destFile) throws IOException { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - FileUtils.moveFile(srcFile, destFile); - } else { - moveFile(srcFile, destFile); - } - } - - private void moveFile(@NonNull final File srcFile, @NonNull final File destFile) { - try { - final boolean isRenamed = srcFile.renameTo(destFile); - if (!isRenamed) { - copyFile(srcFile, destFile); - } - } catch (final SecurityException exception) { - Logger.d("No write access to either srcFile, destFile or both."); - } catch (final NullPointerException exception) { - Logger.d("destFile must not be null."); - } - FileUtils.deleteQuietly(srcFile); - } - - private void copyFile(@NonNull final File srcFile, @NonNull final File destFile) { - //noinspection resource - try ( - final FileChannel dstChannel = new FileOutputStream(destFile).getChannel(); - final FileChannel srcChannel = new FileInputStream(srcFile).getChannel() - ) { - srcChannel.transferTo(0, srcChannel.size(), dstChannel); - } catch (final FileNotFoundException exception) { - Logger.d("Either srcFile, destFile or both do not exist."); - } catch (final SecurityException exception) { - Logger.d("No write access to either srcFile, destFile or both."); - } catch (final IOException exception) { - Logger.e("Could not write srcFile to destFile: %s", exception.toString()); - } - } }