Skip to content
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
5 changes: 3 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
buildscript {
repositories {
jcenter()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.android.tools.build:gradle:3.2.0'
}
}

Expand All @@ -20,7 +21,7 @@ android {
buildToolsVersion safeExtGet('buildToolsVersion', "23.0.1")

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
minSdkVersion safeExtGet('minSdkVersion', 21)
targetSdkVersion safeExtGet('targetSdkVersion', 22)
versionCode 1
versionName "1.0"
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-all.zip
2 changes: 2 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.RNProximity">

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

</manifest>

62 changes: 13 additions & 49 deletions android/src/main/java/com/RNProximity/RNProximityModule.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@

package com.RNProximity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.os.PowerManager;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.util.HashMap;
import java.util.Map;
import static android.content.Context.POWER_SERVICE;

public class RNProximityModule extends ReactContextBaseJavaModule implements SensorEventListener {
public class RNProximityModule extends ReactContextBaseJavaModule {

private static final String TAG = "RNProximityModule";
private static final String KEY_PROXIMITY = "proximity";
private static final String KEY_DISTANCE = "distance";
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 SensorManager mSensorManager;
private Sensor mProximity;
private final PowerManager.WakeLock wakeLock;

public RNProximityModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
mSensorManager = (SensorManager) reactContext.getSystemService(Context.SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
PowerManager powerManager = (PowerManager) reactContext.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TAG + ":wakelocktag");
}

public void sendEvent(String eventName, @Nullable WritableMap params) {
public void sendEvent(String eventName, WritableMap params) {
if (this.reactContext.hasActiveCatalystInstance()) {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
Expand All @@ -51,42 +37,20 @@ public void sendEvent(String eventName, @Nullable WritableMap params) {

@ReactMethod
public void addListener() {
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
if(!wakeLock.isHeld()) {
wakeLock.acquire(10*60*1000L /*10 minutes*/);
}
}

@ReactMethod
public void removeListener() {
mSensorManager.unregisterListener(this);
if(wakeLock.isHeld()) {
wakeLock.release();
}
}

@Override
public String getName() {
return "RNProximity";
}

@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put(KEY_EVENT_ON_SENSOR_CHANGE, EVENT_ON_SENSOR_CHANGE);
return constants;
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
WritableMap params = Arguments.createMap();

double distance = sensorEvent.values[0];
double maximumRange = mProximity.getMaximumRange();
boolean isNearDevice = distance < maximumRange;

params.putBoolean(KEY_PROXIMITY, isNearDevice);
params.putDouble(KEY_DISTANCE, distance);

sendEvent(EVENT_ON_SENSOR_CHANGE, params);
}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
}
12 changes: 4 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,18 @@ exports.addListener = addListener;
let removeListener = null;
exports.removeListener = removeListener;
if (react_native_1.Platform.OS === "ios") {
(exports.addListener = addListener = function (callback) {
(exports.addListener = addListener = function () {
react_native_1.NativeModules.RNProximity.proximityEnabled(true);
return react_native_1.DeviceEventEmitter.addListener("proximityStateDidChange", callback);
}),
(exports.removeListener = removeListener = function (listener) {
(exports.removeListener = removeListener = function () {
react_native_1.NativeModules.RNProximity.proximityEnabled(false);
react_native_1.DeviceEventEmitter.removeAllListeners("proximityStateDidChange", listener);
});
}
else if (react_native_1.Platform.OS == "android") {
exports.addListener = addListener = (callback) => {
exports.addListener = addListener = () => {
nativeModule.addListener();
react_native_1.DeviceEventEmitter.addListener(nativeModule.EVENT_ON_SENSOR_CHANGE, (e) => callback(e));
};
exports.removeListener = removeListener = (listener) => {
exports.removeListener = removeListener = () => {
nativeModule.removeListener();
react_native_1.DeviceEventEmitter.removeAllListeners(nativeModule.EVENT_ON_SENSOR_CHANGE, listener);
};
}
Loading