Skip to content

Commit 87fd889

Browse files
Merge pull request #318 from mttkay/android
Initial support for scheduling on Android Handler threads
2 parents 257a744 + 6f18d8b commit 87fd889

File tree

4 files changed

+210
-1
lines changed

4 files changed

+210
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
apply plugin: 'java'
2+
apply plugin: 'eclipse'
3+
apply plugin: 'idea'
4+
apply plugin: 'osgi'
5+
6+
sourceCompatibility = JavaVersion.VERSION_1_6
7+
targetCompatibility = JavaVersion.VERSION_1_6
8+
9+
dependencies {
10+
compile project(':rxjava-core')
11+
provided 'junit:junit-dep:4.10'
12+
provided 'org.mockito:mockito-core:1.8.5'
13+
provided 'org.robolectric:robolectric:2.1.1'
14+
provided 'com.google.android:android:4.0.1.2'
15+
}
16+
17+
eclipse {
18+
classpath {
19+
// include 'provided' dependencies on the classpath
20+
plusConfigurations += configurations.provided
21+
22+
downloadSources = true
23+
downloadJavadoc = true
24+
}
25+
}
26+
27+
idea {
28+
module {
29+
// include 'provided' dependencies on the classpath
30+
scopes.PROVIDED.plus += configurations.provided
31+
}
32+
}
33+
34+
javadoc {
35+
options {
36+
doclet = "org.benjchristensen.doclet.DocletExclude"
37+
docletpath = [rootProject.file('./gradle/doclet-exclude.jar')]
38+
stylesheetFile = rootProject.file('./gradle/javadocStyleSheet.css')
39+
windowTitle = "RxJava Android Javadoc ${project.version}"
40+
}
41+
options.addStringOption('top').value = '<h2 class="title" style="padding-top:40px">RxJava Android</h2>'
42+
}
43+
44+
jar {
45+
manifest {
46+
name = 'rxjava-android'
47+
instruction 'Bundle-Vendor', 'Netflix'
48+
instruction 'Bundle-DocURL', 'https://github.com/Netflix/RxJava'
49+
instruction 'Import-Package', '!org.junit,!junit.framework,!org.mockito.*,*'
50+
}
51+
}
52+
53+
test {
54+
testLogging {
55+
exceptionFormat "full"
56+
events "started"
57+
displayGranularity 2
58+
}
59+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package rx.android.concurrency;
2+
3+
import android.os.Handler;
4+
import android.os.Looper;
5+
import rx.Scheduler;
6+
7+
/**
8+
* Schedulers that have Android specific functionality
9+
*/
10+
public class AndroidSchedulers {
11+
12+
private static final Scheduler MAIN_THREAD_SCHEDULER =
13+
new HandlerThreadScheduler(new Handler(Looper.getMainLooper()));
14+
15+
private AndroidSchedulers(){
16+
17+
}
18+
19+
/**
20+
* {@link Scheduler} which uses the provided {@link Handler} to execute an action
21+
* @param handler The handler that will be used when executing the action
22+
* @return A handler based scheduler
23+
*/
24+
public static Scheduler handlerThread(final Handler handler) {
25+
return new HandlerThreadScheduler(handler);
26+
}
27+
28+
/**
29+
* {@link Scheduler} which will execute an action on the main Android UI thread.
30+
*
31+
* @return A Main {@link Looper} based scheduler
32+
*/
33+
public static Scheduler mainThread() {
34+
return MAIN_THREAD_SCHEDULER;
35+
}
36+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package rx.android.concurrency;
2+
3+
import android.os.Handler;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.ArgumentCaptor;
7+
import org.robolectric.RobolectricTestRunner;
8+
import org.robolectric.annotation.Config;
9+
import rx.Scheduler;
10+
import rx.Subscription;
11+
import rx.operators.AtomicObservableSubscription;
12+
import rx.util.functions.Func2;
13+
14+
import java.util.concurrent.TimeUnit;
15+
16+
import static org.mockito.Matchers.eq;
17+
import static org.mockito.Mockito.mock;
18+
import static org.mockito.Mockito.verify;
19+
20+
/**
21+
* Schedules actions to run on an Android Handler thread.
22+
*/
23+
public class HandlerThreadScheduler extends Scheduler {
24+
25+
private final Handler handler;
26+
27+
/**
28+
* Constructs a {@link HandlerThreadScheduler} using the given {@link Handler}
29+
* @param handler {@link Handler} to use when scheduling actions
30+
*/
31+
public HandlerThreadScheduler(Handler handler) {
32+
this.handler = handler;
33+
}
34+
35+
/**
36+
* Calls {@link HandlerThreadScheduler#schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
37+
* with a delay of zero milliseconds.
38+
*
39+
* See {@link #schedule(Object, rx.util.functions.Func2, long, java.util.concurrent.TimeUnit)}
40+
*/
41+
@Override
42+
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action) {
43+
return schedule(state, action, 0L, TimeUnit.MILLISECONDS);
44+
}
45+
46+
/**
47+
* Calls {@link Handler#postDelayed(Runnable, long)} with a runnable that executes the given action.
48+
* @param state
49+
* State to pass into the action.
50+
* @param action
51+
* Action to schedule.
52+
* @param delayTime
53+
* Time the action is to be delayed before executing.
54+
* @param unit
55+
* Time unit of the delay time.
56+
* @return A Subscription from which one can unsubscribe from.
57+
*/
58+
@Override
59+
public <T> Subscription schedule(final T state, final Func2<Scheduler, T, Subscription> action, long delayTime, TimeUnit unit) {
60+
final AtomicObservableSubscription subscription = new AtomicObservableSubscription();
61+
final Scheduler _scheduler = this;
62+
handler.postDelayed(new Runnable() {
63+
@Override
64+
public void run() {
65+
subscription.wrap(action.call(_scheduler, state));
66+
}
67+
}, unit.toMillis(delayTime));
68+
return subscription;
69+
}
70+
71+
@RunWith(RobolectricTestRunner.class)
72+
@Config(manifest=Config.NONE)
73+
public static final class UnitTest {
74+
75+
@Test
76+
public void shouldScheduleImmediateActionOnHandlerThread() {
77+
final Handler handler = mock(Handler.class);
78+
final Object state = new Object();
79+
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
80+
81+
Scheduler scheduler = new HandlerThreadScheduler(handler);
82+
scheduler.schedule(state, action);
83+
84+
// verify that we post to the given Handler
85+
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
86+
verify(handler).postDelayed(runnable.capture(), eq(0L));
87+
88+
// verify that the given handler delegates to our action
89+
runnable.getValue().run();
90+
verify(action).call(scheduler, state);
91+
}
92+
93+
@Test
94+
public void shouldScheduleDelayedActionOnHandlerThread() {
95+
final Handler handler = mock(Handler.class);
96+
final Object state = new Object();
97+
final Func2<Scheduler, Object, Subscription> action = mock(Func2.class);
98+
99+
Scheduler scheduler = new HandlerThreadScheduler(handler);
100+
scheduler.schedule(state, action, 1L, TimeUnit.SECONDS);
101+
102+
// verify that we post to the given Handler
103+
ArgumentCaptor<Runnable> runnable = ArgumentCaptor.forClass(Runnable.class);
104+
verify(handler).postDelayed(runnable.capture(), eq(1000L));
105+
106+
// verify that the given handler delegates to our action
107+
runnable.getValue().run();
108+
verify(action).call(scheduler, state);
109+
}
110+
}
111+
}
112+
113+

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ include 'rxjava-core', \
44
'language-adaptors:rxjava-jruby', \
55
'language-adaptors:rxjava-clojure', \
66
'language-adaptors:rxjava-scala', \
7-
'rxjava-contrib:rxjava-swing'
7+
'rxjava-contrib:rxjava-swing', \
8+
'rxjava-contrib:rxjava-android'

0 commit comments

Comments
 (0)