Skip to content

Instantly share code, notes, and snippets.

adb shell cmd package compile -m speed-profile -f <package name>
adb shell am force-stop <package name>
# Download record_android_trace following the instructions here:
# https://perfetto.dev/docs/quickstart/android-tracing#recording-a-trace-through-the-cmdline
~/android-scripts/traces/record_android_trace -c /path/to/default_perfetto_config.textproto
@benbaxter
benbaxter / TwoTruths.java
Created August 25, 2020 16:25
two truths and a lie
public void two_truths_and_a_lie() {
assertThat(benjamin.lovesRoadTrips()).isTrue()
assertThat(benjamin.lovesSkiing()).isTrue()
assertThat(benjamin.lovesPizza()).isTrue()
}
@benbaxter
benbaxter / talkback.sh
Created July 31, 2020 21:14
Talkback enable/disable script
# Copyright 2020 Google LLC.
# SPDX-License-Identifier: Apache-2.0
function talkback() {
if [[ -z $1 ]]; then
echo "Usage: $0 [on/off]"
exit
fi
if [[ $1 == 'on' ]]; then
# 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'
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;
}
//...
}
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 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) {
@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
@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 / 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);
}
}