FairEmail/app/src/debug/java/eu.faircode.email/CoalMine.java

103 lines
3.9 KiB
Java
Raw Normal View History

2022-04-13 06:17:38 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
2022-04-17 12:26:38 +00:00
import android.content.Intent;
2022-04-16 05:50:25 +00:00
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
2022-04-13 06:17:38 +00:00
import leakcanary.LeakCanary;
2022-04-16 05:50:25 +00:00
import shark.HeapField;
import shark.HeapObject;
import shark.ObjectInspector;
import shark.ObjectReporter;
2022-04-13 06:17:38 +00:00
public class CoalMine {
static void setup(boolean enabled) {
2022-04-16 05:50:25 +00:00
List<ObjectInspector> inspectors = new ArrayList<>(LeakCanary.getConfig().getObjectInspectors());
2022-04-16 18:57:55 +00:00
2022-04-16 05:50:25 +00:00
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;
}
});
}
});
2022-04-16 18:57:55 +00:00
inspectors.add(new ObjectInspector() {
@Override
public void inspect(@NonNull ObjectReporter reporter) {
String clazz = TwoStateOwner.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);
}
return null;
}
});
}
});
2022-04-13 06:17:38 +00:00
LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
.dumpHeap(enabled && BuildConfig.DEBUG)
2022-04-16 05:50:25 +00:00
.objectInspectors(inspectors)
2022-04-13 06:17:38 +00:00
.build();
LeakCanary.setConfig(config);
2022-04-17 12:26:38 +00:00
LeakCanary.INSTANCE.showLeakDisplayActivityLauncherIcon(false);
2022-04-13 06:17:38 +00:00
}
static void check() {
LeakCanary.INSTANCE.dumpHeap();
}
static void watch(Object object, String reason) {
//AppWatcher.INSTANCE.getObjectWatcher().expectWeaklyReachable(object, reason);
}
2022-04-17 12:26:38 +00:00
static Intent getIntent() {
return LeakCanary.INSTANCE.newLeakDisplayActivityIntent();
}
2022-04-13 06:17:38 +00:00
}