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

120 lines
5.2 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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2022-04-13 06:17:38 +00:00
*/
2022-04-23 20:46:59 +00:00
import android.app.Application;
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;
2022-04-24 06:40:22 +00:00
import java.util.concurrent.TimeUnit;
2022-04-16 05:50:25 +00:00
import kotlin.Unit;
import kotlin.jvm.functions.Function2;
2022-04-17 19:34:33 +00:00
import leakcanary.AppWatcher;
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 {
2022-04-23 20:46:59 +00:00
static void install(@NonNull Application app) {
2022-04-24 06:40:22 +00:00
AppWatcher.INSTANCE.manualInstall(app, TimeUnit.SECONDS.toMillis(5));
2022-04-23 20:46:59 +00:00
}
2022-04-13 06:17:38 +00:00
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-18 07:06:25 +00:00
// https://square.github.io/leakcanary/recipes/#identifying-leaking-objects-and-labeling-objects
for (Class<?> clazz : new Class<?>[]{SimpleTask.class, TwoStateOwner.class, RunnableEx.class})
inspectors.add(new ObjectInspector() {
@Override
public void inspect(@NonNull ObjectReporter reporter) {
String className = clazz.getName();
reporter.whenInstanceOf(className, new Function2<ObjectReporter, HeapObject.HeapInstance, Unit>() {
@Override
public Unit invoke(ObjectReporter reporter, HeapObject.HeapInstance instance) {
HeapField hfName = instance.get(className, "name");
if (hfName != null) {
String label = hfName.getValue().readAsJavaString();
reporter.getLabels().add("name=" + label);
}
// Could be different class loader
if (className.equals(SimpleTask.class.getName())) {
HeapField hfStarted = instance.get(className, "started");
if (hfStarted != null) {
Long started = hfStarted.getValue().getAsLong();
if (started != null) {
String label = (started == 0 ? null : new Date(started).toString());
reporter.getLabels().add("started=" + label);
}
}
2022-04-24 08:37:47 +00:00
HeapField hfDestroyed = instance.get(className, "destroyed");
if (hfDestroyed != null) {
Boolean destroyed = hfDestroyed.getValue().getAsBoolean();
if (destroyed != null)
reporter.getLabels().add("destroyed=" + destroyed);
}
2022-04-18 10:52:20 +00:00
} else if (className.equals(TwoStateOwner.class.getName())) {
HeapField hfState = instance.get(className, "state");
if (hfState != null) {
String state = hfState.getValue().readAsJavaString();
reporter.getLabels().add("state=" + state);
}
HeapField hfOwned = instance.get(className, "owned");
if (hfOwned != null) {
Boolean owned = hfOwned.getValue().getAsBoolean();
reporter.getLabels().add("owned=" + owned);
}
2022-04-18 07:06:25 +00:00
}
return null;
2022-04-16 05:50:25 +00:00
}
2022-04-18 07:06:25 +00:00
});
}
});
2022-04-17 19:34:33 +00:00
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-23 20:46:59 +00:00
2022-04-18 06:48:57 +00:00
LeakCanary.INSTANCE.showLeakDisplayActivityLauncherIcon(true);
2022-04-13 06:17:38 +00:00
}
static void check() {
LeakCanary.INSTANCE.dumpHeap();
}
2022-04-17 19:34:33 +00:00
static void watch(@NonNull Object object, String reason) {
Log.i("Watching " + object.getClass() + " because " + reason);
AppWatcher.INSTANCE.getObjectWatcher().expectWeaklyReachable(object, reason);
2022-04-13 06:17:38 +00:00
}
2022-04-17 12:26:38 +00:00
static Intent getIntent() {
return LeakCanary.INSTANCE.newLeakDisplayActivityIntent();
}
2022-04-13 06:17:38 +00:00
}