Skip to content

Commit

Permalink
add new feature fixes #49, #52, #54
Browse files Browse the repository at this point in the history
  • Loading branch information
webyonet committed May 12, 2018
1 parent 0595fed commit 9e5a3fc
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 27 deletions.
66 changes: 48 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Under `protected List<ReactPackage> getPackages() {`:
### Usage

```javascript
import { BackHandler } from 'react-native';
import { BackHandler, DeviceEventEmitter } from 'react-native';
import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";

LocationServicesDialogBox.checkLocationServicesIsEnabled({
Expand All @@ -68,8 +68,9 @@ LocationServicesDialogBox.checkLocationServicesIsEnabled({
enableHighAccuracy: true, // true => GPS AND NETWORK PROVIDER, false => GPS OR NETWORK PROVIDER
showDialog: true, // false => Opens the Location access page directly
openLocationServices: true, // false => Directly catch method is called if location services are turned off
preventOutSideTouch: false, //true => To prevent the location services window from closing when it is clicked outside
preventBackClick: false //true => To prevent the location services popup from closing when it is clicked back button
preventOutSideTouch: false, // true => To prevent the location services window from closing when it is clicked outside
preventBackClick: false, // true => To prevent the location services popup from closing when it is clicked back button
providerListener: false // true ==> Trigger locationProviderStatusChange listener when the location state changes
}).then(function(success) {
console.log(success); // success => {alreadyEnabled: false, enabled: true, status: "enabled"}
}).catch((error) => {
Expand All @@ -79,12 +80,20 @@ LocationServicesDialogBox.checkLocationServicesIsEnabled({
BackHandler.addEventListener('hardwareBackPress', () => { //(optional) you can use it if you need it
LocationServicesDialogBox.forceCloseDialog();
});

DeviceEventEmitter.addListener('locationProviderStatusChange', function(status) { // only trigger when "providerListener" is enabled
console.log(status); // status => {enabled: false, status: "disabled"} or {enabled: true, status: "enabled"}
});

componentWillUnmount() {
LocationServicesDialogBox.stopListener(); // Stop the "locationProviderStatusChange" listener
}
```

### Usage And Example For Async Method `ES6`

```javascript
import { BackHandler } from 'react-native';
import { BackHandler, DeviceEventEmitter } from 'react-native';
import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";

export default class LocationServiceTestPage extends Component {
Expand All @@ -96,6 +105,10 @@ export default class LocationServiceTestPage extends Component {
BackHandler.addEventListener('hardwareBackPress', () => { //(optional) you can use it if you need it
LocationServicesDialogBox.forceCloseDialog();
});

DeviceEventEmitter.addListener('locationProviderStatusChange', function(status) { // only trigger when "providerListener" is enabled
console.log(status); // status => {enabled: false, status: "disabled"} or {enabled: true, status: "enabled"}
});
}

async checkIsLocation():Promise {
Expand All @@ -107,11 +120,16 @@ export default class LocationServiceTestPage extends Component {
showDialog: true, // false => Opens the Location access page directly
openLocationServices: true, // false => Directly catch method is called if location services are turned off
preventOutSideTouch: false, //true => To prevent the location services window from closing when it is clicked outside
preventBackClick: false //true => To prevent the location services popup from closing when it is clicked back button
preventBackClick: false, //true => To prevent the location services popup from closing when it is clicked back button
providerListener: true // true ==> Trigger "locationProviderStatusChange" listener when the location state changes
}).catch(error => error);

return Object.is(check.status, "enabled");
}
}

componentWillUnmount() {
LocationServicesDialogBox.stopListener(); // Stop the "locationProviderStatusChange" listener
}
}
```

Expand All @@ -122,7 +140,8 @@ import {
AppRegistry,
Text,
View,
BackHandler
BackHandler,
DeviceEventEmitter
} from 'react-native';

import LocationServicesDialogBox from "react-native-android-location-services-dialog-box";
Expand All @@ -141,7 +160,8 @@ class SampleApp extends Component {
showDialog: true, // false => Opens the Location access page directly
openLocationServices: true, // false => Directly catch method is called if location services are turned off
preventOutSideTouch: false, //true => To prevent the location services popup from closing when it is clicked outside
preventBackClick: false //true => To prevent the location services popup from closing when it is clicked back button
preventBackClick: false, //true => To prevent the location services popup from closing when it is clicked back button
providerListener: true // true ==> Trigger "locationProviderStatusChange" listener when the location state changes
}).then(function(success) {
// success => {alreadyEnabled: true, enabled: true, status: "enabled"}
navigator.geolocation.getCurrentPosition((position) => {
Expand All @@ -156,7 +176,15 @@ class SampleApp extends Component {
BackHandler.addEventListener('hardwareBackPress', () => { //(optional) you can use it if you need it
LocationServicesDialogBox.forceCloseDialog();
});

DeviceEventEmitter.addListener('locationProviderStatusChange', function(status) { // only trigger when "providerListener" is enabled
console.log(status); // status => {enabled: false, status: "disabled"} or {enabled: true, status: "enabled"}
});
}

componentWillUnmount() {
LocationServicesDialogBox.stopListener(); // Stop the "locationProviderStatusChange" listener
}

render() {
return (
Expand All @@ -173,23 +201,25 @@ AppRegistry.registerComponent('SampleApp', () => SampleApp);

### Props

| Prop | Type | Default | Description |
|-----------------------------------|-------------|-------------|-------------------------------------------------------------------------------------|
|`message` |`HTML` |`null` |Dialog box content text |
|`ok` |`String` |`null` |Dialog box ok button text |
|`cancel` |`String` |`null` |Dialog box cancel button text |
|`enableHighAccuracy` (optional) |`Boolean` |`true` |Provider switch (GPS OR NETWORK OR GPS AND NETWORK) |
|`showDialog` (optional) |`Boolean` |`true` |Indicate whether to display the dialog box |
|`openLocationServices` (optional) |`Boolean` |`true` |Indicate whether to display the location services screen |
|`preventOutSideTouch` (optional) |`Boolean` |`true` |To prevent the location services window from closing when it is clicked outside |
|`preventBackClick` (optional) |`Boolean` |`true` |To prevent the location services popup from closing when it is clicked back button |
| Prop | Type | Default | Description |
|-----------------------------------|-------------|-------------|------------------------------------------------------------------------------------------|
|`message` |`HTML` |`null` |Dialog box content text |
|`ok` |`String` |`null` |Dialog box ok button text |
|`cancel` |`String` |`null` |Dialog box cancel button text |
|`enableHighAccuracy` (optional) |`Boolean` |`true` |Provider switch (GPS OR NETWORK OR GPS AND NETWORK) |
|`showDialog` (optional) |`Boolean` |`true` |Indicate whether to display the dialog box |
|`openLocationServices` (optional) |`Boolean` |`true` |Indicate whether to display the location services screen |
|`preventOutSideTouch` (optional) |`Boolean` |`true` |To prevent the location services window from closing when it is clicked outside |
|`preventBackClick` (optional) |`Boolean` |`true` |To prevent the location services popup from closing when it is clicked back button |
|`providerListener` (optional) |`Boolean` |`false` |Used to trigger `locationProviderStatusChange listener when the location state changes. |

### Methods

| Name | Return | Return Value |
|------------------------------------|--------------------|------------------|
|`checkLocationServicesIsEnabled` | Promise | Object |
|`forceCloseDialog` (optional using) | void | - |
|`stopListener` (optional using) | void | Object |


[![NPM](https://nodei.co/npm/react-native-android-location-services-dialog-box.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/react-native-android-location-services-dialog-box/)
4 changes: 2 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.showlocationservicesdialogbox">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.showlocationservicesdialogbox">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.*;
import android.location.LocationManager;
import android.text.Html;
import android.text.Spanned;
import com.facebook.react.bridge.*;
import com.facebook.react.modules.core.DeviceEventManagerModule;

class LocationServicesDialogBoxModule extends ReactContextBaseJavaModule implements ActivityEventListener {
public class LocationServicesDialogBoxModule extends ReactContextBaseJavaModule implements ActivityEventListener {
private Promise promiseCallback;
private ReadableMap map;
private Activity currentActivity;
private AlertDialog alertDialog;
private static final int ENABLE_LOCATION_SERVICES = 1009;
private static AlertDialog alertDialog;
private Boolean isReceive = false;
private BroadcastReceiver providerReceiver = null;

LocationServicesDialogBoxModule(ReactApplicationContext reactContext) {
super(reactContext);
Expand Down Expand Up @@ -47,6 +48,18 @@ public void forceCloseDialog() {
}
}

@ReactMethod
public void stopListener() {
isReceive = false;
try {
if (providerReceiver != null) {
getReactApplicationContext().unregisterReceiver(providerReceiver);
providerReceiver = null;
}
} catch (Exception ignored) {
}
}

private void checkLocationService(Boolean activityResult) {
if (currentActivity == null || map == null || promiseCallback == null) return;
LocationManager locationManager = (LocationManager) currentActivity.getSystemService(Context.LOCATION_SERVICE);
Expand All @@ -71,6 +84,10 @@ private void checkLocationService(Boolean activityResult) {
newActivity(currentActivity);
}
} else {
if (map.hasKey("providerListener") && map.getBoolean("providerListener")) {
startListener();
}

result.putString("status", "enabled");
result.putBoolean("enabled", true);
result.putBoolean("alreadyEnabled", !activityResult);
Expand Down Expand Up @@ -122,11 +139,50 @@ private void newActivity(final Activity activity) {
activity.startActivityForResult(new Intent(action), ENABLE_LOCATION_SERVICES);
}

private void startListener() {
try {
providerReceiver = new LocationProviderChangedReceiver();
getReactApplicationContext().registerReceiver(providerReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
isReceive = true;
} catch (Exception ignored) {
}
}

private void sendEvent() {
if (isReceive) {
LocationManager locationManager = (LocationManager) currentActivity.getSystemService(Context.LOCATION_SERVICE);
WritableMap params = Arguments.createMap();
if (locationManager != null) {
boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

params.putString("status", (enabled ? "enabled" : "disabled"));
params.putBoolean("enabled", enabled);

ReactContext reactContext = getReactApplicationContext();

if (reactContext != null) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("locationProviderStatusChange", params);
}
}
}
}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == ENABLE_LOCATION_SERVICES) {
currentActivity = activity;
checkLocationService(true);
}
}
}

private final class LocationProviderChangedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (action != null && action.matches("android.location.PROVIDERS_CHANGED")) {
sendEvent();
}
}
}
}
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-android-location-services-dialog-box",
"version": "2.4.4",
"version": "2.5.0",
"description": "A react-native component for turn on the dialog box from android location services",
"author": "webyonet <[email protected]>",
"main": "index.js",
Expand Down

0 comments on commit 9e5a3fc

Please sign in to comment.