Skip to content

Instantly share code, notes, and snippets.

@ademirqueiroga
Last active October 6, 2024 14:30
Show Gist options
  • Save ademirqueiroga/589a0559b6c161db943e51eebd77a0f9 to your computer and use it in GitHub Desktop.
Save ademirqueiroga/589a0559b6c161db943e51eebd77a0f9 to your computer and use it in GitHub Desktop.
InitChannelsBroadcastReceiver implementation
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