Skip to content

Instantly share code, notes, and snippets.

@jsaund
jsaund / rx_bluetooth_scanner.java
Created February 23, 2017 09:37
Demonstration of using a RxJava to convert an asynchronous API in to a reactive one using the static helper fromEmitter
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.support.annotation.NonNull;
import rx.Emitter;
import rx.Observable;
import java.util.List;
public class RxBluetoothScanner {
@jsaund
jsaund / rx_shared_prefs.java
Created February 23, 2017 08:47
Demonstrates converting a synchronous operation, such as committing to shared preferences, to a reactive one by using the fromCallable static helper.
public Observable<Boolean> enablePushNotifications(boolean enable) {
return Observable.fromCallable(() -> sharedPrefs
.edit()
.putBoolean(KEY_PUSH_NOTIFICATIONS_PREFS, enable)
.commit());
}
@jsaund
jsaund / rx_file_reader.java
Last active May 9, 2023 10:50
Read files reactively using RxJava
public Observable<byte[]> readFile(@NonNull FileInputStream stream) {
final SyncOnSubscribe<FileInputStream, byte[]> fileReader = SyncOnSubscribe.createStateful(
() -> stream,
(stream, output) -> {
try {
final byte[] buffer = new byte[BUFFER_SIZE];
int count = stream.read(buffer);
if (count < 0) {
output.onCompleted();
} else {
public void touchEventHandler(@NotNull View view) {
final ConnectedObservable<MotionEvent> motionEventObservable = RxView.touches(view).publish();
// Capture down events
final Observable<MotionEvent> downEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_DOWN);
// Capture up events
final Observable<MotionEvent> upEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_UP);
// Show a red circle at the position where the down event ocurred
@jsaund
jsaund / share_op_ex.java
Last active May 24, 2018 09:22
Contrived example of using the share operator
public void touchEventHandler(@NotNull View view) {
final Observable<MotionEvent> motionEventObservable = RxView.touches(view).share();
// Capture down events
final Observable<MotionEvent> downEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_DOWN);
// Capture up events
final Observable<MotionEvent> upEventsObservable = motionEventObservable
.filter(event -> event.getAction() == MotionEvent.ACTION_UP);
// Show a red circle at the position where the down event ocurred
@jsaund
jsaund / rename.sh
Last active December 15, 2016 20:35
Simple batch rename files in folder to a specific format with leading zeros
#!/bin/bash
COUNT=0
RENAME_FILE="enter_new_filename_%02d.png"
for FILE in *.png; do
mv "$FILE" $(printf $RENAME_FILE $COUNT)
let COUNT=COUNT+1
done
public class MainActivity extends AppCompatActivity {
private static final String IMAGE_URL = "https://www.android.com/static/2016/img/logo-android-green_2x.png";
private static final int MSG_SHOW_PROGRESS = 1;
private static final int MSG_SHOW_IMAGE = 2;
private ProgressBar progressIndicator;
private ImageView imageView;
private Handler handler;
class ImageFetcher implements Runnable {
public class MainActivity extends AppCompatActivity {
private static final String IMAGE_URL = "https://www.android.com/static/2016/img/logo-android-green_2x.png";
private ProgressBar progressIndicator;
private ImageView imageView;
private Handler handler;
class ImageFetcher implements Runnable {
final String url;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Ping";
private Handler handler;
class PingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "Ping message received");
}
public class MainActivity extends AppCompatActivity {
private static final String IMAGE_URL = "https://www.android.com/static/img/android.png";
private static final int MSG_SHOW_PROGRESS = 1;
private static final int MSG_SHOW_IMAGE = 2;
private ProgressBar progressIndicator;
private ImageView imageView;
private Handler handler;