Skip to content

Commit

Permalink
optimize the remote configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Oct 29, 2024
1 parent b3567d9 commit ca9a035
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 91 deletions.
12 changes: 12 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
## 更新日誌

### v1.3.8.0-kitkat

* 優化遠程配置

### v1.3.8.0

* 優化遠程配置

### v1.3.7.20

* 解決一個可能引起閃退的問題

### v1.3.7.20-kitkat

* 解決EPG和proxy無法置空的問題
Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

## 使用

* 遙控器左鍵/觸屏單擊打開節目列表
* 遙控器左鍵/觸屏單擊打開視頻列表
* 遙控器右鍵/觸屏雙擊打開配置
* 遙控器返回鍵關閉節目列表/配置
* 打開配置頁后,選擇遠程配置,掃描二維碼可以配置視頻源地址等。
* 如果視頻源地址已配置,並且打開了“啟動后自動更新視頻源”后,軟件啟動后自動更新視頻源
* 在節目列表顯示的時候,右鍵收藏/取消收藏
* 配置地址 http://0.0.0.0:34567 ,
* 遙控器返回鍵關閉視頻列表/配置
* 在聚焦視頻標題的時候,右鍵收藏/取消收藏
* 打開配置后,選擇遠程配置,掃描二維碼可以配置視頻源等。也可以直接遠程配置地址 http://0.0.0.0:34567
* 如果視頻源地址已配置,並且打開了“應用啟動后更新視頻源”后,應用啟動后會自動更新視頻源
* 默認遙控器下鍵/觸屏下滑切換到下一個視頻。換台反轉打開後,邏輯相反

注意:

Expand Down Expand Up @@ -54,7 +54,9 @@
下載安裝 [releases](https://github.com/lizongying/my-tv-0/releases/)
更多地址 [my-tv-0](https://lyrics.run/my-tv-0.html)
注意,“*-kitkat”為安卓4.4兼容版本
更多下載地址 [my-tv-0](https://lyrics.run/my-tv-0.html)
![image](./screenshots/Screenshot_20240810_151748.png)
![image](./screenshots/Screenshot_20240813_232847.png)
Expand Down
88 changes: 54 additions & 34 deletions app/src/main/java/com/lizongying/mytv0/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class MainViewModel : ViewModel() {
private lateinit var appDirectory: File
var listModel: List<TVModel> = listOf()
val groupModel = TVGroupModel()

private var cacheFile: File? = null
private var cacheConfig = ""
private var initialized = false

private val _channelsOk = MutableLiveData<Boolean>()
val channelsOk: LiveData<Boolean>
Expand Down Expand Up @@ -71,27 +73,43 @@ class MainViewModel : ViewModel() {
}
}

private fun getCache(): String {
return if (cacheFile!!.exists()) {
cacheFile!!.readText()
} else {
""
}
}

fun init(context: Context) {
groupModel.addTVListModel(TVListModel("我的收藏", 0))
groupModel.addTVListModel(TVListModel("全部頻道", 1))

appDirectory = context.filesDir
val file = File(appDirectory, FILE_NAME)
val str = if (file.exists()) {
file.readText()
} else {
context.resources.openRawResource(R.raw.channels).bufferedReader()
cacheFile = File(appDirectory, FILE_NAME)
if (!cacheFile!!.exists()) {
cacheFile!!.createNewFile()
}

cacheConfig = getCache()
Log.i(TAG, "cacheConfig $cacheConfig")

if (cacheConfig.isEmpty()) {
cacheConfig = context.resources.openRawResource(R.raw.channels).bufferedReader()
.use { it.readText() }
Log.i(TAG, "cacheConfig $cacheConfig")
}

try {
str2List(str)
str2List(cacheConfig)
} catch (e: Exception) {
e.printStackTrace()
file.deleteOnExit()
cacheFile!!.deleteOnExit()
R.string.channel_read_error.showToast()
}

initialized = true

_channelsOk.value = true
}

Expand Down Expand Up @@ -128,20 +146,9 @@ class MainViewModel : ViewModel() {
val response = HttpClient.okHttpClient.newCall(request).execute()

if (response.isSuccessful) {
val file = File(appDirectory, FILE_NAME)
if (!file.exists()) {
file.createNewFile()
}
val str = response.body()!!.string()
withContext(Dispatchers.Main) {
if (str2List(str)) {
file.writeText(str)
SP.config = serverUrl
_channelsOk.value = true
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
tryStr2List(str, null, serverUrl)
}
} else {
Log.e(TAG, "Request status ${response.code()}")
Expand Down Expand Up @@ -186,32 +193,45 @@ class MainViewModel : ViewModel() {
return
}

try {
if (str2List(str)) {
SP.config = uri.toString()
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
} catch (e: Exception) {
e.printStackTrace()
file.deleteOnExit()
R.string.channel_read_error.showToast()
}
tryStr2List(str, file, uri.toString())
} else {
CoroutineScope(Dispatchers.IO).launch {
update(uri.toString())
}
}
}

fun str2List(str: String): Boolean {
fun tryStr2List(str: String, file: File?, url: String) {
try {
if (str2List(str)) {
cacheFile!!.writeText(str)
cacheConfig = str
SP.config = url
_channelsOk.value = true
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
} catch (e: Exception) {
e.printStackTrace()
file?.deleteOnExit()
R.string.channel_read_error.showToast()
}
}

private fun str2List(str: String): Boolean {
var string = str
if (initialized && string == cacheConfig) {
return false
}
val g = Gua()
if (g.verify(str)) {
string = g.decode(str)
}
if (string.isBlank()) {
if (string.isEmpty()) {
return false
}
if (initialized && string == cacheConfig) {
return false
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/ModalFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lizongying.mytv0

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -47,8 +49,14 @@ class ModalFragment : DialogFragment() {
Glide.with(requireContext())
.load(bitmap)
.into(binding.modalImage)
binding.modalText.text = arguments?.getString(KEY_TEXT)
val text = arguments?.getString(KEY_TEXT)
binding.modalText.text = text
binding.modalText.visibility = View.VISIBLE
binding.modal.setOnClickListener {
val url = "http://$text"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
}
} else {
Glide.with(requireContext())
.load(arguments?.getInt(KEY_DRAWABLE_ID))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.Request
import okhttp3.RequestBody
import java.io.IOException
import kotlin.system.exitProcess

class MyTVExceptionHandler(private val context: Context) : Thread.UncaughtExceptionHandler {
Expand Down Expand Up @@ -60,7 +59,6 @@ class MyTVExceptionHandler(private val context: Context) : Thread.UncaughtExcept

private suspend fun saveLog(crashInfo: String) {
withContext(Dispatchers.IO) {

val requestBody = RequestBody.create(MediaType.parse("text/plain"), crashInfo)
val request = Request.Builder()
.url("https://lyrics.run/my-tv-0/v1/log")
Expand All @@ -74,7 +72,7 @@ class MyTVExceptionHandler(private val context: Context) : Thread.UncaughtExcept
Log.e(TAG, "log failed: ${response.code()}")
}
}
} catch (e: IOException) {
} catch (e: Exception) {
e.printStackTrace()
}
}
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.lifecycle.ViewModelProvider
import com.lizongying.mytv0.ModalFragment.Companion.KEY_BITMAP
import com.lizongying.mytv0.ModalFragment.Companion.KEY_TEXT
import com.lizongying.mytv0.SimpleServer.Companion.PORT
import com.lizongying.mytv0.Utils.getDateTimestamp
import com.lizongying.mytv0.databinding.SettingBinding
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -120,7 +121,7 @@ class SettingFragment : Fragment() {
binding.qrcode.setOnClickListener {
val imageModalFragment = ModalFragment()
val size = Utils.dpToPx(200)
val img = QrCodeUtil().createQRCodeBitmap(server, size, size)
val img = QrCodeUtil().createQRCodeBitmap("$server?${getDateTimestamp()}", size, size)
val args = Bundle()
args.putString(KEY_TEXT, server.removePrefix("http://"))
args.putParcelable(KEY_BITMAP, img)
Expand Down Expand Up @@ -293,15 +294,14 @@ class SettingFragment : Fragment() {

SP.config = SP.DEFAULT_CONFIG_URL
Log.i(TAG, "config url: ${SP.config}")
context.deleteFile(FILE_NAME)
viewModel.reset(context)
confirmConfig()

SP.channel = SP.DEFAULT_CHANNEL
Log.i(TAG, "default channel: ${SP.channel}")
confirmChannel()

// context.deleteFile(FILE_NAME)

SP.deleteLike()
Log.i(TAG, "clear like")

Expand Down Expand Up @@ -372,7 +372,7 @@ class SettingFragment : Fragment() {

override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden) {
if (_binding != null && !hidden) {
binding.qrcode.requestFocus()
}
}
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/SimpleServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,19 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
private fun handleSettings(): Response {
val response: String
try {
val file = File(context.filesDir, FILE_NAME)
var str = if (file.exists()) {
file.readText()
} else {
""
}
if (str.isEmpty()) {
str = context.resources.openRawResource(R.raw.channels).bufferedReader()
.use { it.readText() }
}
val respSettings = RespSettings(
channelUri = SP.config ?: "",
channelText = str,
channelDefault = SP.channel,
proxy = SP.proxy ?: "",
epg = SP.epg ?: "",
Expand All @@ -68,13 +79,7 @@ class SimpleServer(private val context: Context, private val viewModel: MainView
try {
readBody(session)?.let {
handler.post {
if (viewModel.str2List(it)) {
File(context.filesDir, FILE_NAME).writeText(it)
SP.config = "file://"
R.string.channel_import_success.showToast()
} else {
R.string.channel_import_error.showToast()
}
viewModel.tryStr2List(it, null, "")
}
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.lizongying.mytv0.data

data class RespSettings(
val channelUri: String,
val channelText: String,
val channelDefault: Int,
val proxy: String,
val epg: String,
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ class TVGroupModel : ViewModel() {
// get & set
// keep: In the current list loop
fun getPrev(keep: Boolean = false): TVModel? {

Log.i(TAG, "keep $keep")

// No item
if (tvGroupValue.size < 2 || tvGroupValue[1].size() == 0) {
return null
Expand Down Expand Up @@ -206,8 +203,6 @@ class TVGroupModel : ViewModel() {

// get & set
fun getNext(keep: Boolean = false): TVModel? {
Log.i(TAG, "keep $keep")

// No item
if (tvGroupValue.size < 2 || tvGroupValue[1].size() == 0) {
return null
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/modal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/modal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
Expand Down
Loading

0 comments on commit ca9a035

Please sign in to comment.