Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AndroidX fragments #957

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The instanceof check here defeats the purpose of this abstraction a bit but this looks like a working fix otherwise.

// 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