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_Messaging.cpp 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. namespace Android
  21. {
  22. class Runnable : public juce::AndroidInterfaceImplementer
  23. {
  24. public:
  25. virtual void run() = 0;
  26. private:
  27. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  28. {
  29. auto* env = getEnv();
  30. auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, JavaMethod.getName));
  31. if (methodName == "run")
  32. {
  33. run();
  34. return nullptr;
  35. }
  36. // invoke base class
  37. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  38. }
  39. };
  40. struct Handler
  41. {
  42. Handler() : nativeHandler (LocalRef<jobject> (getEnv()->NewObject (AndroidHandler, AndroidHandler.constructor))) {}
  43. ~Handler() { clearSingletonInstance(); }
  44. JUCE_DECLARE_SINGLETON (Handler, false)
  45. bool post (jobject runnable)
  46. {
  47. return (getEnv()->CallBooleanMethod (nativeHandler.get(), AndroidHandler.post, runnable) != 0);
  48. }
  49. GlobalRef nativeHandler;
  50. };
  51. JUCE_IMPLEMENT_SINGLETON (Handler)
  52. }
  53. //==============================================================================
  54. struct AndroidMessageQueue : private Android::Runnable
  55. {
  56. JUCE_DECLARE_SINGLETON_SINGLETHREADED (AndroidMessageQueue, true)
  57. AndroidMessageQueue()
  58. : self (CreateJavaInterface (this, "java/lang/Runnable"))
  59. {
  60. }
  61. ~AndroidMessageQueue() override
  62. {
  63. JUCE_ASSERT_MESSAGE_THREAD
  64. clearSingletonInstance();
  65. }
  66. bool post (MessageManager::MessageBase::Ptr&& message)
  67. {
  68. queue.add (std::move (message));
  69. // this will call us on the message thread
  70. return handler.post (self.get());
  71. }
  72. private:
  73. void run() override
  74. {
  75. for (;;)
  76. {
  77. MessageManager::MessageBase::Ptr message (queue.removeAndReturn (0));
  78. if (message == nullptr)
  79. break;
  80. message->messageCallback();
  81. }
  82. }
  83. // the this pointer to this class in Java land
  84. GlobalRef self;
  85. ReferenceCountedArray<MessageManager::MessageBase, CriticalSection> queue;
  86. Android::Handler handler;
  87. };
  88. JUCE_IMPLEMENT_SINGLETON (AndroidMessageQueue)
  89. //==============================================================================
  90. void MessageManager::doPlatformSpecificInitialisation() { AndroidMessageQueue::getInstance(); }
  91. void MessageManager::doPlatformSpecificShutdown() { AndroidMessageQueue::deleteInstance(); }
  92. //==============================================================================
  93. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool)
  94. {
  95. Logger::outputDebugString ("*** Modal loops are not possible in Android!! Exiting...");
  96. exit (1);
  97. return true;
  98. }
  99. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  100. {
  101. return AndroidMessageQueue::getInstance()->post (message);
  102. }
  103. //==============================================================================
  104. void MessageManager::broadcastMessage (const String&)
  105. {
  106. }
  107. void MessageManager::runDispatchLoop()
  108. {
  109. }
  110. void MessageManager::stopDispatchLoop()
  111. {
  112. struct QuitCallback : public CallbackMessage
  113. {
  114. QuitCallback() {}
  115. void messageCallback() override
  116. {
  117. auto* env = getEnv();
  118. LocalRef<jobject> activity (getCurrentActivity());
  119. if (activity != nullptr)
  120. {
  121. jmethodID quitMethod = env->GetMethodID (AndroidActivity, "finishAndRemoveTask", "()V");
  122. if (quitMethod != nullptr)
  123. {
  124. env->CallVoidMethod (activity.get(), quitMethod);
  125. return;
  126. }
  127. quitMethod = env->GetMethodID (AndroidActivity, "finish", "()V");
  128. jassert (quitMethod != nullptr);
  129. env->CallVoidMethod (activity.get(), quitMethod);
  130. }
  131. else
  132. {
  133. jassertfalse;
  134. }
  135. }
  136. };
  137. (new QuitCallback())->post();
  138. quitMessagePosted = true;
  139. }
  140. //==============================================================================
  141. class JuceAppLifecycle : public ActivityLifecycleCallbacks
  142. {
  143. public:
  144. JuceAppLifecycle (juce::JUCEApplicationBase* (*initSymbolAddr)())
  145. : createApplicationSymbol (initSymbolAddr)
  146. {
  147. LocalRef<jobject> appContext (getAppContext());
  148. if (appContext != nullptr)
  149. {
  150. auto* env = getEnv();
  151. myself = GlobalRef (CreateJavaInterface (this, "android/app/Application$ActivityLifecycleCallbacks"));
  152. env->CallVoidMethod (appContext.get(), AndroidApplication.registerActivityLifecycleCallbacks, myself.get());
  153. }
  154. }
  155. ~JuceAppLifecycle() override
  156. {
  157. LocalRef<jobject> appContext (getAppContext());
  158. if (appContext != nullptr && myself != nullptr)
  159. {
  160. auto* env = getEnv();
  161. clear();
  162. env->CallVoidMethod (appContext.get(), AndroidApplication.unregisterActivityLifecycleCallbacks, myself.get());
  163. myself.clear();
  164. }
  165. }
  166. void onActivityCreated (jobject, jobject) override
  167. {
  168. checkCreated();
  169. }
  170. void onActivityDestroyed (jobject activity) override
  171. {
  172. auto* env = getEnv();
  173. // if the main activity is being destroyed, only then tear-down JUCE
  174. if (env->IsSameObject (getMainActivity().get(), activity) != 0)
  175. {
  176. JUCEApplicationBase::appWillTerminateByForce();
  177. JNIClassBase::releaseAllClasses (env);
  178. jclass systemClass = (jclass) env->FindClass ("java/lang/System");
  179. jmethodID exitMethod = env->GetStaticMethodID (systemClass, "exit", "(I)V");
  180. env->CallStaticVoidMethod (systemClass, exitMethod, 0);
  181. }
  182. }
  183. void onActivityStarted (jobject) override
  184. {
  185. checkCreated();
  186. }
  187. void onActivityPaused (jobject) override
  188. {
  189. if (auto* app = JUCEApplicationBase::getInstance())
  190. app->suspended();
  191. }
  192. void onActivityResumed (jobject) override
  193. {
  194. checkInitialised();
  195. if (auto* app = JUCEApplicationBase::getInstance())
  196. app->resumed();
  197. }
  198. static JuceAppLifecycle& getInstance (juce::JUCEApplicationBase* (*initSymbolAddr)())
  199. {
  200. static JuceAppLifecycle juceAppLifecycle (initSymbolAddr);
  201. return juceAppLifecycle;
  202. }
  203. private:
  204. void checkCreated()
  205. {
  206. if (JUCEApplicationBase::getInstance() == nullptr)
  207. {
  208. DBG (SystemStats::getJUCEVersion());
  209. JUCEApplicationBase::createInstance = createApplicationSymbol;
  210. initialiseJuce_GUI();
  211. if (! JUCEApplicationBase::createInstance())
  212. jassertfalse; // you must supply an application object for an android app!
  213. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  214. }
  215. }
  216. void checkInitialised()
  217. {
  218. checkCreated();
  219. if (! hasBeenInitialised)
  220. {
  221. if (auto* app = JUCEApplicationBase::getInstance())
  222. {
  223. hasBeenInitialised = app->initialiseApp();
  224. if (! hasBeenInitialised)
  225. exit (app->shutdownApp());
  226. }
  227. }
  228. }
  229. GlobalRef myself;
  230. juce::JUCEApplicationBase* (*createApplicationSymbol)();
  231. bool hasBeenInitialised = false;
  232. };
  233. //==============================================================================
  234. File juce_getExecutableFile();
  235. void juce_juceEventsAndroidStartApp()
  236. {
  237. auto dllPath = juce_getExecutableFile().getFullPathName();
  238. auto addr = reinterpret_cast<juce::JUCEApplicationBase*(*)()> (DynamicLibrary (dllPath)
  239. .getFunction ("juce_CreateApplication"));
  240. if (addr != nullptr)
  241. JuceAppLifecycle::getInstance (addr);
  242. }
  243. } // namespace juce