Skip to content

Commit

Permalink
Merge pull request firofame#30 from cristianoccazinsp/android-cleanup
Browse files Browse the repository at this point in the history
Android cleanup
  • Loading branch information
firofame authored Jan 6, 2022
2 parents 69bb202 + 1e40b61 commit c16ce13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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.Promise;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
Expand All @@ -18,36 +18,32 @@

import android.content.Context;

import androidx.annotation.NonNull;

import com.facebook.react.modules.core.DeviceEventManagerModule;


public class CompassHeadingModule extends ReactContextBaseJavaModule implements SensorEventListener {

private final ReactApplicationContext reactContext;

private static Context mApplicationContext;

private int mAzimuth = 0; // degree
private int mFilter = 1;

private SensorManager sensorManager;

private Sensor gsensor;
private Sensor msensor;

private float[] mGravity = new float[3];
private float[] mGeomagnetic = new float[3];
private final float[] mGravity = new float[3];
private final float[] mGeomagnetic = new float[3];

private float[] R = new float[9];
private float[] I = new float[9];
private final float[] R = new float[9];
private final float[] I = new float[9];

public CompassHeadingModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;

mApplicationContext = reactContext.getApplicationContext();
}

@NonNull
@Override
public String getName() {
return "CompassHeading";
Expand All @@ -69,8 +65,8 @@ public void start(int filter, Promise promise) {
try{
sensorManager = (SensorManager) mApplicationContext.getSystemService(Context.SENSOR_SERVICE);

gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
Sensor gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor msensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

sensorManager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
Expand Down Expand Up @@ -137,15 +133,23 @@ public void onSensorChanged(SensorEvent event) {

if (success) {

float orientation[] = new float[3];
float[] orientation = new float[3];
SensorManager.getOrientation(R, orientation);

int newAzimuth = (int) Math.toDegrees(orientation[0]);
newAzimuth = (newAzimuth + 360) % 360;

Display disp = (((WindowManager) mApplicationContext.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay();
Display disp = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
Activity activity = getReactApplicationContext().getCurrentActivity();
if (activity != null) {
disp = activity.getDisplay();
}
} else {
disp = (((WindowManager) mApplicationContext.getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay();
}

if(disp != null){
if (disp != null) {
int rotation = disp.getRotation();

if(rotation == Surface.ROTATION_90){
Expand Down Expand Up @@ -173,13 +177,8 @@ else if(rotation == Surface.ROTATION_180){
}
}
}


}


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

}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-compass-heading",
"title": "React Native Compass Heading",
"version": "1.2.1",
"version": "1.2.2",
"description": "Listener to device's compass information.",
"main": "index.js",
"scripts": {
Expand All @@ -23,11 +23,11 @@
"licenseFilename": "LICENSE",
"readmeFilename": "README.md",
"peerDependencies": {
"react": "^16.8.1",
"react-native": ">=0.60.0-rc.0 <1.0.x"
"react": "^17.0.2",
"react-native": ">=0.60.0 <1.0.x"
},
"devDependencies": {
"react": "^16.9.0",
"react-native": "^0.61.5"
"react": "^17.0.2",
"react-native": "^0.66.0"
}
}

0 comments on commit c16ce13

Please sign in to comment.