Skip to content

Commit

Permalink
see 12/04 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Dec 4, 2018
1 parent a9687d0 commit 14ed64d
Show file tree
Hide file tree
Showing 23 changed files with 365 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

[logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png

[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.22.0-brightgreen.svg
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.22.1-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode

[apiSvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ If this project helps you a lot and you want to support the project's developmen

[logo]: https://raw.githubusercontent.com/Blankj/AndroidUtilCode/master/art/logo.png

[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.22.0-brightgreen.svg
[aucSvg]: https://img.shields.io/badge/AndroidUtilCode-v1.22.1-brightgreen.svg
[auc]: https://github.com/Blankj/AndroidUtilCode

[apiSvg]: https://img.shields.io/badge/API-14+-brightgreen.svg
Expand Down
3 changes: 2 additions & 1 deletion app/__bus__.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"CloseAdaptActivity#start": "void com.blankj.androidutilcode.feature.core.adaptScreen.CloseAdaptActivity.start(android.content.Context)"
"CloseAdaptActivity#start": "void com.blankj.androidutilcode.feature.core.adaptScreen.CloseAdaptActivity.start(android.content.Context)",
"AdaptScreenActivity#start": "void com.blankj.androidutilcode.feature.core.adaptScreen.AdaptScreenActivityKt.start(android.content.Context)"
}
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ dependencies {
// LeakCanary
debugImplementation dep.leakcanary.android
releaseImplementation dep.leakcanary.android_no_op
// implementation 'com.blankj:utilcode:1.22.0'
// implementation 'com.blankj:utilcode:1.22.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.view.View;

Expand Down Expand Up @@ -84,7 +86,6 @@ public void activityClick(View view) {

public void adaptScreenClick(View view) {
BusUtils.post("AdaptScreenActivity#start", this);
// AdaptScreenActivity.start(this);
}

public void appClick(View view) {
Expand Down Expand Up @@ -174,4 +175,14 @@ public void spannableClick(View view) {
public void toastClick(View view) {
ToastActivity.start(this);
}

public static float getAnimationScale(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f);
} else {
return Settings.System.getFloat(context.getContentResolver(),
Settings.System.ANIMATOR_DURATION_SCALE, 1.0f);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class AdaptScreenActivity : BaseBackActivity() {
override fun initView(savedInstanceState: Bundle?, contentView: View?) {
adaptWidthBtn.setOnClickListener { WidthActivity.start(this) }
adaptHeightBtn.setOnClickListener { HeightActivity.start(this) }
closeAdaptBtn.setOnClickListener {
BusUtils.post<Any>("CloseAdaptActivity#start", this)
// CloseAdaptActivity.start(this)
}
closeAdaptBtn.setOnClickListener { CloseAdaptActivity.start(this) }
}

override fun doBusiness() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import android.support.v7.app.AppCompatActivity
import android.view.WindowManager
import com.blankj.androidutilcode.R
import com.blankj.utilcode.util.AdaptScreenUtils
import com.blankj.utilcode.util.BusUtils

class CloseAdaptActivity : AppCompatActivity() {

companion object Instance {
@JvmStatic
@BusUtils.Subscribe(name = "CloseAdaptActivity#start")
companion object {
fun start(context: Context) {
val starter = Intent(context, CloseAdaptActivity::class.java)
context.startActivity(starter)
Expand Down
19 changes: 19 additions & 0 deletions bus-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Change Log

## v1.5
升级 javassist 来修复 Kotlin 匿名类 NotFoundException

## v1.4
优化 inject 兼容 Kotlin 的 bus

## v1.3
去除 static bus,只注入 utilcode

## v1.2
修复 bug

## v1.1
升级 javassist 版本,兼容 Java8

## v1.0
发布初版本
Binary file added bus-gradle-plugin/com/blankj/bus/BusUtils.class
Binary file not shown.
37 changes: 23 additions & 14 deletions bus-gradle-plugin/src/main/java/com/blankj/bus/BusInject.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,34 @@ import com.blankj.util.JavassistUtils
import com.blankj.util.ZipUtils
import javassist.CtClass
import javassist.CtMethod
import javassist.NotFoundException
import org.apache.commons.io.FileUtils

class BusInject {

static void start(HashMap<String, String> bus, File busJar) {
String jarPath = busJar.getAbsolutePath()
String decompressedJarPath = jarPath.substring(0, jarPath.length() - 4);
File decompressedJar = new File(decompressedJarPath)
ZipUtils.unzipFile(busJar, decompressedJar)
String jarPath = busJar.getAbsolutePath()
String decompressedJarPath = jarPath.substring(0, jarPath.length() - 4);
File decompressedJar = new File(decompressedJarPath)
ZipUtils.unzipFile(busJar, decompressedJar)

CtClass busUtils = JavassistUtils.getPool().get(Config.CLASS_BUS_UTILS)
CtMethod callMethod = busUtils.getDeclaredMethod("post");
callMethod.insertAfter(getInsertContent(bus));
busUtils.writeFile(decompressedJarPath)
busUtils.defrost();
FileUtils.forceDelete(busJar)
ZipUtils.zipFile(decompressedJar, busJar)
FileUtils.forceDelete(decompressedJar)
CtClass busUtils = JavassistUtils.getPool().get(Config.BUS_UTILS_CLASS)
CtMethod callMethod;
try {
callMethod = busUtils.getDeclaredMethod("injectShell");
callMethod.insertAfter(getInsertContent(bus, false));
} catch (NotFoundException ignore) {
callMethod = busUtils.getDeclaredMethod("post");
callMethod.insertAfter(getInsertContent(bus, true));
}
busUtils.writeFile(decompressedJarPath)
busUtils.defrost();
FileUtils.forceDelete(busJar)
ZipUtils.zipFile(decompressedJar, busJar)
FileUtils.forceDelete(decompressedJar)
}

private static String getInsertContent(HashMap<String, String> bus) {
private static String getInsertContent(HashMap<String, String> bus, boolean isLow) {
StringBuilder sb = new StringBuilder();
bus.each { String key, String val ->
String name = key
Expand Down Expand Up @@ -72,7 +79,9 @@ class BusInject {
}
sb.append("}")
}
sb.append('android.util.Log.e("BusUtils", "bus of <" + $1 + "> didn\'t exist.");')
if (isLow) {
sb.append('android.util.Log.e("BusUtils", "bus of <" + $1 + "> didn\'t exist.");')
}
return sb.toString()
}
}
56 changes: 48 additions & 8 deletions bus-gradle-plugin/src/main/java/com/blankj/bus/BusScan.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.blankj.util.ZipUtils
import com.blankj.utilcode.util.BusUtils
import groovy.io.FileType
import javassist.CtClass
import javassist.CtField
import javassist.CtMethod
import javassist.NotFoundException
import org.apache.commons.io.FileUtils

import java.lang.reflect.Modifier
Expand Down Expand Up @@ -54,20 +56,58 @@ class BusScan {
className = className.substring(0, className.length() - 6)

CtClass ctClass = JavassistUtils.getPool().get(className)

CtMethod[] methods = ctClass.getDeclaredMethods();
CtMethod[] methods = ctClass.getMethods();
for (CtMethod method : methods) {
if (method.hasAnnotation(BusUtils.Subscribe)) {
if (method.hasAnnotation(BusUtils.Subscribe.class)) {
String name = ((BusUtils.Subscribe) method.getAnnotation(BusUtils.Subscribe.class)).name();
if (busMap.containsKey(name)) {
LogUtils.l("bus of " + name + " has registered: " + method.getLongName());
continue;
}
String methodLongName = method.getLongName();
if (Modifier.isStatic(method.getModifiers())) {
String name = method.getAnnotation(BusUtils.Subscribe).name()
String sign = method.getReturnType().getName() + ' ' + method.getLongName()
busMap.put(name, sign)
} else {
LogUtils.l(method.getLongName() + "is not static")
String sign = method.getReturnType().getName() + ' ' + methodLongName;
busMap.put(name, sign);
} else {// may be is kotlin
processKt(method, name, methodLongName)
}
}
}
}
}
}

private void processKt(CtMethod method, String name, String longMethodName) {
CtClass ktClass = method.getDeclaringClass();
try {
CtField instance = ktClass.getField("INSTANCE");
LogUtils.l("find INSTANCE: " + name + ": " + longMethodName);
int i = longMethodName.lastIndexOf('(');
String temp = longMethodName.substring(0, i);
int j = temp.lastIndexOf('.');
String sign = method.getReturnType().getName() + ' ' +
longMethodName.substring(0, j) +
".INSTANCE" +
longMethodName.substring(j);
busMap.put(name, sign);
} catch (NotFoundException ignore) {
String innerClassSimpleName = ktClass.getSimpleName();
if (innerClassSimpleName.contains('$') && !innerClassSimpleName.endsWith('$')) {
String innerClassName = ktClass.getName();
String outerClassName = innerClassName.substring(0, innerClassName.lastIndexOf('$'));
CtClass outerClass = JavassistUtils.getPool().get(outerClassName);
try {
CtField ctField = outerClass.getField(innerClassSimpleName.substring(innerClassSimpleName.lastIndexOf('$') + 1));
String fieldName = ctField.getName();
String methodName = longMethodName.replace('$' + fieldName, '.' + fieldName);
String sign = method.getReturnType().getName() + ' ' + methodName;
busMap.put(name, sign);
} catch (NotFoundException ignored) {
LogUtils.l(longMethodName + "is not static");
}
} else {
LogUtils.l(longMethodName + "is not static");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ class Config {

public static final String FILE_SEP = System.getProperty("file.separator")

public static final String CLASS_BUS_UTILS = 'com.blankj.utilcode.util.BusUtils'
public static final String BUS_UTILS_CLASS = 'com.blankj.utilcode.util.BusUtils'
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class JavassistUtils {
sPool = new ClassPool(null)
sPool.appendSystemPath()
// 加入本地 android 包
LogUtils.l(project.android.bootClasspath[0].toString())
sPool.appendClassPath(project.android.bootClasspath[0].toString())
}

Expand Down
Loading

0 comments on commit 14ed64d

Please sign in to comment.