mirror of
https://github.com/lizongying/my-tv.git
synced 2026-10-26 22:26:29 +08:00
auto-startup
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
android:name="android.software.leanback"
|
||||
android:required="true" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:largeHeap="true"
|
||||
@@ -29,5 +30,13 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<receiver
|
||||
android:name=".BootReceiver"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -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 {
|
||||
|
||||
@@ -64,6 +64,12 @@
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
<Switch
|
||||
android:id="@+id/switch_boot_startup"
|
||||
android:text="@string/title_boot_startup"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
<ImageView
|
||||
android:layout_width="300dp"
|
||||
|
||||
@@ -7,6 +7,14 @@
|
||||
android:background="@color/black"
|
||||
android:keepScreenOn="true">
|
||||
|
||||
<SurfaceView
|
||||
android:id="@+id/surface_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="center"
|
||||
app:resize_mode="fill"
|
||||
app:use_controller="false" />
|
||||
|
||||
<androidx.media3.ui.PlayerView
|
||||
android:id="@+id/player_view"
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
<string name="title_channel_reversal">换台反转</string>
|
||||
<string name="title_channel_num">数字选台</string>
|
||||
<string name="check_version">检查更新</string>
|
||||
<string name="title_boot_startup">开机自启</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user