/
MainReactPackageWithFrescoCache.java
121 lines (99 loc) · 3.99 KB
/
MainReactPackageWithFrescoCache.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package iyegoroff.imagefilterkit;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import com.facebook.common.internal.Supplier;
import com.facebook.common.logging.FLog;
import com.facebook.imagepipeline.cache.DefaultBitmapMemoryCacheParamsSupplier;
import com.facebook.imagepipeline.cache.MemoryCacheParams;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.ModuleSpec;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.modules.fresco.FrescoModule;
import com.facebook.react.shell.MainReactPackage;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import iyegoroff.reflectutils.ReflectUtils;
public class MainReactPackageWithFrescoCache extends MainReactPackage {
private @Nullable final Integer mMaxCacheEntries;
private @Nullable final Integer mMaxCacheSizeInBytes;
private static @Nonnull Bitmap.Config sBitmapsConfig = Bitmap.Config.ARGB_8888;
@SuppressWarnings("WeakerAccess")
public MainReactPackageWithFrescoCache(
@Nullable Integer maxCacheEntries,
@Nullable Integer maxCacheSizeInBytes,
@Nullable Bitmap.Config bitmapsConfig
) {
super();
mMaxCacheEntries = maxCacheEntries;
mMaxCacheSizeInBytes = maxCacheSizeInBytes;
sBitmapsConfig = bitmapsConfig == null ? sBitmapsConfig : bitmapsConfig;
}
@SuppressWarnings("WeakerAccess")
public MainReactPackageWithFrescoCache() {
this(null, null, null);
}
@SuppressWarnings("UnusedDeclaration")
public static List<ReactPackage> inject(List<ReactPackage> packages) {
for (int i = 0; i < packages.size(); i++) {
if (packages.get(i).getClass() == MainReactPackage.class) {
packages.set(i, new MainReactPackageWithFrescoCache());
break;
}
}
return packages;
}
public static Bitmap.Config bitmapsConfig() {
return sBitmapsConfig;
}
private static boolean isFresco(ModuleSpec module) {
@Nullable String name = ReflectUtils.invokeMethod(module, "getName");
name = name == null ? ReflectUtils.invokeMethod(module, "getClassName") : name;
return name != null && name.endsWith("FrescoModule");
}
private Supplier<MemoryCacheParams> createCacheParamsSupplier(
final ReactApplicationContext context
) {
ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
final Supplier<MemoryCacheParams> cacheParamsSupplier =
new DefaultBitmapMemoryCacheParamsSupplier(manager) {
@Override
public MemoryCacheParams get() {
MemoryCacheParams params = super.get();
return new MemoryCacheParams(
mMaxCacheSizeInBytes == null ? params.maxCacheSize : mMaxCacheSizeInBytes,
mMaxCacheEntries == null ? params.maxCacheEntries : mMaxCacheEntries,
params.maxEvictionQueueSize,
params.maxEvictionQueueEntries,
params.maxCacheEntrySize
);
}
};
String size = String.valueOf(cacheParamsSupplier.get().maxCacheSize / 1024 / 1024);
String entries = String.valueOf(cacheParamsSupplier.get().maxCacheEntries);
FLog.d(
ReactConstants.TAG,
"ImageFilterKit: Fresco cache size - " + entries + " entries, " + size + " MB overall"
);
return cacheParamsSupplier;
}
private FrescoModule createFrescoModule(final ReactApplicationContext context) {
return new FrescoModule(
context,
true,
FrescoModule.getDefaultConfigBuilder(context)
.setMemoryTrimmableRegistry(MemoryTrimmer.getInstance())
.setBitmapMemoryCacheParamsSupplier(createCacheParamsSupplier(context))
.setBitmapsConfig(sBitmapsConfig)
.build()
);
}
public NativeModule getModule(String name, ReactApplicationContext context) {
return FrescoModule.NAME.equals(name)
? createFrescoModule(context)
: super.getModule(name, context);
}
}