cordova-plugin-screen-orientation icon indicating copy to clipboard operation
cordova-plugin-screen-orientation copied to clipboard

Orientation not working on Android

Open florisdh opened this issue 5 years ago • 5 comments

Bug Report

Problem

Since a few weeks we noticed that the orientation suddenly didn't allow unlocking anymore in our app.

What is expected to happen?

After calling unlock(), the app should use the sensor for the right orientation.

What does actually happen?

When calling unlock(), it was going back to forced portrait.

Environment, Platform, Device

Tried on multiple android devices.

Version information

  • @capacitor/*: 2.4.0
  • Android build: (minSdkVersion = 21, compileSdkVersion = 29, targetSdkVersion = 29)
  • Android version: 10

I've made a fix which we are using now, which can be found here.

  • Based on the 3.0.2 tag since the master branch didn't work for us

florisdh avatar Oct 19 '20 08:10 florisdh

Sorry, clicked the wrong button. Can you elaborate what's the difference in your fix and why it works again as expected?

timbru31 avatar Oct 19 '20 18:10 timbru31

From the android docs

Sensor:

Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. Ignores user's setting to turn off sensor-based rotation.

Unspecified:

No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device.

It sounds like if we used sensor it may cause the app to ignore user settings, which sounds like a bad assumption to make.

breautek avatar Oct 19 '20 18:10 breautek

Hey guys, thanks for the quick reply. 🤘

In the end I decided to create a demo project showing the issue that we're facing. After some hours of debugging why our project actually has this behaviour, I found what was causing this weird behaviour here

You can run my demo project and see that running screen.orientation.unlock() will cause the screen to lock to portrait for some reason.

This is probably caused by the underlaying activity somehow being locked to portrait by this and the unspecified option used by this plugin will use this as seen in the docs

Unspecified: ... This will either be the orientation selected by the activity below, or ...

Aside from this my 'fix' was using Sensor as breautek mentioned, which is also not really ideal since it would ignore the user settings, so I changed it to use the 'ActivityInfo.SCREEN_ORIENTATION_USER' as seen here. This will only use the sensor if enabled.

As a developer I would expect that calling screen.orientation.unlock() would actually unlock the screen orientation by using the sensor if enabled in the settings, thus I think it would be best to change it to ActivityInfo.SCREEN_ORIENTATION_USER.

Anyway our issue is fixed, so I rest my case.

Thanks for the support!

florisdh avatar Oct 20 '20 18:10 florisdh

In my case, .lock("landscape") doesn't work. It either still uses the setting from the config.xml or, if you used .unlock() first, the phone will still use all orientations instead of just "landscape".

Andalusio avatar Dec 03 '20 14:12 Andalusio