Skip to content

Commit

Permalink
Merge pull request transistorsoft#294 from transistorsoft/android-onD…
Browse files Browse the repository at this point in the history
…estroy

Refactor Android onDestroy; Implementing Android Location Authorization
  • Loading branch information
christocracy committed Sep 13, 2017
2 parents 15fb319 + 5baf028 commit 9e0db9b
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

- [Changed] Refactor Android `onDestroy` mechanism attempting to solve nagging and un-reproducible null pointer exceptions.
- [Added] Implement Android location permissions handling using `PermissionsAndroid` API. You no longer need to use 3rd-party permissions module to obtain Android location permission.
- [Fixed] Fixed bug not where `stopAfterElapsedMinutes` is not evaluated when executing `#getCurrentPosition`.
- [Fixed] Modifications for Android O. For now, `foregroundService: true` will be enforced when running on Android O (api 26).

## [2.9.2] - 2017-08-28
- [Changed] iOS `motionchange` position will be fetch by `CLLocationManager#startUpdatingLocation` rather than `#requestLocation`, since `#requestLocation` cannot keep the app alive in the background. This could cause the app to be suspended when `motionchange` position was requested due to a background-fetch event.
- [Fixed] Android `stopOnTerminate` was not setting the `enabled` value to `false` when terminated. This caused the plugin to automatically `#start` the first time the app was booted (it would work correctly every boot thereafter).
Expand Down
51 changes: 39 additions & 12 deletions RNBackgroundGeolocation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

import {
NativeEventEmitter,
NativeModules,
Expand All @@ -11,13 +13,21 @@ const TAG = "TSLocationManager";
const PLATFORM_ANDROID = "android";
const PLATFORM_IOS = "ios";

// Android permissions handler. iOS manages this automatically within TSLocationManager
let permissionsHandler = null;

function withPermission(success, failure) {
if (!permissionsHandler) { return success(); }
permissionsHandler(success, failure);
}

let emptyFn = function() {};

/**
* Client log method
*/
function log(level, msg) {
RNBackgroundGeolocation.log(level, msg);
RNBackgroundGeolocation.log(level, msg);
}

let API = {
Expand Down Expand Up @@ -59,7 +69,10 @@ let API = {
NOTIFICATION_PRIORITY_LOW: -1,
NOTIFICATION_PRIORITY_MAX: 2,
NOTIFICATION_PRIORITY_MIN: -2,


setPermissionsHandler(handler) {
permissionsHandler = handler;
},
configure: function(config, success, failure) {
success = success || emptyFn;
failure = failure || emptyFn;
Expand Down Expand Up @@ -115,10 +128,12 @@ let API = {
un: function(event, callback) {
this.removeListener(event, callback);
},
start: function(success, failure) {
start: async function(success, failure) {
success = success || emptyFn;
failure = failure || emptyFn;
RNBackgroundGeolocation.start(success, failure);
withPermission(() => {
RNBackgroundGeolocation.start(success, failure);
}, failure);
},
stop: function(success, failure) {
success = success || emptyFn;
Expand All @@ -128,7 +143,10 @@ let API = {
startSchedule: function(success, failure) {
success = success || emptyFn;
failure = failure || emptyFn;
RNBackgroundGeolocation.startSchedule(success, failure);

withPermission(() => {
RNBackgroundGeolocation.startSchedule(success, failure);
}, failure);
},
stopSchedule: function(success, failure) {
success = success || emptyFn;
Expand All @@ -138,7 +156,10 @@ let API = {
startGeofences: function(success, failure) {
success = success || emptyFn;
failure = failure || emptyFn;
RNBackgroundGeolocation.startGeofences(success, failure);

withPermission(() => {
RNBackgroundGeolocation.startGeofences(success, failure);
}, failure);
},
onHttp: function(callback) {
return EventEmitter.addListener("http", callback);
Expand Down Expand Up @@ -188,7 +209,7 @@ let API = {
},
// new
getCurrentPosition: function(success, failure, options) {
var _success = emptyFn
var _success = emptyFn,
_failure = emptyFn,
_options = {};

Expand All @@ -206,7 +227,9 @@ let API = {
_failure = failure || emptyFn;
_options = options || {};
}
RNBackgroundGeolocation.getCurrentPosition(_options, _success, _failure);
withPermission(() => {
RNBackgroundGeolocation.getCurrentPosition(_options, _success, _failure);
}, _failure);
},
watchPosition: function(success, failure, options) {
if (typeof(failure) === 'object') {
Expand All @@ -215,9 +238,10 @@ let API = {
}
options = options || {};
failure = failure || emptyFn;
RNBackgroundGeolocation.watchPosition(options, function() {
EventEmitter.addListener("watchposition", success);
}, failure);

withPermission(() => {
RNBackgroundGeolocation.watchPosition(options, function() { EventEmitter.addListener("watchposition", success); }, failure);
}, failure);
},
stopWatchPosition: function(success, failure) {
success = success || emptyFn;
Expand Down Expand Up @@ -253,7 +277,10 @@ let API = {
setOdometer: function(value, success, failure) {
success = success || emptyFn;
failure = failure || emptyFn;
RNBackgroundGeolocation.setOdometer(value, success, failure);

withPermission(() => {
RNBackgroundGeolocation.setOdometer(value, success, failure);
}, failure);
},
resetOdometer: function(success, failure) {
this.setOdometer(0, success, failure);
Expand Down
Binary file modified android/libs/tslocationmanager.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public void onHostPause() {
public void onHostDestroy() {
initialized = false;
configured = false;
removeAllListeners();
getAdapter().onActivityDestroy();
}

Expand Down Expand Up @@ -990,6 +991,6 @@ private BackgroundGeolocation getAdapter() {
@Override
public void onCatalystInstanceDestroy() {
initialized = false;
getAdapter().onActivityDestroy();
removeAllListeners();
}
}
34 changes: 34 additions & 0 deletions index.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
PermissionsAndroid
} from "react-native"

import API from "./RNBackgroundGeolocation";

const ACCESS_FINE = PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION;
const ACCESS_COARSE = PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION;

async function requestPermission(success, failure) {
try {
const granted = await PermissionsAndroid.requestMultiple([ACCESS_FINE, ACCESS_COARSE]);

if ((granted[ACCESS_FINE] === PermissionsAndroid.RESULTS.GRANTED) && (granted[ACCESS_COARSE] === PermissionsAndroid.RESULTS.GRANTED)) {
success();
} else {
console.warn("ACCESS_FINE_LOCATION permission denied");
failure(1);
}
} catch (err) {
console.warn(err);
}
}

async function checkPermission(success, failure) {
PermissionsAndroid.check(ACCESS_FINE).then(isAuthorized => {
if (isAuthorized) { return success(); }
requestPermission(success, failure);
});
}

API.setPermissionsHandler(checkPermission);

module.exports = API;
3 changes: 3 additions & 0 deletions index.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import API from "./RNBackgroundGeolocation";

module.exports = API;
6 changes: 4 additions & 2 deletions ios/RNBackgroundGeolocation/RNBackgroundGeolocation.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ -(instancetype)init

RCT_EXPORT_METHOD(addListener:(NSString*)event)
{
// Careful: we're overrideing a RCTEventEmitter method here.
[super addListener:event];
// Careful: we're overrideing a RCTEventEmitter method here and RCTEventEmitter didn't always have this method.
if ([[self superclass] instancesRespondToSelector:@selector(addListener:)]) {
[super addListener:event];
}

@synchronized(listeners) {
if ([listeners objectForKey:event]) {
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"name": "react-native-background-geolocation-android",
"version": "2.9.2",
"version": "2.9.3",
"description": "The most sophisticated background location tracking & geofencing module with battery-conscious motion-detection",
"main": "RNBackgroundGeolocation.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down

0 comments on commit 9e0db9b

Please sign in to comment.