From b0d0a00f15575c0d6a95fb4b94c82558b67e3002 Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 2 Oct 2023 16:27:53 +0200 Subject: [PATCH] Make database copy activity, only for debug purposes at the moment --- app/src/main/AndroidManifest.xml | 18 ++++ .../android/PokerAnalyticsApplication.kt | 5 +- .../ui/activity/DatabaseCopyActivity.kt | 99 +++++++++++++++++++ .../pokeranalytics/android/util/FileUtils.kt | 16 +++ 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/net/pokeranalytics/android/ui/activity/DatabaseCopyActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f381a06a..d124c6b5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -63,6 +63,24 @@ + + + + + + + + + + + + + + + + + + + val destination = File(path) + inputStream.copyStreamToFile(destination) + toast("Please restart app") + } + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + when (requestCode) { + RequestCode.IMPORT.value -> { + if (resultCode == ResultCode.IMPORT_UNRECOGNIZED_FORMAT.value) { + showAlertDialog(context = this, messageResId = R.string.unknown_import_format_popup_message, positiveAction = { + finish() + }) + } + } + } + } + + // Import + + private fun requestImportConfirmation() { + + showAlertDialog(context = this, title = R.string.import_confirmation, showCancelButton = true, positiveAction = { + initUI() + }, negativeAction = { + finish() + }) + + } + +} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/util/FileUtils.kt b/app/src/main/java/net/pokeranalytics/android/util/FileUtils.kt index 3c0f1bf5..92dce53b 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/FileUtils.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/FileUtils.kt @@ -6,6 +6,7 @@ import timber.log.Timber import java.io.File import java.io.FileOutputStream import java.io.IOException +import java.io.InputStream class FileUtils { @@ -54,4 +55,19 @@ class FileUtils { } +} + +fun InputStream.copyStreamToFile(outputFile: File) { + this.use { input -> + val outputStream = FileOutputStream(outputFile) + outputStream.use { output -> + val buffer = ByteArray(4 * 1024) // buffer size + while (true) { + val byteCount = input.read(buffer) + if (byteCount < 0) break + output.write(buffer, 0, byteCount) + } + output.flush() + } + } } \ No newline at end of file