From 598f6ded1a44995700dd402e662d0170db92c8a6 Mon Sep 17 00:00:00 2001 From: Li ZongYing Date: Fri, 16 Aug 2024 12:54:23 +0800 Subject: [PATCH] fix crash --- HISTORY.md | 2 +- .../java/com/lizongying/mytv0/InfoFragment.kt | 6 ++- .../java/com/lizongying/mytv0/ListAdapter.kt | 2 +- .../lizongying/mytv0/LocaleContextWrapper.kt | 2 +- .../java/com/lizongying/mytv0/MainActivity.kt | 42 ++++++++----------- .../java/com/lizongying/mytv0/MenuFragment.kt | 20 ++++----- .../com/lizongying/mytv0/PlayerFragment.kt | 2 +- .../com/lizongying/mytv0/SettingFragment.kt | 6 +-- .../lizongying/mytv0/models/TVGroupModel.kt | 10 +++-- .../lizongying/mytv0/models/TVListModel.kt | 10 +++-- .../com/lizongying/mytv0/models/TVModel.kt | 30 +++++++++---- version.json | 2 +- 12 files changed, 76 insertions(+), 58 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 8bdce31..d7fa868 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ ## 更新日誌 -### v1.3.7.9 +### v1.3.7.10 * 修復閃退問題 diff --git a/app/src/main/java/com/lizongying/mytv0/InfoFragment.kt b/app/src/main/java/com/lizongying/mytv0/InfoFragment.kt index 7a0a795..2dad5c2 100644 --- a/app/src/main/java/com/lizongying/mytv0/InfoFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/InfoFragment.kt @@ -32,7 +32,6 @@ class InfoFragment : Fragment() { ): View { _binding = InfoBinding.inflate(inflater, container, false) - val application = requireActivity().applicationContext as MyTVApplication binding.info.layoutParams.width = application.px2Px(binding.info.layoutParams.width) @@ -67,6 +66,11 @@ class InfoFragment : Fragment() { return binding.root } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + (activity as MainActivity).ready(TAG) + } + fun show(tvViewModel: TVModel) { val context = requireContext() binding.title.text = tvViewModel.tv.title diff --git a/app/src/main/java/com/lizongying/mytv0/ListAdapter.kt b/app/src/main/java/com/lizongying/mytv0/ListAdapter.kt index d053c45..1507ddd 100644 --- a/app/src/main/java/com/lizongying/mytv0/ListAdapter.kt +++ b/app/src/main/java/com/lizongying/mytv0/ListAdapter.kt @@ -114,7 +114,7 @@ class ListAdapter( viewHolder.focus(true) focused = view if (visiable) { - if (position != listTVModel.position.value) { + if (position != listTVModel.positionValue) { listTVModel.setPosition(position) } } else { diff --git a/app/src/main/java/com/lizongying/mytv0/LocaleContextWrapper.kt b/app/src/main/java/com/lizongying/mytv0/LocaleContextWrapper.kt index 7eb39f2..ff3bcc1 100644 --- a/app/src/main/java/com/lizongying/mytv0/LocaleContextWrapper.kt +++ b/app/src/main/java/com/lizongying/mytv0/LocaleContextWrapper.kt @@ -8,7 +8,7 @@ import java.util.Locale class LocaleContextWrapper(base: Context) : ContextWrapper(base) { companion object { - fun wrap(context: Context, newLocale: Locale): ContextWrapper { + fun wrap(context: Context, newLocale: Locale): Context { val resources = context.resources val configuration = resources.configuration diff --git a/app/src/main/java/com/lizongying/mytv0/MainActivity.kt b/app/src/main/java/com/lizongying/mytv0/MainActivity.kt index fa887b0..0191c7b 100644 --- a/app/src/main/java/com/lizongying/mytv0/MainActivity.kt +++ b/app/src/main/java/com/lizongying/mytv0/MainActivity.kt @@ -133,7 +133,7 @@ class MainActivity : AppCompatActivity() { fun ready(tag: String) { Log.i(TAG, "ready $tag") ok++ - if (ok == 4) { + if (ok == 5) { Log.i(TAG, "watch") viewModel.groupModel.change.observe(this) { _ -> Log.i(TAG, "groupModel changed") @@ -179,11 +179,11 @@ class MainActivity : AppCompatActivity() { viewModel.groupModel.getCurrent() } tvModel?.setReady() - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue) + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList() ?.let { Log.i(TAG, "list name ${it.getName()}") - it.setPositionPlaying(it.positionValue) + it.setPlaying() } val currentGroup = viewModel.groupModel.positionValue @@ -220,7 +220,7 @@ class MainActivity : AppCompatActivity() { tvModel.errInfo.observe(this) { _ -> if (tvModel.errInfo.value != null -// && tvModel.tv.id == TVList.position.value +// && tvModel.tv.id == TVList.positionValue ) { hideFragment(loadingFragment) if (tvModel.errInfo.value == "") { @@ -240,7 +240,7 @@ class MainActivity : AppCompatActivity() { // not first time && channel is not changed if (tvModel.ready.value != null -// && tvModel.tv.id == TVList.position.value +// && tvModel.tv.id == TVList.positionValue ) { Log.i(TAG, "loading ${tvModel.tv.title}") hideFragment(errorFragment) @@ -259,7 +259,8 @@ class MainActivity : AppCompatActivity() { if (liked) { viewModel.groupModel.getTVListModelNotFilter(0)?.replaceTVModel(tvModel) } else { - viewModel.groupModel.getTVListModelNotFilter(0)?.removeTVModel(tvModel.tv.id) + viewModel.groupModel.getTVListModelNotFilter(0) + ?.removeTVModel(tvModel.tv.id) } SP.setLike(tvModel.tv.id, liked) } @@ -385,12 +386,9 @@ class MainActivity : AppCompatActivity() { val prevGroup = viewModel.groupModel.positionValue val tvModel = viewModel.groupModel.getPosition(position) - tvModel!!.setReady() - Log.i(TAG, "play tv ${tvModel.tv.name}") - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let { - it.setPositionPlaying(it.positionValue) - } + tvModel?.setReady() + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList()?.setPlaying() val currentGroup = viewModel.groupModel.positionValue if (currentGroup != prevGroup) { @@ -413,12 +411,9 @@ class MainActivity : AppCompatActivity() { viewModel.groupModel.getPrev() } - tvModel!!.setReady() - Log.i(TAG, "tv ${tvModel.tv.name}") - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let { - it.setPositionPlaying(it.positionValue) - } + tvModel?.setReady() + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList()?.setPlaying() val currentGroup = viewModel.groupModel.positionValue if (currentGroup != prevGroup) { @@ -438,12 +433,9 @@ class MainActivity : AppCompatActivity() { viewModel.groupModel.getNext() } - tvModel!!.setReady() - Log.i(TAG, "tv ${tvModel.tv.name}") - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let { - it.setPositionPlaying(it.positionValue) - } + tvModel?.setReady() + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList()?.setPlaying() val currentGroup = viewModel.groupModel.positionValue if (currentGroup != prevGroup) { diff --git a/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt b/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt index 936cfd7..7564e3e 100644 --- a/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/MenuFragment.kt @@ -63,7 +63,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList groupAdapter.setItemListener(this) var listTVModel = - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue) + viewModel.groupModel.getCurrentList() Log.i(TAG, "listTVModel0 ${viewModel.groupModel.positionValue} $listTVModel") if (listTVModel == null) { @@ -71,7 +71,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList } listTVModel = - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue) + viewModel.groupModel.getCurrentList() Log.i(TAG, "listTVModel1 ${viewModel.groupModel.positionValue} $listTVModel") listAdapter = ListAdapter( context, @@ -101,14 +101,14 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList groupAdapter.update(viewModel.groupModel) var listTVModel = - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue) + viewModel.groupModel.getCurrentList() Log.i(TAG, "listTVModel3 ${viewModel.groupModel.positionValue} $listTVModel") if (listTVModel == null) { viewModel.groupModel.setPosition(0) } listTVModel = - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue) + viewModel.groupModel.getCurrentList() Log.i(TAG, "listTVModel4 ${viewModel.groupModel.positionValue} $listTVModel") if (listTVModel != null) { @@ -168,11 +168,11 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList } override fun onItemClicked(position: Int, type: String) { - viewModel.groupModel.getTVListModel()?.setPosition(position) - viewModel.groupModel.getCurrent()?.setReady() - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let { - it.setPositionPlaying(it.positionValue) + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList()?.let { + it.setPosition(position) + it.setPlaying() + it.getCurrent()?.setReady() } (activity as MainActivity).hideMenuFragment() } @@ -224,7 +224,7 @@ class MenuFragment : Fragment(), GroupAdapter.ItemListener, ListAdapter.ItemList // listAdapter.focusable(false) // listAdapter.clear() // Log.i(TAG, "group toPosition on left") -// groupAdapter.toPosition(TVList.groupModel.position.value!!) +// groupAdapter.toPosition(TVList.groupModel.positionValue) // return true // } } diff --git a/app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt b/app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt index 07a3fa7..38bed74 100644 --- a/app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt @@ -187,7 +187,7 @@ class PlayerFragment : Fragment() { if (dataSource != null) { setMediaSource(dataSource) } else { - setMediaItem(tvModel.mediaItem) + setMediaItem(tvModel.getMediaItem()) } prepare() diff --git a/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt b/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt index 499cc0c..c201e32 100644 --- a/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt +++ b/app/src/main/java/com/lizongying/mytv0/SettingFragment.kt @@ -306,10 +306,8 @@ class SettingFragment : Fragment() { SP.position = 0 val tvModel = viewModel.groupModel.getPosition(0) tvModel?.setReady() - viewModel.groupModel.setPositionPlaying(viewModel.groupModel.positionValue) - viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)?.let { - it.setPositionPlaying(it.positionValue) - } + viewModel.groupModel.setPlaying() + viewModel.groupModel.getCurrentList()?.setPlaying() SP.showAllChannels = SP.DEFAULT_SHOW_ALL_CHANNELS SP.compactMenu = SP.DEFAULT_COMPACT_MENU diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt b/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt index e2b268c..589b2da 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt @@ -44,11 +44,15 @@ class TVGroupModel : ViewModel() { } fun setPositionPlaying(position: Int) { - Log.i(TAG, "group setPositionPlaying $position") _positionPlaying.value = position SP.positionGroup = position } + fun setPlaying() { + _positionPlaying.value = positionValue + SP.positionGroup = positionValue + } + fun setPrevPosition() { _position.value = (positionValue - 1) % size() } @@ -97,7 +101,7 @@ class TVGroupModel : ViewModel() { } fun getTVListModel(): TVListModel? { - return getTVListModel(position.value as Int) + return getTVListModel(positionValue) } fun getTVListModel(idx: Int): TVListModel? { @@ -148,7 +152,7 @@ class TVGroupModel : ViewModel() { return null } - return getTVListModelNotFilter(positionValue)?.getCurrent() + return getCurrentList()?.getCurrent() } fun getCurrentList(): TVListModel? { diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt b/app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt index 44f00fc..eb0b4a9 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt @@ -19,7 +19,7 @@ class TVListModel(private val name: String, private val groupIndex: Int) : ViewM private val _tvList = MutableLiveData>() val tvList: LiveData> get() = _tvList - val tvListValue: List + private val tvListValue: List get() = _tvList.value ?: listOf() private val _position = MutableLiveData() @@ -40,11 +40,15 @@ class TVListModel(private val name: String, private val groupIndex: Int) : ViewM } fun setPositionPlaying(position: Int) { - Log.i(TAG, "list setPositionPlaying $position") _positionPlaying.value = position SP.position = position } + fun setPlaying() { + _positionPlaying.value = positionValue + SP.position = positionValue + } + private val _change = MutableLiveData() val change: LiveData get() = _change @@ -112,7 +116,7 @@ class TVListModel(private val name: String, private val groupIndex: Int) : ViewM } fun getTVModel(): TVModel? { - return getTVModel(position.value as Int) + return getTVModel(positionValue) } fun getTVModel(idx: Int): TVModel? { diff --git a/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt b/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt index 1ea1057..9ca3e5d 100644 --- a/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt +++ b/app/src/main/java/com/lizongying/mytv0/models/TVModel.kt @@ -106,7 +106,20 @@ class TVModel(var tv: TV) : ViewModel() { get() = _videoIndex private var userAgent = "" - lateinit var mediaItem: MediaItem + + // TODO Maybe _mediaItem has not been initialized when play + private lateinit var _mediaItem: MediaItem + + fun getMediaItem(): MediaItem { + if (::_mediaItem.isInitialized) { + return _mediaItem + } else { + // TODO Maybe url is null + _mediaItem = MediaItem.fromUri(getVideoUrl()!!) + return _mediaItem + } + } + private lateinit var httpDataSource: DefaultHttpDataSource.Factory init { @@ -142,7 +155,7 @@ class TVModel(var tv: TV) : ViewModel() { } } - mediaItem = MediaItem.fromUri(uri.toString()) + _mediaItem = MediaItem.fromUri(uri.toString()) if (path.lowercase().endsWith(".m3u8")) { addSource(SourceType.HLS) @@ -192,20 +205,23 @@ class TVModel(var tv: TV) : ViewModel() { if (sources.isEmpty()) { return null } + if (!::_mediaItem.isInitialized) { + return null + } sourceIndex = max(0, sourceIndex) sourceIndex = min(sourceIndex, sources.size - 1) return when (sources[sourceIndex]) { - SourceType.HLS -> HlsMediaSource.Factory(httpDataSource).createMediaSource(mediaItem) + SourceType.HLS -> HlsMediaSource.Factory(httpDataSource).createMediaSource(_mediaItem) SourceType.RTSP -> if (userAgent.isEmpty()) { - RtspMediaSource.Factory().createMediaSource(mediaItem) + RtspMediaSource.Factory().createMediaSource(_mediaItem) } else { - RtspMediaSource.Factory().setUserAgent(userAgent).createMediaSource(mediaItem) + RtspMediaSource.Factory().setUserAgent(userAgent).createMediaSource(_mediaItem) } - SourceType.DASH -> DashMediaSource.Factory(httpDataSource).createMediaSource(mediaItem) + SourceType.DASH -> DashMediaSource.Factory(httpDataSource).createMediaSource(_mediaItem) SourceType.PROGRESSIVE -> ProgressiveMediaSource.Factory(httpDataSource) - .createMediaSource(mediaItem) + .createMediaSource(_mediaItem) else -> null } diff --git a/version.json b/version.json index e8de3f4..0776196 100644 --- a/version.json +++ b/version.json @@ -1 +1 @@ -{"version_code": 16975625, "version_name": "v1.3.7.9"} +{"version_code": 16975626, "version_name": "v1.3.7.10"}