added download button, but without functions

This commit is contained in:
oxidiert
2025-08-11 10:20:31 +02:00
parent fbe548c8d8
commit 7571b39f7a
4 changed files with 99 additions and 2 deletions

View File

@ -0,0 +1,74 @@
package com.dano.test1
import android.content.Context
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.File
import java.io.FileOutputStream
object DatabaseDownloader {
private const val DB_NAME = "questionnaire_database"
private const val SERVER_DOWNLOAD_URL = "http://49.13.157.44/exported_encrypted_database.db"
private val client = OkHttpClient()
fun downloadAndReplaceDatabase(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
try {
Log.d("DOWNLOAD", "Download gestartet: $SERVER_DOWNLOAD_URL")
val request = Request.Builder()
.url(SERVER_DOWNLOAD_URL)
.build()
val response = client.newCall(request).execute()
if (!response.isSuccessful) {
Log.e("DOWNLOAD", "Fehler beim Download: ${response.code}")
return@launch
}
// Temporäre verschlüsselte Datei speichern
val encryptedFile = File(context.cacheDir, "downloaded_encrypted_database.db")
response.body?.byteStream()?.use { input ->
FileOutputStream(encryptedFile).use { output ->
input.copyTo(output)
}
}
Log.d("DOWNLOAD", "Verschlüsselte Datei gespeichert: ${encryptedFile.absolutePath}")
// Datei entschlüsseln (hier deine echte Entschlüsselungslogik einfügen)
val decryptedFile = File(context.cacheDir, "decrypted_database.db")
decryptDatabase(encryptedFile, decryptedFile)
Log.d("DOWNLOAD", "Datei entschlüsselt: ${decryptedFile.absolutePath}")
// Alte DB ersetzen
val dbFile = context.getDatabasePath(DB_NAME)
if (dbFile.exists()) {
if (dbFile.delete()) {
Log.d("DOWNLOAD", "Alte Datenbank gelöscht.")
} else {
Log.e("DOWNLOAD", "Löschen der alten Datenbank fehlgeschlagen!")
return@launch
}
}
decryptedFile.copyTo(dbFile, overwrite = true)
Log.d("DOWNLOAD", "Neue Datenbank erfolgreich eingesetzt.")
} catch (e: Exception) {
Log.e("DOWNLOAD", "Fehler beim Download oder Ersetzen der DB", e)
}
}
}
// Platzhalter für echte Entschlüsselung
private fun decryptDatabase(encryptedFile: File, outputFile: File) {
// TODO: hier echte Entschlüsselungslogik einfügen
encryptedFile.copyTo(outputFile, overwrite = true)
}
}

View File

@ -85,4 +85,4 @@ object DatabaseUploader {
}
})
}
}
}

View File

@ -24,6 +24,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
private lateinit var saveButton: Button
private lateinit var editButton: Button
private lateinit var uploadButton: Button
private lateinit var downloadButton: Button
private val dynamicButtons = mutableListOf<Button>()
private val questionnaireFiles = mutableMapOf<Button, String>()
@ -43,6 +44,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
setupSaveButton()
setupEditButton()
setupUploadButton()
setupDownloadButton()
if (!editText.text.isNullOrBlank()) {
buttonLoad.performClick()
@ -58,6 +60,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
saveButton = activity.findViewById(R.id.saveButton)
editButton = activity.findViewById(R.id.editButton)
uploadButton = activity.findViewById(R.id.uploadButton)
downloadButton = activity.findViewById(R.id.downloadButton)
val tag = editText.tag as? String ?: ""
editText.hint = LanguageManager.getText(languageID, tag)
@ -556,5 +559,16 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
}
}
private fun setupDownloadButton() {
downloadButton.text = "Download"
downloadButton.setOnClickListener {
val clientCode = editText.text.toString().trim()
GlobalValues.LAST_CLIENT_CODE = clientCode
Toast.makeText(activity, "Datenbank wird heruntergeladen...", Toast.LENGTH_SHORT).show()
DatabaseDownloader.downloadAndReplaceDatabase(activity)
}
}
}

View File

@ -62,6 +62,15 @@
app:layout_constraintStart_toStartOf="@id/editText"
app:layout_constraintTop_toBottomOf="@id/editText" />
<Button
android:id="@+id/downloadButton"
android:layout_width="200dp"
android:layout_height="42dp"
android:layout_marginTop="92dp"
app:layout_constraintEnd_toEndOf="@id/editText"
app:layout_constraintStart_toStartOf="@id/editText"
app:layout_constraintTop_toBottomOf="@id/editText" />
<Button
android:id="@+id/editButton"
android:layout_width="72dp"
@ -90,4 +99,4 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.307" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>