/* * Copyright (c) Meta Platforms, Inc. and affiliates. * All rights reserved. * * Licensed under the Oculus SDK License Agreement (the "License"); * you may not use the Oculus SDK except in compliance with the License, * which is provided at the time of installation or download, or which * otherwise accompanies this software in either electronic or hard copy form. * * You may obtain a copy of the License at * * https://developer.oculus.com/licenses/oculussdk/ * * Unless required by applicable law or agreed to in writing, the Oculus SDK * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Android; /// /// This class handles android permission requests for the capabilities listed in . /// internal static class OVRPermissionsRequester { /// /// Occurs when a is granted. /// public static event Action PermissionGranted; /// /// Enum listing the capabilities this class can request permission for. /// public enum Permission { /// /// Represents the Face Tracking capability. /// FaceTracking, /// /// Represents the Body Tracking capability. /// BodyTracking, /// /// Represents the Eye Tracking capability. /// EyeTracking, /// /// Represents the Scene capability. /// Scene, } private const string FaceTrackingPermission = "com.oculus.permission.FACE_TRACKING"; private const string EyeTrackingPermission = "com.oculus.permission.EYE_TRACKING"; private const string BodyTrackingPermission = "com.oculus.permission.BODY_TRACKING"; private const string ScenePermission = "com.oculus.permission.USE_SCENE"; /// /// Returns the permission ID of the given to be requested from the user. /// /// The to get the ID of. /// /// Thrown when an invalid is used. public static string GetPermissionId(Permission permission) { return permission switch { Permission.FaceTracking => FaceTrackingPermission, Permission.BodyTracking => BodyTrackingPermission, Permission.EyeTracking => EyeTrackingPermission, Permission.Scene => ScenePermission, _ => throw new ArgumentOutOfRangeException(nameof(permission), permission, null) }; } private static bool IsPermissionSupportedByPlatform(Permission permission) { return permission switch { Permission.FaceTracking => OVRPlugin.faceTrackingSupported, Permission.BodyTracking => OVRPlugin.bodyTrackingSupported, Permission.EyeTracking => OVRPlugin.eyeTrackingSupported, // Scene is a no-op on unsupported platforms, but the request can always be made Permission.Scene => true, _ => throw new ArgumentOutOfRangeException(nameof(permission), permission, null) }; } /// /// Returns whether the has been granted. /// /// /// These permissions are Android-specific, therefore we always return /// true if on any other platform. /// /// to be checked. public static bool IsPermissionGranted(Permission permission) { #if UNITY_ANDROID && !UNITY_EDITOR return UnityEngine.Android.Permission.HasUserAuthorizedPermission(GetPermissionId(permission)); #else return true; #endif } /// /// Requests the listed . /// /// Set of to be requested. public static void Request(IEnumerable permissions) { #if UNITY_ANDROID && !UNITY_EDITOR RequestPermissions(permissions); #endif // UNITY_ANDROID && !UNITY_EDITOR } private static void RequestPermissions(IEnumerable permissions) { var permissionIdsToRequest = new List(); foreach (var permission in permissions) { if (ShouldRequestPermission(permission)) { permissionIdsToRequest.Add(GetPermissionId(permission)); } } if (permissionIdsToRequest.Count > 0) { UnityEngine.Android.Permission.RequestUserPermissions(permissionIdsToRequest.ToArray(), BuildPermissionCallbacks()); } } private static bool ShouldRequestPermission(Permission permission) { if (!IsPermissionSupportedByPlatform(permission)) { Debug.LogWarning( $"[[{nameof(OVRPermissionsRequester)}] Permission {permission} is not supported by the platform and can't be requested."); return false; } return !IsPermissionGranted(permission); } private static PermissionCallbacks BuildPermissionCallbacks() { var permissionCallbacks = new PermissionCallbacks(); permissionCallbacks.PermissionDenied += permissionId => { Debug.LogWarning($"[{nameof(OVRPermissionsRequester)}] Permission {permissionId} was denied."); }; permissionCallbacks.PermissionDeniedAndDontAskAgain += permissionId => { Debug.LogWarning( $"[{nameof(OVRPermissionsRequester)}] Permission {permissionId} was denied and blocked from being requested again."); }; permissionCallbacks.PermissionGranted += permissionId => { Debug.Log($"[{nameof(OVRPermissionsRequester)}] Permission {permissionId} was granted."); PermissionGranted?.Invoke(permissionId); }; return permissionCallbacks; } }