8
8
import android .content .Intent ;
9
9
import android .content .pm .ResolveInfo ;
10
10
import android .graphics .Color ;
11
+ import android .graphics .BitmapFactory ;
11
12
import android .provider .Browser ;
12
13
import androidx .annotation .Nullable ;
13
14
import androidx .browser .customtabs .CustomTabsIntent ;
15
+ import androidx .core .graphics .ColorUtils ;
14
16
15
17
import com .facebook .react .bridge .Arguments ;
16
18
import com .facebook .react .bridge .Promise ;
@@ -40,8 +42,10 @@ public class RNInAppBrowser {
40
42
private static final String KEY_ANIMATION_START_EXIT = "startExit" ;
41
43
private static final String KEY_ANIMATION_END_ENTER = "endEnter" ;
42
44
private static final String KEY_ANIMATION_END_EXIT = "endExit" ;
45
+ private static final String HASBACKBUTTON = "hasBackButton" ;
43
46
44
47
private @ Nullable Promise mOpenBrowserPromise ;
48
+ private Boolean isLightTheme ;
45
49
private Activity currentActivity ;
46
50
private static final Pattern animationIdentifierPattern = Pattern .compile ("^.+:.+/" );
47
51
@@ -68,6 +72,7 @@ public void open(Context context, final ReadableMap options, final Promise promi
68
72
final String colorString = options .getString (KEY_TOOLBAR_COLOR );
69
73
try {
70
74
builder .setToolbarColor (Color .parseColor (colorString ));
75
+ isLightTheme = toolbarIsLight (colorString );
71
76
} catch (IllegalArgumentException e ) {
72
77
throw new JSApplicationIllegalArgumentException (
73
78
"Invalid toolbar color '" + colorString + "': " + e .getMessage ());
@@ -94,6 +99,11 @@ public void open(Context context, final ReadableMap options, final Promise promi
94
99
final ReadableMap animations = options .getMap (KEY_ANIMATIONS );
95
100
applyAnimation (context , builder , animations );
96
101
}
102
+ if (options .hasKey (HASBACKBUTTON ) &&
103
+ options .getBoolean (HASBACKBUTTON )) {
104
+ builder .setCloseButtonIcon (BitmapFactory .decodeResource (
105
+ context .getResources (), isLightTheme ? R .drawable .ic_arrow_back_black : R .drawable .ic_arrow_back_white ));
106
+ }
97
107
98
108
CustomTabsIntent customTabsIntent = builder .build ();
99
109
@@ -226,4 +236,8 @@ private void unRegisterEventBus() {
226
236
EventBus .getDefault ().unregister (this );
227
237
}
228
238
}
239
+
240
+ private Boolean toolbarIsLight (String themeColor ) {
241
+ return ColorUtils .calculateLuminance (Color .parseColor (themeColor )) > 0.5 ;
242
+ }
229
243
}
0 commit comments