Skip to content

Commit

Permalink
Code & kdoc cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler committed Jan 4, 2022
1 parent 5a992ab commit 042b2a6
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 149 deletions.
4 changes: 1 addition & 3 deletions docs/upgrading-to-leakcanary-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ RefWatcher refWatcher = LeakCanary.refWatcher(this)

```xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name="com.example.DebugExampleApplication">
<service android:name="com.example.LeakUploadService" />
</application>
Expand Down
17 changes: 6 additions & 11 deletions leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,12 @@ object LeakCanary {
val eventListeners: List<EventListener> = listOf(
LogcatEventListener,
ToastEventListener,
if (InternalLeakCanary.formFactor == TV) {
TvEventListener
} else {
NotificationEventListener
},
if (RemoteWorkManagerHeapAnalyzer.remoteLeakCanaryServiceInClasspath) {
RemoteWorkManagerHeapAnalyzer
} else if (WorkManagerHeapAnalyzer.workManagerInClasspath) {
WorkManagerHeapAnalyzer
} else {
BackgroundThreadHeapAnalyzer
if (InternalLeakCanary.formFactor == TV) TvEventListener else NotificationEventListener,
when {
RemoteWorkManagerHeapAnalyzer.remoteLeakCanaryServiceInClasspath ->
RemoteWorkManagerHeapAnalyzer
WorkManagerHeapAnalyzer.workManagerInClasspath -> WorkManagerHeapAnalyzer
else -> BackgroundThreadHeapAnalyzer
}
),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.squareup.leakcanary.core.R
import leakcanary.EventListener.Event
import leakcanary.EventListener.Event.DumpingHeap
import leakcanary.EventListener.Event.HeapAnalysisDone
import leakcanary.EventListener.Event.HeapAnalysisDone.HeapAnalysisFailed
import leakcanary.EventListener.Event.HeapAnalysisDone.HeapAnalysisSucceeded
import leakcanary.EventListener.Event.HeapAnalysisProgress
import leakcanary.EventListener.Event.HeapDumpFailed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package leakcanary.internal

import java.io.File
import java.io.IOException
import leakcanary.EventListener
import leakcanary.EventListener.Event.HeapAnalysisDone
import leakcanary.EventListener.Event.HeapAnalysisDone.HeapAnalysisFailed
import leakcanary.EventListener.Event.HeapAnalysisDone.HeapAnalysisSucceeded
Expand All @@ -12,9 +13,6 @@ import leakcanary.internal.activity.LeakActivity
import leakcanary.internal.activity.db.HeapAnalysisTable
import leakcanary.internal.activity.db.LeakTable
import leakcanary.internal.activity.db.LeaksDbHelper
import leakcanary.internal.activity.screen.HeapAnalysisFailureScreen
import leakcanary.internal.activity.screen.HeapDumpScreen
import leakcanary.internal.activity.screen.HeapDumpsScreen
import shark.ConstantMemoryMetricsDualSourceProvider
import shark.HeapAnalysis
import shark.HeapAnalysisException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,26 @@ import android.os.StrictMode
import android.provider.OpenableColumns
import android.text.TextUtils
import android.webkit.MimeTypeMap
import org.xmlpull.v1.XmlPullParser.END_DOCUMENT
import org.xmlpull.v1.XmlPullParser.START_TAG
import org.xmlpull.v1.XmlPullParserException
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.util.HashMap
import org.xmlpull.v1.XmlPullParser.END_DOCUMENT
import org.xmlpull.v1.XmlPullParser.START_TAG
import org.xmlpull.v1.XmlPullParserException

/**
* Copy of androidx.core.content.FileProvider, converted to Kotlin.
*/
internal class LeakCanaryFileProvider : ContentProvider() {

private var mStrategy: PathStrategy? = null
private lateinit var mStrategy: PathStrategy

/**
* The default FileProvider implementation does not need to be initialized. If you want to
* override this method, you must provide your own subclass of FileProvider.
*/
override fun onCreate(): Boolean {
return true
}
override fun onCreate(): Boolean = true

/**
* After the FileProvider is instantiated, this method is called to provide the system with
Expand All @@ -75,7 +73,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
throw SecurityException("Provider must grant uri permissions")
}

mStrategy = getPathStrategy(context, info.authority)
mStrategy = getPathStrategy(context, info.authority)!!
}

/**
Expand All @@ -91,7 +89,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
* [ ContentProvider.query()][ContentProvider.query].
*
* @param uri A content URI returned by [.getUriForFile].
* @param projection The list of columns to put into the [Cursor]. If null all columns are
* @param projectionArg The list of columns to put into the [Cursor]. If null all columns are
* included.
* @param selection Selection criteria to apply. If null then all data that matches the content
* URI is returned.
Expand All @@ -113,7 +111,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
): Cursor {
val projection = projectionArg ?: COLUMNS
// ContentProvider has already checked granted permissions
val file = mStrategy!!.getFileForUri(uri)
val file = mStrategy.getFileForUri(uri)

var cols = arrayOfNulls<String>(projection.size)
var values = arrayOfNulls<Any>(projection.size)
Expand Down Expand Up @@ -147,7 +145,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
*/
override fun getType(uri: Uri): String {
// ContentProvider has already checked granted permissions
val file = mStrategy!!.getFileForUri(uri)
val file = mStrategy.getFileForUri(uri)

val lastDot = file.name.lastIndexOf('.')
if (lastDot >= 0) {
Expand Down Expand Up @@ -203,7 +201,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
selectionArgs: Array<String>?
): Int {
// ContentProvider has already checked granted permissions
val file = mStrategy!!.getFileForUri(uri)
val file = mStrategy.getFileForUri(uri)
return if (file.delete()) 1 else 0
}

Expand All @@ -227,7 +225,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
mode: String
): ParcelFileDescriptor? {
// ContentProvider has already checked granted permissions
val file = mStrategy!!.getFileForUri(uri)
val file = mStrategy.getFileForUri(uri)
val fileMode = modeToMode(mode)
return ParcelFileDescriptor.open(file, fileMode)
}
Expand Down Expand Up @@ -324,11 +322,8 @@ internal class LeakCanaryFileProvider : ContentProvider() {

// Start at first char of path under root
val rootPath = mostSpecific.value.path
path = if (rootPath.endsWith("/")) {
path.substring(rootPath.length)
} else {
path.substring(rootPath.length + 1)
}
val startIndex = if (rootPath.endsWith("/")) rootPath.length else rootPath.length + 1
path = path.substring(startIndex)

// Encode the tag and path separately
path = Uri.encode(mostSpecific.key) + '/'.toString() + Uri.encode(path, "/")
Expand All @@ -340,16 +335,16 @@ internal class LeakCanaryFileProvider : ContentProvider() {
}

override fun getFileForUri(uri: Uri): File {
var path = uri.encodedPath
var path = uri.encodedPath!!

val splitIndex = path!!.indexOf('/', 1)
val splitIndex = path.indexOf('/', 1)
val tag = Uri.decode(path.substring(1, splitIndex))
path = Uri.decode(path.substring(splitIndex + 1))

val root =
mRoots[tag] ?: throw IllegalArgumentException("Unable to find configured root for $uri")
val root = mRoots[tag]
?: throw IllegalArgumentException("Unable to find configured root for $uri")

var file = File(root, path!!)
var file = File(root, path)
try {
file = file.canonicalFile
} catch (e: IOException) {
Expand Down Expand Up @@ -473,7 +468,7 @@ internal class LeakCanaryFileProvider : ContentProvider() {
"Missing $META_DATA_FILE_PROVIDER_PATHS meta-data"
)

var type = 0
var type: Int
while (run {
type = resourceParser.next()
(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import androidx.work.WorkerParameters
import androidx.work.impl.utils.futures.SettableFuture
import androidx.work.multiprocess.RemoteListenableWorker
import com.google.common.util.concurrent.ListenableFuture
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.lang.RuntimeException
import leakcanary.BackgroundThreadHeapAnalyzer.heapAnalyzerThreadHandler
import leakcanary.EventListener.Event.HeapAnalysisDone
import leakcanary.EventListener.Event.HeapDump
import leakcanary.internal.HeapAnalyzerWorker.Companion.asEvent
import shark.SharkLog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package leakcanary.internal.activity

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.AsyncTask
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LeakCanaryConfigTest {

private fun configBuilderFunctions() = LeakCanary.Config.Builder::class.memberFunctions
.map { it.name }
.subtract(listOf("build", "equals", "hashCode", "toString"))
.subtract(setOf("build", "equals", "hashCode", "toString"))

private fun configProperties() = LeakCanary.Config::class.memberProperties
.map { it.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DetectLeaksAfterTestSuccessTest {
}

@get:Rule
val rule = RuleChain.outerRule(CheckAssertNoLeaksInvoked).around(DetectLeaksAfterTestSuccess())
val rule: RuleChain = RuleChain.outerRule(CheckAssertNoLeaksInvoked).around(DetectLeaksAfterTestSuccess())

@Test fun emptyTest() {
// This test triggers the rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import androidx.lifecycle.ViewModelStoreOwner
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import leakcanary.internal.friendly.noOpDelegate
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
import kotlin.reflect.KClass
import leakcanary.internal.friendly.noOpDelegate

interface HasActivityTestRule<T : Activity> {
val activityRule: ActivityTestRule<T>

val activity
val activity: T
get() = activityRule.activity!!
}

Expand Down Expand Up @@ -164,4 +164,4 @@ fun FragmentActivity.removeFragmentNow(fragment: Fragment) {
.beginTransaction()
.remove(fragment)
.commitNow()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import shark.AndroidObjectInspectors
import shark.AndroidReferenceMatchers
import shark.HeapAnalyzer
import shark.KeyedWeakReferenceFinder
import shark.OnAnalysisProgressListener
import shark.OnAnalysisProgressListener.Step
import shark.SharkLog
import java.io.File
import java.io.FileOutputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import shark.FilteringLeakingObjectFinder
import shark.LeakingObjectFinder
import shark.MetadataExtractor
import shark.ObjectInspector
import shark.IgnoredReferenceMatcher
import shark.ReferenceMatcher
import shark.LibraryLeakReferenceMatcher

data class HeapAnalysisConfig(

Expand Down Expand Up @@ -70,4 +72,4 @@ data class HeapAnalysisConfig(
* dump exists on disk with potential PII.
*/
val stripHeapDump: Boolean = false
)
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2015 Square, Inc.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,7 +13,4 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.leakcanary.test">
</manifest>
<manifest package="com.leakcanary.test" />
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class AppWatcherTest {
member.annotations.none { it is Deprecated }
}
.map { it.name }
.subtract(listOf("build", "equals", "hashCode", "toString"))
.subtract(setOf("build", "equals", "hashCode", "toString"))

private fun configProperties() = AppWatcher.Config::class.memberProperties
.filter { member ->
// Ignore deprecated fields, we don't need builders for those
member.annotations.none { it is Deprecated }
}
.map { it.name }
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.squareup.leakcanary.fragments.android_support" />
<manifest package="com.squareup.leakcanary.fragments.android_support" />
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,26 @@ import leakcanary.AppWatcher
internal class MainProcessAppWatcherInstaller : ContentProvider() {

override fun onCreate(): Boolean {
val application = context!!.applicationContext as Application
val application = requireContext().applicationContext as Application
AppWatcher.manualInstall(application)
return true
}

override fun query(
uri: Uri,
strings: Array<String>?,
s: String?,
strings1: Array<String>?,
s1: String?
): Cursor? {
return null
}
projectionArg: Array<String>?,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor? = null

override fun getType(uri: Uri): String? {
return null
}
override fun getType(uri: Uri): String? = null

override fun insert(
uri: Uri,
contentValues: ContentValues?
): Uri? {
return null
}
override fun insert(uri: Uri, contentValues: ContentValues?): Uri? = null

override fun delete(
uri: Uri,
s: String?,
strings: Array<String>?
): Int {
return 0
}
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0

override fun update(
uri: Uri,
contentValues: ContentValues?,
s: String?,
strings: Array<String>?
): Int {
return 0
}
uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?
): Int = 0
}
4 changes: 1 addition & 3 deletions plumber-android-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.squareup.leakcanary.plumber.core">
</manifest>
<manifest package="com.squareup.leakcanary.plumber.core" />
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import java.lang.reflect.Modifier
import java.lang.reflect.Proxy
import java.util.EnumSet
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.SECONDS

/**
Expand Down
Loading

0 comments on commit 042b2a6

Please sign in to comment.