Skip to content

Commit

Permalink
Don't let Monkeys delete leaks
Browse files Browse the repository at this point in the history
Fixes #2054
  • Loading branch information
pyricau committed Mar 26, 2021
1 parent 295f948 commit 3132dcc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package leakcanary.internal.activity.screen

import android.app.ActivityManager
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
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
Expand Down Expand Up @@ -56,11 +56,11 @@ internal class HeapAnalysisFailureScreen(
Please <a href="file_issue">click here</a> 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) {
"""
"""
<br><br>To help reproduce the issue, please share the
<a href="share_hprof">Heap Dump file</a> and upload it to the GitHub issue.
"""
} else "")
} else "")

val failure = Html.fromHtml(failureText) as SpannableStringBuilder

Expand All @@ -76,7 +76,7 @@ internal class HeapAnalysisFailureScreen(
shareHeapDump(heapAnalysis.heapDumpFile)
}
}
"try_again"-> {
"try_again" -> {
{
HeapAnalyzerService.runAnalysis(
context = context,
Expand All @@ -97,16 +97,18 @@ internal class HeapAnalysisFailureScreen(
findViewById<TextView>(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
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<ListView>(R.id.leak_canary_list)
listView.adapter =
SimpleListAdapter(
R.layout.leak_canary_simple_row, emptyList<Any>()
) { _, _ -> }
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<ListView>(R.id.leak_canary_list)
listView.adapter =
SimpleListAdapter(
R.layout.leak_canary_simple_row, emptyList<Any>()
) { _, _ -> }
}
}
}
}
.setNegativeButton(android.R.string.cancel, null)
.show()
true
}
.setNegativeButton(android.R.string.cancel, null)
.show()
true
}
}
}

findViewById<View>(R.id.leak_canary_import_heap_dump).setOnClickListener {
Expand Down

0 comments on commit 3132dcc

Please sign in to comment.