8
8
import com .facebook .react .bridge .ReactApplicationContext ;
9
9
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
10
10
import com .facebook .react .bridge .ReactMethod ;
11
- import com .facebook .react .bridge .UiThreadUtil ;
12
11
import com .facebook .react .bridge .WritableMap ;
13
12
import com .facebook .react .bridge .WritableNativeMap ;
14
13
import com .facebook .react .module .annotations .ReactModule ;
23
22
@ ReactModule (name = TsAuthenticationModule .NAME )
24
23
public class TsAuthenticationModule extends ReactContextBaseJavaModule {
25
24
public static final String NAME = "TsAuthentication" ;
25
+ ReactApplicationContext reactContext ;
26
26
27
27
public TsAuthenticationModule (ReactApplicationContext reactContext ) {
28
28
super (reactContext );
29
+ this .reactContext = reactContext ;
29
30
}
30
31
31
32
@ Override
@@ -36,17 +37,15 @@ public String getName() {
36
37
37
38
@ ReactMethod
38
39
@ NonNull public void initialize (String clientId , String domain , String baseUrl , Promise promise ) {
39
- UiThreadUtil .runOnUiThread (
40
- new Runnable () {
41
- @ Override
42
- public void run () {
43
- TSAuthentication .init (getReactApplicationContext (),
44
- baseUrl ,
45
- clientId
46
- );
47
- promise .resolve (true );
48
- }
49
- });
40
+
41
+ if (reactContext .getCurrentActivity () != null ) {
42
+ TSAuthentication .init (
43
+ reactContext ,
44
+ baseUrl ,
45
+ clientId
46
+ );
47
+ promise .resolve (true );
48
+ }
50
49
}
51
50
52
51
// Registration
@@ -56,108 +55,91 @@ public void run() {
56
55
String username ,
57
56
String displayName ,
58
57
Promise promise ) {
59
-
60
- UiThreadUtil .runOnUiThread (
61
- new Runnable () {
62
- @ Override
63
- public void run () {
64
- TSAuthentication .isPlatformAuthenticatorSupported (
65
- getReactApplicationContext (),
66
- new TSAuthCallback <Boolean >() {
67
- @ Override
68
- public void success (Boolean aBoolean ) {
69
- continueRegistration (username , displayName , promise );
70
- }
71
-
72
- @ Override
73
- public void error (@ NonNull AuthenticationError authenticationError ) {
74
- promise .reject (new Error ("Unsupported platform" ));
75
- }
76
- }
77
- );
58
+ if (reactContext .getCurrentActivity () != null ) {
59
+ TSAuthentication .isPlatformAuthenticatorSupported (
60
+ reactContext .getCurrentActivity (),
61
+ new TSAuthCallback <Boolean >() {
62
+ @ Override
63
+ public void success (Boolean aBoolean ) {
64
+ continueRegistration (username , displayName , promise );
65
+ }
66
+
67
+ @ Override
68
+ public void error (@ NonNull AuthenticationError authenticationError ) {
69
+ promise .reject (new Error ("Unsupported platform" ));
70
+ }
78
71
}
79
- });
72
+ );
73
+ }
80
74
}
81
75
private void continueRegistration (String username , String displayName , Promise promise ) {
82
- UiThreadUtil .runOnUiThread (
83
- new Runnable () {
84
- @ Override
85
- public void run () {
86
- TSAuthentication .register (
87
- getReactApplicationContext (),
88
- username ,
89
- displayName ,
90
- new TSAuthCallback <RegistrationResult >() {
91
- @ Override
92
- public void success (RegistrationResult registrationResult ) {
93
- WritableMap map = new WritableNativeMap ();
94
- map .putString (registrationResult .result (), NAME );
95
- promise .resolve (map );
96
- }
97
-
98
- @ Override
99
- public void error (@ NonNull AuthenticationError authenticationError ) {
100
- promise .reject (NAME , authenticationError .toString ());
101
- }
102
- }
103
- );
76
+ if (reactContext .getCurrentActivity () != null ) {
77
+ TSAuthentication .register (
78
+ reactContext .getCurrentActivity (),
79
+ username ,
80
+ displayName ,
81
+ new TSAuthCallback <RegistrationResult >() {
82
+ @ Override
83
+ public void success (RegistrationResult registrationResult ) {
84
+ WritableMap map = new WritableNativeMap ();
85
+ map .putString (registrationResult .result (), NAME );
86
+ promise .resolve (map );
87
+ }
88
+
89
+ @ Override
90
+ public void error (@ NonNull AuthenticationError authenticationError ) {
91
+ promise .reject (NAME , authenticationError .toString ());
92
+ }
104
93
}
105
- } );
94
+ );
106
95
}
107
96
108
97
// Authentication
109
98
@ ReactMethod
110
99
@ NonNull public void authenticate (String username , Promise promise ) {
111
- UiThreadUtil .runOnUiThread (
112
- new Runnable () {
113
- @ Override
114
- public void run () {
115
- TSAuthentication .authenticate (
116
- getReactApplicationContext (),
117
- username ,
118
- new TSAuthCallback <AuthenticationResult >() {
119
- @ Override
120
- public void success (AuthenticationResult authenticationResult ) {
121
- WritableMap map = new WritableNativeMap ();
122
- map .putString (authenticationResult .result (), NAME );
123
- promise .resolve (map );
124
- }
125
-
126
- @ Override
127
- public void error (@ NonNull AuthenticationError authenticationError ) {
128
- promise .reject (NAME , authenticationError .toString ());
129
- }
100
+ if (reactContext .getCurrentActivity () != null ) {
101
+ TSAuthentication .authenticate (
102
+ reactContext .getCurrentActivity (),
103
+ username ,
104
+ new TSAuthCallback <AuthenticationResult >() {
105
+ @ Override
106
+ public void success (AuthenticationResult authenticationResult ) {
107
+ WritableMap map = new WritableNativeMap ();
108
+ map .putString (authenticationResult .result (), NAME );
109
+ promise .resolve (map );
130
110
}
131
- );
132
- }
133
- });
111
+
112
+ @ Override
113
+ public void error (@ NonNull AuthenticationError authenticationError ) {
114
+ promise .reject (NAME , authenticationError .toString ());
115
+ }
116
+ }
117
+ );
118
+ }
119
+ }
134
120
}
135
121
136
122
@ ReactMethod
137
123
@ NonNull public void signTransaction (String username , Promise promise ) {
138
- UiThreadUtil .runOnUiThread (
139
- new Runnable () {
140
- @ Override
141
- public void run () {
142
- TSAuthentication .signTransaction (
143
- getReactApplicationContext (),
144
- username ,
145
- new TSAuthCallback <AuthenticationResult >() {
146
- @ Override
147
- public void success (AuthenticationResult authenticationResult ) {
148
- WritableMap map = new WritableNativeMap ();
149
- map .putString (authenticationResult .result (), NAME );
150
- promise .resolve (map );
151
- }
152
-
153
- @ Override
154
- public void error (@ NonNull AuthenticationError authenticationError ) {
155
- promise .reject (NAME , authenticationError .toString ());
156
- }
157
- }
158
- );
124
+ if (reactContext .getCurrentActivity () != null ) {
125
+ TSAuthentication .signTransaction (
126
+ reactContext .getCurrentActivity (),
127
+ username ,
128
+ new TSAuthCallback <AuthenticationResult >() {
129
+ @ Override
130
+ public void success (AuthenticationResult authenticationResult ) {
131
+ WritableMap map = new WritableNativeMap ();
132
+ map .putString (authenticationResult .result (), NAME );
133
+ promise .resolve (map );
134
+ }
135
+
136
+ @ Override
137
+ public void error (@ NonNull AuthenticationError authenticationError ) {
138
+ promise .reject (NAME , authenticationError .toString ());
139
+ }
159
140
}
160
- });
141
+ );
142
+ }
161
143
}
162
144
}
163
145
0 commit comments