5
5
import android .net .Uri ;
6
6
7
7
import androidx .annotation .ColorInt ;
8
- import androidx .annotation .NonNull ;
9
8
import androidx .browser .customtabs .CustomTabColorSchemeParams ;
10
9
import androidx .browser .customtabs .CustomTabsIntent ;
11
10
import androidx .core .graphics .ColorUtils ;
12
11
13
- import com .facebook .react .bridge .LifecycleEventListener ;
14
12
import com .facebook .react .bridge .Promise ;
15
13
import com .facebook .react .bridge .ReactApplicationContext ;
16
- import com .facebook .react .bridge .ReactContextBaseJavaModule ;
17
- import com .facebook .react .bridge .ReactMethod ;
18
- import com .facebook .react .bridge .ReadableMap ;
19
- import com .facebook .react .module .annotations .ReactModule ;
20
14
import com .facebook .react .modules .core .DeviceEventManagerModule .RCTDeviceEventEmitter ;
21
15
22
16
import io .swan .rnbrowser .helpers .CustomTabActivityHelper ;
23
17
24
- @ ReactModule (name = RNSwanBrowserModule .NAME )
25
- public class RNSwanBrowserModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
18
+ public class RNSwanBrowserModuleImpl {
26
19
27
20
static final String NAME = "RNSwanBrowser" ;
28
- private boolean mBrowserVisible = false ;
29
21
30
- public RNSwanBrowserModule (ReactApplicationContext reactContext ) {
31
- super (reactContext );
32
- reactContext .addLifecycleEventListener (this );
33
- }
34
-
35
- @ NonNull
36
- @ Override
37
- public String getName () {
38
- return NAME ;
39
- }
40
-
41
- @ Override
42
- public void onHostResume () {
43
- ReactApplicationContext reactContext = getReactApplicationContext ();
44
-
45
- if (mBrowserVisible && reactContext .hasActiveReactInstance ()) {
22
+ protected static void onHostResume (final ReactApplicationContext reactContext ) {
23
+ if (reactContext .hasActiveReactInstance ()) {
46
24
reactContext
47
25
.getJSModule (RCTDeviceEventEmitter .class )
48
26
.emit ("swanBrowserDidClose" , null );
49
27
}
50
-
51
- mBrowserVisible = false ;
52
28
}
53
29
54
- @ Override
55
- public void onHostPause () {}
56
-
57
- @ Override
58
- public void onHostDestroy () {}
59
-
60
- @ ReactMethod
61
- public void open (final String url , final ReadableMap options , final Promise promise ) {
62
- if (mBrowserVisible ) {
63
- promise .reject ("swan_browser_visible" ,
64
- "An instance of the swan browser is already visible" );
65
- }
66
-
67
- final Activity activity = getCurrentActivity ();
68
-
69
- if (activity == null ) {
70
- promise .reject ("no_current_activity" ,
71
- "Couldn't call open() when the app is in background" );
72
- return ;
73
- }
74
-
75
- mBrowserVisible = true ;
76
-
30
+ protected static void open (final Activity activity ,
31
+ final String url ,
32
+ final Double barTintColor ,
33
+ final Promise promise ) {
77
34
CustomTabsIntent .Builder intentBuilder = new CustomTabsIntent .Builder ();
78
35
intentBuilder .setShowTitle (false );
79
36
intentBuilder .setInstantAppsEnabled (false );
@@ -87,13 +44,13 @@ public void open(final String url, final ReadableMap options, final Promise prom
87
44
CustomTabColorSchemeParams .Builder paramsBuilder = new CustomTabColorSchemeParams .Builder ();
88
45
paramsBuilder .setNavigationBarColor (blackColor );
89
46
90
- if (options . hasKey ( " barTintColor" ) ) {
91
- @ ColorInt int barTintColor = options . getInt ( "barTintColor" );
47
+ if (barTintColor != null ) {
48
+ @ ColorInt int intValue = barTintColor . intValue ( );
92
49
93
- paramsBuilder .setToolbarColor (barTintColor );
94
- paramsBuilder .setSecondaryToolbarColor (barTintColor );
50
+ paramsBuilder .setToolbarColor (intValue );
51
+ paramsBuilder .setSecondaryToolbarColor (intValue );
95
52
96
- intentBuilder .setColorScheme (ColorUtils .calculateLuminance (barTintColor ) > 0.5
53
+ intentBuilder .setColorScheme (ColorUtils .calculateLuminance (intValue ) > 0.5
97
54
? CustomTabsIntent .COLOR_SCHEME_LIGHT
98
55
: CustomTabsIntent .COLOR_SCHEME_DARK );
99
56
}
@@ -114,19 +71,4 @@ public void openUri(Activity activity, Uri uri) {
114
71
115
72
promise .resolve (null );
116
73
}
117
-
118
- @ ReactMethod
119
- public void close () {
120
- // noop on Android since the modal is closed by deep-link
121
- }
122
-
123
- @ ReactMethod
124
- public void addListener (String eventName ) {
125
- // Set up any upstream listeners or background tasks as necessary
126
- }
127
-
128
- @ ReactMethod
129
- public void removeListeners (Integer count ) {
130
- // Remove upstream listeners, stop unnecessary background tasks
131
- }
132
74
}
0 commit comments