Skip to content

Commit

Permalink
修复webdav加载目录失败和播放失败
Browse files Browse the repository at this point in the history
  • Loading branch information
rRemix committed Jul 21, 2024
1 parent 261ba91 commit 00cc1d1
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 169 deletions.
89 changes: 0 additions & 89 deletions app/src/main/java/remix/myplayer/db/DBOpenHelper.java

This file was deleted.

56 changes: 56 additions & 0 deletions app/src/main/java/remix/myplayer/db/Migrations.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package remix.myplayer.db

import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import remix.myplayer.db.room.model.WebDav

object Migrations {
val migration3to4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `title` TEXT NOT NULL DEFAULT ''")
db.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `data` TEXT NOT NULL DEFAULT ''")
db.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `account` TEXT")
db.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `pwd` TEXT")

db.execSQL("CREATE TABLE IF NOT EXISTS `WebDav` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `alias` TEXT NOT NULL, `account` TEXT, `pwd` TEXT, `server` TEXT NOT NULL, `lastPath` TEXT, `createAt` INTEGER NOT NULL)")
}
}

val migration4to5 = object : Migration(4, 5) {
@SuppressLint("Range")
override fun migrate(db: SupportSQLiteDatabase) {
val temp = ArrayList<WebDav>()
val cursor = db.query("select * from `Webdav`")
while (cursor.moveToNext()) {
temp.add(
WebDav(
cursor.getString(cursor.getColumnIndex("alias")),
cursor.getString(cursor.getColumnIndex("account")),
cursor.getString(cursor.getColumnIndex("pwd")),
cursor.getString(cursor.getColumnIndex("server")),
cursor.getString(cursor.getColumnIndex("server")),
cursor.getLong(cursor.getColumnIndex("createAt"))
).apply {
id = cursor.getInt(cursor.getColumnIndex("id"))
})
}
print(temp)
db.execSQL("DROP TABLE `WebDav`")
db.execSQL("CREATE TABLE IF NOT EXISTS `WebDav` (`alias` TEXT NOT NULL, `account` TEXT NOT NULL, `pwd` TEXT NOT NULL, `server` TEXT NOT NULL, `lastUrl` TEXT NOT NULL, `createAt` INTEGER NOT NULL, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)")
temp.forEach { webDav ->
db.insert("WebDav", SQLiteDatabase.CONFLICT_REPLACE, ContentValues().apply {
put("alias", webDav.alias)
put("account", webDav.account)
put("pwd", webDav.pwd)
put("server", webDav.server)
put("lastUrl", webDav.lastUrl)
put("createAt", webDav.createAt)
put("id", webDav.id)
})
}
}
}
}
27 changes: 7 additions & 20 deletions app/src/main/java/remix/myplayer/db/room/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import remix.myplayer.db.Migrations.migration3to4
import remix.myplayer.db.Migrations.migration4to5
import remix.myplayer.db.room.AppDatabase.Companion.VERSION
import remix.myplayer.db.room.dao.HistoryDao
import remix.myplayer.db.room.dao.PlayListDao
Expand Down Expand Up @@ -44,11 +46,11 @@ abstract class AppDatabase : RoomDatabase() {
abstract fun webDavDao(): WebDavDao

companion object {
const val VERSION = 4

const val VERSION = 5
@Volatile
private var INSTANCE: AppDatabase? = null

@JvmStatic
fun getInstance(context: Context): AppDatabase =
INSTANCE ?: synchronized(this) {
Expand All @@ -57,26 +59,11 @@ abstract class AppDatabase : RoomDatabase() {

private fun buildDatabase(context: Context): AppDatabase {
val migration1to3 = object : Migration(1, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
override fun migrate(db: SupportSQLiteDatabase) {
}

}
val migration3to4 = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `title` TEXT NOT NULL DEFAULT ''")
database.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `data` TEXT NOT NULL DEFAULT ''")
database.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `account` TEXT")
database.execSQL("ALTER TABLE `PlayQueue` ADD COLUMN `pwd` TEXT")

database.execSQL("CREATE TABLE IF NOT EXISTS `WebDav` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `alias` TEXT NOT NULL, `account` TEXT, `pwd` TEXT, `server` TEXT NOT NULL, `lastPath` TEXT, `createAt` INTEGER NOT NULL)")
}
}
val migration4to5 = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE `WebDav`")
database.execSQL("CREATE TABLE IF NOT EXISTS `WebDav` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `alias` TEXT NOT NULL, `account` TEXT, `pwd` TEXT, `server` TEXT NOT NULL, `lastPath` TEXT, `createAt` INTEGER NOT NULL)")
}
}

val database =
Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "aplayer.db")
.addMigrations(migration1to3)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/remix/myplayer/db/room/dao/WebDavDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import remix.myplayer.db.room.model.WebDav
@Dao
interface WebDavDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrReplace(WebDav: WebDav): Long
suspend fun insertOrReplace(webdav: WebDav): Long

@Query(
"""
Expand Down
34 changes: 8 additions & 26 deletions app/src/main/java/remix/myplayer/db/room/model/WebDav.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@ import java.io.Serializable
@Entity
data class WebDav(
var alias: String,
var account: String?,
var pwd: String?,
var account: String,
var pwd: String,
var server: String,
var lastPath: String? = null,
var lastUrl: String,
val createAt: Long = System.currentTimeMillis()
) : Serializable {
@PrimaryKey(autoGenerate = true)
var id: Int = 0

fun isRoot(url: String): Boolean {
return url.removePrefix("/") == root()
}

fun root() = server.removeSuffix("/")

fun initialPath(): String {
val uri = Uri.parse(server)
return uri.path ?: ""
}

fun base(): String {
val uri = Uri.parse(server)
return "${uri.scheme}://${uri.host}"
}

fun last(): String {
return if (!lastPath.isNullOrEmpty()) {
var lastPath = lastPath!!
if (!lastPath.startsWith("/")) {
lastPath = "/$lastPath"
}
base().plus(lastPath).removeSuffix("/")
} else {
root()
var url = "${uri.scheme}://${uri.host}"
if (uri.port > 0) {
url = "${url}:${uri.port}"
}
return url
}

}
11 changes: 4 additions & 7 deletions app/src/main/java/remix/myplayer/ui/activity/WebDavActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.inputmethod.EditorInfo
import com.thegrizzlylabs.sardineandroid.impl.OkHttpSardine
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
import remix.myplayer.R
import remix.myplayer.databinding.ActivityWebdavBinding
import remix.myplayer.databinding.DialogCreateWebdavBinding
Expand Down Expand Up @@ -181,17 +180,15 @@ class WebDavActivity : ToolbarActivity(), CoroutineScope by MainScope() {
ToastUtil.show(activity, R.string.can_t_be_empty, activity.getString(R.string.pwd))
return
}

// val initialPath = binding.pathLayout.editText?.text?.toString() ?: "/"

if (webDav == null) {
insertOrReplaceWebDav(activity, WebDav(alias, account, pwd, server))
insertOrReplaceWebDav(activity, WebDav(alias, account, pwd, server, server))
} else {
webDav.alias = alias
webDav.server = server
// webDav.initialPath = initialPath
webDav.account = account
webDav.pwd = pwd
webDav.lastPath = ""
webDav.lastUrl = server
insertOrReplaceWebDav(activity, webDav)
}
}
Expand All @@ -203,7 +200,7 @@ class WebDavActivity : ToolbarActivity(), CoroutineScope by MainScope() {
sardine.setCredentials(webdav.account, webdav.pwd)
try {
val davResources = withContext(Dispatchers.IO) {
sardine.list(webdav.root())
sardine.list(webdav.server)
}
if (davResources.isNotEmpty()) {
AppDatabase.getInstance(activity.applicationContext).webDavDao().insertOrReplace(webdav)
Expand Down
Loading

0 comments on commit 00cc1d1

Please sign in to comment.