public static void OnOrientationChange(DeviceViewOrientation currentOrientation, bool?likelyPortrait = null, bool forceNotification = false) { //If the orientation is Unknown, then we want to try and call DeviceViewOrientationChecker to get it if (currentOrientation == DeviceViewOrientation.Unknown) { currentOrientation = DeviceViewOrientationChecker?.Invoke() ?? currentOrientation; } //If it is still unknown, then we will try to infer it from likelyPortrait and the lastKnownOrientation if (currentOrientation == DeviceViewOrientation.Unknown && likelyPortrait != null) { if (likelyPortrait.Value) { currentOrientation = (lastKnownOrientation == DeviceViewOrientation.PortraitNormal || lastKnownOrientation == DeviceViewOrientation.PortraitUpsideDown) ? lastKnownOrientation : DeviceViewOrientation.PortraitNormal; } else { currentOrientation = (lastKnownOrientation == DeviceViewOrientation.LandscapeLeft || lastKnownOrientation == DeviceViewOrientation.LandscapeRight) ? lastKnownOrientation : DeviceViewOrientation.LandscapeLeft; } } lock (orientationLocker) { if (currentOrientation != DeviceViewOrientation.Unknown && lastKnownOrientation != currentOrientation) { previousOrientation = lastKnownOrientation; lastKnownOrientation = currentOrientation; foreach (IOrientationAware item in orientationAwareObjects) { item.OnDeviceOrientationChanged(); } } else if (forceNotification) { foreach (IOrientationAware item in orientationAwareObjects) { item.OnDeviceOrientationChanged(); } } } }
public static void Initialize(AndroidPlatformConfigBase platformConfig) { _platformConfig = platformConfig ?? throw new ArgumentNullException(nameof(platformConfig)); SubscribeToPageScreenSizeNotifications(); try { _orientationListener = new OrientationListener(platformConfig.MainActivityContext, SensorDelay.Normal); if (!_orientationListener.TryEnabling()) { _orientationListener = null; } } catch (Exception) { _orientationListener = null; } if (_orientationListener?.Enabled ?? false) { CodeBrixViewModelBase.DeviceViewOrientationChecker = () => { DeviceViewOrientation orientation = DeviceViewOrientation.Unknown; if (_orientationListener.Enabled && _orientationListener.FirstOrientationSet) { int rotation = _orientationListener.Orientation; switch (rotation) { case 0: orientation = DeviceViewOrientation.PortraitNormal; break; case 90: orientation = DeviceViewOrientation.LandscapeRight; break; case 180: orientation = DeviceViewOrientation.PortraitUpsideDown; break; case 270: orientation = DeviceViewOrientation.LandscapeLeft; break; default: break; } } if (orientation == DeviceViewOrientation.Unknown) { orientation = (_lastKnownOrientation == Orientation.Landscape) ? DeviceViewOrientation.LandscapeLeft : DeviceViewOrientation.PortraitNormal; } return(orientation); }; } SetDeviceOrientation(_platformConfig.Resources.Configuration.Orientation); _initialized = true; }