├── .gitignore ├── Main.xmind ├── README.md ├── RxJava.md ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── union │ │ └── uc │ │ └── com │ │ └── rxjava_example │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── union │ │ │ └── uc │ │ │ └── com │ │ │ └── rxjava_example │ │ │ ├── App.java │ │ │ ├── MainActivity.java │ │ │ ├── api │ │ │ ├── AsyncActivity.java │ │ │ ├── BlockingObservableActivity.java │ │ │ ├── CombineActivity.java │ │ │ ├── ConditionActivity.java │ │ │ ├── ConnectableObservableActivity.java │ │ │ ├── CustomeOperatorActivity.java │ │ │ ├── ErrorHandleActivity.java │ │ │ ├── FilterActivity.java │ │ │ ├── MathAggregateActivity.java │ │ │ ├── ObservableCreateActivity.java │ │ │ ├── PluginActivity.java │ │ │ ├── ReactiveStreamActivity.java │ │ │ ├── SchedulerActivity.java │ │ │ ├── StringActivity.java │ │ │ ├── SubjectActivity.java │ │ │ ├── TransformationActivity.java │ │ │ └── UtilityActivity.java │ │ │ ├── base │ │ │ ├── APIBaseActivity.java │ │ │ ├── AsyncExecutor.java │ │ │ ├── BaseActivity.java │ │ │ ├── Tuple.java │ │ │ └── UIThreadExecutor.java │ │ │ ├── contants │ │ │ └── Constants.java │ │ │ ├── plugin │ │ │ ├── DescriptionPlugin.java │ │ │ ├── DisplayPluginManager.java │ │ │ ├── MarbleDiagram.java │ │ │ ├── MarbleDiagramPlugin.java │ │ │ ├── SampleCode.java │ │ │ └── SampleCodePlugin.java │ │ │ ├── sample │ │ │ ├── AdFetchActivity.java │ │ │ ├── AutoCompleteActivity.java │ │ │ ├── BufferActivity.java │ │ │ ├── ConditionActivity.java │ │ │ ├── GroupByActivity.java │ │ │ ├── OtherAPIActivity.java │ │ │ └── RWFileActivity.java │ │ │ └── ui │ │ │ ├── SeperatorView.java │ │ │ └── Utils.java │ └── res │ │ ├── drawable-v21 │ │ ├── ic_menu_camera.xml │ │ ├── ic_menu_gallery.xml │ │ ├── ic_menu_manage.xml │ │ ├── ic_menu_send.xml │ │ ├── ic_menu_share.xml │ │ └── ic_menu_slideshow.xml │ │ ├── drawable │ │ ├── all_png.png │ │ ├── amb_png.png │ │ ├── and_then_when_c_png.png │ │ ├── and_then_when_c_png_1.png │ │ ├── average_png.png │ │ ├── average_png_1.png │ │ ├── average_png_2.png │ │ ├── average_png_3.png │ │ ├── average_png_4.png │ │ ├── average_png_5.png │ │ ├── average_png_6.png │ │ ├── average_png_7.png │ │ ├── b_foreach_png.png │ │ ├── b_getiterator_png.png │ │ ├── b_last_png.png │ │ ├── b_lastordefault_png.png │ │ ├── b_mostrecent_png.png │ │ ├── b_next_png.png │ │ ├── b_next_png_1.png │ │ ├── b_single_png.png │ │ ├── b_singleordefault_png.png │ │ ├── b_tofuture_png.png │ │ ├── b_toiterable_png.png │ │ ├── b_toiterable_png_1.png │ │ ├── buffer1_png.png │ │ ├── cache_png.png │ │ ├── cast_png.png │ │ ├── combinelatest_png.png │ │ ├── concat_png.png │ │ ├── concatmap_png.png │ │ ├── contains_png.png │ │ ├── create_png.png │ │ ├── debounce_png.png │ │ ├── defaultifempty_png.png │ │ ├── defaultifempty_png_1.png │ │ ├── defer_png.png │ │ ├── deferfuture_png.png │ │ ├── delay_oo_png.png │ │ ├── delay_oo_png_1.png │ │ ├── delaysubscription_o_png.png │ │ ├── delaysubscription_o_png_1.png │ │ ├── dematerialize_c_png.png │ │ ├── distinct_key_png.png │ │ ├── distinctuntilchanged_png.png │ │ ├── distinctuntilchanged_png_1.png │ │ ├── dooncompleted_png.png │ │ ├── dooneach_png.png │ │ ├── doonerror_png.png │ │ ├── doonerror_png_1.png │ │ ├── doonsubscribe_png.png │ │ ├── doonterminate_png.png │ │ ├── doonunsubscribe_png.png │ │ ├── dowhile_png.png │ │ ├── elementat_png.png │ │ ├── elementatordefault_png.png │ │ ├── empty_png.png │ │ ├── error_png.png │ │ ├── exists_png.png │ │ ├── filter_png.png │ │ ├── finallydo_png.png │ │ ├── first_png.png │ │ ├── firstordefault_png.png │ │ ├── flatmap_png.png │ │ ├── foreachfuture_png.png │ │ ├── from_future_png.png │ │ ├── from_png.png │ │ ├── fromaction_png.png │ │ ├── fromcallable_png.png │ │ ├── fromrunnable_png.png │ │ ├── groupby_png.png │ │ ├── groupjoin_png.png │ │ ├── groupjoin_png_1.png │ │ ├── ifthen_png.png │ │ ├── ignoreelements_png.png │ │ ├── ignoreelements_png_1.png │ │ ├── isempty_png.png │ │ ├── join__png.png │ │ ├── join__png_1.png │ │ ├── just_png.png │ │ ├── last_png.png │ │ ├── lastordefault_png.png │ │ ├── map_png.png │ │ ├── materialize_png.png │ │ ├── materialize_png_1.png │ │ ├── max_png.png │ │ ├── maxby_png.png │ │ ├── merge_png.png │ │ ├── mergedelayerror_png.png │ │ ├── mergemapiterable_png.png │ │ ├── mergemapiterable_png_1.png │ │ ├── min_png.png │ │ ├── min_png_1.png │ │ ├── minby_png.png │ │ ├── never_png.png │ │ ├── observeon_png.png │ │ ├── ofclass_png.png │ │ ├── onerrorresumenext_png.png │ │ ├── onerrorreturn_png.png │ │ ├── onexceptionresumenextviaobservable_png.png │ │ ├── publishconnect_png.png │ │ ├── publishconnect_png_1.png │ │ ├── publishrefcount_c_png.png │ │ ├── publishrefcount_c_png_1.png │ │ ├── range_png.png │ │ ├── repeat_o_png.png │ │ ├── repeatwhen_f_png.png │ │ ├── replay_c_png.png │ │ ├── retry_png.png │ │ ├── retry_png_1.png │ │ ├── retrywhen_f_png.png │ │ ├── s_asyncsubject_png.png │ │ ├── s_asyncsubject_png_1.png │ │ ├── s_behaviorsubject_png.png │ │ ├── s_behaviorsubject_png_1.png │ │ ├── s_behaviorsubject_png_2.png │ │ ├── s_publishsubject_png.png │ │ ├── s_replaysubject_png.png │ │ ├── s_replaysubject_png_1.png │ │ ├── s_replaysubject_png_2.png │ │ ├── sample_png.png │ │ ├── scan_png.png │ │ ├── sequenceequal_png.png │ │ ├── sequenceequal_png_1.png │ │ ├── side_nav_bar.xml │ │ ├── single_png.png │ │ ├── singleordefault_png.png │ │ ├── skip_png.png │ │ ├── skiplast_png.png │ │ ├── skiplast_png_1.png │ │ ├── skipuntil_png.png │ │ ├── skipuntil_png_1.png │ │ ├── skipwhile_png.png │ │ ├── st_byline_png.png │ │ ├── st_decode_png.png │ │ ├── st_encode_png.png │ │ ├── st_from_png.png │ │ ├── st_join_png.png │ │ ├── st_split_png.png │ │ ├── st_split_png_1.png │ │ ├── st_stringconcat_png.png │ │ ├── start_png.png │ │ ├── startfuture_png.png │ │ ├── startwith_o_png.png │ │ ├── subscribeon_png.png │ │ ├── sum_f_png.png │ │ ├── sum_f_png_1.png │ │ ├── sum_f_png_2.png │ │ ├── sum_f_png_3.png │ │ ├── sum_f_png_4.png │ │ ├── switchcase_png.png │ │ ├── switchdo_png.png │ │ ├── switchmap_png.png │ │ ├── synchronize_png.png │ │ ├── take_png.png │ │ ├── takefirstn_png.png │ │ ├── takefirstn_png_1.png │ │ ├── takelast_t_png.png │ │ ├── takelastbuffer_png.png │ │ ├── takelastbuffer_png_1.png │ │ ├── takeuntil_png.png │ │ ├── takewhile_png.png │ │ ├── takewhile_png_1.png │ │ ├── throttlefirst_png.png │ │ ├── throttlelast_png.png │ │ ├── throttlelast_png_1.png │ │ ├── throttlewithtimeout_png.png │ │ ├── timeinterval_png.png │ │ ├── timer_p_png.png │ │ ├── timer_p_png_1.png │ │ ├── timer_png.png │ │ ├── timestamp_png.png │ │ ├── toasync_png.png │ │ ├── using_png.png │ │ ├── using_png_1.png │ │ ├── whiledo_png.png │ │ ├── window1_png.png │ │ └── zip_png.png │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── app_bar_main.xml │ │ ├── content_main.xml │ │ └── nav_header_main.xml │ │ ├── menu │ │ ├── activity_main_drawer.xml │ │ ├── main.xml │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-en │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── drawables.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values-zh │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── drawables.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── union │ └── uc │ └── com │ └── rxjava_example │ └── ObservableTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img_scale.py ├── marble_diagram_generator.py ├── rxjava-example.iml ├── rxjava-examples.iml ├── sample_code_generator.py ├── settings.gradle └── wiki ├── code_analyse.markdown ├── home.markdown └── res ├── Thumbs.db ├── main-classes.dia ├── main-classes.dia.autosave ├── main-classes.png ├── main-sequence.dia.autosave ├── main-sequence.dia~ ├── main.dia ├── range_map.dia ├── range_map.dia~ ├── rxjava-example-design.xml ├── subscribe-sequence.dia ├── subscribe-sequence.png ├── subscrive-sequence-uml.dia └── subscrive-sequence-uml.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # Log Files 26 | *.log 27 | 28 | # Android Studio Navigation editor temp files 29 | .navigation/ 30 | 31 | # Android Studio captures folder 32 | captures/ 33 | .idea/ 34 | *.swp 35 | *.swo 36 | -------------------------------------------------------------------------------- /Main.xmind: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leeowenowen/rxjava-examples/995c3236ca35f9e9975b51fe0c96eb39e1f9f264/Main.xmind -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #RxJava 示例工程 2 | 3 | #理解RxJava API的一个关键是明白API需要Observable的Case是异步使用场景,而返回普通对象类型的是同步使用场景, 比如flatmap与map, buffer中的多个重载等。 4 | 5 | 讲解RxJava如何使用的示例工程,你可以将它安装在android手机上,然后就可以随手学习RxJava的api了. 6 | 点击API名称,可以看到源码,源码输出以及对应的marble-diagram. 7 | >API描述目前支持中文和英文两种语言, 如果你的Android设备使用的是中文,那么你看到的API描述会跟我如下的截图有所不同. 8 | 9 | # rxjava-examples 10 | rxjava-example android project 11 | 12 | Show how to use RxJava API, you can install the [Demo APK](https://raw.githubusercontent.com/wiki/leeowenowen/rxjava-examples/apk/app-debug.apk) on your android phone, and then you 13 | can learn RxJava API everywhere! 14 | 15 | click the api name, and you can see the source code, output and relevant marble diagram at the same time. 16 | all the source codes show on the sample gui are generated from my project source, and all the marble diagram is loaded from 17 | load. 18 | 19 | [Download APK](https://raw.githubusercontent.com/wiki/leeowenowen/rxjava-examples/apk/app-debug.apk) 20 | 21 | 22 | ![Barcode/二维码](https://raw.githubusercontent.com/wiki/leeowenowen/rxjava-examples/res/barcode.png) 23 | 24 | ![Demo Screenshot1](https://raw.githubusercontent.com/wiki/leeowenowen/rxjava-examples/res/rxjava-1.png) 25 | 26 | 27 | ![Demo Screenshot2](https://raw.githubusercontent.com/wiki/leeowenowen/rxjava-examples/res/rxjava-2.png) 28 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "union.uc.com.rxjava_example" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.2.1' 26 | compile 'com.android.support:design:23.2.1' 27 | compile 'io.reactivex:rxjava:1.0.10' 28 | compile 'io.reactivex:rxjava-string:1.+' 29 | // compile 'io.reactivex:rxjava-file-utils:0.+' 30 | // compile 'io.reactivex:rxjava-debug:1.+' 31 | // compile 'io.reactivex:rxjava-guava:1.+' 32 | compile 'io.reactivex:rxjava-reactive-streams:1.+' 33 | compile 'io.reactivex:rxjava-joins:0.+' 34 | compile 'io.reactivex:rxjava-math:1.+' 35 | compile 'io.reactivex:rxjava-async-util:0.+' 36 | compile 'io.reactivex:rxjava-computation-expressions:0.+' 37 | androidTestCompile 'junit:junit:4.12' 38 | compile 'us.feras.mdv:markdownview:1.1.0' 39 | compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' 40 | 41 | compile 'com.nineoldandroids:library:2.4.0' 42 | compile 'com.daimajia.easing:library:1.0.1@aar' 43 | compile 'com.daimajia.androidanimations:library:1.1.3@aar' 44 | } 45 | task showMeCache << { 46 | configurations.compile.each { println it } 47 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/wangli/env/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/union/uc/com/rxjava_example/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 17 | 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 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/App.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example; 2 | 3 | import android.app.Application; 4 | 5 | import com.nostra13.universalimageloader.core.ImageLoader; 6 | import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; 7 | 8 | import union.uc.com.rxjava_example.plugin.DescriptionPlugin; 9 | import union.uc.com.rxjava_example.plugin.DisplayPluginManager; 10 | import union.uc.com.rxjava_example.plugin.MarbleDiagramPlugin; 11 | import union.uc.com.rxjava_example.plugin.SampleCodePlugin; 12 | 13 | /** 14 | * Created by wangli on 4/16/16. 15 | */ 16 | public class App extends Application { 17 | @Override 18 | public void onCreate() { 19 | super.onCreate(); 20 | //init 21 | // Create global configuration and initialize ImageLoader with this config 22 | ImageLoaderConfiguration config = ImageLoaderConfiguration.createDefault(this); 23 | ImageLoader.getInstance().init(config); 24 | 25 | //init DisplayPluginManager 26 | DisplayPluginManager dpm = DisplayPluginManager.singleton(); 27 | dpm.register(new MarbleDiagramPlugin()); 28 | dpm.register(new DescriptionPlugin()); 29 | dpm.register(new SampleCodePlugin()); 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/AsyncActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import java.util.concurrent.Callable; 4 | import java.util.concurrent.Future; 5 | 6 | import rx.Observable; 7 | import rx.Observer; 8 | import rx.Subscription; 9 | import rx.functions.Action0; 10 | import rx.functions.Action1; 11 | import rx.functions.Action2; 12 | import rx.functions.Func0; 13 | import rx.schedulers.Schedulers; 14 | import rx.util.async.Async; 15 | import union.uc.com.rxjava_example.base.APIBaseActivity; 16 | import union.uc.com.rxjava_example.base.AsyncExecutor; 17 | import union.uc.com.rxjava_example.contants.Constants; 18 | 19 | /** 20 | * Created by wangli on 4/12/16. 21 | */ 22 | public class AsyncActivity extends APIBaseActivity { 23 | 24 | @Override 25 | protected void onRegisterAction(ActionRegistery registery) { 26 | registery.add(Constants.Async.start, new Runnable() { 27 | @Override 28 | public void run() { 29 | Async.start(new Func0() { 30 | @Override 31 | public Integer call() { 32 | log("action run on " + Thread.currentThread().getName()); 33 | return 3; 34 | } 35 | }).subscribe(new Action1() { 36 | @Override 37 | public void call(Integer integer) { 38 | log(integer); 39 | } 40 | }); 41 | } 42 | }); 43 | registery.add(Constants.Async.toAsync, new Runnable() { 44 | @Override 45 | public void run() { 46 | Async.toAsync(new Action0() { 47 | @Override 48 | public void call() { 49 | log("action run on " + Thread.currentThread().getName()); 50 | log("Action0.call ...on subscribe"); 51 | } 52 | }).call(); 53 | logLineSeperator(); 54 | Async.toAsync(new Action0() { 55 | @Override 56 | public void call() { 57 | log("action run on " + Thread.currentThread().getName()); 58 | log("Action0.call"); 59 | } 60 | }).call().subscribe(new Action1() { 61 | @Override 62 | public void call(Void aVoid) { 63 | log("Action1.call"); 64 | } 65 | }); 66 | } 67 | }); 68 | registery.add(Constants.Async.startFuture, new Runnable() { 69 | @Override 70 | public void run() { 71 | Async.startFuture(new Func0>() { 72 | @Override 73 | public Future call() { 74 | return AsyncExecutor.SINGLETON.submit(new Callable() { 75 | @Override 76 | public Integer call() throws Exception { 77 | return 3; 78 | } 79 | }); 80 | } 81 | }).subscribe(new Action1() { 82 | @Override 83 | public void call(Integer integer) { 84 | log(integer); 85 | } 86 | }); 87 | } 88 | }); 89 | registery.add(Constants.Async.deferFuture, new Runnable() { 90 | @Override 91 | public void run() { 92 | Async.deferFuture(new Func0>>() { 93 | @Override 94 | public Future> call() { 95 | return AsyncExecutor.SINGLETON.submit(new Callable>() { 96 | @Override 97 | public Observable call() throws Exception { 98 | return Observable.just(1, 2, 3); 99 | } 100 | }); 101 | } 102 | }).subscribe(new Action1() { 103 | @Override 104 | public void call(Integer integer) { 105 | log(integer); 106 | } 107 | }); 108 | } 109 | }); 110 | registery.add(Constants.Async.forEachFuture, new Runnable() { 111 | @Override 112 | public void run() { 113 | Future f = Async.forEachFuture(Observable.just(1, 2, 3), new Action1() { 114 | @Override 115 | public void call(Integer integer) { 116 | log(integer); 117 | } 118 | }, new Action1() { 119 | @Override 120 | public void call(Throwable throwable) { 121 | log(throwable); 122 | } 123 | }); 124 | log("task done:" + f.isDone()); 125 | } 126 | }); 127 | registery.add(Constants.Async.fromAction, new Runnable() { 128 | @Override 129 | public void run() { 130 | Async.fromAction(new Action0() { 131 | @Override 132 | public void call() { 133 | log("action run on " + Thread.currentThread().getName()); 134 | log("Action0.call"); 135 | } 136 | }, 3).subscribe(new Action1() { 137 | @Override 138 | public void call(Integer integer) { 139 | log(integer); 140 | } 141 | }); 142 | } 143 | }); 144 | registery.add(Constants.Async.fromCallable, new Runnable() { 145 | @Override 146 | public void run() { 147 | Async.fromCallable(new Callable() { 148 | @Override 149 | public Integer call() throws Exception { 150 | log("action run on " + Thread.currentThread().getName()); 151 | return 3; 152 | } 153 | }).subscribe(new Action1() { 154 | @Override 155 | public void call(Integer integer) { 156 | log(integer); 157 | } 158 | }); 159 | } 160 | }); 161 | registery.add(Constants.Async.fromRunnable, new Runnable() { 162 | @Override 163 | public void run() { 164 | Async.fromRunnable(new Runnable() { 165 | @Override 166 | public void run() { 167 | log("Runnable.run on " + Thread.currentThread().getName()); 168 | } 169 | }, 3).subscribe(new Action1() { 170 | @Override 171 | public void call(Integer integer) { 172 | log(integer); 173 | } 174 | }); 175 | } 176 | }); 177 | registery.add(Constants.Async.runAsync, new Runnable() { 178 | @Override 179 | public void run() { 180 | Async.runAsync(Schedulers.io(), new Action2, Subscription>() { 181 | @Override 182 | public void call(Observer observer, Subscription subscription) { 183 | log("Action2 run on " + Thread.currentThread().getName()); 184 | observer.onNext(1); 185 | observer.onNext(2); 186 | observer.onCompleted(); 187 | } 188 | }).subscribe(new Action1() { 189 | @Override 190 | public void call(Integer integer) { 191 | log(integer); 192 | } 193 | }); 194 | } 195 | }); 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/BlockingObservableActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | 4 | import java.util.Iterator; 5 | import java.util.concurrent.ExecutionException; 6 | import java.util.concurrent.Future; 7 | 8 | import rx.Observable; 9 | import rx.Subscriber; 10 | import rx.functions.Action1; 11 | import rx.schedulers.Schedulers; 12 | import union.uc.com.rxjava_example.base.APIBaseActivity; 13 | import union.uc.com.rxjava_example.contants.Constants; 14 | 15 | /** 16 | * Created by wangli on 4/12/16. 17 | */ 18 | public class BlockingObservableActivity extends APIBaseActivity { 19 | 20 | @Override 21 | protected void onRegisterAction(ActionRegistery registery) { 22 | registery.add(Constants.BlockingObservable.forEach, new Runnable() { 23 | @Override 24 | public void run() { 25 | Observable.create(new Observable.OnSubscribe() { 26 | @Override 27 | public void call(Subscriber subscriber) { 28 | subscriber.onNext(1); 29 | subscriber.onNext(2); 30 | subscriber.onCompleted(); 31 | } 32 | }).subscribeOn(Schedulers.newThread()).toBlocking().forEach(new Action1() { 33 | @Override 34 | public void call(Integer integer) { 35 | log(integer); 36 | } 37 | }); 38 | } 39 | }); 40 | registery.add(Constants.BlockingObservable.first, new Runnable() { 41 | @Override 42 | public void run() { 43 | Integer i = Observable.create(new Observable.OnSubscribe() { 44 | @Override 45 | public void call(Subscriber subscriber) { 46 | subscriber.onNext(1); 47 | subscriber.onNext(2); 48 | subscriber.onCompleted(); 49 | } 50 | }).subscribeOn(Schedulers.newThread()).toBlocking().first(); 51 | log(i); 52 | } 53 | }); 54 | registery.add(Constants.BlockingObservable.firstOrDefault, new Runnable() { 55 | @Override 56 | public void run() { 57 | Integer i = Observable.create(new Observable.OnSubscribe() { 58 | @Override 59 | public void call(Subscriber subscriber) { 60 | subscriber.onCompleted(); 61 | } 62 | }).subscribeOn(Schedulers.newThread()).toBlocking().firstOrDefault(5000); 63 | log(i); 64 | } 65 | }); 66 | registery.add(Constants.BlockingObservable.last, new Runnable() { 67 | @Override 68 | public void run() { 69 | Integer i = Observable.create(new Observable.OnSubscribe() { 70 | @Override 71 | public void call(Subscriber subscriber) { 72 | subscriber.onNext(1); 73 | subscriber.onNext(2); 74 | subscriber.onCompleted(); 75 | } 76 | }).subscribeOn(Schedulers.newThread()).toBlocking().last(); 77 | log(i); 78 | } 79 | }); 80 | registery.add(Constants.BlockingObservable.lastOrDefault, new Runnable() { 81 | @Override 82 | public void run() { 83 | Integer i = Observable.create(new Observable.OnSubscribe() { 84 | @Override 85 | public void call(Subscriber subscriber) { 86 | subscriber.onCompleted(); 87 | } 88 | }).subscribeOn(Schedulers.newThread()).toBlocking().lastOrDefault(5000); 89 | log(i); 90 | } 91 | }); 92 | registery.add(Constants.BlockingObservable.mostRecent, new Runnable() { 93 | @Override 94 | public void run() { 95 | Iterator itr = Observable.create(new Observable.OnSubscribe() { 96 | @Override 97 | public void call(Subscriber subscriber) { 98 | subscriber.onNext(1); 99 | subscriber.onNext(2); 100 | subscriber.onCompleted(); 101 | } 102 | }).subscribeOn(Schedulers.newThread()).toBlocking().mostRecent(5000).iterator(); 103 | while (itr.hasNext()) { 104 | log(itr.next()); 105 | } 106 | } 107 | }); 108 | registery.add(Constants.BlockingObservable.next, new Runnable() { 109 | @Override 110 | public void run() { 111 | Iterator itr = Observable.create(new Observable.OnSubscribe() { 112 | @Override 113 | public void call(Subscriber subscriber) { 114 | subscriber.onNext(1); 115 | subscriber.onNext(2); 116 | subscriber.onCompleted(); 117 | } 118 | }).subscribeOn(Schedulers.newThread()).toBlocking().next().iterator(); 119 | while (itr.hasNext()) { 120 | log(itr.next()); 121 | } 122 | } 123 | }); 124 | registery.add(Constants.BlockingObservable.latest, new Runnable() { 125 | @Override 126 | public void run() { 127 | Iterator itr = Observable.create(new Observable.OnSubscribe() { 128 | @Override 129 | public void call(Subscriber subscriber) { 130 | subscriber.onNext(1); 131 | subscriber.onNext(2); 132 | subscriber.onCompleted(); 133 | } 134 | }).subscribeOn(Schedulers.newThread()).toBlocking().latest().iterator(); 135 | while (itr.hasNext()) { 136 | log(itr.next()); 137 | } 138 | } 139 | }); 140 | registery.add(Constants.BlockingObservable.single, new Runnable() { 141 | @Override 142 | public void run() { 143 | Integer i = Observable.create(new Observable.OnSubscribe() { 144 | @Override 145 | public void call(Subscriber subscriber) { 146 | subscriber.onNext(1); 147 | subscriber.onCompleted(); 148 | } 149 | }).subscribeOn(Schedulers.newThread()).toBlocking().single(); 150 | log(i); 151 | } 152 | }); 153 | registery.add(Constants.BlockingObservable.singleOrDefault, new Runnable() { 154 | @Override 155 | public void run() { 156 | Integer i = Observable.create(new Observable.OnSubscribe() { 157 | @Override 158 | public void call(Subscriber subscriber) { 159 | subscriber.onCompleted(); 160 | } 161 | }).subscribeOn(Schedulers.newThread()).toBlocking().singleOrDefault(3000); 162 | log(i); 163 | } 164 | }); 165 | registery.add(Constants.BlockingObservable.toFuture, new Runnable() { 166 | @Override 167 | public void run() { 168 | Future future = Observable.create(new Observable.OnSubscribe() { 169 | @Override 170 | public void call(Subscriber subscriber) { 171 | subscriber.onNext(2); 172 | subscriber.onCompleted(); 173 | } 174 | }).subscribeOn(Schedulers.newThread()).toBlocking().toFuture(); 175 | try { 176 | log(future.get()); 177 | } catch (InterruptedException e) { 178 | e.printStackTrace(); 179 | log(e); 180 | } catch (ExecutionException e) { 181 | e.printStackTrace(); 182 | log(e); 183 | } 184 | } 185 | }); 186 | registery.add(Constants.BlockingObservable.toIterable, new Runnable() { 187 | @Override 188 | public void run() { 189 | Iterator itr = Observable.create(new Observable.OnSubscribe() { 190 | @Override 191 | public void call(Subscriber subscriber) { 192 | subscriber.onNext(1); 193 | subscriber.onNext(2); 194 | subscriber.onCompleted(); 195 | } 196 | }).subscribeOn(Schedulers.newThread()).toBlocking().toIterable().iterator(); 197 | while (itr.hasNext()) { 198 | log(itr.next()); 199 | } 200 | } 201 | }); 202 | registery.add(Constants.BlockingObservable.getIterator, new Runnable() { 203 | @Override 204 | public void run() { 205 | Iterator itr = Observable.create(new Observable.OnSubscribe() { 206 | @Override 207 | public void call(Subscriber subscriber) { 208 | subscriber.onNext(1); 209 | subscriber.onNext(2); 210 | subscriber.onCompleted(); 211 | } 212 | }).subscribeOn(Schedulers.newThread()).toBlocking().getIterator(); 213 | while (itr.hasNext()) { 214 | log(itr.next()); 215 | } 216 | } 217 | }); 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/ConnectableObservableActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import rx.Observable; 4 | import rx.Subscriber; 5 | import rx.Subscription; 6 | import rx.functions.Action1; 7 | import rx.observables.ConnectableObservable; 8 | import rx.schedulers.Schedulers; 9 | import union.uc.com.rxjava_example.base.APIBaseActivity; 10 | import union.uc.com.rxjava_example.contants.Constants; 11 | 12 | /** 13 | * Created by wangli on 4/12/16. 14 | */ 15 | public class ConnectableObservableActivity extends APIBaseActivity { 16 | 17 | @Override 18 | protected void onRegisterAction(ActionRegistery registery) { 19 | registery.add(Constants.ConnectableObservable.connect, new Runnable() { 20 | @Override 21 | public void run() { 22 | Observable o = Observable.create(new Observable.OnSubscribe() { 23 | @Override 24 | public void call(Subscriber subscriber) { 25 | for (int i = 0; i < 5; ++i) { 26 | subscriber.onNext(i); 27 | sleep(500); 28 | } 29 | subscriber.onCompleted(); 30 | } 31 | }).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()); 32 | o.subscribe(new Action1() { 33 | @Override 34 | public void call(Integer integer) { 35 | log("s1:" + integer); 36 | } 37 | }); 38 | sleep(1000); 39 | ConnectableObservable co = o.publish(); 40 | co.subscribe(new Action1() { 41 | @Override 42 | public void call(Integer integer) { 43 | log("s2:" + integer); 44 | } 45 | }); 46 | 47 | log("begin connect"); 48 | co.connect(); 49 | } 50 | }); 51 | registery.add(Constants.ConnectableObservable.publish, new Runnable() { 52 | @Override 53 | public void run() { 54 | log("showed in connect!"); 55 | } 56 | }); 57 | registery.add(Constants.ConnectableObservable.replay, new Runnable() { 58 | @Override 59 | public void run() { 60 | Observable o = Observable.create(new Observable.OnSubscribe() { 61 | @Override 62 | public void call(Subscriber subscriber) { 63 | for (int i = 0; i < 5; ++i) { 64 | subscriber.onNext(i); 65 | sleep(500); 66 | } 67 | subscriber.onCompleted(); 68 | } 69 | }).subscribeOn(Schedulers.newThread()).observeOn(Schedulers.newThread()); 70 | o.subscribe(new Action1() { 71 | @Override 72 | public void call(Integer integer) { 73 | log("s1:" + integer); 74 | } 75 | }); 76 | sleep(1000); 77 | ConnectableObservable co = o.publish(); 78 | co.subscribe(new Action1() { 79 | @Override 80 | public void call(Integer integer) { 81 | log("s2:" + integer); 82 | } 83 | }); 84 | 85 | log("begin connect"); 86 | co.connect(); 87 | } 88 | }); 89 | registery.add(Constants.ConnectableObservable.refCount, new Runnable() { 90 | @Override 91 | public void run() { 92 | ConnectableObservable co = 93 | Observable.create(new Observable.OnSubscribe() { 94 | @Override 95 | public void call(Subscriber subscriber) { 96 | for (int i = 0; i < 5; ++i) { 97 | subscriber.onNext(i); 98 | sleep(500); 99 | } 100 | subscriber.onCompleted(); 101 | } 102 | }).subscribeOn(Schedulers.newThread()).publish(); 103 | 104 | Subscription s1 = co.subscribe(new Action1() { 105 | @Override 106 | public void call(Integer integer) { 107 | log("s1:" + integer); 108 | } 109 | }); 110 | Subscription s2 = co.subscribe(new Action1() { 111 | @Override 112 | public void call(Integer integer) { 113 | log("s2:" + integer); 114 | } 115 | }); 116 | sleep(1000); 117 | log("begin refcount!"); 118 | Observable o = co.refCount(); 119 | o.subscribe(new Action1() { 120 | @Override 121 | public void call(Integer integer) { 122 | log("obs:" + integer); 123 | } 124 | }); 125 | 126 | sleep(1000); 127 | log("begin connect!"); 128 | co.connect(); 129 | s1.unsubscribe(); 130 | s2.unsubscribe(); 131 | log("both disconnected!"); 132 | } 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/CustomeOperatorActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import rx.Observable; 4 | import rx.Subscriber; 5 | import rx.exceptions.Exceptions; 6 | import rx.exceptions.OnErrorThrowable; 7 | import rx.functions.Action1; 8 | import rx.functions.Func1; 9 | import union.uc.com.rxjava_example.base.APIBaseActivity; 10 | import union.uc.com.rxjava_example.contants.Constants; 11 | 12 | /** 13 | * Created by wangli on 4/12/16. 14 | */ 15 | public class CustomeOperatorActivity extends APIBaseActivity { 16 | 17 | private class MyMapOperator implements Observable.Operator { 18 | private Func1 mTransformer; 19 | public MyMapOperator(Func1 transformer) { 20 | mTransformer = transformer; 21 | } 22 | 23 | @Override 24 | public Subscriber call(final Subscriber subscriber) { 25 | return new Subscriber() { 26 | @Override 27 | public void onCompleted() { 28 | log("onComplete"); 29 | subscriber.onCompleted(); 30 | } 31 | 32 | @Override 33 | public void onError(Throwable e) { 34 | log("onError:" + e); 35 | subscriber.onError(e); 36 | } 37 | 38 | @Override 39 | public void onNext(T t) { 40 | log("onNext:" + t); 41 | try { 42 | R r = mTransformer.call(t); 43 | subscriber.onNext(r); 44 | } catch (Throwable ex) { 45 | Exceptions.throwIfFatal(ex); 46 | unsubscribe(); 47 | onError(OnErrorThrowable.addValueAsLastCause(ex, t)); 48 | } 49 | } 50 | }; 51 | } 52 | } 53 | 54 | @Override 55 | protected void onRegisterAction(ActionRegistery registery) { 56 | registery.add(Constants.CustomerOperator.customeOperator, new Runnable() { 57 | @Override 58 | public void run() { 59 | Observable.range(1, 10) 60 | .lift(new MyMapOperator(new Func1() { 61 | @Override 62 | public Integer call(Integer integer) { 63 | return integer * 2; 64 | } 65 | })) 66 | .subscribe(new Action1() { 67 | @Override 68 | public void call(Integer integer) { 69 | log(integer); 70 | } 71 | }); 72 | } 73 | }); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/ErrorHandleActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import rx.Observable; 6 | import rx.functions.Action1; 7 | import rx.functions.Func1; 8 | import union.uc.com.rxjava_example.base.APIBaseActivity; 9 | import union.uc.com.rxjava_example.contants.Constants; 10 | 11 | /** 12 | * Created by wangli on 4/12/16. 13 | */ 14 | public class ErrorHandleActivity extends APIBaseActivity { 15 | 16 | @Override 17 | protected void onRegisterAction(ActionRegistery registery) { 18 | registery.add(Constants.ErrorHandler.onErrorResumeNext, new Runnable() { 19 | @Override 20 | public void run() { 21 | Observable.just(1, "abc") 22 | .cast(Integer.class) 23 | .onErrorResumeNext(Observable.just(1, 2)) 24 | .subscribe(new Action1() { 25 | @Override 26 | public void call(Integer integer) { 27 | log(integer); 28 | } 29 | }, new Action1() { 30 | @Override 31 | public void call(Throwable throwable) { 32 | log(throwable); 33 | } 34 | }); 35 | } 36 | }); 37 | registery.add(Constants.ErrorHandler.onErrorReturn, new Runnable() { 38 | @Override 39 | public void run() { 40 | Observable.just(1, "abc") 41 | .cast(Integer.class) 42 | .onErrorReturn(new Func1() { 43 | @Override 44 | public Integer call(Throwable throwable) { 45 | return -1; 46 | } 47 | }) 48 | .subscribe(new Action1() { 49 | @Override 50 | public void call(Integer integer) { 51 | log(integer); 52 | } 53 | }, new Action1() { 54 | @Override 55 | public void call(Throwable throwable) { 56 | log(throwable); 57 | } 58 | }); 59 | } 60 | }); 61 | registery.add(Constants.ErrorHandler.onExceptionResumeNext, new Runnable() { 62 | @Override 63 | public void run() { 64 | Observable.just(1, "abc", "2") 65 | .cast(Integer.class) 66 | .onExceptionResumeNext(Observable.just(5, 6)) 67 | .subscribe(new Action1() { 68 | @Override 69 | public void call(Integer integer) { 70 | log(integer); 71 | } 72 | }, new Action1() { 73 | @Override 74 | public void call(Throwable throwable) { 75 | log(throwable); 76 | } 77 | }); 78 | } 79 | }); 80 | registery.add(Constants.ErrorHandler.retry, new Runnable() { 81 | @Override 82 | public void run() { 83 | Observable.just(1, "abc", 2).cast(Integer.class).retry(2).subscribe(new Action1() { 84 | @Override 85 | public void call(Integer integer) { 86 | log(integer); 87 | } 88 | }, new Action1() { 89 | @Override 90 | public void call(Throwable throwable) { 91 | log(throwable); 92 | } 93 | }); 94 | } 95 | }); 96 | registery.add(Constants.ErrorHandler.retryWhen, new Runnable() { 97 | @Override 98 | public void run() { 99 | Observable.just(1, "abc", "2") 100 | .cast(Integer.class) 101 | .retryWhen(new Func1, Observable>() { 102 | @Override 103 | public Observable call(Observable observable) { 104 | observable.subscribe(new Action1() { 105 | @Override 106 | public void call(Throwable throwable) { 107 | log("error inner:" + throwable.getMessage()); 108 | } 109 | }); 110 | return Observable.timer(1, TimeUnit.SECONDS); 111 | } 112 | }) 113 | .subscribe(new Action1() { 114 | @Override 115 | public void call(Integer integer) { 116 | log(integer); 117 | } 118 | }, new Action1() { 119 | @Override 120 | public void call(Throwable throwable) { 121 | log(throwable); 122 | } 123 | }); 124 | } 125 | }); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/MathAggregateActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import rx.Observable; 4 | import rx.functions.Action1; 5 | import rx.observables.MathObservable; 6 | import union.uc.com.rxjava_example.base.APIBaseActivity; 7 | import union.uc.com.rxjava_example.contants.Constants; 8 | 9 | /** 10 | * Created by wangli on 4/12/16. 11 | */ 12 | public class MathAggregateActivity extends APIBaseActivity { 13 | 14 | @Override 15 | protected void onRegisterAction(ActionRegistery registery) { 16 | registery.add(Constants.MathAggregate.averageInteger, new Runnable() { 17 | @Override 18 | public void run() { 19 | MathObservable.averageInteger(Observable.just(2, 4, 1, 5)) 20 | .subscribe(new Action1() { 21 | @Override 22 | public void call(Integer integer) { 23 | log(integer); 24 | } 25 | }); 26 | } 27 | }); 28 | registery.add(Constants.MathAggregate.averageLong, new Runnable() { 29 | @Override 30 | public void run() { 31 | MathObservable.averageLong(Observable.just(2L, 4L, 1L, 5L)).subscribe(new Action1() { 32 | @Override 33 | public void call(Long l) { 34 | log(l); 35 | } 36 | }); 37 | } 38 | }); 39 | registery.add(Constants.MathAggregate.averageFloat, new Runnable() { 40 | @Override 41 | public void run() { 42 | MathObservable.averageFloat(Observable.just(1.0f, 2.0f, 3.0f, 4.0f)) 43 | .subscribe(new Action1() { 44 | @Override 45 | public void call(Float f) { 46 | log(f); 47 | } 48 | }); 49 | } 50 | }); 51 | registery.add(Constants.MathAggregate.averageDouble, new Runnable() { 52 | @Override 53 | public void run() { 54 | MathObservable.averageDouble(Observable.just(1.0, 2.0, 3.0, 4.0)) 55 | .subscribe(new Action1() { 56 | @Override 57 | public void call(Double f) { 58 | log(f); 59 | } 60 | }); 61 | } 62 | }); 63 | registery.add(Constants.MathAggregate.max, new Runnable() { 64 | @Override 65 | public void run() { 66 | MathObservable.max(Observable.just(1, 2, 3)).subscribe(new Action1() { 67 | @Override 68 | public void call(Integer integer) { 69 | log(integer); 70 | } 71 | }); 72 | } 73 | }); 74 | registery.add(Constants.MathAggregate.maxBy, new Runnable() { 75 | @Override 76 | public void run() { 77 | logNotImplemented(); 78 | } 79 | }); 80 | registery.add(Constants.MathAggregate.min, new Runnable() { 81 | @Override 82 | public void run() { 83 | MathObservable.min(Observable.just(1, 2, 3)).subscribe(new Action1() { 84 | @Override 85 | public void call(Integer integer) { 86 | log(integer); 87 | } 88 | }); 89 | } 90 | }); 91 | registery.add(Constants.MathAggregate.minBy, new Runnable() { 92 | @Override 93 | public void run() { 94 | logNotImplemented(); 95 | } 96 | }); 97 | registery.add(Constants.MathAggregate.sumInteger, new Runnable() { 98 | @Override 99 | public void run() { 100 | MathObservable.sumInteger(Observable.just(1, 2, 3)).subscribe(new Action1() { 101 | @Override 102 | public void call(Integer integer) { 103 | log(integer); 104 | } 105 | }); 106 | } 107 | }); 108 | registery.add(Constants.MathAggregate.sumLong, new Runnable() { 109 | @Override 110 | public void run() { 111 | MathObservable.sumLong(Observable.just(1L, 2L, 3L)).subscribe(new Action1() { 112 | @Override 113 | public void call(Long l) { 114 | log(l); 115 | } 116 | }); 117 | } 118 | }); 119 | registery.add(Constants.MathAggregate.sumFloat, new Runnable() { 120 | @Override 121 | public void run() { 122 | MathObservable.sumFloat(Observable.just(1.0f, 2.0f, 3.0f, 4.0f)) 123 | .subscribe(new Action1() { 124 | @Override 125 | public void call(Float f) { 126 | log(f); 127 | } 128 | }); 129 | } 130 | }); 131 | registery.add(Constants.MathAggregate.sumDouble, new Runnable() { 132 | @Override 133 | public void run() { 134 | MathObservable.sumDouble(Observable.just(1.0, 2.0, 3.0, 4.0)) 135 | .subscribe(new Action1() { 136 | @Override 137 | public void call(Double f) { 138 | log(f); 139 | } 140 | }); 141 | } 142 | }); 143 | registery.add(Constants.MathAggregate.concat, new Runnable() { 144 | @Override 145 | public void run() { 146 | logUseObservable(); 147 | } 148 | }); 149 | registery.add(Constants.MathAggregate.count, new Runnable() { 150 | @Override 151 | public void run() { 152 | logUseObservable(); 153 | } 154 | }); 155 | registery.add(Constants.MathAggregate.countLong, new Runnable() { 156 | @Override 157 | public void run() { 158 | logUseObservable(); 159 | } 160 | }); 161 | registery.add(Constants.MathAggregate.reduce, new Runnable() { 162 | @Override 163 | public void run() { 164 | logUseObservable(); 165 | } 166 | }); 167 | registery.add(Constants.MathAggregate.collect, new Runnable() { 168 | @Override 169 | public void run() { 170 | logUseObservable(); 171 | } 172 | }); 173 | registery.add(Constants.MathAggregate.toList, new Runnable() { 174 | @Override 175 | public void run() { 176 | logUseObservable(); 177 | } 178 | }); 179 | registery.add(Constants.MathAggregate.toSortedList, new Runnable() { 180 | @Override 181 | public void run() { 182 | logUseObservable(); 183 | } 184 | }); 185 | registery.add(Constants.MathAggregate.toMap, new Runnable() { 186 | @Override 187 | public void run() { 188 | logUseObservable(); 189 | } 190 | }); 191 | registery.add(Constants.MathAggregate.toMultiMap, new Runnable() { 192 | @Override 193 | public void run() { 194 | logUseObservable(); 195 | } 196 | }); 197 | } 198 | } 199 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/PluginActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import android.os.Bundle; 4 | 5 | import java.util.concurrent.Executors; 6 | import java.util.concurrent.ScheduledExecutorService; 7 | 8 | import rx.Observable; 9 | import rx.Scheduler; 10 | import rx.Subscription; 11 | import rx.functions.Action0; 12 | import rx.functions.Action1; 13 | import rx.plugins.RxJavaErrorHandler; 14 | import rx.plugins.RxJavaObservableExecutionHook; 15 | import rx.plugins.RxJavaPlugins; 16 | import rx.plugins.RxJavaSchedulersHook; 17 | import rx.schedulers.Schedulers; 18 | import union.uc.com.rxjava_example.base.APIBaseActivity; 19 | import union.uc.com.rxjava_example.contants.Constants; 20 | 21 | /** 22 | * Created by wangli on 4/12/16. 23 | */ 24 | public class PluginActivity extends APIBaseActivity { 25 | ScheduledExecutorService SERVICE = Executors.newScheduledThreadPool(3); 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | log("from now on, all operation is hooked!"); 31 | registerHook(); 32 | } 33 | 34 | private void registerHook() { 35 | RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() { 36 | @Override 37 | public void handleError(Throwable e) { 38 | super.handleError(e); 39 | log("hook handleError:" + e); 40 | } 41 | 42 | @Override 43 | protected String render(Object item) throws InterruptedException { 44 | log("hook render:" + item); 45 | return super.render(item); 46 | } 47 | }); 48 | RxJavaPlugins.getInstance() 49 | .registerObservableExecutionHook(new RxJavaObservableExecutionHook() { 50 | @Override 51 | public Observable.OnSubscribe onCreate(Observable.OnSubscribe f) { 52 | log("hook Observable::onCreate"); 53 | return super.onCreate(f); 54 | } 55 | 56 | @Override 57 | public Observable.OnSubscribe onSubscribeStart(Observable observableInstance, 58 | Observable.OnSubscribe onSubscribe) { 59 | log("hook onSubscribeStart"); 60 | return super.onSubscribeStart(observableInstance, onSubscribe); 61 | } 62 | 63 | @Override 64 | public Subscription onSubscribeReturn(Subscription subscription) { 65 | log("hook onSubscribeReturn"); 66 | return super.onSubscribeReturn(subscription); 67 | } 68 | 69 | @Override 70 | public Throwable onSubscribeError(Throwable e) { 71 | log("hook onSubscribeError"); 72 | return super.onSubscribeError(e); 73 | } 74 | 75 | @Override 76 | public Observable.Operator onLift(Observable.Operator lift) { 77 | log("hook onLift"); 78 | return super.onLift(lift); 79 | } 80 | }); 81 | RxJavaPlugins.getInstance().registerSchedulersHook(new RxJavaSchedulersHook() { 82 | @Override 83 | public Scheduler getComputationScheduler() { 84 | log("hook getComputationScheduler, You can replace the default one"); 85 | return super.getComputationScheduler(); 86 | } 87 | 88 | @Override 89 | public Scheduler getIOScheduler() { 90 | log("hook getIOScheduler, You can replace the default one"); 91 | return super.getIOScheduler(); 92 | } 93 | 94 | @Override 95 | public Scheduler getNewThreadScheduler() { 96 | log("hook getNewThreadScheduler, You can replace the default one"); 97 | return super.getNewThreadScheduler(); 98 | } 99 | 100 | @Override 101 | public Action0 onSchedule(Action0 action) { 102 | log("hook onSchedule, You can replace the default one"); 103 | return super.onSchedule(action); 104 | } 105 | }); 106 | } 107 | 108 | @Override 109 | protected void onRegisterAction(ActionRegistery registery) { 110 | registery.add(Constants.Plugin.start_hook, new Runnable() { 111 | @Override 112 | public void run() { 113 | Observable.range(1, 10).observeOn(Schedulers.io()).subscribe(new Action1() { 114 | @Override 115 | public void call(Integer integer) { 116 | log(integer); 117 | } 118 | }); 119 | } 120 | }); 121 | 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/ReactiveStreamActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import union.uc.com.rxjava_example.base.APIBaseActivity; 4 | import union.uc.com.rxjava_example.contants.Constants; 5 | 6 | /** 7 | * Created by wangli on 4/12/16. 8 | */ 9 | public class ReactiveStreamActivity extends APIBaseActivity { 10 | 11 | @Override 12 | protected void onRegisterAction(ActionRegistery registery) { 13 | registery.add(Constants.ReactiveStream.materialize, new Runnable() { 14 | @Override 15 | public void run() { 16 | 17 | } 18 | }); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/SchedulerActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import rx.Observable; 4 | import rx.functions.Action1; 5 | import rx.schedulers.Schedulers; 6 | import union.uc.com.rxjava_example.base.APIBaseActivity; 7 | import union.uc.com.rxjava_example.base.UIThreadExecutor; 8 | import union.uc.com.rxjava_example.contants.Constants; 9 | 10 | /** 11 | * Created by wangli on 4/12/16. 12 | */ 13 | public class SchedulerActivity extends APIBaseActivity { 14 | 15 | @Override 16 | protected void onRegisterAction(ActionRegistery registery) { 17 | registery.add(Constants.Scheduler.io, new Runnable() { 18 | @Override 19 | public void run() { 20 | Observable.just("a", "b").observeOn(Schedulers.io()).subscribe(new Action1() { 21 | @Override 22 | public void call(String s) { 23 | log(s + " on " + Thread.currentThread().getName()); 24 | } 25 | }); 26 | } 27 | }); 28 | registery.add(Constants.Scheduler.compute, new Runnable() { 29 | @Override 30 | public void run() { 31 | Observable.just("a", "b") 32 | .observeOn(Schedulers.computation()) 33 | .subscribe(new Action1() { 34 | @Override 35 | public void call(String s) { 36 | log(s + " on " + Thread.currentThread().getName()); 37 | } 38 | }); 39 | } 40 | }); 41 | registery.add(Constants.Scheduler.new_thread, new Runnable() { 42 | @Override 43 | public void run() { 44 | Observable.just("a", "b") 45 | .observeOn(Schedulers.newThread()) 46 | .subscribe(new Action1() { 47 | @Override 48 | public void call(String s) { 49 | log(s + " on " + Thread.currentThread().getName()); 50 | } 51 | }); 52 | } 53 | }); 54 | registery.add(Constants.Scheduler.trampoline, new Runnable() { 55 | @Override 56 | public void run() { 57 | Observable.just("a", "b") 58 | .observeOn(Schedulers.trampoline()) 59 | .subscribe(new Action1() { 60 | @Override 61 | public void call(String s) { 62 | log(s + " on " + Thread.currentThread().getName()); 63 | } 64 | }); 65 | log("i'm on thread " + Thread.currentThread().getName()); 66 | } 67 | }); 68 | registery.add(Constants.Scheduler.immediate, new Runnable() { 69 | @Override 70 | public void run() { 71 | Observable.just("a", "b") 72 | .observeOn(Schedulers.immediate()) 73 | .subscribe(new Action1() { 74 | @Override 75 | public void call(String s) { 76 | log(s + " on " + Thread.currentThread().getName()); 77 | } 78 | }); 79 | log("i'm on thread " + Thread.currentThread().getName()); 80 | } 81 | }); 82 | registery.add(Constants.Scheduler.self_define, new Runnable() { 83 | @Override 84 | public void run() { 85 | Observable.just("a", "b") 86 | .observeOn(Schedulers.from(UIThreadExecutor.SINGLETON)) 87 | .subscribe(new Action1() { 88 | @Override 89 | public void call(String s) { 90 | log(s + " on " + Thread.currentThread().getName()); 91 | } 92 | }); 93 | } 94 | }); 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/StringActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import java.io.ByteArrayInputStream; 4 | import java.nio.charset.Charset; 5 | 6 | import rx.Observable; 7 | import rx.functions.Action1; 8 | import rx.observables.StringObservable; 9 | import union.uc.com.rxjava_example.base.APIBaseActivity; 10 | import union.uc.com.rxjava_example.contants.Constants; 11 | 12 | /** 13 | * Created by wangli on 4/12/16. 14 | */ 15 | public class StringActivity extends APIBaseActivity { 16 | 17 | @Override 18 | protected void onRegisterAction(ActionRegistery registery) { 19 | registery.add(Constants.Strings.byLine, new Runnable() { 20 | @Override 21 | public void run() { 22 | StringObservable.byLine(Observable.just("ab\r\ncd\r\nef", "12\r\n34")) 23 | .subscribe(new Action1() { 24 | @Override 25 | public void call(String s) { 26 | log(s); 27 | } 28 | }); 29 | } 30 | }); 31 | registery.add(Constants.Strings.decode, new Runnable() { 32 | @Override 33 | public void run() { 34 | StringObservable.decode(Observable.just("ABC".getBytes(Charset.forName("UTF-8"))), 35 | Charset.forName("UTF-8")).subscribe(new Action1() { 36 | @Override 37 | public void call(String s) { 38 | log(s); 39 | } 40 | }); 41 | } 42 | }); 43 | registery.add(Constants.Strings.encode, new Runnable() { 44 | @Override 45 | public void run() { 46 | StringObservable.encode(Observable.just("abc"), "UTF-8").subscribe(new Action1() { 47 | @Override 48 | public void call(byte[] bytes) { 49 | log(bytes.length); 50 | } 51 | }); 52 | } 53 | }); 54 | registery.add(Constants.Strings.from, new Runnable() { 55 | @Override 56 | public void run() { 57 | StringObservable.from(new ByteArrayInputStream("ABC".getBytes())) 58 | .subscribe(new Action1() { 59 | @Override 60 | public void call(byte[] bytes) { 61 | log(bytes.length); 62 | } 63 | }); 64 | } 65 | }); 66 | registery.add(Constants.Strings.join, new Runnable() { 67 | @Override 68 | public void run() { 69 | StringObservable.join(Observable.just("abc", "def"), "#").subscribe(new Action1() { 70 | @Override 71 | public void call(String s) { 72 | log(s); 73 | } 74 | }); 75 | } 76 | }); 77 | registery.add(Constants.Strings.split, new Runnable() { 78 | @Override 79 | public void run() { 80 | StringObservable.split(Observable.just("ab#cd#ef"), "#").subscribe(new Action1() { 81 | @Override 82 | public void call(String s) { 83 | log(s); 84 | } 85 | }); 86 | } 87 | }); 88 | registery.add(Constants.Strings.stringConcat, new Runnable() { 89 | @Override 90 | public void run() { 91 | StringObservable.stringConcat(Observable.just("abc", "def")) 92 | .subscribe(new Action1() { 93 | @Override 94 | public void call(String s) { 95 | log(s); 96 | } 97 | }); 98 | } 99 | }); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/SubjectActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import rx.functions.Action1; 6 | import rx.schedulers.Schedulers; 7 | import rx.subjects.AsyncSubject; 8 | import rx.subjects.BehaviorSubject; 9 | import rx.subjects.PublishSubject; 10 | import rx.subjects.ReplaySubject; 11 | import union.uc.com.rxjava_example.base.APIBaseActivity; 12 | import union.uc.com.rxjava_example.contants.Constants; 13 | 14 | /** 15 | * Created by wangli on 4/12/16. 16 | */ 17 | public class SubjectActivity extends APIBaseActivity { 18 | 19 | @Override 20 | protected void onRegisterAction(ActionRegistery registery) { 21 | registery.add(Constants.Subject.async, new Runnable() { 22 | @Override 23 | public void run() { 24 | AsyncSubject s = AsyncSubject.create(); 25 | s.subscribe(new Action1() { 26 | @Override 27 | public void call(Integer integer) { 28 | log("" + integer); 29 | } 30 | }); 31 | s.onNext(0); 32 | s.onNext(1); 33 | s.onNext(2); 34 | s.onCompleted(); 35 | } 36 | }); 37 | registery.add(Constants.Subject.behavior, new Runnable() { 38 | @Override 39 | public void run() { 40 | BehaviorSubject s = BehaviorSubject.create(); 41 | s.onNext(1); 42 | s.onNext(2); 43 | s.subscribe(new Action1() { 44 | @Override 45 | public void call(Integer integer) { 46 | log("" + integer); 47 | } 48 | }); 49 | s.onNext(3); 50 | } 51 | }); 52 | registery.add(Constants.Subject.behavior_with_init_value, new Runnable() { 53 | @Override 54 | public void run() { 55 | BehaviorSubject s = BehaviorSubject.create(0); 56 | s.subscribe(new Action1() { 57 | @Override 58 | public void call(Integer integer) { 59 | log("" + integer); 60 | } 61 | }); 62 | s.onNext(1); 63 | } 64 | }); 65 | registery.add(Constants.Subject.publish, new Runnable() { 66 | @Override 67 | public void run() { 68 | PublishSubject subject = PublishSubject.create(); 69 | subject.onNext(1); 70 | subject.subscribe(new Action1() { 71 | @Override 72 | public void call(Integer integer) { 73 | log("" + integer); 74 | } 75 | }); 76 | subject.onNext(2); 77 | subject.onNext(3); 78 | subject.onNext(4); 79 | } 80 | }); 81 | registery.add(Constants.Subject.replay, new Runnable() { 82 | @Override 83 | public void run() { 84 | ReplaySubject subject = ReplaySubject.create(); 85 | subject.onNext(1); 86 | subject.subscribe(new Action1() { 87 | @Override 88 | public void call(Integer integer) { 89 | log("Subscriber1:" + integer); 90 | } 91 | }); 92 | subject.onNext(2); 93 | subject.onNext(3); 94 | subject.subscribe(new Action1() { 95 | @Override 96 | public void call(Integer integer) { 97 | log("Subscriber2:" + integer); 98 | } 99 | }); 100 | subject.onNext(4); 101 | } 102 | }); 103 | registery.add(Constants.Subject.replay_create_with_time, new Runnable() { 104 | @Override 105 | public void run() { 106 | try { 107 | ReplaySubject subject = 108 | ReplaySubject.createWithTime(150, TimeUnit.MILLISECONDS, Schedulers.immediate()); 109 | subject.onNext(1); 110 | Thread.sleep(100); 111 | subject.onNext(2); 112 | Thread.sleep(100); 113 | subject.onNext(3); 114 | subject.subscribe(new Action1() { 115 | @Override 116 | public void call(Integer integer) { 117 | log("" + integer); 118 | } 119 | }); 120 | subject.onNext(4); 121 | } catch (InterruptedException e) { 122 | log("error:" + e.getMessage()); 123 | } 124 | } 125 | }); 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/api/TransformationActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.api; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import rx.Observable; 7 | import rx.Subscriber; 8 | import rx.functions.Action1; 9 | import rx.functions.Func1; 10 | import rx.functions.Func2; 11 | import rx.observables.GroupedObservable; 12 | import rx.schedulers.Schedulers; 13 | import union.uc.com.rxjava_example.base.APIBaseActivity; 14 | import union.uc.com.rxjava_example.contants.Constants; 15 | 16 | /** 17 | * Created by wangli on 4/12/16. 18 | */ 19 | public class TransformationActivity extends APIBaseActivity { 20 | 21 | @Override 22 | protected void onRegisterAction(ActionRegistery registery) { 23 | registery.add(Constants.Transformation.map, new Runnable() { 24 | @Override 25 | public void run() { 26 | Observable.range(1, 10).map(new Func1() { 27 | @Override 28 | public Integer call(Integer integer) { 29 | return integer * 2; 30 | } 31 | }).subscribe(new Action1() { 32 | @Override 33 | public void call(Integer integer) { 34 | log(integer); 35 | } 36 | }); 37 | } 38 | }); 39 | registery.add(Constants.Transformation.flatMap, new Runnable() { 40 | @Override 41 | public void run() { 42 | Observable.just(1, 2).flatMap(new Func1>() { 43 | @Override 44 | public Observable call(final Integer integer) { 45 | return Observable.create(new Observable.OnSubscribe() { 46 | @Override 47 | public void call(Subscriber subscriber) { 48 | subscriber.onNext(integer); 49 | subscriber.onCompleted(); 50 | } 51 | }).subscribeOn(Schedulers.newThread()); 52 | } 53 | }).subscribe(new Action1() { 54 | @Override 55 | public void call(Integer integer) { 56 | log(integer); 57 | } 58 | }); 59 | } 60 | }); 61 | registery.add(Constants.Transformation.concatMap, new Runnable() { 62 | @Override 63 | public void run() { 64 | Observable.just(1, 2).concatMap(new Func1>() { 65 | @Override 66 | public Observable call(Integer integer) { 67 | return Observable.just(integer); 68 | } 69 | }).subscribe(new Action1() { 70 | @Override 71 | public void call(Integer integer) { 72 | log(integer); 73 | } 74 | }); 75 | } 76 | }); 77 | registery.add(Constants.Transformation.flatMapIterable, new Runnable() { 78 | @Override 79 | public void run() { 80 | Observable.just(1, 2).flatMapIterable(new Func1>() { 81 | @Override 82 | public Iterable call(Integer integer) { 83 | return Arrays.asList(new String[] {"" + integer, "" + integer * 3}); 84 | } 85 | }).subscribe(new Action1() { 86 | @Override 87 | public void call(String s) { 88 | log(s); 89 | } 90 | }); 91 | } 92 | }); 93 | registery.add(Constants.Transformation.switchMap, new Runnable() { 94 | @Override 95 | public void run() { 96 | Observable.create(new Observable.OnSubscribe() { 97 | @Override 98 | public void call(Subscriber subscriber) { 99 | for (int i = 0; i < 3; ++i) { 100 | subscriber.onNext(i); 101 | sleep(500); 102 | } 103 | subscriber.onCompleted(); 104 | } 105 | }).subscribeOn(Schedulers.newThread()).switchMap(new Func1>() { 106 | @Override 107 | public Observable call(final Integer integer) { 108 | return Observable.create(new Observable.OnSubscribe() { 109 | @Override 110 | public void call(Subscriber subscriber) { 111 | subscriber.onNext(integer); 112 | sleep(500); 113 | subscriber.onNext(integer); 114 | subscriber.onCompleted(); 115 | } 116 | }).subscribeOn(Schedulers.newThread()); 117 | } 118 | }).subscribe(new Action1() { 119 | @Override 120 | public void call(Integer s) { 121 | log(s); 122 | } 123 | }); 124 | } 125 | }); 126 | registery.add(Constants.Transformation.scan, new Runnable() { 127 | @Override 128 | public void run() { 129 | Observable.range(1, 10).scan(new Func2() { 130 | @Override 131 | public Integer call(Integer integer, Integer integer2) { 132 | return integer + integer2; 133 | } 134 | }).subscribe(new Action1() { 135 | @Override 136 | public void call(Integer integer) { 137 | log(integer); 138 | } 139 | }); 140 | } 141 | }); 142 | registery.add(Constants.Transformation.groupBy, new Runnable() { 143 | @Override 144 | public void run() { 145 | Observable.range(1, 10).groupBy(new Func1() { 146 | @Override 147 | public String call(Integer integer) { 148 | return (integer % 2 == 0) ? "even" : "odd"; 149 | } 150 | }).subscribe(new Action1>() { 151 | @Override 152 | public void call(final GroupedObservable stringIntegerGroupedObservable) { 153 | log("group ok:" + stringIntegerGroupedObservable.getKey()); 154 | stringIntegerGroupedObservable.subscribe(new Action1() { 155 | @Override 156 | public void call(Integer integer) { 157 | log(integer + " of group " + stringIntegerGroupedObservable.getKey()); 158 | } 159 | }); 160 | } 161 | }); 162 | } 163 | }); 164 | registery.add(Constants.Transformation.buffer, new Runnable() { 165 | @Override 166 | public void run() { 167 | Observable.range(1, 10).buffer(3).subscribe(new Action1>() { 168 | @Override 169 | public void call(List integers) { 170 | String s = ""; 171 | for (Integer i : integers) { 172 | s += i; 173 | } 174 | log(s); 175 | } 176 | }); 177 | } 178 | }); 179 | registery.add(Constants.Transformation.window, new Runnable() { 180 | @Override 181 | public void run() { 182 | Observable.range(1, 10).window(3).subscribe(new Action1>() { 183 | @Override 184 | public void call(final Observable integerObservable) { 185 | integerObservable.subscribe(new Action1() { 186 | @Override 187 | public void call(Integer integer) { 188 | log(integer + " of window " + integerObservable); 189 | } 190 | }); 191 | } 192 | }); 193 | } 194 | }); 195 | registery.add(Constants.Transformation.cast, new Runnable() { 196 | @Override 197 | public void run() { 198 | Observable.just(1, 2, 3). 199 | cast(Integer.class).subscribe(new Action1() { 200 | @Override 201 | public void call(Integer integer) { 202 | log(integer); 203 | } 204 | }); 205 | } 206 | }); 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/base/AsyncExecutor.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.base; 2 | 3 | import java.util.concurrent.Executors; 4 | import java.util.concurrent.ScheduledExecutorService; 5 | 6 | /** 7 | * Created by wangli on 4/5/16. 8 | */ 9 | public class AsyncExecutor { 10 | public static ScheduledExecutorService SINGLETON = Executors.newScheduledThreadPool(3); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/base/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.base; 2 | 3 | import android.app.Activity; 4 | import android.graphics.Color; 5 | import android.os.Bundle; 6 | import android.view.Window; 7 | import android.view.WindowManager; 8 | import android.widget.LinearLayout; 9 | import android.widget.ScrollView; 10 | import android.widget.TextView; 11 | 12 | /** 13 | * Created by wangli on 4/5/16. 14 | */ 15 | public abstract class BaseActivity extends Activity { 16 | 17 | private LinearLayout mContainer; 18 | private TextView mDescription; 19 | private TextView mTip; 20 | private LinearLayout mWorkspace; 21 | 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | this.requestWindowFeature(Window.FEATURE_NO_TITLE); 27 | this.getWindow() 28 | .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 29 | WindowManager.LayoutParams.FLAG_FULLSCREEN); 30 | setupUIComponents(); 31 | } 32 | 33 | private void setupUIComponents() { 34 | mContainer = new LinearLayout(this); 35 | mContainer.setOrientation(LinearLayout.VERTICAL); 36 | 37 | mDescription = new TextView(this); 38 | mDescription.setBackgroundColor(Color.BLUE); 39 | mDescription.setTextColor(Color.YELLOW); 40 | mDescription.setText(myDescription()); 41 | mContainer.addView(mDescription, 42 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 43 | 44 | mWorkspace = new LinearLayout(this); 45 | setupWorkspace(mWorkspace); 46 | mContainer.addView(mWorkspace, 47 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 48 | LinearLayout.LayoutParams.WRAP_CONTENT)); 49 | 50 | mTip = new TextView(this); 51 | mTip.setSingleLine(false); 52 | appendTip("Messages:(Scroll to show more ...)"); 53 | ScrollView scrollView = new ScrollView(this); 54 | scrollView.setBackgroundColor(Color.GRAY); 55 | scrollView.addView(mTip); 56 | mContainer.addView(scrollView, 57 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1)); 58 | setContentView(mContainer); 59 | } 60 | 61 | protected void appendTip(final String tipLine) { 62 | //ensure log on ui thread 63 | runOnUiThread(new Runnable() { 64 | @Override 65 | public void run() { 66 | mTip.setText(mTip.getText().toString() + "\r\n" + tipLine); 67 | } 68 | }); 69 | } 70 | 71 | 72 | protected void clearTip() { 73 | //ensure log on ui thread 74 | runOnUiThread(new Runnable() { 75 | @Override 76 | public void run() { 77 | mTip.setText("Messages:(Scroll to show more ...)"); 78 | } 79 | }); 80 | } 81 | 82 | protected abstract String myDescription(); 83 | 84 | protected abstract void setupWorkspace(LinearLayout workspace); 85 | } 86 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/base/Tuple.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.base; 2 | 3 | public class Tuple { 4 | private Tuple() { 5 | } 6 | 7 | public static class Tuple2 { 8 | public T1 item1; 9 | public T2 item2; 10 | 11 | public Tuple2(T1 item1, T2 item2) { 12 | this.item1 = item1; 13 | this.item2 = item2; 14 | } 15 | } 16 | 17 | public static class Tuple3 { 18 | public T1 item1; 19 | public T2 item2; 20 | public T3 item3; 21 | 22 | public Tuple3(T1 item1, T2 item2, T3 item3) { 23 | this.item1 = item1; 24 | this.item2 = item2; 25 | this.item3 = item3; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/base/UIThreadExecutor.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.base; 2 | 3 | import android.os.Handler; 4 | import android.os.Looper; 5 | 6 | import java.util.concurrent.Executor; 7 | 8 | /** 9 | * Created by wangli on 4/5/16. 10 | */ 11 | public class UIThreadExecutor implements Executor { 12 | private final Handler mHandler = new Handler(Looper.getMainLooper()); 13 | public static UIThreadExecutor SINGLETON = new UIThreadExecutor(); 14 | 15 | @Override 16 | public void execute(Runnable command) { 17 | mHandler.post(command); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/plugin/DisplayPluginManager.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.plugin; 2 | 3 | import android.content.Context; 4 | import android.view.View; 5 | 6 | import java.util.ArrayList; 7 | import java.util.List; 8 | 9 | import rx.Observable; 10 | import union.uc.com.rxjava_example.base.Tuple; 11 | import union.uc.com.rxjava_example.base.UIThreadExecutor; 12 | 13 | /** 14 | * Created by wangli on 4/16/16. 15 | */ 16 | //run on init and ui thread 17 | public class DisplayPluginManager { 18 | private static final DisplayPluginManager INSTANCE = new DisplayPluginManager(); 19 | private List mPlugins = new ArrayList<>(); 20 | 21 | public static DisplayPluginManager singleton() { 22 | return INSTANCE; 23 | } 24 | 25 | public interface Plugin { 26 | Tuple.Tuple2, View> getView(Context context, String key); 27 | } 28 | 29 | public void register(final Plugin plugin) { 30 | UIThreadExecutor.SINGLETON.execute(new Runnable() { 31 | @Override 32 | public void run() { 33 | mPlugins.add(plugin); 34 | } 35 | }); 36 | } 37 | 38 | public void unregister(final Plugin plugin) { 39 | UIThreadExecutor.SINGLETON.execute(new Runnable() { 40 | @Override 41 | public void run() { 42 | mPlugins.remove(plugin); 43 | } 44 | }); 45 | } 46 | 47 | public List getAll() { 48 | return mPlugins; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/plugin/SampleCodePlugin.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.plugin; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.view.MotionEvent; 6 | import android.view.View; 7 | 8 | import java.lang.ref.Reference; 9 | import java.lang.ref.WeakReference; 10 | 11 | import rx.Observable; 12 | import rx.functions.Func1; 13 | import rx.schedulers.Schedulers; 14 | import union.uc.com.rxjava_example.base.Tuple; 15 | import union.uc.com.rxjava_example.base.UIThreadExecutor; 16 | import us.feras.mdv.MarkdownView; 17 | 18 | /** 19 | * Created by wangli on 4/16/16. 20 | */ 21 | public class SampleCodePlugin implements DisplayPluginManager.Plugin { 22 | @Override 23 | public Tuple.Tuple2, View> getView(final Context context, String key) { 24 | MarkdownView markdownView = new MarkdownView(context){ 25 | @Override 26 | public boolean onTouchEvent(MotionEvent event) { 27 | requestDisallowInterceptTouchEvent(true); 28 | return super.onTouchEvent(event); 29 | } 30 | }; 31 | markdownView.setBackgroundColor(Color.LTGRAY); 32 | final Reference ref = new WeakReference<>(markdownView); 33 | Observable o = Observable.just(key) 34 | // .observeOn(Schedulers.io()) 35 | .map(new Func1() { 36 | @Override 37 | public String call(String s) { 38 | return mSampleCode.get(s); 39 | } 40 | }) 41 | .observeOn(Schedulers.from(UIThreadExecutor.SINGLETON)) 42 | .map(new Func1() { 43 | @Override 44 | public View call(String s) { 45 | MarkdownView mv = ref.get(); 46 | if (mv != null) { 47 | mv.loadMarkdown(s); 48 | } 49 | return mv; 50 | } 51 | }); 52 | return new Tuple.Tuple2<>(o, (View) markdownView); 53 | } 54 | 55 | private SampleCode mSampleCode = new SampleCode(); 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/sample/AdFetchActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.sample; 2 | 3 | import android.view.View; 4 | import android.widget.Button; 5 | import android.widget.LinearLayout; 6 | 7 | import rx.Observable; 8 | import rx.functions.Action1; 9 | import rx.functions.Func1; 10 | import rx.schedulers.Schedulers; 11 | import union.uc.com.rxjava_example.base.BaseActivity; 12 | 13 | /** 14 | * Created by wangli on 4/7/16. 15 | */ 16 | public class AdFetchActivity extends BaseActivity { 17 | @Override 18 | protected String myDescription() { 19 | return "本示例展示广告获取流程及错误处理:获取配置信息(ok)-->获取广告(除错)-->ok"; 20 | } 21 | 22 | @Override 23 | protected void setupWorkspace(LinearLayout workspace) { 24 | workspace.setOrientation(LinearLayout.VERTICAL); 25 | Button btn = new Button(this); 26 | btn.setText("Start"); 27 | workspace.addView(btn); 28 | btn.setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | startRx(); 32 | } 33 | }); 34 | } 35 | 36 | private class AdConfig { 37 | private final String mAdvertiser; 38 | 39 | public AdConfig(String advertiser) { 40 | mAdvertiser = advertiser; 41 | } 42 | 43 | public String getAdvertiser() { 44 | return mAdvertiser; 45 | } 46 | } 47 | 48 | private class Ad { 49 | private final String mTitle; 50 | 51 | public Ad(String title) { 52 | mTitle = title; 53 | } 54 | 55 | public String getTitle() { 56 | return mTitle; 57 | } 58 | } 59 | 60 | 61 | private void startRx() { 62 | String pub = "pubabc"; 63 | Observable o = // 64 | Observable.just(pub)// 65 | .observeOn(Schedulers.computation())// 66 | .map(new Func1() { 67 | @Override 68 | public AdConfig call(String s) { 69 | appendTip("fetch config ok! on " + Thread.currentThread().getName()); 70 | return new AdConfig("facebook"); 71 | } 72 | })// 73 | .observeOn(Schedulers.io())// 74 | .flatMap(new Func1>() { 75 | @Override 76 | public Observable call(AdConfig adConfig) { 77 | return Observable.error(new Exception("Fetch ad failed!")); 78 | } 79 | })// 80 | .observeOn(Schedulers.newThread());// 81 | o.subscribe(new Action1() { 82 | @Override 83 | public void call(Ad ad) { 84 | appendTip("finished1, get Ad:" + ad.getTitle() + " on " + Thread.currentThread().getName()); 85 | } 86 | }, new Action1() { 87 | @Override 88 | public void call(Throwable throwable) { 89 | appendTip("fetch error1:" + throwable.getMessage()); 90 | } 91 | }); 92 | 93 | o.subscribe(new Action1() { 94 | @Override 95 | public void call(Ad ad) { 96 | appendTip("finished2, get Ad:" + ad.getTitle() + " on " + Thread.currentThread().getName()); 97 | } 98 | }, new Action1() { 99 | @Override 100 | public void call(Throwable throwable) { 101 | appendTip("fetch error2:" + throwable.getMessage()); 102 | } 103 | }); 104 | 105 | 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/sample/AutoCompleteActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.sample; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.text.Editable; 6 | import android.text.TextUtils; 7 | import android.text.TextWatcher; 8 | import android.widget.EditText; 9 | import android.widget.LinearLayout; 10 | import android.widget.TextView; 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import rx.Observable; 15 | import rx.functions.Action0; 16 | import rx.functions.Action1; 17 | import rx.functions.Func1; 18 | import rx.schedulers.Schedulers; 19 | import rx.subjects.PublishSubject; 20 | import union.uc.com.rxjava_example.base.BaseActivity; 21 | import union.uc.com.rxjava_example.base.UIThreadExecutor; 22 | 23 | /** 24 | * Created by wangli on 4/5/16. 25 | */ 26 | public class AutoCompleteActivity extends BaseActivity { 27 | private EditText mSearchBox; 28 | private TextView mResult; 29 | 30 | 31 | @Override 32 | protected void onCreate(Bundle savedInstanceState) { 33 | super.onCreate(savedInstanceState); 34 | setupRx(); 35 | } 36 | 37 | @Override 38 | protected void setupWorkspace(LinearLayout workspace) { 39 | workspace.setOrientation(LinearLayout.VERTICAL); 40 | mSearchBox = new EditText(this); 41 | mSearchBox.setHintTextColor(Color.GRAY); 42 | mSearchBox.setHint("input your search key words ..."); 43 | workspace.addView(mSearchBox, 44 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 45 | LinearLayout.LayoutParams.WRAP_CONTENT)); 46 | 47 | mResult = new TextView(this); 48 | mResult.setBackgroundColor(Color.LTGRAY); 49 | workspace.addView(mResult, 50 | new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 51 | LinearLayout.LayoutParams.WRAP_CONTENT)); 52 | } 53 | 54 | @Override 55 | protected String myDescription() { 56 | return "本示例展示如何监视用户输入:\r\n" + 57 | "1. 输入中包含字符串\"ab\"才会触发后续处理\r\n" + 58 | "2. 每隔1秒钟采集一次输入"; 59 | } 60 | 61 | private void setupRx() { 62 | final PublishSubject subject = PublishSubject.create(); 63 | mSearchBox.addTextChangedListener(new TextWatcher() { 64 | @Override 65 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { 66 | 67 | } 68 | 69 | @Override 70 | public void onTextChanged(CharSequence s, int start, int before, int count) { 71 | 72 | } 73 | 74 | @Override 75 | public void afterTextChanged(Editable s) { 76 | subject.onNext(s); 77 | } 78 | }); 79 | 80 | subject// 81 | .throttleLast(1, TimeUnit.SECONDS)// 82 | .subscribeOn(Schedulers.from(UIThreadExecutor.SINGLETON))// 83 | .observeOn(Schedulers.io())// 84 | .doOnSubscribe(new Action0() { 85 | @Override 86 | public void call() { 87 | appendTip("subscribe on:" + Thread.currentThread().getName()); 88 | } 89 | }).flatMap(new Func1>() { 90 | @Override 91 | public Observable call(Editable editable) { 92 | appendTip("flatMap on:" + Thread.currentThread().getName()); 93 | return Observable.just(editable.toString()); 94 | } 95 | })// 96 | .filter(new Func1() { 97 | @Override 98 | public Boolean call(String s) { 99 | appendTip("filter on:" + Thread.currentThread().getName()); 100 | boolean match = !TextUtils.isEmpty(s) && s.contains("ab"); 101 | return match; 102 | } 103 | })// 104 | // .compose(new Observable.Transformer() { 105 | // @Override 106 | // public Observable call(Observable stringObservable) { 107 | // Observable ob = stringObservable.mergeWith(Observable.just("##")); 108 | // ob// 109 | // // .reduce(new Func2() { 110 | // // @Override 111 | // // public String call(String s, String s2) { 112 | // // return s + s2; 113 | // // } 114 | // // })// 115 | // .map(new Func1() { 116 | // @Override 117 | // public String call(String s) { 118 | // return s + "##"; 119 | // } 120 | // }); 121 | // return ob; 122 | // } 123 | // })// 124 | .retry(2)// 125 | .observeOn(Schedulers.from(UIThreadExecutor.SINGLETON))// 126 | .map(new Func1() { 127 | @Override 128 | public String call(String s) { 129 | appendTip("map on:" + Thread.currentThread().getName()); 130 | if (TextUtils.isEmpty(s)) { 131 | return s; 132 | } 133 | return s.toUpperCase(); 134 | } 135 | })// 136 | .subscribe(new Action1() {// 137 | @Override 138 | public void call(String s) { 139 | appendTip("observer run on:" + Thread.currentThread().getName()); 140 | mResult.setText(s); 141 | } 142 | }); 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/sample/BufferActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.sample; 2 | 3 | import android.view.View; 4 | import android.widget.Button; 5 | import android.widget.LinearLayout; 6 | 7 | import java.util.List; 8 | import java.util.concurrent.Executors; 9 | import java.util.concurrent.TimeUnit; 10 | 11 | import rx.Observable; 12 | import rx.functions.Action0; 13 | import rx.functions.Action1; 14 | import rx.functions.Func0; 15 | import rx.subjects.PublishSubject; 16 | import union.uc.com.rxjava_example.base.BaseActivity; 17 | 18 | /** 19 | * Created by wangli on 4/8/16. 20 | */ 21 | public class BufferActivity extends BaseActivity { 22 | @Override 23 | protected String myDescription() { 24 | return "本示例展示缓存的使用"; 25 | } 26 | 27 | @Override 28 | protected void setupWorkspace(LinearLayout workspace) { 29 | workspace.setOrientation(LinearLayout.VERTICAL); 30 | Button btn = new Button(this); 31 | btn.setText("Start"); 32 | workspace.addView(btn); 33 | btn.setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | startRx(); 37 | } 38 | }); 39 | } 40 | 41 | private void startRx() { 42 | //buffer no skip 43 | Observable.range(1, 10).buffer(3).subscribe(new Action1>() { 44 | @Override 45 | public void call(List integers) { 46 | appendTip("on buffered result:"); 47 | String s = ""; 48 | for (int l : integers) { 49 | s += l; 50 | s += " "; 51 | } 52 | appendTip(s); 53 | } 54 | }); 55 | //buffer with skip 56 | Observable.range(1, 10).buffer(2, 3).subscribe(new Action1>() { 57 | @Override 58 | public void call(List integers) { 59 | appendTip("on buffered result:"); 60 | String s = ""; 61 | for (int l : integers) { 62 | s += l; 63 | s += " "; 64 | } 65 | appendTip(s); 66 | } 67 | }); 68 | //self define buffer strategy 69 | final PublishSubject p = PublishSubject.create(); 70 | p.buffer(new Func0>() { 71 | @Override 72 | public Observable call() { 73 | final PublishSubject p2 = PublishSubject.create(); 74 | for (int i = 0; i < 3; ++i) { 75 | final Long index = (long)i; 76 | Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() { 77 | @Override 78 | public void run() { 79 | p2.onNext(index); 80 | } 81 | }, 1000 * i, TimeUnit.MILLISECONDS); 82 | } 83 | return p2; 84 | } 85 | })// 86 | .doOnNext(new Action1>() { 87 | @Override 88 | public void call(List longs) { 89 | appendTip("on next"); 90 | } 91 | })// 92 | .doOnTerminate(new Action0() { 93 | @Override 94 | public void call() { 95 | appendTip("on terminate"); 96 | } 97 | })// 98 | .subscribe(new Action1>() { 99 | @Override 100 | public void call(List ls) { 101 | appendTip("on buffered result:"); 102 | String s = ""; 103 | for (Long l : ls) { 104 | s += l; 105 | s += " "; 106 | } 107 | appendTip(s); 108 | } 109 | }); 110 | 111 | for (int i = 0; i < 2; ++i) { 112 | final Long index = (long)i; 113 | Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() { 114 | @Override 115 | public void run() { 116 | p.onNext(index); 117 | } 118 | }, 100 * i, TimeUnit.MILLISECONDS); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/union/uc/com/rxjava_example/sample/ConditionActivity.java: -------------------------------------------------------------------------------- 1 | package union.uc.com.rxjava_example.sample; 2 | 3 | import android.view.View; 4 | import android.widget.Button; 5 | import android.widget.LinearLayout; 6 | 7 | import rx.functions.Action0; 8 | import rx.functions.Action1; 9 | import rx.functions.Func1; 10 | import rx.schedulers.Schedulers; 11 | import rx.subjects.PublishSubject; 12 | import union.uc.com.rxjava_example.base.BaseActivity; 13 | import union.uc.com.rxjava_example.base.UIThreadExecutor; 14 | 15 | /** 16 | * Created by wangli on 4/5/16. 17 | */ 18 | public class ConditionActivity extends BaseActivity { 19 | @Override 20 | protected String myDescription() { 21 | return "本示例展示条件判断:\r\n" + 22 | "监听任意按钮点击事件,当点击到button3的时候停止监听."; 23 | } 24 | 25 | @Override 26 | protected void setupWorkspace(LinearLayout workspace) { 27 | final PublishSubject