未验证 提交 2ac1116e 编写于 作者: 金台 提交者: GitHub

Merge pull request #906 from mikaelzero/bigimg_coil

add big image detect for coil
......@@ -162,6 +162,7 @@ dependencies {
implementation rootProject.ext.dependencies["glide"]
// implementation rootProject.ext.dependencies["glide_okhttp3"]
kapt rootProject.ext.dependencies["glide_compiler"]
implementation rootProject.ext.dependencies["coil"]
implementation rootProject.ext.dependencies["picasso"]
implementation rootProject.ext.dependencies["fresco"]
implementation rootProject.ext.dependencies["fresco-processors"]
......
......@@ -22,6 +22,8 @@ import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.imageLoader
import coil.request.CachePolicy
import com.amap.api.location.AMapLocationListener
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ThreadUtils
......@@ -399,6 +401,7 @@ class MainDebugActivityOkhttpV3 : BaseActivity(), View.OnClickListener,
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fyouimg1.c-ctrip.com%2Ftarget%2Ftg%2F035%2F063%2F726%2F3ea4031f045945e1843ae5156749d64c.jpg&refer=http%3A%2F%2Fyouimg1.c-ctrip.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621652979&t=7150aaa2071d512cf2f6b556e126dd66"
val imageLoaderImageUrl =
"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fyouimg1.c-ctrip.com%2Ftarget%2Ftg%2F004%2F531%2F381%2F4339f96900344574a0c8ca272a7b8f27.jpg&refer=http%3A%2F%2Fyouimg1.c-ctrip.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621652979&t=b7e83ecc987c64cc31079469d292eb56"
val coilImageUrl = "https://cdn.nlark.com/yuque/0/2020/png/252337/1587091196083-assets/web-upload/62122ab5-986b-4662-be88-d3007a5e31c5.png"
Picasso.get().load(picassoImgUrl)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.placeholder(R.mipmap.cat)
......@@ -413,6 +416,16 @@ class MainDebugActivityOkhttpV3 : BaseActivity(), View.OnClickListener,
.skipMemoryCache(true)
.transform(CircleCrop())
.into((findViewById<View>(R.id.iv_glide) as ImageView))
findViewById<ImageView>(R.id.iv_coil).apply {
val request = coil.request.ImageRequest.Builder(this.context)
.memoryCachePolicy(CachePolicy.DISABLED)
.diskCachePolicy(CachePolicy.DISABLED)
.data(coilImageUrl)
.target(this)
.build()
imageLoader.enqueue(request)
}
val imageLoader = ImageLoader.getInstance()
imageLoader.displayImage(
imageLoaderImageUrl,
......
......@@ -45,6 +45,49 @@
android:background="#999999"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Coil"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_coil"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1" />
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
......
......@@ -88,6 +88,20 @@ class BigImgClassTransformer : AbsClassTransformer() {
}
}
//Coil
if (className == "coil.request.ImageRequest") {
"hook Coil Start".println()
klass.methods.find { methodNode ->
methodNode.name == "<init>" && methodNode.desc != null
}.let { methodNode ->
//函数结束的地方插入
methodNode?.instructions?.getMethodExitInsnNodes()?.forEach {
"${context.projectDir.lastPath()}->hook Coil succeed: ${className}_${methodNode.name}_${methodNode.desc}".println()
methodNode.instructions?.insertBefore(it, createCoilInsnList())
}
}
}
return klass
}
......@@ -201,5 +215,20 @@ class BigImgClassTransformer : AbsClassTransformer() {
this
}
}
private fun createCoilInsnList(): InsnList {
return with(InsnList()) {
add(VarInsnNode(ALOAD, 0))
add(
MethodInsnNode(
INVOKESTATIC,
"com/didichuxing/doraemonkit/aop/bigimg/coil/CoilHook",
"proxy",
"(Ljava/lang/Object;)V",
false
)
)
this
}
}
}
......@@ -95,6 +95,7 @@ ext {
"glide_v3" : 'com.github.bumptech.glide:glide:3.8.0',
"glide_okhttp3" : "com.github.bumptech.glide:okhttp3-integration:${android["glide_version"]}",
"glide_compiler" : "com.github.bumptech.glide:compiler:${android["glide_version"]}",
"coil" : 'io.coil-kt:coil:1.3.2',
"picasso" : 'com.squareup.picasso:picasso:2.71828',
"fresco" : 'com.facebook.fresco:fresco:1.13.0',
"fresco-processors" : "jp.wasabeef:fresco-processors:2.1.0",
......
......@@ -88,6 +88,19 @@ class BigImgClassTransformer : AbsClassTransformer() {
}
}
//Coil
if (className == "coil.request.ImageRequest") {
"hook Coil Start".println()
klass.methods.find { methodNode ->
methodNode.name == "<init>" && methodNode.desc != null
}.let { methodNode ->
//函数结束的地方插入
methodNode?.instructions?.getMethodExitInsnNodes()?.forEach {
"${context.projectDir.lastPath()}->hook Coil succeed: ${className}_${methodNode.name}_${methodNode.desc}".println()
methodNode.instructions?.insertBefore(it, createCoilInsnList())
}
}
}
return klass
}
......@@ -201,5 +214,20 @@ class BigImgClassTransformer : AbsClassTransformer() {
this
}
}
private fun createCoilInsnList(): InsnList {
return with(InsnList()) {
add(VarInsnNode(ALOAD, 0))
add(
MethodInsnNode(
INVOKESTATIC,
"com/didichuxing/doraemonkit/aop/bigimg/coil/CoilHook",
"proxy",
"(Ljava/lang/Object;)V",
false
)
)
this
}
}
}
......@@ -101,6 +101,7 @@ dependencies {
//三大图片框架
compileOnly rootProject.ext.dependencies["coil"]
compileOnly rootProject.ext.dependencies["glide"]
compileOnly rootProject.ext.dependencies["picasso"]
compileOnly rootProject.ext.dependencies["fresco"]
......
package com.didichuxing.doraemonkit.aop.bigimg.coil;
import android.util.Log;
import com.didichuxing.doraemonkit.util.ReflectUtils;
import java.util.ArrayList;
import java.util.List;
import coil.request.ImageRequest;
import coil.transform.Transformation;
/**
* ================================================
* 作 者:mikaelzero
* 版 本:1.0
* 创建日期:2021/9/17
* 描 述:注入到 coil.request.ImageRequest#init(方法中)
* 修订历史:
* ================================================
*/
public class CoilHook {
/**
* hook transformations
*
*/
public static void proxy(Object request) {
Log.e("CoilHook", "CoilHook proxy");
try {
if (request instanceof ImageRequest) {
ImageRequest requestObj = (ImageRequest) request;
List<Transformation> transformations = requestObj.getTransformations();
if (transformations.isEmpty()) {
transformations = new ArrayList<>();
transformations.add(new DokitCoilTransformation(requestObj.getData()));
} else {
transformations.clear();
transformations.add(new DokitCoilTransformation(requestObj.getData()));
}
ReflectUtils.reflect(request).field("transformations", transformations);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.didichuxing.doraemonkit.aop.bigimg.coil;
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.didichuxing.doraemonkit.config.PerformanceSpInfoConfig;
import com.didichuxing.doraemonkit.constant.MemoryConstants;
import com.didichuxing.doraemonkit.kit.largepicture.LargePictureManager;
import com.didichuxing.doraemonkit.util.ConvertUtils;
import coil.bitmap.BitmapPool;
import coil.size.Size;
import coil.transform.Transformation;
import kotlin.coroutines.Continuation;
/**
* ================================================
* 作 者:mikaelzero
* 版 本:1.0
* 创建日期:2021/9/17
* 描 述:
* 修订历史:
* ================================================
*/
public class DokitCoilTransformation implements Transformation {
private static final String TAG = "DokitTransformation";
private Object mUri;
public DokitCoilTransformation(Object uri) {
this.mUri = uri;
}
@Override
public String key() {
return "Dokit&Coil&LargeBitmapTransformation";
}
@Nullable
@Override
public Object transform(@NonNull BitmapPool bitmapPool, @NonNull Bitmap source, @NonNull Size size, @NonNull Continuation<? super Bitmap> continuation) {
try {
if (PerformanceSpInfoConfig.isLargeImgOpen()) {
double imgSize = ConvertUtils.byte2MemorySize(source.getByteCount(), MemoryConstants.MB);
if (mUri != null) {
LargePictureManager.getInstance().saveImageInfo(mUri.toString(), imgSize, source.getWidth(), source.getHeight(), "Coil");
} else {
LargePictureManager.getInstance().saveImageInfo("Null Uri", imgSize, source.getWidth(), source.getHeight(), "Coil");
}
}
} catch (Exception e) {
e.printStackTrace();
}
return source;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册