Skip to content

Commit

Permalink
7.3.3 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
XilinJia committed Jan 11, 2025
1 parent 18600ad commit 4efe4be
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 159 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ android {
vectorDrawables.useSupportLibrary false
vectorDrawables.generatedDensities = []

versionCode 3020339
versionName "7.3.2"
versionCode 3020340
versionName "7.3.3"

ndkVersion "27.0.12077973"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
feed!!.episodes.forEach { e -> if (!e.title.isNullOrEmpty()) nameEpisodeMap[generateFileName(e.title!!)] = e }
nameEpisodeMap.keys.forEach { Logd(TAG, "key: $it") }
val dirFiles = srcFile.listFiles()
Logd(TAG, "srcFile: ${srcFile.name} dirFiles: ${dirFiles.size}")
Logd(TAG, "srcFile: ${srcFile.name} dirFiles: ${dirFiles?.size}")
if (!dirFiles.isNullOrEmpty()) {
val destDir = destRootDir.findFile(relativePath) ?: destRootDir.createDirectory(relativePath) ?: return
dirFiles.forEach { file -> copyRecursive(context, file, srcFile, destDir, move) }
Expand Down Expand Up @@ -192,7 +192,7 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
throw e
}
}
private fun copyRecursive(context: Context, srcFile: DocumentFile, srcRootDir: DocumentFile, destRootDir: DocumentFile, move: Boolean) {
private fun copyRecursive(context: Context, srcFile: DocumentFile, srcRootDir: DocumentFile, destRootDir: DocumentFile, move: Boolean, onlyUpdateDB: Boolean = false) {
val relativePath = srcFile.uri.path?.substring(srcRootDir.uri.path!!.length+1) ?: return
if (srcFile.isDirectory) {
Logd(TAG, "copyRecursiveDD folder title: $relativePath")
Expand All @@ -204,7 +204,7 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
val destDir = destRootDir.findFile(relativePath) ?: destRootDir.createDirectory(relativePath) ?: return
val files = srcFile.listFiles()
if (files.isNotEmpty()) files.forEach { file -> copyRecursive(context, file, srcFile, destDir, move) }
if (move) srcFile.delete()
if (!onlyUpdateDB && move) srcFile.delete()
} else {
val nameParts = relativePath.split(".")
if (nameParts.size < 3) return
Expand All @@ -216,7 +216,7 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
val destName = "$title.${episode.id}.$ext"
val destFile = destRootDir.createFile(getMimeType(destName), destName) ?: return
Logd(TAG, "copyRecursiveDD copying file to: ${destFile.uri}")
copyFile(srcFile, destFile, context, move)
if (!onlyUpdateDB) copyFile(srcFile, destFile, context, move)
upsertBlk(episode) {
it.fileUrl = destFile.uri.toString()
it.setIsDownloaded()
Expand Down Expand Up @@ -289,6 +289,54 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
var bytesRead: Int
while (inputStream.read(buffer).also { bytesRead = it } != -1) outputStream.write(buffer, 0, bytesRead)
}
private fun copyRecursive(srcFile: File, srcRootDir: File, destRootDir: File, move: Boolean, onlyUpdateDB: Boolean = false) {
val relativePath = srcFile.absolutePath.substring(srcRootDir.absolutePath.length+1)
// val relativePath = srcFile.name
if (srcFile.isDirectory) {
feed = nameFeedMap[relativePath] ?: return
Logd(TAG, "copyRecursiveFD found feed: ${feed?.title}")
nameEpisodeMap.clear()
feed!!.episodes.forEach { e -> if (!e.title.isNullOrEmpty()) nameEpisodeMap[generateFileName(e.title!!)] = e }
nameEpisodeMap.keys.forEach { Logd(TAG, "key: $it") }
val destFile = File(destRootDir, relativePath)
if (!destFile.exists()) destFile.mkdirs()
val files = srcFile.listFiles()
if (!files.isNullOrEmpty()) files.forEach { file -> copyRecursive(file, srcFile, destFile, move) }
if (!onlyUpdateDB && move) srcFile.delete()
} else {
val nameParts = relativePath.split(".")
if (nameParts.size < 3) return
val ext = nameParts[nameParts.size-1]
val title = nameParts.dropLast(2).joinToString(".")
Logd(TAG, "copyRecursiveFD file title: $title")
val episode = nameEpisodeMap[title] ?: return
Logd(TAG, "copyRecursiveFD found episode: ${episode.title}")
val destName = "$title.${episode.id}.$ext"
val destFile = File(destRootDir, destName)
if (!destFile.exists()) {
Logd(TAG, "copyRecursiveDF copying file to: ${destFile.absolutePath}")
if (!onlyUpdateDB) copyFile(srcFile, destFile, move)
upsertBlk(episode) {
it.fileUrl = Uri.fromFile(destFile).toString()
Logd(TAG, "copyRecursiveDF fileUrl: ${it.fileUrl}")
it.setIsDownloaded()
}
}
}
}
private fun copyFile(sourceFile: File, destFile: File, move: Boolean) {
try {
val inputStream = FileInputStream(sourceFile)
val outputStream = FileOutputStream(destFile)
copyStream(inputStream, outputStream)
inputStream.close()
outputStream.close()
if (move) sourceFile.delete()
} catch (e: IOException) {
Log.e("Error", "Error copying file: $e")
throw e
}
}
@Throws(IOException::class)
fun importFromUri(uri: Uri, context: Context, move: Boolean = false, verify : Boolean = true) {
try {
Expand Down Expand Up @@ -317,6 +365,33 @@ class MediaFilesTransporter(val mediaFilesDirName: String) {
feed = null
}
}
@Throws(IOException::class)
fun updateDB(context: Context) {
try {
val feeds = getFeedList()
feeds.forEach { f -> if (!f.title.isNullOrEmpty()) nameFeedMap[generateFileName(f.title!!)] = f }
Logd(TAG, "importFromUri customMediaUriString: [$customMediaUriString]")
if (customMediaUriString.isNotBlank()) {
val customUri = Uri.parse(customMediaUriString)
val directory = DocumentFile.fromTreeUri(getAppContext(), customUri) ?: throw IllegalArgumentException("Invalid tree URI: $customMediaUriString")
val exportedDir = directory
val fileList = exportedDir.listFiles()
fileList.forEach { file -> copyRecursive(context, file, exportedDir, directory, false, true) }
} else {
val mediaDir = context.getExternalFilesDir("media") ?: return
val exportedDir = mediaDir
val fileList = exportedDir.listFiles()
fileList?.forEach { file -> copyRecursive(file, exportedDir, mediaDir, false, true) }
}
} catch (e: IOException) {
Log.e(TAG, Log.getStackTraceString(e))
throw e
} finally {
nameFeedMap.clear()
nameEpisodeMap.clear()
feed = null
}
}
}

class DatabaseTransporter {
Expand Down
54 changes: 35 additions & 19 deletions app/src/main/kotlin/ac/mdiq/podcini/storage/database/RealmDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,30 +101,30 @@ object RealmDB {
Logd(TAG, "migrating DB from below 37")
mContext.enumerate(className = "Episode") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
newObject?.run {
Logd(TAG, "start: ${getNullableValue("title", String::class)}")
// Logd(TAG, "start: ${getNullableValue("title", String::class)}")
val media = oldObject.getObject(propertyName = "media")
if (media != null) {
set("fileUrl", media.getNullableValue("fileUrl", String::class))
set("downloadUrl", media.getNullableValue("downloadUrl", String::class))
set("mimeType", media.getNullableValue("mimeType", String::class))
Logd(TAG, "after mimeType")
// Logd(TAG, "after mimeType")
set("downloaded", media.getValue("downloaded", Boolean::class))
Logd(TAG, "after downloaded")
// Logd(TAG, "after downloaded")
set("downloadTime", media.getValue("downloadTime", Long::class))
set("duration", media.getValue("duration", Long::class))
set("position", media.getValue("position", Long::class))
set("lastPlayedTime", media.getValue("lastPlayedTime", Long::class))
set("startPosition", media.getValue("startPosition", Long::class))
Logd(TAG, "after startPosition")
// Logd(TAG, "after startPosition")
set("playedDurationWhenStarted", media.getValue("playedDurationWhenStarted", Long::class))
set("playedDuration", media.getValue("playedDuration", Long::class))
set("timeSpentOnStart", media.getValue("timeSpentOnStart", Long::class))
set("startTime", media.getValue("startTime", Long::class))
Logd(TAG, "after startTime")
// Logd(TAG, "after startTime")
set("timeSpent", media.getValue("timeSpent", Long::class))
set("size", media.getValue("size", Long::class))
set("playbackCompletionTime", media.getValue("playbackCompletionTime", Long::class))
Logd(TAG, "after all")
// Logd(TAG, "after all")
}
}
}
Expand All @@ -139,55 +139,71 @@ object RealmDB {
set("keepUpdated", pref.getValue("keepUpdated", Boolean::class))
set("username", pref.getNullableValue("username", String::class))
set("password", pref.getNullableValue("password", String::class))
Logd(TAG, "after password")
// Logd(TAG, "after password")
set("videoMode", pref.getValue("videoMode", Long::class))
set("playSpeed", pref.getValue("playSpeed", Float::class))
set("introSkip", pref.getValue("introSkip", Long::class))
set("endingSkip", pref.getValue("endingSkip", Long::class))
set("autoDelete", pref.getValue("autoDelete", Long::class))
Logd(TAG, "after autoDelete")
// Logd(TAG, "after autoDelete")
set("audioType", pref.getValue("audioType", Long::class))
set("volumeAdaption", pref.getValue("volumeAdaption", Long::class))
set("audioQuality", pref.getValue("audioQuality", Long::class))
set("videoQuality", pref.getValue("videoQuality", Long::class))
set("prefStreamOverDownload", pref.getValue("prefStreamOverDownload", Boolean::class))
set("filterString", pref.getValue("filterString", String::class))
Logd(TAG, "after filterString")
// Logd(TAG, "after filterString")
set("sortOrderCode", pref.getValue("sortOrderCode", Long::class))
val tagsSet = getValueSet<String>("tags")
tagsSet.addAll(pref.getValueSet<String>("tags"))
set("autoDownload", pref.getValue("autoDownload", Boolean::class))
set("queueId", pref.getValue("queueId", Long::class))
Logd(TAG, "after queueId")
// Logd(TAG, "after queueId")
set("autoAddNewToQueue", pref.getValue("autoAddNewToQueue", Boolean::class))
set("autoDLInclude", pref.getNullableValue("autoDLInclude", String::class))
set("autoDLExclude", pref.getNullableValue("autoDLExclude", String::class))
set("autoDLMinDuration", pref.getValue("autoDLMinDuration", Long::class))
Logd(TAG, "after autoDLMinDuration")
// Logd(TAG, "after autoDLMinDuration")
set("markExcludedPlayed", pref.getValue("markExcludedPlayed", Boolean::class))
set("autoDLMaxEpisodes", pref.getValue("autoDLMaxEpisodes", Long::class))
set("countingPlayed", pref.getValue("countingPlayed", Boolean::class))
set("autoDLPolicyCode", pref.getValue("autoDLPolicyCode", Long::class))
Logd(TAG, "after all")
// Logd(TAG, "after all")
}
}
}
}
if (oldRealm.schemaVersion() < 39) {
Logd(TAG, "migrating DB from below 39")
// val oldEpisodes = oldRealm.query(className = "Episode").find()
// for (oe in oldEpisodes) {
// try {
// val fileUrl = oe.getNullableValue("fileUrl", String::class)
// if (!fileUrl.isNullOrBlank()) {
// val f = File(fileUrl)
// val uri = Uri.fromFile(f)
// val ne = newRealm.findLatest(oe)
// ne?.set("fileUrl", uri.toString())
// }
// } catch (e: Throwable) {
// Log.e(TAG, " can't create uri from fileUrl")
// val ne = newRealm.findLatest(oe)
// ne?.set("fileUrl", "")
// }
// }
mContext.enumerate(className = "Episode") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
newObject?.run {
val fileUrl = oldObject.getNullableValue("fileUrl", String::class)
Logd(TAG, "fileUrl: $fileUrl")
if (!fileUrl.isNullOrBlank()) {
try {
try {
val fileUrl = oldObject.getNullableValue("fileUrl", String::class)
Logd(TAG, "fileUrl: $fileUrl")
if (!fileUrl.isNullOrBlank()) {
val f = File(fileUrl)
val uri = Uri.fromFile(f)
set("fileUrl", uri.toString())
} catch (e: Throwable) {
Log.e(TAG, " can't create uri from $fileUrl")
set("fileUrl", "")
}
} catch (e: Throwable) {
Log.e(TAG, " can't create uri from fileUrl")
set("fileUrl", "")
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/kotlin/ac/mdiq/podcini/storage/model/Episode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import ac.mdiq.podcini.storage.utils.StorageUtils.getDataFolder
import ac.mdiq.podcini.storage.utils.StorageUtils.getMimeType
import ac.mdiq.podcini.util.Logd
import ac.mdiq.vista.extractor.Vista
import ac.mdiq.vista.extractor.stream.AudioStream
import ac.mdiq.vista.extractor.stream.StreamInfo
import ac.mdiq.vista.extractor.stream.VideoStream
import android.content.ContentResolver
import android.content.Context
import android.media.MediaMetadataRetriever
Expand Down Expand Up @@ -178,6 +180,9 @@ class Episode : RealmObject {
return field
}

// var audioStreamsList: RealmList<AudioStream> = realmListOf()
// var videoStreamsList: RealmList<VideoStream> = realmListOf()

@Ignore
val isRemote = mutableStateOf(false)

Expand Down Expand Up @@ -535,7 +540,10 @@ class Episode : RealmObject {
false
}
}
else -> throw IllegalArgumentException("Unsupported URI scheme: ${fileuri.scheme}")
else -> {
Logd(TAG, "Unsupported URI scheme: ${fileuri.scheme}")
false
}
}
}

Expand Down
Loading

0 comments on commit 4efe4be

Please sign in to comment.