8
8
import android .os .Build .VERSION ;
9
9
import android .os .Build .VERSION_CODES ;
10
10
import android .support .v4 .app .ActivityCompat ;
11
- import com . facebook . react . bridge . Arguments ;
11
+ import android . util . Log ;
12
12
import com .facebook .react .bridge .Callback ;
13
13
import com .facebook .react .bridge .ReactApplicationContext ;
14
14
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
41
41
42
42
public class RNBubblesReactBridgeModule extends ReactContextBaseJavaModule {
43
43
44
- private static final String TAG = "BubblesReactBridge " ;
44
+ private static final String TAG = "RNBubblesReactBridge " ;
45
45
46
46
private static String DEFAULT_SUCCESS_HANDLER_RETURN = null ;
47
47
private static String DEFAULT_FAILED_HANDLER_RETURN = null ;
@@ -57,11 +57,13 @@ public class RNBubblesReactBridgeModule extends ReactContextBaseJavaModule {
57
57
}
58
58
}
59
59
60
- private static final int CALLBACK_CODE_JSON_EXCEPTION = 0 ;
61
- private static final int CALLBACK_CODE_UNKNOWN_SERVICE = 1 ;
62
- private static final int CALLBACK_CODE_BRIDGE_VERSION = 2 ;
63
- private static final int CALLBACK_CODE_BLUETOOTH_ERROR = 3 ;
64
- private static final int CALLBACK_CODE_BLUETOOTH_ON = 4 ;
60
+ private static final String CALLBACK_CODE_JSON_EXCEPTION = "JSON_EXCEPTION" ;
61
+ private static final String CALLBACK_CODE_UNKNOWN_SERVICE = "UNKNOWN_SERVICE" ;
62
+ private static final String CALLBACK_CODE_BRIDGE_VERSION = "UNKNOWN_BRIDGE_VERSION" ;
63
+ private static final String CALLBACK_CODE_BLUETOOTH_ERROR = "BLUETOOTH_ERROR" ;
64
+ private static final String CALLBACK_CODE_BLUETOOTH_ON = "BLUETOOTH_ON" ;
65
+ private static final String CALLBACK_CODE_PERMISSION_REQUIRED = "PERMISSION_REQUIRED" ;
66
+ private static final String CALLBACK_CODE_INTERNAL_ERROR = "INTERNAL_ERROR" ;
65
67
66
68
private final ReactApplicationContext reactContext ;
67
69
@@ -79,7 +81,7 @@ public RNBubblesReactBridgeModule(ReactApplicationContext reactContext) {
79
81
80
82
@ Override
81
83
public String getName () {
82
- return "BubblesReactBridge " ;
84
+ return "RNBubblesReactBridge " ;
83
85
}
84
86
85
87
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -94,6 +96,7 @@ public void reactIsUpToDate() {
94
96
@ ReactMethod
95
97
public void log (String data ) {
96
98
ML .e (TAG , data );
99
+ Log .e (TAG , data );
97
100
}
98
101
99
102
@ ReactMethod
@@ -160,6 +163,71 @@ public void getLocalizationPermissionState(Callback callback) {
160
163
}
161
164
}
162
165
166
+ @ ReactMethod
167
+ public void getDeviceId (Callback callback ) {
168
+ String deviceID = MyBubblesSDK .mInstance .deviceID ;
169
+ if (deviceID != null ) {
170
+ try {
171
+ JSONObject result = new JSONObject ();
172
+ result .put ("deviceId" , deviceID );
173
+ callback .invoke (null , result .toString ());
174
+ } catch (JSONException e ) {
175
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
176
+ }
177
+ } else {
178
+ callback .invoke (createRejectCallback (CALLBACK_CODE_INTERNAL_ERROR , "Is SDK init?" ), null );
179
+ }
180
+ }
181
+
182
+ @ ReactMethod
183
+ public void getApplicationId (Callback callback ) {
184
+ String apiKey = MyBubblesSDK .mInstance .apiKey ;
185
+ if (apiKey != null ) {
186
+ try {
187
+ JSONObject result = new JSONObject ();
188
+ result .put ("applicationId" , apiKey );
189
+ callback .invoke (null , result .toString ());
190
+ } catch (JSONException e ) {
191
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
192
+ }
193
+ } else {
194
+ callback .invoke (createRejectCallback (CALLBACK_CODE_INTERNAL_ERROR , "Is SDK init?" ), null );
195
+ }
196
+ }
197
+
198
+ @ ReactMethod
199
+ public void getUserId (Callback callback ) {
200
+ String userID = MyBubblesSDK .mInstance .userID ;
201
+ if (userID != null ) {
202
+ try {
203
+ JSONObject result = new JSONObject ();
204
+ result .put ("userId" , userID );
205
+ callback .invoke (null , result .toString ());
206
+ } catch (JSONException e ) {
207
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
208
+ }
209
+ } else {
210
+ callback .invoke (createRejectCallback (CALLBACK_CODE_INTERNAL_ERROR , "Is SDK init?" ), null );
211
+ }
212
+ }
213
+
214
+ @ ReactMethod
215
+ public void getUniqueDeviceId (Callback callback ) {
216
+ String uniqueID = MyBubblesSDK .mInstance .uniqueID ;
217
+ if (uniqueID != null ) {
218
+ try {
219
+ JSONObject result = new JSONObject ();
220
+ result .put ("isAuthorized" , true );
221
+ result .put ("uniqueDeviceId" , uniqueID );
222
+ callback .invoke (null , result .toString ());
223
+ } catch (JSONException e ) {
224
+ callback .invoke (createRejectCallback (CALLBACK_CODE_JSON_EXCEPTION , e .getMessage ()), null );
225
+ }
226
+ } else {
227
+ callback .invoke (createRejectCallback (CALLBACK_CODE_PERMISSION_REQUIRED , "Permission required" ), null );
228
+ }
229
+ }
230
+
163
231
@ ReactMethod
164
232
public void askForUniqueIdPermission () {
165
233
log ("askForUniqueIdPermission" );
@@ -359,9 +427,14 @@ public void update(Observable observable, Object data) {
359
427
try {
360
428
361
429
log ("onSendUniqueId" );
430
+ boolean isAuthorized = (Boolean ) data ;
362
431
363
432
JSONObject result = new JSONObject ();
364
- result .put ("isAuthorized" , data );
433
+ result .put ("isAuthorized" , isAuthorized );
434
+ if (isAuthorized ) {
435
+ result .put ("uniqueDeviceId" , MyBubblesSDK .mInstance .uniqueID );
436
+ }
437
+
365
438
sendEvent ("onSendUniqueId" , result .toString ());
366
439
367
440
} catch (JSONException e ) {
@@ -458,7 +531,7 @@ private String createFormattableFailedReturn(final String message) {
458
531
return null ;
459
532
}
460
533
461
- private WritableMap createRejectCallback (int code , String message ) {
534
+ private WritableMap createRejectCallback (String code , String message ) {
462
535
ErrorObject errorObject = new ErrorObject (code , message );
463
536
WritableMap errorMap = null ;
464
537
try {
@@ -471,10 +544,10 @@ private WritableMap createRejectCallback(int code, String message) {
471
544
472
545
private class ErrorObject {
473
546
474
- private int code ;
547
+ private String code ;
475
548
private String message ;
476
549
477
- public ErrorObject (final int code , final String message ) {
550
+ public ErrorObject (final String code , final String message ) {
478
551
this .code = code ;
479
552
this .message = message ;
480
553
}
0 commit comments