//================================================================================================================================
//
// Copyright (c) 2015-2021 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
// EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
// and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
//
//================================================================================================================================
using System;
using System.Collections.Generic;
using UnityEngine;
namespace easyar
{
///
/// Display device interface.
/// 显示设备接口。
///
public interface IDisplay
{
///
/// Device rotation.
/// 设备旋转信息。
///
int Rotation { get; }
}
///
/// Display device.
/// 显示设备。
///
internal class Display : IDisplay, IDisposable
{
private Dictionary rotations = new Dictionary();
#if UNITY_ANDROID && !UNITY_EDITOR
private static AndroidJavaObject defaultDisplay;
#endif
public Display()
{
if (Application.platform == RuntimePlatform.Android)
{
InitializeAndroid();
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
InitializeIOS();
}
}
~Display()
{
DeleteAndroidJavaObjects();
}
public int Rotation
{
get
{
if (Application.platform == RuntimePlatform.Android)
{
#if UNITY_ANDROID && !UNITY_EDITOR
var rotation = defaultDisplay.Call("getRotation");
return rotations[rotation];
#endif
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
return rotations[(int)Screen.orientation];
}
return 0;
}
}
///
/// Dispose resources.
/// 销毁资源。
///
public void Dispose()
{
DeleteAndroidJavaObjects();
GC.SuppressFinalize(this);
}
private void InitializeIOS()
{
rotations[(int)ScreenOrientation.Portrait] = 0;
rotations[(int)ScreenOrientation.LandscapeLeft] = 90;
rotations[(int)ScreenOrientation.PortraitUpsideDown] = 180;
rotations[(int)ScreenOrientation.LandscapeRight] = 270;
}
private void InitializeAndroid()
{
#if UNITY_ANDROID && !UNITY_EDITOR
using (var surfaceClass = new AndroidJavaClass("android.view.Surface"))
using (var contextClass = new AndroidJavaClass("android.content.Context"))
using (var windowService = contextClass.GetStatic("WINDOW_SERVICE"))
using (var unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var currentActivity = unityPlayerClass.GetStatic("currentActivity"))
using (var systemService = currentActivity.Call("getSystemService", windowService))
{
defaultDisplay = systemService.Call("getDefaultDisplay");
rotations[surfaceClass.GetStatic("ROTATION_0")] = 0;
rotations[surfaceClass.GetStatic("ROTATION_90")] = 90;
rotations[surfaceClass.GetStatic("ROTATION_180")] = 180;
rotations[surfaceClass.GetStatic("ROTATION_270")] = 270;
}
#endif
}
private void DeleteAndroidJavaObjects()
{
#if UNITY_ANDROID && !UNITY_EDITOR
if (defaultDisplay != null) { defaultDisplay.Dispose(); }
#endif
}
}
///
/// Display emulator.
/// Display模拟。
///
internal class DisplayEmulator : IDisplay
{
public int Rotation { get; private set; }
internal void EmulateRotation(int value)
{
Rotation = value;
}
}
}