The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

287 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  21. METHOD (getItemCount, "getItemCount", "()I") \
  22. METHOD (getItemAt, "getItemAt", "(I)Landroid/content/ClipData$Item;")
  23. DECLARE_JNI_CLASS (ClipData, "android/content/ClipData")
  24. #undef JNI_CLASS_MEMBERS
  25. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \
  26. METHOD (getUri, "getUri", "()Landroid/net/Uri;")
  27. DECLARE_JNI_CLASS (ClipDataItem, "android/content/ClipData$Item")
  28. #undef JNI_CLASS_MEMBERS
  29. class FileChooser::Native final : public FileChooser::Pimpl
  30. {
  31. public:
  32. //==============================================================================
  33. Native (FileChooser& fileChooser, int flags) : owner (fileChooser)
  34. {
  35. if (currentFileChooser == nullptr)
  36. {
  37. currentFileChooser = this;
  38. auto* env = getEnv();
  39. auto sdkVersion = getAndroidSDKVersion();
  40. auto saveMode = ((flags & FileBrowserComponent::saveMode) != 0);
  41. auto selectsDirectories = ((flags & FileBrowserComponent::canSelectDirectories) != 0);
  42. auto canSelectMultiple = ((flags & FileBrowserComponent::canSelectMultipleItems) != 0);
  43. // You cannot save a directory
  44. jassert (! (saveMode && selectsDirectories));
  45. if (sdkVersion < 19)
  46. {
  47. // native save dialogs are only supported in Android versions >= 19
  48. jassert (! saveMode);
  49. saveMode = false;
  50. }
  51. if (sdkVersion < 21)
  52. {
  53. // native directory chooser dialogs are only supported in Android versions >= 21
  54. jassert (! selectsDirectories);
  55. selectsDirectories = false;
  56. }
  57. const char* action = (selectsDirectories ? "android.intent.action.OPEN_DOCUMENT_TREE"
  58. : (saveMode ? "android.intent.action.CREATE_DOCUMENT"
  59. : (sdkVersion >= 19 ? "android.intent.action.OPEN_DOCUMENT"
  60. : "android.intent.action.GET_CONTENT")));
  61. intent = GlobalRef (LocalRef<jobject> (env->NewObject (AndroidIntent, AndroidIntent.constructWithString,
  62. javaString (action).get())));
  63. if (owner.startingFile != File())
  64. {
  65. if (saveMode && (! owner.startingFile.isDirectory()))
  66. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraString,
  67. javaString ("android.intent.extra.TITLE").get(),
  68. javaString (owner.startingFile.getFileName()).get());
  69. URL url (owner.startingFile);
  70. LocalRef<jobject> uri (env->CallStaticObjectMethod (AndroidUri, AndroidUri.parse,
  71. javaString (url.toString (true)).get()));
  72. if (uri)
  73. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraParcelable,
  74. javaString ("android.provider.extra.INITIAL_URI").get(),
  75. uri.get());
  76. }
  77. if (canSelectMultiple && sdkVersion >= 18)
  78. {
  79. env->CallObjectMethod (intent.get(),
  80. AndroidIntent.putExtraBool,
  81. javaString ("android.intent.extra.ALLOW_MULTIPLE").get(),
  82. true);
  83. }
  84. if (! selectsDirectories)
  85. {
  86. env->CallObjectMethod (intent.get(), AndroidIntent.addCategory,
  87. javaString ("android.intent.category.OPENABLE").get());
  88. auto mimeTypes = convertFiltersToMimeTypes (owner.filters);
  89. if (mimeTypes.size() == 1)
  90. {
  91. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeTypes[0]).get());
  92. }
  93. else
  94. {
  95. String mimeGroup = "*";
  96. if (mimeTypes.size() > 0)
  97. {
  98. mimeGroup = mimeTypes[0].upToFirstOccurrenceOf ("/", false, false);
  99. auto allMimeTypesHaveSameGroup = true;
  100. LocalRef<jobjectArray> jMimeTypes (env->NewObjectArray (mimeTypes.size(), JavaString,
  101. javaString ("").get()));
  102. for (int i = 0; i < mimeTypes.size(); ++i)
  103. {
  104. env->SetObjectArrayElement (jMimeTypes.get(), i, javaString (mimeTypes[i]).get());
  105. if (mimeGroup != mimeTypes[i].upToFirstOccurrenceOf ("/", false, false))
  106. allMimeTypesHaveSameGroup = false;
  107. }
  108. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraStrings,
  109. javaString ("android.intent.extra.MIME_TYPES").get(),
  110. jMimeTypes.get());
  111. if (! allMimeTypesHaveSameGroup)
  112. mimeGroup = "*";
  113. }
  114. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeGroup + "/*").get());
  115. }
  116. }
  117. }
  118. else
  119. jassertfalse; // there can only be a single file chooser
  120. }
  121. ~Native() override
  122. {
  123. masterReference.clear();
  124. currentFileChooser = nullptr;
  125. }
  126. void runModally() override
  127. {
  128. // Android does not support modal file choosers
  129. jassertfalse;
  130. }
  131. void launch() override
  132. {
  133. auto* env = getEnv();
  134. if (currentFileChooser != nullptr)
  135. {
  136. startAndroidActivityForResult (LocalRef<jobject> (env->NewLocalRef (intent.get())), /*READ_REQUEST_CODE*/ 42,
  137. [myself = WeakReference<Native> { this }] (int requestCode, int resultCode, LocalRef<jobject> intentData) mutable
  138. {
  139. if (myself != nullptr)
  140. myself->onActivityResult (requestCode, resultCode, intentData);
  141. });
  142. }
  143. else
  144. {
  145. jassertfalse; // There is already a file chooser running
  146. }
  147. }
  148. void onActivityResult (int /*requestCode*/, int resultCode, const LocalRef<jobject>& intentData)
  149. {
  150. currentFileChooser = nullptr;
  151. auto* env = getEnv();
  152. const auto getUrls = [&]() -> Array<URL>
  153. {
  154. if (resultCode != /*Activity.RESULT_OK*/ -1 || intentData == nullptr)
  155. return {};
  156. Array<URL> chosenURLs;
  157. const auto addUrl = [env, &chosenURLs] (jobject uri)
  158. {
  159. if (auto jStr = (jstring) env->CallObjectMethod (uri, JavaObject.toString))
  160. chosenURLs.add (URL (juceString (env, jStr)));
  161. };
  162. if (LocalRef<jobject> clipData { env->CallObjectMethod (intentData.get(), AndroidIntent.getClipData) })
  163. {
  164. const auto count = env->CallIntMethod (clipData.get(), ClipData.getItemCount);
  165. for (auto i = 0; i < count; ++i)
  166. {
  167. if (LocalRef<jobject> item { env->CallObjectMethod (clipData.get(), ClipData.getItemAt, i) })
  168. {
  169. if (LocalRef<jobject> itemUri { env->CallObjectMethod (item.get(), ClipDataItem.getUri) })
  170. addUrl (itemUri.get());
  171. }
  172. }
  173. }
  174. else if (LocalRef<jobject> uri { env->CallObjectMethod (intentData.get(), AndroidIntent.getData )})
  175. {
  176. addUrl (uri.get());
  177. }
  178. return chosenURLs;
  179. };
  180. owner.finished (getUrls());
  181. }
  182. static Native* currentFileChooser;
  183. static StringArray convertFiltersToMimeTypes (const String& fileFilters)
  184. {
  185. StringArray result;
  186. auto wildcards = StringArray::fromTokens (fileFilters, ";", "");
  187. for (auto wildcard : wildcards)
  188. {
  189. if (wildcard.upToLastOccurrenceOf (".", false, false) == "*")
  190. {
  191. auto extension = wildcard.fromLastOccurrenceOf (".", false, false);
  192. result.addArray (detail::MimeTypeTable::getMimeTypesForFileExtension (extension));
  193. }
  194. }
  195. result.removeDuplicates (false);
  196. return result;
  197. }
  198. private:
  199. JUCE_DECLARE_WEAK_REFERENCEABLE (Native)
  200. FileChooser& owner;
  201. GlobalRef intent;
  202. };
  203. FileChooser::Native* FileChooser::Native::currentFileChooser = nullptr;
  204. std::shared_ptr<FileChooser::Pimpl> FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  205. FilePreviewComponent*)
  206. {
  207. if (FileChooser::Native::currentFileChooser == nullptr)
  208. return std::make_shared<FileChooser::Native> (owner, flags);
  209. // there can only be one file chooser on Android at a once
  210. jassertfalse;
  211. return nullptr;
  212. }
  213. bool FileChooser::isPlatformDialogAvailable()
  214. {
  215. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  216. return false;
  217. #else
  218. return true;
  219. #endif
  220. }
  221. void FileChooser::registerCustomMimeTypeForFileExtension (const String& mimeType,
  222. const String& fileExtension)
  223. {
  224. detail::MimeTypeTable::registerCustomMimeTypeForFileExtension (mimeType, fileExtension);
  225. }
  226. } // namespace juce