Skip to content

Commit

Permalink
Backport mysword and mybible document install / backup / deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Feb 13, 2023
1 parent 9306d94 commit 52e73f5
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 108 deletions.
65 changes: 45 additions & 20 deletions app/src/main/java/net/bible/android/control/backup/BackupControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import net.bible.android.BibleApplication
import net.bible.android.SharedConstants
import net.bible.android.activity.BuildConfig
import net.bible.android.activity.R
import net.bible.android.activity.databinding.BackupViewBinding
Expand All @@ -44,14 +45,17 @@ import net.bible.android.database.DATABASE_VERSION
import net.bible.android.view.activity.base.ActivityBase
import net.bible.android.view.activity.base.Dialogs
import net.bible.android.view.activity.installzip.InstallZip
import net.bible.android.view.activity.page.MainBibleActivity
import net.bible.android.view.activity.page.MainBibleActivity.Companion._mainBibleActivity
import net.bible.android.view.util.Hourglass
import net.bible.service.common.CommonUtils
import net.bible.service.common.CommonUtils.windowControl
import net.bible.service.common.FileManager
import net.bible.service.db.DATABASE_NAME
import net.bible.service.db.DatabaseContainer
import net.bible.service.db.DatabaseContainer.db
import net.bible.service.download.isPseudoBook
import net.bible.service.sword.dbFile
import net.bible.service.sword.mybible.isMyBibleBook
import net.bible.service.sword.mysword.isMySwordBook
import org.crosswire.jsword.book.Book
Expand All @@ -73,7 +77,6 @@ import kotlin.coroutines.suspendCoroutine


object BackupControl {

/** Backup database to Uri returned from ACTION_CREATE_DOCUMENT intent
*/
private suspend fun backupDatabaseToUri(activity: ActivityBase, uri: Uri, file: File) {
Expand Down Expand Up @@ -118,16 +121,16 @@ object BackupControl {
val subject = callingActivity.getString(R.string.backup_email_subject_2, CommonUtils.applicationNameMedium)
val message = callingActivity.getString(R.string.backup_email_message_2, CommonUtils.applicationNameMedium)
val uri = FileProvider.getUriForFile(callingActivity, BuildConfig.APPLICATION_ID + ".provider", targetFile)
val email = Intent(Intent.ACTION_SEND).apply {
val email = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_STREAM, uri)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, message)
type = "application/x-sqlite3"
}
val chooserIntent = Intent.createChooser(email, getString(R.string.send_backup_file))
val chooserIntent = Intent.createChooser(email, getString(R.string.send_backup_file))
chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
hourglass.dismiss()
callingActivity.awaitIntent(chooserIntent)
callingActivity.awaitIntent(chooserIntent)
}

fun resetDatabase() {
Expand Down Expand Up @@ -180,9 +183,9 @@ object BackupControl {
var result: List<Book>? = null
withContext(Dispatchers.Main) {
result = suspendCoroutine {
val books = Books.installed().books.filter { !it.isMyBibleBook && !it.isMySwordBook && !it.isPseudoBook }.sortedBy { it.language }
val books = Books.installed().books.filter { !it.isPseudoBook }.sortedBy { it.language }
val bookNames = books.map {
context.getString(R.string.something_with_parenthesis, it.name, it.language.code)
context.getString(R.string.something_with_parenthesis, it.name, "${it.initials}, ${it.language.code}")
}.toTypedArray()

val checkedItems = bookNames.map { false }.toBooleanArray()
Expand Down Expand Up @@ -241,22 +244,43 @@ object BackupControl {
}
}

fun addModuleFile(outFile: ZipOutputStream, moduleFile: File) {
FileInputStream(moduleFile).use { inFile ->
BufferedInputStream(inFile).use { origin ->
val fileNameInsideZip = moduleFile.relativeTo(moduleDir).path
val entry = ZipEntry(fileNameInsideZip)
outFile.putNextEntry(entry)
origin.copyTo(outFile)
}
}
}

withContext(Dispatchers.IO) {
ZipOutputStream(FileOutputStream(zipFile)).use { outFile ->
for(b in books) {
val bmd = b.bookMetaData as SwordBookMetaData
val configFile = bmd.configFile
val rootDir = configFile.parentFile!!.parentFile!!

addFile(outFile, rootDir, configFile)
val dataPath = bmd.getProperty("DataPath")
val dataDir = File(rootDir, dataPath).run {
if(listOf(BookCategory.DICTIONARY, BookCategory.GENERAL_BOOK, BookCategory.MAPS).contains(b.bookCategory))
parentFile
else this
}
for(f in dataDir.walkTopDown().filter { it.isFile }) {
addFile(outFile, rootDir, f)
if (b.isMyBibleBook) {
addModuleFile(outFile, b.dbFile)
} else if(b.isMySwordBook) {
addModuleFile(outFile, b.dbFile)
} else {
val configFile = bmd.configFile
val rootDir = configFile.parentFile!!.parentFile!!
addFile(outFile, rootDir, configFile)
val dataPath = bmd.getProperty("DataPath")
val dataDir = File(rootDir, dataPath).run {
if (listOf(
BookCategory.DICTIONARY,
BookCategory.GENERAL_BOOK,
BookCategory.MAPS
).contains(b.bookCategory)
)
parentFile
else this
}
for (f in dataDir.walkTopDown().filter { it.isFile }) {
addFile(outFile, rootDir, f)
}
}
}
}
Expand Down Expand Up @@ -307,7 +331,7 @@ object BackupControl {

// send intent to pick file
var ok = true
val result = suspendCoroutine<BackupResult> {
val result = suspendCoroutine {
AlertDialog.Builder(callingActivity)
.setTitle(callingActivity.getString(R.string.backup_backup_title))
.setMessage(callingActivity.getString(R.string.backup_backup_message))
Expand Down Expand Up @@ -434,7 +458,7 @@ object BackupControl {

suspend fun startBackupOldAppDatabase(callingActivity: ActivityBase, file: File) {
val result = withContext(Dispatchers.Main) {
suspendCoroutine <BackupResult> {
suspendCoroutine {
AlertDialog.Builder(callingActivity)
.setTitle(callingActivity.getString(R.string.backup_backup_title))
.setMessage(callingActivity.getString(R.string.backup_backup_message))
Expand Down Expand Up @@ -510,6 +534,7 @@ object BackupControl {
activity.awaitIntent(intent)
}

private var moduleDir: File = SharedConstants.MODULE_DIR
private lateinit var internalDbDir : File
private lateinit var internalDbBackupDir: File // copy of db is created in this dir when doing backups
private const val MODULE_BACKUP_NAME = "modules.zip"
Expand Down
Loading

0 comments on commit 52e73f5

Please sign in to comment.