├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── cn
│ │ └── sun
│ │ └── pxandroidtest
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── cn
│ │ │ └── sun
│ │ │ └── pxandroidtest
│ │ │ ├── MainActivity.java
│ │ │ ├── TestUtil.java
│ │ │ ├── collection
│ │ │ ├── IPlayerStateDispatcher.java
│ │ │ ├── PlayerManager.java
│ │ │ ├── PlayerStateDispatcher.java
│ │ │ ├── PlayerStateDispatcher2.java
│ │ │ └── PlayerStateListener.java
│ │ │ ├── invoke
│ │ │ ├── InvokeTest.java
│ │ │ ├── TestInterface.java
│ │ │ └── TestProxy.java
│ │ │ └── mvp
│ │ │ ├── IModel.java
│ │ │ ├── IPresenter.java
│ │ │ ├── IViews.java
│ │ │ ├── MVPTestActivity.java
│ │ │ ├── Model.java
│ │ │ └── Presenter.java
│ └── res
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── activity_mvp_test.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-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── cn
│ └── sun
│ └── pxandroid
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── pxandroid
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── cn
│ │ └── sun
│ │ └── pxandroid
│ │ └── ApplicationTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── cn
│ │ │ └── sun
│ │ │ └── pxandroid
│ │ │ ├── Action.java
│ │ │ ├── Function.java
│ │ │ ├── Px.java
│ │ │ ├── PxLog.java
│ │ │ ├── PxTask.java
│ │ │ └── proxy
│ │ │ ├── CollectionProxy.java
│ │ │ ├── HandlerProxy.java
│ │ │ ├── NewThreadProxy.java
│ │ │ ├── NullableProxy.java
│ │ │ ├── UIProxy.java
│ │ │ └── WorkProxy.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── cn
│ └── sun
│ └── pxandroid
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .idea
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PxAndroid
2 | 这是一个使用动态代理技术来解决线程切换,集合事件分发,以及仿写RxJava的一个项目。
3 | 下面我们来简单针对各个模块做介绍。
4 |
5 | ##动态代理
6 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '27.0.3'
6 |
7 | defaultConfig {
8 | applicationId "cn.sun.pxandroidtest"
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 | lintOptions {
21 | abortOnError false
22 | }
23 | }
24 |
25 | dependencies {
26 | implementation fileTree(dir: 'libs', include: ['*.jar'])
27 | implementation 'com.android.support:appcompat-v7:23.2.0'
28 | implementation project(':pxandroid')
29 | implementation 'io.reactivex:rxandroid:1.0.1'
30 | implementation 'io.reactivex:rxjava:1.0.14'
31 | }
32 |
--------------------------------------------------------------------------------
/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 /Users/sunzheng/Library/Android/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/cn/sun/pxandroidtest/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest;
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 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/MainActivity.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.os.Looper;
7 | import android.util.Log;
8 | import android.view.View;
9 |
10 | import cn.sun.pxandroid.Action;
11 | import cn.sun.pxandroid.Function;
12 | import cn.sun.pxandroid.PxLog;
13 | import cn.sun.pxandroid.Px;
14 | import cn.sun.pxandroidtest.collection.PlayerManager;
15 | import cn.sun.pxandroidtest.invoke.InvokeTest;
16 | import cn.sun.pxandroidtest.mvp.MVPTestActivity;
17 | import rx.Observable;
18 | import rx.android.schedulers.AndroidSchedulers;
19 | import rx.functions.Action1;
20 | import rx.functions.Func1;
21 | import rx.schedulers.Schedulers;
22 |
23 | public class MainActivity extends Activity implements View.OnClickListener {
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 | findViewById(R.id.btnPxAndroid2).setOnClickListener(this);
30 | findViewById(R.id.btnMvpTest).setOnClickListener(this);
31 | findViewById(R.id.btnRxJava).setOnClickListener(this);
32 | findViewById(R.id.btnPxAndroid).setOnClickListener(this);
33 | findViewById(R.id.btnCollectionTest).setOnClickListener(this);
34 | findViewById(R.id.btnProxy).setOnClickListener(this);
35 | }
36 |
37 | private static boolean isMainThread() {
38 | return Looper.getMainLooper() == Looper.myLooper();
39 | }
40 |
41 | public static void testPxAndroid() {
42 | Px.just("hello", "world", "sunzheng").io().map(new Function() {
43 | @Override
44 | public String call(Px px, String data) {//io线程,每一个px行为有一个
45 | TestUtil.sleep(500);
46 | Log.d("suntest", "Function1." + data + "." + isMainThread());
47 | return data + ".call1.";
48 | }
49 | }).newThread().map(new Function() {
50 | @Override
51 | public String call(Px px, String data) {//全新的IO线程
52 | Log.d("suntest", "Function2." + data + "." + isMainThread());
53 | TestUtil.sleep(500);
54 | return data + ".call2";
55 | }
56 | }).newThread().map(new Function() {
57 | @Override
58 | public String call(Px px, String data) {//全新的IO线程
59 | Log.d("suntest", "Function3." + data + "." + isMainThread());
60 | TestUtil.sleep(500);
61 | return data + ".call3";
62 | }
63 | }).io().map(new Function() {
64 | @Override
65 | public String call(Px px, String data) {//线程池
66 | Log.d("suntest", "Function4." + data + "." + isMainThread());
67 | TestUtil.sleep(500);
68 | return data + ".call4";
69 | }
70 | }).ui().execute(new Action() {
71 | @Override
72 | public void run(String Object) {
73 | Log.d("suntest", "Function5." + Object + "." + isMainThread());
74 | }
75 | });
76 | }
77 |
78 | private static void testPxAndroid2() {
79 | Px.just("one", "two", "three")
80 | .newThread().map(new Function() {
81 | @Override
82 | public String call(Px px, String s) {
83 | PxLog.d("suntest", "pxAndroid.Funct1." + s + "." + isMainThread());
84 | TestUtil.sleep(500);
85 | return s + ".Funct1.";
86 | }
87 | }).ui().execute(new Action() {
88 | @Override
89 | public void run(String Object) {
90 | Log.d("suntest", "Function5." + Object + "." + isMainThread());
91 | }
92 | });
93 | }
94 |
95 | @Override
96 | public void onClick(View v) {
97 | switch (v.getId()) {
98 | case R.id.btnMvpTest: {
99 | startActivity(new Intent(this, MVPTestActivity.class));
100 | }
101 | break;
102 | case R.id.btnRxJava: {
103 | testRxJava();
104 | }
105 | break;
106 | case R.id.btnPxAndroid: {
107 | testPxAndroid();
108 | }
109 | break;
110 | case R.id.btnPxAndroid2: {
111 | testPxAndroid2();
112 | }
113 | break;
114 | case R.id.btnCollectionTest: {
115 | testCollectionProxy();
116 | }
117 | break;
118 | case R.id.btnProxy: {
119 | testProxy();
120 | }
121 | break;
122 | }
123 | }
124 |
125 | private void testProxy() {
126 | InvokeTest.newInstance().helloWorld();
127 | }
128 |
129 | private void testCollectionProxy() {
130 | PlayerManager.getInstance().test();
131 | }
132 |
133 | private void testRxJava() {
134 | Observable.just("one", "two", "three", "four", "five")
135 | .map(new Func1() {
136 | @Override
137 | public String call(String s) {
138 | PxLog.d("suntest", "rxJava.Funct1." + s + "." + isMainThread());
139 | TestUtil.sleep(500);
140 | return s + ".Funct1.";
141 | }
142 | }).subscribeOn(Schedulers.newThread())
143 | .observeOn(AndroidSchedulers.mainThread())
144 | .subscribe(new Action1() {
145 | @Override
146 | public void call(String s) {
147 | PxLog.d("suntest", "rxJava.subscribe." + s + "." + isMainThread());
148 | }
149 | });
150 | }
151 | }
152 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/TestUtil.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/28.
5 | */
6 | public class TestUtil {
7 | public static void sleep(int times) {
8 | try {
9 | Thread.sleep(times);
10 | } catch (InterruptedException e) {
11 | e.printStackTrace();
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/collection/IPlayerStateDispatcher.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.collection;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/28.
5 | */
6 | public interface IPlayerStateDispatcher {
7 | void dispatcherStart();
8 |
9 | void dispatcherParpered();
10 |
11 | void dispatcherPause();
12 |
13 | void dispatcherStop();
14 |
15 | void dispatcherError(int errorCode);
16 |
17 | void dispatcherComplection();
18 |
19 | void addPlayerStateListener(PlayerStateListener playerStateListener);
20 |
21 | void removePlayerStateListener(PlayerStateListener playerStateListener);
22 | }
23 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/collection/PlayerManager.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.collection;
2 |
3 | import cn.sun.pxandroid.PxLog;
4 |
5 | /**
6 | * Created by sunzheng on 16/6/28.
7 | */
8 | public class PlayerManager {
9 |
10 | private static PlayerManager instance = new PlayerManager();
11 |
12 | public static PlayerManager getInstance() {
13 | return instance;
14 | }
15 |
16 | private PlayerStateListener myListener = new PlayerStateListener() {
17 | @Override
18 | public void onStart() {
19 | PxLog.d("suntest", "onStart." + Thread.currentThread());
20 | }
21 |
22 | @Override
23 | public void onParpered() {
24 | PxLog.d("suntest", "onParpered." + Thread.currentThread());
25 | }
26 |
27 | @Override
28 | public void onPause() {
29 | PxLog.d("suntest", "onPause." + Thread.currentThread());
30 | }
31 |
32 | @Override
33 | public void onStop() {
34 | PxLog.d("suntest", "onStop." + Thread.currentThread());
35 | }
36 |
37 | @Override
38 | public void onError(int errorCode) {
39 | PxLog.d("suntest", "onError." + Thread.currentThread());
40 | }
41 |
42 | @Override
43 | public void onComplection() {
44 | PxLog.d("suntest", "onComplection." + Thread.currentThread());
45 | }
46 | };
47 | private PlayerStateListener myListener2 = new PlayerStateListener() {
48 | @Override
49 | public void onStart() {
50 | PxLog.d("suntest", "onStart2." + Thread.currentThread());
51 | }
52 |
53 | @Override
54 | public void onParpered() {
55 | PxLog.d("suntest", "onParpered2." + Thread.currentThread());
56 | }
57 |
58 | @Override
59 | public void onPause() {
60 | PxLog.d("suntest", "onPause2." + Thread.currentThread());
61 | }
62 |
63 | @Override
64 | public void onStop() {
65 | PxLog.d("suntest", "onStop2." + Thread.currentThread());
66 | }
67 |
68 | @Override
69 | public void onError(int errorCode) {
70 | PxLog.d("suntest", "onError2." + Thread.currentThread());
71 | }
72 |
73 | @Override
74 | public void onComplection() {
75 | PxLog.d("suntest", "onComplection2." + Thread.currentThread());
76 | }
77 | };
78 | public void test() {
79 | PxLog.d("suntest","一般写法回调");
80 | //一般写法
81 | IPlayerStateDispatcher dispatcher1 = new PlayerStateDispatcher();
82 | dispatcher1.addPlayerStateListener(myListener);
83 | dispatcher1.addPlayerStateListener(myListener2);
84 | dispatcher1.dispatcherStart();
85 | dispatcher1.dispatcherParpered();
86 | dispatcher1.dispatcherError(0);
87 | dispatcher1.dispatcherComplection();
88 |
89 | PxLog.d("suntest","集合代理回调");
90 | //集合代理
91 | IPlayerStateDispatcher dispatcher2 = new PlayerStateDispatcher2();
92 | dispatcher2.addPlayerStateListener(myListener);
93 | dispatcher2.addPlayerStateListener(myListener2);
94 | dispatcher2.dispatcherStart();
95 | dispatcher2.dispatcherParpered();
96 | dispatcher2.dispatcherError(0);
97 | dispatcher2.dispatcherComplection();
98 |
99 |
100 | // //集合代理+线程代理
101 | // MyLog.d("suntest","集合代理+线程代理回调");
102 | // IPlayerStateDispatcher dispatcher3 = (IPlayerStateDispatcher) WorkProxy.proxy(new PlayerStateDispatcher2());
103 | // dispatcher3.addPlayerStateListener(myListener);
104 | // dispatcher3.addPlayerStateListener(myListener2);
105 | // dispatcher3.dispatcherStart();
106 | // dispatcher3.dispatcherParpered();
107 | // dispatcher3.dispatcherError(0);
108 | // dispatcher3.dispatcherComplection();
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/collection/PlayerStateDispatcher.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.collection;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | /**
7 | * Created by sunzheng on 16/6/28.
8 | */
9 | public class PlayerStateDispatcher implements IPlayerStateDispatcher {
10 | private List playerStateListeners = new ArrayList<>();
11 |
12 | @Override
13 | public void dispatcherStart() {
14 | synchronized (playerStateListeners) {
15 | for (PlayerStateListener listener : playerStateListeners) {
16 | listener.onStart();
17 | }
18 | }
19 | }
20 |
21 | @Override
22 | public void dispatcherParpered() {
23 | synchronized (playerStateListeners) {
24 | for (PlayerStateListener listener : playerStateListeners) {
25 | listener.onParpered();
26 | }
27 | }
28 | }
29 |
30 | @Override
31 | public void dispatcherPause() {
32 | synchronized (playerStateListeners) {
33 | for (PlayerStateListener listener : playerStateListeners) {
34 | listener.onPause();
35 | }
36 | }
37 | }
38 |
39 | @Override
40 | public void dispatcherStop() {
41 | synchronized (playerStateListeners) {
42 | for (PlayerStateListener listener : playerStateListeners) {
43 | listener.onStop();
44 | }
45 | }
46 | }
47 |
48 | @Override
49 | public void dispatcherError(int errorCode) {
50 | synchronized (playerStateListeners) {
51 | for (PlayerStateListener listener : playerStateListeners) {
52 | listener.onError(errorCode);
53 | }
54 | }
55 | }
56 |
57 | @Override
58 | public void dispatcherComplection() {
59 | synchronized (playerStateListeners) {
60 | for (PlayerStateListener listener : playerStateListeners) {
61 | listener.onComplection();
62 | }
63 | }
64 | }
65 |
66 | @Override
67 | public void addPlayerStateListener(PlayerStateListener playerStateListener) {
68 | synchronized (playerStateListeners){
69 | playerStateListeners.add(playerStateListener);
70 | }
71 | }
72 |
73 | @Override
74 | public void removePlayerStateListener(PlayerStateListener playerStateListener) {
75 | synchronized (playerStateListeners){
76 | playerStateListeners.remove(playerStateListener);
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/collection/PlayerStateDispatcher2.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.collection;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import cn.sun.pxandroid.proxy.CollectionProxy;
7 |
8 | /**
9 | * Created by sunzheng on 16/6/28.
10 | */
11 | public class PlayerStateDispatcher2 implements IPlayerStateDispatcher {
12 |
13 | private List playerStateListeners = new ArrayList<>();
14 |
15 | private PlayerStateListener listenerProxy = (PlayerStateListener) CollectionProxy.proxy(playerStateListeners, PlayerStateListener.class);
16 |
17 | @Override
18 | public void dispatcherStart() {
19 | listenerProxy.onStart();
20 | }
21 |
22 | @Override
23 | public void dispatcherParpered() {
24 | listenerProxy.onParpered();
25 | }
26 |
27 | @Override
28 | public void dispatcherPause() {
29 | listenerProxy.onPause();
30 | }
31 |
32 | @Override
33 | public void dispatcherStop() {
34 | listenerProxy.onStop();
35 | }
36 |
37 | @Override
38 | public void dispatcherError(int errorCode) {
39 | listenerProxy.onError(errorCode);
40 | }
41 |
42 | @Override
43 | public void dispatcherComplection() {
44 | listenerProxy.onComplection();
45 | }
46 |
47 | @Override
48 | public void addPlayerStateListener(PlayerStateListener playerStateListener) {
49 | synchronized (playerStateListeners) {
50 | playerStateListeners.add(playerStateListener);
51 | }
52 | }
53 |
54 | @Override
55 | public void removePlayerStateListener(PlayerStateListener playerStateListener) {
56 | synchronized (playerStateListeners) {
57 | playerStateListeners.remove(playerStateListener);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/collection/PlayerStateListener.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.collection;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/28.
5 | */
6 | public interface PlayerStateListener {
7 |
8 | void onStart();
9 |
10 | void onParpered();
11 |
12 | void onPause();
13 |
14 | void onStop();
15 |
16 | void onError(int errorCode);
17 |
18 | void onComplection();
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/invoke/InvokeTest.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.invoke;
2 |
3 | import java.lang.reflect.Proxy;
4 |
5 | import cn.sun.pxandroid.PxLog;
6 |
7 | /**
8 | * Created by sunzheng on 16/6/28.
9 | */
10 | public class InvokeTest implements TestInterface {
11 | public static TestInterface newInstance() {
12 | return (TestInterface) Proxy.newProxyInstance(InvokeTest.class.getClassLoader(),
13 | new Class>[]{TestInterface.class}, new TestProxy(new InvokeTest()));
14 | }
15 |
16 | @Override
17 | public void helloWorld() {
18 | PxLog.d("suntest", "helloWorld");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/invoke/TestInterface.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.invoke;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/28.
5 | */
6 | public interface TestInterface {
7 | void helloWorld();
8 | }
9 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/invoke/TestProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.invoke;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 |
6 | import cn.sun.pxandroid.PxLog;
7 |
8 | /**
9 | * Created by sunzheng on 16/6/28.
10 | */
11 | public class TestProxy implements InvocationHandler {
12 | private Object subject;
13 |
14 | public TestProxy(Object subject) {
15 | this.subject = subject;
16 | }
17 |
18 | @Override
19 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
20 | PxLog.d("suntest", "TestProxy.before.invoke." + method.getName());
21 | Object object = method.invoke(subject, args);
22 | PxLog.d("suntest", "TestProxy.after.invoke");
23 | return object;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/IModel.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/1.
5 | */
6 | public interface IModel {
7 | public String getMessage();
8 | }
9 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/IPresenter.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/1.
5 | */
6 | public interface IPresenter {
7 | void loadMessage();
8 | }
9 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/IViews.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/1.
5 | */
6 | public interface IViews {
7 | public void showMessage(String message);
8 | }
9 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/MVPTestActivity.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.TextView;
7 |
8 | import cn.sun.pxandroid.PxLog;
9 | import cn.sun.pxandroid.proxy.WorkProxy;
10 | import cn.sun.pxandroidtest.R;
11 |
12 | /**
13 | * Created by sunzheng on 16/6/1.
14 | */
15 | public class MVPTestActivity extends Activity implements IViews, View.OnClickListener {
16 |
17 | private IPresenter presenter;
18 |
19 | @Override
20 | protected void onCreate(Bundle savedInstanceState) {
21 | super.onCreate(savedInstanceState);
22 | setContentView(R.layout.activity_mvp_test);
23 | findViewById(R.id.btnMvpTest).setOnClickListener(this);
24 |
25 | IPresenter iPresenter = new Presenter(this, new Model());
26 | presenter = (IPresenter) WorkProxy.proxy(iPresenter);
27 | }
28 |
29 | @Override
30 | public void showMessage(String message) {
31 | PxLog.d("suntest", "IViews.showMessage." + Thread.currentThread());
32 | ((TextView) findViewById(R.id.tvTest)).setText(message);
33 | }
34 |
35 | @Override
36 | public void onClick(View v) {
37 | switch (v.getId()) {
38 | case R.id.btnMvpTest: {
39 | //method will be invoke in work thread
40 | presenter.loadMessage();
41 | }
42 | break;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/Model.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | import java.util.UUID;
4 |
5 | import cn.sun.pxandroid.PxLog;
6 | import cn.sun.pxandroidtest.TestUtil;
7 |
8 | /**
9 | * Created by sunzheng on 16/6/1.
10 | */
11 | public class Model implements IModel {
12 |
13 | @Override
14 | public String getMessage() {
15 | PxLog.d("suntest", "Model.getMessage." + Thread.currentThread());
16 | TestUtil.sleep(500);
17 | return "new message:" + UUID.randomUUID();
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/sun/pxandroidtest/mvp/Presenter.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroidtest.mvp;
2 |
3 | import cn.sun.pxandroid.PxLog;
4 | import cn.sun.pxandroid.proxy.UIProxy;
5 |
6 | /**
7 | * Created by sunzheng on 16/6/1.
8 | */
9 | public class Presenter implements IPresenter {
10 | private IViews iViews;
11 | private IModel model;
12 |
13 | public Presenter(IViews views, IModel model) {
14 | //notice a UI proxy will be returen;
15 | this.iViews = (IViews) UIProxy.proxy(views);
16 | this.model = model;
17 | }
18 |
19 | @Override
20 | public void loadMessage() {
21 | PxLog.d("suntest", "Presenter.loadMessage." + Thread.currentThread());
22 | //notice this is work thread,耗时操作
23 | String messgae = model.getMessage();
24 | //next action will be invoked in UI thread
25 | iViews.showMessage(messgae);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
24 |
25 |
30 |
31 |
36 |
37 |
42 |
43 |
48 |
49 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_mvp_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
24 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PxAndroid
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/cn/sun/pxandroid/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | google()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:3.1.3'
10 |
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | allprojects {
17 | repositories {
18 | jcenter()
19 | google()
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunzheng123pr/PxAndroid/6a5426e2e2e6420179397d0188338979969d5d84/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Oct 26 00:43:54 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/pxandroid/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/pxandroid/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'maven'
3 | android {
4 | compileSdkVersion 23
5 | buildToolsVersion '27.0.3'
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 23
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | lintOptions {
20 | abortOnError false
21 | }
22 |
23 | }
24 | uploadArchives {
25 | repositories {
26 | mavenDeployer {
27 | repository(url: "file://" + projectDir + "/../../cbg-libs-maven") {
28 | pom.groupId = 'cn.sun.pxandroid'
29 | pom.artifactId = 'core'
30 | pom.version = '1.0.1'
31 | }
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------
/pxandroid/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 /Users/sunzheng/Library/Android/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 |
--------------------------------------------------------------------------------
/pxandroid/src/androidTest/java/cn/sun/pxandroid/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
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 | }
--------------------------------------------------------------------------------
/pxandroid/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/Action.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | /**
4 | * Created by sunzheng on 16/6/2.
5 | */
6 | public interface Action {
7 | void run(T Object);
8 | }
9 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/Function.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | /**
4 | * function to changed data
5 | * Created by sunzheng on 16/6/2.
6 | */
7 | public interface Function {
8 | R call(Px px,T data);
9 | }
10 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/Px.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | import android.os.Handler;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Arrays;
7 | import java.util.Collection;
8 |
9 | import cn.sun.pxandroid.proxy.HandlerProxy;
10 | import cn.sun.pxandroid.proxy.NewThreadProxy;
11 | import cn.sun.pxandroid.proxy.UIProxy;
12 | import cn.sun.pxandroid.proxy.WorkProxy;
13 |
14 | /**
15 | * Created by sunzheng on 16/6/1.
16 | */
17 | public final class Px implements PxTask {
18 | private Collection datas;
19 | private boolean isCanceled;
20 | private PxTask next;
21 | private PxTask first;
22 | private Function mFunction;
23 | private Action mAction;
24 |
25 | public Px(Collection datas) {
26 | this.datas = datas;
27 | this.first = this;
28 | }
29 |
30 | /**
31 | * changed to ui thread
32 | */
33 | public Px ui() {
34 | Px nextTask = Px.just();
35 | this.next = (PxTask) UIProxy.proxy(nextTask);
36 | nextTask.first = this.first;
37 | return nextTask;
38 | }
39 |
40 | /**
41 | * change to io thread
42 | * 每一个Px行为有一个work thread
43 | */
44 | public Px io() {
45 | Px nextTask = Px.just();
46 | this.next = (PxTask) WorkProxy.proxy(nextTask);
47 | nextTask.first = this.first;
48 | return nextTask;
49 | }
50 |
51 | /**
52 | * change to handle thread
53 | */
54 | public Px handle(Handler handler) {
55 | Px nextTask = Px.just();
56 | this.next = (PxTask) HandlerProxy.proxy(nextTask, handler);
57 | nextTask.first = this.first;
58 | return nextTask;
59 | }
60 |
61 | /**
62 | * 切换到新的后台线程
63 | */
64 | public Px newThread() {
65 | Px nextTask = Px.just();
66 | this.next = (PxTask) NewThreadProxy.proxy(nextTask);
67 | nextTask.first = this.first;
68 | return nextTask;
69 | }
70 |
71 | public final Px map(Function super T, ? extends R> func) {
72 | Px nextTask = Px.just();
73 | nextTask.first = this.first;
74 | this.next = nextTask;
75 | this.mFunction = func;
76 | return nextTask;
77 | }
78 |
79 | @Override
80 | public void onExecute() {
81 | for (T object : datas) {
82 | if (isCanceled) {
83 | return;
84 | }
85 | if (mFunction != null) {
86 | if (next != null) {
87 | Object result = mFunction.call(this, object);
88 | if (isCanceled) {
89 | return;
90 | }
91 | next.onDispatch(result);
92 | }
93 | } else if (next != null) {
94 | next.onDispatch(object);
95 | } else {
96 | if (this.mAction != null) {
97 | this.mAction.run(object);
98 | }
99 | }
100 |
101 | }
102 | }
103 |
104 | @Override
105 | public void onDispatch(T object) {
106 | datas = Arrays.asList(object);
107 | onExecute();
108 | }
109 |
110 | public final void execute(Action action) {
111 | this.mAction = action;
112 | execute();
113 | }
114 |
115 | public final void execute() {
116 | first.onExecute();
117 | }
118 |
119 | public final void cancel() {
120 | isCanceled = true;
121 | }
122 |
123 |
124 | public static Px create(Collection datas) {
125 | return new Px<>(datas);
126 | }
127 |
128 | public static Px from(Collection datas) {
129 | return new Px<>(datas);
130 | }
131 |
132 | public static Px from(T[] array) {
133 | Collection collection = new ArrayList();
134 | if (array != null) {
135 | for (T t : array) {
136 | collection.add(t);
137 | }
138 | }
139 | return create(collection);
140 | }
141 |
142 | public static Px just(final T value) {
143 | return create(Arrays.asList(value));
144 | }
145 |
146 | public static Px just(final T... value) {
147 | return create(Arrays.asList(value));
148 | }
149 | }
150 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/PxLog.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | import android.provider.Settings;
4 | import android.util.Log;
5 |
6 | /**
7 | * Created by sunzheng on 16/6/24.
8 | */
9 | public class PxLog {
10 | private static final String TAG = "px-tag";
11 |
12 | private static final boolean DEBUG = true;
13 |
14 | public static void d(String message) {
15 | d(TAG, message);
16 | }
17 |
18 | public static void d(String tag, String message) {
19 | if (DEBUG) {
20 | Log.d(tag, message);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/PxTask.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | public interface PxTask {
4 | void onExecute();
5 |
6 | void onDispatch(T object);
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/CollectionProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Method;
5 | import java.lang.reflect.Proxy;
6 | import java.util.Collection;
7 |
8 | /**
9 | * Created by sunzheng on 16/5/31.
10 | */
11 | public class CollectionProxy {
12 |
13 | public static Object proxy(Collection> collection, Class>... interfaces) {
14 | CollectionInvocationH invocationH = new CollectionInvocationH(collection, null);
15 | return Proxy.newProxyInstance(invocationH.getClass().getClassLoader(), interfaces, invocationH);
16 | }
17 |
18 | public static Object proxy(Collection> collection, Strategy strategy, Class>... interfaces) {
19 | CollectionInvocationH invocationH = new CollectionInvocationH(collection, strategy);
20 | return Proxy.newProxyInstance(invocationH.getClass().getClassLoader(), interfaces, invocationH);
21 | }
22 |
23 | public static class CollectionInvocationH implements InvocationHandler {
24 |
25 | private Collection> subSubjects;
26 | private Strategy mStrategy;
27 |
28 | public CollectionInvocationH(Collection> subSubjects, Strategy strategy) {
29 | this.subSubjects = subSubjects;
30 | this.mStrategy = strategy;
31 | }
32 |
33 | @Override
34 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
35 | synchronized (subSubjects) {
36 | for (Object object : subSubjects) {
37 | Object result = method.invoke(object, args);
38 | if (mStrategy == Strategy.FINISH_WHEN_TRUE) {
39 | if (result == Boolean.TRUE) {
40 | return result;
41 | }
42 | }
43 | }
44 | }
45 | return null;
46 | }
47 | }
48 |
49 | enum Strategy {
50 | FINISH_WHEN_TRUE
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/HandlerProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import android.os.Handler;
4 | import android.os.Looper;
5 | import android.os.Message;
6 |
7 | import java.lang.reflect.InvocationHandler;
8 | import java.lang.reflect.Method;
9 | import java.lang.reflect.Proxy;
10 |
11 | /**
12 | * method will be called in another mHandler
13 | *
14 | * Created by sunzheng on 16/5/30.
15 | */
16 | public class HandlerProxy {
17 | public static Object proxy(Object target, Handler handler) {
18 | return proxy(target, handler.getLooper(), target.getClass().getInterfaces());
19 | }
20 |
21 | public static Object proxy(Object target, Handler handler, Class>... interfaces) {
22 | return proxy(target, handler.getLooper(), interfaces);
23 | }
24 |
25 | public static Object proxy(Object target, Looper looper) {
26 | return proxy(target, looper, target.getClass().getInterfaces());
27 | }
28 |
29 | public static Object proxy(Object target, Looper looper, Class>... interfaces) {
30 | PostInvocationH postInvocationH = new PostInvocationH(target, looper);
31 | return Proxy.newProxyInstance(postInvocationH.getClass().getClassLoader(), interfaces, postInvocationH);
32 | }
33 |
34 |
35 | public static class PostInvocationH implements InvocationHandler {
36 | private static final String TAG = "HandlerProxy";
37 | protected Looper mLooper;
38 |
39 | private Handler mHandler;
40 |
41 | protected Object subject;
42 |
43 | public PostInvocationH(Object subject, Looper looper) {
44 | this.subject = subject;
45 | mHandler = new H(looper);
46 | mLooper = looper;
47 | }
48 |
49 | private static final int MSG_INVOKE_METHOD = 0x001;
50 |
51 | @Override
52 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
53 | if (!method.getReturnType().equals(Void.TYPE)) {
54 | throw new IllegalArgumentException("only void method is support in mHandler dispatcher." + method.getReturnType());
55 | }
56 | mHandler.obtainMessage(MSG_INVOKE_METHOD, InvokeRecord.obtain(subject, method, args)).sendToTarget();
57 |
58 | return null;
59 | }
60 |
61 | protected class H extends Handler {
62 |
63 | public H(Looper looper) {
64 | super(looper);
65 | }
66 |
67 | @Override
68 | public void handleMessage(Message msg) {
69 | super.handleMessage(msg);
70 | switch (msg.what) {
71 | case MSG_INVOKE_METHOD: {
72 | handleInvoke((InvokeRecord) msg.obj);
73 | }
74 | break;
75 | }
76 | }
77 | }
78 |
79 | /**
80 | * method called in io handle
81 | */
82 | protected void handleInvoke(InvokeRecord record) {
83 | record.invoke();
84 | }
85 | }
86 |
87 | /**
88 | * Created by sunzheng on 16/5/30.
89 | */
90 | public static class InvokeRecord {
91 |
92 | public Object proxy;
93 |
94 | public Method method;
95 |
96 | public Object[] args;
97 |
98 | public InvokeRecord(Object proxy, Method method, Object[] args) {
99 | this.proxy = proxy;
100 | this.method = method;
101 | this.args = args;
102 | }
103 |
104 | public static InvokeRecord obtain(Object proxy, Method method, Object[] args) {
105 | return new InvokeRecord(proxy, method, args);
106 | }
107 |
108 | public void invoke() {
109 | try {
110 | method.invoke(proxy, args);
111 | } catch (Exception e) {
112 | throw new RuntimeException(e);
113 | }
114 | }
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/NewThreadProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import android.os.Looper;
4 | import android.util.Log;
5 |
6 | import java.lang.reflect.InvocationHandler;
7 | import java.lang.reflect.InvocationTargetException;
8 | import java.lang.reflect.Method;
9 | import java.lang.reflect.Proxy;
10 | import java.util.concurrent.ExecutorService;
11 | import java.util.concurrent.Executors;
12 |
13 | /**
14 | * Created by sunzheng on 16/6/1.
15 | */
16 | public class NewThreadProxy {
17 | public static Object proxy(Object target) {
18 | return proxy(target, target.getClass().getInterfaces());
19 | }
20 |
21 | public static Object proxy(Object target, Class>... interfaces) {
22 | NewThreadInvocationH invocationH = new NewThreadInvocationH(target);
23 | return Proxy.newProxyInstance(invocationH.getClass().getClassLoader(), interfaces, invocationH);
24 | }
25 |
26 | public static class NewThreadInvocationH implements InvocationHandler {
27 |
28 | private Object mSubject;
29 |
30 | public NewThreadInvocationH(Object subject) {
31 | this.mSubject = subject;
32 | }
33 |
34 | @Override
35 | public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
36 | new Thread(new Runnable() {
37 | @Override
38 | public void run() {
39 | try {
40 | method.invoke(mSubject, args);
41 | } catch (IllegalAccessException e) {
42 | e.printStackTrace();
43 | } catch (InvocationTargetException e) {
44 | e.printStackTrace();
45 | }
46 | }
47 | }).start();
48 | return null;
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/NullableProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.InvocationTargetException;
5 | import java.lang.reflect.Method;
6 | import java.lang.reflect.Proxy;
7 |
8 | public class NullableProxy {
9 | public static Object proxy(Object target, Class>... interfaces) {
10 | NullInvocationH invocationH = new NullInvocationH(target);
11 | return Proxy.newProxyInstance(invocationH.getClass().getClassLoader(), interfaces, invocationH);
12 | }
13 |
14 | public static class NullInvocationH implements InvocationHandler {
15 |
16 | private Object mSubject;
17 |
18 | public NullInvocationH(Object subject) {
19 | this.mSubject = subject;
20 | }
21 |
22 | @Override
23 | public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
24 | if (mSubject != null) {
25 | method.invoke(mSubject, args);
26 | }
27 | return null;
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/UIProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import android.os.Looper;
4 |
5 | import java.lang.reflect.Proxy;
6 |
7 | /**
8 | * Created by sunzheng on 16/6/1.
9 | */
10 | public class UIProxy {
11 | public static Object proxy(Object target) {
12 | return proxy(target, target.getClass().getInterfaces());
13 | }
14 |
15 | public static Object proxy(Object target, Class>... interfaces) {
16 | return HandlerProxy.proxy(target, Looper.getMainLooper(), interfaces);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/pxandroid/src/main/java/cn/sun/pxandroid/proxy/WorkProxy.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid.proxy;
2 |
3 | import android.os.HandlerThread;
4 | import android.os.Looper;
5 |
6 | import java.lang.reflect.InvocationHandler;
7 | import java.lang.reflect.InvocationTargetException;
8 | import java.lang.reflect.Method;
9 | import java.lang.reflect.Proxy;
10 | import java.util.Collection;
11 | import java.util.concurrent.ExecutorService;
12 | import java.util.concurrent.Executors;
13 | import java.util.concurrent.atomic.AtomicInteger;
14 |
15 | /**
16 | * Created by sunzheng on 16/6/1.
17 | */
18 | public class WorkProxy {
19 | private static ExecutorService mExecutor = Executors.newFixedThreadPool(4);
20 |
21 | public static Object proxy(Object target) {
22 | return proxy(target, target.getClass().getInterfaces());
23 | }
24 |
25 | public static Object proxy(Object target, Class>... interfaces) {
26 | WorkInvocationH invocationH = new WorkInvocationH(target);
27 | return Proxy.newProxyInstance(invocationH.getClass().getClassLoader(), interfaces, invocationH);
28 | }
29 |
30 | public static class WorkInvocationH implements InvocationHandler {
31 |
32 | private Object mSubject;
33 |
34 | public WorkInvocationH(Object subject) {
35 | this.mSubject = subject;
36 | }
37 |
38 | @Override
39 | public Object invoke(Object proxy, final Method method, final Object[] args) throws Throwable {
40 | mExecutor.execute(new Runnable() {
41 | @Override
42 | public void run() {
43 | try {
44 | method.invoke(mSubject, args);
45 | } catch (IllegalAccessException e) {
46 | e.printStackTrace();
47 | } catch (InvocationTargetException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 | });
52 | return null;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/pxandroid/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | pxandroid
3 |
4 |
--------------------------------------------------------------------------------
/pxandroid/src/test/java/cn/sun/pxandroid/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.sun.pxandroid;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * To work on unit tests, switch the Test Artifact in the Build Variants view.
9 | */
10 | public class ExampleUnitTest {
11 | @Test
12 | public void addition_isCorrect() throws Exception {
13 | assertEquals(4, 2 + 2);
14 | }
15 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':pxandroid'
2 |
--------------------------------------------------------------------------------