Forked from ademirqueiroga/InitChannelsBroadcastReceiver.kt
Created
October 6, 2024 14:30
-
-
Save andhikayuana/f93c8a5077f8fe6878bad1ded1844566 to your computer and use it in GitHub Desktop.
InitChannelsBroadcastReceiver implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InitChannelsBroadcastReceiver : BroadcastReceiver() { | |
override fun onReceive(context: Context, intent: Intent?) { | |
if (defaultChannelAlreadyAdded) { | |
// Make sure we are not trying to re-create the channel if it's already added. | |
// You can save this on your shared preferences or database. | |
return | |
} | |
val channelHelper = PreviewChannelHelper(context) | |
val channel = Channel.Builder() | |
.setType(TvContractCompat.Channels.TYPE_PREVIEW) | |
.setInternalProviderId("default_channel") | |
.setDisplayName("ChannelSample: Hand picked recommendations!") | |
.setAppLinkIntentUri("content://channelsample.com/discover".toUri()) | |
.build() | |
val channelUri = context.contentResolver.insert( | |
TvContractCompat.Channels.CONTENT_URI, | |
channel.toContentValues() | |
) | |
if (channelUri != null) { | |
val channelId = ContentUris.parseId(channelUri) | |
val myChannels = channelHelper.allChannels.filter { | |
it.packageName == context.packageName | |
} | |
// If there are no browsable channels, i.e., channels visible on the TV Home screen, we can | |
// make the first channel browsable without asking the user permission. | |
if (myChannels.none { it.isBrowsable }) { | |
TvContractCompat.requestChannelBrowsable(context, channelId) | |
} | |
MovieList.list.forEach { movie -> | |
val program = movie.toPreviewProgram(channelId) | |
channelHelper.publishPreviewProgram(program) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment