added download button, but without functions
This commit is contained in:
74
app/src/main/java/com/dano/test1/DatabaseDownloader.kt
Normal file
74
app/src/main/java/com/dano/test1/DatabaseDownloader.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -24,6 +24,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
|||||||
private lateinit var saveButton: Button
|
private lateinit var saveButton: Button
|
||||||
private lateinit var editButton: Button
|
private lateinit var editButton: Button
|
||||||
private lateinit var uploadButton: Button
|
private lateinit var uploadButton: Button
|
||||||
|
private lateinit var downloadButton: Button
|
||||||
|
|
||||||
private val dynamicButtons = mutableListOf<Button>()
|
private val dynamicButtons = mutableListOf<Button>()
|
||||||
private val questionnaireFiles = mutableMapOf<Button, String>()
|
private val questionnaireFiles = mutableMapOf<Button, String>()
|
||||||
@ -43,6 +44,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
|||||||
setupSaveButton()
|
setupSaveButton()
|
||||||
setupEditButton()
|
setupEditButton()
|
||||||
setupUploadButton()
|
setupUploadButton()
|
||||||
|
setupDownloadButton()
|
||||||
|
|
||||||
if (!editText.text.isNullOrBlank()) {
|
if (!editText.text.isNullOrBlank()) {
|
||||||
buttonLoad.performClick()
|
buttonLoad.performClick()
|
||||||
@ -58,6 +60,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
|||||||
saveButton = activity.findViewById(R.id.saveButton)
|
saveButton = activity.findViewById(R.id.saveButton)
|
||||||
editButton = activity.findViewById(R.id.editButton)
|
editButton = activity.findViewById(R.id.editButton)
|
||||||
uploadButton = activity.findViewById(R.id.uploadButton)
|
uploadButton = activity.findViewById(R.id.uploadButton)
|
||||||
|
downloadButton = activity.findViewById(R.id.downloadButton)
|
||||||
|
|
||||||
val tag = editText.tag as? String ?: ""
|
val tag = editText.tag as? String ?: ""
|
||||||
editText.hint = LanguageManager.getText(languageID, tag)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -62,6 +62,15 @@
|
|||||||
app:layout_constraintStart_toStartOf="@id/editText"
|
app:layout_constraintStart_toStartOf="@id/editText"
|
||||||
app:layout_constraintTop_toBottomOf="@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
|
<Button
|
||||||
android:id="@+id/editButton"
|
android:id="@+id/editButton"
|
||||||
android:layout_width="72dp"
|
android:layout_width="72dp"
|
||||||
|
|||||||
Reference in New Issue
Block a user