Skip to content

Commit

Permalink
fix crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lizongying committed Aug 16, 2024
1 parent 412e88e commit 598f6de
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 58 deletions.
2 changes: 1 addition & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 更新日誌

### v1.3.7.9
### v1.3.7.10

* 修復閃退問題

Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/lizongying/mytv0/InfoFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/ListAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 17 additions & 25 deletions app/src/main/java/com/lizongying/mytv0/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 == "") {
Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/java/com/lizongying/mytv0/MenuFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ 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) {
viewModel.groupModel.setPosition(0)
}

listTVModel =
viewModel.groupModel.getTVListModelNotFilter(viewModel.groupModel.positionValue)
viewModel.groupModel.getCurrentList()
Log.i(TAG, "listTVModel1 ${viewModel.groupModel.positionValue} $listTVModel")
listAdapter = ListAdapter(
context,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
// }
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/lizongying/mytv0/PlayerFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class PlayerFragment : Fragment() {
if (dataSource != null) {
setMediaSource(dataSource)
} else {
setMediaItem(tvModel.mediaItem)
setMediaItem(tvModel.getMediaItem())
}

prepare()
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/lizongying/mytv0/SettingFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/lizongying/mytv0/models/TVGroupModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -97,7 +101,7 @@ class TVGroupModel : ViewModel() {
}

fun getTVListModel(): TVListModel? {
return getTVListModel(position.value as Int)
return getTVListModel(positionValue)
}

fun getTVListModel(idx: Int): TVListModel? {
Expand Down Expand Up @@ -148,7 +152,7 @@ class TVGroupModel : ViewModel() {
return null
}

return getTVListModelNotFilter(positionValue)?.getCurrent()
return getCurrentList()?.getCurrent()
}

fun getCurrentList(): TVListModel? {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/com/lizongying/mytv0/models/TVListModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TVListModel(private val name: String, private val groupIndex: Int) : ViewM
private val _tvList = MutableLiveData<List<TVModel>>()
val tvList: LiveData<List<TVModel>>
get() = _tvList
val tvListValue: List<TVModel>
private val tvListValue: List<TVModel>
get() = _tvList.value ?: listOf()

private val _position = MutableLiveData<Int>()
Expand All @@ -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<Boolean>()
val change: LiveData<Boolean>
get() = _change
Expand Down Expand Up @@ -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? {
Expand Down
30 changes: 23 additions & 7 deletions app/src/main/java/com/lizongying/mytv0/models/TVModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version_code": 16975625, "version_name": "v1.3.7.9"}
{"version_code": 16975626, "version_name": "v1.3.7.10"}

0 comments on commit 598f6de

Please sign in to comment.