Remove stetho unused types
Summary: ^ Reviewed By: LukeDefeo Differential Revision: D39572018 fbshipit-source-id: 437b245b7eee06392cda97eb56b4f4d0a5df23fc
This commit is contained in:
committed by
Facebook GitHub Bot
parent
86364cbd40
commit
3a4286b2ba
@@ -1,15 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.app.Dialog
|
|
||||||
|
|
||||||
interface DialogFragmentAccessor<DIALOG_FRAGMENT, FRAGMENT, FRAGMENT_MANAGER> :
|
|
||||||
FragmentAccessor<FRAGMENT, FRAGMENT_MANAGER> {
|
|
||||||
fun getDialog(dialogFragment: DIALOG_FRAGMENT): Dialog?
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.content.res.Resources
|
|
||||||
import android.view.View
|
|
||||||
|
|
||||||
interface FragmentAccessor<FRAGMENT, FRAGMENT_MANAGER> {
|
|
||||||
fun getFragmentManager(fragment: FRAGMENT): FRAGMENT_MANAGER?
|
|
||||||
fun getResources(fragment: FRAGMENT): Resources?
|
|
||||||
fun getId(fragment: FRAGMENT): Int
|
|
||||||
fun getTag(fragment: FRAGMENT): String?
|
|
||||||
fun getView(fragment: FRAGMENT): View?
|
|
||||||
fun getChildFragmentManager(fragment: FRAGMENT): FRAGMENT_MANAGER?
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val NO_ID = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
|
|
||||||
interface FragmentActivityAccessor<Activity, FRAGMENT_MANAGER> {
|
|
||||||
fun getFragmentManager(activity: Activity): FRAGMENT_MANAGER?
|
|
||||||
}
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.view.View
|
|
||||||
import com.facebook.flipper.plugins.uidebugger.stetho.ReflectionUtil.getFieldValue
|
|
||||||
import com.facebook.flipper.plugins.uidebugger.stetho.ReflectionUtil.tryGetClassForName
|
|
||||||
import java.lang.reflect.Field
|
|
||||||
import javax.annotation.concurrent.NotThreadSafe
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compatibility abstraction which allows us to generalize access to both the support library's
|
|
||||||
* fragments and the built-in framework version. Note: both versions can be live at the same time in
|
|
||||||
* a single application and even on a single object instance.
|
|
||||||
*
|
|
||||||
* Type safety is enforced via generics internal to the implementation but treated as opaque from
|
|
||||||
* the outside.
|
|
||||||
*
|
|
||||||
* @param <FRAGMENT>
|
|
||||||
* @param <DIALOG_FRAGMENT>
|
|
||||||
* @param <FRAGMENT_MANAGER>
|
|
||||||
* @param <FRAGMENT_ACTIVITY>
|
|
||||||
*/
|
|
||||||
@NotThreadSafe
|
|
||||||
abstract class FragmentCompat<
|
|
||||||
FRAGMENT, DIALOG_FRAGMENT, FRAGMENT_MANAGER, FRAGMENT_ACTIVITY : Activity> {
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
var frameworkInstance: FragmentCompat<*, *, *, *>? = null
|
|
||||||
get() {
|
|
||||||
if (field == null) {
|
|
||||||
field = FragmentCompatFramework()
|
|
||||||
}
|
|
||||||
return field
|
|
||||||
}
|
|
||||||
|
|
||||||
var supportInstance: FragmentCompat<*, *, *, *>? = null
|
|
||||||
get() {
|
|
||||||
if (field == null && hasSupportFragment) {
|
|
||||||
field = FragmentCompatSupportLib()
|
|
||||||
}
|
|
||||||
return field
|
|
||||||
}
|
|
||||||
|
|
||||||
private var hasSupportFragment = false
|
|
||||||
init {
|
|
||||||
hasSupportFragment = tryGetClassForName("androidx.fragment.app.Fragment") != null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isDialogFragment(fragment: Any): Boolean {
|
|
||||||
val supportLib: FragmentCompat<*, *, *, *>? = supportInstance
|
|
||||||
if (supportLib != null && supportLib.dialogFragmentClass?.isInstance(fragment) == true) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
val framework: FragmentCompat<*, *, *, *>? = frameworkInstance
|
|
||||||
return framework != null && framework.dialogFragmentClass?.isInstance(fragment) == true
|
|
||||||
}
|
|
||||||
|
|
||||||
fun findFragmentForView(view: View): Any? {
|
|
||||||
val activity = ViewUtil.tryGetActivity(view) ?: return null
|
|
||||||
return findFragmentForViewInActivity(activity, view)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun findFragmentForViewInActivity(activity: Activity, view: View): Any? {
|
|
||||||
val supportLib: FragmentCompat<*, *, *, *>? = supportInstance
|
|
||||||
|
|
||||||
// Try the support library version if it is present and the activity is FragmentActivity.
|
|
||||||
if (supportLib != null && supportLib.fragmentActivityClass?.isInstance(activity) == true) {
|
|
||||||
val fragment = supportLib.findFragmentForViewInActivity(activity, view)
|
|
||||||
if (fragment != null) {
|
|
||||||
return fragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try the actual Android runtime version if we are on a sufficiently high API level for it to
|
|
||||||
// exist. Note that technically we can have both the support library and the framework
|
|
||||||
// version in the same object instance due to FragmentActivity extending Activity (which has
|
|
||||||
// fragment support in the system).
|
|
||||||
val framework: FragmentCompat<*, *, *, *>? = frameworkInstance
|
|
||||||
if (framework != null) {
|
|
||||||
val fragment = framework.findFragmentForViewInActivity(activity, view)
|
|
||||||
if (fragment != null) {
|
|
||||||
return fragment
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract val fragmentClass: Class<FRAGMENT>?
|
|
||||||
abstract val dialogFragmentClass: Class<DIALOG_FRAGMENT>?
|
|
||||||
abstract val fragmentActivityClass: Class<FRAGMENT_ACTIVITY>?
|
|
||||||
|
|
||||||
abstract fun forFragment(): FragmentAccessor<FRAGMENT, FRAGMENT_MANAGER>?
|
|
||||||
abstract fun forDialogFragment():
|
|
||||||
DialogFragmentAccessor<DIALOG_FRAGMENT, FRAGMENT, FRAGMENT_MANAGER>?
|
|
||||||
abstract fun forFragmentManager(): FragmentManagerAccessor<FRAGMENT_MANAGER, FRAGMENT>?
|
|
||||||
abstract fun forFragmentActivity(): FragmentActivityAccessor<FRAGMENT_ACTIVITY, FRAGMENT_MANAGER>?
|
|
||||||
|
|
||||||
abstract fun getDialogFragments(activity: Activity): List<Any>
|
|
||||||
abstract fun findFragmentForViewInActivity(activity: Activity, view: View): Any?
|
|
||||||
abstract fun findFragmentForViewInFragment(fragment: Any, view: View): Any?
|
|
||||||
|
|
||||||
class FragmentManagerAccessorViaReflection<FRAGMENT_MANAGER, FRAGMENT> :
|
|
||||||
FragmentManagerAccessor<FRAGMENT_MANAGER, FRAGMENT> {
|
|
||||||
private var fieldMAdded: Field? = null
|
|
||||||
|
|
||||||
override fun getAddedFragments(fragmentManager: FRAGMENT_MANAGER): List<FRAGMENT> {
|
|
||||||
|
|
||||||
// This field is actually sitting on FragmentManagerImpl, which derives from FragmentManager
|
|
||||||
if (fieldMAdded == null) {
|
|
||||||
fragmentManager?.let { manager ->
|
|
||||||
val field = manager::class.java.getDeclaredField("mAdded")
|
|
||||||
field.isAccessible = true
|
|
||||||
fieldMAdded = field
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldMAdded?.let { field ->
|
|
||||||
return getFieldValue(field, fragmentManager) as List<FRAGMENT>
|
|
||||||
}
|
|
||||||
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,220 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.annotation.TargetApi
|
|
||||||
import android.app.*
|
|
||||||
import android.app.Dialog
|
|
||||||
import android.app.DialogFragment
|
|
||||||
import android.app.Fragment
|
|
||||||
import android.app.FragmentManager
|
|
||||||
import android.content.res.Resources
|
|
||||||
import android.os.Build
|
|
||||||
import android.view.View
|
|
||||||
|
|
||||||
class FragmentCompatFramework :
|
|
||||||
FragmentCompat<Fragment, DialogFragment, FragmentManager, Activity>() {
|
|
||||||
companion object {
|
|
||||||
private var fragmentAccessor: FragmentAccessorFrameworkHoneycomb? = null
|
|
||||||
private var dialogFragmentAccessor: DialogFragmentAccessorFramework? = null
|
|
||||||
private val fragmentManagerAccessor =
|
|
||||||
FragmentManagerAccessorViaReflection<FragmentManager, Fragment>()
|
|
||||||
private val fragmentActivityAccessor = FragmentActivityAccessorFramework()
|
|
||||||
|
|
||||||
init {
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
|
||||||
fragmentAccessor = FragmentAccessorFrameworkJellyBean()
|
|
||||||
} else {
|
|
||||||
fragmentAccessor = FragmentAccessorFrameworkHoneycomb()
|
|
||||||
}
|
|
||||||
fragmentAccessor?.let { accessor ->
|
|
||||||
dialogFragmentAccessor = DialogFragmentAccessorFramework(accessor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override val fragmentClass: Class<Fragment>
|
|
||||||
get() = Fragment::class.java
|
|
||||||
|
|
||||||
override val dialogFragmentClass: Class<DialogFragment>
|
|
||||||
get() = DialogFragment::class.java
|
|
||||||
|
|
||||||
override val fragmentActivityClass: Class<Activity>
|
|
||||||
get() = Activity::class.java
|
|
||||||
|
|
||||||
override fun forFragment(): FragmentAccessorFrameworkHoneycomb? {
|
|
||||||
return fragmentAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forDialogFragment(): DialogFragmentAccessorFramework? {
|
|
||||||
return dialogFragmentAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forFragmentManager():
|
|
||||||
FragmentManagerAccessorViaReflection<FragmentManager, Fragment> {
|
|
||||||
return fragmentManagerAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forFragmentActivity(): FragmentActivityAccessorFramework {
|
|
||||||
return fragmentActivityAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getDialogFragments(activity: Activity): List<Any> {
|
|
||||||
if (!fragmentActivityClass.isInstance(activity)) {
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
val activityAccessor = forFragmentActivity()
|
|
||||||
val fragmentManager = activityAccessor.getFragmentManager(activity) ?: return emptyList()
|
|
||||||
|
|
||||||
val fragmentManagerAccessor = forFragmentManager()
|
|
||||||
var addedFragments: List<Any?>? = null
|
|
||||||
try {
|
|
||||||
addedFragments = fragmentManagerAccessor.getAddedFragments(fragmentManager)
|
|
||||||
} catch (e: Exception) {}
|
|
||||||
|
|
||||||
if (addedFragments != null) {
|
|
||||||
val n = addedFragments.size - 1
|
|
||||||
val dialogFragments = mutableListOf<Any>()
|
|
||||||
for (i in 0..n) {
|
|
||||||
val fragment = addedFragments[i]
|
|
||||||
if (fragment != null && dialogFragmentClass.isInstance(fragment)) {
|
|
||||||
dialogFragments.add(fragment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dialogFragments
|
|
||||||
}
|
|
||||||
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun findFragmentForViewInActivity(activity: Activity, view: View): Any? {
|
|
||||||
if (!fragmentActivityClass.isInstance(activity)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val activityAccessor = forFragmentActivity()
|
|
||||||
val fragmentManager = activityAccessor.getFragmentManager(activity) ?: return null
|
|
||||||
|
|
||||||
return findFragmentForViewInFragmentManager(fragmentManager, view)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun findFragmentForViewInFragmentManager(
|
|
||||||
fragmentManager: FragmentManager,
|
|
||||||
view: View
|
|
||||||
): Any? {
|
|
||||||
val fragmentManagerAccessor = forFragmentManager()
|
|
||||||
var fragments: List<Any>? = null
|
|
||||||
try {
|
|
||||||
fragments = fragmentManagerAccessor.getAddedFragments(fragmentManager)
|
|
||||||
} catch (e: Exception) {}
|
|
||||||
|
|
||||||
if (fragments != null) {
|
|
||||||
val n = fragments.size - 1
|
|
||||||
for (i in 0..n) {
|
|
||||||
val fragment = fragments[i]
|
|
||||||
val result = findFragmentForViewInFragment(fragment, view)
|
|
||||||
if (result != null) {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun findFragmentForViewInFragment(fragment: Any, view: View): Any? {
|
|
||||||
if (!fragmentClass.isInstance(fragment)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val fragmentAccessor = forFragment()
|
|
||||||
fragmentAccessor?.let { accessor ->
|
|
||||||
if (accessor.getView(fragment as Fragment) === view) {
|
|
||||||
return fragment
|
|
||||||
}
|
|
||||||
val childFragmentManager = accessor.getChildFragmentManager(fragment as Fragment)
|
|
||||||
return if (childFragmentManager != null) {
|
|
||||||
findFragmentForViewInFragmentManager(childFragmentManager, view)
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
open class FragmentAccessorFrameworkHoneycomb : FragmentAccessor<Fragment, FragmentManager> {
|
|
||||||
override fun getFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragment.fragmentManager
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getResources(fragment: Fragment): Resources {
|
|
||||||
return fragment.resources
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getId(fragment: Fragment): Int {
|
|
||||||
return fragment.id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getTag(fragment: Fragment): String? {
|
|
||||||
return fragment.tag
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getView(fragment: Fragment): View? {
|
|
||||||
return fragment.view
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChildFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
|
||||||
class FragmentAccessorFrameworkJellyBean : FragmentAccessorFrameworkHoneycomb() {
|
|
||||||
override fun getChildFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragment.childFragmentManager
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DialogFragmentAccessorFramework(
|
|
||||||
val fragmentAccessor: FragmentAccessor<Fragment, FragmentManager>
|
|
||||||
) : DialogFragmentAccessor<DialogFragment, Fragment, FragmentManager> {
|
|
||||||
|
|
||||||
override fun getDialog(dialogFragment: DialogFragment): Dialog {
|
|
||||||
return dialogFragment.dialog
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragmentAccessor.getFragmentManager(fragment)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getResources(fragment: Fragment): Resources? {
|
|
||||||
return fragmentAccessor.getResources(fragment)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getId(fragment: Fragment): Int {
|
|
||||||
return fragmentAccessor.getId(fragment)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getTag(fragment: Fragment): String? {
|
|
||||||
return fragmentAccessor.getTag(fragment)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getView(fragment: Fragment): View? {
|
|
||||||
return fragmentAccessor.getView(fragment)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChildFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragmentAccessor.getChildFragmentManager(fragment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FragmentActivityAccessorFramework : FragmentActivityAccessor<Activity, FragmentManager> {
|
|
||||||
override fun getFragmentManager(activity: Activity): FragmentManager? {
|
|
||||||
return activity.fragmentManager
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,172 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.app.Dialog
|
|
||||||
import android.content.res.Resources
|
|
||||||
import android.view.View
|
|
||||||
import androidx.fragment.app.DialogFragment
|
|
||||||
import androidx.fragment.app.Fragment
|
|
||||||
import androidx.fragment.app.FragmentActivity
|
|
||||||
import androidx.fragment.app.FragmentManager
|
|
||||||
|
|
||||||
class FragmentCompatSupportLib :
|
|
||||||
FragmentCompat<Fragment, DialogFragment, FragmentManager, FragmentActivity>() {
|
|
||||||
override val fragmentClass: Class<Fragment>
|
|
||||||
get() = Fragment::class.java
|
|
||||||
override val dialogFragmentClass: Class<DialogFragment>
|
|
||||||
get() = DialogFragment::class.java
|
|
||||||
override val fragmentActivityClass: Class<FragmentActivity>
|
|
||||||
get() = FragmentActivity::class.java
|
|
||||||
|
|
||||||
override fun forFragment(): FragmentAccessorSupportLib {
|
|
||||||
return fragmentAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forDialogFragment(): DialogFragmentAccessorSupportLib {
|
|
||||||
return dialogFragmentAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forFragmentManager(): FragmentManagerAccessor<FragmentManager, Fragment> {
|
|
||||||
return fragmentManagerAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun forFragmentActivity(): FragmentActivityAccessorSupportLib {
|
|
||||||
return fragmentActivityAccessor
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getDialogFragments(activity: Activity): List<Any> {
|
|
||||||
if (!fragmentActivityClass.isInstance(activity)) {
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
val activityAccessor = forFragmentActivity()
|
|
||||||
val fragmentManager =
|
|
||||||
activityAccessor.getFragmentManager(activity as FragmentActivity) ?: return emptyList()
|
|
||||||
|
|
||||||
val fragmentManagerAccessor = forFragmentManager()
|
|
||||||
var addedFragments: List<Any?>? = null
|
|
||||||
try {
|
|
||||||
addedFragments = fragmentManagerAccessor.getAddedFragments(fragmentManager)
|
|
||||||
} catch (e: Exception) {}
|
|
||||||
|
|
||||||
if (addedFragments != null) {
|
|
||||||
val n = addedFragments.size - 1
|
|
||||||
val dialogFragments = mutableListOf<Any>()
|
|
||||||
for (i in 0..n) {
|
|
||||||
val fragment = addedFragments[i]
|
|
||||||
if (fragment != null && dialogFragmentClass.isInstance(fragment)) {
|
|
||||||
dialogFragments.add(fragment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dialogFragments
|
|
||||||
}
|
|
||||||
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun findFragmentForViewInActivity(activity: Activity, view: View): Any? {
|
|
||||||
if (!fragmentActivityClass.isInstance(activity)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val activityAccessor = forFragmentActivity()
|
|
||||||
val fragmentManager =
|
|
||||||
activityAccessor.getFragmentManager(activity as FragmentActivity) ?: return null
|
|
||||||
|
|
||||||
return findFragmentForViewInFragmentManager(fragmentManager, view)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun findFragmentForViewInFragmentManager(
|
|
||||||
fragmentManager: FragmentManager,
|
|
||||||
view: View
|
|
||||||
): Any? {
|
|
||||||
val fragmentManagerAccessor = forFragmentManager()
|
|
||||||
var fragments: List<Any>? = null
|
|
||||||
try {
|
|
||||||
fragments = fragmentManagerAccessor.getAddedFragments(fragmentManager)
|
|
||||||
} catch (e: Exception) {}
|
|
||||||
|
|
||||||
if (fragments != null) {
|
|
||||||
val n = fragments.size - 1
|
|
||||||
for (i in 0..n) {
|
|
||||||
val fragment = fragments[i]
|
|
||||||
val result = findFragmentForViewInFragment(fragment, view)
|
|
||||||
if (result != null) {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun findFragmentForViewInFragment(fragment: Any, view: View): Any? {
|
|
||||||
if (!fragmentClass.isInstance(fragment)) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
val fragmentAccessor = forFragment()
|
|
||||||
if (fragmentAccessor.getView(fragment as Fragment) === view) {
|
|
||||||
return fragment
|
|
||||||
}
|
|
||||||
val childFragmentManager = fragmentAccessor.getChildFragmentManager(fragment as Fragment)
|
|
||||||
return if (childFragmentManager != null) {
|
|
||||||
findFragmentForViewInFragmentManager(childFragmentManager, view)
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
open class FragmentAccessorSupportLib : FragmentAccessor<Fragment, FragmentManager> {
|
|
||||||
override fun getFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragment.fragmentManager
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getResources(fragment: Fragment): Resources? {
|
|
||||||
return fragment.resources
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getId(fragment: Fragment): Int {
|
|
||||||
return fragment.id
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getTag(fragment: Fragment): String? {
|
|
||||||
return fragment.tag
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getView(fragment: Fragment): View? {
|
|
||||||
return fragment.view
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getChildFragmentManager(fragment: Fragment): FragmentManager? {
|
|
||||||
return fragment.childFragmentManager
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class DialogFragmentAccessorSupportLib :
|
|
||||||
FragmentAccessorSupportLib(),
|
|
||||||
DialogFragmentAccessor<DialogFragment, Fragment, FragmentManager> {
|
|
||||||
override fun getDialog(dialogFragment: DialogFragment): Dialog? {
|
|
||||||
return dialogFragment.dialog
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FragmentActivityAccessorSupportLib :
|
|
||||||
FragmentActivityAccessor<FragmentActivity, FragmentManager> {
|
|
||||||
override fun getFragmentManager(activity: FragmentActivity): FragmentManager {
|
|
||||||
return activity.supportFragmentManager
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
private val fragmentAccessor = FragmentAccessorSupportLib()
|
|
||||||
private val dialogFragmentAccessor = DialogFragmentAccessorSupportLib()
|
|
||||||
private val fragmentManagerAccessor =
|
|
||||||
FragmentManagerAccessorViaReflection<FragmentManager, Fragment>()
|
|
||||||
private val fragmentActivityAccessor = FragmentActivityAccessorSupportLib()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
interface FragmentManagerAccessor<FRAGMENT_MANAGER, FRAGMENT> {
|
|
||||||
fun getAddedFragments(fragmentManager: FRAGMENT_MANAGER): List<FRAGMENT>
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import java.lang.reflect.Field
|
|
||||||
|
|
||||||
object ReflectionUtil {
|
|
||||||
fun tryGetClassForName(className: String): Class<*>? {
|
|
||||||
return try {
|
|
||||||
Class.forName(className)
|
|
||||||
} catch (e: ClassNotFoundException) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun tryGetDeclaredField(theClass: Class<*>, fieldName: String): Field? {
|
|
||||||
return try {
|
|
||||||
theClass.getDeclaredField(fieldName)
|
|
||||||
} catch (e: NoSuchFieldException) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getFieldValue(field: Field, target: Any?): Any? {
|
|
||||||
return try {
|
|
||||||
field[target]
|
|
||||||
} catch (e: IllegalAccessException) {
|
|
||||||
throw RuntimeException(e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.facebook.flipper.plugins.uidebugger.stetho
|
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.Context
|
|
||||||
import android.content.ContextWrapper
|
|
||||||
import android.view.View
|
|
||||||
|
|
||||||
object ViewUtil {
|
|
||||||
fun tryGetActivity(view: View?): Activity? {
|
|
||||||
if (view == null) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
val context = view.context
|
|
||||||
val activityFromContext = tryGetActivity(context)
|
|
||||||
if (activityFromContext != null) {
|
|
||||||
return activityFromContext
|
|
||||||
}
|
|
||||||
val parent = view.parent
|
|
||||||
if (parent is View) {
|
|
||||||
val parentView = parent as View
|
|
||||||
return tryGetActivity(parentView)
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun tryGetActivity(context: Context): Activity? {
|
|
||||||
var ctx: Context? = context
|
|
||||||
while (ctx != null) {
|
|
||||||
ctx =
|
|
||||||
when (ctx) {
|
|
||||||
is Activity -> {
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
is ContextWrapper -> {
|
|
||||||
ctx.baseContext
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user