diff --git a/android/android-base/src/main/java/org/isoron/androidbase/AndroidDirFinder.kt b/android/android-base/src/main/java/org/isoron/androidbase/AndroidDirFinder.kt index 9dbeeeb65..ec1d6d783 100644 --- a/android/android-base/src/main/java/org/isoron/androidbase/AndroidDirFinder.kt +++ b/android/android-base/src/main/java/org/isoron/androidbase/AndroidDirFinder.kt @@ -25,7 +25,7 @@ import java.io.File import javax.inject.Inject class AndroidDirFinder @Inject constructor(@param:AppContext private val context: Context) { - fun getFilesDir(relativePath: String?): File? { + fun getFilesDir(relativePath: String): File? { return FileUtils.getDir( ContextCompat.getExternalFilesDirs(context, null), relativePath diff --git a/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.java b/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.java deleted file mode 100644 index b04f969b8..000000000 --- a/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2017 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.androidbase.utils; - -import android.os.*; -import android.util.*; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.io.*; - -public abstract class FileUtils -{ - public static void copy(File src, File dst) throws IOException - { - FileInputStream inStream = new FileInputStream(src); - FileOutputStream outStream = new FileOutputStream(dst); - copy(inStream, outStream); - } - - public static void copy(InputStream inStream, File dst) throws IOException - { - FileOutputStream outStream = new FileOutputStream(dst); - copy(inStream, outStream); - } - - public static void copy(InputStream in, OutputStream out) throws IOException - { - int numBytes; - byte[] buffer = new byte[1024]; - - while ((numBytes = in.read(buffer)) != -1) - out.write(buffer, 0, numBytes); - } - - @Nullable - public static File getDir(@NonNull File potentialParentDirs[], - @Nullable String relativePath) - { - if (relativePath == null) relativePath = ""; - - File chosenDir = null; - for (File dir : potentialParentDirs) - { - if (dir == null || !dir.canWrite()) continue; - chosenDir = dir; - break; - } - - if (chosenDir == null) - { - Log.e("FileUtils", - "getDir: all potential parents are null or non-writable"); - return null; - } - - File dir = new File( - String.format("%s/%s/", chosenDir.getAbsolutePath(), relativePath)); - if (!dir.exists() && !dir.mkdirs()) - { - Log.e("FileUtils", - "getDir: chosen dir does not exist and cannot be created"); - return null; - } - - return dir; - } - - @Nullable - public static File getSDCardDir(@Nullable String relativePath) - { - File parents[] = - new File[]{ Environment.getExternalStorageDirectory() }; - return getDir(parents, relativePath); - } -} diff --git a/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.kt b/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.kt new file mode 100644 index 000000000..7e6b68f88 --- /dev/null +++ b/android/android-base/src/main/java/org/isoron/androidbase/utils/FileUtils.kt @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2017 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.androidbase.utils + +import android.os.Environment +import android.util.Log +import java.io.* + + +fun File.copyTo(dst: File) { + val inStream = FileInputStream(this) + val outStream = FileOutputStream(dst) + inStream.copyTo(outStream) +} + +fun InputStream.copyTo(dst: File) { + val outStream = FileOutputStream(dst) + this.copyTo(outStream) +} + +fun InputStream.copyTo(out: OutputStream) { + var numBytes: Int + val buffer = ByteArray(1024) + while (this.read(buffer).also { numBytes = it } != -1) { + out.write(buffer, 0, numBytes) + } +} + +object FileUtils { + @JvmStatic + fun getDir(potentialParentDirs: Array, relativePath: String): File? { + val chosenDir: File? = potentialParentDirs.firstOrNull { dir -> dir.canWrite() } + if (chosenDir == null) { + Log.e("FileUtils", "getDir: all potential parents are null or non-writable") + return null + } + val dir = File("${chosenDir.absolutePath}/${relativePath}/") + if (!dir.exists() && !dir.mkdirs()) { + Log.e("FileUtils", "getDir: chosen dir does not exist and cannot be created") + return null + } + return dir + } + + @JvmStatic + fun getSDCardDir(relativePath: String): File? { + val parents = arrayOf(Environment.getExternalStorageDirectory()) + return getDir(parents, relativePath) + } +} \ No newline at end of file diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt index bd963f50e..e4c3752a1 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.kt @@ -104,10 +104,10 @@ class ListHabitsScreen if (data == null) return if (resultCode != Activity.RESULT_OK) return try { - val inStream = activity.contentResolver.openInputStream(data.data!!) + val inStream = activity.contentResolver.openInputStream(data.data!!)!! val cacheDir = activity.externalCacheDir val tempFile = File.createTempFile("import", "", cacheDir) - FileUtils.copy(inStream, tempFile) + inStream.copyTo(tempFile) onImportData(tempFile) { tempFile.delete() } } catch (e: IOException) { showMessage(R.string.could_not_import) diff --git a/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java b/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java index 050f9295f..1108ad3d2 100644 --- a/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java +++ b/android/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java @@ -100,7 +100,7 @@ public abstract class DatabaseUtils File db = getDatabaseFile(context); File dbCopy = new File(filename); - FileUtils.copy(db, dbCopy); + FileUtilsKt.copyTo(db, dbCopy); return dbCopy.getAbsolutePath(); }