Skip to content

Commit

Permalink
Merge pull request eddieowens#16 from BrantApps/feat/jobintentservice…
Browse files Browse the repository at this point in the history
…_refactor

(Local) Broadcast the geofence events to the parent module
  • Loading branch information
eddieowens authored Mar 28, 2019
2 parents a6b6a43 + c1fcfdf commit 23c31d3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
36 changes: 35 additions & 1 deletion android/src/main/java/com/eddieowens/RNBoundaryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import android.Manifest;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.eddieowens.receivers.BoundaryEventBroadcastReceiver;
Expand All @@ -18,6 +22,7 @@
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingClient;
import com.google.android.gms.location.GeofencingRequest;
Expand All @@ -32,9 +37,11 @@
public class RNBoundaryModule extends ReactContextBaseJavaModule implements LifecycleEventListener {

public static final String TAG = "RNBoundary";
private GeofencingClient mGeofencingClient;
public static final String GEOFENCE_DATA_TO_EMIT = "com.eddieowens.GEOFENCE_DATA_TO_EMIT";

private GeofencingClient mGeofencingClient;
private PendingIntent mBoundaryPendingIntent;
private GeofenceDataChangedReceiver geofenceDataChangedReceiver = new GeofenceDataChangedReceiver();

RNBoundaryModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -242,13 +249,40 @@ public void onHostResume() {
if (mGeofencingClient == null) {
this.mGeofencingClient = LocationServices.getGeofencingClient(getReactApplicationContext());
}
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(GEOFENCE_DATA_TO_EMIT);
LocalBroadcastManager
.getInstance(this.getReactApplicationContext())
.registerReceiver(geofenceDataChangedReceiver, intentFilter);
}

@Override
public void onHostPause() {
LocalBroadcastManager.getInstance(this.getReactApplicationContext())
.unregisterReceiver(geofenceDataChangedReceiver);
}

@Override
public void onHostDestroy() {
}


private class GeofenceDataChangedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
final String event = intent.getStringExtra("event");
final ArrayList<String> geofenceIds = intent.getStringArrayListExtra("params");
final WritableArray geofenceIdsToBridge = Arguments.createArray();
for (String geofenceId : geofenceIds) {
geofenceIdsToBridge.pushString(geofenceId);
}

Log.i(TAG, "Sending events " + event);
RNBoundaryModule.this.getReactApplicationContext()
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(event, geofenceIdsToBridge);
Log.i(TAG, "Sent events");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.eddieowens.RNBoundaryModule;
import com.eddieowens.errors.GeofenceErrorMessages;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -13,6 +15,9 @@
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;

import java.util.ArrayList;
import java.util.List;

import static com.eddieowens.RNBoundaryModule.TAG;

public class BoundaryEventJobIntentService extends JobIntentService {
Expand All @@ -37,28 +42,29 @@ protected void onHandleWork(@NonNull Intent intent) {
switch (geofencingEvent.getGeofenceTransition()) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Log.i(TAG, "Enter geofence event detected. Sending event.");
WritableArray writableArray = Arguments.createArray();
final ArrayList<String> enteredGeofences = new ArrayList<>();
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
writableArray.pushString(geofence.getRequestId());
enteredGeofences.add(geofence.getRequestId());
}
sendEvent(ON_ENTER, writableArray);
sendEvent(ON_ENTER, enteredGeofences);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Log.i(TAG, "Exit geofence event detected. Sending event.");
WritableArray writableArray1 = Arguments.createArray();
final ArrayList<String> exitingGeofences = new ArrayList<>();
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
writableArray1.pushString(geofence.getRequestId());
exitingGeofences.add(geofence.getRequestId());
}
sendEvent(ON_EXIT, writableArray1);
sendEvent(ON_EXIT, exitingGeofences);
break;
}
}

private void sendEvent(String event, Object params) {
Log.i(TAG, "Sending events " + event);
((ReactApplicationContext) this.getBaseContext())
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(event, params);
Log.i(TAG, "Sent events");
private void sendEvent(String event, ArrayList<String> params) {
final LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(this.getApplicationContext());
final Intent intent = new Intent(RNBoundaryModule.GEOFENCE_DATA_TO_EMIT);
intent.putExtra("event", event);
intent.putExtra("params", params);
lbManager.sendBroadcast(intent);
}
}

0 comments on commit 23c31d3

Please sign in to comment.