Skip to content

Commit

Permalink
Remove dependency to Android X (#1463)
Browse files Browse the repository at this point in the history
The Android X dependency is causing too many issues for consumers. It doesn't only mandate migrating from the support lib, but then there's resolution of version conflicts to take care of. LeakCanary should be 0 set up.

Fixes #1462
  • Loading branch information
pyricau committed Jul 14, 2019
1 parent 9e7a133 commit 6d1bd8a
Show file tree
Hide file tree
Showing 28 changed files with 684 additions and 103 deletions.
1 change: 0 additions & 1 deletion leakcanary-analyzer/build.gradle
Expand Up @@ -8,7 +8,6 @@ dependencies {
api project(':leakcanary-watcher')
api project(':leakcanary-haha')

implementation deps.androidx.annotation
implementation deps.kotlin.stdlib

testImplementation deps.assertj_core
Expand Down
3 changes: 1 addition & 2 deletions leakcanary-android-core/build.gradle
Expand Up @@ -4,9 +4,8 @@ apply plugin: 'kotlin-android'
dependencies {
api project(':leakcanary-analyzer')
api project(':leakcanary-leaksentry')
api project(':leakcanary-fragments-androidx')

implementation deps.androidx.annotation
implementation deps.androidx.core
implementation deps.kotlin.stdlib

testImplementation deps.junit
Expand Down
Expand Up @@ -16,16 +16,14 @@
package leakcanary.internal

import android.content.Context
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.format.DateUtils
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.text.HtmlCompat
import com.squareup.leakcanary.core.R
import leakcanary.LeakNodeStatus.LEAKING
import leakcanary.LeakNodeStatus.NOT_LEAKING
Expand All @@ -50,7 +48,10 @@ import leakcanary.internal.MoreDetailsView.Details.NONE
import leakcanary.internal.MoreDetailsView.Details.OPENED
import leakcanary.internal.activity.db.LeakingInstanceTable.InstanceProjection
import leakcanary.internal.navigation.inflate
import android.os.Build.VERSION.SDK_INT
import leakcanary.internal.navigation.getColorCompat

@Suppress("DEPRECATION")
internal class DisplayLeakAdapter constructor(
context: Context,
private val leakTrace: LeakTrace,
Expand Down Expand Up @@ -139,27 +140,23 @@ internal class DisplayLeakAdapter constructor(
val resources = view.resources
if (isFirstConnectorRow(position)) {
titleView.text = if (isLeakGroup) {
HtmlCompat.fromHtml(
Html.fromHtml(
"""
<font color='$helpColorHexString'>
<b>Known likely causes of leak group</b>
</font>
""",
HtmlCompat.FROM_HTML_MODE_LEGACY
"""
)
} else {
HtmlCompat.fromHtml(
Html.fromHtml(
"""
<font color='$helpColorHexString'>
<b>${resources.getString(R.string.leak_canary_help_title)}</b>
</font>
""",
HtmlCompat.FROM_HTML_MODE_LEGACY
"""
)
}
} else {
val isLast = position == (TOP_ROW_COUNT + leakTrace.elements.size) - 1

val elementIndex = elementIndex(position)
val element = leakTrace.elements[elementIndex]

Expand All @@ -172,7 +169,7 @@ internal class DisplayLeakAdapter constructor(
titleView.text = htmlTitle

if (opened[position]) {
val htmlDetail = htmlDetails(isLast, element)
val htmlDetail = htmlDetails(element)
detailView.text = htmlDetail
}
}
Expand Down Expand Up @@ -249,19 +246,15 @@ internal class DisplayLeakAdapter constructor(
if (exclusion != null) {
htmlString += " (excluded)"
}
val builder =
HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_LEGACY) as SpannableStringBuilder
val builder = Html.fromHtml(htmlString) as SpannableStringBuilder
if (maybeLeakCause) {
SquigglySpan.replaceUnderlineSpans(builder, context)
}

return builder
}

private fun htmlDetails(
isLeakingInstance: Boolean,
element: LeakTraceElement
): Spanned {
private fun htmlDetails(element: LeakTraceElement): Spanned {
var htmlString = ""
val exclusion = element.exclusion
if (exclusion != null) {
Expand All @@ -271,7 +264,7 @@ internal class DisplayLeakAdapter constructor(
htmlString += " because <font color='#f3cf83'>" + exclusion.reason + "</font>"
}
}
return HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_LEGACY)
return Html.fromHtml(htmlString)
}

private fun getConnectorType(position: Int): Type {
Expand Down Expand Up @@ -353,8 +346,8 @@ internal class DisplayLeakAdapter constructor(
private const val TOP_ROW_COUNT = 2

// https://stackoverflow.com/a/6540378/703646
private fun hexStringColor(context: Context, @ColorRes colorResId: Int): String {
return String.format("#%06X", 0xFFFFFF and ContextCompat.getColor(context, colorResId))
private fun hexStringColor(context: Context, colorResId: Int): String {
return String.format("#%06X", 0xFFFFFF and context.getColorCompat(colorResId))
}
}
}
Expand Up @@ -26,7 +26,6 @@ import android.graphics.PorterDuff.Mode.CLEAR
import android.graphics.PorterDuffXfermode
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.squareup.leakcanary.core.R
import leakcanary.internal.DisplayLeakConnectorView.Type.END
import leakcanary.internal.DisplayLeakConnectorView.Type.END_FIRST_UNREACHABLE
Expand All @@ -39,6 +38,7 @@ import leakcanary.internal.DisplayLeakConnectorView.Type.NODE_UNKNOWN
import leakcanary.internal.DisplayLeakConnectorView.Type.NODE_UNREACHABLE
import leakcanary.internal.DisplayLeakConnectorView.Type.START
import leakcanary.internal.DisplayLeakConnectorView.Type.START_LAST_REACHABLE
import leakcanary.internal.navigation.getColorCompat

internal class DisplayLeakConnectorView(
context: Context,
Expand Down Expand Up @@ -81,18 +81,18 @@ internal class DisplayLeakConnectorView(
.toFloat()

classNamePaint = Paint(Paint.ANTI_ALIAS_FLAG)
classNamePaint.color = ContextCompat.getColor(context, R.color.leak_canary_class_name)
classNamePaint.color = context.getColorCompat(R.color.leak_canary_class_name)
classNamePaint.strokeWidth = strokeSize

leakGroupRootPaint = Paint(Paint.ANTI_ALIAS_FLAG)
leakGroupRootPaint.color = ContextCompat.getColor(context, R.color.leak_canary_class_name)
leakGroupRootPaint.color = context.getColorCompat(R.color.leak_canary_class_name)
leakGroupRootPaint.strokeWidth = strokeSize
val pathLines = resources.getDimensionPixelSize(R.dimen.leak_canary_connector_leak_dash_line)
.toFloat()
leakGroupRootPaint.pathEffect = DashPathEffect(floatArrayOf(pathLines, pathLines), 0f)

leakPaint = Paint(Paint.ANTI_ALIAS_FLAG)
leakPaint.color = ContextCompat.getColor(context, R.color.leak_canary_leak)
leakPaint.color = context.getColorCompat(R.color.leak_canary_leak)
leakPaint.style = Paint.Style.STROKE
leakPaint.strokeWidth = strokeSize

Expand All @@ -105,7 +105,7 @@ internal class DisplayLeakConnectorView(
clearPaint.xfermode = CLEAR_XFER_MODE

referencePaint = Paint(Paint.ANTI_ALIAS_FLAG)
referencePaint.color = ContextCompat.getColor(context, R.color.leak_canary_reference)
referencePaint.color = context.getColorCompat(R.color.leak_canary_reference)
referencePaint.strokeWidth = strokeSize
}

Expand Down
Expand Up @@ -17,8 +17,8 @@ package leakcanary.internal

import android.content.Context
import android.content.Intent
import android.os.Build.VERSION.SDK_INT
import android.os.Process
import androidx.core.content.ContextCompat
import com.squareup.leakcanary.core.R
import leakcanary.AnalyzerProgressListener
import leakcanary.AndroidKnownReference
Expand Down Expand Up @@ -88,7 +88,19 @@ internal class HeapAnalyzerService : ForegroundService(
) {
val intent = Intent(context, HeapAnalyzerService::class.java)
intent.putExtra(HEAPDUMP_FILE_EXTRA, heapDumpFile)
ContextCompat.startForegroundService(context, intent)
startForegroundService(context, intent)
}

fun startForegroundService(
context: Context,
intent: Intent
) {
if (SDK_INT >= 26) {
context.startForegroundService(intent)
} else {
// Pre-O behavior.
context.startService(intent)
}
}
}
}
Expand Up @@ -125,6 +125,7 @@ internal class HeapDumpTrigger(
val retainedReferenceCount = refWatcher.retainedInstanceCount
if (retainedReferenceCount == 0) {
CanaryLog.d("No retained instances after GC")
@Suppress("DEPRECATION")
val builder = Notification.Builder(application)
.setContentTitle(
application.getString(R.string.leak_canary_notification_no_retained_instance_title)
Expand Down Expand Up @@ -279,6 +280,7 @@ internal class HeapDumpTrigger(
if (!Notifications.canShowNotification) {
return
}
@Suppress("DEPRECATION")
val builder = Notification.Builder(application)
.setContentTitle(
application.getString(R.string.leak_canary_notification_retained_title, instanceCount)
Expand Down

0 comments on commit 6d1bd8a

Please sign in to comment.