Skip to content

Commit

Permalink
feat(android): Simplified the code and integrated job intent servcie …
Browse files Browse the repository at this point in the history
…to headless js. Also revved to 1.0.8.
  • Loading branch information
eddieowens committed May 19, 2019
1 parent 22a18f1 commit 02df608
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 52 deletions.
6 changes: 4 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application>
<!-- Services -->
<service
android:name=".services.BoundaryEventJobIntentService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
<service android:name="com.eddieowens.services.BoundaryEventHeadlessTaskService" />

<!-- Receivers -->
<receiver
android:name=".receivers.BoundaryEventBroadcastReceiver"
android:enabled="true"
android:exported="true" />
android:enabled="true" />
</application>
</manifest>

39 changes: 2 additions & 37 deletions android/src/main/java/com/eddieowens/RNBoundaryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@

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.os.Bundle;
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;
import com.eddieowens.services.BoundaryEventHeadlessTaskService;
import com.facebook.react.HeadlessJsTaskService;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
Expand All @@ -25,7 +18,6 @@
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 @@ -40,11 +32,12 @@
public class RNBoundaryModule extends ReactContextBaseJavaModule implements LifecycleEventListener {

public static final String TAG = "RNBoundary";
public static final String ON_ENTER = "onEnter";
public static final String ON_EXIT = "onExit";
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 @@ -249,41 +242,13 @@ public String getName() {

@Override
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");
Log.i(TAG, "Sending events " + event);
Bundle bundle = new Bundle();
bundle.putString("event", event);
bundle.putStringArrayList("ids", intent.getStringArrayListExtra("params"));

Intent headlessBoundaryIntent = new Intent(context, BoundaryEventHeadlessTaskService.class);
headlessBoundaryIntent.putExtras(bundle);

context.startService(headlessBoundaryIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
Log.i(TAG, "Sent events");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
package com.eddieowens.services;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.HeadlessJsTaskService;
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;

Expand Down Expand Up @@ -46,25 +43,32 @@ protected void onHandleWork(@NonNull Intent intent) {
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
enteredGeofences.add(geofence.getRequestId());
}
sendEvent(ON_ENTER, enteredGeofences);
sendEvent(this.getApplicationContext(), ON_ENTER, enteredGeofences);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Log.i(TAG, "Exit geofence event detected. Sending event.");
final ArrayList<String> exitingGeofences = new ArrayList<>();
for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) {
exitingGeofences.add(geofence.getRequestId());
}
sendEvent(ON_EXIT, exitingGeofences);
sendEvent(this.getApplicationContext(), ON_EXIT, exitingGeofences);
break;
}
}

private void sendEvent(String event, ArrayList<String> params) {
final LocalBroadcastManager lbManager =
LocalBroadcastManager.getInstance(this.getApplicationContext());
private void sendEvent(Context context, String event, ArrayList<String> params) {
final Intent intent = new Intent(RNBoundaryModule.GEOFENCE_DATA_TO_EMIT);
intent.putExtra("event", event);
intent.putExtra("params", params);
lbManager.sendBroadcast(intent);

Bundle bundle = new Bundle();
bundle.putString("event", event);
bundle.putStringArrayList("ids", intent.getStringArrayListExtra("params"));

Intent headlessBoundaryIntent = new Intent(context, BoundaryEventHeadlessTaskService.class);
headlessBoundaryIntent.putExtras(bundle);

context.startService(headlessBoundaryIntent);
HeadlessJsTaskService.acquireWakeLockNow(context);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-boundary",
"version": "1.0.7",
"version": "1.0.8",
"description": "native geofencing and region monitoring",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 02df608

Please sign in to comment.