/// <summary>
        /// Create an object to read the hinge sensor
        /// </summary>
        public static HingeSensor Start()
        {
#if UNITY_ANDROID
            var sensor = OnPlayer.Run(p =>
            {
                var context = p.GetStatic <AndroidJavaObject>("currentActivity");

                var plugin = new AndroidJavaClass("com.microsoft.device.dualscreen.unity.HingeAngleSensor")
                             .CallStatic <AndroidJavaObject>("getInstance", context);

                if (plugin != null)
                {
                    plugin.Call("setupSensor");
                    return(new HingeSensor(plugin));
                }
                else
                {
                    return(null);
                }
            });

            return(sensor);
#else
            return(null);
#endif
        }
        /// <summary>
        /// Returns the coordinates of the Hinge in a Rect object.
        /// </summary>
        public static RectInt GetHinge()
        {
            var hinge = OnPlayer.Run(p =>
            {
                var context = p.GetStatic <AndroidJavaObject>("currentActivity");

                using (var dm = new AndroidJavaClass(SCREENHELPER_CLASSNAME))
                {
                    return(dm.CallStatic <AndroidJavaObject>("getHinge", context));
                }
            });

            if (hinge != null)
            {
                var left   = hinge.Get <int>("left");
                var top    = hinge.Get <int>("top");
                var width  = hinge.Call <int>("width");
                var height = hinge.Call <int>("height");

                return(new RectInt(left, top, width, height));
            }
            else
            {
                return(new RectInt(0, 0, 0, 0)); // TODO: cannot return null - is ok?
            }
        }
        /// <summary>
        /// Returns a list of two elements that contain the coordinates of the screen rectangles
        /// </summary>
        public static RectInt[] GetScreenRectangles()
        {
            var jScreenRects = OnPlayer.Run(p =>
            {
                var context = p.GetStatic <AndroidJavaObject>("currentActivity");
                using (var dm = new AndroidJavaClass(SCREENHELPER_CLASSNAME))
                {
                    return(dm.CallStatic <AndroidJavaObject>("getScreenRectangles", context));
                }
            });

            var size = jScreenRects.Call <int>("size");

            if (size > 0)
            {
                var rectangles = new RectInt[size];
                for (var i = 0; i < size; i++)
                {
                    var jRect = jScreenRects.Call <AndroidJavaObject>("get", i);

                    var left   = jRect.Get <int>("left");
                    var top    = jRect.Get <int>("top");
                    var width  = jRect.Call <int>("width");
                    var height = jRect.Call <int>("height");

                    rectangles[i] = new RectInt(left, top, width, height);
                    Debug.LogWarning($"GetScreenRectangles [{i}]: {rectangles[i]}");
                }
                return(rectangles);
            }
            else
            {
                return(new RectInt[0]); // TODO: return null??
            }
        }
예제 #4
0
        /// <summary>
        /// Returns a list of two elements that contain the coordinates of the screen rectangles
        /// </summary>
        public static RectInt[] GetScreenRectangles()
        {
            var jScreenRects = OnPlayer.Run(p =>
            {
                return(screenInfoObject.Call <AndroidJavaObject>("getScreenRectangles"));
            });

            var size = jScreenRects.Call <int>("size");

            if (size > 0)
            {
                var rectangles = new RectInt[size];
                for (var i = 0; i < size; i++)
                {
                    var jRect = jScreenRects.Call <AndroidJavaObject>("get", i);

                    var left   = jRect.Get <int>("left");
                    var top    = jRect.Get <int>("top");
                    var width  = jRect.Call <int>("width");
                    var height = jRect.Call <int>("height");

                    rectangles[i] = new RectInt(left, top, width, height);
                    Debug.LogWarning($"GetScreenRectangles [{i}]: {rectangles[i]}");
                }
                return(rectangles);
            }
            else
            {
                return(new RectInt[0]); // TODO: return null??
            }
        }
예제 #5
0
        /// <summary>
        /// Returns the rotation of the screen - Surface.ROTATION_0 (0), Surface.ROTATION_90 (1), Surface.ROTATION_180 (2), Surface.ROTATION_270 (3)
        /// </summary>
        /// <remarks>_was_ getCurrentRotation</remarks>
        public static int GetCurrentRotation()
        {
            var rotation = OnPlayer.Run(p =>
            {
                return(screenInfoObject.Call <int>("getScreenRotation")); // was getCurrentRotation
            });

            return(rotation);
        }
예제 #6
0
        /// <summary>
        /// Returns a boolean that indicates whether the application is in spanned mode or not
        /// </summary>
        public static bool IsDualMode()
        {
            var isDualMode = OnPlayer.Run(p =>
            {
                return(screenInfoObject.Call <bool>("isDualMode"));
            });

            return(isDualMode);
        }
        /// <summary>
        /// Returns the rotation of the screen - Surface.ROTATION_0 (0), Surface.ROTATION_90 (1), Surface.ROTATION_180 (2), Surface.ROTATION_270 (3)
        /// </summary>
        public static int GetCurrentRotation()
        {
            var rotation = OnPlayer.Run(p =>
            {
                var activity = p.GetStatic <AndroidJavaObject>("currentActivity");
                using (var dm = new AndroidJavaClass(SCREENHELPER_CLASSNAME))
                {
                    return(dm.CallStatic <int>("getCurrentRotation", activity));
                }
            });

            return(rotation);
        }
        /// <summary>
        /// Returns a boolean that indicates whether the application is in spanned mode or not
        /// </summary>
        public static bool IsDualMode()
        {
            var isDualMode = OnPlayer.Run(p =>
            {
                var activity = p.GetStatic <AndroidJavaObject>("currentActivity");
                using (var sc = new AndroidJavaClass(SCREENHELPER_CLASSNAME))
                {
                    return(sc.CallStatic <bool>("isDualMode", activity));
                }
            });

            return(isDualMode);
        }
예제 #9
0
        static ScreenHelper()
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            screenInfoObject = OnPlayer.Run(p =>
            {
                var activity = p.GetStatic <AndroidJavaObject>("currentActivity");
                using (var sc = new AndroidJavaClass(SCREENINFOPROVIDER_CLASSNAME))
                {
                    return(sc.CallStatic <AndroidJavaObject>("getScreenInfo", activity));
                }
            });
#endif
        }
        /// <summary>
        /// Creates the display mask according to config_mainBuiltInDisplayMaskRect.
        /// </summary>
        public static DisplayMask FromResourcesRect()
        {
            var mask = OnPlayer.Run(p =>
            {
                var context = p.GetStatic <AndroidJavaObject>("currentActivity")
                              .Call <AndroidJavaObject>("getApplicationContext");

                using (var dm = new AndroidJavaClass(DISPLAYMASK_CLASSNAME))
                {
                    return(dm.CallStatic <AndroidJavaObject>("fromResourcesRect", context));
                }
            });

            return(new DisplayMask(mask));
        }
        /// <summary>
        /// Determine whether your app is running on a dual-screen device.
        /// You should perform this check before you call APIs from the Surface Duo SDK.
        /// This method uses regular Android hasSystemFeature check, it does NOT require
        /// any custom SDK be referenced.
        /// </summary>
        /// <remarks>
        /// https://docs.microsoft.com/en-us/dual-screen/android/sample-code/is-dual-screen-device?tabs=java
        /// </remarks>
        public static bool IsDualScreenDevice()
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            try
            {
                return(OnPlayer.Run(p => p
                                    .GetStatic <AndroidJavaObject>("currentActivity")
                                    .Call <AndroidJavaObject>("getApplicationContext")
                                    .Call <AndroidJavaObject>("getPackageManager")
                                    .Call <bool>("hasSystemFeature", "com.microsoft.device.display.displaymask")));
            }
            catch
            {
                return(false);
            }
#else
            return(false);
#endif
        }
예제 #12
0
        /// <summary>
        /// Is the device a dual-screen Surface Duo?
        /// Uses the SDK ScreenHelper.isDeviceSurfaceDuo method
        /// </summary>
        public static bool IsDeviceSurfaceDuo()
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            try
            {
                var isDuo = OnPlayer.Run(p =>
                {
                    return(screenInfoObject.Call <bool>("isSurfaceDuoDevice")); // was isDeviceSurfaceDuo
                });
                return(isDuo);
            }
            catch
            {
                return(false);
            }
#else
            return(false);
#endif
        }
        /// <summary>
        /// Whether the app is spanned across both screens
        /// </summary>
        /// <remarks>
        /// https://docs.microsoft.com/en-us/dual-screen/android/sample-code/is-app-spanned?tabs=java
        /// </remarks>
        public static bool IsAppSpanned()
        {
            var isSpanned = OnPlayer.Run(p =>
            {
                var context = p.GetStatic <AndroidJavaObject>("currentActivity")
                              .Call <AndroidJavaObject>("getApplicationContext");
                using (var dm = new AndroidJavaClass(DISPLAYMASK_CLASSNAME))
                {
                    var displayMask = dm.CallStatic <AndroidJavaObject>("fromResourcesRectApproximation", context);
                    var region      = displayMask.Call <AndroidJavaObject>("getBounds");
                    var boundings   = displayMask.Call <AndroidJavaObject>("getBoundingRects");
                    var size        = boundings.Call <int>("size");
                    if (size > 0)
                    {
                        var first    = boundings.Call <AndroidJavaObject>("get", 0);
                        var rootView = p.GetStatic <AndroidJavaObject>("currentActivity")
                                       .Call <AndroidJavaObject>("getWindow")
                                       .Call <AndroidJavaObject>("getDecorView")
                                       .Call <AndroidJavaObject>("getRootView");
                        var drawingRect = new AndroidJavaObject("android.graphics.Rect");
                        rootView.Call("getDrawingRect", drawingRect);
                        if (first.Call <bool>("intersect", drawingRect))
                        {
                            Debug.LogWarning($"Dual screen - intersect");
                            return(true);
                        }
                        else
                        {
                            Debug.LogWarning($"Single screen - not intersect");
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            });

            return(isSpanned);
        }
예제 #14
0
        /// <summary>
        /// Returns the coordinates of the Hinge in a Rect object.
        /// </summary>
        public static RectInt GetHinge()
        {
            var hinge = OnPlayer.Run(p =>
            {
                return(screenInfoObject?.Call <AndroidJavaObject>("getHinge"));
            });

            if (hinge != null)
            {
                var left   = hinge.Get <int>("left");
                var top    = hinge.Get <int>("top");
                var width  = hinge.Call <int>("width");
                var height = hinge.Call <int>("height");

                return(new RectInt(left, top, width, height));
            }
            else
            {
                return(new RectInt(0, 0, 0, 0)); // TODO: cannot return null - is ok?
            }
        }
        /// <summary>
        /// Is the device a dual-screen Surface Duo?
        /// Uses the SDK ScreenHelper.isDeviceSurfaceDuo method
        /// </summary>
        public static bool IsDeviceSurfaceDuo()
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            try
            {
                var isDuo = OnPlayer.Run(p =>
                {
                    var activity = p.GetStatic <AndroidJavaObject>("currentActivity");

                    using (var dm = new AndroidJavaClass(SCREENHELPER_CLASSNAME))
                    {
                        return(dm.CallStatic <bool>("isDeviceSurfaceDuo", activity));
                    }
                });
                return(isDuo);
            }
            catch
            {
                return(false);
            }
#else
            return(false);
#endif
        }