99import com .facebook .react .bridge .ReactMethod ;
1010import com .google .gson .Gson ;
1111import com .google .gson .reflect .TypeToken ;
12+ import com .facebook .react .bridge .Promise ;
1213
1314import java .util .HashMap ;
1415import java .util .Map ;
1920import ai .api .android .AIConfiguration ;
2021import ai .api .android .AIDataService ;
2122import ai .api .android .AIService ;
23+ import ai .api .android .SessionIdStorage ;
2224import ai .api .model .AIError ;
2325import ai .api .model .AIRequest ;
2426import ai .api .model .AIResponse ;
@@ -69,6 +71,8 @@ public class RNApiAiModule extends ReactContextBaseJavaModule implements AIListe
6971 private Callback onListeningFinishedCallback ;
7072 private Callback onAudioLevelCallback ;
7173
74+ private String accessToken ;
75+
7276
7377 public RNApiAiModule (ReactApplicationContext reactContext ) {
7478 super (reactContext );
@@ -82,25 +86,28 @@ public String getName() {
8286
8387 @ ReactMethod
8488 public void setConfiguration (String clientAccessToken , String languageTag ) {
89+ this .accessToken = clientAccessToken ;
8590 config = new AIConfiguration (clientAccessToken , AIConfiguration .SupportedLanguages .fromLanguageTag (languageTag ), AIConfiguration .RecognitionEngine .System );
8691 }
8792
8893 @ ReactMethod
8994 public void setContextsAsJson (String contextsAsJson ) {
90- Gson gson = new Gson ();
91- contexts = gson .fromJson (contextsAsJson , new TypeToken <List <Entity >>(){}.getType ());
95+ Gson gson = new Gson ();
96+ contexts = gson .fromJson (contextsAsJson , new TypeToken <List <Entity >>() {
97+ }.getType ());
9298 }
9399
94100
95101 @ ReactMethod
96102 public void setEntitiesAsJson (String userEntitiesAsJson ) throws AIServiceException {
97- Gson gson = new Gson ();
98- entities = gson .fromJson (userEntitiesAsJson , new TypeToken <List <Entity >>(){}.getType ());
103+ Gson gson = new Gson ();
104+ entities = gson .fromJson (userEntitiesAsJson , new TypeToken <List <Entity >>() {
105+ }.getType ());
99106 }
100107
101108
102109 @ ReactMethod
103- public void startListening (Callback onResult , Callback onError ){//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
110+ public void startListening (Callback onResult , Callback onError ) {//, Callback onListeningStarted, Callback onListeningCanceled, Callback onListeningFinished, Callback onAudioLevel) {
104111
105112 onResultCallback = onResult ;
106113 onErrorCallback = onError ;
@@ -135,9 +142,9 @@ public void stopListening() {
135142
136143 public void run () {
137144
138- if (aiService != null ) {
139- aiService .stopListening ();
140- }
145+ if (aiService != null ) {
146+ aiService .stopListening ();
147+ }
141148 }
142149 });
143150 }
@@ -157,7 +164,6 @@ public void run() {
157164 }
158165
159166
160-
161167 @ Override
162168 public void onResult (AIResponse response ) {
163169
@@ -267,7 +273,6 @@ public void requestQuery(String query, Callback onResult, Callback onError) {
267273 aiRequest .setQuery (query );
268274
269275
270-
271276 new AsyncTask <AIRequest , Void , AIResponse >() {
272277 @ Override
273278 protected AIResponse doInBackground (AIRequest ... requests ) {
@@ -290,7 +295,7 @@ protected AIResponse doInBackground(AIRequest... requests) {
290295
291296 return response ;
292297 } catch (AIServiceException e ) {
293- Gson gson = new Gson ();
298+ Gson gson = new Gson ();
294299 try {
295300 onErrorCallback .invoke (gson .toJson (e ));
296301 } catch (Exception e1 ) {
@@ -299,6 +304,7 @@ protected AIResponse doInBackground(AIRequest... requests) {
299304 }
300305 return null ;
301306 }
307+
302308 @ Override
303309 protected void onPostExecute (AIResponse aiResponse ) {
304310 if (aiResponse != null ) {
@@ -308,6 +314,16 @@ protected void onPostExecute(AIResponse aiResponse) {
308314 }.execute (aiRequest );
309315 }
310316
317+ @ ReactMethod
318+ public void getAccessToken (Promise promise ) {
319+ promise .resolve (accessToken );
320+ }
321+
322+ @ ReactMethod
323+ public void getSessionId (Promise promise ) {
324+ promise .resolve (SessionIdStorage .getSessionId (getReactApplicationContext ()));
325+ }
326+
311327 @ Override
312328 public Map <String , Object > getConstants () {
313329 final Map <String , Object > constants = new HashMap <>();
0 commit comments