Skip to content
This repository was archived by the owner on Jul 21, 2021. It is now read-only.

enable disable proximity flag in android #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ npm install --save react-native-proximity
react-native link react-native-proximity
```

## Android

Include the following permission in `AndroidManifest.xml`:

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

## Usage

Import the library
Expand All @@ -31,6 +39,7 @@ import Proximity from 'react-native-proximity';
The callback function returns an object with *proximity* and *distance* properties. If *proximity* is true, it means the device is close to an physical object. *distance* is only supported in Android.
```javascript
componentDidMount(){
Proximity.enableScreenOnOff(true) // enable screen on-off with proximity in android
Proximity.addListener(this._proximityListener);
},

Expand Down
21 changes: 21 additions & 0 deletions android/src/main/java/com/RNProximity/RNProximityModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class RNProximityModule extends ReactContextBaseJavaModule implements Sen
private static final String KEY_EVENT_ON_SENSOR_CHANGE = "EVENT_ON_SENSOR_CHANGE";
private static final String EVENT_ON_SENSOR_CHANGE = "onSensorChanged";
private final ReactApplicationContext reactContext;
private PowerManager.WakeLock wakeLock;

private SensorManager mSensorManager;
private Sensor mProximity;
Expand All @@ -49,6 +50,16 @@ public void sendEvent(String eventName, @Nullable WritableMap params) {
}
}

private PowerManager.WakeLock getWakeLock(){
Context context = this.reactContext.getApplicationContext();
PowerManager powerManager = context.getSystemService(PowerManager.class);
PowerManager.WakeLock wl;
if (powerManager.isWakeLockLevelSupported(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK)) {
return powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "WAKE_LOCK:" + TAG);
}
return null;
}

@ReactMethod
public void addListener() {
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
Expand All @@ -59,6 +70,16 @@ public void removeListener() {
mSensorManager.unregisterListener(this);
}

@ReactMethod
public void proximityEnabled(boolean enable) {
if(this.wakeLock != null){
if(enable){
this.wakeLock.acquire();
} else
this.wakeLock.release();
}
}

@Override
public String getName() {
return "RNProximity";
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const nativeModule = NativeModules.RNProximity;
let addListener = null;
let removeListener = null;

// only for Android
let screenOnOff = false;
const enableScreenOnOff = function(value) {
screenOnOff = value;
};

if (Platform.OS === 'ios') {
addListener = function(callback) {
NativeModules.RNProximity.proximityEnabled(true);
Expand All @@ -26,10 +32,16 @@ if (Platform.OS === 'ios') {
}
} else if (Platform.OS == 'android') {
addListener = (callback) => {
if (screenOnOff) {
nativeModule.proximityEnabled(true);
}
nativeModule.addListener();
DeviceEventEmitter.addListener(nativeModule.EVENT_ON_SENSOR_CHANGE, e => callback(e));
};
removeListener = (listener) => {
if (screenOnOff) {
nativeModule.proximityEnabled(false);
}
nativeModule.removeListener();
DeviceEventEmitter.removeAllListeners(nativeModule.EVENT_ON_SENSOR_CHANGE, listener);
};
Expand All @@ -38,4 +50,5 @@ if (Platform.OS === 'ios') {
module.exports = {
addListener,
removeListener,
enableScreenOnOff
};