diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..98a2418 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,20 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages + +name: Node.js Package + +on: + workflow_dispatch: + +jobs: + publish-npm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 16 + registry-url: https://registry.npmjs.org/ + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.NODE_AUTH_TOKEN}} diff --git a/README.md b/README.md index c6b5447..9514505 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ credits - https://github.com/vnil/react-native-simple-compass `$ yarn add react-native-compass-heading` -`$ cd ios/ && pod install && cd ..` +`$ npx pod-install` ## Usage ```javascript @@ -25,7 +25,7 @@ const App = () => { // accuracy on android will be hardcoded to 1 // since the value is not available. // For iOS, it is in degrees - CompassHeading.start(degree_update_rate, {heading, accuracy} => { + CompassHeading.start(degree_update_rate, ({heading, accuracy}) => { setCompassHeading(heading); }); 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) {} } diff --git a/package.json b/package.json index b6747a6..82fd37b 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.3.3", "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" } }