Skip to content

Instantly share code, notes, and snippets.

View pflammertsma's full-sized avatar

Paul Lammertsma pflammertsma

View GitHub Profile
@pflammertsma
pflammertsma / MainActivity.kt
Created March 18, 2022 13:58
Sample for refresh rate switching with application focus
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
val params: WindowManager.LayoutParams = window.attributes
if (requestedMode.modeId !== params.preferredDisplayModeId) {
params.preferredDisplayModeId = requestedMode.modeId
window.attributes = params
}
@pflammertsma
pflammertsma / MainActivity.kt
Last active June 2, 2022 14:47
Sample for refresh rate switching during playback
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Determine whether the transition will be seamless.
// Non-seamless transitions may cause a 1-2 second black screen.
val refreshRates = display?.mode?.alternativeRefreshRates
val willBeSeamless = Arrays.asList<FloatArray>(refreshRates).contains(newRefreshRate)
if (willBeSeamless) {
// Set the frame rate, but only if the transition will be seamless.
surface.setFrameRate(newRefreshRate,
@pflammertsma
pflammertsma / build.gradle
Last active December 16, 2021 16:42
DropHelper release
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
implementation 'androidx.draganddrop:draganddrop:1.0.0-alpha02'
@pflammertsma
pflammertsma / MainActivity.kt
Last active December 16, 2021 16:42
Sample of DropHelper
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
DropHelper.configureView(
// Activity that will handle the drop
this,
// Target drop view to be highlighted
outerDropTarget,
// Supported MIME types
arrayOf(MIMETYPE_TEXT_PLAIN, "image/*"),
// Options for configuring drop targets
@pflammertsma
pflammertsma / MainActivity.kt
Last active December 16, 2021 16:42
Sample of DragStartHelper
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Make a view draggable to share a file. DragStartHelper takes care of
// intercepting drag gestures and attaching the listener.
DragStartHelper(draggableView) { view, _ ->
// Sets the appropriate MIME types automatically.
val dragClipData = ClipData.newUri(contentResolver, "File", fileUri)
// Set the visual look of the dragged object.
// Can be extended and customized; we use the default here.
setHeaderPresenterSelector(object : PresenterSelector() {
override fun getPresenter(item: Any?) = IconHeaderItemPresenter()
})
override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
val headerItem = (item as ListRow).headerItem
viewHolder.view.apply {
(findViewById<View>(R.id.header_icon) as ImageView).apply {
val drawableResId = when (headerItem.id) {
ID_POPULAR -> R.drawable.ic_popular
ID_EDITOR_PICKS -> R.drawable.ic_editor_picks
...
}
setImageDrawable(resources.getDrawable(drawableResId, null))
<androidx.leanback.widget.NonOverlappingLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/header_icon"
android:layout_width="32dp"
android:layout_height="32dp"
class IconHeaderItemPresenter : RowHeaderPresenter() {
override fun onCreateViewHolder(viewGroup: ViewGroup): ViewHolder {
// inflate layout
}
override fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any) {
// set text, icons, etc.
}
val program = PreviewProgram.Builder()
.setChannelId(channelId)
// Matches the intent filter in the manifest for VIEW action
.setIntentUri(Uri.parse("https://.../program/$id"))
.build()
helper.publishPreviewProgram(program)