Leak canary: annotate simple task

This commit is contained in:
M66B 2022-04-16 07:50:25 +02:00
parent 77d2316535
commit e4c9f80e04
1 changed files with 37 additions and 1 deletions

View File

@ -19,13 +19,49 @@ package eu.faircode.email;
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import leakcanary.AppWatcher;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
import leakcanary.LeakCanary;
import shark.HeapField;
import shark.HeapObject;
import shark.ObjectInspector;
import shark.ObjectReporter;
public class CoalMine {
static void setup(boolean enabled) {
List<ObjectInspector> inspectors = new ArrayList<>(LeakCanary.getConfig().getObjectInspectors());
inspectors.add(new ObjectInspector() {
@Override
public void inspect(@NonNull ObjectReporter reporter) {
String clazz = SimpleTask.class.getName();
reporter.whenInstanceOf(clazz, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
@Override
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
HeapField fname = instance.get(clazz, "name");
if (fname != null) {
String name = fname.getValue().readAsJavaString();
reporter.getNotLeakingReasons().add("name=" + name);
}
HeapField fstarted = instance.get(clazz, "started");
if (fstarted != null) {
Long started = fstarted.getValue().getAsLong();
if (started != null)
reporter.getNotLeakingReasons().add("started=" + new Date(started));
}
return null;
}
});
}
});
LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
.dumpHeap(enabled && BuildConfig.DEBUG)
.objectInspectors(inspectors)
.build();
LeakCanary.setConfig(config);
LeakCanary.INSTANCE.showLeakDisplayActivityLauncherIcon(BuildConfig.DEBUG);