Skip to content

Commit

Permalink
Do not ask many times if want to install studypad file
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Jun 13, 2024
1 parent 43d5ff1 commit 4c33e87
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,36 @@ class InstallZip : ActivityBase() {
lifecycleScope.launch {
when (intent?.action) {
Intent.ACTION_VIEW -> {
val q = getString(R.string.install_do_you_want, intent.data?.run {getDisplayName(this) }?: "?")
val result = Dialogs.simpleQuestion(this@InstallZip, q)
if(result) {
val mimeType = getMimeType(intent.data!!)
val uri = intent.data!!
val inputStream = BufferedInputStream(contentResolver.openInputStream(intent.data!!))
val mimeType = getMimeType(uri)
val displayName = getDisplayName(uri)
val manifest = AndBibleBackupManifest.fromInputStream(inputStream)
if(
// installStudyPads will ask after reading the file (will show stats from db)
manifest?.backupType == BackupType.STUDYPAD_EXPORT
|| askIfWantInstall(displayName)
) {
if(mimeType == "application/epub+zip") {
installEpub(intent.data!!)
installEpub(inputStream, displayName)
} else {
installZip(intent!!.data!!)
installZip(inputStream, displayName)
}
} else {
finish()
}
}
Intent.ACTION_SEND -> installZip(intent.getParcelableExtra(Intent.EXTRA_STREAM)!!)
Intent.ACTION_SEND -> {
val uri = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)!!
val inputStream = BufferedInputStream(contentResolver.openInputStream(uri))
val displayName = getDisplayName(uri) ?: UUID.randomUUID().toString()
installZip(inputStream, displayName)
}
Intent.ACTION_SEND_MULTIPLE -> {
for (uri in intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM)!!) {
installZip(uri)
val inputStream = BufferedInputStream(contentResolver.openInputStream(uri))
val displayName = getDisplayName(uri) ?: UUID.randomUUID().toString()
installZip(inputStream, displayName)
}
}
else -> {
Expand All @@ -345,6 +358,11 @@ class InstallZip : ActivityBase() {
}
}

private suspend fun askIfWantInstall(displayName: String?): Boolean {
val q = getString(R.string.install_do_you_want, displayName?: "?")
return Dialogs.simpleQuestion(this@InstallZip, q)
}

override fun onDestroy() {
ABEventBus.unregister(this)
super.onDestroy()
Expand Down Expand Up @@ -381,8 +399,13 @@ class InstallZip : ActivityBase() {

val result = awaitIntent(intent)
if (result.resultCode == Activity.RESULT_OK) {
val uri = result.data!!.data!!
val displayName = getDisplayName(uri) ?: UUID.randomUUID().toString()
val inputStream = BufferedInputStream(contentResolver.openInputStream(intent.data!!))
val mimeType = getMimeType(uri)

try {
installFromFile(result.data!!.data!!)
installFromFile(inputStream, displayName, mimeType)
} catch (e: InstallZipError) {
Log.e(TAG, "Error occurred in installing module", e)
val msg = when(e) {
Expand Down Expand Up @@ -426,22 +449,15 @@ class InstallZip : ActivityBase() {
if(mimeTypeIdx < 0) null else it.getString(mimeTypeIdx)
}

private suspend fun installFromFile(uri: Uri): Boolean {
val displayName = getDisplayName(uri) ?: "unknown-filename"
val mimeType = getMimeType(uri)
private suspend fun installFromFile(inputStream: BufferedInputStream, displayName: String, mimeType: String?): Boolean {
if(mimeType == "application/epub+zip") {
return installEpub(uri)
return installEpub(inputStream, displayName)
}

val inputStream = try {
BufferedInputStream(contentResolver.openInputStream(uri))
} catch (e: FileNotFoundException) {
throw FileNotFound()
}
val fileTypeFromContent = determineFileType(inputStream)

if (fileTypeFromContent == BackupControl.AbDbFileType.ZIP) {
return installZip(uri)
return installZip(inputStream, displayName)
}

if(fileTypeFromContent != BackupControl.AbDbFileType.SQLITE3) {
Expand Down Expand Up @@ -512,16 +528,14 @@ class InstallZip : ActivityBase() {
}


private suspend fun installZip(uri: Uri): Boolean {
private suspend fun installZip(bufferedInputStream: BufferedInputStream, displayName: String?): Boolean {
var result = false
binding.installZipLabel.text = getString(R.string.checking_zip_file)

val bufferedInputStream = BufferedInputStream(contentResolver.openInputStream(uri))

binding.loadingIndicator.visibility = View.VISIBLE
val manifest = AndBibleBackupManifest.fromInputStream(bufferedInputStream)
if (manifest?.backupType == BackupType.STUDYPAD_EXPORT) {
result = installStudyPads(uri)
result = installStudyPads(bufferedInputStream)
finish()
} else {
val zh = ZipHandler(
Expand All @@ -539,7 +553,7 @@ class InstallZip : ActivityBase() {
try {
zh.execute()
} catch (e: EpubFile) {
installEpub(uri)
installEpub(bufferedInputStream, displayName)
finish()
}
}
Expand All @@ -550,9 +564,8 @@ class InstallZip : ActivityBase() {
return result
}

private suspend fun installStudyPads(uri: Uri): Boolean {
private suspend fun installStudyPads(inputStream: InputStream): Boolean {
val category = SyncableDatabaseDefinition.BOOKMARKS
val inputStream = contentResolver.openInputStream(uri)!!
val unzipFolder = File(BackupControl.internalDbBackupDir, "unzip")
unzipInputStream(inputStream, unzipFolder)
val file = File(unzipFolder, "db/${BookmarkDatabase.dbFileName}")
Expand All @@ -573,11 +586,11 @@ class InstallZip : ActivityBase() {

private val bookmarksDao get() = DatabaseContainer.instance.bookmarkDb.bookmarkDao()

private suspend fun installEpub(uri: Uri): Boolean = withContext(Dispatchers.IO) {
private suspend fun installEpub(inputStream: InputStream, displayName_: String?): Boolean = withContext(Dispatchers.IO) {
val displayName = displayName_ ?: UUID.randomUUID().toString()
withContext(Dispatchers.Main) {
binding.loadingIndicator.visibility = View.VISIBLE
}
val displayName = getDisplayName(uri) ?: UUID.randomUUID().toString()
val dir = File(SharedConstants.modulesDir, "epub/$displayName")
if (dir.exists()) {
val initials = epubInitials(displayName)
Expand All @@ -595,7 +608,7 @@ class InstallZip : ActivityBase() {
}
}
dir.mkdirs()
unzipInputStream(contentResolver.openInputStream(uri)!!, dir)
unzipInputStream(inputStream, dir)
val installOk = addManuallyInstalledEpubBooks()
withContext(Dispatchers.Main) {
if(installOk) {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/net/bible/service/common/CommonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import androidx.lifecycle.lifecycleScope
import com.google.android.gms.common.internal.service.Common
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
Expand Down Expand Up @@ -1869,14 +1870,20 @@ val Key.shortName: String get() =
enum class DbType {
BOOKMARKS, WORKSPACES, READINGPLANS, SETTINGS, REPOSITORIES, MODULES, EPUBS
}
enum class BackupType {STUDYPAD_EXPORT, DB_BACKUP, MODULE_BACKUP}
enum class BackupType {
// Note! We can only trust STUDYPAD_EXPORT, as manifest is not existing before AB version 822.
STUDYPAD_EXPORT,
DB_BACKUP,
MODULE_BACKUP
}

const val ANDBIBLE_BACKUP_MANIFEST_FILENAME = "AndBibleBackupManifest.json"
@Serializable
data class AndBibleBackupManifest(
val backupType: BackupType,
val contains: Set<DbType>? = null,
val version: Int = 1,
val manifestVersion: Int = 1,
val andBibleVersion: Int = CommonUtils.applicationVersionNumber
) {
fun toJson(): String {
return CommonUtils.json.encodeToString(serializer(), this)
Expand Down

0 comments on commit 4c33e87

Please sign in to comment.