PermissionsManager.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. #if UNITY_ANDROID
  4. using UnityEngine.Android;
  5. #endif
  6. using UnityEngine.Events;
  7. namespace UnityEngine.XR.Interaction.Toolkit.Samples.StarterAssets
  8. {
  9. /// <summary>
  10. /// Utility class to help define and manage Android device permissions and specify corresponding permission callbacks via <see cref="UnityEvent"/>.
  11. /// </summary>
  12. /// <remarks>
  13. /// This component is currently designed to work with Android platform permissions only.
  14. /// </remarks>
  15. [DefaultExecutionOrder(-9999)]
  16. public class PermissionsManager : MonoBehaviour
  17. {
  18. const string k_DefaultPermissionId = "com.oculus.permission.USE_SCENE";
  19. [SerializeField, Tooltip("Enables or disables the processing of permissions on Awake. If disabled, permissions will not be processed until the ProcessPermissions method is called.")]
  20. bool m_ProcessPermissionsOnAwake = true;
  21. [SerializeField, Tooltip("The system permissions to request when this component starts.")]
  22. List<PermissionRequestGroup> m_PermissionGroups = new List<PermissionRequestGroup>();
  23. /// <summary>
  24. /// Current platform permission group to process. This is determined during the <see cref="Awake"/> method using based on <see cref="XRPlatformUnderstanding"/>.
  25. /// </summary>
  26. PermissionRequestGroup m_CurrentPlatformPermissionGroup = new PermissionRequestGroup();
  27. /// <summary>
  28. /// A group of permissions to request based on a specific platform.
  29. /// </summary>
  30. [Serializable]
  31. class PermissionRequestGroup
  32. {
  33. [Tooltip("The platform type for which these permissions is intended for.")]
  34. public XRPlatformType platformType;
  35. public List<PermissionRequest> permissions;
  36. }
  37. /// <summary>
  38. /// A permission request to be made to the Android operating system.
  39. /// </summary>
  40. [Serializable]
  41. class PermissionRequest
  42. {
  43. [Tooltip("The Android system permission to request when this component starts.")]
  44. public string permissionId = k_DefaultPermissionId;
  45. [Tooltip("Whether to request permission from the operating system.")]
  46. public bool enabled = true;
  47. [HideInInspector]
  48. public bool requested = false;
  49. [HideInInspector]
  50. public bool responseReceived = false;
  51. [HideInInspector]
  52. public bool granted = false;
  53. public UnityEvent<string> onPermissionGranted;
  54. public UnityEvent<string> onPermissionDenied;
  55. }
  56. void Awake()
  57. {
  58. if (m_ProcessPermissionsOnAwake)
  59. ProcessPermissions();
  60. }
  61. /// <summary>
  62. /// Process the permissions defined in the <see cref="m_PermissionGroups"/> list.
  63. /// </summary>
  64. public void ProcessPermissions()
  65. {
  66. #if UNITY_ANDROID
  67. // Grab the current platform permission group based on the current platform in use.
  68. var currentPlatform = XRPlatformUnderstanding.CurrentPlatform;
  69. m_CurrentPlatformPermissionGroup = m_PermissionGroups.Find(g => g.platformType == currentPlatform);
  70. if (m_CurrentPlatformPermissionGroup == null)
  71. {
  72. // No permission group defined for the current platform.
  73. // No permissions will be requested by this component.
  74. return;
  75. }
  76. var permissionIds = new List<string>();
  77. // Loop through the current platform's permissions and add them to the
  78. // list of permissions to request if they are enabled and not already requested.
  79. for (var i = 0; i < m_CurrentPlatformPermissionGroup.permissions.Count; i++)
  80. {
  81. var permission = m_CurrentPlatformPermissionGroup.permissions[i];
  82. if (!permission.enabled)
  83. continue;
  84. // If permission is not granted and not requested, add it to the list of permissions to request
  85. if (!Permission.HasUserAuthorizedPermission(permission.permissionId) && !permission.requested)
  86. {
  87. permissionIds.Add(permission.permissionId);
  88. permission.requested = true;
  89. }
  90. else
  91. {
  92. Debug.Log($"User has permission for: {permission.permissionId}", this);
  93. }
  94. }
  95. // Process permissions that were not already granted
  96. if (permissionIds.Count > 0)
  97. {
  98. var callbacks = new PermissionCallbacks();
  99. callbacks.PermissionDenied += OnPermissionDenied;
  100. callbacks.PermissionGranted += OnPermissionGranted;
  101. Permission.RequestUserPermissions(permissionIds.ToArray(), callbacks);
  102. }
  103. #endif // UNITY_ANDROID
  104. }
  105. void OnPermissionGranted(string permissionStr)
  106. {
  107. // Find the permission
  108. var permission = m_CurrentPlatformPermissionGroup.permissions.Find(p => p.permissionId == permissionStr);
  109. if (permission == null)
  110. {
  111. Debug.LogWarning($"Permission granted callback received for an unexpected permission request, permission ID {permissionStr}", this);
  112. return;
  113. }
  114. // Enable permission
  115. permission.granted = true;
  116. permission.responseReceived = true;
  117. Debug.Log($"User granted permission for: {permissionStr}", this);
  118. permission.onPermissionGranted.Invoke(permissionStr);
  119. }
  120. void OnPermissionDenied(string permissionStr)
  121. {
  122. // Find the permission
  123. var permission = m_CurrentPlatformPermissionGroup.permissions.Find(p => p.permissionId == permissionStr);
  124. if (permission == null)
  125. {
  126. Debug.LogWarning($"Permission denied callback received for an unexpected permission request, permission ID {permissionStr}", this);
  127. return;
  128. }
  129. // Disable permission
  130. permission.granted = false;
  131. permission.responseReceived = true;
  132. Debug.LogWarning($"User denied permission for: {permissionStr}", this);
  133. permission.onPermissionDenied.Invoke(permissionStr);
  134. }
  135. }
  136. }