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.

225 lines
7.9KB

  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 have try to save a direcrtory
  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, javaString (action).get()));
  52. if (owner.startingFile != File())
  53. {
  54. if (saveMode && (! owner.startingFile.isDirectory()))
  55. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraString,
  56. javaString ("android.intent.extra.TITLE").get(),
  57. javaString (owner.startingFile.getFileName()).get());
  58. URL url (owner.startingFile);
  59. LocalRef<jobject> uri (env->CallStaticObjectMethod (Uri, Uri.parse, javaString (url.toString (true)).get()));
  60. if (uri)
  61. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraParcelable,
  62. javaString ("android.provider.extra.INITIAL_URI").get(),
  63. uri.get());
  64. }
  65. if (! selectsDirectories)
  66. {
  67. env->CallObjectMethod (intent.get(), AndroidIntent.addCategory, javaString ("android.intent.category.OPENABLE").get());
  68. auto mimeTypes = convertFiltersToMimeTypes (owner.filters);
  69. if (mimeTypes.size() == 1)
  70. {
  71. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeTypes[0]).get());
  72. }
  73. else
  74. {
  75. String mimeGroup = "*";
  76. if (mimeTypes.size() > 0)
  77. {
  78. mimeGroup = mimeTypes[0].upToFirstOccurrenceOf ("/", false, false);
  79. auto allMimeTypesHaveSameGroup = true;
  80. LocalRef<jobjectArray> jMimeTypes (env->NewObjectArray (mimeTypes.size(), JavaString, javaString("").get()));
  81. for (int i = 0; i < mimeTypes.size(); ++i)
  82. {
  83. env->SetObjectArrayElement (jMimeTypes.get(), i, javaString (mimeTypes[i]).get());
  84. if (mimeGroup != mimeTypes[0].upToFirstOccurrenceOf ("/", false, false))
  85. allMimeTypesHaveSameGroup = false;
  86. }
  87. env->CallObjectMethod (intent.get(), AndroidIntent.putExtraStrings,
  88. javaString ("android.intent.extra.MIME_TYPES").get(),
  89. jMimeTypes.get());
  90. if (! allMimeTypesHaveSameGroup)
  91. mimeGroup = "*";
  92. }
  93. env->CallObjectMethod (intent.get(), AndroidIntent.setType, javaString (mimeGroup + "/*").get());
  94. }
  95. }
  96. }
  97. else
  98. jassertfalse; // there can only be a single file chooser
  99. }
  100. ~Native()
  101. {
  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. if (currentFileChooser != nullptr)
  112. android.activity.callVoidMethod (JuceAppActivity.startActivityForResult, intent.get(), /*READ_REQUEST_CODE*/ 42);
  113. else
  114. jassertfalse; // There is already a file chooser running
  115. }
  116. void completed (int resultCode, jobject intentData)
  117. {
  118. currentFileChooser = nullptr;
  119. auto* env = getEnv();
  120. Array<URL> chosenURLs;
  121. if (resultCode == /*Activity.RESULT_OK*/ -1 && intentData != nullptr)
  122. {
  123. LocalRef<jobject> uri (env->CallObjectMethod (intentData, AndroidIntent.getData));
  124. if (uri != nullptr)
  125. {
  126. auto jStr = (jstring) env->CallObjectMethod (uri, JavaObject.toString);
  127. if (jStr != nullptr)
  128. chosenURLs.add (URL (juceString (env, jStr)));
  129. }
  130. }
  131. owner.finished (chosenURLs, true);
  132. }
  133. static Native* currentFileChooser;
  134. static StringArray convertFiltersToMimeTypes (const String& fileFilters)
  135. {
  136. StringArray result;
  137. auto wildcards = StringArray::fromTokens (fileFilters, ";", "");
  138. for (auto wildcard : wildcards)
  139. {
  140. if (wildcard.upToLastOccurrenceOf (".", false, false) == "*")
  141. {
  142. auto extension = wildcard.fromLastOccurrenceOf (".", false, false);
  143. result.addArray (getMimeTypesForFileExtension (extension));
  144. }
  145. }
  146. result.removeDuplicates (false);
  147. return result;
  148. }
  149. private:
  150. FileChooser& owner;
  151. GlobalRef intent;
  152. };
  153. FileChooser::Native* FileChooser::Native::currentFileChooser = nullptr;
  154. void juce_fileChooserCompleted (int resultCode, void* intentData)
  155. {
  156. if (FileChooser::Native::currentFileChooser != nullptr)
  157. FileChooser::Native::currentFileChooser->completed (resultCode, (jobject) intentData);
  158. }
  159. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags,
  160. FilePreviewComponent*)
  161. {
  162. return new FileChooser::Native (owner, flags);
  163. }
  164. bool FileChooser::isPlatformDialogAvailable()
  165. {
  166. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  167. return false;
  168. #else
  169. return true;
  170. #endif
  171. }
  172. } // namespace juce