-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LinuxPara
authored and
LinuxPara
committed
Dec 7, 2019
1 parent
8728e16
commit 546b39b
Showing
17 changed files
with
365 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,38 @@ | ||
apply plugin: 'java-library' | ||
apply plugin: 'com.android.library' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.2" | ||
|
||
|
||
defaultConfig { | ||
minSdkVersion 15 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
|
||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
} | ||
|
||
sourceCompatibility = "7" | ||
targetCompatibility = "7" | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test:runner:1.2.0' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
} |
2 changes: 2 additions & 0 deletions
2
MvpPluginTest/MvpPluginDependent/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.mvp.plugin.dependent" /> |
14 changes: 14 additions & 0 deletions
14
...nTest/MvpPluginDependent/src/main/java/com/mvp/plugin/dependent/annotation/ExecuteOn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.mvp.plugin.dependent.annotation; | ||
|
||
import com.mvp.plugin.dependent.thread.ThreadMode; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface ExecuteOn { | ||
ThreadMode thread(); | ||
} |
83 changes: 83 additions & 0 deletions
83
...nDependent/src/main/java/com/mvp/plugin/dependent/delegate/DelegateInvocationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.mvp.plugin.dependent.delegate; | ||
|
||
import com.mvp.plugin.dependent.annotation.ExecuteOn; | ||
import com.mvp.plugin.dependent.thread.ThreadTool; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.lang.ref.WeakReference; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class DelegateInvocationHandler implements InvocationHandler { | ||
|
||
private final WeakReference<Object> mWrTarget; | ||
|
||
public DelegateInvocationHandler(Object target) { | ||
mWrTarget = new WeakReference<>(target); | ||
} | ||
|
||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) { | ||
if (mWrTarget.get() != null) { | ||
try { | ||
//被代理的方法 | ||
Method delegateMethod = mWrTarget.get().getClass().getMethod(method.getName(), method.getParameterTypes()); | ||
delegateMethod.setAccessible(true); | ||
if (!needThreadHandle(delegateMethod, args)) { | ||
return invokeMethod(delegateMethod, mWrTarget.get(), args); | ||
} | ||
} catch (NoSuchMethodException e) { | ||
e.printStackTrace(); | ||
} | ||
} else { | ||
Exception e = new Exception("View的被代理对象被回收了!代理的方法:" + method.getName() + " 参数:" + Arrays.toString(method.getParameterTypes())); | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
private boolean needThreadHandle(Method method, Object[] args) { | ||
ExecuteOn executeOnAnno = method.getAnnotation(ExecuteOn.class); | ||
if (executeOnAnno != null) { | ||
if (method.getReturnType() != Void.TYPE) { | ||
throw new RuntimeException("ExecuteOn注解修饰的函数" + method.getName() + "返回值必须是void!"); | ||
} | ||
switch (executeOnAnno.thread()) { | ||
case MAIN: | ||
ThreadTool.executeOnMainThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
invokeMethod(method, mWrTarget.get(), args); | ||
} | ||
}); | ||
return true; | ||
case ASYNC: | ||
ThreadTool.executeOnAsyncThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
invokeMethod(method, mWrTarget.get(), args); | ||
} | ||
}); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private Object invokeMethod(Method method, Object target, Object[] args) { | ||
try { | ||
return method.invoke(target, args); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.