Skip to content

Instantly share code, notes, and snippets.

@benbaxter
benbaxter / PlaybackSeekMetadataDataProvider.java
Last active August 21, 2017 21:13
Calculating seek positions for PlaybackSeekMetadataDataProvider
public class PlaybackSeekMetadataDataProvider extends PlaybackSeekDataProvider {
//…
public PlaybackSeekMetadataDataProvider(Context context,
String videoUrl,
long interval) {
mContext = context;
mVideoUrl = videoUrl;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(mVideoUrl, new HashMap<>());
@benbaxter
benbaxter / GetThumbnail.java
Created August 21, 2017 21:16
Steps to get a thumbnail
@Override
public void getThumbnail(int index, final ResultCallback callback) {
// Step 1. Get position from the index.
long position = getSeekPositions()[index];
// Step 2. Retrieve thumbnail in the background to not block the UI thread.
retrieveThumbnailForPosition(position, index, (bitmap) -> {
// Step 3. Return the thumbnail to be presented in the UI.
callback.onThumbnailLoaded(bitmap, index);
});
@benbaxter
benbaxter / LoadBitmapAsyncTask.java
Last active August 21, 2017 21:19
Loads a bitmap from an async task.
@Override
public void getThumbnail(int index, ResultCallback callback) {
LoadBitmapAsyncTask task = new LoadBitmapAsyncTask(index, callback);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
class LoadBitmapAsyncTask extends AsyncTask<Void, Void, Bitmap> {
final int mIndex;
final ResultCallback mResultCallback;
@benbaxter
benbaxter / GetThumbnailAsync.java
Created August 21, 2017 21:21
Cache background tasks per index.
@Override
public void getThumbnail(int index, ResultCallback callback) {
LoadBitmapAsyncTask task = mTasks.get(index);
if (task == null) {
task = new LoadBitmapAsyncTask(index, callback);
mTasks.put(index, task);
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
@benbaxter
benbaxter / ResetThumbnailTasks.java
Created August 21, 2017 21:36
Leanback hook to release memory.
@Override
public void reset() {
for (int i = 0; i < mTasks.size(); i++) {
LoadBitmapAsyncTask task = mTasks.get(i);
task.cancel(true);
}
mTasks.clear();
}
@benbaxter
benbaxter / DummyItemCardPresenter.kt
Last active December 15, 2017 02:05
Sample code to experiment using DiffCallback
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
public abstract class DiffCallback<Value> {
public abstract boolean areItemsTheSame(@NonNull Value oldItem,
@NonNull Value newItem);
public abstract boolean areContentsTheSame(@NonNull Value oldItem,
@NonNull Value newItem);
@SuppressWarnings("WeakerAccess")
public Object getChangePayload(@NonNull Value oldItem, @NonNull Value newItem) {
val diffCallback = object : DiffCallback<DummyItem>() {
override fun areItemsTheSame(oldItem: DummyItem, newItem: DummyItem): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: DummyItem, newItem: DummyItem): Boolean =
oldItem == newItem
}
itemsAdapter.setItems(randomItems(), diffCallback)
public void setItems(final List itemList, final DiffCallback callback) {
if (callback == null) {
// shortcut when DiffCallback is not provided
mItems.clear();
mItems.addAll(itemList);
notifyChanged();
return;
}
//...
}
# This is for ATV only
alias adb-dev-options='adb shell am start -n com.android.tv.settings/.system.development.DevelopmentActivity'
# For mobile use:
#alias adb-dev-options='adb shell am start -n com.android.settings/.DevelopmentSettings'
# Animations
alias animations-off='adb shell settings put global animator_duration_scale 0'
alias animations-slow='adb shell settings put global animator_duration_scale 10'
alias animations-normal='adb shell settings put global animator_duration_scale 1'
alias animations-fast='adb shell settings put global animator_duration_scale 0.5'