Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/io/snabble/sdk/ProductDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -1468,4 +1469,4 @@ public interface UpdateCallback {

void error();
}
}
}
58 changes: 58 additions & 0 deletions utils/src/main/java/io/snabble/sdk/utils/FileUtilsSupport.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
}
46 changes: 1 addition & 45 deletions utils/src/main/java/io/snabble/sdk/utils/StringDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@
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;
import org.apache.commons.io.IOUtils;

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;
Expand Down Expand Up @@ -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());
Expand All @@ -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());
}
}
}