forked from square/leakcanary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
guwen
committed
May 20, 2020
1 parent
f050322
commit e72cf61
Showing
10 changed files
with
277 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
leakcanary-object-watcher-android-androidx/consumer-proguard-rules.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# AndroidXFragmentDestroyWatcher is loaded via reflection | ||
-keep class leakcanary.internal.AndroidXFragmentDestroyWatcher { *; } | ||
-keep class leakcanary.internal.LiveAndroidXFragmentDestroyWatcher { *; } | ||
# ViewModelClearedWatcher reaches into ViewModelStore using reflection. | ||
-keep class androidx.lifecycle.ViewModelStore { *; } |
80 changes: 80 additions & 0 deletions
80
...-android-androidx/src/main/java/leakcanary/internal/LiveAndroidXFragmentDestroyWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2018 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package leakcanary.internal | ||
|
||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentActivity | ||
import androidx.fragment.app.FragmentManager | ||
import leakcanary.AppWatcher.Config | ||
import leakcanary.ObjectWatcher | ||
|
||
internal class LiveAndroidXFragmentDestroyWatcher( | ||
private val objectWatcher: ObjectWatcher, | ||
private val configProvider: () -> Config | ||
) : (Activity) -> Unit { | ||
|
||
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() { | ||
|
||
override fun onFragmentCreated( | ||
fm: FragmentManager, | ||
fragment: Fragment, | ||
savedInstanceState: Bundle? | ||
) { | ||
ViewModelClearedWatcher.install(fragment, objectWatcher, configProvider) | ||
} | ||
|
||
override fun onFragmentViewDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
val view = fragment.view | ||
if (view != null && configProvider().watchFragmentViews) { | ||
Log.d("LiveLeakCanary", "X onFragmentViewDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
view, "${fragment::class.java.name} received Fragment#onDestroyView() callback " + | ||
"(references to its views should be cleared to prevent leaks)" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFragmentDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
if (configProvider().watchFragments) { | ||
Log.d("LiveLeakCanary", "X onFragmentDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
fragment, "${fragment::class.java.name} received Fragment#onDestroy() callback" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun invoke(activity: Activity) { | ||
if (activity is FragmentActivity) { | ||
val supportFragmentManager = activity.supportFragmentManager | ||
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, true) | ||
ViewModelClearedWatcher.install(activity, objectWatcher, configProvider) | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
leakcanary-object-watcher-android-support-fragments/consumer-proguard-rules.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# AndroidSupportFragmentDestroyWatcher is loaded via reflection | ||
-keep class leakcanary.internal.AndroidSupportFragmentDestroyWatcher { *; } | ||
-keep class leakcanary.internal.LiveAndroidSupportFragmentDestroyWatcher { *; } |
70 changes: 70 additions & 0 deletions
70
...t-fragments/src/main/java/leakcanary/internal/LiveAndroidSupportFragmentDestroyWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2019 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package leakcanary.internal | ||
|
||
import android.app.Activity | ||
import android.support.v4.app.Fragment | ||
import android.support.v4.app.FragmentActivity | ||
import android.support.v4.app.FragmentManager | ||
import android.util.Log | ||
import leakcanary.AppWatcher.Config | ||
import leakcanary.ObjectWatcher | ||
|
||
internal class LiveAndroidSupportFragmentDestroyWatcher( | ||
private val objectWatcher: ObjectWatcher, | ||
private val configProvider: () -> Config | ||
) : (Activity) -> Unit { | ||
|
||
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() { | ||
|
||
override fun onFragmentViewDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
val view = fragment.view | ||
if (view != null && configProvider().watchFragmentViews) { | ||
Log.d("LiveLeakCanary", "Support onFragmentViewDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
view, "${fragment::class.java.name} received Fragment#onDestroyView() callback " + | ||
"(references to its views should be cleared to prevent leaks)" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFragmentDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
if (configProvider().watchFragments) { | ||
Log.d("LiveLeakCanary", "Support onFragmentViewDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
fragment, "${fragment::class.java.name} received Fragment#onDestroy() callback" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun invoke(activity: Activity) { | ||
if (activity is FragmentActivity) { | ||
val supportFragmentManager = activity.supportFragmentManager | ||
supportFragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, true) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ry-object-watcher-android/src/main/java/leakcanary/internal/LiveActivityDestroyWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package leakcanary.internal | ||
|
||
import android.app.Activity | ||
import android.app.Application | ||
import android.util.Log | ||
import leakcanary.AppWatcher | ||
import leakcanary.ObjectWatcher | ||
|
||
/** | ||
* | ||
* @ClassName: LiveActivityDestroyWatcher | ||
* @Description: 只检查live相关的类 | ||
* @CreateDate: 2020/5/20 14:14 | ||
* @Version: 1.0 | ||
*/ | ||
internal class LiveActivityDestroyWatcher private constructor( | ||
private val objectWatcher: ObjectWatcher, | ||
private val configProvider: () -> AppWatcher.Config | ||
) { | ||
|
||
private val lifecycleCallbacks = | ||
object : Application.ActivityLifecycleCallbacks by InternalAppWatcher.noOpDelegate() { | ||
override fun onActivityDestroyed(activity: Activity) { | ||
if (configProvider().watchActivities) { | ||
Log.d("LiveLeakCanary", "onActivityDestroyed activity.localClassName = ${activity::class.java.name}") | ||
if (activity::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
activity, "${activity::class.java.name} received Activity#onDestroy() callback" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
fun install( | ||
application: Application, | ||
objectWatcher: ObjectWatcher, | ||
configProvider: () -> AppWatcher.Config | ||
) { | ||
val activityDestroyWatcher = | ||
LiveActivityDestroyWatcher(objectWatcher, configProvider) | ||
application.registerActivityLifecycleCallbacks(activityDestroyWatcher.lifecycleCallbacks) | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...t-watcher-android/src/main/java/leakcanary/internal/LiveAndroidOFragmentDestroyWatcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (C) 2018 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package leakcanary.internal | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.app.Fragment | ||
import android.app.FragmentManager | ||
import android.util.Log | ||
import leakcanary.AppWatcher.Config | ||
import leakcanary.ObjectWatcher | ||
|
||
@SuppressLint("NewApi") | ||
internal class LiveAndroidOFragmentDestroyWatcher( | ||
private val objectWatcher: ObjectWatcher, | ||
private val configProvider: () -> Config | ||
) : (Activity) -> Unit { | ||
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() { | ||
|
||
override fun onFragmentViewDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
val view = fragment.view | ||
if (view != null && configProvider().watchFragmentViews) { | ||
Log.d("LiveLeakCanary", "O onFragmentViewDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
view, "${fragment::class.java.name} received Fragment#onDestroyView() callback " + | ||
"(references to its views should be cleared to prevent leaks)" | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun onFragmentDestroyed( | ||
fm: FragmentManager, | ||
fragment: Fragment | ||
) { | ||
if (configProvider().watchFragments) { | ||
Log.d("LiveLeakCanary", "O onFragmentDestroyed fragment.localClassName = ${fragment::class.java.name}") | ||
if (fragment::class.java.name.contains("live")) { | ||
objectWatcher.watch( | ||
fragment, "${fragment::class.java.name} received Fragment#onDestroy() callback" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun invoke(activity: Activity) { | ||
val fragmentManager = activity.fragmentManager | ||
fragmentManager.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks, true) | ||
} | ||
} |