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.

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