From 58437cd10a667bbcbc16781df855fd7c3d73bf49 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Mon, 4 Feb 2019 07:46:49 -0800 Subject: [PATCH] fix checkbox casting crash in old android versions (#23281) Summary: fixes casting on older android versions reported in #22885 [Android] [Fixed] - fix casting crash in android checkbox Pull Request resolved: https://github.com/facebook/react-native/pull/23281 Differential Revision: D13941776 Pulled By: cpojer fbshipit-source-id: ff3695f64d3399790a03b02d5b1363cacc655336 --- .../views/checkbox/ReactCheckBoxManager.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/checkbox/ReactCheckBoxManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/checkbox/ReactCheckBoxManager.java index fcff4e6f2eb081..f139e967447180 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/checkbox/ReactCheckBoxManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/checkbox/ReactCheckBoxManager.java @@ -6,6 +6,8 @@ */ package com.facebook.react.views.checkbox; +import android.content.Context; +import android.support.v7.widget.TintContextWrapper; import android.widget.CompoundButton; import com.facebook.react.bridge.ReactContext; import com.facebook.react.uimanager.SimpleViewManager; @@ -13,7 +15,6 @@ import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.ViewProps; import com.facebook.react.uimanager.annotations.ReactProp; -import com.facebook.react.uimanager.events.EventDispatcher; /** View manager for {@link ReactCheckBox} components. */ public class ReactCheckBoxManager extends SimpleViewManager { @@ -24,11 +25,22 @@ public class ReactCheckBoxManager extends SimpleViewManager { new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - ReactContext reactContext = (ReactContext) buttonView.getContext(); + ReactContext reactContext = getReactContext(buttonView); reactContext .getNativeModule(UIManagerModule.class).getEventDispatcher() .dispatchEvent(new ReactCheckBoxEvent(buttonView.getId(), isChecked)); } + + private ReactContext getReactContext(CompoundButton buttonView) { + ReactContext reactContext; + Context ctx = buttonView.getContext(); + if (ctx instanceof TintContextWrapper) { + reactContext = (ReactContext) ((TintContextWrapper) ctx).getBaseContext(); + } else { + reactContext = (ReactContext) buttonView.getContext(); + } + return reactContext; + } }; @Override