1
0
mirror of https://github.com/dzeiocom/OpenHealth.git synced 2025-04-22 19:02:16 +00:00

fix: import not working as intended

This commit is contained in:
Florian Bouillon 2023-02-28 02:16:14 +01:00
parent 468f30bcca
commit 0e4e24ac67
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
2 changed files with 43 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package com.dzeio.openhealth.data
import android.content.Context
import android.util.Log
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.Room
@ -16,6 +17,7 @@ import com.dzeio.openhealth.data.weight.WeightDao
import com.dzeio.openhealth.utils.ZipFile
import java.io.File
import java.io.IOException
import java.util.zip.ZipInputStream
/**
* ROOM SQLite database for the application
@ -82,7 +84,7 @@ abstract class AppDatabase : RoomDatabase() {
if (instance == null) return null
val dbFile = context.getDatabasePath(DATABASE_NAME)
val dbWalFile = File(dbFile.path + "-whal")
val dbWalFile = File(dbFile.path + "-wal")
val dbShmFile = File(dbFile.path + "-shm")
checkpoint()
@ -98,6 +100,45 @@ abstract class AppDatabase : RoomDatabase() {
}
}
fun importDatabase(context: Context, zip: ZipInputStream) {
var entry = zip.nextEntry
var dbImported = false
var walImported = false
var shmImported = false
val dbFile = context.getDatabasePath(DATABASE_NAME)
val dbWalFile = File(dbFile.path + "-wal")
val dbShmFile = File(dbFile.path + "-shm")
while (entry != null) {
val direction = File(context.getDatabasePath(DATABASE_NAME).parent, entry.name)
if (entry.name.equals(dbFile.name)) {
dbImported = true
Log.d("AppDatabase", "db imported!")
} else if (entry.name.equals(dbWalFile.name)) {
walImported = true
Log.d("AppDatabase", "wal imported!")
} else if (entry.name.equals(dbShmFile.name)) {
shmImported = true
Log.d("AppDatabase", "shm imported!")
} else {
Log.d("AppDatabase", "trying to import random file?!? \"${entry.name}\"")
continue
}
direction.writeBytes(zip.readBytes())
entry = zip.nextEntry
}
if (dbImported) {
if (!walImported) {
dbWalFile.delete()
Log.d("AppDatabase", "deleting wal")
}
if (!shmImported) {
dbShmFile.delete()
Log.d("AppDatabase", "deleting shm")
}
}
}
fun checkpoint() {
val db = this.openHelper.writableDatabase
db.query("PRAGMA wal_checkpoint(FULL);", emptyArray())

View File

@ -2,7 +2,6 @@ package com.dzeio.openhealth.ui.importexport
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -10,10 +9,8 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.dzeio.openhealth.R
import com.dzeio.openhealth.core.BaseFragment
import com.dzeio.openhealth.data.AppDatabase
import com.dzeio.openhealth.databinding.FragmentImportExportBinding
import dagger.hilt.android.AndroidEntryPoint
import java.io.File
import java.util.zip.ZipInputStream
import kotlin.system.exitProcess
@ -38,16 +35,7 @@ class ImportExportFragment : BaseFragment<ImportExportViewModel, FragmentImportE
// read the zipfile
val stream = requireActivity().contentResolver.openInputStream(it)
val zip = ZipInputStream(stream)
var entry = zip.nextEntry
// copy each file to the database directory
while (entry != null) {
val direction = File(requireContext().getDatabasePath(AppDatabase.DATABASE_NAME).parent, entry.name)
Log.d("Pouet", entry.name)
direction.writeBytes(zip.readBytes())
entry = zip.nextEntry
}
viewModel.appDatabase.importDatabase(requireContext(), zip)
// Restart the app
val i = requireContext().packageManager.getLaunchIntentForPackage(requireContext().packageName)