Skip to content

Commit bc0d5b0

Browse files
committed
Refactor CustomTabsModule
1 parent e2ee65e commit bc0d5b0

File tree

5 files changed

+60
-54
lines changed

5 files changed

+60
-54
lines changed

android/src/main/java/com/github/droibit/android/reactnative/customtabs/CustomTabsModule.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.graphics.Color;
77
import android.os.Bundle;
88
import android.provider.Browser;
9+
import android.support.annotation.NonNull;
910
import android.support.customtabs.CustomTabsIntent;
1011
import android.text.TextUtils;
1112
import com.droibit.android.customtabs.launcher.CustomTabsLauncher;
@@ -22,7 +23,6 @@
2223
import com.facebook.react.common.annotations.VisibleForTesting;
2324
import java.util.Map;
2425
import java.util.regex.Pattern;
25-
import javax.annotation.Nullable;
2626

2727
/**
2828
* CustomTabs module.
@@ -41,17 +41,23 @@ public class CustomTabsModule extends ReactContextBaseJavaModule {
4141
private static final String KEY_ANIMATION_END_ENTER = "endEnter";
4242
private static final String KEY_ANIMATION_END_EXIT = "endExit";
4343

44+
private static final String ANIMATION_SLIDE_IN_RIGHT = "ANIMATION_SLIDE_IN_RIGHT";
45+
private static final String ANIMATION_SLIDE_OUT_RIGHT = "ANIMATION_SLIDE_OUT_RIGHT";
46+
private static final String ANIMATION_SLIDE_IN_LEFT = "ANIMATION_SLIDE_IN_LEFT";
47+
private static final String ANIMATION_SLIDE_OUT_LEFT = "ANIMATION_SLIDE_OUT_LEFT";
48+
private static final String ANIMATION_FADE_IN = "ANIMATION_FADE_IN";
49+
private static final String ANIMATION_FADE_OUT = "ANIMATION_FADE_OUT";
50+
4451
private static final Map<String, Object> CONSTANTS;
4552

4653
static {
4754
CONSTANTS = MapBuilder.newHashMap();
48-
CONSTANTS.put(KEY_TOOLBAR_COLOR, KEY_TOOLBAR_COLOR);
49-
CONSTANTS.put(KEY_ENABLE_URL_BAR_HIDING, KEY_ENABLE_URL_BAR_HIDING);
50-
CONSTANTS.put(KEY_SHOW_PAGE_TITLE, KEY_SHOW_PAGE_TITLE);
51-
CONSTANTS.put(KEY_DEFAULT_SHARE_MENU_ITEM, KEY_DEFAULT_SHARE_MENU_ITEM);
52-
CONSTANTS.put(KEY_ANIMATIONS, KEY_ANIMATIONS);
53-
CONSTANTS.put(KEY_HEADERS, KEY_HEADERS);
54-
CONSTANTS.put(KEY_FORCE_CLOSE_ON_REDIRECTION, KEY_FORCE_CLOSE_ON_REDIRECTION);
55+
CONSTANTS.put(ANIMATION_SLIDE_IN_RIGHT, "slide_in_right");
56+
CONSTANTS.put(ANIMATION_SLIDE_OUT_RIGHT, "android:anim/slide_out_right");
57+
CONSTANTS.put(ANIMATION_SLIDE_IN_LEFT, "android:anim/slide_in_left");
58+
CONSTANTS.put(ANIMATION_SLIDE_OUT_LEFT, "slide_out_left");
59+
CONSTANTS.put(ANIMATION_FADE_IN, "android:anim/fade_in");
60+
CONSTANTS.put(ANIMATION_FADE_OUT, "android:anim/fade_out");
5561
}
5662

5763
private static final String MODULE_NAME = "CustomTabsManager";
@@ -69,8 +75,6 @@ public String getName() {
6975
return MODULE_NAME;
7076
}
7177

72-
@Nullable
73-
@Override
7478
public Map<String, Object> getConstants() {
7579
return CONSTANTS;
7680
}
@@ -115,8 +119,7 @@ public void openURL(String url, ReadableMap option, Promise promise) {
115119
}
116120
}
117121

118-
@VisibleForTesting
119-
CustomTabsIntent buildIntent(Context context,
122+
private CustomTabsIntent buildIntent(Context context,
120123
CustomTabsIntent.Builder builder,
121124
ReadableMap option) {
122125
if (option.hasKey(KEY_TOOLBAR_COLOR)) {
@@ -140,8 +143,6 @@ CustomTabsIntent buildIntent(Context context,
140143
builder.addDefaultShareMenuItem();
141144
}
142145

143-
// TODO: If it does not launch Chrome, animation is unnecessary?
144-
145146
if (option.hasKey(KEY_ANIMATIONS)) {
146147
final ReadableMap animations = option.getMap(KEY_ANIMATIONS);
147148
applyAnimation(context, builder, animations);
@@ -182,35 +183,35 @@ CustomTabsIntent buildIntent(Context context,
182183
return customTabsIntent;
183184
}
184185

185-
@VisibleForTesting
186-
boolean httpOrHttpsScheme(String url) {
186+
private boolean httpOrHttpsScheme(String url) {
187187
return url.startsWith("http") || url.startsWith("https");
188188
}
189189

190-
@VisibleForTesting
191-
void applyAnimation(Context context, CustomTabsIntent.Builder builder,
192-
ReadableMap animations) {
193-
final int startEnterAnimationId = animations.hasKey(KEY_ANIMATION_START_ENTER)
190+
private void applyAnimation(
191+
@NonNull Context context,
192+
@NonNull CustomTabsIntent.Builder destBuilder,
193+
@NonNull ReadableMap srcAnimations) {
194+
final int startEnterAnimationId = srcAnimations.hasKey(KEY_ANIMATION_START_ENTER)
194195
? resolveAnimationIdentifierIfNeeded(context,
195-
animations.getString(KEY_ANIMATION_START_ENTER))
196+
srcAnimations.getString(KEY_ANIMATION_START_ENTER))
196197
: -1;
197-
final int startExitAnimationId = animations.hasKey(KEY_ANIMATION_START_EXIT)
198+
final int startExitAnimationId = srcAnimations.hasKey(KEY_ANIMATION_START_EXIT)
198199
? resolveAnimationIdentifierIfNeeded(context,
199-
animations.getString(KEY_ANIMATION_START_EXIT))
200+
srcAnimations.getString(KEY_ANIMATION_START_EXIT))
200201
: -1;
201-
final int endEnterAnimationId = animations.hasKey(KEY_ANIMATION_END_ENTER)
202-
? resolveAnimationIdentifierIfNeeded(context, animations.getString(KEY_ANIMATION_END_ENTER))
202+
final int endEnterAnimationId = srcAnimations.hasKey(KEY_ANIMATION_END_ENTER)
203+
? resolveAnimationIdentifierIfNeeded(context, srcAnimations.getString(KEY_ANIMATION_END_ENTER))
203204
: -1;
204-
final int endExitAnimationId = animations.hasKey(KEY_ANIMATION_END_EXIT)
205-
? resolveAnimationIdentifierIfNeeded(context, animations.getString(KEY_ANIMATION_END_EXIT))
205+
final int endExitAnimationId = srcAnimations.hasKey(KEY_ANIMATION_END_EXIT)
206+
? resolveAnimationIdentifierIfNeeded(context, srcAnimations.getString(KEY_ANIMATION_END_EXIT))
206207
: -1;
207208

208209
if (startEnterAnimationId != -1 && startExitAnimationId != -1) {
209-
builder.setStartAnimations(context, startEnterAnimationId, startExitAnimationId);
210+
destBuilder.setStartAnimations(context, startEnterAnimationId, startExitAnimationId);
210211
}
211212

212213
if (endEnterAnimationId != -1 && endExitAnimationId != -1) {
213-
builder.setExitAnimations(context, endEnterAnimationId, endExitAnimationId);
214+
destBuilder.setExitAnimations(context, endEnterAnimationId, endExitAnimationId);
214215
}
215216
}
216217

src/Animations.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/CustomTabs.android.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@ import { NativeModules } from "react-native";
44

55
const CustomTabsManager = NativeModules.CustomTabsManager;
66

7+
/**
8+
* Start and exit animations of Custom Tabs.
9+
* Slide in from left at start, Slide out to right.at exit.
10+
*/
11+
export const ANIMATIONS_SLIDE = {
12+
startEnter: CustomTabsManager.ANIMATION_SLIDE_IN_RIGHT,
13+
startExit: CustomTabsManager.ANIMATION_SLIDE_OUT_LEFT,
14+
endEnter: CustomTabsManager.ANIMATION_SLIDE_IN_LEFT,
15+
endExit: CustomTabsManager.ANIMATION_SLIDE_OUT_LEFT,
16+
};
17+
18+
/**
19+
* Start and exit animations of Custom Tabs.
20+
* Fade in at start, Fade out at exit.
21+
*/
22+
export const ANIMATIONS_FADE = {
23+
startEnter: CustomTabsManager.ANIMATION_FADE_IN,
24+
startExit: CustomTabsManager.ANIMATION_FADE_OUT,
25+
endEnter: CustomTabsManager.ANIMATION_FADE_IN,
26+
endExit: CustomTabsManager.ANIMATION_FADE_OUT,
27+
};
28+
729
/**
830
* To open the URL of the http or https in Chrome Custom Tabs.
931
* If Chrome is not installed, opens the URL in other browser.

src/CustomTabs.ios.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { NativeModules } from "react-native";
44

55
const ChromeManager = NativeModules.DBChromeManager;
66

7+
export const ANIMATIONS_SLIDE = {};
8+
export const ANIMATIONS_FADE = {};
9+
710
/**
811
* To open the URL of the http or https in Chrome.
912
* If Chrome is not installed, opens the URL in safari.

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
"use strict";
22

3-
export { default as CustomTabs } from "./CustomTabs";
4-
export { ANIMATIONS_SLIDE, ANIMATIONS_FADE } from "./Animations";
3+
export {
4+
default as CustomTabs,
5+
ANIMATIONS_SLIDE,
6+
ANIMATIONS_FADE,
7+
} from "./CustomTabs";

0 commit comments

Comments
 (0)