88import com .facebook .react .bridge .ReactApplicationContext ;
99import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1010import com .facebook .react .bridge .ReactMethod ;
11- import com .facebook .react .bridge .UiThreadUtil ;
1211import com .facebook .react .bridge .WritableMap ;
1312import com .facebook .react .bridge .WritableNativeMap ;
1413import com .facebook .react .module .annotations .ReactModule ;
2322@ ReactModule (name = TsAuthenticationModule .NAME )
2423public class TsAuthenticationModule extends ReactContextBaseJavaModule {
2524 public static final String NAME = "TsAuthentication" ;
25+ ReactApplicationContext reactContext ;
2626
2727 public TsAuthenticationModule (ReactApplicationContext reactContext ) {
2828 super (reactContext );
29+ this .reactContext = reactContext ;
2930 }
3031
3132 @ Override
@@ -36,17 +37,15 @@ public String getName() {
3637
3738 @ ReactMethod
3839 @ 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+ }
5049 }
5150
5251 // Registration
@@ -56,108 +55,91 @@ public void run() {
5655 String username ,
5756 String displayName ,
5857 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+ }
7871 }
79- });
72+ );
73+ }
8074 }
8175 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+ }
10493 }
105- } );
94+ );
10695 }
10796
10897 // Authentication
10998 @ ReactMethod
11099 @ 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 );
130110 }
131- );
132- }
133- });
111+
112+ @ Override
113+ public void error (@ NonNull AuthenticationError authenticationError ) {
114+ promise .reject (NAME , authenticationError .toString ());
115+ }
116+ }
117+ );
118+ }
119+ }
134120 }
135121
136122 @ ReactMethod
137123 @ 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+ }
159140 }
160- });
141+ );
142+ }
161143 }
162144}
163145
0 commit comments