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.

juce_android_FileChooser.cpp 8.6KB

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