// // /*=============================================================================== // // Copyright (C) 2025 PhantomsXR Ltd. All Rights Reserved. // // // // This file is part of the Phantom.XRMOD.OpenXRModule.Runtime. // // // // The XR-MOD cannot be copied, distributed, or made available to // // third-parties for commercial purposes without written permission of PhantomsXR Ltd. // // // // Contact nswell@phantomsxr.com for licensing requests. // // ===============================================================================*/ #if QUEST_INSTALL using System.Collections.Generic; using UnityEngine; using UnityEngine.Android; using UnityEngine.Events; namespace Phantom.XRMOD.OpenXRMOD.Runtime { public class OpenXRPermissionHelper : MonoBehaviour { public const string k_DefaultPermissionId = "com.oculus.permission.USE_SCENE"; public const string k_ScenePermission = "android.permission.SCENE_UNDERSTANDING"; public const string k_HandTrackingPermission = "android.permission.HAND_TRACKING"; public const string k_AvatarEyesPermission = "android.permission.EYE_TRACKING"; public const string k_EyeGazeFinePermisson = "android.permission.EYE_TRACKING_FINE"; #pragma warning disable CS0414 [Tooltip("Invoked when permission is denied")] public UnityEvent PermissionDenied; [Tooltip("Invoked when permission is granted")] public UnityEvent PermissionGranted; public bool IsScenePermissionGranted { get; private set; } public bool IsSceneUnderstandingPermissionGranted { get; private set; } public bool IsHandTrackingPermissionGranted { get; private set; } public bool IsEyeTrackingPermissionGranted { get; private set; } public bool IsEyeTrackingFinePermissionGranted { get; private set; } #pragma warning restore CS0414 #if UNITY_ANDROID PermissionCallbacks permissionCallbacks = new PermissionCallbacks(); void Start() { permissionCallbacks.PermissionDenied += OnPermissionDenied; permissionCallbacks.PermissionGranted += OnPermissionGranted; } /// /// Checks whether the current application has obtained this permission. /// /// The permission key that needs to be detected public void CheckPermission(string _permissionName = k_DefaultPermissionId) { if (Permission.HasUserAuthorizedPermission(_permissionName)) { OnPermissionGranted(_permissionName); } else { PermissionRequest(_permissionName); } } /// /// Checks whether the current application has obtained this permission. /// /// The permission key that needs to be detected public void CheckPermissions(string[] _permissionNames = null) { var tmp_Permissions = new List(); if (_permissionNames != null) { foreach (string tmp_PermissionName in _permissionNames) { if (!Permission.HasUserAuthorizedPermission(tmp_PermissionName)) { tmp_Permissions.Add(tmp_PermissionName); } } } PermissionsRequest(tmp_Permissions.ToArray()); } /// /// Request the permission on android platform /// /// The permission key public void PermissionRequest(string _permissionName = k_DefaultPermissionId) { Permission.RequestUserPermission(_permissionName, permissionCallbacks); } /// /// Request the permission on android platform /// /// The permission key public void PermissionsRequest(string[] _permissionName) { Permission.RequestUserPermissions(_permissionName, permissionCallbacks); } void OnPermissionDenied(string _permission) { PermissionDenied.Invoke(_permission); switch (_permission) { case k_DefaultPermissionId: IsScenePermissionGranted = false; break; case k_ScenePermission: IsSceneUnderstandingPermissionGranted = false; break; case k_HandTrackingPermission: IsHandTrackingPermissionGranted = false; break; case k_AvatarEyesPermission: IsEyeTrackingPermissionGranted = false; break; case k_EyeGazeFinePermisson: IsEyeTrackingFinePermissionGranted = false; break; } } void OnPermissionGranted(string _permission) { PermissionGranted.Invoke(_permission); switch (_permission) { case k_DefaultPermissionId: IsScenePermissionGranted = true; break; case k_ScenePermission: IsSceneUnderstandingPermissionGranted = true; break; case k_HandTrackingPermission: IsHandTrackingPermissionGranted = true; break; case k_AvatarEyesPermission: IsEyeTrackingPermissionGranted = true; break; case k_EyeGazeFinePermisson: IsEyeTrackingFinePermissionGranted = true; break; } } #endif // UNITY_ANDROID } } #endif