Skip to content

Commit

Permalink
Added leak canary
Browse files Browse the repository at this point in the history
  • Loading branch information
M66B committed Apr 12, 2022
1 parent 1ea9637 commit b02c31e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ FairEmail uses:
* [Lato font](https://fonts.google.com/specimen/Lato). By Łukasz Dziedzic. [Apache License 2.0](https://fonts.google.com/specimen/Lato#license).
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/).
* [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt).
6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ dependencies {
def rxjava2_version = "2.2.21"
def svg_version = "1.4"
def compress_version = "1.21"
def canary_version = "2.8.1"

// https://developer.android.com/jetpack/androidx/releases/startup
implementation "androidx.startup:startup-runtime:$startup_version"
Expand Down Expand Up @@ -585,4 +586,9 @@ dependencies {
// https://commons.apache.org/proper/commons-compress/
// https://mvnrepository.com/artifact/org.apache.commons/commons-compress
implementation "org.apache.commons:commons-compress:$compress_version"

// https://github.com/square/leakcanary
// https://square.github.io/leakcanary/getting_started/
// https://mvnrepository.com/artifact/com.squareup.leakcanary/leakcanary-android
implementation "com.squareup.leakcanary:leakcanary-android:$canary_version"
}
1 change: 1 addition & 0 deletions app/src/main/assets/ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ FairEmail uses:
* [Lato font](https://fonts.google.com/specimen/Lato). By Łukasz Dziedzic. [Apache License 2.0](https://fonts.google.com/specimen/Lato#license).
* [Caladea font](https://fonts.google.com/specimen/Caladea). By Andrés Torresi, Carolina Giovanolli. [Apache License 2.0](https://fonts.google.com/specimen/Caladea#license).
* [Apache Commons Compress](https://commons.apache.org/proper/commons-compress/). Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved. [Apache License 2.0](https://www.apache.org/licenses/).
* [LeakCanary](https://github.com/square/leakcanary). Copyright 2015 Square, Inc. [Apache License 2.0](https://github.com/square/leakcanary/blob/main/LICENSE.txt).
1 change: 1 addition & 0 deletions app/src/main/java/eu/faircode/email/ApplicationEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public void uncaughtException(@NonNull Thread thread, @NonNull Throwable ex) {
});

Log.setup(this);
Log.setupLeakCanary(crash_reports);

upgrade(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/

import android.app.Activity;
import android.app.ActivityManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand Down Expand Up @@ -664,6 +663,7 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
.putBoolean("crash_reports", checked)
.apply();
Log.setCrashReporting(checked);
Log.setupLeakCanary(checked);
}
});

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/eu/faircode/email/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
import javax.net.ssl.TrustManagerFactory;

import io.requery.android.database.CursorWindowAllocationException;
import leakcanary.LeakCanary;

public class Log {
private static Context ctx;
Expand Down Expand Up @@ -569,6 +570,18 @@ private boolean shouldNotify(Throwable ex) {
}
}

static void setupLeakCanary(boolean enabled) {
LeakCanary.Config config = LeakCanary.getConfig().newBuilder()
.dumpHeap(enabled && BuildConfig.DEBUG)
.build();
LeakCanary.setConfig(config);
LeakCanary.INSTANCE.showLeakDisplayActivityLauncherIcon(BuildConfig.DEBUG);
}

static void checkCanary() {
LeakCanary.INSTANCE.dumpHeap();
}

static void logExtras(Intent intent) {
if (intent != null)
logBundle(intent.getExtras());
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/eu/faircode/email/ServiceSynchronize.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,11 +1288,15 @@ private NotificationCompat.Builder getNotificationService(Integer accounts, Inte
PendingIntent piWhy = PendingIntentCompat.getActivity(
this, ActivityView.PI_WHY, why, PendingIntent.FLAG_UPDATE_CURRENT);

Intent dump = new Intent(this, ServiceUI.class).setAction("dump");
PendingIntent piDump = PendingIntentCompat.getService(
this, ServiceUI.PI_DUMP, dump, PendingIntent.FLAG_UPDATE_CURRENT);

// Build notification
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "service")
.setSmallIcon(R.drawable.baseline_compare_arrows_white_24)
.setContentIntent(piWhy)
.setContentIntent(BuildConfig.DEBUG ? piDump : piWhy)
.setAutoCancel(false)
.setShowWhen(false)
.setPriority(NotificationCompat.PRIORITY_MIN)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/eu/faircode/email/ServiceUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ServiceUI extends IntentService {
static final int PI_SNOOZE = 10;
static final int PI_IGNORED = 11;
static final int PI_DELETE = 12;
static final int PI_DUMP = 13;

public ServiceUI() {
this(ServiceUI.class.getName());
Expand Down Expand Up @@ -175,6 +176,10 @@ protected void onHandleIntent(@Nullable Intent intent) {
// ignore
break;

case "dump":
Log.checkCanary();
break;

default:
throw new IllegalArgumentException("Unknown UI action: " + parts[0]);
}
Expand Down

0 comments on commit b02c31e

Please sign in to comment.