Skip to content

Commit 988b79c

Browse files
author
Kevin Boulongne
committed
Revamped Handlers communication with RN.
Added Promises when possible, removed EventListener when possible.
1 parent efe44d5 commit 988b79c

File tree

2 files changed

+210
-99
lines changed

2 files changed

+210
-99
lines changed

android/src/main/java/com/reactlibrary/RNBubblesReactBridgeModule.java

Lines changed: 140 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55
import android.bluetooth.BluetoothAdapter;
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
8-
import android.net.Uri;
98
import android.os.Build.VERSION;
109
import android.os.Build.VERSION_CODES;
1110
import android.support.annotation.Nullable;
1211
import android.support.v4.app.ActivityCompat;
13-
import android.widget.Toast;
1412
import com.facebook.react.bridge.Arguments;
13+
import com.facebook.react.bridge.Callback;
1514
import com.facebook.react.bridge.ReactApplicationContext;
1615
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1716
import com.facebook.react.bridge.ReactMethod;
17+
import com.facebook.react.bridge.WritableArray;
1818
import com.facebook.react.bridge.WritableMap;
19+
import com.facebook.react.bridge.WritableNativeArray;
20+
import com.facebook.react.bridge.WritableNativeMap;
1921
import com.facebook.react.modules.core.DeviceEventManagerModule;
22+
import com.google.gson.GsonBuilder;
2023
import com.mybubbles.sdk.BuildConfig;
2124
import com.mybubbles.sdk.activities.ActivityAllowBluetooth;
2225
import com.mybubbles.sdk.instance.MyBubblesSDK;
@@ -29,9 +32,8 @@
2932
import com.mybubbles.sdk.observables.UniqueIdObservable;
3033
import com.mybubbles.sdk.utils.Const;
3134
import com.mybubbles.sdk.utils.ML;
32-
import java.util.HashMap;
35+
import java.util.Iterator;
3336
import java.util.List;
34-
import java.util.Map;
3537
import java.util.Observable;
3638
import java.util.Observer;
3739
import org.json.JSONArray;
@@ -56,13 +58,16 @@ public class RNBubblesReactBridgeModule extends ReactContextBaseJavaModule {
5658
}
5759
}
5860

59-
private static final String DURATION_SHORT_KEY = "SHORT";
60-
private static final String DURATION_LONG_KEY = "LONG";
61-
62-
public static boolean isReactUpToDate = false;
61+
private static final int CALLBACK_CODE_JSON_EXCEPTION = 0;
62+
private static final int CALLBACK_CODE_UNKNOWN_SERVICE = 1;
63+
private static final int CALLBACK_CODE_BRIDGE_VERSION = 2;
64+
private static final int CALLBACK_CODE_BLUETOOTH_ERROR = 3;
65+
private static final int CALLBACK_CODE_BLUETOOTH_ON = 4;
6366

6467
private final ReactApplicationContext reactContext;
6568

69+
public static boolean isReactUpToDate = false;
70+
6671
public RNBubblesReactBridgeModule(ReactApplicationContext reactContext) {
6772
super(reactContext);
6873
this.reactContext = reactContext;
@@ -78,15 +83,6 @@ public String getName() {
7883
return "BubblesReactBridge";
7984
}
8085

81-
// Optional
82-
@Override
83-
public Map<String, Object> getConstants() {
84-
final Map<String, Object> constants = new HashMap<>();
85-
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
86-
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
87-
return constants;
88-
}
89-
9086
////////////////////////////////////////////////////////////////////////////////////////////////////
9187
///////////////////////////////// CALL FROM JAVASCRIPT TO PHONE OS /////////////////////////////////
9288
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -102,15 +98,13 @@ public void log(String data) {
10298
}
10399

104100
@ReactMethod
105-
public void getBeaconsAround() {
101+
public void getBeaconsAround(Callback callback) {
106102
log("getBeaconsAround");
107103

108-
List<MyBeacon> beaconsList = MyBubblesSDK.getInstance().getCurrentBeacons();
109-
110-
String ret;
111104
try {
112105
JSONObject result = new JSONObject();
113106
JSONArray beacons = new JSONArray();
107+
List<MyBeacon> beaconsList = MyBubblesSDK.getInstance().getCurrentBeacons();
114108
for (int i = 0; i < beaconsList.size(); i++) {
115109
MyBeacon beacon = beaconsList.get(i);
116110
JSONObject beaconJson = new JSONObject();
@@ -121,18 +115,10 @@ public void getBeaconsAround() {
121115
beacons.put(beaconJson);
122116
}
123117
result.put("beacons", beacons);
124-
result.put("success", true);
125-
ret = result.toString();
118+
callback.invoke(null, result.toString());
126119
} catch (JSONException e) {
127-
ret = "{'success':false}";
120+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
128121
}
129-
130-
log("list : " + ret);
131-
132-
WritableMap params = Arguments.createMap();
133-
params.putString("beaconsList", ret);
134-
135-
sendEvent("getBeaconsAround", params);
136122
}
137123

138124
@ReactMethod
@@ -146,37 +132,20 @@ public void closeService() {
146132
}
147133

148134
@ReactMethod
149-
public void openURI(String uri) {
150-
log("openURI");
151-
152-
ML.e(TAG, "jsonData : " + uri);
135+
public void getBluetoothState(Callback callback) {
136+
log("getBluetoothState");
153137

154138
try {
155-
JSONObject jsonData = new JSONObject(uri);
156-
uri = jsonData.getString("uri");
139+
JSONObject result = new JSONObject();
140+
result.put("isActivated", getPhoneBluetoothState());
141+
callback.invoke(null, result.toString());
157142
} catch (JSONException e) {
158-
return;
143+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
159144
}
160-
161-
// Otherwise, the link is not for a page on our Service, so launch another Activity that handles URLs
162-
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
163-
Activity activity = getCurrentActivity();
164-
if (activity != null) {
165-
activity.startActivity(intent);
166-
}
167-
}
168-
169-
@ReactMethod
170-
public void getBluetoothState() {
171-
log("getBluetoothState");
172-
173-
WritableMap params = Arguments.createMap();
174-
params.putBoolean("isActivated", getPhoneBluetoothState());
175-
sendEvent("getBluetoothState", params);
176145
}
177146

178147
@ReactMethod
179-
public void getLocalizationPermissionState() {
148+
public void getLocalizationPermissionState(Callback callback) {
180149
log("getLocalizationPermissionState");
181150

182151
boolean isAuthorized = true;
@@ -185,9 +154,11 @@ public void getLocalizationPermissionState() {
185154
isAuthorized = false;
186155
}
187156

188-
WritableMap params = Arguments.createMap();
189-
params.putBoolean("isAuthorized", isAuthorized);
190-
sendEvent("getLocalizationPermissionState", params);
157+
try {
158+
callback.invoke(null, new JSONObject().put("isAuthorized", isAuthorized).toString());
159+
} catch (JSONException e) {
160+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
161+
}
191162
}
192163

193164
@ReactMethod
@@ -216,11 +187,9 @@ public void askForLocalizationPermission() {
216187
}
217188

218189
@ReactMethod
219-
public void getServices() {
190+
public void getServices(Callback callback) {
220191
log("getServices");
221192

222-
WritableMap params = Arguments.createMap();
223-
224193
try {
225194
JSONObject result = new JSONObject();
226195
JSONArray services = new JSONArray();
@@ -246,13 +215,11 @@ public void getServices() {
246215
services.put(jsonService);
247216

248217
result.put("services", services);
249-
result.put("success", true);
250-
params.putString("result", result.toString());
218+
callback.invoke(null, result.toString());
251219

252220
} catch (JSONException e) {
253-
params.putString("result", DEFAULT_FAILED_HANDLER_RETURN);
221+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
254222
}
255-
sendEvent("getServices", params);
256223
}
257224

258225
@ReactMethod
@@ -263,22 +230,19 @@ public void fetchServices() {
263230
}
264231

265232
@ReactMethod
266-
public void openService(String serviceId) {
233+
public void openService(String serviceId, Callback callback) {
267234
log("openService");
268235

269236
log("jsonData : " + serviceId);
270237

271-
WritableMap params = Arguments.createMap();
272238
String ret;
273239

274240
String id;
275241
try {
276242
JSONObject jsonData = new JSONObject(serviceId);
277243
id = jsonData.getString("service_id");
278244
} catch (JSONException e) {
279-
ret = createFormattableFailedReturn(e.getMessage());
280-
params.putString("callback", ret);
281-
sendEvent("openService", params);
245+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
282246
return;
283247
}
284248

@@ -290,9 +254,7 @@ public void openService(String serviceId) {
290254
}
291255
}
292256
if (!flag) {
293-
ret = createFormattableFailedReturn("unknown service");
294-
params.putString("callback", ret);
295-
sendEvent("openService", params);
257+
callback.invoke(createRejectCallback(CALLBACK_CODE_UNKNOWN_SERVICE, "unknown service"), null);
296258
return;
297259
}
298260

@@ -302,25 +264,44 @@ public void openService(String serviceId) {
302264
}
303265

304266
ret = DEFAULT_SUCCESS_HANDLER_RETURN;
305-
params.putString("callback", ret);
306-
sendEvent("openService", params);
267+
callback.invoke(null, ret);
307268
}
308269

309270
@ReactMethod
310-
public void getVersion() {
271+
public void getVersion(Callback callback) {
311272
log("getVersion");
312273

313-
WritableMap params = Arguments.createMap();
314-
params.putString("version", BuildConfig.BRIDGE_VERSION);
315-
sendEvent("getVersion", params);
274+
String version = BuildConfig.BRIDGE_VERSION;
275+
if (ML.isValidStr(version)) {
276+
try {
277+
callback.invoke(null, new JSONObject().put("version", version).toString());
278+
} catch (JSONException e) {
279+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
280+
}
281+
} else {
282+
callback.invoke(createRejectCallback(CALLBACK_CODE_BRIDGE_VERSION, "bridge version not found"), null);
283+
}
316284
}
317285

318-
319286
@ReactMethod
320-
public void enableBluetooth() {
287+
public void enableBluetooth(Callback callback) {
321288
log("enableBluetooth");
322289

323-
MyBubblesSDK.getInstance().enableBluetooth();
290+
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
291+
if (bluetoothAdapter != null) {
292+
if (!bluetoothAdapter.isEnabled()) {
293+
bluetoothAdapter.enable();
294+
try {
295+
callback.invoke(null, new JSONObject().put("enabled", true).toString());
296+
} catch (JSONException e) {
297+
callback.invoke(createRejectCallback(CALLBACK_CODE_JSON_EXCEPTION, e.getMessage()), null);
298+
}
299+
return;
300+
}
301+
callback.invoke(createRejectCallback(CALLBACK_CODE_BLUETOOTH_ON, "bluetooth is already activated"), null);
302+
return;
303+
}
304+
callback.invoke(createRejectCallback(CALLBACK_CODE_BLUETOOTH_ERROR, "bluetooth activation failed"), null);
324305
}
325306

326307
////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -446,6 +427,11 @@ private void sendEvent(String eventName, @Nullable WritableMap params) {
446427
.emit(eventName, params);
447428
}
448429

430+
private boolean getPhoneBluetoothState() {
431+
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
432+
return adapter != null && adapter.isEnabled();
433+
}
434+
449435
private String createFormattableFailedReturn(final String message) {
450436
try {
451437
return FORMATTABLE_FAILED_HANDLER_RETURN.put("error", message).toString();
@@ -455,8 +441,77 @@ private String createFormattableFailedReturn(final String message) {
455441
return null;
456442
}
457443

458-
private boolean getPhoneBluetoothState() {
459-
final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
460-
return adapter != null && adapter.isEnabled();
444+
private WritableMap createRejectCallback(int code, String message) {
445+
ErrorObject errorObject = new ErrorObject(code, message);
446+
WritableMap errorMap = null;
447+
try {
448+
errorMap = convertJsonToMap(new JSONObject(errorObject.toJsonString()));
449+
} catch (JSONException e) {
450+
e.printStackTrace();
451+
}
452+
return errorMap;
453+
}
454+
455+
private class ErrorObject {
456+
457+
private int code;
458+
private String message;
459+
460+
public ErrorObject(final int code, final String message) {
461+
this.code = code;
462+
this.message = message;
463+
}
464+
465+
public String toJsonString() {
466+
return new GsonBuilder().create().toJson(this);
467+
}
468+
}
469+
470+
private static WritableMap convertJsonToMap(JSONObject jsonObject) throws JSONException {
471+
WritableMap map = new WritableNativeMap();
472+
Iterator<String> iterator = jsonObject.keys();
473+
while (iterator.hasNext()) {
474+
String key = iterator.next();
475+
Object value = jsonObject.get(key);
476+
if (value instanceof JSONObject) {
477+
map.putMap(key, convertJsonToMap((JSONObject) value));
478+
} else if (value instanceof JSONArray) {
479+
map.putArray(key, convertJsonToArray((JSONArray) value));
480+
} else if (value instanceof Boolean) {
481+
map.putBoolean(key, (Boolean) value);
482+
} else if (value instanceof Integer) {
483+
map.putInt(key, (Integer) value);
484+
} else if (value instanceof Double) {
485+
map.putDouble(key, (Double) value);
486+
} else if (value instanceof String) {
487+
map.putString(key, (String) value);
488+
} else {
489+
map.putString(key, value.toString());
490+
}
491+
}
492+
return map;
493+
}
494+
495+
private static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException {
496+
WritableArray array = new WritableNativeArray();
497+
for (int i = 0; i < jsonArray.length(); i++) {
498+
Object value = jsonArray.get(i);
499+
if (value instanceof JSONObject) {
500+
array.pushMap(convertJsonToMap((JSONObject) value));
501+
} else if (value instanceof JSONArray) {
502+
array.pushArray(convertJsonToArray((JSONArray) value));
503+
} else if (value instanceof Boolean) {
504+
array.pushBoolean((Boolean) value);
505+
} else if (value instanceof Integer) {
506+
array.pushInt((Integer) value);
507+
} else if (value instanceof Double) {
508+
array.pushDouble((Double) value);
509+
} else if (value instanceof String) {
510+
array.pushString((String) value);
511+
} else {
512+
array.pushString(value.toString());
513+
}
514+
}
515+
return array;
461516
}
462517
}

0 commit comments

Comments
 (0)