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 4.5KB

7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 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. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  20. METHOD (constructor, "<init>", "()V") \
  21. METHOD (post, "post", "(Ljava/lang/Runnable;)Z") \
  22. DECLARE_JNI_CLASS (JNIHandler, "android/os/Handler");
  23. #undef JNI_CLASS_MEMBERS
  24. //==============================================================================
  25. namespace Android
  26. {
  27. class Runnable : public juce::AndroidInterfaceImplementer
  28. {
  29. public:
  30. virtual void run() = 0;
  31. private:
  32. jobject invoke (jobject proxy, jobject method, jobjectArray args) override
  33. {
  34. auto* env = getEnv();
  35. auto methodName = juce::juceString ((jstring) env->CallObjectMethod (method, Method.getName));
  36. if (methodName == "run")
  37. {
  38. run();
  39. return nullptr;
  40. }
  41. // invoke base class
  42. return AndroidInterfaceImplementer::invoke (proxy, method, args);
  43. }
  44. };
  45. struct Handler
  46. {
  47. juce_DeclareSingleton (Handler, false)
  48. Handler() : nativeHandler (getEnv()->NewObject (JNIHandler, JNIHandler.constructor)) {}
  49. bool post (Runnable* runnable)
  50. {
  51. return (getEnv()->CallBooleanMethod (nativeHandler.get(), JNIHandler.post,
  52. CreateJavaInterface (runnable, "java/lang/Runnable").get()) != 0);
  53. }
  54. GlobalRef nativeHandler;
  55. };
  56. juce_ImplementSingleton (Handler);
  57. }
  58. //==============================================================================
  59. void MessageManager::doPlatformSpecificInitialisation() { Android::Handler::getInstance(); }
  60. void MessageManager::doPlatformSpecificShutdown() {}
  61. //==============================================================================
  62. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool)
  63. {
  64. Logger::outputDebugString ("*** Modal loops are not possible in Android!! Exiting...");
  65. exit (1);
  66. return true;
  67. }
  68. //==============================================================================
  69. struct AndroidMessageCallback : public Android::Runnable
  70. {
  71. AndroidMessageCallback (const MessageManager::MessageBase::Ptr& messageToDeliver)
  72. : message (messageToDeliver)
  73. {}
  74. AndroidMessageCallback (MessageManager::MessageBase::Ptr && messageToDeliver)
  75. : message (static_cast<MessageManager::MessageBase::Ptr&&> (messageToDeliver))
  76. {}
  77. void run() override
  78. {
  79. JUCE_TRY
  80. {
  81. message->messageCallback();
  82. // delete the message already here as Java will only run the
  83. // destructor of this runnable the next time the garbage
  84. // collector kicks in.
  85. message = nullptr;
  86. }
  87. JUCE_CATCH_EXCEPTION
  88. }
  89. MessageManager::MessageBase::Ptr message;
  90. };
  91. bool MessageManager::postMessageToSystemQueue (MessageManager::MessageBase* const message)
  92. {
  93. return Android::Handler::getInstance()->post (new AndroidMessageCallback (message));
  94. }
  95. //==============================================================================
  96. void MessageManager::broadcastMessage (const String&)
  97. {
  98. }
  99. void MessageManager::runDispatchLoop()
  100. {
  101. }
  102. void MessageManager::stopDispatchLoop()
  103. {
  104. struct QuitCallback : public CallbackMessage
  105. {
  106. QuitCallback() {}
  107. void messageCallback() override
  108. {
  109. android.activity.callVoidMethod (JuceAppActivity.finish);
  110. }
  111. };
  112. (new QuitCallback())->post();
  113. quitMessagePosted = true;
  114. }
  115. } // namespace juce