Skip to content
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

Android14 #430

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
9 changes: 4 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 34

defaultConfig {
minSdkVersion 16
targetSdkVersion 22
minSdkVersion 26
targetSdkVersion 34
versionCode 1
versionName "1.0"
ndk {
Expand All @@ -16,5 +15,5 @@ android {
}

dependencies {
compile "com.facebook.react:react-native:+"
implementation "com.facebook.react:react-native:+"
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,21 @@ private String getOrientationString(int orientation) {
}
}

@RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
@Override
public void onHostResume() {
final Activity activity = getCurrentActivity();

if (activity == null) {
FLog.e(ReactConstants.TAG, "no activity to register receiver");
return;
}
activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"),
Context.RECEIVER_NOT_EXPORTED);
} else {
activity.registerReceiver(receiver, new IntentFilter("onConfigurationChanged"));
}
}
@Override
public void onHostPause() {
Expand Down
15 changes: 7 additions & 8 deletions demo/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,12 @@ def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
compileSdkVersion 34

defaultConfig {
applicationId "com.demo"
minSdkVersion 16
targetSdkVersion 22
minSdkVersion 26
targetSdkVersion 34
versionCode 1
versionName "1.0"
ndk {
Expand Down Expand Up @@ -133,10 +132,10 @@ android {
}

dependencies {
compile project(':react-native-orientation')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules
implementation project(':react-native-orientation')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:23.0.1"
implementation "com.facebook.react:react-native:+" // From node_modules
}

// Run this once to be able to run the application with BUCK
Expand Down
2 changes: 1 addition & 1 deletion demo/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
94 changes: 73 additions & 21 deletions iOS/RCTOrientation/Orientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#import "RCTEventDispatcher.h"
#endif

#import <React/RCTUtils.h>

@implementation Orientation
@synthesize bridge = _bridge;

Expand All @@ -20,6 +22,72 @@ + (UIInterfaceOrientationMask)getOrientation {
return _orientation;
}

-(UIViewController*) topViewControllerWithRootViewController:(id)rootViewController
{

if (rootViewController == nil)
{
return nil;
}

if ([rootViewController isKindOfClass:[UITabBarController class]])
{
UITabBarController *selectedTabBarController = rootViewController;
return [self topViewControllerWithRootViewController:selectedTabBarController.selectedViewController];
}
else if ([rootViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *selectedNavController = rootViewController;
return [self topViewControllerWithRootViewController:selectedNavController.visibleViewController];
}
else
{
UIViewController *selectedViewController = rootViewController;
if (selectedViewController.presentedViewController != nil)
{
return [self topViewControllerWithRootViewController:selectedViewController.presentedViewController];
}
}
return rootViewController;
}

- (void)forceOrientation: (UIInterfaceOrientationMask)orientation {
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
if (@available(iOS 16, *)) {
NSArray *array = [[[UIApplication sharedApplication] connectedScenes] allObjects];
UIWindowScene *scene = (UIWindowScene *)array[0];

for (UIWindow *window in scene.windows) {
UIViewController* vc = [self topViewControllerWithRootViewController:window.rootViewController];
if (vc) {
[vc setNeedsUpdateOfSupportedInterfaceOrientations];
}
}

UIWindowSceneGeometryPreferencesIOS *geometryPreferences = [[UIWindowSceneGeometryPreferencesIOS alloc] initWithInterfaceOrientations:orientation];
[scene requestGeometryUpdateWithPreferences:geometryPreferences errorHandler:^(NSError * _Nonnull error) {
NSLog(@"Error while trying to lock orientation: %@", error);
}];

} else {
UIInterfaceOrientation o = UIInterfaceOrientationUnknown;
switch (orientation) {
case UIInterfaceOrientationMaskLandscapeLeft:
o = UIInterfaceOrientationLandscapeLeft;
break;
case UIInterfaceOrientationMaskLandscapeRight:
o = UIInterfaceOrientationLandscapeRight;
break;
case UIInterfaceOrientationMaskPortrait:
o = UIInterfaceOrientationPortrait;
break;
}
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: o] forKey:@"orientation"];
}
}];
}

- (instancetype)init
{
if ((self = [super init])) {
Expand Down Expand Up @@ -157,10 +225,7 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
NSLog(@"Locked to Portrait");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskPortrait];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"];
}];
[self forceOrientation: UIInterfaceOrientationMaskPortrait];

}

Expand All @@ -173,16 +238,10 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
NSString *orientationStr = [self getSpecificOrientationStr:orientation];
if ([orientationStr isEqualToString:@"LANDSCAPE-LEFT"]) {
[Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}];
[self forceOrientation: UIInterfaceOrientationMaskLandscapeRight];
} else {
[Orientation setOrientation:UIInterfaceOrientationMaskLandscape];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
}];
[self forceOrientation: UIInterfaceOrientationMaskLandscapeLeft];
}
}

Expand All @@ -192,10 +251,7 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
NSLog(@"Locked to Landscape Left");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeLeft];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
}];
[self forceOrientation: UIInterfaceOrientationMaskLandscapeLeft];

}

Expand All @@ -205,11 +261,7 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
NSLog(@"Locked to Landscape Right");
#endif
[Orientation setOrientation:UIInterfaceOrientationMaskLandscapeRight];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
// this seems counter intuitive
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
}];
[self forceOrientation: UIInterfaceOrientationMaskLandscapeRight];

}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-orientation",
"version": "3.1.3",
"version": "3.1.5",
"description": "Listen to device orientation changes in React Native applications and programmatically set preferred orientations on a per screen basis",
"main": "index.js",
"scripts": {
Expand Down