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.

229 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. class FileChooser::Native : public FileChooser::Pimpl
  22. {
  23. public:
  24. Native (FileChooser& fileChooser, int flags) : owner (fileChooser)
  25. {
  26. if (currentFileChooser == nullptr)
  27. {
  28. currentFileChooser = this;
  29. auto* env = getEnv();
  30. auto sdkVersion = env->CallStaticIntMethod (JuceAppActivity, JuceAppActivity.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 (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()
  105. {
  106. currentFileChooser = nullptr;
  107. }
  108. void runModally() override
  109. {
  110. // Android does not support modal file choosers
  111. jassertfalse;
  112. }
  113. void launch() override
  114. {
  115. if (currentFileChooser != nullptr)
  116. android.activity.callVoidMethod (JuceAppActivity.startActivityForResult, intent.get(), /*READ_REQUEST_CODE*/ 42);
  117. else
  118. jassertfalse; // There is already a file chooser running
  119. }
  120. void completed (int resultCode, jobject intentData)
  121. {
  122. currentFileChooser = nullptr;
  123. auto* env = getEnv();
  124. Array<URL> chosenURLs;
  125. if (resultCode == /*Activity.RESULT_OK*/ -1 && intentData != nullptr)
  126. {
  127. LocalRef<jobject> uri (env->CallObjectMethod (intentData, AndroidIntent.getData));
  128. if (uri != nullptr)
  129. {
  130. auto jStr = (jstring) env->CallObjectMethod (uri, JavaObject.toString);
  131. if (jStr != nullptr)
  132. chosenURLs.add (URL (juceString (env, jStr)));
  133. }
  134. }
  135. owner.finished (chosenURLs);
  136. }
  137. static Native* currentFileChooser;
  138. static StringArray convertFiltersToMimeTypes (const String& fileFilters)
  139. {
  140. StringArray result;
  141. auto wildcards = StringArray::fromTokens (fileFilters, ";", "");
  142. for (auto wildcard : wildcards)
  143. {
  144. if (wildcard.upToLastOccurrenceOf (".", false, false) == "*")
  145. {
  146. auto extension = wildcard.fromLastOccurrenceOf (".", false, false);
  147. result.addArray (getMimeTypesForFileExtension (extension));
  148. }
  149. }
  150. result.removeDuplicates (false);
  151. return result;
  152. }
  153. private:
  154. FileChooser& owner;
  155. GlobalRef intent;
  156. };
  157. FileChooser::Native* FileChooser::Native::currentFileChooser = nullptr;
  158. void juce_fileChooserCompleted (int resultCode, void* intentData)
  159. {
  160. if (FileChooser::Native::currentFileChooser != nullptr)
  161. FileChooser::Native::currentFileChooser->completed (resultCode, (jobject) intentData);
  162. }
  163. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  164. FilePreviewComponent*)
  165. {
  166. return new FileChooser::Native (owner, flags);
  167. }
  168. bool FileChooser::isPlatformDialogAvailable()
  169. {
  170. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  171. return false;
  172. #else
  173. return true;
  174. #endif
  175. }
  176. } // namespace juce