From 9c38527e3d0670cb557477cc78072f8c4a9b2f26 Mon Sep 17 00:00:00 2001 From: Cristiano Coelho Date: Mon, 20 Dec 2021 14:18:49 -0300 Subject: [PATCH 1/2] code cleanup and remove deprecated method --- .../compassheading/CompassHeadingModule.java | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/reactlibrary/compassheading/CompassHeadingModule.java b/android/src/main/java/com/reactlibrary/compassheading/CompassHeadingModule.java index 710130c..85e7cb5 100644 --- a/android/src/main/java/com/reactlibrary/compassheading/CompassHeadingModule.java +++ b/android/src/main/java/com/reactlibrary/compassheading/CompassHeadingModule.java @@ -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; @@ -18,13 +18,13 @@ 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 @@ -32,22 +32,18 @@ public class CompassHeadingModule extends ReactContextBaseJavaModule implements 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"; @@ -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); @@ -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){ @@ -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) {} } From 1e40b61b46b72593b4d1a087bfb46555552913ea Mon Sep 17 00:00:00 2001 From: Cristiano Coelho Date: Mon, 20 Dec 2021 14:20:55 -0300 Subject: [PATCH 2/2] bump deps versions --- package.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b6747a6..2dbcd8b 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" } }