Skip to content

Commit

Permalink
Add support for AndroidX fragments
Browse files Browse the repository at this point in the history
Summary:
Fix #931

This is not how I would *like* to fix this, but it should do the job.
When the switch over to AndroidX was made, the overall abstraction
started to leak and we really need to remodel this in its entirety.
There's also the question of whether we want to support both support
fragments and AndroidX fragments or not. Right now it's kinda-sorta
supported but only under some circumstances, which is not great.

Test Plan:
Changed the sample app to include some AndroidX fragments and they
now show up (again) in the view hierarchy:
  • Loading branch information
passy committed Apr 1, 2020
1 parent 0711617 commit 767b7c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.facebook.flipper.plugins.inspector.descriptors;

import android.app.Activity;
import android.util.Log;
import android.view.Window;
import com.facebook.flipper.core.FlipperDynamic;
import com.facebook.flipper.core.FlipperObject;
Expand All @@ -25,6 +26,8 @@

public class ActivityDescriptor extends NodeDescriptor<Activity> {

private static final String TAG = "ActivityDescriptor";

@Override
public void init(Activity node) {}

Expand Down Expand Up @@ -112,9 +115,14 @@ private static List<Object> getDialogFragments(FragmentCompat compat, Activity a
}

FragmentManagerAccessor fragmentManagerAccessor = compat.forFragmentManager();
List<Object> addedFragments = fragmentManagerAccessor.getAddedFragments(fragmentManager);
List<Object> addedFragments = null;
try {
addedFragments = fragmentManagerAccessor.getAddedFragments(fragmentManager);
} catch (Exception e) {
Log.e(TAG, "Failed to obtain list of fragments.", e);
}
if (addedFragments == null) {
return Collections.EMPTY_LIST;
return Collections.emptyList();
}

final List<Object> dialogFragments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@
package com.facebook.flipper.plugins.inspector.descriptors.utils.stethocopies;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import androidx.fragment.app.FragmentManager;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;
Expand Down Expand Up @@ -37,7 +43,7 @@ public abstract class FragmentCompat<

static {
sHasSupportFragment =
ReflectionUtil.tryGetClassForName("android.support.v4.app.Fragment") != null;
ReflectionUtil.tryGetClassForName("androidx.fragment.app.Fragment") != null;
}

@Nullable
Expand Down Expand Up @@ -82,20 +88,22 @@ static class FragmentManagerAccessorViaReflection<FRAGMENT_MANAGER, FRAGMENT>
@Nullable
@Override
public List<FRAGMENT> getAddedFragments(FRAGMENT_MANAGER fragmentManager) {
// This field is actually sitting on FragmentManagerImpl, which derives from FragmentManager.
if (mFieldMAdded == null) {
Field fieldMAdded =
ReflectionUtil.tryGetDeclaredField(fragmentManager.getClass(), "mAdded");

if (fieldMAdded != null) {
fieldMAdded.setAccessible(true);
mFieldMAdded = fieldMAdded;
if (fragmentManager instanceof android.app.FragmentManager) {
// This field is actually sitting on FragmentManagerImpl, which derives from
// FragmentManager.
if (mFieldMAdded == null) {
Field fieldMAdded =
ReflectionUtil.tryGetDeclaredField(fragmentManager.getClass(), "mAdded");

if (fieldMAdded != null) {
fieldMAdded.setAccessible(true);
mFieldMAdded = fieldMAdded;
}
}
} else if (fragmentManager instanceof androidx.fragment.app.FragmentManager) {
return (List<FRAGMENT>) ((FragmentManager) fragmentManager).getFragments();
}

return (mFieldMAdded != null)
? (List<FRAGMENT>) ReflectionUtil.getFieldValue(mFieldMAdded, fragmentManager)
: null;
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
package com.facebook.flipper.plugins.inspector.descriptors.utils.stethocopies;

import android.app.Activity;
import android.util.Log;
import android.view.View;

import java.util.Collections;
import java.util.List;
import javax.annotation.Nullable;

public final class FragmentCompatUtil {
private static final String TAG = "FragmentCompatUtil";

private FragmentCompatUtil() {}

public static boolean isDialogFragment(Object fragment) {
Expand Down Expand Up @@ -79,7 +84,13 @@ private static Object findFragmentForViewInActivity(
@Nullable
private static Object findFragmentForViewInFragmentManager(
FragmentCompat compat, Object fragmentManager, View view) {
List<?> fragments = compat.forFragmentManager().getAddedFragments(fragmentManager);
List<?> fragments;
try {
fragments = compat.forFragmentManager().getAddedFragments(fragmentManager);
} catch (Exception e) {
fragments = Collections.emptyList();
Log.e(TAG, "Failed to obtain list of fragments.", e);
}

if (fragments != null) {
for (int i = 0, N = fragments.size(); i < N; ++i) {
Expand Down

0 comments on commit 767b7c8

Please sign in to comment.