27
27
import org .greenrobot .eventbus .EventBus ;
28
28
import org .greenrobot .eventbus .Subscribe ;
29
29
30
+ import java .lang .reflect .Method ;
30
31
import java .util .Arrays ;
31
32
import java .util .regex .Pattern ;
32
33
import java .util .List ;
@@ -62,6 +63,25 @@ public class RNInAppBrowser {
62
63
private Activity currentActivity ;
63
64
private static final Pattern animationIdentifierPattern = Pattern .compile ("^.+:.+/" );
64
65
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
+
65
85
public void open (Context context , final ReadableMap options , final Promise promise , Activity activity ) {
66
86
final String url = options .getString ("url" );
67
87
currentActivity = activity ;
@@ -82,43 +102,14 @@ public void open(Context context, final ReadableMap options, final Promise promi
82
102
83
103
CustomTabsIntent .Builder builder = new CustomTabsIntent .Builder ();
84
104
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 );
121
108
}
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
+
122
113
if (options .hasKey (KEY_DEFAULT_SHARE_MENU_ITEM ) &&
123
114
options .getBoolean (KEY_DEFAULT_SHARE_MENU_ITEM )) {
124
115
builder .addDefaultShareMenuItem ();
@@ -292,8 +283,8 @@ private void unRegisterEventBus() {
292
283
}
293
284
}
294
285
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 ;
297
288
}
298
289
299
290
private List <ResolveInfo > getPreferredPackages (Context context ) {
0 commit comments