Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class FileChooser::Native : public FileChooser::Pimpl
  21. {
  22. public:
  23. //==============================================================================
  24. Native (FileChooser& fileChooser, int flags) : owner (fileChooser)
  25. {
  26. if (currentFileChooser == nullptr)
  27. {
  28. currentFileChooser = this;
  29. auto* env = getEnv();
  30. auto sdkVersion = getAndroidSDKVersion();
  31. auto saveMode = ((flags & FileBrowserComponent::saveMode) != 0);
  32. auto selectsDirectories = ((flags & FileBrowserComponent::canSelectDirectories) != 0);
  33. // You cannot save a directory
  34. jassert (! (saveMode && selectsDirectories));
  35. if (sdkVersion < 19)
  36. {
  37. // native save dialogs are only supported in Android versions >= 19
  38. jassert (! saveMode);
  39. saveMode = false;
  40. }
  41. if (sdkVersion < 21)
  42. {
  43. // native directory chooser dialogs are only supported in Android versions >= 21
  44. jassert (! selectsDirectories);
  45. selectsDirectories = false;
  46. }
  47. const char* action = (selectsDirectories ? "android.intent.action.OPEN_DOCUMENT_TREE"
  48. : (saveMode ? "android.intent.action.CREATE_DOCUMENT"
  49. : (sdkVersion >= 19 ? "android.intent.action.OPEN_DOCUMENT"
  50. : "android.intent.action.GET_CONTENT")));
  51. intent = GlobalRef (LocalRef<jobject> (env->NewObject (AndroidIntent, AndroidIntent.constructWithString,
  52. javaString (action).get())));
  53. if (owner.startingFile != File())
  54. {
  55. if (saveMode && (! owner.startingFile.isDirectory()))
  56. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraString,
  57. javaString ("android.intent.extra.TITLE").get(),
  58. javaString (owner.startingFile.getFileName()).get());
  59. URL url (owner.startingFile);
  60. LocalRef<jobject> uri (env->CallStaticObjectMethod (AndroidUri, AndroidUri.parse,
  61. javaString (url.toString (true)).get()));
  62. if (uri)
  63. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraParcelable,
  64. javaString ("android.provider.extra.INITIAL_URI").get(),
  65. uri.get());
  66. }
  67. if (! selectsDirectories)
  68. {
  69. env->CallObjectMethod (intent.get(), AndroidIntent.addCategory,
  70. javaString ("android.intent.category.OPENABLE").get());
  71. auto mimeTypes = convertFiltersToMimeTypes (owner.filters);
  72. if (mimeTypes.size() == 1)
  73. {
  74. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeTypes[0]).get());
  75. }
  76. else
  77. {
  78. String mimeGroup = "*";
  79. if (mimeTypes.size() > 0)
  80. {
  81. mimeGroup = mimeTypes[0].upToFirstOccurrenceOf ("/", false, false);
  82. auto allMimeTypesHaveSameGroup = true;
  83. LocalRef<jobjectArray> jMimeTypes (env->NewObjectArray (mimeTypes.size(), JavaString,
  84. javaString("").get()));
  85. for (int i = 0; i < mimeTypes.size(); ++i)
  86. {
  87. env->SetObjectArrayElement (jMimeTypes.get(), i, javaString (mimeTypes[i]).get());
  88. if (mimeGroup != mimeTypes[i].upToFirstOccurrenceOf ("/", false, false))
  89. allMimeTypesHaveSameGroup = false;
  90. }
  91. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraStrings,
  92. javaString ("android.intent.extra.MIME_TYPES").get(),
  93. jMimeTypes.get());
  94. if (! allMimeTypesHaveSameGroup)
  95. mimeGroup = "*";
  96. }
  97. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeGroup + "/*").get());
  98. }
  99. }
  100. }
  101. else
  102. jassertfalse; // there can only be a single file chooser
  103. }
  104. ~Native() override
  105. {
  106. masterReference.clear();
  107. currentFileChooser = nullptr;
  108. }
  109. void runModally() override
  110. {
  111. // Android does not support modal file choosers
  112. jassertfalse;
  113. }
  114. void launch() override
  115. {
  116. auto* env = getEnv();
  117. if (currentFileChooser != nullptr)
  118. {
  119. WeakReference<Native> myself (this);
  120. startAndroidActivityForResult (LocalRef<jobject> (env->NewLocalRef (intent.get())), /*READ_REQUEST_CODE*/ 42,
  121. [myself] (int requestCode, int resultCode, LocalRef<jobject> intentData) mutable
  122. {
  123. if (myself != nullptr)
  124. myself->onActivityResult (requestCode, resultCode, intentData);
  125. });
  126. }
  127. else
  128. {
  129. jassertfalse; // There is already a file chooser running
  130. }
  131. }
  132. void onActivityResult (int /*requestCode*/, int resultCode, const LocalRef<jobject>& intentData)
  133. {
  134. currentFileChooser = nullptr;
  135. auto* env = getEnv();
  136. Array<URL> chosenURLs;
  137. if (resultCode == /*Activity.RESULT_OK*/ -1 && intentData != nullptr)
  138. {
  139. LocalRef<jobject> uri (env->CallObjectMethod (intentData.get(), AndroidIntent.getData));
  140. if (uri != nullptr)
  141. {
  142. auto jStr = (jstring) env->CallObjectMethod (uri, JavaObject.toString);
  143. if (jStr != nullptr)
  144. chosenURLs.add (URL (juceString (env, jStr)));
  145. }
  146. }
  147. owner.finished (chosenURLs);
  148. }
  149. static Native* currentFileChooser;
  150. static StringArray convertFiltersToMimeTypes (const String& fileFilters)
  151. {
  152. StringArray result;
  153. auto wildcards = StringArray::fromTokens (fileFilters, ";", "");
  154. for (auto wildcard : wildcards)
  155. {
  156. if (wildcard.upToLastOccurrenceOf (".", false, false) == "*")
  157. {
  158. auto extension = wildcard.fromLastOccurrenceOf (".", false, false);
  159. result.addArray (getMimeTypesForFileExtension (extension));
  160. }
  161. }
  162. result.removeDuplicates (false);
  163. return result;
  164. }
  165. private:
  166. JUCE_DECLARE_WEAK_REFERENCEABLE (Native)
  167. FileChooser& owner;
  168. GlobalRef intent;
  169. };
  170. FileChooser::Native* FileChooser::Native::currentFileChooser = nullptr;
  171. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  172. FilePreviewComponent*)
  173. {
  174. if (FileChooser::Native::currentFileChooser == nullptr)
  175. return new FileChooser::Native (owner, flags);
  176. // there can only be one file chooser on Android at a once
  177. jassertfalse;
  178. return nullptr;
  179. }
  180. bool FileChooser::isPlatformDialogAvailable()
  181. {
  182. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  183. return false;
  184. #else
  185. return true;
  186. #endif
  187. }
  188. } // namespace juce