Skip to content

Commit 8aaa9b6

Browse files
author
Aitor Cubeles Torres
committed
hasBackButton feature on Android browser
1 parent ef70ac3 commit 8aaa9b6

File tree

9 files changed

+14
-0
lines changed

9 files changed

+14
-0
lines changed

android/src/main/java/com/proyecto26/inappbrowser/RNInAppBrowser.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import android.content.Context;
88
import android.content.Intent;
99
import android.graphics.Color;
10+
import android.graphics.BitmapFactory;
1011
import android.provider.Browser;
1112
import androidx.annotation.Nullable;
1213
import androidx.browser.customtabs.CustomTabsIntent;
14+
import androidx.core.graphics.ColorUtils;
1315

1416
import com.facebook.react.bridge.Arguments;
1517
import com.facebook.react.bridge.Promise;
@@ -38,8 +40,10 @@ public class RNInAppBrowser {
3840
private static final String KEY_ANIMATION_START_EXIT = "startExit";
3941
private static final String KEY_ANIMATION_END_ENTER = "endEnter";
4042
private static final String KEY_ANIMATION_END_EXIT = "endExit";
43+
private static final String HASBACKBUTTON = "hasBackButton";
4144

4245
private @Nullable Promise mOpenBrowserPromise;
46+
private Boolean isLightTheme;
4347
private Activity currentActivity;
4448
private static final Pattern animationIdentifierPattern = Pattern.compile("^.+:.+/");
4549

@@ -66,6 +70,7 @@ public void open(Context context, final ReadableMap options, final Promise promi
6670
final String colorString = options.getString(KEY_TOOLBAR_COLOR);
6771
try {
6872
builder.setToolbarColor(Color.parseColor(colorString));
73+
isLightTheme = toolbarIsLight(colorString);
6974
} catch (IllegalArgumentException e) {
7075
throw new JSApplicationIllegalArgumentException(
7176
"Invalid toolbar color '" + colorString + "': " + e.getMessage());
@@ -92,6 +97,11 @@ public void open(Context context, final ReadableMap options, final Promise promi
9297
final ReadableMap animations = options.getMap(KEY_ANIMATIONS);
9398
applyAnimation(context, builder, animations);
9499
}
100+
if (options.hasKey(HASBACKBUTTON) &&
101+
options.getBoolean(HASBACKBUTTON)) {
102+
builder.setCloseButtonIcon(BitmapFactory.decodeResource(
103+
context.getResources(), isLightTheme ? R.drawable.ic_arrow_back_black : R.drawable.ic_arrow_back_white));
104+
}
95105

96106
CustomTabsIntent customTabsIntent = builder.build();
97107

@@ -218,4 +228,8 @@ private void unRegisterEventBus() {
218228
EventBus.getDefault().unregister(this);
219229
}
220230
}
231+
232+
private Boolean toolbarIsLight(String themeColor) {
233+
return ColorUtils.calculateLuminance(Color.parseColor(themeColor)) > 0.5;
234+
}
221235
}
146 Bytes
Loading
146 Bytes
Loading
109 Bytes
Loading
111 Bytes
Loading
156 Bytes
Loading
153 Bytes
Loading
193 Bytes
Loading
195 Bytes
Loading

0 commit comments

Comments
 (0)