auto-startup

This commit is contained in:
Li ZongYing
2024-02-19 15:57:39 +08:00
parent ce678b8741
commit 1417bbf57b
11 changed files with 114 additions and 6 deletions
@@ -0,0 +1,19 @@
package com.lizongying.mytv
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val sp = context.getSharedPreferences("MainActivity", Context.MODE_PRIVATE)
if (sp.getBoolean(MainActivity.BOOT_STARTUP, true)) {
context.startActivity(
Intent(context, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
}
}
}
@@ -47,6 +47,7 @@ class MainActivity : FragmentActivity() {
lateinit var sharedPref: SharedPreferences
private var channelReversal = false
private var channelNum = true
private var bootStartup = true
private var versionName = ""
@@ -83,6 +84,7 @@ class MainActivity : FragmentActivity() {
sharedPref = getPreferences(Context.MODE_PRIVATE)
channelReversal = sharedPref.getBoolean(CHANNEL_REVERSAL, channelReversal)
channelNum = sharedPref.getBoolean(CHANNEL_NUM, channelNum)
bootStartup = sharedPref.getBoolean(BOOT_STARTUP, bootStartup)
val packageInfo = getPackageInfo()
versionName = packageInfo.versionName
@@ -91,7 +93,7 @@ class MainActivity : FragmentActivity() {
} else {
packageInfo.versionCode.toLong()
}
settingFragment = SettingFragment(versionName, versionCode, channelReversal, channelNum)
settingFragment = SettingFragment(versionName, versionCode, channelReversal, channelNum, bootStartup)
}
fun showInfoFragment(tvViewModel: TVViewModel) {
@@ -249,6 +251,14 @@ class MainActivity : FragmentActivity() {
this.channelNum = channelNum
}
fun saveBootStartup(bootStartup: Boolean) {
with(sharedPref.edit()) {
putBoolean(BOOT_STARTUP, bootStartup)
apply()
}
this.bootStartup = bootStartup
}
private fun showSetting() {
if (!mainFragment.isHidden) {
return
@@ -537,5 +547,6 @@ class MainActivity : FragmentActivity() {
private const val TAG = "MainActivity"
private const val CHANNEL_REVERSAL = "channel_reversal"
private const val CHANNEL_NUM = "channel_num"
const val BOOT_STARTUP = "boot_startup"
}
}
@@ -3,6 +3,8 @@ package com.lizongying.mytv
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
@@ -15,23 +17,39 @@ import androidx.media3.common.VideoSize
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.google.android.exoplayer2.SimpleExoPlayer
import com.lizongying.mytv.databinding.PlayerBinding
import com.lizongying.mytv.models.TVViewModel
class PlayerFragment : Fragment() {
class PlayerFragment : Fragment(), SurfaceHolder.Callback {
private var _binding: PlayerBinding? = null
private var playerView: PlayerView? = null
private var tvViewModel: TVViewModel? = null
private val aspectRatio = 16f / 9f
private lateinit var surfaceView: SurfaceView
private lateinit var surfaceHolder: SurfaceHolder
private var exoPlayer: SimpleExoPlayer? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = PlayerBinding.inflate(inflater, container, false)
playerView = _binding!!.playerView
if (Utils.isTmallDevice()) {
_binding!!.playerView.visibility = View.GONE
surfaceView = _binding!!.surfaceView
surfaceHolder = surfaceView.holder
surfaceHolder.addCallback(this)
} else {
_binding!!.surfaceView.visibility = View.GONE
playerView = _binding!!.playerView
}
playerView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
@@ -78,6 +96,10 @@ class PlayerFragment : Fragment() {
setMediaItem(MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
prepare()
}
exoPlayer?.run {
setMediaItem(com.google.android.exoplayer2.MediaItem.fromUri(tvViewModel.getVideoUrlCurrent()))
prepare()
}
}
override fun onStart() {
@@ -87,8 +109,11 @@ class PlayerFragment : Fragment() {
Log.i(TAG, "replay")
playerView!!.player?.prepare()
playerView!!.player?.play()
} else {
Log.i(TAG, "playing")
}
if (exoPlayer?.isPlaying == false) {
Log.i(TAG, "replay")
exoPlayer?.prepare()
exoPlayer?.play()
}
}
@@ -102,6 +127,9 @@ class PlayerFragment : Fragment() {
if (playerView != null && playerView!!.player?.isPlaying == true) {
playerView!!.player?.stop()
}
if (exoPlayer?.isPlaying == true) {
exoPlayer?.stop()
}
}
override fun onDestroy() {
@@ -109,6 +137,7 @@ class PlayerFragment : Fragment() {
if (playerView != null) {
playerView!!.player?.release()
}
exoPlayer?.release()
}
override fun onDestroyView() {
@@ -119,4 +148,16 @@ class PlayerFragment : Fragment() {
companion object {
private const val TAG = "PlaybackVideoFragment"
}
override fun surfaceCreated(holder: SurfaceHolder) {
exoPlayer = SimpleExoPlayer.Builder(requireContext()).build()
exoPlayer?.setVideoSurfaceHolder(surfaceHolder)
exoPlayer?.playWhenReady = true
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
}
}
@@ -15,6 +15,7 @@ class SettingFragment(
private val versionCode: Long,
private val channelReversal: Boolean,
private val channelNum: Boolean,
private val bootStartup: Boolean,
) :
DialogFragment() {
@@ -49,6 +50,12 @@ class SettingFragment(
(activity as MainActivity).saveChannelNum(isChecked)
}
val switchBootStartup = _binding?.switchBootStartup
switchBootStartup?.isChecked = bootStartup
switchBootStartup?.setOnCheckedChangeListener { _, isChecked ->
(activity as MainActivity).saveBootStartup(isChecked)
}
updateManager = UpdateManager(context, this, versionCode)
_binding?.checkVersion?.setOnClickListener(OnClickListenerCheckVersion(updateManager))
@@ -2,6 +2,7 @@ package com.lizongying.mytv
import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.util.TypedValue
import com.google.gson.Gson
import com.lizongying.mytv.api.TimeResponse
@@ -74,6 +75,8 @@ object Utils {
).toInt()
}
fun isTmallDevice() = Build.MANUFACTURER.equals("Tmall", ignoreCase = true)
/**
* 获取可读写的目录
*
@@ -17,7 +17,6 @@ class ApiClient {
private val protoUrl = "https://capi.yangshipin.cn/"
private val traceUrl = "https://btrace.yangshipin.cn/"
private var okHttpClient = getUnsafeOkHttpClient()
val yspApiService: YSPApiService by lazy {