mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
This patch works around Android's limitations on reflection. With it embedded platform views that use virtual node hierarchy trees should be accessible on all Android versions to date. The workarounds in this PR are brittle. Ideally the methods would be made public in Android instead so we wouldn't need to employ tactics like these to work around the missing methods. `AccessibilityNodeInfo#getChildId` is blocked from any type of reflection access, but the underlying private member that the getter accesses, `mChildNodeIds`, can still be reflected on. On Android versions where we can't access the getter, this patch falls back on reflectively accessing the field instead. Unfortunately this field is a [`LongArray`](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/util/LongArray.java), a utility data class private to Android. So in this case we're reflecting to both access the member and actually read data from it, since we need to use reflection to call `LongArray.get(index)`. `AccessibilityNodeInfo#getParent()` doesn't have any lucky available underlying members. However, `AccessibilityNodeInfo` itself is `Parcelable`, and `mParentNodeId` is one of the pieces of data that's written to a parcel via `AccessibilityNodeInfo#writeToParcel`. So the fallback for that is to write the node to a parcel and then read the parcel for the ID in question. This will break if the implementation details of `AccessibilityNodeInfo#writeToParcel` ever change. The details have already changed enough in the past to require two sets of logic for reading from the parcel.