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.

1169 lines
45KB

  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. extern juce::JUCEApplicationBase* juce_CreateApplication(); // (from START_JUCE_APPLICATION)
  20. namespace juce
  21. {
  22. //==============================================================================
  23. #if JUCE_PUSH_NOTIFICATIONS && JUCE_MODULE_AVAILABLE_juce_gui_extra
  24. // Returns true if the intent was handled.
  25. extern bool juce_handleNotificationIntent (void*);
  26. extern void juce_firebaseDeviceNotificationsTokenRefreshed (void*);
  27. extern void juce_firebaseRemoteNotificationReceived (void*);
  28. extern void juce_firebaseRemoteMessagesDeleted();
  29. extern void juce_firebaseRemoteMessageSent(void*);
  30. extern void juce_firebaseRemoteMessageSendError (void*, void*);
  31. #endif
  32. #if JUCE_IN_APP_PURCHASES && JUCE_MODULE_AVAILABLE_juce_product_unlocking
  33. extern void juce_inAppPurchaseCompleted (void*);
  34. #endif
  35. #if ! JUCE_DISABLE_NATIVE_FILECHOOSERS
  36. extern void juce_fileChooserCompleted (int, void*);
  37. #endif
  38. //==============================================================================
  39. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, launchApp, void, (JNIEnv* env, jobject activity,
  40. jstring appFile, jstring appDataDir))
  41. {
  42. setEnv (env);
  43. android.initialise (env, activity, appFile, appDataDir);
  44. DBG (SystemStats::getJUCEVersion());
  45. JUCEApplicationBase::createInstance = &juce_CreateApplication;
  46. initialiseJuce_GUI();
  47. if (JUCEApplicationBase* app = JUCEApplicationBase::createInstance())
  48. {
  49. if (! app->initialiseApp())
  50. exit (app->shutdownApp());
  51. }
  52. else
  53. {
  54. jassertfalse; // you must supply an application object for an android app!
  55. }
  56. jassert (MessageManager::getInstance()->isThisTheMessageThread());
  57. }
  58. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, suspendApp, void, (JNIEnv* env, jobject))
  59. {
  60. setEnv (env);
  61. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  62. app->suspended();
  63. }
  64. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, resumeApp, void, (JNIEnv* env, jobject))
  65. {
  66. setEnv (env);
  67. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  68. app->resumed();
  69. }
  70. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, quitApp, void, (JNIEnv* env, jobject))
  71. {
  72. setEnv (env);
  73. JUCEApplicationBase::appWillTerminateByForce();
  74. android.shutdown (env);
  75. }
  76. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, appActivityResult, void, (JNIEnv* env, jobject, jint requestCode, jint resultCode, jobject intentData))
  77. {
  78. setEnv (env);
  79. #if JUCE_IN_APP_PURCHASES && JUCE_MODULE_AVAILABLE_juce_product_unlocking
  80. if (requestCode == 1001)
  81. juce_inAppPurchaseCompleted (intentData);
  82. #endif
  83. #if ! JUCE_DISABLE_NATIVE_FILECHOOSERS
  84. if (requestCode == /*READ_REQUEST_CODE*/42)
  85. juce_fileChooserCompleted (resultCode, intentData);
  86. #endif
  87. ignoreUnused (intentData, requestCode);
  88. }
  89. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, appNewIntent, void, (JNIEnv* env, jobject, jobject intentData))
  90. {
  91. setEnv (env);
  92. #if JUCE_PUSH_NOTIFICATIONS && JUCE_MODULE_AVAILABLE_juce_gui_extra
  93. if (juce_handleNotificationIntent ((void *)intentData))
  94. return;
  95. // Add other functions processing intents here as needed.
  96. #else
  97. ignoreUnused (intentData);
  98. #endif
  99. }
  100. #if defined(JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME)
  101. JUCE_JNI_CALLBACK (JUCE_FIREBASE_INSTANCE_ID_SERVICE_CLASSNAME, firebaseInstanceIdTokenRefreshed, void, (JNIEnv* env, jobject /*activity*/, jstring token))
  102. {
  103. setEnv (env);
  104. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  105. juce_firebaseDeviceNotificationsTokenRefreshed (token);
  106. #else
  107. ignoreUnused (token);
  108. #endif
  109. }
  110. JUCE_JNI_CALLBACK (JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME, firebaseRemoteMessageReceived, void, (JNIEnv* env, jobject /*activity*/, jobject remoteMessage))
  111. {
  112. setEnv (env);
  113. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  114. juce_firebaseRemoteNotificationReceived (remoteMessage);
  115. #else
  116. ignoreUnused (remoteMessage);
  117. #endif
  118. }
  119. JUCE_JNI_CALLBACK (JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME, firebaseRemoteMessagesDeleted, void, (JNIEnv* env, jobject /*activity*/))
  120. {
  121. setEnv (env);
  122. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  123. juce_firebaseRemoteMessagesDeleted();
  124. #endif
  125. }
  126. JUCE_JNI_CALLBACK (JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME, firebaseRemoteMessageSent, void, (JNIEnv* env, jobject /*activity*/, jstring messageId))
  127. {
  128. setEnv (env);
  129. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  130. juce_firebaseRemoteMessageSent (messageId);
  131. #else
  132. ignoreUnused (messageId);
  133. #endif
  134. }
  135. JUCE_JNI_CALLBACK (JUCE_FIREBASE_MESSAGING_SERVICE_CLASSNAME, firebaseRemoteMessageSendError, void, (JNIEnv* env, jobject /*activity*/, jstring messageId, jstring error))
  136. {
  137. setEnv (env);
  138. #if JUCE_MODULE_AVAILABLE_juce_gui_extra
  139. juce_firebaseRemoteMessageSendError (messageId, error);
  140. #else
  141. ignoreUnused (messageId, error);
  142. #endif
  143. }
  144. #endif
  145. //==============================================================================
  146. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  147. METHOD (drawBitmap, "drawBitmap", "([IIIFFIIZLandroid/graphics/Paint;)V") \
  148. METHOD (getClipBounds, "getClipBounds", "()Landroid/graphics/Rect;")
  149. DECLARE_JNI_CLASS (CanvasMinimal, "android/graphics/Canvas");
  150. #undef JNI_CLASS_MEMBERS
  151. //==============================================================================
  152. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  153. METHOD (setViewName, "setViewName", "(Ljava/lang/String;)V") \
  154. METHOD (setVisible, "setVisible", "(Z)V") \
  155. METHOD (isVisible, "isVisible", "()Z") \
  156. METHOD (containsPoint, "containsPoint", "(II)Z") \
  157. METHOD (showKeyboard, "showKeyboard", "(Ljava/lang/String;)V") \
  158. METHOD (setSystemUiVisibilityCompat, "setSystemUiVisibilityCompat", "(I)V") \
  159. DECLARE_JNI_CLASS (ComponentPeerView, JUCE_ANDROID_ACTIVITY_CLASSPATH "$ComponentPeerView");
  160. #undef JNI_CLASS_MEMBERS
  161. //==============================================================================
  162. class AndroidComponentPeer : public ComponentPeer,
  163. private Timer
  164. {
  165. public:
  166. AndroidComponentPeer (Component& comp, const int windowStyleFlags)
  167. : ComponentPeer (comp, windowStyleFlags),
  168. fullScreen (false),
  169. navBarsHidden (false),
  170. sizeAllocated (0),
  171. scale ((float) Desktop::getInstance().getDisplays().getMainDisplay().scale)
  172. {
  173. // NB: must not put this in the initialiser list, as it invokes a callback,
  174. // which will fail if the peer is only half-constructed.
  175. view = GlobalRef (android.activity.callObjectMethod (JuceAppActivity.createNewView,
  176. (jboolean) component.isOpaque(),
  177. (jlong) this));
  178. if (isFocused())
  179. handleFocusGain();
  180. }
  181. ~AndroidComponentPeer()
  182. {
  183. if (MessageManager::getInstance()->isThisTheMessageThread())
  184. {
  185. frontWindow = nullptr;
  186. android.activity.callVoidMethod (JuceAppActivity.deleteView, view.get());
  187. }
  188. else
  189. {
  190. struct ViewDeleter : public CallbackMessage
  191. {
  192. ViewDeleter (const GlobalRef& view_) : view (view_) {}
  193. void messageCallback() override
  194. {
  195. android.activity.callVoidMethod (JuceAppActivity.deleteView, view.get());
  196. }
  197. private:
  198. GlobalRef view;
  199. };
  200. (new ViewDeleter (view))->post();
  201. }
  202. view.clear();
  203. }
  204. void* getNativeHandle() const override
  205. {
  206. return (void*) view.get();
  207. }
  208. void setVisible (bool shouldBeVisible) override
  209. {
  210. if (MessageManager::getInstance()->isThisTheMessageThread())
  211. {
  212. view.callVoidMethod (ComponentPeerView.setVisible, shouldBeVisible);
  213. }
  214. else
  215. {
  216. struct VisibilityChanger : public CallbackMessage
  217. {
  218. VisibilityChanger (const GlobalRef& view_, bool shouldBeVisible_)
  219. : view (view_), shouldBeVisible (shouldBeVisible_)
  220. {}
  221. void messageCallback() override
  222. {
  223. view.callVoidMethod (ComponentPeerView.setVisible, shouldBeVisible);
  224. }
  225. GlobalRef view;
  226. bool shouldBeVisible;
  227. };
  228. (new VisibilityChanger (view, shouldBeVisible))->post();
  229. }
  230. }
  231. void setTitle (const String& title) override
  232. {
  233. view.callVoidMethod (ComponentPeerView.setViewName, javaString (title).get());
  234. }
  235. void setBounds (const Rectangle<int>& userRect, bool isNowFullScreen) override
  236. {
  237. Rectangle<int> r = (userRect.toFloat() * scale).toNearestInt();
  238. if (MessageManager::getInstance()->isThisTheMessageThread())
  239. {
  240. fullScreen = isNowFullScreen;
  241. view.callVoidMethod (AndroidView.layout,
  242. r.getX(), r.getY(), r.getRight(), r.getBottom());
  243. }
  244. else
  245. {
  246. class ViewMover : public CallbackMessage
  247. {
  248. public:
  249. ViewMover (const GlobalRef& v, const Rectangle<int>& boundsToUse) : view (v), bounds (boundsToUse) {}
  250. void messageCallback() override
  251. {
  252. view.callVoidMethod (AndroidView.layout,
  253. bounds.getX(), bounds.getY(), bounds.getRight(), bounds.getBottom());
  254. }
  255. private:
  256. GlobalRef view;
  257. Rectangle<int> bounds;
  258. };
  259. (new ViewMover (view, r))->post();
  260. }
  261. }
  262. Rectangle<int> getBounds() const override
  263. {
  264. return (Rectangle<float> (view.callIntMethod (AndroidView.getLeft),
  265. view.callIntMethod (AndroidView.getTop),
  266. view.callIntMethod (AndroidView.getWidth),
  267. view.callIntMethod (AndroidView.getHeight)) / scale).toNearestInt();
  268. }
  269. void handleScreenSizeChange() override
  270. {
  271. ComponentPeer::handleScreenSizeChange();
  272. if (isFullScreen())
  273. setFullScreen (true);
  274. }
  275. Point<float> getScreenPosition() const
  276. {
  277. return Point<float> (view.callIntMethod (AndroidView.getLeft),
  278. view.callIntMethod (AndroidView.getTop)) / scale;
  279. }
  280. Point<float> localToGlobal (Point<float> relativePosition) override
  281. {
  282. return relativePosition + getScreenPosition();
  283. }
  284. Point<float> globalToLocal (Point<float> screenPosition) override
  285. {
  286. return screenPosition - getScreenPosition();
  287. }
  288. void setMinimised (bool /*shouldBeMinimised*/) override
  289. {
  290. // n/a
  291. }
  292. bool isMinimised() const override
  293. {
  294. return false;
  295. }
  296. bool shouldNavBarsBeHidden (bool shouldBeFullScreen) const
  297. {
  298. if (shouldBeFullScreen)
  299. if (Component* kiosk = Desktop::getInstance().getKioskModeComponent())
  300. if (kiosk->getPeer() == this)
  301. return true;
  302. return false;
  303. }
  304. void setNavBarsHidden (bool hidden)
  305. {
  306. enum
  307. {
  308. SYSTEM_UI_FLAG_VISIBLE = 0,
  309. SYSTEM_UI_FLAG_LOW_PROFILE = 1,
  310. SYSTEM_UI_FLAG_HIDE_NAVIGATION = 2,
  311. SYSTEM_UI_FLAG_FULLSCREEN = 4,
  312. SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION = 512,
  313. SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN = 1024,
  314. SYSTEM_UI_FLAG_IMMERSIVE = 2048,
  315. SYSTEM_UI_FLAG_IMMERSIVE_STICKY = 4096
  316. };
  317. view.callVoidMethod (ComponentPeerView.setSystemUiVisibilityCompat,
  318. hidden ? (jint) (SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_FULLSCREEN | SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
  319. : (jint) (SYSTEM_UI_FLAG_VISIBLE));
  320. navBarsHidden = hidden;
  321. }
  322. void setFullScreen (bool shouldBeFullScreen) override
  323. {
  324. // updating the nav bar visibility is a bit odd on Android - need to wait for
  325. if (shouldNavBarsBeHidden (shouldBeFullScreen))
  326. {
  327. if (! navBarsHidden && ! isTimerRunning())
  328. {
  329. startTimer (500);
  330. }
  331. }
  332. else
  333. {
  334. setNavBarsHidden (false);
  335. }
  336. Rectangle<int> r (shouldBeFullScreen ? Desktop::getInstance().getDisplays().getMainDisplay().userArea
  337. : lastNonFullscreenBounds);
  338. if ((! shouldBeFullScreen) && r.isEmpty())
  339. r = getBounds();
  340. // (can't call the component's setBounds method because that'll reset our fullscreen flag)
  341. if (! r.isEmpty())
  342. setBounds (r, shouldBeFullScreen);
  343. component.repaint();
  344. }
  345. bool isFullScreen() const override
  346. {
  347. return fullScreen;
  348. }
  349. void timerCallback() override
  350. {
  351. setNavBarsHidden (shouldNavBarsBeHidden (fullScreen));
  352. setFullScreen (fullScreen);
  353. stopTimer();
  354. }
  355. void setIcon (const Image& /*newIcon*/) override
  356. {
  357. // n/a
  358. }
  359. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  360. {
  361. return isPositiveAndBelow (localPos.x, component.getWidth())
  362. && isPositiveAndBelow (localPos.y, component.getHeight())
  363. && ((! trueIfInAChildWindow) || view.callBooleanMethod (ComponentPeerView.containsPoint,
  364. localPos.x * scale,
  365. localPos.y * scale));
  366. }
  367. BorderSize<int> getFrameSize() const override
  368. {
  369. // TODO
  370. return BorderSize<int>();
  371. }
  372. bool setAlwaysOnTop (bool /*alwaysOnTop*/) override
  373. {
  374. // TODO
  375. return false;
  376. }
  377. void toFront (bool makeActive) override
  378. {
  379. // Avoid calling bringToFront excessively: it's very slow
  380. if (frontWindow != this)
  381. {
  382. view.callVoidMethod (AndroidView.bringToFront);
  383. frontWindow = this;
  384. }
  385. if (makeActive)
  386. grabFocus();
  387. handleBroughtToFront();
  388. }
  389. void toBehind (ComponentPeer*) override
  390. {
  391. // TODO
  392. }
  393. //==============================================================================
  394. void handleMouseDownCallback (int index, Point<float> sysPos, int64 time)
  395. {
  396. Point<float> pos = sysPos / scale;
  397. lastMousePos = localToGlobal (pos);
  398. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  399. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons(),
  400. MouseInputSource::invalidPressure, MouseInputSource::invalidOrientation, time, {}, index);
  401. if (isValidPeer (this))
  402. handleMouseDragCallback (index, sysPos, time);
  403. }
  404. void handleMouseDragCallback (int index, Point<float> pos, int64 time)
  405. {
  406. pos /= scale;
  407. lastMousePos = localToGlobal (pos);
  408. jassert (index < 64);
  409. touchesDown = (touchesDown | (1 << (index & 63)));
  410. currentModifiers = currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  411. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier),
  412. MouseInputSource::invalidPressure, MouseInputSource::invalidOrientation, time, {}, index);
  413. }
  414. void handleMouseUpCallback (int index, Point<float> pos, int64 time)
  415. {
  416. pos /= scale;
  417. lastMousePos = localToGlobal (pos);
  418. jassert (index < 64);
  419. touchesDown = (touchesDown & ~(1 << (index & 63)));
  420. if (touchesDown == 0)
  421. currentModifiers = currentModifiers.withoutMouseButtons();
  422. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, currentModifiers.withoutMouseButtons(), MouseInputSource::invalidPressure,
  423. MouseInputSource::invalidOrientation, time, {}, index);
  424. }
  425. void handleKeyDownCallback (int k, int kc)
  426. {
  427. handleKeyPress (k, static_cast<juce_wchar> (kc));
  428. }
  429. void handleKeyUpCallback (int /*k*/, int /*kc*/)
  430. {
  431. }
  432. void handleBackButtonCallback()
  433. {
  434. if (JUCEApplicationBase* const app = JUCEApplicationBase::getInstance())
  435. app->backButtonPressed();
  436. }
  437. //==============================================================================
  438. bool isFocused() const override
  439. {
  440. if (view != nullptr)
  441. return view.callBooleanMethod (AndroidView.hasFocus);
  442. return false;
  443. }
  444. void grabFocus() override
  445. {
  446. if (view != nullptr)
  447. view.callBooleanMethod (AndroidView.requestFocus);
  448. }
  449. void handleFocusChangeCallback (bool hasFocus)
  450. {
  451. if (hasFocus)
  452. handleFocusGain();
  453. else
  454. handleFocusLoss();
  455. }
  456. static const char* getVirtualKeyboardType (TextInputTarget::VirtualKeyboardType type) noexcept
  457. {
  458. switch (type)
  459. {
  460. case TextInputTarget::textKeyboard: return "text";
  461. case TextInputTarget::numericKeyboard: return "number";
  462. case TextInputTarget::decimalKeyboard: return "numberDecimal";
  463. case TextInputTarget::urlKeyboard: return "textUri";
  464. case TextInputTarget::emailAddressKeyboard: return "textEmailAddress";
  465. case TextInputTarget::phoneNumberKeyboard: return "phone";
  466. default: jassertfalse; break;
  467. }
  468. return "text";
  469. }
  470. void textInputRequired (Point<int>, TextInputTarget& target) override
  471. {
  472. view.callVoidMethod (ComponentPeerView.showKeyboard,
  473. javaString (getVirtualKeyboardType (target.getKeyboardType())).get());
  474. }
  475. void dismissPendingTextInput() override
  476. {
  477. view.callVoidMethod (ComponentPeerView.showKeyboard, javaString ("").get());
  478. }
  479. //==============================================================================
  480. void handlePaintCallback (JNIEnv* env, jobject canvas, jobject paint)
  481. {
  482. jobject rect = env->CallObjectMethod (canvas, CanvasMinimal.getClipBounds);
  483. const int left = env->GetIntField (rect, AndroidRectClass.left);
  484. const int top = env->GetIntField (rect, AndroidRectClass.top);
  485. const int right = env->GetIntField (rect, AndroidRectClass.right);
  486. const int bottom = env->GetIntField (rect, AndroidRectClass.bottom);
  487. env->DeleteLocalRef (rect);
  488. const Rectangle<int> clip (left, top, right - left, bottom - top);
  489. const int sizeNeeded = clip.getWidth() * clip.getHeight();
  490. if (sizeAllocated < sizeNeeded)
  491. {
  492. buffer.clear();
  493. sizeAllocated = sizeNeeded;
  494. buffer = GlobalRef (env->NewIntArray (sizeNeeded));
  495. }
  496. else if (sizeNeeded == 0)
  497. {
  498. return;
  499. }
  500. if (jint* dest = env->GetIntArrayElements ((jintArray) buffer.get(), 0))
  501. {
  502. {
  503. Image temp (new PreallocatedImage (clip.getWidth(), clip.getHeight(),
  504. dest, ! component.isOpaque()));
  505. {
  506. LowLevelGraphicsSoftwareRenderer g (temp);
  507. g.setOrigin (-clip.getPosition());
  508. g.addTransform (AffineTransform::scale (scale));
  509. handlePaint (g);
  510. }
  511. }
  512. env->ReleaseIntArrayElements ((jintArray) buffer.get(), dest, 0);
  513. env->CallVoidMethod (canvas, CanvasMinimal.drawBitmap, (jintArray) buffer.get(), 0, clip.getWidth(),
  514. (jfloat) clip.getX(), (jfloat) clip.getY(),
  515. clip.getWidth(), clip.getHeight(), true, paint);
  516. }
  517. }
  518. void repaint (const Rectangle<int>& userArea) override
  519. {
  520. Rectangle<int> area = userArea * scale;
  521. if (MessageManager::getInstance()->isThisTheMessageThread())
  522. {
  523. view.callVoidMethod (AndroidView.invalidate, area.getX(), area.getY(), area.getRight(), area.getBottom());
  524. }
  525. else
  526. {
  527. struct ViewRepainter : public CallbackMessage
  528. {
  529. ViewRepainter (const GlobalRef& view_, const Rectangle<int>& area_)
  530. : view (view_), area (area_) {}
  531. void messageCallback() override
  532. {
  533. view.callVoidMethod (AndroidView.invalidate, area.getX(), area.getY(),
  534. area.getRight(), area.getBottom());
  535. }
  536. private:
  537. GlobalRef view;
  538. const Rectangle<int> area;
  539. };
  540. (new ViewRepainter (view, area))->post();
  541. }
  542. }
  543. void performAnyPendingRepaintsNow() override
  544. {
  545. // TODO
  546. }
  547. void setAlpha (float /*newAlpha*/) override
  548. {
  549. // TODO
  550. }
  551. StringArray getAvailableRenderingEngines() override
  552. {
  553. return StringArray ("Software Renderer");
  554. }
  555. //==============================================================================
  556. static ModifierKeys currentModifiers;
  557. static Point<float> lastMousePos;
  558. static int64 touchesDown;
  559. private:
  560. //==============================================================================
  561. GlobalRef view;
  562. GlobalRef buffer;
  563. bool fullScreen;
  564. bool navBarsHidden;
  565. int sizeAllocated;
  566. float scale;
  567. static AndroidComponentPeer* frontWindow;
  568. struct PreallocatedImage : public ImagePixelData
  569. {
  570. PreallocatedImage (const int width_, const int height_, jint* data_, bool hasAlpha_)
  571. : ImagePixelData (Image::ARGB, width_, height_), data (data_), hasAlpha (hasAlpha_)
  572. {
  573. if (hasAlpha_)
  574. zeromem (data_, static_cast<size_t> (width * height) * sizeof (jint));
  575. }
  576. ~PreallocatedImage()
  577. {
  578. if (hasAlpha)
  579. {
  580. PixelARGB* pix = (PixelARGB*) data;
  581. for (int i = width * height; --i >= 0;)
  582. {
  583. pix->unpremultiply();
  584. ++pix;
  585. }
  586. }
  587. }
  588. ImageType* createType() const override { return new SoftwareImageType(); }
  589. LowLevelGraphicsContext* createLowLevelContext() override { return new LowLevelGraphicsSoftwareRenderer (Image (this)); }
  590. void initialiseBitmapData (Image::BitmapData& bm, int x, int y, Image::BitmapData::ReadWriteMode /*mode*/) override
  591. {
  592. bm.lineStride = width * static_cast<int> (sizeof (jint));
  593. bm.pixelStride = static_cast<int> (sizeof (jint));
  594. bm.pixelFormat = Image::ARGB;
  595. bm.data = (uint8*) (data + x + y * width);
  596. }
  597. ImagePixelData::Ptr clone() override
  598. {
  599. PreallocatedImage* s = new PreallocatedImage (width, height, 0, hasAlpha);
  600. s->allocatedData.malloc (sizeof (jint) * static_cast<size_t> (width * height));
  601. s->data = s->allocatedData;
  602. memcpy (s->data, data, sizeof (jint) * static_cast<size_t> (width * height));
  603. return s;
  604. }
  605. private:
  606. jint* data;
  607. HeapBlock<jint> allocatedData;
  608. bool hasAlpha;
  609. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PreallocatedImage)
  610. };
  611. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AndroidComponentPeer)
  612. };
  613. ModifierKeys AndroidComponentPeer::currentModifiers = 0;
  614. Point<float> AndroidComponentPeer::lastMousePos;
  615. int64 AndroidComponentPeer::touchesDown = 0;
  616. AndroidComponentPeer* AndroidComponentPeer::frontWindow = nullptr;
  617. //==============================================================================
  618. #define JUCE_VIEW_CALLBACK(returnType, javaMethodName, params, juceMethodInvocation) \
  619. JUCE_JNI_CALLBACK (JUCE_JOIN_MACRO (JUCE_ANDROID_ACTIVITY_CLASSNAME, _00024ComponentPeerView), javaMethodName, returnType, params) \
  620. { \
  621. setEnv (env); \
  622. if (AndroidComponentPeer* peer = (AndroidComponentPeer*) (pointer_sized_uint) host) \
  623. peer->juceMethodInvocation; \
  624. }
  625. JUCE_VIEW_CALLBACK (void, handlePaint, (JNIEnv* env, jobject /*view*/, jlong host, jobject canvas, jobject paint), handlePaintCallback (env, canvas, paint))
  626. JUCE_VIEW_CALLBACK (void, handleMouseDown, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDownCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  627. JUCE_VIEW_CALLBACK (void, handleMouseDrag, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseDragCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  628. JUCE_VIEW_CALLBACK (void, handleMouseUp, (JNIEnv* env, jobject /*view*/, jlong host, jint i, jfloat x, jfloat y, jlong time), handleMouseUpCallback (i, Point<float> ((float) x, (float) y), (int64) time))
  629. JUCE_VIEW_CALLBACK (void, viewSizeChanged, (JNIEnv* env, jobject /*view*/, jlong host), handleMovedOrResized())
  630. JUCE_VIEW_CALLBACK (void, focusChanged, (JNIEnv* env, jobject /*view*/, jlong host, jboolean hasFocus), handleFocusChangeCallback (hasFocus))
  631. JUCE_VIEW_CALLBACK (void, handleKeyDown, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyDownCallback ((int) k, (int) kc))
  632. JUCE_VIEW_CALLBACK (void, handleKeyUp, (JNIEnv* env, jobject /*view*/, jlong host, jint k, jint kc), handleKeyUpCallback ((int) k, (int) kc))
  633. JUCE_VIEW_CALLBACK (void, handleBackButton, (JNIEnv* env, jobject /*view*/, jlong host), handleBackButtonCallback())
  634. //==============================================================================
  635. ComponentPeer* Component::createNewPeer (int styleFlags, void*)
  636. {
  637. return new AndroidComponentPeer (*this, styleFlags);
  638. }
  639. //==============================================================================
  640. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  641. METHOD (getRotation, "getRotation", "()I")
  642. DECLARE_JNI_CLASS (Display, "android/view/Display");
  643. #undef JNI_CLASS_MEMBERS
  644. #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD) \
  645. METHOD (getDefaultDisplay, "getDefaultDisplay", "()Landroid/view/Display;")
  646. DECLARE_JNI_CLASS (WindowManager, "android/view/WindowManager");
  647. #undef JNI_CLASS_MEMBERS
  648. bool Desktop::canUseSemiTransparentWindows() noexcept
  649. {
  650. return true;
  651. }
  652. double Desktop::getDefaultMasterScale()
  653. {
  654. return 1.0;
  655. }
  656. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  657. {
  658. enum
  659. {
  660. ROTATION_0 = 0,
  661. ROTATION_90 = 1,
  662. ROTATION_180 = 2,
  663. ROTATION_270 = 3
  664. };
  665. JNIEnv* env = getEnv();
  666. LocalRef<jstring> windowServiceString (javaString ("window"));
  667. LocalRef<jobject> windowManager = LocalRef<jobject> (env->CallObjectMethod (android.activity, JuceAppActivity.getSystemService, windowServiceString.get()));
  668. if (windowManager.get() != 0)
  669. {
  670. LocalRef<jobject> display = LocalRef<jobject> (env->CallObjectMethod (windowManager, WindowManager.getDefaultDisplay));
  671. if (display.get() != 0)
  672. {
  673. int rotation = env->CallIntMethod (display, Display.getRotation);
  674. switch (rotation)
  675. {
  676. case ROTATION_0: return upright;
  677. case ROTATION_90: return rotatedAntiClockwise;
  678. case ROTATION_180: return upsideDown;
  679. case ROTATION_270: return rotatedClockwise;
  680. }
  681. }
  682. }
  683. jassertfalse;
  684. return upright;
  685. }
  686. bool MouseInputSource::SourceList::addSource()
  687. {
  688. addSource (sources.size(), MouseInputSource::InputSourceType::touch);
  689. return true;
  690. }
  691. bool MouseInputSource::SourceList::canUseTouch()
  692. {
  693. return true;
  694. }
  695. Point<float> MouseInputSource::getCurrentRawMousePosition()
  696. {
  697. return AndroidComponentPeer::lastMousePos;
  698. }
  699. void MouseInputSource::setRawMousePosition (Point<float>)
  700. {
  701. // not needed
  702. }
  703. //==============================================================================
  704. bool KeyPress::isKeyCurrentlyDown (const int /*keyCode*/)
  705. {
  706. // TODO
  707. return false;
  708. }
  709. void ModifierKeys::updateCurrentModifiers() noexcept
  710. {
  711. currentModifiers = AndroidComponentPeer::currentModifiers;
  712. }
  713. ModifierKeys ModifierKeys::getCurrentModifiersRealtime() noexcept
  714. {
  715. return AndroidComponentPeer::currentModifiers;
  716. }
  717. JUCE_API void JUCE_CALLTYPE Process::hide()
  718. {
  719. if (android.activity.callBooleanMethod (JuceAppActivity.moveTaskToBack, true) == 0)
  720. {
  721. auto* env = getEnv();
  722. GlobalRef intent (env->NewObject (AndroidIntent, AndroidIntent.constructor));
  723. env->CallObjectMethod (intent, AndroidIntent.setAction, javaString ("android.intent.action.MAIN") .get());
  724. env->CallObjectMethod (intent, AndroidIntent.addCategory, javaString ("android.intent.category.HOME").get());
  725. android.activity.callVoidMethod (JuceAppActivity.startActivity, intent.get());
  726. }
  727. }
  728. //==============================================================================
  729. // TODO
  730. JUCE_API bool JUCE_CALLTYPE Process::isForegroundProcess() { return true; }
  731. JUCE_API void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  732. //==============================================================================
  733. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType /*iconType*/,
  734. const String& title, const String& message,
  735. Component* /*associatedComponent*/,
  736. ModalComponentManager::Callback* callback)
  737. {
  738. android.activity.callVoidMethod (JuceAppActivity.showMessageBox, javaString (title).get(),
  739. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  740. }
  741. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType /*iconType*/,
  742. const String& title, const String& message,
  743. Component* /*associatedComponent*/,
  744. ModalComponentManager::Callback* callback)
  745. {
  746. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  747. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  748. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  749. javaString (TRANS ("OK")).get(), javaString (TRANS ("Cancel")).get());
  750. return false;
  751. }
  752. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType /*iconType*/,
  753. const String& title, const String& message,
  754. Component* /*associatedComponent*/,
  755. ModalComponentManager::Callback* callback)
  756. {
  757. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  758. android.activity.callVoidMethod (JuceAppActivity.showYesNoCancelBox, javaString (title).get(),
  759. javaString (message).get(), (jlong) (pointer_sized_int) callback);
  760. return 0;
  761. }
  762. int JUCE_CALLTYPE NativeMessageBox::showYesNoBox (AlertWindow::AlertIconType /*iconType*/,
  763. const String& title, const String& message,
  764. Component* /*associatedComponent*/,
  765. ModalComponentManager::Callback* callback)
  766. {
  767. jassert (callback != nullptr); // on android, all alerts must be non-modal!!
  768. android.activity.callVoidMethod (JuceAppActivity.showOkCancelBox, javaString (title).get(),
  769. javaString (message).get(), (jlong) (pointer_sized_int) callback,
  770. javaString (TRANS ("Yes")).get(), javaString (TRANS ("No")).get());
  771. return 0;
  772. }
  773. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, alertDismissed, void, (JNIEnv* env, jobject /*activity*/,
  774. jlong callbackAsLong, jint result))
  775. {
  776. setEnv (env);
  777. if (ModalComponentManager::Callback* callback = (ModalComponentManager::Callback*) callbackAsLong)
  778. {
  779. callback->modalStateFinished (result);
  780. delete callback;
  781. }
  782. }
  783. //==============================================================================
  784. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  785. {
  786. android.activity.callVoidMethod (JuceAppActivity.setScreenSaver, isEnabled);
  787. }
  788. bool Desktop::isScreenSaverEnabled()
  789. {
  790. return android.activity.callBooleanMethod (JuceAppActivity.getScreenSaver);
  791. }
  792. //==============================================================================
  793. void Desktop::setKioskComponent (Component* kioskComp, bool enableOrDisable, bool allowMenusAndBars)
  794. {
  795. ignoreUnused (allowMenusAndBars);
  796. if (AndroidComponentPeer* peer = dynamic_cast<AndroidComponentPeer*> (kioskComp->getPeer()))
  797. peer->setFullScreen (enableOrDisable);
  798. else
  799. jassertfalse; // (this should have been checked by the caller)
  800. }
  801. //==============================================================================
  802. static jint getAndroidOrientationFlag (int orientations) noexcept
  803. {
  804. enum
  805. {
  806. SCREEN_ORIENTATION_LANDSCAPE = 0,
  807. SCREEN_ORIENTATION_PORTRAIT = 1,
  808. SCREEN_ORIENTATION_USER = 2,
  809. SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8,
  810. SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9,
  811. SCREEN_ORIENTATION_USER_LANDSCAPE = 11,
  812. SCREEN_ORIENTATION_USER_PORTRAIT = 12,
  813. };
  814. switch (orientations)
  815. {
  816. case Desktop::upright: return (jint) SCREEN_ORIENTATION_PORTRAIT;
  817. case Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_REVERSE_PORTRAIT;
  818. case Desktop::upright + Desktop::upsideDown: return (jint) SCREEN_ORIENTATION_USER_PORTRAIT;
  819. case Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_LANDSCAPE;
  820. case Desktop::rotatedClockwise: return (jint) SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
  821. case Desktop::rotatedClockwise + Desktop::rotatedAntiClockwise: return (jint) SCREEN_ORIENTATION_USER_LANDSCAPE;
  822. default: return (jint) SCREEN_ORIENTATION_USER;
  823. }
  824. }
  825. void Desktop::allowedOrientationsChanged()
  826. {
  827. android.activity.callVoidMethod (JuceAppActivity.setRequestedOrientation,
  828. getAndroidOrientationFlag (allowedOrientations));
  829. }
  830. //==============================================================================
  831. bool juce_areThereAnyAlwaysOnTopWindows()
  832. {
  833. return false;
  834. }
  835. //==============================================================================
  836. void Desktop::Displays::findDisplays (float masterScale)
  837. {
  838. Display d;
  839. d.isMain = true;
  840. d.dpi = android.dpi;
  841. d.scale = masterScale * (d.dpi / 150.);
  842. d.userArea = d.totalArea = Rectangle<int> (android.screenWidth,
  843. android.screenHeight) / d.scale;
  844. displays.add (d);
  845. }
  846. JUCE_JNI_CALLBACK (JUCE_ANDROID_ACTIVITY_CLASSNAME, setScreenSize, void, (JNIEnv* env, jobject /*activity*/,
  847. jint screenWidth, jint screenHeight,
  848. jint dpi))
  849. {
  850. setEnv (env);
  851. android.screenWidth = screenWidth;
  852. android.screenHeight = screenHeight;
  853. android.dpi = dpi;
  854. const_cast<Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
  855. }
  856. //==============================================================================
  857. Image juce_createIconForFile (const File& /*file*/)
  858. {
  859. return Image();
  860. }
  861. //==============================================================================
  862. void* CustomMouseCursorInfo::create() const { return nullptr; }
  863. void* MouseCursor::createStandardMouseCursor (const MouseCursor::StandardCursorType) { return nullptr; }
  864. void MouseCursor::deleteMouseCursor (void* const /*cursorHandle*/, const bool /*isStandard*/) {}
  865. //==============================================================================
  866. void MouseCursor::showInWindow (ComponentPeer*) const {}
  867. void MouseCursor::showInAllWindows() const {}
  868. //==============================================================================
  869. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& /*files*/, const bool /*canMove*/,
  870. Component* /*srcComp*/)
  871. {
  872. return false;
  873. }
  874. bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/, Component* /*srcComp*/)
  875. {
  876. return false;
  877. }
  878. //==============================================================================
  879. void LookAndFeel::playAlertSound()
  880. {
  881. }
  882. //==============================================================================
  883. void SystemClipboard::copyTextToClipboard (const String& text)
  884. {
  885. const LocalRef<jstring> t (javaString (text));
  886. android.activity.callVoidMethod (JuceAppActivity.setClipboardContent, t.get());
  887. }
  888. String SystemClipboard::getTextFromClipboard()
  889. {
  890. const LocalRef<jstring> text ((jstring) android.activity.callObjectMethod (JuceAppActivity.getClipboardContent));
  891. return juceString (text);
  892. }
  893. //==============================================================================
  894. const int extendedKeyModifier = 0x10000;
  895. const int KeyPress::spaceKey = ' ';
  896. const int KeyPress::returnKey = 66;
  897. const int KeyPress::escapeKey = 4;
  898. const int KeyPress::backspaceKey = 67;
  899. const int KeyPress::leftKey = extendedKeyModifier + 1;
  900. const int KeyPress::rightKey = extendedKeyModifier + 2;
  901. const int KeyPress::upKey = extendedKeyModifier + 3;
  902. const int KeyPress::downKey = extendedKeyModifier + 4;
  903. const int KeyPress::pageUpKey = extendedKeyModifier + 5;
  904. const int KeyPress::pageDownKey = extendedKeyModifier + 6;
  905. const int KeyPress::endKey = extendedKeyModifier + 7;
  906. const int KeyPress::homeKey = extendedKeyModifier + 8;
  907. const int KeyPress::deleteKey = extendedKeyModifier + 9;
  908. const int KeyPress::insertKey = -1;
  909. const int KeyPress::tabKey = 61;
  910. const int KeyPress::F1Key = extendedKeyModifier + 10;
  911. const int KeyPress::F2Key = extendedKeyModifier + 11;
  912. const int KeyPress::F3Key = extendedKeyModifier + 12;
  913. const int KeyPress::F4Key = extendedKeyModifier + 13;
  914. const int KeyPress::F5Key = extendedKeyModifier + 14;
  915. const int KeyPress::F6Key = extendedKeyModifier + 16;
  916. const int KeyPress::F7Key = extendedKeyModifier + 17;
  917. const int KeyPress::F8Key = extendedKeyModifier + 18;
  918. const int KeyPress::F9Key = extendedKeyModifier + 19;
  919. const int KeyPress::F10Key = extendedKeyModifier + 20;
  920. const int KeyPress::F11Key = extendedKeyModifier + 21;
  921. const int KeyPress::F12Key = extendedKeyModifier + 22;
  922. const int KeyPress::F13Key = extendedKeyModifier + 23;
  923. const int KeyPress::F14Key = extendedKeyModifier + 24;
  924. const int KeyPress::F15Key = extendedKeyModifier + 25;
  925. const int KeyPress::F16Key = extendedKeyModifier + 26;
  926. const int KeyPress::F17Key = extendedKeyModifier + 50;
  927. const int KeyPress::F18Key = extendedKeyModifier + 51;
  928. const int KeyPress::F19Key = extendedKeyModifier + 52;
  929. const int KeyPress::F20Key = extendedKeyModifier + 53;
  930. const int KeyPress::F21Key = extendedKeyModifier + 54;
  931. const int KeyPress::F22Key = extendedKeyModifier + 55;
  932. const int KeyPress::F23Key = extendedKeyModifier + 56;
  933. const int KeyPress::F24Key = extendedKeyModifier + 57;
  934. const int KeyPress::F25Key = extendedKeyModifier + 58;
  935. const int KeyPress::F26Key = extendedKeyModifier + 59;
  936. const int KeyPress::F27Key = extendedKeyModifier + 60;
  937. const int KeyPress::F28Key = extendedKeyModifier + 61;
  938. const int KeyPress::F29Key = extendedKeyModifier + 62;
  939. const int KeyPress::F30Key = extendedKeyModifier + 63;
  940. const int KeyPress::F31Key = extendedKeyModifier + 64;
  941. const int KeyPress::F32Key = extendedKeyModifier + 65;
  942. const int KeyPress::F33Key = extendedKeyModifier + 66;
  943. const int KeyPress::F34Key = extendedKeyModifier + 67;
  944. const int KeyPress::F35Key = extendedKeyModifier + 68;
  945. const int KeyPress::numberPad0 = extendedKeyModifier + 27;
  946. const int KeyPress::numberPad1 = extendedKeyModifier + 28;
  947. const int KeyPress::numberPad2 = extendedKeyModifier + 29;
  948. const int KeyPress::numberPad3 = extendedKeyModifier + 30;
  949. const int KeyPress::numberPad4 = extendedKeyModifier + 31;
  950. const int KeyPress::numberPad5 = extendedKeyModifier + 32;
  951. const int KeyPress::numberPad6 = extendedKeyModifier + 33;
  952. const int KeyPress::numberPad7 = extendedKeyModifier + 34;
  953. const int KeyPress::numberPad8 = extendedKeyModifier + 35;
  954. const int KeyPress::numberPad9 = extendedKeyModifier + 36;
  955. const int KeyPress::numberPadAdd = extendedKeyModifier + 37;
  956. const int KeyPress::numberPadSubtract = extendedKeyModifier + 38;
  957. const int KeyPress::numberPadMultiply = extendedKeyModifier + 39;
  958. const int KeyPress::numberPadDivide = extendedKeyModifier + 40;
  959. const int KeyPress::numberPadSeparator = extendedKeyModifier + 41;
  960. const int KeyPress::numberPadDecimalPoint = extendedKeyModifier + 42;
  961. const int KeyPress::numberPadEquals = extendedKeyModifier + 43;
  962. const int KeyPress::numberPadDelete = extendedKeyModifier + 44;
  963. const int KeyPress::playKey = extendedKeyModifier + 45;
  964. const int KeyPress::stopKey = extendedKeyModifier + 46;
  965. const int KeyPress::fastForwardKey = extendedKeyModifier + 47;
  966. const int KeyPress::rewindKey = extendedKeyModifier + 48;
  967. } // namespace juce