2727import org .greenrobot .eventbus .EventBus ;
2828import org .greenrobot .eventbus .Subscribe ;
2929
30+ import java .lang .reflect .Method ;
3031import java .util .Arrays ;
3132import java .util .regex .Pattern ;
3233import java .util .List ;
@@ -62,6 +63,25 @@ public class RNInAppBrowser {
6263 private Activity currentActivity ;
6364 private static final Pattern animationIdentifierPattern = Pattern .compile ("^.+:.+/" );
6465
66+ public Integer setColor (CustomTabsIntent .Builder builder , final ReadableMap options , String key , String method , String colorName ) {
67+ String colorString = options .getString (key );
68+ Integer color = null ;
69+ try {
70+ if (colorString != null ) {
71+ color = Color .parseColor (colorString );
72+ Method findMethod = builder .getClass ().getDeclaredMethod (method , int .class );
73+ findMethod .invoke (builder , color );
74+ }
75+ } catch (Exception e ) {
76+ if (e instanceof IllegalArgumentException ) {
77+ throw new JSApplicationIllegalArgumentException (
78+ "Invalid " + colorName + " color '" + colorString + "': " + e .getMessage ());
79+ }
80+ } finally {
81+ return color ;
82+ }
83+ }
84+
6585 public void open (Context context , final ReadableMap options , final Promise promise , Activity activity ) {
6686 final String url = options .getString ("url" );
6787 currentActivity = activity ;
@@ -82,43 +102,14 @@ public void open(Context context, final ReadableMap options, final Promise promi
82102
83103 CustomTabsIntent .Builder builder = new CustomTabsIntent .Builder ();
84104 isLightTheme = false ;
85- if (options .hasKey (KEY_TOOLBAR_COLOR )) {
86- final String colorString = options .getString (KEY_TOOLBAR_COLOR );
87- try {
88- builder .setToolbarColor (Color .parseColor (colorString ));
89- isLightTheme = toolbarIsLight (colorString );
90- } catch (IllegalArgumentException e ) {
91- throw new JSApplicationIllegalArgumentException (
92- "Invalid toolbar color '" + colorString + "': " + e .getMessage ());
93- }
94- }
95- if (options .hasKey (KEY_SECONDARY_TOOLBAR_COLOR )) {
96- final String colorString = options .getString (KEY_SECONDARY_TOOLBAR_COLOR );
97- try {
98- builder .setSecondaryToolbarColor (Color .parseColor (colorString ));
99- } catch (IllegalArgumentException e ) {
100- throw new JSApplicationIllegalArgumentException (
101- "Invalid secondary toolbar color '" + colorString + "': " + e .getMessage ());
102- }
103- }
104- if (options .hasKey (KEY_NAVIGATION_BAR_COLOR )) {
105- final String colorString = options .getString (KEY_NAVIGATION_BAR_COLOR );
106- try {
107- builder .setNavigationBarColor (Color .parseColor (colorString ));
108- } catch (IllegalArgumentException e ) {
109- throw new JSApplicationIllegalArgumentException (
110- "Invalid navigation bar color '" + colorString + "': " + e .getMessage ());
111- }
112- }
113- if (options .hasKey (KEY_NAVIGATION_BAR_DIVIDER_COLOR )) {
114- final String colorString = options .getString (KEY_NAVIGATION_BAR_DIVIDER_COLOR );
115- try {
116- builder .setNavigationBarDividerColor (Color .parseColor (colorString ));
117- } catch (IllegalArgumentException e ) {
118- throw new JSApplicationIllegalArgumentException (
119- "Invalid navigation bar divider color '" + colorString + "': " + e .getMessage ());
120- }
105+ final Integer toolbarColor = setColor (builder , options , KEY_TOOLBAR_COLOR , "setToolbarColor" , "toolbar" );
106+ if (toolbarColor != null ) {
107+ isLightTheme = toolbarIsLight (toolbarColor );
121108 }
109+ setColor (builder , options , KEY_SECONDARY_TOOLBAR_COLOR , "setSecondaryToolbarColor" , "secondary toolbar" );
110+ setColor (builder , options , KEY_NAVIGATION_BAR_COLOR , "setNavigationBarColor" , "navigation bar" );
111+ setColor (builder , options , KEY_NAVIGATION_BAR_DIVIDER_COLOR , "setNavigationBarDividerColor" , "navigation bar divider" );
112+
122113 if (options .hasKey (KEY_DEFAULT_SHARE_MENU_ITEM ) &&
123114 options .getBoolean (KEY_DEFAULT_SHARE_MENU_ITEM )) {
124115 builder .addDefaultShareMenuItem ();
@@ -292,8 +283,8 @@ private void unRegisterEventBus() {
292283 }
293284 }
294285
295- private Boolean toolbarIsLight (String themeColor ) {
296- return ColorUtils .calculateLuminance (Color . parseColor ( themeColor ) ) > 0.5 ;
286+ private Boolean toolbarIsLight (int themeColor ) {
287+ return ColorUtils .calculateLuminance (themeColor ) > 0.5 ;
297288 }
298289
299290 private List <ResolveInfo > getPreferredPackages (Context context ) {
0 commit comments