From 3132dcc6ea4b3ce760833219f861c7cb4b686df0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Thu, 25 Mar 2021 21:30:08 -0700 Subject: [PATCH] Don't let Monkeys delete leaks Fixes #2054 --- .../screen/HeapAnalysisFailureScreen.kt | 26 +++++------ .../activity/screen/HeapDumpScreen.kt | 21 ++++----- .../activity/screen/HeapDumpsScreen.kt | 43 ++++++++++--------- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapAnalysisFailureScreen.kt b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapAnalysisFailureScreen.kt index a1d11f0a4d..3f77274fb2 100644 --- a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapAnalysisFailureScreen.kt +++ b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapAnalysisFailureScreen.kt @@ -1,5 +1,6 @@ package leakcanary.internal.activity.screen +import android.app.ActivityManager import android.text.Html import android.text.SpannableStringBuilder import android.text.method.LinkMovementMethod @@ -7,7 +8,6 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView import com.squareup.leakcanary.core.R -import leakcanary.LeakCanary import leakcanary.internal.HeapAnalyzerService import leakcanary.internal.activity.db.HeapAnalysisTable import leakcanary.internal.activity.db.executeOnDb @@ -56,11 +56,11 @@ internal class HeapAnalysisFailureScreen( Please click here to file a bug report. The stacktrace details will be copied into the clipboard and you just need to paste into the GitHub issue description.""" + (if (heapDumpFileExist) { - """ + """

To help reproduce the issue, please share the Heap Dump file and upload it to the GitHub issue. """ - } else "") + } else "") val failure = Html.fromHtml(failureText) as SpannableStringBuilder @@ -76,7 +76,7 @@ internal class HeapAnalysisFailureScreen( shareHeapDump(heapAnalysis.heapDumpFile) } } - "try_again"-> { + "try_again" -> { { HeapAnalyzerService.runAnalysis( context = context, @@ -97,16 +97,18 @@ internal class HeapAnalysisFailureScreen( findViewById(R.id.leak_canary_stacktrace).text = heapAnalysis.exception.toString() onCreateOptionsMenu { menu -> - menu.add(R.string.leak_canary_delete) - .setOnMenuItemClickListener { - executeOnDb { - HeapAnalysisTable.delete(db, analysisId, heapAnalysis.heapDumpFile) - updateUi { - goBack() + if (!ActivityManager.isUserAMonkey()) { + menu.add(R.string.leak_canary_delete) + .setOnMenuItemClickListener { + executeOnDb { + HeapAnalysisTable.delete(db, analysisId, heapAnalysis.heapDumpFile) + updateUi { + goBack() + } } + true } - true - } + } } } } \ No newline at end of file diff --git a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpScreen.kt b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpScreen.kt index 1e47249459..c3fab664f1 100644 --- a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpScreen.kt +++ b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpScreen.kt @@ -2,12 +2,11 @@ package leakcanary.internal.activity.screen import android.R.drawable import android.R.string -import android.app.AlertDialog +import android.app.ActivityManager import android.app.AlertDialog.Builder import android.text.Html import android.text.SpannableStringBuilder import android.text.method.LinkMovementMethod -import android.util.Log import android.view.View import android.view.View.GONE import android.view.View.VISIBLE @@ -68,16 +67,18 @@ internal class HeapDumpScreen( activity.title = TimeFormatter.formatTimestamp(context, heapAnalysis.createdAtTimeMillis) onCreateOptionsMenu { menu -> - menu.add(R.string.leak_canary_delete) - .setOnMenuItemClickListener { - executeOnDb { - HeapAnalysisTable.delete(db, analysisId, heapAnalysis.heapDumpFile) - updateUi { - goBack() + if (!ActivityManager.isUserAMonkey()) { + menu.add(R.string.leak_canary_delete) + .setOnMenuItemClickListener { + executeOnDb { + HeapAnalysisTable.delete(db, analysisId, heapAnalysis.heapDumpFile) + updateUi { + goBack() + } } + true } - true - } + } if (heapDumpFileExist) { menu.add(R.string.leak_canary_options_menu_render_heap_dump) .setOnMenuItemClickListener { diff --git a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpsScreen.kt b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpsScreen.kt index 22ba564594..bedd41633b 100644 --- a/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpsScreen.kt +++ b/leakcanary-android-core/src/main/java/leakcanary/internal/activity/screen/HeapDumpsScreen.kt @@ -1,5 +1,6 @@ package leakcanary.internal.activity.screen +import android.app.ActivityManager import android.app.AlertDialog import android.view.View import android.view.ViewGroup @@ -32,28 +33,30 @@ internal class HeapDumpsScreen : Screen() { onScreenExiting { unsubscribeRefresh() } onCreateOptionsMenu { menu -> - menu.add(R.string.leak_canary_delete_all) - .setOnMenuItemClickListener { - AlertDialog.Builder(context) - .setIcon(android.R.drawable.ic_dialog_alert) - .setTitle(R.string.leak_canary_delete_all) - .setMessage(R.string.leak_canary_delete_all_leaks_title) - .setPositiveButton(android.R.string.ok) { _, _ -> - executeOnDb { - HeapAnalysisTable.deleteAll(db) - updateUi { - val listView = findViewById(R.id.leak_canary_list) - listView.adapter = - SimpleListAdapter( - R.layout.leak_canary_simple_row, emptyList() - ) { _, _ -> } + if (!ActivityManager.isUserAMonkey()) { + menu.add(R.string.leak_canary_delete_all) + .setOnMenuItemClickListener { + AlertDialog.Builder(context) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(R.string.leak_canary_delete_all) + .setMessage(R.string.leak_canary_delete_all_leaks_title) + .setPositiveButton(android.R.string.ok) { _, _ -> + executeOnDb { + HeapAnalysisTable.deleteAll(db) + updateUi { + val listView = findViewById(R.id.leak_canary_list) + listView.adapter = + SimpleListAdapter( + R.layout.leak_canary_simple_row, emptyList() + ) { _, _ -> } + } } } - } - .setNegativeButton(android.R.string.cancel, null) - .show() - true - } + .setNegativeButton(android.R.string.cancel, null) + .show() + true + } + } } findViewById(R.id.leak_canary_import_heap_dump).setOnClickListener {