Skip to content

registerReceiver patch #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Rai-burger opened this issue Apr 1, 2025 · 0 comments
Open

registerReceiver patch #309

Rai-burger opened this issue Apr 1, 2025 · 0 comments

Comments

@Rai-burger
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-orientation-locker/android/src/main/java/org/wonday/orientation/OrientationModule.java b/node_modules/react-native-orientation-locker/android/src/main/java/org/wonday/orientation/OrientationModule.java
index 4a44ff6..7842baf 100644
--- a/node_modules/react-native-orientation-locker/android/src/main/java/org/wonday/orientation/OrientationModule.java
+++ b/node_modules/react-native-orientation-locker/android/src/main/java/org/wonday/orientation/OrientationModule.java
@@ -16,6 +16,7 @@ import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
 import android.hardware.SensorManager;
+import android.os.Build;
 import android.view.Display;
 import android.view.OrientationEventListener;
 import android.view.Surface;
@@ -59,7 +60,6 @@ public class OrientationModule extends ReactContextBaseJavaModule implements Ori
 
                 String deviceOrientationValue = lastDeviceOrientationValue;
 
-
                 if (orientation == -1) {
                     deviceOrientationValue = "UNKNOWN";
                 } else if (orientation > 355 || orientation < 5) {
@@ -349,11 +349,20 @@ public class OrientationModule extends ReactContextBaseJavaModule implements Ori
         return constants;
     }
 
+    // Add this method to handle receiver registration with Android 14 compatibility
+    private void compatRegisterReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter, boolean exported) {
+        if (Build.VERSION.SDK_INT >= 34 && context.getApplicationInfo().targetSdkVersion >= 34) {
+            context.registerReceiver(receiver, filter, exported ? Context.RECEIVER_EXPORTED : Context.RECEIVER_NOT_EXPORTED);
+        } else {
+            context.registerReceiver(receiver, filter);
+        }
+    }
+
     @Override
     public void start() {
         FLog.i(ReactConstants.TAG, "orientation detect enabled.");
         mOrientationListener.enable();
-        ctx.registerReceiver(mReceiver, new IntentFilter("onConfigurationChanged"));
+        compatRegisterReceiver(ctx, mReceiver, new IntentFilter("onConfigurationChanged"), false);
     }
 
     @Override

This issue body was partially generated by patch-package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant