juce_RuntimePermissions.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_RUNTIMEPERMISSIONS_H_INCLUDED
  22. #define JUCE_RUNTIMEPERMISSIONS_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Class to handle app runtime permissions for certain functionality on some platforms.
  26. The use of this class is currently only required if the app should run on
  27. Android API level 23 and higher.
  28. On lower API levels, the permissions are specified in the app manifest. On iOS,
  29. runtime permission requests are handled automatically by the Apple APIs and not
  30. manually in the app code. On Windows, OS X, and Linux, runtime permissions are not
  31. used at all. In all these cases, request() will simply call through to the
  32. callback with no overhead and pass true (making it safe to use on all platforms).
  33. For example, to enable audio recording on Android in your cross-platform app,
  34. you could modify your code as follows:
  35. Old code:
  36. audioDeviceManager.initialise (2, 2, nullptr, true, String(), nullptr);
  37. New code:
  38. RuntimePermissions::request (
  39. RuntimePermissions::audioRecording,
  40. [this] (bool wasGranted)
  41. {
  42. if (! wasGranted)
  43. {
  44. // e.g. display an error or initialise with 0 input channels
  45. return;
  46. }
  47. audioDeviceManager.initialise (2, 2, nullptr, true, String(), nullptr);
  48. }
  49. );
  50. */
  51. class JUCE_API RuntimePermissions
  52. {
  53. public:
  54. //==============================================================================
  55. enum PermissionID
  56. {
  57. /** Permission to access the microphone (required on Android).
  58. You need to request this, for example, to initialise an AudioDeviceManager with
  59. a non-zero number of input channels, and to open the default audio input device.
  60. */
  61. recordAudio = 1,
  62. /** Permission to scan for and pair to Bluetooth MIDI devices (required on Android).
  63. You need to request this before calling BluetoothMidiDevicePairingDialogue::open(),
  64. otherwise no devices will be found.
  65. */
  66. bluetoothMidi = 2,
  67. };
  68. //==============================================================================
  69. /** Function type of runtime permission request callbacks. */
  70. #if JUCE_COMPILER_SUPPORTS_LAMBDAS
  71. typedef std::function<void (bool)> Callback;
  72. #else
  73. typedef void (*Callback) (bool);
  74. #endif
  75. //==============================================================================
  76. /** Call this method to request a runtime permission.
  77. @param permission The PermissionID of the permission you want to request.
  78. @param callback The callback to be called after the request has been granted
  79. or denied; the argument passed will be true if the permission
  80. has been granted and false otherwise.
  81. If no runtime request is required or possible to obtain the permission, the
  82. callback will be called immediately. The argument passed in will be true
  83. if the permission is granted or no permission is required on this platform,
  84. and false otherwise.
  85. If a runtime request is required to obtain the permission, the callback
  86. will be called asynchronously after the OS has granted or denied the requested
  87. permission (typically by displaying a dialog box to the user and waiting until
  88. the user has responded).
  89. */
  90. static void request (PermissionID permission, Callback callback);
  91. /** Returns whether a runtime request is required to obtain the permission
  92. on the current platform.
  93. */
  94. static bool isRequired (PermissionID permission);
  95. /** Returns true if the app has been already granted this permission, either
  96. via a previous runtime request or otherwise, or no permission is necessary.
  97. Note that this can be false even if isRequired returns false. In this case,
  98. the permission can not be obtained at all at runtime.
  99. */
  100. static bool isGranted (PermissionID permission);
  101. };
  102. #endif // JUCE_RUNTIMEPERMISSIONS_H_INCLUDED