MicPermissionsManager.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #if UNITY_ANDROID && !UNITY_2020_2_OR_NEWER
  21. #define MISSING_ANDROID_PERMISSION_CALLBACK
  22. #endif
  23. using System;
  24. using System.Collections;
  25. using UnityEngine;
  26. #if UNITY_ANDROID
  27. using UnityEngine.Android;
  28. #endif
  29. #if MISSING_ANDROID_PERMISSION_CALLBACK
  30. using Meta.WitAi;
  31. #endif
  32. namespace Oculus.VoiceSDK.Utilities
  33. {
  34. public static class MicPermissionsManager
  35. {
  36. public static bool HasMicPermission()
  37. {
  38. #if UNITY_ANDROID
  39. return Permission.HasUserAuthorizedPermission(Permission.Microphone);
  40. #else
  41. return true;
  42. #endif
  43. }
  44. public static void RequestMicPermission(Action<string> permissionGrantedCallback = null)
  45. {
  46. #if UNITY_ANDROID
  47. if (HasMicPermission())
  48. {
  49. permissionGrantedCallback?.Invoke(Permission.Microphone);
  50. return;
  51. }
  52. #if MISSING_ANDROID_PERMISSION_CALLBACK
  53. Permission.RequestUserPermission(Permission.Microphone);
  54. CoroutineUtility.StartCoroutine(CheckPermissionGranted(permissionGrantedCallback));
  55. #else
  56. var callbacks = new PermissionCallbacks();
  57. callbacks.PermissionGranted += s => permissionGrantedCallback?.Invoke(s);
  58. Permission.RequestUserPermission(Permission.Microphone, callbacks);
  59. #endif
  60. #else
  61. permissionGrantedCallback?.Invoke("android.permission.RECORD_AUDIO");
  62. // Do nothing for now, but eventually we may want to handle IOS/whatever permissions here, too.
  63. #endif
  64. }
  65. #if MISSING_ANDROID_PERMISSION_CALLBACK
  66. private const int PERMISSION_CHECK_FRAMES = 3;
  67. private static IEnumerator CheckPermissionGranted(Action<string> permissionGrantedCallback)
  68. {
  69. // Exit immediately
  70. if (permissionGrantedCallback == null)
  71. {
  72. yield break;
  73. }
  74. // Wait specified amount of frames
  75. for (int i = 0; i < PERMISSION_CHECK_FRAMES; i++)
  76. {
  77. yield return new WaitForEndOfFrame();
  78. }
  79. // Successful
  80. if (HasMicPermission())
  81. {
  82. permissionGrantedCallback?.Invoke(Permission.Microphone);
  83. }
  84. }
  85. #endif
  86. }
  87. }