wcoder / Xamarin.Plugin.DeviceOrientation

Cross-platform plugin to work with screen orientation of mobile device.
MIT License
61 stars 20 forks source link

Landscape and Portrait switched in Android #10

Open Bracefor opened 6 years ago

Bracefor commented 6 years ago

On iOS, everything is perfectly fine, but as soon as I use this on Android, everything is switched around? When I rotate the device and print out the orientation, it says it is Landscape, when it is actually Portrait?

Here is my custom Image class where the error exists:

using Plugin.DeviceOrientation;
using Plugin.DeviceOrientation.Abstractions;
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace Project.Custom
{
    public class DSImage : Image
    {
        public DSImage(int phonePortrait, int phoneLandscape, int tabletPortrait, int tabletLandscape)
        {
            if (Device.Idiom == TargetIdiom.Phone)
            {
                if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
                {
                    System.Diagnostics.Debug.WriteLine("Phone Portrait");
                    HeightRequest = phonePortrait;
                }
                else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
                {
                    System.Diagnostics.Debug.WriteLine("Phone Landscape");
                    HeightRequest = phoneLandscape;
                }
            }
            else if (Device.Idiom == TargetIdiom.Tablet)
            {
                if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
                {
                    System.Diagnostics.Debug.WriteLine("Tablet Portrait");
                    HeightRequest = tabletPortrait;
                }
                else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
                {
                    System.Diagnostics.Debug.WriteLine("Tablet Landscape");
                    HeightRequest = tabletLandscape;
                }
            }

            CrossDeviceOrientation.Current.OrientationChanged += (sender, args) =>
            {
                if (Device.Idiom == TargetIdiom.Phone)
                {
                    if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
                    {
                        System.Diagnostics.Debug.WriteLine("Phone Portrait");
                        HeightRequest = phonePortrait;
                    }
                    else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
                    {
                        System.Diagnostics.Debug.WriteLine("Phone Landscape");
                        HeightRequest = phoneLandscape;
                    }
                }
                else if (Device.Idiom == TargetIdiom.Tablet)
                {
                    if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Portrait || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.PortraitFlipped)
                    {
                        System.Diagnostics.Debug.WriteLine("Tablet Portrait");
                        HeightRequest = tabletPortrait;
                    }
                    else if (CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.Landscape || CrossDeviceOrientation.Current.CurrentOrientation == DeviceOrientations.LandscapeFlipped)
                    {
                        System.Diagnostics.Debug.WriteLine("Tablet Landscape");
                        HeightRequest = tabletLandscape;
                    }
                }
            };
        }
    }
}
wcoder commented 6 years ago

@Bracefor, thanks for your report!

I published a new version of the plugin: https://www.nuget.org/packages/Plugin.DeviceOrientation/1.0.7

For the fix, look at this section: https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation#xamarinforms-android

Could you please check this version, and tell me about your results?

Huaba93 commented 4 years ago

@wcoder

This bug still exists. On some Android tablets CrossDeviceOrientation.Current.CurrentOrientation is reversed. I guess this is because some tablets are in landscape mode by default. We could reproduce this with

I suspect there are many more devices with this problem. we could't reproduce it with emulators.

Huaba93 commented 4 years ago

@wcoder I could also reproduce on a Samsung Galaxy Tab 4

activity.Resources.Configuration.Orientation returns Landscape

activity.WindowManager.DefaultDisplay.Rotation returns Rotation180 and is converted by your plugin to DeviceOrientations.PortraitFlipped but this is wrong

activity.WindowManager.DefaultDisplay is deprecated since API Level 11, this should really be replaced with a more recent solution