1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
package cgeo.geocaching.utils;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Scheduler;
import rx.Scheduler.Worker;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.functions.Func0;
import rx.functions.Func1;
import rx.internal.util.RxThreadFactory;
import rx.observables.BlockingObservable;
import rx.observers.Subscribers;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
import rx.subscriptions.Subscriptions;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class RxUtils {
private RxUtils() {
// Utility class, not to be instantiated
}
public final static Scheduler computationScheduler = Schedulers.computation();
public static final Scheduler networkScheduler = Schedulers.from(Executors.newFixedThreadPool(10, new RxThreadFactory("network-")));
public static final Scheduler refreshScheduler = Schedulers.from(Executors.newFixedThreadPool(3, new RxThreadFactory("refresh-")));
private static final HandlerThread looperCallbacksThread =
new HandlerThread("looper callbacks", Process.THREAD_PRIORITY_DEFAULT);
static {
looperCallbacksThread.start();
}
public static final Looper looperCallbacksLooper = looperCallbacksThread.getLooper();
public static final Scheduler looperCallbacksScheduler = AndroidSchedulers.handlerThread(new Handler(looperCallbacksLooper));
public static final Worker looperCallbacksWorker = looperCallbacksScheduler.createWorker();
public static <T> void waitForCompletion(final BlockingObservable<T> observable) {
observable.lastOrDefault(null);
}
public static void waitForCompletion(final Observable<?>... observables) {
waitForCompletion(Observable.merge(observables).toBlocking());
}
/**
* Subscribe function whose subscription and unsubscription take place on a looper thread.
*
* @param <T>
* the type of the observable
*/
public static abstract class LooperCallbacks<T> implements OnSubscribe<T> {
final AtomicInteger counter = new AtomicInteger(0);
final long stopDelay;
final TimeUnit stopDelayUnit;
final protected PublishSubject<T> subject = PublishSubject.create();
public LooperCallbacks(final long stopDelay, final TimeUnit stopDelayUnit) {
this.stopDelay = stopDelay;
this.stopDelayUnit = stopDelayUnit;
}
public LooperCallbacks() {
this(0, TimeUnit.SECONDS);
}
@Override
final public void call(final Subscriber<? super T> subscriber) {
subscriber.add(subject.subscribe(Subscribers.from(subscriber)));
looperCallbacksWorker.schedule(new Action0() {
@Override
public void call() {
if (counter.getAndIncrement() == 0) {
onStart();
}
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
looperCallbacksWorker.schedule(new Action0() {
@Override
public void call() {
if (counter.decrementAndGet() == 0) {
onStop();
}
}
}, stopDelay, stopDelayUnit);
}
}));
}
});
}
abstract protected void onStart();
abstract protected void onStop();
}
public static<T> Observable<T> rememberLast(final Observable<T> observable, final T initialValue) {
final AtomicReference<T> lastValue = new AtomicReference<>(initialValue);
return observable.doOnNext(new Action1<T>() {
@Override
public void call(final T value) {
lastValue.set(value);
}
}).startWith(Observable.defer(new Func0<Observable<T>>() {
@Override
public Observable<T> call() {
final T last = lastValue.get();
return last != null ? Observable.just(last) : Observable.<T>empty();
}
})).replay(1).refCount();
}
public static <T> void andThenOnUi(final Scheduler scheduler, final Func0<T> background, final Action1<T> foreground) {
scheduler.createWorker().schedule(new Action0() {
@Override
public void call() {
final T value = background.call();
AndroidSchedulers.mainThread().createWorker().schedule(new Action0() {
@Override
public void call() {
foreground.call(value);
}
});
}
});
}
public static void andThenOnUi(final Scheduler scheduler, final Action0 background, final Action0 foreground) {
scheduler.createWorker().schedule(new Action0() {
@Override
public void call() {
background.call();
AndroidSchedulers.mainThread().createWorker().schedule(foreground);
}
});
}
/**
* Cache the last value of observables so that every key is associated to only one of them.
*
* @param <K> the type of the key
* @param <V> the type of the value
*/
public static class ObservableCache<K, V> {
final private Func1<K, Observable<V>> func;
final private Map<K, Observable<V>> cached = new HashMap<>();
/**
* Create a new observables cache.
*
* @param func the function transforming a key into an observable
*/
public ObservableCache(final Func1<K, Observable<V>> func) {
this.func = func;
}
/**
* Get the observable corresponding to a key. If the key has not already been
* seen, the function passed to the constructor will be called to build the observable
* <p/>
* If the observable has already emitted values, only the last one will be remembered.
*
* @param key the key
* @return the observable corresponding to the key
*/
public synchronized Observable<V> get(final K key) {
if (cached.containsKey(key)) {
return cached.get(key);
}
final Observable<V> value = func.call(key).replay(1).refCount();
cached.put(key, value);
return value;
}
}
}
|