Skip to content

Commit aeb6296

Browse files
hurali97axinvdkot331107
authored
chore: cherry picks for 0.71.15 (#41993)
* Fix android platform border color (#39893) Summary: If you try to apply PlatformColor to borders on Android app will crash with the next error: "Error while updating property 'borderColor' of a view managed by: RCTView" ## Changelog: [ANDROID] [FIXED] - Fix android crash when apply PlatformColor to borders Pull Request resolved: #39893 Test Plan: In RNTester example, go to APIs -> PlatformColor | Before | After | | ----------- | ----------- | | <img src="https://github.com/facebook/react-native/assets/70860930/66ac2880-53da-4438-bd9a-332f8ea40645" alt="drawing" width="200"/> | <img src="https://github.com/facebook/react-native/assets/70860930/151f58a1-d857-4b3d-9ec6-de74eb065127" alt="drawing" width="200"/> | Reviewed By: NickGerleman Differential Revision: D50011758 Pulled By: javache fbshipit-source-id: ea06c18c6aef4b6731e9b9b87422a1e0d13de208 * Android: fix ClassCastException in ReactRootView.java when software keyboard is shown (#40755) Summary: Fixes #40754 Hi all! We noticed that our app started to crash after bumping to RN v0.71.13, anyways after a deeper investigation we also found that the crash occurs in the latest version as well. Crash log: ``` E FATAL EXCEPTION: main Process: com.nfl.fantasy.core.android.debug, PID: 6034 java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.getActivity(ReactRootView.java:926) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.checkForKeyboardEvents(ReactRootView.java:946) at com.facebook.react.ReactRootView$CustomGlobalLayoutListener.onGlobalLayout(ReactRootView.java:912) at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1061) ``` The code which causes ClassCastException is following [here](https://github.com/facebook/react-native/blob/ea88fbe229e1d276753ee8e118184274fc872138/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java#L864). In this code explicit type conversion to Activity is not safe because it's not guaranteed by the compiler that context will be compatible with Activity type. The appropriate issue [has been filed](#40754). <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID] [FIXED] - Fixed crash occurring in certain native views when keyboard events are fired. Pull Request resolved: #40755 Test Plan: Tested it manually with the [reference application](https://github.com/kot331107/rnCrashReproducer). Repro steps are as follows: - Build and run the app on Android - Tap the button "Open Modal" - You should see the red popup fragment to the bottom of the screen - Tap on the text input to open software keyboard - Expected: it should show the keyboard and no crash happens. Reviewed By: arushikesarwani94 Differential Revision: D50198424 Pulled By: NickGerleman fbshipit-source-id: a5a6d86334856f4ffbe818150da5793380da4702 * chore: bump podfile.lock --------- Co-authored-by: Ivan Alexandrov <axinvd@gmail.com> Co-authored-by: Filipp Mikheev <kot331107@gmail.com>
1 parent 0bc2dd0 commit aeb6296

3 files changed

Lines changed: 580 additions & 553 deletions

File tree

ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import static com.facebook.react.uimanager.common.UIManagerType.FABRIC;
1313
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
1414

15-
import android.app.Activity;
1615
import android.content.Context;
17-
import android.content.ContextWrapper;
1816
import android.graphics.Canvas;
1917
import android.graphics.Insets;
2018
import android.graphics.Point;
@@ -917,12 +915,18 @@ public void onGlobalLayout() {
917915
checkForDeviceDimensionsChanges();
918916
}
919917

920-
private Activity getActivity() {
921-
Context context = getContext();
922-
while (!(context instanceof Activity) && context instanceof ContextWrapper) {
923-
context = ((ContextWrapper) context).getBaseContext();
918+
private @Nullable WindowManager.LayoutParams getWindowLayoutParams() {
919+
View view = ReactRootView.this;
920+
if (view.getLayoutParams() instanceof WindowManager.LayoutParams) {
921+
return (WindowManager.LayoutParams) view.getLayoutParams();
924922
}
925-
return (Activity) context;
923+
while (view.getParent() instanceof View) {
924+
view = (View) view.getParent();
925+
if (view.getLayoutParams() instanceof WindowManager.LayoutParams) {
926+
return (WindowManager.LayoutParams) view.getLayoutParams();
927+
}
928+
}
929+
return null;
926930
}
927931

928932
@RequiresApi(api = Build.VERSION_CODES.R)
@@ -942,7 +946,13 @@ private void checkForKeyboardEvents() {
942946
Insets barInsets = rootInsets.getInsets(WindowInsets.Type.systemBars());
943947
int height = imeInsets.bottom - barInsets.bottom;
944948

945-
int softInputMode = getActivity().getWindow().getAttributes().softInputMode;
949+
int softInputMode;
950+
WindowManager.LayoutParams windowLayoutParams = getWindowLayoutParams();
951+
if (windowLayoutParams != null) {
952+
softInputMode = windowLayoutParams.softInputMode;
953+
} else {
954+
return;
955+
}
946956
int screenY =
947957
softInputMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING
948958
? mVisibleViewArea.bottom - height

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewManagersPropertyCache.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ public ColorPropSetter(ReactProp prop, Method setter, int defaultValue) {
204204
mDefaultValue = defaultValue;
205205
}
206206

207+
public ColorPropSetter(ReactPropGroup prop, Method setter, int index, int defaultValue) {
208+
super(prop, "mixed", setter, index);
209+
mDefaultValue = defaultValue;
210+
}
211+
207212
@Override
208213
protected Object getValueOrDefault(Object value, Context context) {
209214
if (value == null) {
@@ -331,6 +336,10 @@ public BoxedColorPropSetter(ReactProp prop, Method setter) {
331336
super(prop, "mixed", setter);
332337
}
333338

339+
public BoxedColorPropSetter(ReactPropGroup prop, Method setter, int index) {
340+
super(prop, "mixed", setter, index);
341+
}
342+
334343
@Override
335344
protected @Nullable Object getValueOrDefault(Object value, Context context) {
336345
if (value != null) {
@@ -463,7 +472,11 @@ private static void createPropSetters(
463472
}
464473
} else if (propTypeClass == int.class) {
465474
for (int i = 0; i < names.length; i++) {
466-
props.put(names[i], new IntPropSetter(annotation, method, i, annotation.defaultInt()));
475+
if ("Color".equals(annotation.customType())) {
476+
props.put(names[i], new ColorPropSetter(annotation, method, i, annotation.defaultInt()));
477+
} else {
478+
props.put(names[i], new IntPropSetter(annotation, method, i, annotation.defaultInt()));
479+
}
467480
}
468481
} else if (propTypeClass == float.class) {
469482
for (int i = 0; i < names.length; i++) {
@@ -476,7 +489,11 @@ private static void createPropSetters(
476489
}
477490
} else if (propTypeClass == Integer.class) {
478491
for (int i = 0; i < names.length; i++) {
479-
props.put(names[i], new BoxedIntPropSetter(annotation, method, i));
492+
if ("Color".equals(annotation.customType())) {
493+
props.put(names[i], new BoxedColorPropSetter(annotation, method, i));
494+
} else {
495+
props.put(names[i], new BoxedIntPropSetter(annotation, method, i));
496+
}
480497
}
481498
} else {
482499
throw new RuntimeException(

0 commit comments

Comments
 (0)