juce_android_FileChooser.cpp 8.8 KB

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