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.

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